summaryrefslogtreecommitdiff
path: root/ext/fetch/22_body.js
diff options
context:
space:
mode:
authorKenta Moriuchi <moriken@kimamass.com>2023-04-03 02:41:41 +0900
committerGitHub <noreply@github.com>2023-04-02 19:41:41 +0200
commit03edd48edd004cec091541e6b71095cfbc4b4c87 (patch)
tree72aed1dae803334b73479ffebc7ca8c83d10addf /ext/fetch/22_body.js
parentad8d0c90d1887beb8a5f2c6d30f9dc71cc63e4fe (diff)
chore: Turn back on dlintPreferPrimordials (#17715)
Closes #17709
Diffstat (limited to 'ext/fetch/22_body.js')
-rw-r--r--ext/fetch/22_body.js70
1 files changed, 50 insertions, 20 deletions
diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js
index dd11df2a2..9dbd58fa4 100644
--- a/ext/fetch/22_body.js
+++ b/ext/fetch/22_body.js
@@ -38,17 +38,24 @@ import {
const primordials = globalThis.__bootstrap.primordials;
const {
ArrayBufferPrototype,
+ ArrayBufferPrototypeGetByteLength,
ArrayBufferIsView,
ArrayPrototypeMap,
+ DataViewPrototypeGetBuffer,
+ DataViewPrototypeGetByteLength,
+ DataViewPrototypeGetByteOffset,
JSONParse,
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
// TODO(lucacasonato): add SharedArrayBuffer to primordials
// SharedArrayBufferPrototype
+ TypedArrayPrototypeGetBuffer,
+ TypedArrayPrototypeGetByteLength,
+ TypedArrayPrototypeGetByteOffset,
+ TypedArrayPrototypeGetSymbolToStringTag,
TypedArrayPrototypeSlice,
TypeError,
Uint8Array,
- Uint8ArrayPrototype,
} = primordials;
/**
@@ -328,7 +335,7 @@ function mixinBody(prototype, bodySymbol, mimeTypeSymbol) {
function packageData(bytes, type, mimeType) {
switch (type) {
case "ArrayBuffer":
- return chunkToU8(bytes).buffer;
+ return TypedArrayPrototypeGetBuffer(chunkToU8(bytes));
case "Blob":
return new Blob([bytes], {
type: mimeType !== null ? mimesniff.serializeMimeType(mimeType) : "",
@@ -385,22 +392,45 @@ function extractBody(object) {
if (object.type.length !== 0) {
contentType = object.type;
}
- } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, object)) {
- // Fast(er) path for common case of Uint8Array
- const copy = TypedArrayPrototypeSlice(object, 0, object.byteLength);
- source = copy;
- } else if (
- ArrayBufferIsView(object) ||
- ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)
- ) {
- const u8 = ArrayBufferIsView(object)
- ? new Uint8Array(
- object.buffer,
- object.byteOffset,
- object.byteLength,
- )
- : new Uint8Array(object);
- const copy = TypedArrayPrototypeSlice(u8, 0, u8.byteLength);
+ } else if (ArrayBufferIsView(object)) {
+ const tag = TypedArrayPrototypeGetSymbolToStringTag(object);
+ if (tag === "Uint8Array") {
+ // Fast(er) path for common case of Uint8Array
+ const copy = TypedArrayPrototypeSlice(
+ object,
+ TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (object)),
+ TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (object)),
+ );
+ source = copy;
+ } else if (tag !== undefined) {
+ // TypedArray
+ const copy = TypedArrayPrototypeSlice(
+ new Uint8Array(
+ TypedArrayPrototypeGetBuffer(/** @type {Uint8Array} */ (object)),
+ TypedArrayPrototypeGetByteOffset(/** @type {Uint8Array} */ (object)),
+ TypedArrayPrototypeGetByteLength(/** @type {Uint8Array} */ (object)),
+ ),
+ );
+ source = copy;
+ } else {
+ // DataView
+ const copy = TypedArrayPrototypeSlice(
+ new Uint8Array(
+ DataViewPrototypeGetBuffer(/** @type {DataView} */ (object)),
+ DataViewPrototypeGetByteOffset(/** @type {DataView} */ (object)),
+ DataViewPrototypeGetByteLength(/** @type {DataView} */ (object)),
+ ),
+ );
+ source = copy;
+ }
+ } else if (ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)) {
+ const copy = TypedArrayPrototypeSlice(
+ new Uint8Array(
+ object,
+ 0,
+ ArrayBufferPrototypeGetByteLength(object),
+ ),
+ );
source = copy;
} else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) {
const res = formDataToBlob(object);
@@ -426,9 +456,9 @@ function extractBody(object) {
// no observable side-effect for users so far, but could change
stream = { body: source, consumed: false };
length = null; // NOTE: string length != byte length
- } else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, source)) {
+ } else if (TypedArrayPrototypeGetSymbolToStringTag(source) === "Uint8Array") {
stream = { body: source, consumed: false };
- length = source.byteLength;
+ length = TypedArrayPrototypeGetByteLength(source);
}
const body = new InnerBody(stream);
body.source = source;