Skip to content

Commit 9999dc9

Browse files
committed
fix bufferisms
1 parent c2c012c commit 9999dc9

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const mutexify = require('mutexify')
2+
const b4a = require('b4a')
23

34
const { BlobReadStream, BlobWriteStream } = require('./lib/streams')
45

@@ -22,12 +23,12 @@ module.exports = class Hyperblobs {
2223
}
2324

2425
async put (blob, opts) {
25-
if (!Buffer.isBuffer(blob)) blob = Buffer.from(blob)
26+
if (!b4a.isBuffer(blob)) blob = b4a.from(blob)
2627
const blockSize = (opts && opts.blockSize) || this.blockSize
2728

2829
const stream = this.createWriteStream(opts)
2930
for (let i = 0; i < blob.length; i += blockSize) {
30-
stream.write(blob.slice(i, i + blockSize))
31+
stream.write(blob.subarray(i, i + blockSize))
3132
}
3233
stream.end()
3334

@@ -42,7 +43,8 @@ module.exports = class Hyperblobs {
4243
for await (const block of this.createReadStream(id, opts)) {
4344
res.push(block)
4445
}
45-
return Buffer.concat(res)
46+
if (res.length === 1) return res[0]
47+
return b4a.concat(res)
4648
}
4749

4850
createReadStream (id, opts) {

lib/streams.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class BlobReadStream extends Readable {
148148

149149
const remainder = this._end - this._pos
150150
if (this._relativeOffset || (remainder < block.length)) {
151-
block = block.slice(this._relativeOffset, this._relativeOffset + remainder)
151+
block = block.subarray(this._relativeOffset, this._relativeOffset + remainder)
152152
}
153153

154154
this._index++

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
},
2323
"homepage": "https://github.com/hypercore-protocol/hyperblobs#readme",
2424
"dependencies": {
25+
"b4a": "^1.6.1",
2526
"mutexify": "^1.4.0",
2627
"streamx": "^2.12.4"
2728
},
2829
"devDependencies": {
30+
"brittle": "^3.1.0",
2931
"hypercore": "^10.0.0",
3032
"random-access-memory": "^6.0.0",
31-
"standard": "^17.0.0",
32-
"brittle": "^3.1.0"
33+
"standard": "^17.0.0"
3334
}
3435
}

0 commit comments

Comments
 (0)