Skip to content

feat(ext/web): add Blob.prototype.bytes() #24148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cli/tsc/dts/lib.dom.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3121,6 +3121,8 @@ interface Blob {
readonly type: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer) */
arrayBuffer(): Promise<ArrayBuffer>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/bytes) */
bytes(): Promise<Uint8Array>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
slice(start?: number, end?: number, contentType?: string): Blob;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream) */
Expand Down
2 changes: 2 additions & 0 deletions cli/tsc/dts/lib.webworker.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,8 @@ interface Blob {
readonly type: string;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/arrayBuffer) */
arrayBuffer(): Promise<ArrayBuffer>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/bytes) */
bytes(): Promise<Uint8Array>;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/slice) */
slice(start?: number, end?: number, contentType?: string): Blob;
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Blob/stream) */
Expand Down
26 changes: 19 additions & 7 deletions ext/web/09_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,9 @@ class Blob {
}

/**
* @returns {Promise<string>}
* @param {number} size
* @returns {Promise<Uint8Array>}
*/
async text() {
webidl.assertBranded(this, BlobPrototype);
const buffer = await this.#u8Array(this.size);
return core.decode(buffer);
}

async #u8Array(size) {
const bytes = new Uint8Array(size);
const partIterator = toIterator(this[_parts]);
Expand All @@ -413,6 +408,15 @@ class Blob {
return bytes;
}

/**
* @returns {Promise<string>}
*/
async text() {
webidl.assertBranded(this, BlobPrototype);
const buffer = await this.#u8Array(this.size);
return core.decode(buffer);
}

/**
* @returns {Promise<ArrayBuffer>}
*/
Expand All @@ -422,6 +426,14 @@ class Blob {
return TypedArrayPrototypeGetBuffer(buf);
}

/**
* @returns {Promise<Uint8Array>}
*/
async bytes() {
webidl.assertBranded(this, BlobPrototype);
return await this.#u8Array(this.size);
}

[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
Expand Down
1 change: 1 addition & 0 deletions ext/web/lib.deno_web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ declare interface Blob {
readonly size: number;
readonly type: string;
arrayBuffer(): Promise<ArrayBuffer>;
bytes(): Promise<Uint8Array>;
slice(start?: number, end?: number, contentType?: string): Blob;
stream(): ReadableStream<Uint8Array>;
text(): Promise<string>;
Expand Down
12 changes: 2 additions & 10 deletions tests/wpt/runner/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -10109,8 +10109,8 @@
"Blob-text.any.worker.html": true,
"Blob-in-worker.worker.html": true,
"Blob-constructor-dom.window.html": false,
"Blob-bytes.any.html": false,
"Blob-bytes.any.worker.html": false,
"Blob-bytes.any.html": true,
"Blob-bytes.any.worker.html": true,
"Blob-constructor-endings.html": false
},
"file": {
Expand Down Expand Up @@ -10183,9 +10183,6 @@
"filereader_result.any.worker.html": true
},
"idlharness.any.html": [
"Blob interface: operation bytes()",
"Blob interface: new Blob([\"TEST\"]) must inherit property \"bytes()\" with the proper type",
"Blob interface: new File([\"myFileBits\"], \"myFileName\") must inherit property \"bytes()\" with the proper type",
"FileList interface: existence and properties of interface object",
"FileList interface object length",
"FileList interface object name",
Expand All @@ -10196,9 +10193,6 @@
"FileList interface: attribute length"
],
"idlharness.any.worker.html": [
"Blob interface: operation bytes()",
"Blob interface: new Blob([\"TEST\"]) must inherit property \"bytes()\" with the proper type",
"Blob interface: new File([\"myFileBits\"], \"myFileName\") must inherit property \"bytes()\" with the proper type",
"FileList interface: existence and properties of interface object",
"FileList interface object length",
"FileList interface object name",
Expand All @@ -10220,7 +10214,6 @@
],
"FileReaderSync.worker.html": false,
"idlharness.worker.html": [
"Blob interface: operation bytes()",
"FileList interface: existence and properties of interface object",
"FileList interface object length",
"FileList interface object name",
Expand Down Expand Up @@ -10265,7 +10258,6 @@
"Service worker test setup"
],
"idlharness.html": [
"Blob interface: operation bytes()",
"FileList interface: existence and properties of interface object",
"FileList interface object length",
"FileList interface object name",
Expand Down