-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dispatcher option to EventSource (#3119)
* add dispatcher option to EventSource * update docs * fixup
- Loading branch information
Showing
5 changed files
with
90 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
|
||
const { createServer } = require('node:http') | ||
const { once } = require('node:events') | ||
const { Agent, EventSource } = require('../..') | ||
const { tspl } = require('@matteo.collina/tspl') | ||
const { test } = require('node:test') | ||
|
||
test('EventSource allows setting custom dispatcher.', async (t) => { | ||
const { completed, deepStrictEqual } = tspl(t, { plan: 1 }) | ||
|
||
const server = createServer(async (req, res) => { | ||
res.writeHead(200, 'OK', { 'Content-Type': 'text/event-stream' }) | ||
deepStrictEqual(req.headers['x-customer-header'], 'hello world') | ||
|
||
res.end() | ||
}).listen(0) | ||
|
||
t.after(() => { | ||
server.close() | ||
eventSourceInstance.close() | ||
}) | ||
|
||
await once(server, 'listening') | ||
|
||
class CustomHeaderAgent extends Agent { | ||
dispatch (opts) { | ||
opts.headers['x-customer-header'] = 'hello world' | ||
return super.dispatch(...arguments) | ||
} | ||
} | ||
|
||
const eventSourceInstance = new EventSource(`http://localhost:${server.address().port}`, { | ||
dispatcher: new CustomHeaderAgent() | ||
}) | ||
|
||
await completed | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters