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
11 changes: 4 additions & 7 deletions lib/messages.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
const c = require('compact-encoding')
const b4a = require('b4a')
const { DEFAULT_NAMESPACE } = require('./caps')
const { INVALID_OPLOG_VERSION } = require('hypercore-errors')
const unslab = require('unslab')

const EMPTY = b4a.alloc(0)

const MANIFEST_PATCH = 0b00000001
const MANIFEST_PROLOGUE = 0b00000010
const MANIFEST_LINKED = 0b00000100
Expand Down Expand Up @@ -403,22 +400,22 @@ const dataUpgrade = {
c.uint.preencode(state, u.length)
nodeArray.preencode(state, u.nodes)
nodeArray.preencode(state, u.additionalNodes)
c.buffer.preencode(state, u.signature)
c.optionalBuffer.preencode(state, u.signature)
},
encode(state, u) {
c.uint.encode(state, u.start)
c.uint.encode(state, u.length)
nodeArray.encode(state, u.nodes)
nodeArray.encode(state, u.additionalNodes)
c.buffer.encode(state, u.signature)
c.optionalBuffer.encode(state, u.signature)
},
decode(state) {
return {
start: c.uint.decode(state),
length: c.uint.decode(state),
nodes: nodeArray.decode(state),
additionalNodes: nodeArray.decode(state),
signature: c.buffer.decode(state)
signature: c.optionalBuffer.decode(state)
}
}
}
Expand Down Expand Up @@ -454,7 +451,7 @@ const dataBlock = {
decode(state) {
return {
index: c.uint.decode(state),
value: c.buffer.decode(state) || EMPTY,
value: c.buffer.decode(state),
nodes: nodeArray.decode(state)
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"b4a": "^1.1.0",
"bare-events": "^2.2.0",
"big-sparse-array": "^1.0.3",
"compact-encoding": "^2.11.0",
"compact-encoding": "^3.0.0",
"fast-fifo": "^1.3.0",
"flat-tree": "^1.9.0",
"hypercore-crypto": "^3.2.1",
Expand Down
17 changes: 17 additions & 0 deletions test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,23 @@ test('basic byte stream', async function (t) {
t.is(expected.length, 0)
})

test('basic byte stream w/ empty buffers', async function (t) {
const core = await create(t)

// 'end' enforces that it attempts to decode the previous blocks even though they dont contribute to byte length
const expected = ['hello', 'world', '', b4a.alloc(0), 'end']

await core.append(expected)

t.is(core.length, expected.length, 'all blocks appended')
for await (const data of core.createByteStream()) {
const r = expected.shift()
t.alike(b4a.toString(data), typeof r === 'string' ? r : b4a.toString(r))
}

t.is(expected.length, 0)
})

test('basic byte stream with byteOffset / byteLength', async function (t) {
const core = await create(t)

Expand Down
Loading