summaryrefslogtreecommitdiff
path: root/op_crates/file/01_file.js
blob: 23886fbd5135001f44b0b7a139bbae5ab3fbc682 (plain)
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
337
338
339
340
341
342
343
344
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

// @ts-check
/// <reference no-default-lib="true" />
/// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../web/internal.d.ts" />
/// <reference path="../web/lib.deno_web.d.ts" />
/// <reference path="./internal.d.ts" />
/// <reference path="./lib.deno_file.d.ts" />
/// <reference lib="esnext" />
"use strict";

((window) => {
  // TODO(lucacasonato): this needs to not be hardcoded and instead depend on
  // host os.
  const isWindows = false;

  /**
   * @param {string} input
   * @param {number} position
   * @returns {{result: string, position: number}}
   */
  function collectCodepointsNotCRLF(input, position) {
    // See https://w3c.github.io/FileAPI/#convert-line-endings-to-native and
    // https://infra.spec.whatwg.org/#collect-a-sequence-of-code-points
    const start = position;
    for (
      let c = input.charAt(position);
      position < input.length && !(c === "\r" || c === "\n");
      c = input.charAt(++position)
    );
    return { result: input.slice(start, position), position };
  }

  /**
   * @param {string} s
   * @returns {string}
   */
  function convertLineEndingsToNative(s) {
    const nativeLineEnding = isWindows ? "\r\n" : "\n";

    let { result, position } = collectCodepointsNotCRLF(s, 0);

    while (position < s.length) {
      const codePoint = s.charAt(position);
      if (codePoint === "\r") {
        result += nativeLineEnding;
        position++;
        if (position < s.length && s.charAt(position) === "\n") {
          position++;
        }
      } else if (codePoint === "\n") {
        position++;
        result += nativeLineEnding;
      }
      const { result: token, position: newPosition } = collectCodepointsNotCRLF(
        s,
        position,
      );
      position = newPosition;
      result += token;
    }

    return result;
  }

  /**
   * @param  {...Uint8Array} bytesArrays
   * @returns {Uint8Array} 
   */
  function concatUint8Arrays(...bytesArrays) {
    let byteLength = 0;
    for (const bytes of bytesArrays) {
      byteLength += bytes.byteLength;
    }
    const finalBytes = new Uint8Array(byteLength);
    let current = 0;
    for (const bytes of bytesArrays) {
      finalBytes.set(bytes, current);
      current += bytes.byteLength;
    }
    return finalBytes;
  }

  const utf8Encoder = new TextEncoder();
  const utf8Decoder = new TextDecoder();

  /** @typedef {BufferSource | Blob | string} BlobPart */

  /** 
     * @param {BlobPart[]} parts
     * @param {string} endings
     * @returns {Uint8Array}
     */
  function processBlobParts(parts, endings) {
    /** @type {Uint8Array[]} */
    const bytesArrays = [];
    for (const element of parts) {
      if (element instanceof ArrayBuffer) {
        bytesArrays.push(new Uint8Array(element.slice(0)));
      } else if (ArrayBuffer.isView(element)) {
        const buffer = element.buffer.slice(
          element.byteOffset,
          element.byteOffset + element.byteLength,
        );
        bytesArrays.push(new Uint8Array(buffer));
      } else if (element instanceof Blob) {
        bytesArrays.push(
          new Uint8Array(element[_byteSequence].buffer.slice(0)),
        );
      } else if (typeof element === "string") {
        let s = element;
        if (endings == "native") {
          s = convertLineEndingsToNative(s);
        }
        bytesArrays.push(utf8Encoder.encode(s));
      } else {
        throw new TypeError("Unreachable code (invalild element type)");
      }
    }
    return concatUint8Arrays(...bytesArrays);
  }

  /**
   * @param {string} str 
   * @returns {string}
   */
  function normalizeType(str) {
    let normalizedType = str;
    if (!/^[\x20-\x7E]*$/.test(str)) {
      normalizedType = "";
    }
    return normalizedType.toLowerCase();
  }

  const _byteSequence = Symbol("[[ByteSequence]]");

  class Blob {
    /** @type {string} */
    #type;

    /** @type {Uint8Array} */
    [_byteSequence];

    /**
     * @param {BlobPart[]} [blobParts]
     * @param {BlobPropertyBag} [options]
     */
    constructor(blobParts, options) {
      if (blobParts === undefined) {
        blobParts = [];
      }
      if (typeof blobParts !== "object") {
        throw new TypeError(
          `Failed to construct 'Blob'. blobParts cannot be converted to a sequence.`,
        );
      }

      const parts = [];
      const iterator = blobParts[Symbol.iterator]?.();
      if (iterator === undefined) {
        throw new TypeError(
          "Failed to construct 'Blob'. The provided value cannot be converted to a sequence",
        );
      }
      while (true) {
        const { value: element, done } = iterator.next();
        if (done) break;
        if (
          ArrayBuffer.isView(element) || element instanceof ArrayBuffer ||
          element instanceof Blob
        ) {
          parts.push(element);
        } else {
          parts.push(String(element));
        }
      }

      if (!options || typeof options === "function") {
        options = {};
      }
      if (typeof options !== "object") {
        throw new TypeError(
          `Failed to construct 'Blob'. options is not an object.`,
        );
      }
      const endings = options.endings?.toString() ?? "transparent";
      const type = options.type?.toString() ?? "";

      /** @type {Uint8Array} */
      this[_byteSequence] = processBlobParts(parts, endings);
      this.#type = normalizeType(type);
    }

    /** @returns {number} */
    get size() {
      return this[_byteSequence].byteLength;
    }

    /** @returns {string} */
    get type() {
      return this.#type;
    }

    /** 
     * @param {number} [start]
     * @param {number} [end]
     * @param {string} [contentType]
     * @returns {Blob}
     */
    slice(start, end, contentType) {
      const O = this;
      /** @type {number} */
      let relativeStart;
      if (start === undefined) {
        relativeStart = 0;
      } else {
        start = Number(start);
        if (start < 0) {
          relativeStart = Math.max(O.size + start, 0);
        } else {
          relativeStart = Math.min(start, O.size);
        }
      }
      /** @type {number} */
      let relativeEnd;
      if (end === undefined) {
        relativeEnd = O.size;
      } else {
        end = Number(end);
        if (end < 0) {
          relativeEnd = Math.max(O.size + end, 0);
        } else {
          relativeEnd = Math.min(end, O.size);
        }
      }
      /** @type {string} */
      let relativeContentType;
      if (contentType === undefined) {
        relativeContentType = "";
      } else {
        relativeContentType = normalizeType(String(contentType));
      }
      return new Blob([
        O[_byteSequence].buffer.slice(relativeStart, relativeEnd),
      ], { type: relativeContentType });
    }

    /**
     * @returns {ReadableStream<Uint8Array>}
     */
    stream() {
      const bytes = this[_byteSequence];
      const stream = new ReadableStream({
        type: "bytes",
        /** @param {ReadableByteStreamController} controller */
        start(controller) {
          const chunk = new Uint8Array(bytes.buffer.slice(0));
          if (chunk.byteLength > 0) controller.enqueue(chunk);
          controller.close();
        },
      });
      return stream;
    }

    /**
     * @returns {Promise<string>}
     */
    async text() {
      const buffer = await this.arrayBuffer();
      return utf8Decoder.decode(buffer);
    }

    /**
     * @returns {Promise<ArrayBuffer>}
     */
    async arrayBuffer() {
      const stream = this.stream();
      let bytes = new Uint8Array();
      for await (const chunk of stream) {
        bytes = concatUint8Arrays(bytes, chunk);
      }
      return bytes.buffer;
    }

    get [Symbol.toStringTag]() {
      return "Blob";
    }
  }

  const _Name = Symbol("[[Name]]");
  const _LastModfied = Symbol("[[LastModified]]");

  class File extends Blob {
    /** @type {string} */
    [_Name];
    /** @type {number} */
    [_LastModfied];

    /**
     * @param {BlobPart[]} fileBits 
     * @param {string} fileName 
     * @param {FilePropertyBag} [options] 
     */
    constructor(fileBits, fileName, options) {
      if (fileBits === undefined) {
        throw new TypeError(
          "Failed to construct 'File'. 2 arguments required, but first not specified.",
        );
      }
      if (fileName === undefined) {
        throw new TypeError(
          "Failed to construct 'File'. 2 arguments required, but second not specified.",
        );
      }
      super(fileBits, { endings: options?.endings, type: options?.type });
      /** @type {string} */
      this[_Name] = String(fileName).replaceAll("/", ":");
      if (options?.lastModified === undefined) {
        /** @type {number} */
        this[_LastModfied] = new Date().getTime();
      } else {
        /** @type {number} */
        this[_LastModfied] = Number(options.lastModified);
      }
    }

    /** @returns {string} */
    get name() {
      return this[_Name];
    }

    /** @returns {number} */
    get lastModified() {
      return this[_LastModfied];
    }
  }

  window.__bootstrap.file = {
    Blob,
    _byteSequence,
    File,
  };
})(this);