@@ -14,33 +14,25 @@ import {
14
14
} from 'ipfs-unixfs'
15
15
16
16
/**
17
+ * @typedef {import('ipfs-core-types/src/utils').ImportCandidate } ImportCandidate
17
18
* @typedef {import('ipfs-core-types/src/utils').ToContent } ToContent
18
19
* @typedef {import('ipfs-unixfs-importer').ImportCandidate } ImporterImportCandidate
19
- * @typedef {import('ipfs-core-types/src/utils').ImportCandidate } ImportCandidate
20
20
* @typedef {import('ipfs-core-types/src/utils').ImportCandidateStream } ImportCandidateStream
21
21
*/
22
22
23
23
/**
24
- * @param {ImportCandidate | ImportCandidateStream } input
24
+ * @param {ImportCandidateStream } input
25
25
* @param {(content:ToContent) => Promise<AsyncIterable<Uint8Array>> } normaliseContent
26
26
*/
27
27
// eslint-disable-next-line complexity
28
- export async function * normalise ( input , normaliseContent ) {
29
- if ( input === null || input === undefined ) {
30
- throw errCode ( new Error ( `Unexpected input: ${ input } ` ) , 'ERR_UNEXPECTED_INPUT' )
31
- }
32
-
28
+ export async function * normaliseCandidateMultiple ( input , normaliseContent ) {
33
29
// String
34
- if ( typeof input === 'string' || input instanceof String ) {
35
- yield toFileObject ( input . toString ( ) , normaliseContent )
36
- return
37
- }
38
-
39
30
// Uint8Array|ArrayBuffer|TypedArray
40
31
// Blob|File
41
- if ( isBytes ( input ) || isBlob ( input ) ) {
42
- yield toFileObject ( input , normaliseContent )
43
- return
32
+ // fs.ReadStream
33
+ // @ts -expect-error _readableState is a property of a node fs.ReadStream
34
+ if ( typeof input === 'string' || input instanceof String || isBytes ( input ) || isBlob ( input ) || input . _readableState ) {
35
+ throw errCode ( new Error ( 'Unexpected input: single item passed - if you are using ipfs.allAll, please use ipfs.add instead' ) , 'ERR_UNEXPECTED_INPUT' )
44
36
}
45
37
46
38
// Browser ReadableStream
@@ -67,42 +59,37 @@ export async function * normalise (input, normaliseContent) {
67
59
68
60
// (Async)Iterable<Number>
69
61
// (Async)Iterable<Bytes>
70
- if ( Number . isInteger ( value ) || isBytes ( value ) ) {
71
- yield toFileObject ( peekable , normaliseContent )
72
- return
62
+ if ( Number . isInteger ( value ) ) {
63
+ throw errCode ( new Error ( 'Unexpected input: single item passed - if you are using ipfs.allAll, please use ipfs.add instead' ) , 'ERR_UNEXPECTED_INPUT' )
73
64
}
74
65
75
- // fs.ReadStream<Bytes >
66
+ // (Async)Iterable< fs.ReadStream>
76
67
if ( value . _readableState ) {
77
- // @ts -ignore Node readable streams have a `.path` property so we need to pass it as the content
68
+ // @ts -ignore Node fs.ReadStreams have a `.path` property so we need to pass it as the content
78
69
yield * map ( peekable , ( /** @type {ImportCandidate } */ value ) => toFileObject ( { content : value } , normaliseContent ) )
79
70
return
80
71
}
81
72
82
- // (Async)Iterable<Blob>
83
- // (Async)Iterable<String>
84
- // (Async)Iterable<{ path, content }>
85
- if ( isFileObject ( value ) || isBlob ( value ) || typeof value === 'string' || value instanceof String ) {
86
- yield * map ( peekable , ( /** @type {ImportCandidate } */ value ) => toFileObject ( value , normaliseContent ) )
73
+ if ( isBytes ( value ) ) {
74
+ yield toFileObject ( { content : peekable } , normaliseContent )
87
75
return
88
76
}
89
77
90
78
// (Async)Iterable<(Async)Iterable<?>>
91
79
// (Async)Iterable<ReadableStream<?>>
92
80
// ReadableStream<(Async)Iterable<?>>
93
81
// ReadableStream<ReadableStream<?>>
94
- if ( value [ Symbol . iterator ] || value [ Symbol . asyncIterator ] || isReadableStream ( value ) ) {
82
+ if ( isFileObject ( value ) || value [ Symbol . iterator ] || value [ Symbol . asyncIterator ] || isReadableStream ( value ) || isBlob ( value ) ) {
95
83
yield * map ( peekable , ( /** @type {ImportCandidate } */ value ) => toFileObject ( value , normaliseContent ) )
96
84
return
97
85
}
98
86
}
99
87
100
88
// { path, content: ? }
101
- // Note: Detected _after_ (Async)Iterable<?> because Node.js streams have a
89
+ // Note: Detected _after_ (Async)Iterable<?> because Node.js fs.ReadStreams have a
102
90
// `path` property that passes this check.
103
91
if ( isFileObject ( input ) ) {
104
- yield toFileObject ( input , normaliseContent )
105
- return
92
+ throw errCode ( new Error ( 'Unexpected input: single item passed - if you are using ipfs.allAll, please use ipfs.add instead' ) , 'ERR_UNEXPECTED_INPUT' )
106
93
}
107
94
108
95
throw errCode ( new Error ( 'Unexpected input: ' + typeof input ) , 'ERR_UNEXPECTED_INPUT' )
0 commit comments