Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 61ae14f

Browse files
author
Alan Shaw
committed
refactor: async iterables
1 parent 0565f47 commit 61ae14f

File tree

226 files changed

+4571
-6267
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

226 files changed

+4571
-6267
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ a case, you should provide a way to trigger migrations manually.**
302302
|------|---------|
303303
| boolean or object | `true` |
304304

305-
Initialize the repo when creating the IPFS node.
305+
Perform repo initialization steps when creating the IPFS node.
306306

307-
If you have already initialized a repo before creating your IPFS node (e.g. you are loading a repo that was saved to disk from a previous run of your program), you must make sure to set this to `false`. Note that *initializing* a repo is different from creating an instance of [`ipfs.Repo`](https://github.com/ipfs/js-ipfs-repo). The IPFS constructor sets many special properties when initializing a repo, so you should usually not try and call `repoInstance.init()` yourself.
307+
Note that *initializing* a repo is different from creating an instance of [`ipfs.Repo`](https://github.com/ipfs/js-ipfs-repo). The IPFS constructor sets many special properties when initializing a repo, so you should usually not try and call `repoInstance.init()` yourself.
308308

309309
Instead of a boolean, you may provide an object with custom initialization options. All properties are optional:
310310

@@ -314,13 +314,13 @@ Instead of a boolean, you may provide an object with custom initialization optio
314314
```js
315315
// Generating a Peer ID:
316316
const PeerId = require('peer-id')
317-
PeerId.create({ bits: 2048 }, (err, peerId) => {
318-
// Generates a new Peer ID, complete with public/private keypair
319-
// See https://github.com/libp2p/js-peer-id
320-
})
317+
// Generates a new Peer ID, complete with public/private keypair
318+
// See https://github.com/libp2p/js-peer-id
319+
const peerId = await PeerId.create({ bits: 2048 })
321320
```
322321
- `pass` (string) A passphrase to encrypt keys. You should generally use the [top-level `pass` option](#optionspass) instead of the `init.pass` option (this one will take its value from the top-level option if not set).
323322
- `profiles` (Array) Apply profile settings to config.
323+
- `allowNew` (boolean, default: `true`) Set to `false` to disallow initialization if the repo does not already exist.
324324

325325
##### `options.start`
326326

package.json

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
],
1616
"main": "src/core/index.js",
1717
"browser": {
18-
"./src/core/components/init-assets.js": false,
19-
"./src/core/runtime/add-from-fs-nodejs.js": "./src/core/runtime/add-from-fs-browser.js",
18+
"./src/core/runtime/init-assets-nodejs.js": "./src/core/runtime/init-assets-browser.js",
2019
"./src/core/runtime/config-nodejs.js": "./src/core/runtime/config-browser.js",
2120
"./src/core/runtime/dns-nodejs.js": "./src/core/runtime/dns-browser.js",
2221
"./src/core/runtime/libp2p-nodejs.js": "./src/core/runtime/libp2p-browser.js",
@@ -25,7 +24,8 @@
2524
"./src/core/runtime/repo-nodejs.js": "./src/core/runtime/repo-browser.js",
2625
"./src/core/runtime/ipld-nodejs.js": "./src/core/runtime/ipld-browser.js",
2726
"./test/utils/create-repo-nodejs.js": "./test/utils/create-repo-browser.js",
28-
"stream": "readable-stream"
27+
"stream": "readable-stream",
28+
"ipfs-utils/src/files/glob-source": false
2929
},
3030
"browser-all-ipld-formats": {
3131
"./src/core/runtime/ipld-browser.js": "./src/core/runtime/ipld-browser-all.js"
@@ -65,6 +65,7 @@
6565
"@hapi/hapi": "^18.3.2",
6666
"@hapi/joi": "^15.0.0",
6767
"abort-controller": "^3.0.0",
68+
"any-signal": "^1.1.0",
6869
"array-shuffle": "^1.0.1",
6970
"async-iterator-to-pull-stream": "^1.3.0",
7071
"async-iterator-to-stream": "^1.1.0",
@@ -81,7 +82,8 @@
8182
"class-is": "^1.1.0",
8283
"dag-cbor-links": "^1.3.2",
8384
"datastore-core": "~0.7.0",
84-
"datastore-pubsub": "^0.2.1",
85+
"datastore-level": "github:ipfs/js-datastore-level#fix/iterator-memory-leak",
86+
"datastore-pubsub": "^0.3.0",
8587
"debug": "^4.1.0",
8688
"dlv": "^1.1.3",
8789
"err-code": "^2.0.0",
@@ -93,20 +95,19 @@
9395
"glob": "^7.1.3",
9496
"hapi-pino": "^6.1.0",
9597
"hashlru": "^2.3.0",
96-
"human-to-milliseconds": "^2.0.0",
9798
"interface-datastore": "~0.8.0",
98-
"ipfs-bitswap": "^0.26.0",
99+
"ipfs-bitswap": "github:ipfs/js-ipfs-bitswap#refactor/libp2p-async",
99100
"ipfs-block": "~0.8.1",
100101
"ipfs-block-service": "~0.16.0",
101-
"ipfs-http-client": "^40.1.0",
102-
"ipfs-http-response": "~0.4.0",
103-
"ipfs-mfs": "^0.13.2",
104-
"ipfs-multipart": "^0.2.0",
102+
"ipfs-http-client": "github:ipfs/js-ipfs-http-client#refactor/async-iterables2",
103+
"ipfs-http-response": "^0.5.0",
104+
"ipfs-mfs": "github:ipfs/js-ipfs-mfs#refactor/remove-streams",
105+
"ipfs-multipart": "^0.3.0",
105106
"ipfs-repo": "^0.30.0",
106-
"ipfs-unixfs": "~0.1.16",
107-
"ipfs-unixfs-exporter": "^0.38.0",
108-
"ipfs-unixfs-importer": "^0.42.0",
109-
"ipfs-utils": "~0.4.0",
107+
"ipfs-unixfs": "^0.3.0",
108+
"ipfs-unixfs-exporter": "^0.40.0",
109+
"ipfs-unixfs-importer": "^0.43.1",
110+
"ipfs-utils": "^0.6.0",
110111
"ipld": "~0.25.0",
111112
"ipld-bitcoin": "~0.3.0",
112113
"ipld-dag-cbor": "~0.15.0",
@@ -115,54 +116,59 @@
115116
"ipld-git": "~0.5.0",
116117
"ipld-raw": "^4.0.0",
117118
"ipld-zcash": "~0.4.0",
118-
"ipns": "^0.6.1",
119+
"ipns": "^0.7.0",
119120
"is-domain-name": "^1.0.1",
120121
"is-ipfs": "~0.6.1",
121122
"is-pull-stream": "~0.0.0",
122123
"is-stream": "^2.0.0",
123124
"iso-url": "~0.4.6",
124125
"it-all": "^1.0.1",
125-
"it-pipe": "^1.0.1",
126+
"it-concat": "^1.0.0",
127+
"it-last": "^1.0.1",
128+
"it-pipe": "^1.1.0",
126129
"it-to-stream": "^0.1.1",
130+
"iterable-ndjson": "^1.1.0",
127131
"jsondiffpatch": "~0.3.11",
128132
"just-safe-set": "^2.1.0",
129133
"kind-of": "^6.0.2",
130134
"ky": "^0.15.0",
131135
"ky-universal": "~0.3.0",
132-
"libp2p": "^0.26.2",
133-
"libp2p-bootstrap": "~0.9.3",
134-
"libp2p-crypto": "^0.16.2",
136+
"libp2p": "github:libp2p/js-libp2p#refactor/async-await",
137+
"libp2p-bootstrap": "^0.10.2",
138+
"libp2p-crypto": "^0.17.1",
135139
"libp2p-delegated-content-routing": "^0.4.1",
136-
"libp2p-delegated-peer-routing": "^0.3.1",
137-
"libp2p-floodsub": "^0.18.0",
138-
"libp2p-gossipsub": "~0.0.5",
139-
"libp2p-kad-dht": "~0.16.0",
140-
"libp2p-keychain": "^0.5.2",
141-
"libp2p-mdns": "~0.12.0",
140+
"libp2p-delegated-peer-routing": "^0.4.0",
141+
"libp2p-floodsub": "^0.20.0",
142+
"libp2p-gossipsub": "^0.2.0",
143+
"libp2p-kad-dht": "^0.18.3",
144+
"libp2p-keychain": "^0.6.0",
145+
"libp2p-mdns": "^0.13.0",
146+
"libp2p-mplex": "^0.9.3",
142147
"libp2p-record": "~0.7.0",
143-
"libp2p-secio": "~0.11.0",
144-
"libp2p-tcp": "^0.13.0",
145-
"libp2p-webrtc-star": "~0.16.0",
148+
"libp2p-secio": "^0.12.1",
149+
"libp2p-tcp": "^0.14.2",
150+
"libp2p-webrtc-star": "^0.17.0",
146151
"libp2p-websocket-star-multi": "~0.4.3",
147-
"libp2p-websockets": "~0.12.3",
152+
"libp2p-websockets": "^0.13.0",
148153
"lodash.flatten": "^4.4.0",
149-
"mafmt": "^6.0.10",
154+
"mafmt": "^7.0.0",
150155
"merge-options": "^2.0.0",
151156
"mime-types": "^2.1.21",
152157
"mkdirp": "~0.5.1",
153158
"mortice": "^2.0.0",
154-
"multiaddr": "^6.1.1",
155-
"multiaddr-to-uri": "^5.0.0",
159+
"multiaddr": "^7.2.1",
160+
"multiaddr-to-uri": "^5.1.0",
156161
"multibase": "~0.6.0",
157-
"multicodec": "~0.5.5",
162+
"multicodec": "^1.0.0",
158163
"multihashes": "~0.4.14",
159164
"multihashing-async": "^0.8.0",
160165
"node-fetch": "^2.3.0",
166+
"p-defer": "^3.0.0",
161167
"p-iteration": "^1.1.8",
162168
"p-queue": "^6.1.0",
163-
"peer-book": "^0.9.1",
164-
"peer-id": "~0.12.2",
165-
"peer-info": "~0.15.1",
169+
"parse-duration": "^0.1.2",
170+
"peer-id": "^0.13.5",
171+
"peer-info": "^0.17.0",
166172
"pretty-bytes": "^5.3.0",
167173
"progress": "^2.0.1",
168174
"promise-nodeify": "^3.0.1",
@@ -172,7 +178,6 @@
172178
"pull-cat": "^1.1.11",
173179
"pull-defer": "~0.2.3",
174180
"pull-file": "^1.1.0",
175-
"pull-mplex": "~0.1.1",
176181
"pull-ndjson": "^0.2.0",
177182
"pull-pushable": "^2.2.0",
178183
"pull-sort": "^1.0.1",
@@ -183,12 +188,15 @@
183188
"readable-stream": "^3.4.0",
184189
"receptacle": "^1.3.2",
185190
"semver": "^6.3.0",
191+
"stream-to-it": "^0.2.0",
186192
"stream-to-pull-stream": "^1.7.3",
193+
"streaming-iterables": "^4.1.1",
187194
"superstruct": "~0.6.2",
188195
"tar-stream": "^2.0.0",
189196
"temp": "~0.9.0",
197+
"timeout-abort-controller": "^1.1.0",
190198
"update-notifier": "^4.0.0",
191-
"uri-to-multiaddr": "^3.0.1",
199+
"uri-to-multiaddr": "^3.0.2",
192200
"varint": "^5.0.0",
193201
"yargs": "^15.0.1",
194202
"yargs-promise": "^1.1.0"
@@ -204,14 +212,16 @@
204212
"execa": "^3.0.0",
205213
"form-data": "^3.0.0",
206214
"hat": "0.0.3",
207-
"interface-ipfs-core": "^0.125.0",
215+
"interface-ipfs-core": "github:ipfs/interface-js-ipfs-core#refactor/async-iterables",
208216
"ipfs-interop": "^0.1.1",
209217
"ipfsd-ctl": "^1.0.2",
210218
"libp2p-websocket-star": "~0.10.2",
211219
"lodash": "^4.17.15",
212220
"ncp": "^2.0.0",
213221
"p-event": "^4.1.0",
214222
"p-map": "^3.0.0",
223+
"peer-book": "^0.9.2",
224+
"pull-mplex": "^0.1.2",
215225
"qs": "^6.5.2",
216226
"rimraf": "^3.0.0",
217227
"sinon": "^7.4.2",

src/cli/bin.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,22 @@ updateNotifier({ pkg, updateCheckInterval: oneWeek }).notify()
3737

3838
const cli = new YargsPromise(parser)
3939

40-
let getIpfs = null
41-
4240
// Apply command aliasing (eg `refs local` -> `refs-local`)
4341
const args = commandAlias(process.argv.slice(2))
4442
cli
4543
.parse(args)
4644
.then(({ data, argv }) => {
47-
getIpfs = argv.getIpfs
4845
if (data) {
4946
print(data)
5047
}
5148
})
5249
.catch(({ error, argv }) => {
53-
getIpfs = argv && argv.getIpfs
54-
5550
if (error.code === InvalidRepoVersionError.code) {
5651
error.message = 'Incompatible repo version. Migration needed. Pass --migrate for automatic migration'
5752
}
5853

59-
if (error.message) {
60-
print(error.message)
61-
debug(error)
62-
} else {
63-
print('Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
64-
debug(error)
65-
}
54+
print(error.message || 'Unknown error, please re-run the command with DEBUG=ipfs:cli to see debug output')
55+
debug(error)
6656

6757
process.exit(1)
6858
})
69-
.finally(() => {
70-
if (getIpfs && getIpfs.instance) {
71-
const cleanup = getIpfs.rest[0]
72-
return cleanup()
73-
}
74-
})

src/cli/commands/add.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,20 @@ module.exports = {
175175
? globSource(argv.file, { recursive: argv.recursive, hidden: argv.hidden })
176176
: process.stdin // Pipe directly to ipfs.add
177177

178-
let finalHash
178+
let finalCid
179179

180180
try {
181-
for await (const file of ipfs._addAsyncIterator(source, options)) {
181+
for await (const file of ipfs.add(source, options)) {
182182
if (argv.silent) {
183183
continue
184184
}
185185

186186
if (argv.quieter) {
187-
finalHash = file.hash
187+
finalCid = file.cid
188188
continue
189189
}
190190

191-
const cid = cidToString(file.hash, { base: argv.cidBase })
191+
const cid = cidToString(file.cid, { base: argv.cidBase })
192192
let message = cid
193193

194194
if (!argv.quiet) {
@@ -203,7 +203,7 @@ module.exports = {
203203
bar.terminate()
204204
}
205205

206-
// Tweak the error message and add more relevant infor for the CLI
206+
// Tweak the error message and add more relevant info for the CLI
207207
if (err.code === 'ERR_DIR_NON_RECURSIVE') {
208208
err.message = `'${err.path}' is a directory, use the '-r' flag to specify directories`
209209
}
@@ -216,7 +216,7 @@ module.exports = {
216216
}
217217

218218
if (argv.quieter) {
219-
log(cidToString(finalHash, { base: argv.cidBase }))
219+
log(cidToString(finalCid, { base: argv.cidBase }))
220220
}
221221
})())
222222
}

src/cli/commands/bitswap/stat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = {
3535
stats.dupDataReceived = prettyBytes(stats.dupDataReceived.toNumber()).toUpperCase()
3636
stats.wantlist = `[${stats.wantlist.length} keys]`
3737
} else {
38-
const wantlist = stats.wantlist.map((elem) => cidToString(elem['/'], { base: cidBase, upgrade: false }))
38+
const wantlist = stats.wantlist.map(cid => cidToString(cid, { base: cidBase, upgrade: false }))
3939
stats.wantlist = `[${wantlist.length} keys]
4040
${wantlist.join('\n ')}`
4141
}

src/cli/commands/bitswap/wantlist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
resolve((async () => {
2626
const ipfs = await getIpfs()
2727
const list = await ipfs.bitswap.wantlist(peer)
28-
list.Keys.forEach(k => print(cidToString(k['/'], { base: cidBase, upgrade: false })))
28+
list.forEach(cid => print(cidToString(cid, { base: cidBase, upgrade: false })))
2929
})())
3030
}
3131
}

src/cli/commands/block/put.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
'use strict'
22

3-
const bl = require('bl')
43
const fs = require('fs')
54
const multibase = require('multibase')
6-
const promisify = require('promisify-es6')
5+
const concat = require('it-concat')
76
const { cidToString } = require('../../../utils/cid')
87

98
module.exports = {
@@ -41,14 +40,9 @@ module.exports = {
4140
let data
4241

4342
if (argv.block) {
44-
data = await promisify(fs.readFile)(argv.block)
43+
data = await fs.readFileSync(argv.block)
4544
} else {
46-
data = await new Promise((resolve, reject) => {
47-
process.stdin.pipe(bl((err, input) => {
48-
if (err) return reject(err)
49-
resolve(input)
50-
}))
51-
})
45+
data = await concat(process.stdin)
5246
}
5347

5448
const ipfs = await argv.getIpfs()

src/cli/commands/block/rm.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module.exports = {
2525
const ipfs = await getIpfs()
2626
let errored = false
2727

28-
for await (const result of ipfs.block._rmAsyncIterator(hash, {
28+
for await (const result of ipfs.block.rm(hash, {
2929
force,
3030
quiet
3131
})) {

src/cli/commands/block/stat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = {
2020
resolve((async () => {
2121
const ipfs = await getIpfs()
2222
const stats = await ipfs.block.stat(key)
23-
print('Key: ' + cidToString(stats.key, { base: cidBase }))
23+
print('Key: ' + cidToString(stats.cid, { base: cidBase }))
2424
print('Size: ' + stats.size)
2525
})())
2626
}

src/cli/commands/cat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module.exports = {
2222
resolve((async () => {
2323
const ipfs = await getIpfs()
2424

25-
for await (const buf of ipfs._catAsyncIterator(ipfsPath, { offset, length })) {
25+
for await (const buf of ipfs.cat(ipfsPath, { offset, length })) {
2626
process.stdout.write(buf)
2727
}
2828
})())

src/cli/commands/dag/resolve.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ module.exports = {
1919
const options = {}
2020

2121
try {
22-
const result = await ipfs.dag.resolve(ref, options)
2322
let lastCid
2423

25-
for (const res of result) {
24+
for await (const res of ipfs.dag.resolve(ref, options)) {
2625
if (CID.isCID(res.value)) {
2726
lastCid = res.value
2827
}

0 commit comments

Comments
 (0)