Skip to content

Commit

Permalink
Merge branch 'main' into http2
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 24, 2023
2 parents bbd1ffe + 37e7fd8 commit 718c3e9
Show file tree
Hide file tree
Showing 206 changed files with 637 additions and 338 deletions.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Expand Up @@ -1760,7 +1760,7 @@ async function httpNetworkFetch (
fetchParams.controller.connection.destroy()

// 2. Return the appropriate network error for fetchParams.
return makeAppropriateNetworkError(fetchParams)
return makeAppropriateNetworkError(fetchParams, err)
}

return makeNetworkError(err)
Expand Down
6 changes: 3 additions & 3 deletions lib/fetch/response.js
Expand Up @@ -426,15 +426,15 @@ function filterResponse (response, type) {
}

// https://fetch.spec.whatwg.org/#appropriate-network-error
function makeAppropriateNetworkError (fetchParams) {
function makeAppropriateNetworkError (fetchParams, err = null) {
// 1. Assert: fetchParams is canceled.
assert(isCancelled(fetchParams))

// 2. Return an aborted network error if fetchParams is aborted;
// otherwise return a network error.
return isAborted(fetchParams)
? makeNetworkError(new DOMException('The operation was aborted.', 'AbortError'))
: makeNetworkError('Request was cancelled.')
? makeNetworkError(Object.assign(new DOMException('The operation was aborted.', 'AbortError'), { cause: err }))
: makeNetworkError(Object.assign(new DOMException('Request was cancelled.'), { cause: err }))
}

// https://whatpr.org/fetch/1392.html#initialize-a-response
Expand Down
37 changes: 24 additions & 13 deletions lib/websocket/websocket.js
Expand Up @@ -3,6 +3,7 @@
const { webidl } = require('../fetch/webidl')
const { DOMException } = require('../fetch/constants')
const { URLSerializer } = require('../fetch/dataURL')
const { getGlobalOrigin } = require('../fetch/global')
const { staticPropertyDescriptors, states, opcodes, emptyBuffer } = require('./constants')
const {
kWebSocketURL,
Expand Down Expand Up @@ -57,38 +58,48 @@ class WebSocket extends EventTarget {
url = webidl.converters.USVString(url)
protocols = options.protocols

// 1. Let urlRecord be the result of applying the URL parser to url.
// 1. Let baseURL be this's relevant settings object's API base URL.
const baseURL = getGlobalOrigin()

// 1. Let urlRecord be the result of applying the URL parser to url with baseURL.
let urlRecord

try {
urlRecord = new URL(url)
urlRecord = new URL(url, baseURL)
} catch (e) {
// 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
// 3. If urlRecord is failure, then throw a "SyntaxError" DOMException.
throw new DOMException(e, 'SyntaxError')
}

// 3. If urlRecord’s scheme is not "ws" or "wss", then throw a
// "SyntaxError" DOMException.
// 4. If urlRecord’s scheme is "http", then set urlRecord’s scheme to "ws".
if (urlRecord.protocol === 'http:') {
urlRecord.protocol = 'ws:'
} else if (urlRecord.protocol === 'https:') {
// 5. Otherwise, if urlRecord’s scheme is "https", set urlRecord’s scheme to "wss".
urlRecord.protocol = 'wss:'
}

// 6. If urlRecord’s scheme is not "ws" or "wss", then throw a "SyntaxError" DOMException.
if (urlRecord.protocol !== 'ws:' && urlRecord.protocol !== 'wss:') {
throw new DOMException(
`Expected a ws: or wss: protocol, got ${urlRecord.protocol}`,
'SyntaxError'
)
}

// 4. If urlRecord’s fragment is non-null, then throw a "SyntaxError"
// 7. If urlRecord’s fragment is non-null, then throw a "SyntaxError"
// DOMException.
if (urlRecord.hash) {
if (urlRecord.hash || urlRecord.href.endsWith('#')) {
throw new DOMException('Got fragment', 'SyntaxError')
}

// 5. If protocols is a string, set protocols to a sequence consisting
// 8. If protocols is a string, set protocols to a sequence consisting
// of just that string.
if (typeof protocols === 'string') {
protocols = [protocols]
}

// 6. If any of the values in protocols occur more than once or otherwise
// 9. If any of the values in protocols occur more than once or otherwise
// fail to match the requirements for elements that comprise the value
// of `Sec-WebSocket-Protocol` fields as defined by The WebSocket
// protocol, then throw a "SyntaxError" DOMException.
Expand All @@ -100,12 +111,12 @@ class WebSocket extends EventTarget {
throw new DOMException('Invalid Sec-WebSocket-Protocol value', 'SyntaxError')
}

// 7. Set this's url to urlRecord.
this[kWebSocketURL] = urlRecord
// 10. Set this's url to urlRecord.
this[kWebSocketURL] = new URL(urlRecord.href)

// 8. Let client be this's relevant settings object.
// 11. Let client be this's relevant settings object.

// 9. Run this step in parallel:
// 12. Run this step in parallel:

// 1. Establish a WebSocket connection given urlRecord, protocols,
// and client.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -67,7 +67,7 @@
"fuzz": "jsfuzz test/fuzzing/fuzz.js corpus"
},
"devDependencies": {
"@sinonjs/fake-timers": "^10.0.2",
"@sinonjs/fake-timers": "^11.1.0",
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
"atomic-sleep": "^1.0.0",
Expand Down
24 changes: 24 additions & 0 deletions test/proxy-agent.js
Expand Up @@ -443,6 +443,30 @@ test('should throw when proxy does not return 200', async (t) => {
t.end()
})

test('pass ProxyAgent proxy status code error when using fetch - #2161', { skip: nodeMajor < 16 }, async (t) => {
const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`

proxy.authenticate = function (req, fn) {
fn(null, false)
}

const proxyAgent = new ProxyAgent(proxyUrl)
try {
await fetch(serverUrl, { dispatcher: proxyAgent })
} catch (e) {
t.hasProp(e, 'cause')
}

server.close()
proxy.close()
proxyAgent.close()
t.end()
})

test('Proxy via HTTP to HTTPS endpoint', async (t) => {
t.plan(4)

Expand Down
8 changes: 0 additions & 8 deletions test/websocket/constructor.js
Expand Up @@ -12,14 +12,6 @@ test('Constructor', (t) => {
}
)

t.throws(
() => new WebSocket('https://www.google.com'),
{
name: 'SyntaxError',
constructor: DOMException
}
)

t.throws(
() => new WebSocket('wss://echo.websocket.events/#a'),
{
Expand Down
4 changes: 3 additions & 1 deletion test/wpt/server/server.mjs
Expand Up @@ -384,7 +384,9 @@ const send = (message) => {
}
}

send({ server: `http://localhost:${server.address().port}` })
const url = `http://localhost:${server.address().port}`
console.log('server opened ' + url)
send({ server: url })

process.on('message', (message) => {
if (message === 'shutdown') {
Expand Down
9 changes: 8 additions & 1 deletion test/wpt/server/websocket.mjs
Expand Up @@ -11,10 +11,17 @@ const wss = new WebSocketServer({
handleProtocols: (protocols) => [...protocols].join(', ')
})

wss.on('connection', (ws) => {
wss.on('connection', (ws, request) => {
ws.on('message', (data, isBinary) => {
const str = data.toString('utf-8')

if (request.url === '/receive-many-with-backpressure') {
setTimeout(() => {
ws.send(str.length.toString(), { binary: false })
}, 100)
return
}

if (str === 'Goodbye') {
// Close-server-initiated-close.any.js sends a "Goodbye" message
// when it wants the server to close the connection.
Expand Down
26 changes: 26 additions & 0 deletions test/wpt/status/websockets.status.json
Expand Up @@ -85,5 +85,31 @@
"flaky": [
"Send 0 byte data on a WebSocket - Connection should be closed"
]
},
"send-many-64K-messages-with-backpressure.any.js": {
"note": "probably flaky based on other flaky tests.",
"flaky": [
"sending 50 messages of size 65536 with backpressure applied should not hang"
]
},
"back-forward-cache-with-closed-websocket-connection-ccns.tentative.window.js": {
"skip": true,
"note": "browser-only test"
},
"back-forward-cache-with-closed-websocket-connection.window.js": {
"skip": true,
"note": "browser-only test"
},
"back-forward-cache-with-open-websocket-connection-ccns.tentative.window.js": {
"skip": true,
"note": "browser-only test"
},
"back-forward-cache-with-open-websocket-connection.window.js": {
"skip": true,
"note": "browser-only test"
},
"mixed-content.https.any.js": {
"note": "node has no concept of origin, thus there is no 'secure' or 'insecure' contexts",
"skip": true
}
}
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-1000-reason.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-1000-verify-code.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-1000.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-1005-verify-code.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-1005.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-2999-reason.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-3000-reason.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-3000-verify-code.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-4999-reason.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-Reason-124Bytes.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wpt_flags=h2
// META: variant=?wss

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-delayed.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-onlyReason.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-readyState-Closed.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-readyState-Closing.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wpt_flags=h2
// META: variant=?wss

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Close-undefined.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wpt_flags=h2
// META: variant=?wss

Expand Down
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Create-blocked-port.any.js
@@ -1,5 +1,5 @@
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wss
// META: variant=?wpt_flags=h2

Expand Down
2 changes: 1 addition & 1 deletion test/wpt/tests/websockets/Create-extensions-empty.any.js
@@ -1,6 +1,6 @@
// META: timeout=long
// META: script=constants.sub.js
// META: variant=
// META: variant=?default
// META: variant=?wpt_flags=h2
// META: variant=?wss

Expand Down
19 changes: 19 additions & 0 deletions test/wpt/tests/websockets/Create-http-urls.any.js
@@ -0,0 +1,19 @@
test(() => {
const url = new URL ("/", location);
url.protocol = "http";
const httpURL = url.href;
url.protocol = "https";
const httpsURL = url.href;
url.protocol = "ws";
const wsURL = url.href;
url.protocol = "wss";
const wssURL = url.href;

let ws = new WebSocket(httpURL);
assert_equals(ws.url, wsURL);
ws.close();

ws = new WebSocket(httpsURL);
assert_equals(ws.url, wssURL);
ws.close();
}, "WebSocket: ensure both HTTP schemes are supported");

0 comments on commit 718c3e9

Please sign in to comment.