Skip to content
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

buffer: extract Blob's .arrayBuffer() & webidl changes #53372

Merged
merged 1 commit into from
Sep 26, 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
67 changes: 37 additions & 30 deletions lib/internal/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,54 +273,32 @@ class Blob {
if (!isBlob(this))
return PromiseReject(new ERR_INVALID_THIS('Blob'));

const { promise, resolve, reject } = createDeferredPromise();
const reader = this[kHandle].getReader();
const buffers = [];
const readNext = () => {
reader.pull((status, buffer) => {
if (status === 0) {
// EOS, concat & resolve
// buffer should be undefined here
resolve(concat(buffers));
return;
} else if (status < 0) {
// The read could fail for many different reasons when reading
// from a non-memory resident blob part (e.g. file-backed blob).
// The error details the system error code.
const error = lazyDOMException('The blob could not be read', 'NotReadableError');
reject(error);
return;
}
if (buffer !== undefined)
buffers.push(buffer);
queueMicrotask(() => readNext());
});
};
readNext();
return promise;
return arrayBuffer(this);
}

/**
* @returns {Promise<string>}
*/
async text() {
text() {
if (!isBlob(this))
throw new ERR_INVALID_THIS('Blob');
return PromiseReject(new ERR_INVALID_THIS('Blob'));

dec ??= new TextDecoder();

return dec.decode(await this.arrayBuffer());
return PromisePrototypeThen(
arrayBuffer(this),
(buffer) => dec.decode(buffer));
}

/**
* @returns {Promise<Uint8Array>}
*/
bytes() {
if (!isBlob(this))
throw new ERR_INVALID_THIS('Blob');
return PromiseReject(new ERR_INVALID_THIS('Blob'));

return PromisePrototypeThen(
this.arrayBuffer(),
arrayBuffer(this),
(buffer) => new Uint8Array(buffer));
}

Expand Down Expand Up @@ -439,6 +417,7 @@ ObjectDefineProperties(Blob.prototype, {
stream: kEnumerableProperty,
text: kEnumerableProperty,
arrayBuffer: kEnumerableProperty,
bytes: kEnumerableProperty,
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
});

function resolveObjectURL(url) {
Expand Down Expand Up @@ -490,6 +469,34 @@ function createBlobFromFilePath(path, options) {
return res;
}

function arrayBuffer(blob) {
const { promise, resolve, reject } = createDeferredPromise();
const reader = blob[kHandle].getReader();
const buffers = [];
const readNext = () => {
reader.pull((status, buffer) => {
if (status === 0) {
// EOS, concat & resolve
// buffer should be undefined here
resolve(concat(buffers));
return;
} else if (status < 0) {
// The read could fail for many different reasons when reading
// from a non-memory resident blob part (e.g. file-backed blob).
// The error details the system error code.
const error = lazyDOMException('The blob could not be read', 'NotReadableError');
reject(error);
return;
}
if (buffer !== undefined)
buffers.push(buffer);
queueMicrotask(() => readNext());
});
};
readNext();
return promise;
}

module.exports = {
Blob,
createBlob,
Expand Down
21 changes: 19 additions & 2 deletions test/parallel/test-blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ assert.throws(() => new Blob({}), {
'stream',
'text',
'arrayBuffer',
'bytes',
];

for (const prop of enumerable) {
Expand Down Expand Up @@ -409,10 +410,13 @@ assert.throws(() => new Blob({}), {
}

(async () => {
await assert.rejects(async () => Blob.prototype.arrayBuffer.call(), {
await assert.rejects(() => Blob.prototype.arrayBuffer.call(), {
code: 'ERR_INVALID_THIS',
});
await assert.rejects(async () => Blob.prototype.text.call(), {
await assert.rejects(() => Blob.prototype.text.call(), {
code: 'ERR_INVALID_THIS',
});
await assert.rejects(() => Blob.prototype.bytes.call(), {
code: 'ERR_INVALID_THIS',
});
})().then(common.mustCall());
Expand Down Expand Up @@ -490,3 +494,16 @@ assert.throws(() => new Blob({}), {
assert.ok(structuredClone(blob).size === blob.size);
assert.ok((await structuredClone(blob).text()) === (await blob.text()));
})().then(common.mustCall());

(async () => {
const blob = new Blob(['hello']);
const { arrayBuffer } = Blob.prototype;

Blob.prototype.arrayBuffer = common.mustNotCall();

try {
assert.strictEqual(await blob.text(), 'hello');
} finally {
Blob.prototype.arrayBuffer = arrayBuffer;
}
})().then(common.mustCall());
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
Loading