Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coverage #4

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ function createThreadInterceptor (opts) {

const port = roundRobin.next()

if (opts.headers) {
delete opts.headers.connection
delete opts.headers['transfer-encoding']
const headers = {
...opts?.headers,
}

delete headers.connection
delete headers['transfer-encoding']
headers.host = url.host

const id = nextId()
const newOpts = {
...opts,
headers,
}
delete newOpts.dispatcher

Expand Down Expand Up @@ -169,7 +173,7 @@ function wire (server, port, opts) {
statusCode: res.statusCode,
}

if (res.headers['content-length'].indexOf('application/json')) {
if (res.headers['content-type'].indexOf('application/json')) {
// fast path because it's utf-8, use a string
newRes.rawPayload = res.payload
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "An Undici interceptor that routes requests over a worker thread",
"main": "index.js",
"scripts": {
"test": "eslint && borp --coverage"
"test": "eslint && borp --coverage --check-coverage"
},
"keywords": [
"undici",
Expand Down
108 changes: 107 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const { test } = require('node:test')
const { deepStrictEqual, strictEqual } = require('node:assert')
const { deepStrictEqual, strictEqual, rejects } = require('node:assert')
const { join } = require('path')
const { Worker } = require('worker_threads')
const { createThreadInterceptor } = require('../')
const { Agent, request } = require('undici')
const { once } = require('events')
const { setTimeout: sleep } = require('timers/promises')
const Fastify = require('fastify')

test('basic', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
Expand Down Expand Up @@ -113,3 +114,108 @@ test('two service in a mesh, one is terminated, then a message is sent', async (
statusCode: 500,
})
})

test('buffer', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { statusCode, body } = await request('http://myserver.local/buffer', {
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(Buffer.from(await body.arrayBuffer()), Buffer.from('hello'))
})

test('handle errors from inject', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'error.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

await rejects(request('http://myserver.local', {
dispatcher: agent,
}), new Error('kaboom'))
})

test('pass through with domain', async (t) => {
const app = Fastify()
app.get('/', async () => {
return { hello: 'world' }
})
await app.listen({ port: 0 })
t.after(() => app.close())

const interceptor = createThreadInterceptor({
domain: '.local',
})

const agent = new Agent().compose(interceptor)

const { statusCode, body } = await request(app.listeningOrigin, {
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(await body.json(), { hello: 'world' })
})

test('unwanted headers are removed', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { statusCode, body } = await request('http://myserver.local/echo-headers', {
headers: {
'x-foo': 'bar',
connection: 'keep-alive',
'transfer-encoding': 'chunked',
},
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(await body.json(), {
'user-agent': 'lightMyRequest',
host: 'myserver.local',
'x-foo': 'bar',
})
})

test('multiple headers', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { statusCode, body, headers } = await request('http://myserver.local/headers', {
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(headers['x-foo'], ['bar', 'baz'])
await body.json()
})
8 changes: 8 additions & 0 deletions test/fixtures/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

const { parentPort } = require('worker_threads')
const { wire } = require('../../')

wire(function (req, res) {
res.destroy(new Error('kaboom'))
}, parentPort)
18 changes: 16 additions & 2 deletions test/fixtures/worker1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,26 @@ const { wire } = require('../../')

const app = fastify()

app.get('/', async (req, reply) => {
app.get('/', (req, reply) => {
reply.send({ hello: workerData?.message || 'world' })
})

app.get('/whoami', async (req, reply) => {
app.get('/whoami', (req, reply) => {
reply.send({ threadId })
})

app.get('/buffer', (req, reply) => {
reply.send(Buffer.from('hello'))
})

app.get('/echo-headers', (req, reply) => {
reply.send(req.headers)
})

app.get('/headers', (req, reply) => {
reply
.header('x-foo', ['bar', 'baz'])
.send({ hello: 'world' })
})

wire(app, parentPort)
6 changes: 6 additions & 0 deletions test/round-robin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { Worker } = require('worker_threads')
const { createThreadInterceptor } = require('../')
const { Agent, request } = require('undici')
const { once } = require('events')
const RoundRobin = require('../lib/roundrobin')

test('round-robin .route with array', async (t) => {
const worker1 = new Worker(join(__dirname, 'fixtures', 'worker1.js'), {
Expand Down Expand Up @@ -141,3 +142,8 @@ test('round-robin one worker exits, in flight request', async (t) => {
dispatcher: agent,
}))
})

test('RoundRobin remove unknown port', () => {
const rr = new RoundRobin()
rr.remove({})
})
Loading