From b693f19f3b547b86761083c0fc8c3d9b4b72df9c Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Wed, 21 Aug 2024 11:16:14 +0200 Subject: [PATCH 1/2] fix: Do not assume response header. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index c0ff8fa..f8aa509 100644 --- a/index.js +++ b/index.js @@ -228,7 +228,7 @@ function wire ({ server: newServer, port, ...undiciOpts }) { statusCode: res.statusCode, } - if (res.headers['content-type'].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 { From 600756d3bbf17a30497ac4c009717dca0deb1b62 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Wed, 21 Aug 2024 11:38:57 +0200 Subject: [PATCH 2/2] chore: Added test. --- test/basic.test.js | 22 +++++++++++++++++++++- test/fixtures/worker1.js | 5 +++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/test/basic.test.js b/test/basic.test.js index 04694e9..fbb7d5b 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,7 +1,7 @@ 'use strict' const { test } = require('node:test') -const { deepStrictEqual, strictEqual, rejects } = require('node:assert') +const { deepStrictEqual, strictEqual, rejects, ifError } = require('node:assert') const { join } = require('path') const { Worker } = require('worker_threads') const { createThreadInterceptor } = require('../') @@ -134,6 +134,26 @@ test('buffer', async (t) => { deepStrictEqual(Buffer.from(await body.arrayBuffer()), Buffer.from('hello')) }) +test('no response 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, headers, body } = await request('http://myserver.local/no-headers', { + dispatcher: agent, + }) + + strictEqual(statusCode, 200) + ifError(headers['content-type']) + deepStrictEqual(await body.text(), 'text') +}) + test('handle errors from inject', async (t) => { const worker = new Worker(join(__dirname, 'fixtures', 'error.js')) t.after(() => worker.terminate()) diff --git a/test/fixtures/worker1.js b/test/fixtures/worker1.js index c25a198..b77696e 100644 --- a/test/fixtures/worker1.js +++ b/test/fixtures/worker1.js @@ -1,5 +1,6 @@ 'use strict' +const { Readable } = require('stream') const { parentPort, workerData, threadId } = require('worker_threads') const fastify = require('fastify') const { wire } = require('../../') @@ -28,4 +29,8 @@ app.get('/headers', (req, reply) => { .send({ hello: 'world' }) }) +app.get('/no-headers', (req, reply) => { + reply.send(Readable.from(['text'], { objectMode: false })) +}) + wire({ server: app, port: parentPort })