| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ec7005a commit 0a793a2
15 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,13 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const fetchImpl = require('./lib/fetch').fetch | |
| 4 | 4 | ||
| 5 | - module.exports.fetch = async function fetch (resource, init = undefined) { | ||
| 6 | - try { | ||
| 7 | - return await fetchImpl(resource, init) | ||
| 8 | - } catch (err) { | ||
| 5 | + module.exports.fetch = function fetch (resource, init = undefined) { | ||
| 6 | + return fetchImpl(resource, init).catch((err) => { | ||
| 9 | 7 | Error.captureStackTrace(err, this) | |
| 10 | 8 | throw err | |
| 11 | - } | ||
| 9 | + }) | ||
| 12 | 10 | } | |
| 13 | 11 | module.exports.FormData = require('./lib/fetch/formdata').FormData | |
| 14 | 12 | module.exports.Headers = require('./lib/fetch/headers').Headers | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -222,6 +222,14 @@ class Request { | |||
| 222 | 222 | if (channels.bodySent.hasSubscribers) { | |
| 223 | 223 | channels.bodySent.publish({ request: this }) | |
| 224 | 224 | } | |
| 225 | + | ||
| 226 | + if (this[kHandler].onRequestSent) { | ||
| 227 | + try { | ||
| 228 | + this[kHandler].onRequestSent() | ||
| 229 | + } catch (err) { | ||
| 230 | + this.onError(err) | ||
| 231 | + } | ||
| 232 | + } | ||
| 225 | 233 | } | |
| 226 | 234 | ||
| 227 | 235 | onConnect (abort) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,8 @@ let ReadableStream = globalThis.ReadableStream | |||
| 26 | 26 | ||
| 27 | 27 | /** @type {globalThis['File']} */ | |
| 28 | 28 | const File = NativeFile ?? UndiciFile | |
| 29 | + const textEncoder = new TextEncoder() | ||
| 30 | + const textDecoder = new TextDecoder() | ||
| 29 | 31 | ||
| 30 | 32 | // https://fetch.spec.whatwg.org/#concept-bodyinit-extract | |
| 31 | 33 | function extractBody (object, keepalive = false) { | |
@@ -49,7 +51,7 @@ function extractBody (object, keepalive = false) { | |||
| 49 | 51 | stream = new ReadableStream({ | |
| 50 | 52 | async pull (controller) { | |
| 51 | 53 | controller.enqueue( | |
| 52 | - typeof source === 'string' ? new TextEncoder().encode(source) : source | ||
| 54 | + typeof source === 'string' ? textEncoder.encode(source) : source | ||
| 53 | 55 | ) | |
| 54 | 56 | queueMicrotask(() => readableStreamClose(controller)) | |
| 55 | 57 | }, | |
@@ -119,21 +121,20 @@ function extractBody (object, keepalive = false) { | |||
| 119 | 121 | // - That the content-length is calculated in advance. | |
| 120 | 122 | // - And that all parts are pre-encoded and ready to be sent. | |
| 121 | 123 | ||
| 122 | - const enc = new TextEncoder() | ||
| 123 | 124 | const blobParts = [] | |
| 124 | 125 | const rn = new Uint8Array([13, 10]) // '\r\n' | |
| 125 | 126 | length = 0 | |
| 126 | 127 | let hasUnknownSizeValue = false | |
| 127 | 128 | ||
| 128 | 129 | for (const [name, value] of object) { | |
| 129 | 130 | if (typeof value === 'string') { | |
| 130 | - const chunk = enc.encode(prefix + | ||
| 131 | + const chunk = textEncoder.encode(prefix + | ||
| 131 | 132 | `; name="${escape(normalizeLinefeeds(name))}"` + | |
| 132 | 133 | `\r\n\r\n${normalizeLinefeeds(value)}\r\n`) | |
| 133 | 134 | blobParts.push(chunk) | |
| 134 | 135 | length += chunk.byteLength | |
| 135 | 136 | } else { | |
| 136 | - const chunk = enc.encode(`${prefix}; name="${escape(normalizeLinefeeds(name))}"` + | ||
| 137 | + const chunk = textEncoder.encode(`${prefix}; name="${escape(normalizeLinefeeds(name))}"` + | ||
| 137 | 138 | (value.name ? `; filename="${escape(value.name)}"` : '') + '\r\n' + | |
| 138 | 139 | `Content-Type: ${ | |
| 139 | 140 | value.type || 'application/octet-stream' | |
@@ -147,7 +148,7 @@ function extractBody (object, keepalive = false) { | |||
| 147 | 148 | } | |
| 148 | 149 | } | |
| 149 | 150 | ||
| 150 | - const chunk = enc.encode(`--${boundary}--`) | ||
| 151 | + const chunk = textEncoder.encode(`--${boundary}--`) | ||
| 151 | 152 | blobParts.push(chunk) | |
| 152 | 153 | length += chunk.byteLength | |
| 153 | 154 | if (hasUnknownSizeValue) { | |
@@ -443,14 +444,16 @@ function bodyMixinMethods (instance) { | |||
| 443 | 444 | let text = '' | |
| 444 | 445 | // application/x-www-form-urlencoded parser will keep the BOM. | |
| 445 | 446 | // https://url.spec.whatwg.org/#concept-urlencoded-parser | |
| 446 | - const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true }) | ||
| 447 | + // Note that streaming decoder is stateful and cannot be reused | ||
| 448 | + const streamingDecoder = new TextDecoder('utf-8', { ignoreBOM: true }) | ||
| 449 | + | ||
| 447 | 450 | for await (const chunk of consumeBody(this[kState].body)) { | |
| 448 | 451 | if (!isUint8Array(chunk)) { | |
| 449 | 452 | throw new TypeError('Expected Uint8Array chunk') | |
| 450 | 453 | } | |
| 451 | - text += textDecoder.decode(chunk, { stream: true }) | ||
| 454 | + text += streamingDecoder.decode(chunk, { stream: true }) | ||
| 452 | 455 | } | |
| 453 | - text += textDecoder.decode() | ||
| 456 | + text += streamingDecoder.decode() | ||
| 454 | 457 | entries = new URLSearchParams(text) | |
| 455 | 458 | } catch (err) { | |
| 456 | 459 | // istanbul ignore next: Unclear when new URLSearchParams can fail on a string. | |
@@ -565,7 +568,7 @@ function utf8DecodeBytes (buffer) { | |||
| 565 | 568 | ||
| 566 | 569 | // 3. Process a queue with an instance of UTF-8’s | |
| 567 | 570 | // decoder, ioQueue, output, and "replacement". | |
| 568 | - const output = new TextDecoder().decode(buffer) | ||
| 571 | + const output = textDecoder.decode(buffer) | ||
| 569 | 572 | ||
| 570 | 573 | // 4. Return output. | |
| 571 | 574 | return output | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,10 +3,12 @@ | |||
| 3 | 3 | const { MessageChannel, receiveMessageOnPort } = require('worker_threads') | |
| 4 | 4 | ||
| 5 | 5 | const corsSafeListedMethods = ['GET', 'HEAD', 'POST'] | |
| 6 | + const corsSafeListedMethodsSet = new Set(corsSafeListedMethods) | ||
| 6 | 7 | ||
| 7 | 8 | const nullBodyStatus = [101, 204, 205, 304] | |
| 8 | 9 | ||
| 9 | 10 | const redirectStatus = [301, 302, 303, 307, 308] | |
| 11 | + const redirectStatusSet = new Set(redirectStatus) | ||
| 10 | 12 | ||
| 11 | 13 | // https://fetch.spec.whatwg.org/#block-bad-port | |
| 12 | 14 | const badPorts = [ | |
@@ -18,6 +20,8 @@ const badPorts = [ | |||
| 18 | 20 | '10080' | |
| 19 | 21 | ] | |
| 20 | 22 | ||
| 23 | + const badPortsSet = new Set(badPorts) | ||
| 24 | + | ||
| 21 | 25 | // https://w3c.github.io/webappsec-referrer-policy/#referrer-policies | |
| 22 | 26 | const referrerPolicy = [ | |
| 23 | 27 | '', | |
@@ -30,10 +34,12 @@ const referrerPolicy = [ | |||
| 30 | 34 | 'strict-origin-when-cross-origin', | |
| 31 | 35 | 'unsafe-url' | |
| 32 | 36 | ] | |
| 37 | + const referrerPolicySet = new Set(referrerPolicy) | ||
| 33 | 38 | ||
| 34 | 39 | const requestRedirect = ['follow', 'manual', 'error'] | |
| 35 | 40 | ||
| 36 | 41 | const safeMethods = ['GET', 'HEAD', 'OPTIONS', 'TRACE'] | |
| 42 | + const safeMethodsSet = new Set(safeMethods) | ||
| 37 | 43 | ||
| 38 | 44 | const requestMode = ['navigate', 'same-origin', 'no-cors', 'cors'] | |
| 39 | 45 | ||
@@ -68,6 +74,7 @@ const requestDuplex = [ | |||
| 68 | 74 | ||
| 69 | 75 | // http://fetch.spec.whatwg.org/#forbidden-method | |
| 70 | 76 | const forbiddenMethods = ['CONNECT', 'TRACE', 'TRACK'] | |
| 77 | + const forbiddenMethodsSet = new Set(forbiddenMethods) | ||
| 71 | 78 | ||
| 72 | 79 | const subresource = [ | |
| 73 | 80 | 'audio', | |
@@ -83,6 +90,7 @@ const subresource = [ | |||
| 83 | 90 | 'xslt', | |
| 84 | 91 | '' | |
| 85 | 92 | ] | |
| 93 | + const subresourceSet = new Set(subresource) | ||
| 86 | 94 | ||
| 87 | 95 | /** @type {globalThis['DOMException']} */ | |
| 88 | 96 | const DOMException = globalThis.DOMException ?? (() => { | |
@@ -132,5 +140,12 @@ module.exports = { | |||
| 132 | 140 | nullBodyStatus, | |
| 133 | 141 | safeMethods, | |
| 134 | 142 | badPorts, | |
| 135 | - requestDuplex | ||
| 143 | + requestDuplex, | ||
| 144 | + subresourceSet, | ||
| 145 | + badPortsSet, | ||
| 146 | + redirectStatusSet, | ||
| 147 | + corsSafeListedMethodsSet, | ||
| 148 | + safeMethodsSet, | ||
| 149 | + forbiddenMethodsSet, | ||
| 150 | + referrerPolicySet | ||
| 136 | 151 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ const { isBlobLike } = require('./util') | |||
| 7 | 7 | const { webidl } = require('./webidl') | |
| 8 | 8 | const { parseMIMEType, serializeAMimeType } = require('./dataURL') | |
| 9 | 9 | const { kEnumerableProperty } = require('../core/util') | |
| 10 | + const encoder = new TextEncoder() | ||
| 10 | 11 | ||
| 11 | 12 | class File extends Blob { | |
| 12 | 13 | constructor (fileBits, fileName, options = {}) { | |
@@ -280,7 +281,7 @@ function processBlobParts (parts, options) { | |||
| 280 | 281 | } | |
| 281 | 282 | ||
| 282 | 283 | // 3. Append the result of UTF-8 encoding s to bytes. | |
| 283 | - bytes.push(new TextEncoder().encode(s)) | ||
| 284 | + bytes.push(encoder.encode(s)) | ||
| 284 | 285 | } else if ( | |
| 285 | 286 | types.isAnyArrayBuffer(element) || | |
| 286 | 287 | types.isTypedArray(element) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments