Skip to content

Commit

Permalink
fix: chunksDecode cuts off 3 characters at the end if having BOM (#2922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Mar 5, 2024
1 parent 4852c23 commit 2e174c5
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 68 deletions.
17 changes: 9 additions & 8 deletions lib/api/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,17 @@ function chunksDecode (chunks, length) {
return ''
}
const buffer = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks, length)
const bufferLength = buffer.length

// Skip BOM.
const start =
buffer.length >= 3 &&
// Skip BOM.
buffer[0] === 0xef &&
buffer[1] === 0xbb &&
buffer[2] === 0xbf
? 3
: 0
return buffer.utf8Slice(start, buffer.length - start)
bufferLength > 2 &&
buffer[0] === 0xef &&
buffer[1] === 0xbb &&
buffer[2] === 0xbf
? 3
: 0
return buffer.utf8Slice(start, bufferLength)
}

function consumeEnd (consume) {
Expand Down
170 changes: 170 additions & 0 deletions test/readable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test, describe } = require('node:test')
const Readable = require('../lib/api/readable')

describe('Readable', () => {
test('avoid body reordering', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('hello'))

process.nextTick(() => {
r.push(Buffer.from('world'))
r.push(null)
})

const text = await r.text()

t.strictEqual(text, 'helloworld')
})

test('destroy timing text', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}

const r = new Readable({ resume, abort })
r.destroy(new Error('kaboom'))

await t.rejects(r.text(), new Error('kaboom'))
})

test('destroy timing promise', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = await new Promise(resolve => {
const r = new Readable({ resume, abort })
r.destroy(new Error('kaboom'))
resolve(r)
})
await new Promise(resolve => {
r.on('error', err => {
t.ok(err)
resolve(null)
})
})
})

test('.arrayBuffer()', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('hello world'))

process.nextTick(() => {
r.push(null)
})

const arrayBuffer = await r.arrayBuffer()

const expected = new ArrayBuffer(11)
const view = new Uint8Array(expected)
view.set(Buffer.from('hello world'))
t.deepStrictEqual(arrayBuffer, expected)
})

test('.json()', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('{"hello": "world"}'))

process.nextTick(() => {
r.push(null)
})

const obj = await r.json()

t.deepStrictEqual(obj, { hello: 'world' })
})

test('.text()', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('hello world'))

process.nextTick(() => {
r.push(null)
})

const text = await r.text()

t.strictEqual(text, 'hello world')
})

test('ignore BOM', async function (t) {
t = tspl(t, { plan: 1 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push('\uFEFF')
r.push(Buffer.from('hello world'))

process.nextTick(() => {
r.push(null)
})

const text = await r.text()

t.strictEqual(text, 'hello world')
})

test('.bodyUsed', async function (t) {
t = tspl(t, { plan: 3 })

function resume () {
}
function abort () {
}
const r = new Readable({ resume, abort })

r.push(Buffer.from('hello world'))

process.nextTick(() => {
r.push(null)
})

t.strictEqual(r.bodyUsed, false)

const text = await r.text()

t.strictEqual(r.bodyUsed, true)

t.strictEqual(text, 'hello world')
})
})
60 changes: 0 additions & 60 deletions test/readable.test.js

This file was deleted.

0 comments on commit 2e174c5

Please sign in to comment.