๐Ÿ“ฆ kitten / multitars

๐Ÿ“„ tarInput.ts ยท 337 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337import { ReadableStreamLike } from './conversions';
import { ReadableStreamBlockReader } from './reader';

import {
  BLOCK_SIZE,
  CHECKSUM_INITIAL,
  TarTypeFlag,
  InternalTypeFlag,
  TarHeader,
  TarFile,
  TarChunk,
  initTarHeader,
  blockPad,
} from './tarShared';

const DECODER = new TextDecoder();

async function decodePax(
  reader: ReadableStreamBlockReader,
  gax: TarHeader | null,
  header: TarHeader
) {
  let remaining = header.size;
  let pax = '';
  while (remaining > 0) {
    let block = await reader.read();
    if (!block)
      throw new Error('Invalid Tar: Unexpected EOF while parsing PAX data');
    remaining -= block.byteLength;
    if (remaining < 0) block = block.subarray(0, remaining);
    pax += DECODER.decode(block, { stream: true });
  }
  for (let from = 0, to = 0; from < pax.length; to = 0) {
    while (to < pax.length && pax.charCodeAt(to) !== 32) to++;
    const length = parseInt(pax.slice(from, to), 10);
    if (!length || length != length) break;
    if (pax.charCodeAt(from + length - 1) !== 10) break;
    const entry = pax.slice(to + 1, from + length - 1);
    const keyIndex = entry.indexOf('=');
    if (keyIndex === -1) break;
    const key = entry.slice(0, keyIndex);
    const value = entry.slice(keyIndex + 1);
    from += length;
    switch (key) {
      case 'path':
        if (gax) gax._paxName = value;
        header._paxName = value;
        break;
      case 'linkpath':
        if (gax) gax._paxLinkName = value;
        header._paxLinkName = value;
        break;
      case 'size':
        if (gax) gax._paxSize = +value;
        header._paxSize = +value;
        break;
      case 'gid':
      case 'uid':
      case 'mode':
      case 'mtime':
        if (gax) gax[key] = +value;
        header[key] = +value;
        break;
      case 'uname':
      case 'gname':
        if (gax) gax[key] = value;
        header[key] = value;
        break;
    }
  }
}

function getTypeFlag(
  bytes: Uint8Array
): TarTypeFlag | InternalTypeFlag | ({} & number) {
  return bytes[156];
}

function checkMagic(bytes: Uint8Array): boolean {
  return (
    bytes[257] === 0x75 &&
    bytes[258] === 0x73 &&
    bytes[259] === 0x74 &&
    bytes[260] === 0x61 &&
    bytes[261] === 0x72 &&
    (bytes[262] === 0x00 || bytes[262] === 0x20)
  );
}

function checkChecksum(bytes: Uint8Array): number {
  let sum = CHECKSUM_INITIAL;
  const chksum = decodeOctal(bytes, 148, 156);
  if (chksum === sum) return sum;
  for (let idx = 0; idx < 148; idx++) sum += bytes[idx];
  for (let idx = 156; idx < 512; idx++) sum += bytes[idx];
  return sum === chksum ? sum : 0;
}

function decodeString(bytes: Uint8Array, from: number, to: number): string {
  let end = from;
  while (end < to && bytes[end] !== 0) end++;
  return end > from ? DECODER.decode(bytes.subarray(from, end)) : '';
}

async function decodeLongString(
  reader: ReadableStreamBlockReader,
  size: number
): Promise<string> {
  let remaining = size;
  let output = '';
  let endIndex = -1;
  while (remaining > 0) {
    let block = await reader.read();
    if (!block)
      throw new Error('Invalid Tar: Unexpected EOF while parsing long string');
    if (endIndex === -1) {
      // We fundamentally don't trust that the length is accurate, and we cut off
      // any strings we parse at the trailing zero byte
      endIndex = block.indexOf(0);
      if (endIndex > -1) block = block.subarray(0, endIndex);
      output += DECODER.decode(block, { stream: true });
    }
    break;
  }
  output += DECODER.decode();
  return output;
}

function decodeOctal(bytes: Uint8Array, from: number, to: number): number {
  const end = to - 1;
  let val = 0;
  let idx = to;
  if (bytes[from] === 0x80) {
    while (idx-- > from + 1) val += bytes[idx] * 256 ** (end - idx);
    return val;
  } else if (bytes[from] === 0xff) {
    let flipped = false;
    while (idx-- > from) {
      const f = flipped
        ? 0xff ^ bytes[idx] // ones comp
        : (0xff ^ bytes[idx]) + 1; // twos comp
      val -= (f & 0xff) * 256 ** (end - idx);
      flipped ||= bytes[idx] !== 0;
    }
    return val;
  } else {
    idx = from;
    while (idx < to && (bytes[idx] === 32 || bytes[idx] === 0)) idx++;
    if (end !== idx) val = parseInt(decodeString(bytes, idx, to), 8);
    return val == val ? val || 0 : 0;
  }
}

function decodeBase(header: TarHeader, buffer: Uint8Array): void {
  /*
  | Field Name | Offset | Length | Field Type                 |
  | ---------- | ------ | ------ | -------------------------- |
  | name       | 0 B    | 100 B  | NUL-terminated if NUL fits |
  | mode       | 100 B  | 8 B    |                            |
  | uid        | 108 B  | 8 B    |                            |
  | gid        | 116 B  | 8 B    |                            |
  | size       | 124 B  | 12 B   |                            |
  | mtime      | 136 B  | 12 B   |                            |
  | chksum     | 148 B  | 8 B    |                            |
  | typeflag   | 156 B  | 1 B    | see below                  |
  | linkname   | 157 B  | 100 B  | NUL-terminated if NUL fits |
  | magic      | 257 B  | 6 B    | must be TMAGIC (NUL term.) |
  | version    | 263 B  | 2 B    | must be TVERSION           |
  | uname      | 265 B  | 32 B   | NUL-terminated             |
  | gname      | 297 B  | 32 B   | NUL-terminated             |
  | devmajor   | 329 B  | 8 B    |                            |
  | devminor   | 337 B  | 8 B    |                            |
  | prefix     | 345 B  | 155 B  | NUL-terminated if NUL fits |
  */
  header.name = decodeString(buffer, 0, 100);
  header.mode ||= decodeOctal(buffer, 100, 108);
  header.uid ||= decodeOctal(buffer, 108, 116);
  header.gid ||= decodeOctal(buffer, 116, 124);
  header.size = decodeOctal(buffer, 124, 136);
  header.mtime ||= decodeOctal(buffer, 136, 148);
  // CHECKSUM: 148 - 156
  header.typeflag = getTypeFlag(buffer);
  header.linkname = decodeString(buffer, 157, 257) || null;
  // TMAGIC: 257 - 263
  // TVERSION: 263 - 265
  header.uname ||= decodeString(buffer, 265, 297) || null;
  header.gname ||= decodeString(buffer, 297, 329) || null;
  header.devmajor = decodeOctal(buffer, 329, 337);
  header.devminor = decodeOctal(buffer, 337, 345);

  if (buffer[345] !== 0) {
    header._prefix = decodeString(buffer, 345, 500);
  }
  if (
    header.typeflag === InternalTypeFlag.OLD_FILE &&
    header.name.endsWith('/')
  ) {
    header.typeflag = TarTypeFlag.DIRECTORY;
  }
}

async function decodeHeader(
  reader: ReadableStreamBlockReader,
  gax: TarHeader
): Promise<TarHeader | undefined> {
  let buffer: Uint8Array | null;
  let header = initTarHeader(gax);
  while ((buffer = await reader.read()) && checkMagic(buffer)) {
    switch (getTypeFlag(buffer)) {
      case InternalTypeFlag.LONG_NAME:
      case InternalTypeFlag.OLD_LONG_NAME:
        decodeBase(header, buffer);
        header._longName = await decodeLongString(reader, header.size);
        continue;

      case InternalTypeFlag.LONG_LINK_NAME:
        decodeBase(header, buffer);
        header._longLinkName = await decodeLongString(reader, header.size);
        continue;

      case InternalTypeFlag.GAX:
        decodeBase(header, buffer);
        await decodePax(reader, gax, header);
        continue;

      case InternalTypeFlag.PAX:
        decodeBase(header, buffer);
        await decodePax(reader, null, header);
        continue;

      case InternalTypeFlag.OLD_FILE:
      case InternalTypeFlag.CONTIGUOUS_FILE:
      case TarTypeFlag.FILE:
      case TarTypeFlag.LINK:
      case TarTypeFlag.SYMLINK:
      case TarTypeFlag.DIRECTORY:
        decodeBase(header, buffer);
        return header;

      default:
        // Usually we don't check the checksum, but if we don't know the type, we should
        // double check it. If it's invalid we can be sure we have an invalid block
        if (!checkChecksum(buffer)) {
          throw new Error(
            'Invalid Tar: Unexpected block with invalid checksum'
          );
        }
        decodeBase(header, buffer);
        return header;
    }
  }
  for (let idx = 0; buffer && idx < buffer.byteLength; idx++)
    if (buffer[idx] !== 0)
      throw new Error('Invalid Tar: Unexpected non-header block');
  return undefined;
}

// NOTE(@kitten): We don't really want to copy but something isn't applying backpressure correctly
function copyUint8Array(src: Uint8Array) {
  const dst = new Uint8Array(src.byteLength);
  dst.set(src);
  return dst;
}

/** Provide tar entry iterator */
export async function* untar(
  stream: ReadableStreamLike<Uint8Array>
): AsyncGenerator<TarFile | TarChunk> {
  const gax = initTarHeader(null);
  const reader = new ReadableStreamBlockReader(stream, BLOCK_SIZE);
  const streamParams = new ByteLengthQueuingStrategy({ highWaterMark: 0 });

  let header: TarHeader | undefined;
  while ((header = await decodeHeader(reader, gax)) != null) {
    const pad = blockPad(header.size);
    let consumedTrailer = pad === 0;
    let remaining = header._paxSize || header.size;
    let cancel: () => Promise<void>;
    const stream = new ReadableStream(
      {
        // NOTE(@kitten): This is needed in Cloudflare to attach the expected size to the stream
        expectedLength: header.size,
        cancel: (cancel = async function cancel() {
          if (!consumedTrailer) {
            consumedTrailer = true;
            remaining += pad;
          }
          if (remaining > 0) {
            const skipped = await reader.skip(remaining);
            if (skipped > 0) throw new Error('Invalid Tar: Unexpected EOF');
            remaining = 0;
          }
        }),
        async pull(controller) {
          if (remaining) {
            const buffer = await reader.pull(remaining);
            if (!buffer) throw new Error('Invalid Tar: Unexpected EOF');
            remaining -= buffer.byteLength;
            controller.enqueue(copyUint8Array(buffer));
          }
          if (!remaining) {
            if (!consumedTrailer) {
              consumedTrailer = true;
              const skipped = await reader.skip(pad);
              if (skipped > 0) throw new Error('Invalid Tar: Unexpected EOF');
            }
            controller.close();
          }
        },
      },
      streamParams
    );

    let chunk: TarFile | TarChunk;
    switch (header.typeflag) {
      case InternalTypeFlag.OLD_FILE:
      case InternalTypeFlag.CONTIGUOUS_FILE:
      case TarTypeFlag.FILE:
        chunk = new TarFile(stream, header);
        break;
      case TarTypeFlag.LINK:
      case TarTypeFlag.SYMLINK:
      case TarTypeFlag.DIRECTORY:
        chunk = new TarChunk(stream, header);
        break;
      default:
        await cancel();
        continue;
    }

    yield chunk;
    if (remaining > 0 || !consumedTrailer) {
      await (stream.locked ? cancel() : stream.cancel());
    }
  }
}