Skip to content

Commit

Permalink
feat(websocket): allow setting custom headers (nodejs#2089)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 2641aa5 commit b30fb85
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const { fireEvent, failWebsocketConnection } = require('./util')
const { CloseEvent } = require('./events')
const { makeRequest } = require('../fetch/request')
const { fetching } = require('../fetch/index')
const { Headers } = require('../fetch/headers')
const { getGlobalDispatcher } = require('../global')
const { kHeadersList } = require('../core/symbols')

const channels = {}
channels.open = diagnosticsChannel.channel('undici:websocket:open')
Expand Down Expand Up @@ -49,6 +51,13 @@ function establishWebSocketConnection (url, protocols, ws, onEstablish, options)
redirect: 'error'
})

// Note: undici extension, allow setting custom headers.
if (options.headers) {
const headersList = new Headers(options.headers)[kHeadersList]

request.headersList = headersList
}

// 3. Append (`Upgrade`, `websocket`) to request’s header list.
// 4. Append (`Connection`, `Upgrade`) to request’s header list.
// Note: both of these are handled by undici currently.
Expand Down
4 changes: 4 additions & 0 deletions lib/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ webidl.converters.WebSocketInit = webidl.dictionaryConverter([
get defaultValue () {
return getGlobalDispatcher()
}
},
{
key: 'headers',
converter: webidl.nullableConverter(webidl.converters.HeadersInit)
}
])

Expand Down
30 changes: 30 additions & 0 deletions test/websocket/custom-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { test } = require('tap')
const assert = require('assert')
const { Agent, WebSocket } = require('../..')

test('Setting custom headers', (t) => {
t.plan(1)

const headers = {
'x-khafra-hello': 'hi',
Authorization: 'Bearer base64orsomethingitreallydoesntmatter'
}

class TestAgent extends Agent {
dispatch (options) {
t.match(options.headers, headers)

return false
}
}

const ws = new WebSocket('wss://echo.websocket.events', {
headers,
dispatcher: new TestAgent()
})

// We don't want to make a request, just ensure the headers are set.
ws.onclose = ws.onerror = ws.onmessage = assert.fail
})
4 changes: 3 additions & 1 deletion types/websocket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
EventListenerOrEventListenerObject
} from './patch'
import Dispatcher from './dispatcher'
import { HeadersInit } from './fetch'

export type BinaryType = 'blob' | 'arraybuffer'

Expand Down Expand Up @@ -125,5 +126,6 @@ export declare const MessageEvent: {

interface WebSocketInit {
protocols?: string | string[],
dispatcher?: Dispatcher
dispatcher?: Dispatcher,
headers?: HeadersInit
}

0 comments on commit b30fb85

Please sign in to comment.