Skip to content

Commit

Permalink
resolving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
crysmags committed May 23, 2024
1 parent 3fed3a8 commit d7ef0fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
33 changes: 20 additions & 13 deletions lib/web/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const defaultUserAgent = typeof __UNDICI_IS_NODE__ !== 'undefined' || typeof esb
: 'undici'

const channels = require('../../core/diagnostics.js').channels.tracingChannel

/** @type {import('buffer').resolveObjectURL} */
let resolveObjectURL

Expand Down Expand Up @@ -122,12 +121,17 @@ class Fetch extends EE {
}
}

function handleFetchDone (response) {
finalizeAndReportTiming(response, 'fetch')
}

// This will publish all diagnostic events only when we have subscribers.
function ifSubscribersRunStores (req, input, init, callback) {
const hasSubscribers = subscribersCheck()
// console.log(arguments)

if (hasSubscribers) {
const context = { req, input, init: init ?? {}, result: null, error: null }
const context = { req, input, init, result: null, error: null }

return channels.start.runStores(context, () => {
try {
Expand Down Expand Up @@ -181,7 +185,7 @@ function createInstrumentedDeferredPromise (context) {

// https://fetch.spec.whatwg.org/#fetch-method
function fetch (input, init = undefined) {
webidl.argumentLengthCheck(arguments, 1, { header: 'globalThis.fetch' })
webidl.argumentLengthCheck(arguments, 1, 'globalThis.fetch')

// 2. Let requestObject be the result of invoking the initial value of
// Request as constructor with input and init as arguments. If this throws
Expand All @@ -200,8 +204,8 @@ function fetch (input, init = undefined) {

// 4. If requestObject’s signal’s aborted flag is set, then:
if (requestObject.signal.aborted) {
// 1. Abort the fetch() call with p, request, null, and
// requestObject’s signal’s abort reason.
// 1. Abort the fetch() call with p, request, null, and
// requestObject’s signal’s abort reason.
abortFetch(p, request, null, requestObject.signal.reason)

// 2. Return p.
Expand All @@ -221,7 +225,6 @@ function fetch (input, init = undefined) {
let responseObject = null

// 8. Let relevantRealm be this’s relevant Realm.
const relevantRealm = null

// 9. Let locallyAborted be false.
let locallyAborted = false
Expand All @@ -242,16 +245,17 @@ function fetch (input, init = undefined) {
// 3. Abort controller with requestObject’s signal’s abort reason.
controller.abort(requestObject.signal.reason)

const realResponse = responseObject?.deref()

// 4. Abort the fetch() call with p, request, responseObject,
// and requestObject’s signal’s abort reason.
abortFetch(p, request, responseObject, requestObject.signal.reason)
abortFetch(p, request, realResponse, requestObject.signal.reason)
}
)

// 12. Let handleFetchDone given response response be to finalize and
// report timing with response, globalObject, and "fetch".
const handleFetchDone = (response) =>
finalizeAndReportTiming(response, 'fetch')
// see function handleFetchDone

// 13. Set controller to the result of calling fetch given request,
// with processResponseEndOfBody set to handleFetchDone, and processResponse
Expand Down Expand Up @@ -285,10 +289,11 @@ function fetch (input, init = undefined) {

// 4. Set responseObject to the result of creating a Response object,
// given response, "immutable", and relevantRealm.
responseObject = fromInnerResponse(response, 'immutable', relevantRealm)
responseObject = new WeakRef(fromInnerResponse(response, 'immutable'))

// 5. Resolve p with responseObject.
p.resolve(responseObject)
p.resolve(responseObject.deref())
p = null
}

controller = fetching({
Expand All @@ -297,6 +302,7 @@ function fetch (input, init = undefined) {
processResponse,
dispatcher: requestObject[kDispatcher] // undici
})

// 14. Return p.
return p.promise
})
Expand Down Expand Up @@ -420,6 +426,7 @@ function fetching ({
useParallelQueue = false,
dispatcher = getGlobalDispatcher() // undici
}) {
console.log('request', request)
// Ensure that the dispatcher is set accordingly
assert(dispatcher)

Expand Down Expand Up @@ -494,9 +501,9 @@ function fetching ({

// 9. If request’s origin is "client", then set request’s origin to request’s
// client’s origin.
console.log('reqyest', request)
if (request.origin === 'client') {
// TODO: What if request.client is null?
request.origin = request.client?.origin
request.origin = request.client.origin
}

// 10. If all of the following conditions are true:
Expand Down
22 changes: 11 additions & 11 deletions test/node-test/diagnostics-channel/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,55 +34,55 @@ describe('diagnosticsChannel for fetch', { skip }, () => {
t = tspl(t, { plan: 17 })

let startCalled = 0
diagnosticsChannel.channel('tracing:undici:fetch:start').subscribe(({ input, init, result, error }) => {
diagnosticsChannel.channel('tracing:undici:fetch:start').subscribe(({ req, input, init, result, error }) => {
startCalled += 1
if (input.redirect) {
t.strictEqual(input, 'badrequest')
t.deepStrictEqual(init, { redirect: 'error' })
} else {
t.strictEqual(input, `http://localhost:${server.address().port}`)
t.deepStrictEqual(init, {})
t.deepStrictEqual(init, undefined)
}
})

let endCalled = 0
diagnosticsChannel.channel('tracing:undici:fetch:end').subscribe(({ input, init, result, error }) => {
diagnosticsChannel.channel('tracing:undici:fetch:end').subscribe(({ req, input, init, result, error }) => {
endCalled += 1
if (init.redirect) {
if (init && init.redirect) {
t.strictEqual(input, 'badrequest')
t.deepStrictEqual(init, { redirect: 'error' })
} else {
t.strictEqual(input, `http://localhost:${server.address().port}`)
t.deepStrictEqual(init, {})
t.deepStrictEqual(init, undefined)
}
t.strictEqual(result, null)
})

let asyncStartCalled = 0
diagnosticsChannel.channel('tracing:undici:fetch:asyncStart').subscribe(({ input, init, result, error }) => {
diagnosticsChannel.channel('tracing:undici:fetch:asyncStart').subscribe(({ req, input, init, result, error }) => {
asyncStartCalled += 1
if (init.redirect) {
if (init && init.redirect) {
t.strictEqual(input, 'badrequest')
t.deepStrictEqual(init, { redirect: 'error' })
} else {
t.strictEqual(input, `http://localhost:${server.address().port}`)
t.deepStrictEqual(init, {})
t.deepStrictEqual(init, undefined)
t.ok(result)
}
})

let asyncEndCalled = 0
diagnosticsChannel.channel('tracing:undici:fetch:asyncEnd').subscribe(async ({ input, init, result, error }) => {
diagnosticsChannel.channel('tracing:undici:fetch:asyncEnd').subscribe(async ({ req, input, init, result, error }) => {
asyncEndCalled += 1
if (init.redirect) {
if (init && init.redirect) {
t.strictEqual(input, 'badrequest')
t.deepStrictEqual(init, { redirect: 'error' })
t.strictEqual(result, null)
t.ok(error)
t.strictEqual(error.cause.code, 'ERR_INVALID_URL')
} else {
t.strictEqual(input, `http://localhost:${server.address().port}`)
t.deepStrictEqual(init, {})
t.deepStrictEqual(init, undefined)
t.ok(result)
t.strictEqual(result.status, 200)
t.strictEqual(error, null)
Expand Down

0 comments on commit d7ef0fb

Please sign in to comment.