Skip to content
Open
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/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Blob {
}

text () {
return Promise.resolve(this[BUFFER].toString())
return Promise.resolve(this[BUFFER].toString().replace(/^\uFEFF/, ''))
}

arrayBuffer () {
Expand Down
5 changes: 3 additions & 2 deletions lib/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ try {

const INTERNALS = Symbol('Body internals')
const CONSUME_BODY = Symbol('consumeBody')
const utf8Decode = buffer => buffer.toString().replace(/^\uFEFF/, '')

class Body {
constructor (bodyArg, options = {}) {
Expand Down Expand Up @@ -74,7 +75,7 @@ class Body {
async json () {
const buf = await this[CONSUME_BODY]()
try {
return JSON.parse(buf.toString())
return JSON.parse(utf8Decode(buf))
} catch (er) {
throw new FetchError(
`invalid json response body at ${this.url} reason: ${er.message}`,
Expand All @@ -84,7 +85,7 @@ class Body {
}

text () {
return this[CONSUME_BODY]().then(buf => buf.toString())
return this[CONSUME_BODY]().then(utf8Decode)
}

buffer () {
Expand Down
16 changes: 16 additions & 0 deletions test/blob.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ t.test('expose the BUFFER symbol as read-only static property', t => {
t.throws(() => Blob.BUFFER = 'fubber')
t.end()
})

t.test('UTF-8 text decoding discards only the initial BOM', async t => {
for (const [text, expected] of [
['\uFEFFtext', 'text'],
['\uFEFF\uFEFFtext', '\uFEFFtext'],
['text\uFEFFinside', 'text\uFEFFinside'],
['\uFEFF', ''],
]) {
const bytes = Buffer.from(text)
const b = new Blob([bytes])
t.equal(await b.text(), expected)
t.equal(b.size, bytes.length)
t.same(Buffer.from(await b.arrayBuffer()), bytes)
t.same(await b.stream().concat(), bytes)
}
})
51 changes: 51 additions & 0 deletions test/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,54 @@ t.test('convert body', t => {

t.end()
})

t.test('UTF-8 text decoding discards only the initial BOM', async t => {
const cases = [
[Buffer.from('\uFEFFtext'), 'text'],
[Buffer.from('\uFEFF\uFEFFtext'), '\uFEFFtext'],
[Buffer.from('text\uFEFFinside'), 'text\uFEFFinside'],
[Buffer.from('\uFEFF'), ''],
[Buffer.alloc(0), ''],
[Buffer.from([0xef, 0xbb]), '\uFFFD'],
[Buffer.from([0xff, 0xef, 0xbb, 0xbf]), '\uFFFD\uFEFF'],
]
for (const [bytes, expected] of cases) {
t.equal(await new Body(bytes).text(), expected)
}
})

t.test('JSON decoding discards the initial UTF-8 BOM', async t => {
const bytes = Buffer.from('\uFEFF{"inner":"a\uFEFFb"}')
const b = new Body(bytes)
t.same(await b.json(), { inner: 'a\uFEFFb' })
t.equal(b.bodyUsed, true)
await t.rejects(b.text(), TypeError)
})

t.test('JSON decoding does not discard interior or repeated BOMs', async t => {
for (const text of ['\uFEFF\uFEFF{}', ' \uFEFF{}']) {
await t.rejects(new Body(text).json(), { type: 'invalid-json' })
}
})

t.test('UTF-8 BOM bytes survive raw body reads', async t => {
const bytes = Buffer.from('\uFEFFtext')
t.same(await new Body(bytes).buffer(), bytes)
t.same(Buffer.from(await new Body(bytes).arrayBuffer()), bytes)
const blob = await new Body(bytes).blob()
t.equal(blob.size, bytes.length)
t.same(Buffer.from(await blob.arrayBuffer()), bytes)
t.equal(await new Body(bytes).textConverted(), 'text')
})

t.test('UTF-8 BOM split across stream chunks', async t => {
const make = () => {
const stream = new Minipass()
stream.write(Buffer.from([0xef]))
stream.write(Buffer.from([0xbb]))
stream.end(Buffer.concat([Buffer.from([0xbf]), Buffer.from('{"ok":true}')]))
return new Body(stream)
}
t.equal(await make().text(), '{"ok":true}')
t.same(await make().json(), { ok: true })
})