Add fetch body serialization support for Blob, ArrayBuffer, and TypedArray (#329)
* Add fetch body serialization support for Blob, ArrayBuffer, and TypedArray * js: fix Blob fetch body serialization The Blob branch passed the Blob to _bodyToUint8Array, which did not know about obscura's Blob (its data lives in _bytes) and fell through to encoding String(blob), so a Blob body was sent as "[object Blob]". Teach _bodyToUint8Array about _bytes, and build the binary string with the loop-based _bytesToBinaryString instead of String.fromCharCode.apply so large blobs do not overflow the argument limit. --------- Co-authored-by: SGavrl <[email protected]>
This commit is contained in:
1 parent
505738450a
commit
260c4c01fa
1 file changed
+18
Vendored
+18
@@ -3020,6 +3020,8 @@ function _bodyToUint8Array(body) {
|
|||||||
if (body instanceof Uint8Array) return body;
|
if (body instanceof Uint8Array) return body;
|
||||||
if (body instanceof ArrayBuffer) return new Uint8Array(body);
|
if (body instanceof ArrayBuffer) return new Uint8Array(body);
|
||||||
if (ArrayBuffer.isView(body)) return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
if (ArrayBuffer.isView(body)) return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
||||||
|
// obscura's Blob materializes its data into _bytes in the constructor.
|
||||||
|
if (body._bytes instanceof Uint8Array) return body._bytes;
|
||||||
return new TextEncoder().encode(String(body));
|
return new TextEncoder().encode(String(body));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3088,6 +3090,22 @@ function _serializeBody(initBody, headers) {
|
|||||||
}
|
}
|
||||||
return initBody.toString();
|
return initBody.toString();
|
||||||
}
|
}
|
||||||
|
if (typeof Blob !== 'undefined' && initBody instanceof Blob) {
|
||||||
|
if (initBody.type && !Object.keys(headers).some(k => k.toLowerCase() === 'content-type')) {
|
||||||
|
headers['Content-Type'] = initBody.type;
|
||||||
|
}
|
||||||
|
return _bytesToBinaryString(_bodyToUint8Array(initBody));
|
||||||
|
}
|
||||||
|
if (typeof ArrayBuffer !== 'undefined' && initBody instanceof ArrayBuffer) {
|
||||||
|
const bytes = new Uint8Array(initBody);
|
||||||
|
let s = ''; for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
if (typeof ArrayBuffer !== 'undefined' && ArrayBuffer.isView(initBody) && initBody.buffer instanceof ArrayBuffer) {
|
||||||
|
const bytes = new Uint8Array(initBody.buffer, initBody.byteOffset, initBody.byteLength);
|
||||||
|
let s = ''; for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
return typeof initBody === 'string' ? initBody : String(initBody);
|
return typeof initBody === 'string' ? initBody : String(initBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user