|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +/** |
| 6 | + * Implementation of the File API Blob interface |
| 7 | + * Based on: https://w3c.github.io/FileAPI/#blob-section |
| 8 | + * WinterTC Compliance: https://min-common-api.proposal.wintertc.org/ |
| 9 | + */ |
| 10 | + |
| 11 | +type BlobPart = BufferSource | string | Blob; |
| 12 | + |
| 13 | +interface BlobPropertyBag { |
| 14 | + type?: string; |
| 15 | + endings?: "transparent" | "native"; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Utility function to convert various input types to byte array |
| 20 | + */ |
| 21 | +function convertBlobPartsToBytes(blobParts: BlobPart[]): number[] { |
| 22 | + const bytes: number[] = []; |
| 23 | + |
| 24 | + for (const part of blobParts) { |
| 25 | + if (typeof part === "string") { |
| 26 | + // Convert string to UTF-8 bytes |
| 27 | + const encoder = new TextEncoder(); |
| 28 | + const stringBytes = encoder.encode(part); |
| 29 | + for (let i = 0; i < stringBytes.length; i++) { |
| 30 | + bytes.push(stringBytes[i]); |
| 31 | + } |
| 32 | + } else if (part instanceof Blob) { |
| 33 | + // Get bytes from another blob |
| 34 | + const partBlob = part as Blob & { _blobId: string }; |
| 35 | + const blobBytes = internal_blob_get_data(partBlob._blobId); |
| 36 | + if (blobBytes) { |
| 37 | + const blobByteArray = blobBytes.split(",").map((b) => |
| 38 | + parseInt(b, 10) |
| 39 | + ).filter((b) => !isNaN(b)); |
| 40 | + bytes.push(...blobByteArray); |
| 41 | + } |
| 42 | + } else if (part instanceof ArrayBuffer) { |
| 43 | + // Convert ArrayBuffer to bytes |
| 44 | + const view = new Uint8Array(part); |
| 45 | + for (let i = 0; i < view.length; i++) { |
| 46 | + bytes.push(view[i]); |
| 47 | + } |
| 48 | + } else if (ArrayBuffer.isView(part)) { |
| 49 | + // Handle TypedArray views |
| 50 | + const view = new Uint8Array( |
| 51 | + part.buffer, |
| 52 | + part.byteOffset, |
| 53 | + part.byteLength, |
| 54 | + ); |
| 55 | + for (let i = 0; i < view.length; i++) { |
| 56 | + bytes.push(view[i]); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return bytes; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Blob represents a file-like object of immutable, raw data |
| 66 | + */ |
| 67 | +class Blob { |
| 68 | + #blobId: string; |
| 69 | + |
| 70 | + constructor( |
| 71 | + blobParts: BlobPart[] = [], |
| 72 | + options: BlobPropertyBag = {}, |
| 73 | + existingBlobId?: string, |
| 74 | + ) { |
| 75 | + if (existingBlobId) { |
| 76 | + // Use existing blob ID (for internal operations like slice) |
| 77 | + this.#blobId = existingBlobId; |
| 78 | + } else { |
| 79 | + // Normal blob creation |
| 80 | + const type = options.type || ""; |
| 81 | + |
| 82 | + // Validate and normalize type |
| 83 | + let normalizedType = ""; |
| 84 | + if (type) { |
| 85 | + // Basic MIME type validation - should be lowercase and ASCII printable |
| 86 | + if ( |
| 87 | + /^[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_]*\/[a-zA-Z0-9][a-zA-Z0-9!#$&\-\^_.]*$/ |
| 88 | + .test(type) |
| 89 | + ) { |
| 90 | + normalizedType = type.toLowerCase(); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Convert blob parts to bytes |
| 95 | + const bytes = convertBlobPartsToBytes(blobParts); |
| 96 | + const bytesString = bytes.join(","); |
| 97 | + |
| 98 | + // Create blob through native implementation |
| 99 | + this.#blobId = internal_blob_create(bytesString, normalizedType); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * The size of the blob in bytes |
| 105 | + */ |
| 106 | + get size(): number { |
| 107 | + return internal_blob_get_size(this.#blobId); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * The MIME type of the blob |
| 112 | + */ |
| 113 | + get type(): string { |
| 114 | + return internal_blob_get_type(this.#blobId); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Returns a new Blob containing the data in the specified range |
| 119 | + */ |
| 120 | + slice(start?: number, end?: number, contentType?: string): Blob { |
| 121 | + const actualStart = start ?? 0; |
| 122 | + const actualEnd = end ?? this.size; |
| 123 | + const actualContentType = contentType ?? ""; |
| 124 | + |
| 125 | + const newBlobId = internal_blob_slice( |
| 126 | + this.#blobId, |
| 127 | + actualStart, |
| 128 | + actualEnd, |
| 129 | + actualContentType, |
| 130 | + ); |
| 131 | + |
| 132 | + // Create a new Blob instance with the sliced blob ID |
| 133 | + return new Blob([], {}, newBlobId); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Returns a ReadableStream of the blob data |
| 138 | + */ |
| 139 | + stream(): ReadableStream<Uint8Array> { |
| 140 | + // TODO: return a proper ReadableStream |
| 141 | + const data = internal_blob_stream(this.#blobId); |
| 142 | + const bytes = data |
| 143 | + ? data.split(",").map((b) => parseInt(b, 10)).filter((b) => |
| 144 | + !isNaN(b) |
| 145 | + ) |
| 146 | + : []; |
| 147 | + const uint8Array = new Uint8Array(bytes); |
| 148 | + |
| 149 | + return new ReadableStream({ |
| 150 | + start(controller) { |
| 151 | + controller.enqueue(uint8Array); |
| 152 | + controller.close(); |
| 153 | + }, |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Returns a Promise that resolves with the blob data as an ArrayBuffer |
| 159 | + */ |
| 160 | + arrayBuffer(): Promise<ArrayBuffer> { |
| 161 | + return new Promise((resolve) => { |
| 162 | + const data = internal_blob_array_buffer(this.#blobId); |
| 163 | + const bytes = data |
| 164 | + ? data.split(",").map((b) => parseInt(b, 10)).filter((b) => |
| 165 | + !isNaN(b) |
| 166 | + ) |
| 167 | + : []; |
| 168 | + |
| 169 | + const buffer = new ArrayBuffer(bytes.length); |
| 170 | + const view = new Uint8Array(buffer); |
| 171 | + for (let i = 0; i < bytes.length; i++) { |
| 172 | + view[i] = bytes[i]; |
| 173 | + } |
| 174 | + |
| 175 | + resolve(buffer); |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Returns a Promise that resolves with the blob data as a string |
| 181 | + */ |
| 182 | + text(): Promise<string> { |
| 183 | + return new Promise((resolve) => { |
| 184 | + resolve(internal_blob_text(this.#blobId)); |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Returns the blob ID (internal method for File implementation) |
| 190 | + */ |
| 191 | + get [Symbol.toStringTag]() { |
| 192 | + return "Blob"; |
| 193 | + } |
| 194 | + |
| 195 | + // Internal accessor for other implementations |
| 196 | + get _blobId(): string { |
| 197 | + return this.#blobId; |
| 198 | + } |
| 199 | +} |
0 commit comments