|
| 1 | +import { Decompress as ZstdDecompress } from "fzstd"; |
| 2 | +import { Transform } from "stream"; |
| 3 | +import { createGunzip } from "zlib"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Creates a transform stream that automatically detects and decompresses data based on magic numbers. |
| 7 | + * |
| 8 | + * Supports three formats: |
| 9 | + * - gzip (magic: 1f 8b) - Streamed through Node.js built-in zlib |
| 10 | + * - zstd (magic: 28 b5 2f fd) - Streamed through fzstd library |
| 11 | + * - uncompressed - Passed through unchanged |
| 12 | + * |
| 13 | + * Both gzip and zstd use streaming decompression to avoid buffering entire layers in memory. |
| 14 | + * This is critical for handling large image layers (multiple GB) without excessive memory usage. |
| 15 | + * |
| 16 | + * OCI images from containerd may use zstd compression, while older Docker archives use gzip. |
| 17 | + * Manifest and config files within OCI archives are typically uncompressed JSON. |
| 18 | + * |
| 19 | + * Named after the gunzip-maybe library, which only handled gzip detection. |
| 20 | + */ |
| 21 | +export function decompressMaybe(): Transform { |
| 22 | + let headerRead = false; |
| 23 | + let compressionType: "gzip" | "zstd" | "none" | null = null; |
| 24 | + let gzipStream: Transform | null = null; |
| 25 | + let zstdStream: ZstdDecompress | null = null; |
| 26 | + const buffer: Buffer[] = []; |
| 27 | + |
| 28 | + const transform = new Transform({ |
| 29 | + transform(chunk: Buffer, _encoding, callback) { |
| 30 | + if (!headerRead) { |
| 31 | + buffer.push(chunk); |
| 32 | + const combined = Buffer.concat(buffer); |
| 33 | + |
| 34 | + // Check for gzip magic number (1f 8b) |
| 35 | + if ( |
| 36 | + combined.length >= 2 && |
| 37 | + combined[0] === 0x1f && |
| 38 | + combined[1] === 0x8b |
| 39 | + ) { |
| 40 | + compressionType = "gzip"; |
| 41 | + headerRead = true; |
| 42 | + |
| 43 | + // Setup gzip decompressor |
| 44 | + gzipStream = createGunzip(); |
| 45 | + gzipStream.on("data", (data: Buffer) => transform.push(data)); |
| 46 | + gzipStream.on("error", (err: Error) => transform.destroy(err)); |
| 47 | + |
| 48 | + // Write buffered data |
| 49 | + gzipStream.write(combined); |
| 50 | + buffer.length = 0; |
| 51 | + callback(); |
| 52 | + } |
| 53 | + // Check for zstd magic number (28 b5 2f fd) |
| 54 | + else if ( |
| 55 | + combined.length >= 4 && |
| 56 | + combined[0] === 0x28 && |
| 57 | + combined[1] === 0xb5 && |
| 58 | + combined[2] === 0x2f && |
| 59 | + combined[3] === 0xfd |
| 60 | + ) { |
| 61 | + compressionType = "zstd"; |
| 62 | + headerRead = true; |
| 63 | + |
| 64 | + // Setup zstd decompressor with streaming API |
| 65 | + zstdStream = new ZstdDecompress( |
| 66 | + (data: Uint8Array, final?: boolean) => { |
| 67 | + transform.push(Buffer.from(data)); |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + // Write buffered data |
| 72 | + try { |
| 73 | + zstdStream.push(new Uint8Array(combined), false); |
| 74 | + } catch (err) { |
| 75 | + callback( |
| 76 | + new Error( |
| 77 | + `zstd decompression failed: ${ |
| 78 | + err instanceof Error ? err.message : String(err) |
| 79 | + }`, |
| 80 | + ), |
| 81 | + ); |
| 82 | + return; |
| 83 | + } |
| 84 | + buffer.length = 0; |
| 85 | + callback(); |
| 86 | + } |
| 87 | + // After 8 bytes, assume uncompressed |
| 88 | + else if (combined.length >= 8) { |
| 89 | + compressionType = "none"; |
| 90 | + headerRead = true; |
| 91 | + |
| 92 | + // Push buffered data as-is |
| 93 | + this.push(combined); |
| 94 | + buffer.length = 0; |
| 95 | + callback(); |
| 96 | + } else { |
| 97 | + // Need more data |
| 98 | + callback(); |
| 99 | + } |
| 100 | + } else { |
| 101 | + // Header already read |
| 102 | + if (compressionType === "gzip" && gzipStream) { |
| 103 | + gzipStream.write(chunk); |
| 104 | + callback(); |
| 105 | + } else if (compressionType === "zstd" && zstdStream) { |
| 106 | + try { |
| 107 | + zstdStream.push(new Uint8Array(chunk), false); |
| 108 | + callback(); |
| 109 | + } catch (err) { |
| 110 | + callback( |
| 111 | + new Error( |
| 112 | + `zstd decompression failed: ${ |
| 113 | + err instanceof Error ? err.message : String(err) |
| 114 | + }`, |
| 115 | + ), |
| 116 | + ); |
| 117 | + } |
| 118 | + } else { |
| 119 | + // No compression |
| 120 | + callback(null, chunk); |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + |
| 125 | + async flush(callback) { |
| 126 | + if (compressionType === "gzip" && gzipStream) { |
| 127 | + gzipStream.once("end", () => callback()); |
| 128 | + gzipStream.end(); |
| 129 | + } else if (compressionType === "zstd" && zstdStream) { |
| 130 | + // Signal end of zstd stream |
| 131 | + try { |
| 132 | + zstdStream.push(new Uint8Array(0), true); |
| 133 | + callback(); |
| 134 | + } catch (err) { |
| 135 | + callback( |
| 136 | + new Error( |
| 137 | + `zstd decompression failed: ${ |
| 138 | + err instanceof Error ? err.message : String(err) |
| 139 | + }`, |
| 140 | + ), |
| 141 | + ); |
| 142 | + } |
| 143 | + } else if (!headerRead && buffer.length > 0) { |
| 144 | + // Stream ended before determining compression, assume uncompressed |
| 145 | + this.push(Buffer.concat(buffer)); |
| 146 | + callback(); |
| 147 | + } else { |
| 148 | + callback(); |
| 149 | + } |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + return transform; |
| 154 | +} |
0 commit comments