Skip to content
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
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ function write (client, request) {
assert(contentLength === null, 'no body must not have content length')
socket.write(`${header}\r\n`, 'ascii')
}
} else if (body instanceof Uint8Array) {
} else if (util.isBuffer(body)) {
assert(contentLength !== null, 'buffer body must have content length')

socket.cork()
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Request {
this.body = null
} else if (util.isStream(body)) {
this.body = body
} else if (body instanceof Uint8Array) {
} else if (util.isBuffer(body)) {
this.body = body.length ? body : null
} else if (typeof body === 'string') {
this.body = body.length ? Buffer.from(body) : null
Expand Down
8 changes: 7 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,17 @@ function parseHeaders (headers, obj) {
return obj
}

function isBuffer (buffer) {
// See, https://github.com/mcollina/undici/pull/319
return buffer instanceof Uint8Array || Buffer.isBuffer(buffer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm totally lost on why Buffer.isBuffer would work, but hey.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related? jestjs/jest#9983

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be enough to do instanceof Uint8Array?
Buffer is a instanceof Uint8Array after all...

}

module.exports = {
nop,
isStream,
isDestroyed,
parseHeaders,
destroy,
bodyLength
bodyLength,
isBuffer
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"lint": "standard | snazzy",
"test": "tap test/*.js --no-coverage",
"test": "tap test/*.js --no-coverage && jest test/jest/test",
"coverage": "standard | snazzy && tap test/*.js",
"bench": "npx concurrently -k -s first \"node benchmarks/server.js\" \"node -e 'setTimeout(() => {}, 1000)' && node benchmarks\""
},
Expand All @@ -32,6 +32,7 @@
"benchmark": "^2.1.4",
"concurrently": "^5.2.0",
"https-pem": "^2.0.0",
"jest": "^26.4.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.0.1",
"snazzy": "^8.0.0",
Expand Down
36 changes: 36 additions & 0 deletions test/jest/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

const { Client } = require('../..')
const { createServer } = require('http')
/* global test, expect */

test('should work in jest', async () => {
const server = createServer((req, res) => {
expect(req.url).toBe('/')
expect(req.method).toBe('POST')
expect(req.headers.host).toBe('localhost')
res.setHeader('Content-Type', 'text/plain')
res.end('hello')
})
await new Promise((resolve, reject) => {
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
client.request({
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: '{}'
}, (err, result) => {
server.close()
client.close()
if (err) {
reject(err)
} else {
resolve(result)
}
})
})
})
})