diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-01-27 13:36:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 13:36:36 +0100 |
commit | 884143218fad0e18f7553aaf079d52de703f7601 (patch) | |
tree | 9b9e9d30ea647041438ef8fa974b8d4234cabf73 /ext/fetch/22_body.js | |
parent | dcf8f144ab0516936bfa4e93357d71f1732d880e (diff) |
refactor: update runtime code for primordial checks for "instanceof" (#13497)
Diffstat (limited to 'ext/fetch/22_body.js')
-rw-r--r-- | ext/fetch/22_body.js | 105 |
1 files changed, 80 insertions, 25 deletions
diff --git a/ext/fetch/22_body.js b/ext/fetch/22_body.js index acfd06b0f..f33097033 100644 --- a/ext/fetch/22_body.js +++ b/ext/fetch/22_body.js @@ -16,23 +16,35 @@ const core = window.Deno.core; const webidl = globalThis.__bootstrap.webidl; const { parseUrlEncoded } = globalThis.__bootstrap.url; - const { parseFormData, formDataFromEntries, formDataToBlob } = - globalThis.__bootstrap.formData; + const { URLSearchParamsPrototype } = globalThis.__bootstrap.url; + const { + parseFormData, + formDataFromEntries, + formDataToBlob, + FormDataPrototype, + } = globalThis.__bootstrap.formData; const mimesniff = globalThis.__bootstrap.mimesniff; - const { isReadableStreamDisturbed, errorReadableStream, createProxy } = - globalThis.__bootstrap.streams; + const { BlobPrototype } = globalThis.__bootstrap.file; + const { + isReadableStreamDisturbed, + errorReadableStream, + createProxy, + ReadableStreamPrototype, + } = globalThis.__bootstrap.streams; const { - ArrayBuffer, + ArrayBufferPrototype, ArrayBufferIsView, ArrayPrototypePush, ArrayPrototypeMap, JSONParse, ObjectDefineProperties, + ObjectPrototypeIsPrototypeOf, PromiseResolve, TypedArrayPrototypeSet, TypedArrayPrototypeSlice, TypeError, Uint8Array, + Uint8ArrayPrototype, } = window.__bootstrap.primordials; /** @@ -66,7 +78,12 @@ } get stream() { - if (!(this.streamOrStatic instanceof ReadableStream)) { + if ( + !ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { const { body, consumed } = this.streamOrStatic; if (consumed) { this.streamOrStatic = new ReadableStream(); @@ -88,7 +105,12 @@ * @returns {boolean} */ unusable() { - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { return this.streamOrStatic.locked || isReadableStreamDisturbed(this.streamOrStatic); } @@ -99,7 +121,12 @@ * @returns {boolean} */ consumed() { - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { return isReadableStreamDisturbed(this.streamOrStatic); } return this.streamOrStatic.consumed; @@ -111,7 +138,12 @@ */ async consume() { if (this.unusable()) throw new TypeError("Body already consumed."); - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { const reader = this.stream.getReader(); /** @type {Uint8Array[]} */ const chunks = []; @@ -136,7 +168,12 @@ } cancel(error) { - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { this.streamOrStatic.cancel(error); } else { this.streamOrStatic.consumed = true; @@ -144,7 +181,12 @@ } error(error) { - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { errorReadableStream(this.streamOrStatic, error); } else { this.streamOrStatic.consumed = true; @@ -168,7 +210,12 @@ */ createProxy() { let proxyStreamOrStatic; - if (this.streamOrStatic instanceof ReadableStream) { + if ( + ObjectPrototypeIsPrototypeOf( + ReadableStreamPrototype, + this.streamOrStatic, + ) + ) { proxyStreamOrStatic = createProxy(this.streamOrStatic); } else { proxyStreamOrStatic = { ...this.streamOrStatic }; @@ -282,7 +329,7 @@ enumerable: true, }, }; - return ObjectDefineProperties(prototype.prototype, mixin); + return ObjectDefineProperties(prototype, mixin); } /** @@ -341,18 +388,21 @@ let source = null; let length = null; let contentType = null; - if (object instanceof Blob) { + if (ObjectPrototypeIsPrototypeOf(BlobPrototype, object)) { stream = object.stream(); source = object; length = object.size; if (object.type.length !== 0) { contentType = object.type; } - } else if (object instanceof Uint8Array) { + } 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) || object instanceof ArrayBuffer) { + } else if ( + ArrayBufferIsView(object) || + ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object) + ) { const u8 = ArrayBufferIsView(object) ? new Uint8Array( object.buffer, @@ -362,26 +412,28 @@ : new Uint8Array(object); const copy = TypedArrayPrototypeSlice(u8, 0, u8.byteLength); source = copy; - } else if (object instanceof FormData) { + } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) { const res = formDataToBlob(object); stream = res.stream(); source = res; length = res.size; contentType = res.type; - } else if (object instanceof URLSearchParams) { + } else if ( + ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, object) + ) { // TODO(@satyarohith): not sure what primordial here. source = object.toString(); contentType = "application/x-www-form-urlencoded;charset=UTF-8"; } else if (typeof object === "string") { source = object; contentType = "text/plain;charset=UTF-8"; - } else if (object instanceof ReadableStream) { + } else if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, object)) { stream = object; if (object.locked || isReadableStreamDisturbed(object)) { throw new TypeError("ReadableStream is locked or disturbed"); } } - if (source instanceof Uint8Array) { + if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, source)) { stream = { body: source, consumed: false }; length = source.byteLength; } else if (typeof source === "string") { @@ -399,19 +451,22 @@ webidl.converters["BodyInit_DOMString"] = (V, opts) => { // Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString) - if (V instanceof ReadableStream) { + if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, V)) { // TODO(lucacasonato): ReadableStream is not branded return V; - } else if (V instanceof Blob) { + } else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) { return webidl.converters["Blob"](V, opts); - } else if (V instanceof FormData) { + } else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, V)) { return webidl.converters["FormData"](V, opts); - } else if (V instanceof URLSearchParams) { + } else if (ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, V)) { // TODO(lucacasonato): URLSearchParams is not branded return V; } if (typeof V === "object") { - if (V instanceof ArrayBuffer || V instanceof SharedArrayBuffer) { + if ( + ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) || + ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V) + ) { return webidl.converters["ArrayBuffer"](V, opts); } if (ArrayBufferIsView(V)) { |