|
| 1 | +// Package bundle provides primitives for bundling apps for portability. |
| 2 | +package bundle |
| 3 | + |
| 4 | +import ( |
| 5 | + "archive/tar" |
| 6 | + "bytes" |
| 7 | + "compress/gzip" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "io/fs" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "slices" |
| 14 | + |
| 15 | + "tidbyt.dev/pixlet/manifest" |
| 16 | + "tidbyt.dev/pixlet/runtime" |
| 17 | +) |
| 18 | + |
| 19 | +type WriteOption interface{} |
| 20 | + |
| 21 | +type withoutRuntimeOption struct{} |
| 22 | + |
| 23 | +// WithoutRuntime is a WriteOption that can be used to write the bundle without |
| 24 | +// using the runtime to determine the files to include in the bundle. Instead, |
| 25 | +// all files in the source FS will be included in the bundle. |
| 26 | +// |
| 27 | +// This is useful when writing a bundle that is known not to contain any |
| 28 | +// unnecessary files, when loading and rewriting a bundle that was already |
| 29 | +// tree-shaken, or when loading the entire runtime is not possible for |
| 30 | +// performance or security reasons. |
| 31 | +func WithoutRuntime() WriteOption { |
| 32 | + return withoutRuntimeOption{} |
| 33 | +} |
| 34 | + |
| 35 | +// WriteBundleToPath is a helper to be able to write the bundle to a provided |
| 36 | +// directory. |
| 37 | +func (b *AppBundle) WriteBundleToPath(dir string, opts ...WriteOption) error { |
| 38 | + path := filepath.Join(dir, AppBundleName) |
| 39 | + f, err := os.Create(path) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("could not create file for bundle: %w", err) |
| 42 | + } |
| 43 | + defer f.Close() |
| 44 | + |
| 45 | + return b.WriteBundle(f, opts...) |
| 46 | +} |
| 47 | + |
| 48 | +// WriteBundle writes a compressed archive to the provided writer. |
| 49 | +func (ab *AppBundle) WriteBundle(out io.Writer, opts ...WriteOption) error { |
| 50 | + var bundleFiles []string |
| 51 | + |
| 52 | + if slices.Contains(opts, WithoutRuntime()) { |
| 53 | + // we can't use the runtime to determine the files to include in the |
| 54 | + // bundle, so we'll just include everything in the source FS. |
| 55 | + err := fs.WalkDir(ab.Source, ".", func(path string, d fs.DirEntry, err error) error { |
| 56 | + if err != nil { |
| 57 | + return fmt.Errorf("walking directory: %w", err) |
| 58 | + } |
| 59 | + if !d.IsDir() { |
| 60 | + bundleFiles = append(bundleFiles, path) |
| 61 | + } |
| 62 | + return nil |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("walking source FS: %w", err) |
| 66 | + } |
| 67 | + } else { |
| 68 | + // we don't want to naively write the entire source FS to the tarball, |
| 69 | + // since it could contain a lot of extraneous files. instead, run the |
| 70 | + // applet and interrogate it for the files it needs to include in the |
| 71 | + // bundle. |
| 72 | + app, err := runtime.NewAppletFromFS(ab.Manifest.ID, ab.Source, runtime.WithPrintDisabled()) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("loading applet for bundling: %w", err) |
| 75 | + } |
| 76 | + bundleFiles = app.PathsForBundle() |
| 77 | + } |
| 78 | + |
| 79 | + // Setup writers. |
| 80 | + gzw := gzip.NewWriter(out) |
| 81 | + defer gzw.Close() |
| 82 | + |
| 83 | + tw := tar.NewWriter(gzw) |
| 84 | + defer tw.Close() |
| 85 | + |
| 86 | + // Write manifest. |
| 87 | + buff := &bytes.Buffer{} |
| 88 | + err := ab.Manifest.WriteManifest(buff) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("could not write manifest to buffer: %w", err) |
| 91 | + } |
| 92 | + b := buff.Bytes() |
| 93 | + |
| 94 | + hdr := &tar.Header{ |
| 95 | + Name: manifest.ManifestFileName, |
| 96 | + Mode: 0600, |
| 97 | + Size: int64(len(b)), |
| 98 | + } |
| 99 | + err = tw.WriteHeader(hdr) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("could not write manifest header: %w", err) |
| 102 | + } |
| 103 | + _, err = tw.Write(b) |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("could not write manifest to archive: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + // write sources. |
| 109 | + for _, path := range bundleFiles { |
| 110 | + stat, err := fs.Stat(ab.Source, path) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("could not stat %s: %w", path, err) |
| 113 | + } |
| 114 | + |
| 115 | + hdr, err := tar.FileInfoHeader(stat, "") |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("creating header for %s: %w", path, err) |
| 118 | + } |
| 119 | + hdr.Name = filepath.ToSlash(path) |
| 120 | + |
| 121 | + err = tw.WriteHeader(hdr) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("writing header for %s: %w", path, err) |
| 124 | + } |
| 125 | + |
| 126 | + if !stat.IsDir() { |
| 127 | + file, err := ab.Source.Open(path) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("opening file %s: %w", path, err) |
| 130 | + } |
| 131 | + |
| 132 | + written, err := io.Copy(tw, file) |
| 133 | + if err != nil { |
| 134 | + return fmt.Errorf("writing file %s: %w", path, err) |
| 135 | + } else if written != stat.Size() { |
| 136 | + return fmt.Errorf("did not write entire file %s: %w", path, err) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
0 commit comments