Skip to content

Commit 251eff0

Browse files
Alan Shawhugomrdias
Alan Shaw
authored andcommitted
feat: convert to async iterators (#15)
* Remove support for Node.js streams and pull streams * Adds a `urlSource` utility that will be exported by `js-ipfs-http-client` and `js-ipfs` along side `globSource` BREAKING CHANGE: Support for Node.js streams and Pull Streams has been removed
1 parent 71a7bf5 commit 251eff0

9 files changed

+21
-407
lines changed

package.json

+5-10
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,18 @@
2929
"buffer": "^5.2.1",
3030
"err-code": "^2.0.0",
3131
"fs-extra": "^8.1.0",
32-
"is-buffer": "^2.0.3",
3332
"is-electron": "^2.2.0",
34-
"is-pull-stream": "0.0.0",
35-
"is-stream": "^2.0.0",
36-
"it-glob": "0.0.4",
37-
"kind-of": "^6.0.2",
38-
"pull-stream-to-async-iterator": "^1.0.2",
39-
"readable-stream": "^3.4.0"
33+
"it-glob": "0.0.6",
34+
"ky": "^0.15.0",
35+
"ky-universal": "^0.3.0",
36+
"stream-to-it": "^0.2.0"
4037
},
4138
"devDependencies": {
4239
"aegir": "^20.3.0",
4340
"async-iterator-all": "^1.0.0",
4441
"chai": "^4.2.0",
4542
"chai-as-promised": "^7.1.1",
46-
"dirty-chai": "^2.0.1",
47-
"pull-stream": "^3.6.13",
48-
"readable-stream-2": "npm:readable-stream@^2.0.0"
43+
"dirty-chai": "^2.0.1"
4944
},
5045
"contributors": [
5146
"Alan Shaw <[email protected]>",

src/files/add-input-validation.js

-31
This file was deleted.

src/files/glob-source.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const fs = require('fs-extra')
44
const glob = require('it-glob')
55
const Path = require('path')
66
const errCode = require('err-code')
7-
const kindOf = require('kind-of')
87

98
/**
109
* Create an async iterator that yields paths that match requested file paths.
@@ -20,7 +19,7 @@ const kindOf = require('kind-of')
2019
module.exports = async function * globSource (paths, options) {
2120
options = options || {}
2221

23-
if (kindOf(paths) === 'string') {
22+
if (typeof paths === 'string') {
2423
paths = [paths]
2524
}
2625

src/files/normalise-input.js

-97
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
const errCode = require('err-code')
44
const { Buffer } = require('buffer')
5-
const pullStreamToIterable = require('pull-stream-to-async-iterator')
6-
const { isSource } = require('is-pull-stream')
75
const globalThis = require('../globalthis')
8-
const { Readable } = require('stream')
9-
const Readable3 = require('readable-stream')
106

117
/*
128
* Transform one of:
@@ -21,8 +17,6 @@ const Readable3 = require('readable-stream')
2117
* { path, content: Iterable<Number> } [single file]
2218
* { path, content: Iterable<Bytes> } [single file]
2319
* { path, content: AsyncIterable<Bytes> } [single file]
24-
* { path, content: PullStream<Bytes> } [single file]
25-
* { path, content: Readable<Bytes> } [single file]
2620
* Iterable<Number> [single file]
2721
* Iterable<Bytes> [single file]
2822
* Iterable<Bloby> [multiple files]
@@ -33,8 +27,6 @@ const Readable3 = require('readable-stream')
3327
* Iterable<{ path, content: Iterable<Number> }> [multiple files]
3428
* Iterable<{ path, content: Iterable<Bytes> }> [multiple files]
3529
* Iterable<{ path, content: AsyncIterable<Bytes> }> [multiple files]
36-
* Iterable<{ path, content: PullStream<Bytes> }> [multiple files]
37-
* Iterable<{ path, content: Readable<Bytes> }> [multiple files]
3830
* AsyncIterable<Bytes> [single file]
3931
* AsyncIterable<Bloby> [multiple files]
4032
* AsyncIterable<String> [multiple files]
@@ -44,30 +36,6 @@ const Readable3 = require('readable-stream')
4436
* AsyncIterable<{ path, content: Iterable<Number> }> [multiple files]
4537
* AsyncIterable<{ path, content: Iterable<Bytes> }> [multiple files]
4638
* AsyncIterable<{ path, content: AsyncIterable<Bytes> }> [multiple files]
47-
* AsyncIterable<{ path, content: PullStream<Bytes> }> [multiple files]
48-
* AsyncIterable<{ path, content: Readable<Bytes> }> [multiple files]
49-
* PullStream<Bytes> [single file]
50-
* PullStream<Bloby> [multiple files]
51-
* PullStream<String> [multiple files]
52-
* PullStream<{ path, content: Bytes }> [multiple files]
53-
* PullStream<{ path, content: Bloby }> [multiple files]
54-
* PullStream<{ path, content: String }> [multiple files]
55-
* PullStream<{ path, content: Iterable<Number> }> [multiple files]
56-
* PullStream<{ path, content: Iterable<Bytes> }> [multiple files]
57-
* PullStream<{ path, content: AsyncIterable<Bytes> }> [multiple files]
58-
* PullStream<{ path, content: PullStream<Bytes> }> [multiple files]
59-
* PullStream<{ path, content: Readable<Bytes> }> [multiple files]
60-
* Readable<Bytes> [single file]
61-
* Readable<Bloby> [multiple files]
62-
* Readable<String> [multiple files]
63-
* Readable<{ path, content: Bytes }> [multiple files]
64-
* Readable<{ path, content: Bloby }> [multiple files]
65-
* Readable<{ path, content: String }> [multiple files]
66-
* Readable<{ path, content: Iterable<Number> }> [multiple files]
67-
* Readable<{ path, content: Iterable<Bytes> }> [multiple files]
68-
* Readable<{ path, content: AsyncIterable<Bytes> }> [multiple files]
69-
* Readable<{ path, content: PullStream<Bytes> }> [multiple files]
70-
* Readable<{ path, content: Readable<Bytes> }> [multiple files]
7139
* ```
7240
* Into:
7341
*
@@ -99,11 +67,6 @@ module.exports = function normaliseInput (input) {
9967
})()
10068
}
10169

102-
// Readable<?>
103-
if (isOldReadable(input)) {
104-
input = upgradeOldStream(input)
105-
}
106-
10770
// Iterable<?>
10871
if (input[Symbol.iterator]) {
10972
return (async function * () { // eslint-disable-line require-await
@@ -176,37 +139,6 @@ module.exports = function normaliseInput (input) {
176139
})()
177140
}
178141

179-
// PullStream<?>
180-
if (isSource(input)) {
181-
return (async function * () {
182-
const iterator = pullStreamToIterable(input)[Symbol.asyncIterator]()
183-
const first = await iterator.next()
184-
if (first.done) return iterator
185-
186-
// PullStream<Bytes>
187-
if (isBytes(first.value)) {
188-
yield toFileObject((async function * () { // eslint-disable-line require-await
189-
yield first.value
190-
yield * iterator
191-
})())
192-
return
193-
}
194-
195-
// PullStream<Bloby>
196-
// PullStream<String>
197-
// PullStream<{ path, content }>
198-
if (isFileObject(first.value) || isBloby(first.value) || typeof first.value === 'string') {
199-
yield toFileObject(first.value)
200-
for await (const obj of iterator) {
201-
yield toFileObject(obj)
202-
}
203-
return
204-
}
205-
206-
throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
207-
})()
208-
}
209-
210142
throw errCode(new Error('Unexpected input: ' + typeof input), 'ERR_UNEXPECTED_INPUT')
211143
}
212144

@@ -235,11 +167,6 @@ function toAsyncIterable (input) {
235167
return blobToAsyncGenerator(input)
236168
}
237169

238-
// Readable<?>
239-
if (isOldReadable(input)) {
240-
input = upgradeOldStream(input)
241-
}
242-
243170
// Iterator<?>
244171
if (input[Symbol.iterator]) {
245172
return (async function * () { // eslint-disable-line require-await
@@ -278,22 +205,9 @@ function toAsyncIterable (input) {
278205
})()
279206
}
280207

281-
// PullStream<Bytes>
282-
if (isSource(input)) {
283-
return pullStreamToIterable(input)
284-
}
285-
286208
throw errCode(new Error(`Unexpected input: ${input}`, 'ERR_UNEXPECTED_INPUT'))
287209
}
288210

289-
function isOldReadable (obj) {
290-
if (obj[Symbol.iterator] || obj[Symbol.asyncIterator]) {
291-
return false
292-
}
293-
294-
return Boolean(obj.readable)
295-
}
296-
297211
function toBuffer (chunk) {
298212
return isBytes(chunk) ? chunk : Buffer.from(chunk)
299213
}
@@ -311,17 +225,6 @@ function isFileObject (obj) {
311225
return typeof obj === 'object' && (obj.path || obj.content)
312226
}
313227

314-
function upgradeOldStream (stream) {
315-
if (stream[Symbol.asyncIterator] || stream[Symbol.iterator]) {
316-
return stream
317-
}
318-
319-
// in the browser the stream.Readable is not an async iterator but readble-stream@3 is...
320-
stream[Symbol.asyncIterator] = Readable.prototype[Symbol.asyncIterator] || Readable3.prototype[Symbol.asyncIterator]
321-
322-
return stream
323-
}
324-
325228
function blobToAsyncGenerator (blob) {
326229
if (typeof blob.stream === 'function') {
327230
// firefox < 69 does not support blob.stream()

src/files/url-source.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const { default: ky } = require('ky-universal')
4+
const toIterable = require('stream-to-it/source')
5+
6+
module.exports = async function * urlSource (url, options) {
7+
options = options || {}
8+
9+
const { body } = await ky.get(url)
10+
11+
yield {
12+
path: decodeURIComponent(new URL(url).pathname.split('/').pop() || ''),
13+
content: toIterable(body)
14+
}
15+
}

src/streams/stream-from-filereader.js

-37
This file was deleted.

test/files/add-input-validation.spec.js

-57
This file was deleted.

0 commit comments

Comments
 (0)