Skip to content

Commit db60a42

Browse files
authored
chore: remove node buffers from runtime code (#66)
Node `Buffers` are subclasses of `Uint8Array` and we don't use any `Buffer`-specific functions, so do not demand `Buffer`s where `Uint8Array`s will do. If other modules in the stack actually require `Buffer`s, let the failure occur there. `Buffer`s are still used in the tests because the `Buffer` requirement needs pull out of (at least) the `cids` and `multihash` modules first. BREAKING CHANGE: does not convert input to node Buffers any more, uses Uint8Arrays instead
1 parent b8b2ee3 commit db60a42

File tree

16 files changed

+826
-1002
lines changed

16 files changed

+826
-1002
lines changed

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"lerna": "3.20.2",
2+
"lerna": "3.22.1",
33
"packages": [
44
"packages/*"
55
],

package-lock.json

+776-459
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"update-contributors": "aegir release --lint=false --test=false --bump=false --build=false --changelog=false --commit=false --tag=false --push=false --ghrelease=false --docs=false --publish=false"
2020
},
2121
"devDependencies": {
22-
"lerna": "^3.20.2"
22+
"lerna": "^3.22.1"
2323
},
2424
"repository": {
2525
"type": "git",

packages/ipfs-unixfs-exporter/README.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const files = []
5050

5151
for await (const file of importer([{
5252
path: '/foo/bar.txt',
53-
content: Buffer.from(0, 1, 2, 3)
53+
content: new Uint8Array([0, 1, 2, 3])
5454
}], ipld)) {
5555
files.push(file)
5656
}
@@ -65,18 +65,16 @@ console.info(entry.name) // bar.txt
6565
console.info(entry.unixfs.fileSize()) // 4
6666

6767
// stream content from unixfs node
68-
const bytes = []
68+
const size = entry.unixfs.fileSize()
69+
const bytes = new Uint8Array(size)
70+
let offset = 0
6971

70-
for await (const buf of entry.content({
71-
offset: 0, // optional offset
72-
length: 4 // optional length
73-
})) {
74-
bytes.push(buf)
72+
for await (const buf of entry.content()) {
73+
bytes.set(buf, offset)
74+
offset += chunk.length
7575
}
7676

77-
const content = Buffer.concat(bytes)
78-
79-
console.info(content) // 0, 1, 2, 3
77+
console.info(bytes) // 0, 1, 2, 3
8078
```
8179

8280
#### API
@@ -175,17 +173,20 @@ There is no `content` function for a `CBOR` node.
175173
When `entry` is a file or a `raw` node, `offset` and/or `length` arguments can be passed to `entry.content()` to return slices of data:
176174
177175
```javascript
178-
const bufs = []
176+
const length = 5
177+
const data = new Uint8Array(length)
178+
let offset = 0
179179

180180
for await (const chunk of entry.content({
181181
offset: 0,
182-
length: 5
182+
length
183183
})) {
184-
bufs.push(chunk)
184+
data.set(chunk, offset)
185+
offset += chunk.length
185186
}
186187

187188
// `data` contains the first 5 bytes of the file
188-
const data = Buffer.concat(bufs)
189+
return data
189190
```
190191
191192
If `entry` is a directory or hamt shard, passing `offset` and/or `length` to `entry.content()` will limit the number of files returned from the directory.

0 commit comments

Comments
 (0)