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

fix: chunksDecode cuts off 3 characters at the end if having BOM #2922

Merged
merged 1 commit into from
Mar 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

buffer length - start was wrong. needed to slice till the buffer end ;).

}

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.