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
4 changes: 3 additions & 1 deletion src/schema/yaml-1.1/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export const binary: ScalarTag = {
str =
buf instanceof Buffer
? buf.toString('base64')
: Buffer.from(buf.buffer).toString('base64')
: Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength).toString(
'base64'
)
} else if (typeof btoa === 'function') {
let s = ''
for (let i = 0; i < buf.length; ++i) s += String.fromCharCode(buf[i])
Expand Down
14 changes: 14 additions & 0 deletions tests/doc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,20 @@ generic: !!binary |-
description: The binary value above is a tiny arrow encoded as a gif image.\n`)
})

test('!!binary stringify honours byteOffset and byteLength', () => {
const view = new Uint8Array([9, 9, 0, 1, 2, 255]).subarray(2)
const src = stringify(view, { version: '1.1' })
const value = parse(src, { version: '1.1' })
expect(value).toBeInstanceOf(Uint8Array)
expect([...value]).toEqual([0, 1, 2, 255])

const view2 = new Uint8Array(new ArrayBuffer(8), 2, 3)
view2.set([5, 6, 7])
const src2 = stringify(view2, { version: '1.1' })
const value2 = parse(src2, { version: '1.1' })
expect([...value2]).toEqual([5, 6, 7])
})

test('!!bool', () => {
const src = `
canonical: y
Expand Down