Skip to content

Commit

Permalink
refactor: move fetch util to core util
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Nov 22, 2021
1 parent 8097b96 commit af20fdc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert')
const { Readable } = require('stream')
const { RequestAbortedError, NotSupportedError } = require('../core/errors')
const util = require('../core/util')
const { ReadableStreamFrom, toUSVString } = require('../fetch/util')
const { ReadableStreamFrom, toUSVString } = require('../core/util')

let Blob

Expand Down
40 changes: 40 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const stream = require('stream')
const net = require('net')
const { InvalidArgumentError } = require('./errors')
const { Blob } = require('buffer')
const nodeUtil = require('util')

function nop () {}

Expand Down Expand Up @@ -269,13 +270,51 @@ function getSocketInfo (socket) {
}
}

let ReadableStream
function ReadableStreamFrom (iterable) {
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

if (ReadableStream.from) {
// https://github.com/whatwg/streams/pull/1083
return ReadableStream.from(iterable)
}

let iterator
return new ReadableStream(
{
async start () {
iterator = iterable[Symbol.asyncIterator]()
},
async pull (controller) {
const { done, value } = await iterator.next()
if (done) {
queueMicrotask(() => {
controller.close()
})
} else {
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
controller.enqueue(new Uint8Array(buf))
}
return controller.desiredSize > 0
},
async cancel (reason) {
await iterator.return()
}
},
0
)
}

const kEnumerableProperty = Object.create(null)
kEnumerableProperty.enumerable = true

module.exports = {
kEnumerableProperty,
nop,
isDisturbed,
toUSVString: nodeUtil.toUSVString || ((val) => `${val}`),
isAborted,
isBlobLike,
parseOrigin,
Expand All @@ -290,6 +329,7 @@ module.exports = {
destroy,
bodyLength,
deepClone,
ReadableStreamFrom,
isBuffer,
isBlob,
validateHandler,
Expand Down
43 changes: 3 additions & 40 deletions lib/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

const { redirectStatus } = require('./constants')
const { performance } = require('perf_hooks')
const nodeUtil = require('util')
const { isBlobLike } = require('../core/util')
const { isBlobLike, toUSVString, ReadableStreamFrom } = require('../core/util')

let File
let ReadableStream

// https://fetch.spec.whatwg.org/#block-bad-port
const badPorts = [
Expand Down Expand Up @@ -145,42 +143,6 @@ function isValidHTTPToken (characters) {
return true
}

function ReadableStreamFrom (iterable) {
if (!ReadableStream) {
ReadableStream = require('stream/web').ReadableStream
}

if (ReadableStream.from) {
// https://github.com/whatwg/streams/pull/1083
return ReadableStream.from(iterable)
}

let iterator
return new ReadableStream(
{
async start () {
iterator = iterable[Symbol.asyncIterator]()
},
async pull (controller) {
const { done, value } = await iterator.next()
if (done) {
queueMicrotask(() => {
controller.close()
})
} else {
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
controller.enqueue(new Uint8Array(buf))
}
return controller.desiredSize > 0
},
async cancel (reason) {
await iterator.return()
}
},
0
)
}

// https://w3c.github.io/webappsec-referrer-policy/#set-requests-referrer-policy-on-redirect
function setRequestReferrerPolicyOnRedirect (request, actualResponse) {
// Given a request request and a response actualResponse, this algorithm
Expand Down Expand Up @@ -343,7 +305,8 @@ module.exports = {
ServiceWorkerGlobalScope,
Window,
EnvironmentSettingsObject,
toUSVString: nodeUtil.toUSVString || ((val) => `${val}`),
ReadableStreamFrom,
toUSVString,
tryUpgradeRequestToAPotentiallyTrustworthyURL,
coarsenedSharedCurrentTime,
matchRequestIntegrity,
Expand Down

0 comments on commit af20fdc

Please sign in to comment.