Skip to content

Commit 0c0b1ee

Browse files
committed
chore: remove node buffers from runtime code
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. `Buffer`s are still used in the tests because the `Buffer` requirement needs pull out of (at least) the `cids` and `multihash` modules first.
1 parent 5ffa2b3 commit 0c0b1ee

File tree

12 files changed

+27
-31
lines changed

12 files changed

+27
-31
lines changed

packages/ipfs-unixfs-exporter/README.md

+3-3
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
}
@@ -74,7 +74,7 @@ for await (const buf of entry.content({
7474
bytes.push(buf)
7575
}
7676

77-
const content = Buffer.concat(bytes)
77+
const content = new Uint8Array(bytes)
7878

7979
console.info(content) // 0, 1, 2, 3
8080
```
@@ -185,7 +185,7 @@ for await (const chunk of entry.content({
185185
}
186186

187187
// `data` contains the first 5 bytes of the file
188-
const data = Buffer.concat(bufs)
188+
const data = new Uint8Array(bufs)
189189
```
190190
191191
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.

packages/ipfs-unixfs-exporter/package-lock.json

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

packages/ipfs-unixfs-exporter/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@
3636
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
3737
"devDependencies": {
3838
"abort-controller": "^3.0.0",
39-
"aegir": "^22.0.0",
39+
"aegir": "^25.0.0",
40+
"buffer": "^5.6.0",
4041
"chai": "^4.2.0",
4142
"chai-as-promised": "^7.1.1",
4243
"detect-node": "^2.0.4",
4344
"dirty-chai": "^2.0.1",
4445
"ipfs-unixfs-importer": "^2.0.1",
4546
"ipld": "^0.26.1",
4647
"ipld-dag-pb": "^0.19.0",
47-
"ipld-in-memory": "^4.0.0",
48+
"ipld-in-memory": "^5.0.0",
4849
"it-all": "^1.0.1",
4950
"it-buffer-stream": "^1.0.2",
5051
"it-first": "^1.0.1",
@@ -53,12 +54,11 @@
5354
"sinon": "^9.0.1"
5455
},
5556
"dependencies": {
56-
"buffer": "^5.6.0",
5757
"cids": "^0.8.0",
5858
"err-code": "^2.0.0",
5959
"hamt-sharding": "^1.0.0",
6060
"ipfs-unixfs": "^1.0.2",
6161
"it-last": "^1.0.1",
62-
"multihashing-async": "^0.8.0"
62+
"multihashing-async": "^1.0.0"
6363
}
6464
}

packages/ipfs-unixfs-exporter/src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const errCode = require('err-code')
54
const CID = require('cids')
65
const resolve = require('./resolvers')
@@ -15,7 +14,7 @@ const toPathComponents = (path = '') => {
1514
}
1615

1716
const cidAndRest = (path) => {
18-
if (Buffer.isBuffer(path)) {
17+
if (path instanceof Uint8Array) {
1918
return {
2019
cid: new CID(path),
2120
toResolve: []

packages/ipfs-unixfs-exporter/src/resolvers/unixfs-v1/content/file.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const extractDataFromBlock = require('../../../utils/extract-data-from-block')
54
const validateOffsetAndLength = require('../../../utils/validate-offset-and-length')
65
const UnixFS = require('ipfs-unixfs')
76
const errCode = require('err-code')
87

98
async function * emitBytes (ipld, node, start, end, streamPosition = 0, options) {
109
// a `raw` node
11-
if (Buffer.isBuffer(node)) {
10+
if (node instanceof Uint8Array) {
1211
const buf = extractDataFromBlock(node, streamPosition, start, end)
1312

1413
if (buf.length) {

packages/ipfs-unixfs-exporter/src/utils/extract-data-from-block.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict'
2-
const { Buffer } = require('buffer')
32

43
module.exports = function extractDataFromBlock (block, blockStart, requestedStart, requestedEnd) {
54
const blockLength = block.length
@@ -8,7 +7,7 @@ module.exports = function extractDataFromBlock (block, blockStart, requestedStar
87
if (requestedStart >= blockEnd || requestedEnd < blockStart) {
98
// If we are looking for a byte range that is starts after the start of the block,
109
// return an empty block. This can happen when internal nodes contain data
11-
return Buffer.alloc(0)
10+
return Uint8Array.from([])
1211
}
1312

1413
if (requestedEnd >= blockStart && requestedEnd < blockEnd) {

packages/ipfs-unixfs-exporter/src/utils/find-cid-in-shard.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict'
22

3-
const { Buffer } = require('buffer')
43
const Bucket = require('hamt-sharding/src/bucket')
54
const multihashing = require('multihashing-async')
65

76
// FIXME: this is copy/pasted from ipfs-unixfs-importer/src/dir-sharded.js
87
const hashFn = async function (value) {
9-
const hash = await multihashing(Buffer.from(value, 'utf8'), 'murmur3-128')
8+
const buf = new TextEncoder('utf-8').encode(value)
9+
const hash = await multihashing(buf, 'murmur3-128')
1010

1111
// Multihashing inserts preamble of 2 bytes. Remove it.
1212
// Also, murmur3 outputs 128 bit but, accidently, IPFS Go's
1313
// implementation only uses the first 64, so we must do the same
1414
// for parity..
1515
const justHash = hash.slice(2, 10)
1616
const length = justHash.length
17-
const result = Buffer.alloc(length)
17+
const result = new Uint8Array(length)
1818
// TODO: invert buffer because that's how Go impl does it
1919
for (let i = 0; i < length; i++) {
2020
result[length - i - 1] = justHash[i]

packages/ipfs-unixfs-importer/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@
3535
},
3636
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
3737
"devDependencies": {
38-
"aegir": "^22.0.0",
38+
"aegir": "^25.0.0",
39+
"buffer": "^5.6.0",
3940
"chai": "^4.2.0",
4041
"cids": "^0.8.0",
4142
"detect-node": "^2.0.4",
4243
"dirty-chai": "^2.0.1",
4344
"ipfs-unixfs-exporter": "^2.0.1",
4445
"ipld": "^0.26.1",
45-
"ipld-in-memory": "^4.0.0",
46+
"ipld-in-memory": "^5.0.0",
4647
"it-buffer-stream": "^1.0.2",
4748
"it-last": "^1.0.1",
4849
"merge-options": "^2.0.0",
@@ -52,7 +53,6 @@
5253
},
5354
"dependencies": {
5455
"bl": "^4.0.0",
55-
"buffer": "^5.6.0",
5656
"err-code": "^2.0.0",
5757
"hamt-sharding": "^1.0.0",
5858
"ipfs-unixfs": "^1.0.2",
@@ -62,7 +62,7 @@
6262
"it-first": "^1.0.1",
6363
"it-parallel-batch": "^1.0.3",
6464
"merge-options": "^2.0.0",
65-
"multihashing-async": "^0.8.0",
65+
"multihashing-async": "^1.0.0",
6666
"rabin-wasm": "^0.1.1"
6767
}
6868
}

packages/ipfs-unixfs-importer/src/dag-builder/validate-chunks.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const errCode = require('err-code')
4-
const { Buffer } = require('buffer')
54

65
// make sure the content only emits buffer-a-likes
76
async function * validateChunks (source) {
@@ -11,9 +10,9 @@ async function * validateChunks (source) {
1110
}
1211

1312
if (typeof content === 'string' || content instanceof String) {
14-
yield Buffer.from(content, 'utf8')
13+
yield new TextEncoder('utf-8').encode(content)
1514
} else if (Array.isArray(content)) {
16-
yield Buffer.from(content)
15+
yield Uint8Array.from(content)
1716
} else {
1817
yield content
1918
}

packages/ipfs-unixfs-importer/src/dir-sharded.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
DAGLink,
55
DAGNode
66
} = require('ipld-dag-pb')
7-
const { Buffer } = require('buffer')
87
const UnixFS = require('ipfs-unixfs')
98
const multihashing = require('multihashing-async')
109
const Dir = require('./dir')
@@ -13,15 +12,16 @@ const Bucket = require('hamt-sharding')
1312
const mergeOptions = require('merge-options').bind({ ignoreUndefined: true })
1413

1514
const hashFn = async function (value) {
16-
const hash = await multihashing(Buffer.from(value, 'utf8'), 'murmur3-128')
15+
const buf = new TextEncoder('utf-8').encode(value)
16+
const hash = await multihashing(buf, 'murmur3-128')
1717

1818
// Multihashing inserts preamble of 2 bytes. Remove it.
1919
// Also, murmur3 outputs 128 bit but, accidently, IPFS Go's
2020
// implementation only uses the first 64, so we must do the same
2121
// for parity..
2222
const justHash = hash.slice(2, 10)
2323
const length = justHash.length
24-
const result = Buffer.alloc(length)
24+
const result = new Uint8Array(length)
2525
// TODO: invert buffer because that's how Go impl does it
2626
for (let i = 0; i < length; i++) {
2727
result[length - i - 1] = justHash[i]
@@ -142,7 +142,7 @@ async function * flush (path, bucket, block, shardRoot, options) {
142142

143143
// go-ipfs uses little endian, that's why we have to
144144
// reverse the bit field before storing it
145-
const data = Buffer.from(children.bitField().reverse())
145+
const data = new Uint8Array(children.bitField().reverse())
146146
const dir = new UnixFS({
147147
type: 'hamt-sharded-directory',
148148
data,

packages/ipfs-unixfs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ const data = new UnixFS([options])
141141
- `metadata`
142142
- `symlink`
143143
- `hamt-sharded-directory`
144-
- data (Buffer): The optional data field for this node
144+
- data (Uint8Array): The optional data field for this node
145145
- blockSizes (Array, default: `[]`): If this is a `file` node that is made up of multiple blocks, `blockSizes` is a list numbers that represent the size of the file chunks stored in each child node. It is used to calculate the total file size.
146146
- mode (Number, default `0644` for files, `0755` for directories/hamt-sharded-directories) file mode
147147
- mtime (`Date`, `{ secs, nsecs }`, `{ Seconds, FractionalNanoseconds }`, `[ secs, nsecs ]`): The modification time of this node

packages/ipfs-unixfs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"homepage": "https://github.com/ipfs/js-ipfs-unixfs#readme",
3737
"devDependencies": {
38-
"aegir": "^22.0.0",
38+
"aegir": "^25.0.0",
3939
"chai": "^4.2.0",
4040
"dirty-chai": "^2.0.1",
4141
"nyc": "^15.0.0"

0 commit comments

Comments
 (0)