diff --git a/lib/messages.js b/lib/messages.js index 1993ec8a..37e8b17c 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -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 @@ -403,14 +400,14 @@ 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 { @@ -418,7 +415,7 @@ const dataUpgrade = { length: c.uint.decode(state), nodes: nodeArray.decode(state), additionalNodes: nodeArray.decode(state), - signature: c.buffer.decode(state) + signature: c.optionalBuffer.decode(state) } } } @@ -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) } } diff --git a/package.json b/package.json index 4848a184..f04b3bf1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/streams.js b/test/streams.js index 48218726..6441420b 100644 --- a/test/streams.js +++ b/test/streams.js @@ -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)