@@ -5,7 +5,7 @@ import blobToIt from 'blob-to-it'
5
5
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
6
6
import all from 'it-all'
7
7
import { File } from '@web-std/file'
8
- import { normaliseInput } from '../../src/files/normalise-input.js'
8
+ import { normaliseInput } from '../../src/files/normalise-input-multiple .js'
9
9
import { isNode } from 'ipfs-utils/src/env.js'
10
10
import resolve from 'aegir/utils/resolve.js'
11
11
@@ -16,6 +16,7 @@ const NEWSTRING = () => new String('hello world') // eslint-disable-line no-new-
16
16
const BUFFER = ( ) => uint8ArrayFromString ( STRING ( ) )
17
17
const ARRAY = ( ) => Array . from ( BUFFER ( ) )
18
18
const TYPEDARRAY = ( ) => Uint8Array . from ( ARRAY ( ) )
19
+ const FILE = ( ) => new File ( [ BUFFER ( ) ] , 'test-file.txt' )
19
20
/** @type {() => Blob } */
20
21
let BLOB
21
22
@@ -54,6 +55,14 @@ async function testContent (input) {
54
55
await verifyNormalisation ( result )
55
56
}
56
57
58
+ /**
59
+ * @param {* } input
60
+ * @param {RegExp } message
61
+ */
62
+ async function testFailure ( input , message ) {
63
+ await expect ( all ( normaliseInput ( input ) ) ) . to . eventually . be . rejectedWith ( message )
64
+ }
65
+
57
66
/**
58
67
* @template T
59
68
* @param {T } thing
@@ -86,18 +95,18 @@ function browserReadableStreamOf (thing) {
86
95
} )
87
96
}
88
97
89
- describe ( 'normalise-input' , function ( ) {
98
+ describe ( 'normalise-input-multiple ' , function ( ) {
90
99
/**
91
100
* @param {() => any } content
92
101
* @param {string } name
93
- * @param {boolean } isBytes
102
+ * @param {{ acceptStream: boolean, acceptContentStream: boolean } } options
94
103
*/
95
- function testInputType ( content , name , isBytes ) {
96
- it ( name , async function ( ) {
97
- await testContent ( content ( ) )
104
+ function testInputType ( content , name , { acceptStream , acceptContentStream } ) {
105
+ it ( `Failure ${ name } ` , async function ( ) {
106
+ await testFailure ( content ( ) , / s i n g l e i t e m p a s s e d / )
98
107
} )
99
108
100
- if ( isBytes ) {
109
+ if ( acceptStream ) {
101
110
if ( ReadableStream ) {
102
111
it ( `ReadableStream<${ name } >` , async function ( ) {
103
112
await testContent ( browserReadableStreamOf ( content ( ) ) )
@@ -111,43 +120,35 @@ describe('normalise-input', function () {
111
120
it ( `AsyncIterable<${ name } >` , async function ( ) {
112
121
await testContent ( asyncIterableOf ( content ( ) ) )
113
122
} )
114
- }
115
-
116
- it ( `{ path: '', content: ${ name } }` , async function ( ) {
117
- await testContent ( { path : '' , content : content ( ) } )
118
- } )
119
-
120
- if ( isBytes ) {
123
+ } else {
121
124
if ( ReadableStream ) {
122
- it ( `{ path: '', content: ReadableStream<${ name } > } ` , async function ( ) {
123
- await testContent ( { path : '' , content : browserReadableStreamOf ( content ( ) ) } )
125
+ it ( `Failure ReadableStream<${ name } >` , async function ( ) {
126
+ await testFailure ( browserReadableStreamOf ( content ( ) ) , / s i n g l e i t e m p a s s e d / )
124
127
} )
125
128
}
126
129
127
- it ( `{ path: '', content: Iterable<${ name } > } ` , async function ( ) {
128
- await testContent ( { path : '' , content : iterableOf ( content ( ) ) } )
130
+ it ( `Failure Iterable<${ name } >` , async function ( ) {
131
+ await testFailure ( iterableOf ( content ( ) ) , / s i n g l e i t e m p a s s e d / )
129
132
} )
130
133
131
- it ( `{ path: '', content: AsyncIterable<${ name } > } ` , async function ( ) {
132
- await testContent ( { path : '' , content : asyncIterableOf ( content ( ) ) } )
134
+ it ( `Failure AsyncIterable<${ name } >` , async function ( ) {
135
+ await testFailure ( asyncIterableOf ( content ( ) ) , / s i n g l e i t e m p a s s e d / )
133
136
} )
134
137
}
135
138
136
- if ( ReadableStream ) {
137
- it ( `ReadableStream<${ name } >` , async function ( ) {
138
- await testContent ( browserReadableStreamOf ( content ( ) ) )
139
- } )
140
- }
139
+ it ( `Failure { path: '', content: ${ name } }` , async function ( ) {
140
+ await testFailure ( { path : '' , content : content ( ) } , / s i n g l e i t e m p a s s e d / )
141
+ } )
141
142
142
- it ( `Iterable<{ path: '', content: ${ name } }` , async function ( ) {
143
+ it ( `Iterable<{ path: '', content: ${ name } }> ` , async function ( ) {
143
144
await testContent ( iterableOf ( { path : '' , content : content ( ) } ) )
144
145
} )
145
146
146
- it ( `AsyncIterable<{ path: '', content: ${ name } }` , async function ( ) {
147
+ it ( `AsyncIterable<{ path: '', content: ${ name } }> ` , async function ( ) {
147
148
await testContent ( asyncIterableOf ( { path : '' , content : content ( ) } ) )
148
149
} )
149
150
150
- if ( isBytes ) {
151
+ if ( acceptContentStream ) {
151
152
if ( ReadableStream ) {
152
153
it ( `Iterable<{ path: '', content: ReadableStream<${ name } > }>` , async function ( ) {
153
154
await testContent ( iterableOf ( { path : '' , content : browserReadableStreamOf ( content ( ) ) } ) )
@@ -175,40 +176,85 @@ describe('normalise-input', function () {
175
176
it ( `AsyncIterable<{ path: '', content: AsyncIterable<${ name } > }>` , async function ( ) {
176
177
await testContent ( asyncIterableOf ( { path : '' , content : asyncIterableOf ( content ( ) ) } ) )
177
178
} )
179
+ } else {
180
+ if ( ReadableStream ) {
181
+ it ( `Failure Iterable<{ path: '', content: ReadableStream<${ name } > }>` , async function ( ) {
182
+ await testFailure ( iterableOf ( { path : '' , content : browserReadableStreamOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
183
+ } )
184
+ }
185
+
186
+ it ( `Failure Iterable<{ path: '', content: Iterable<${ name } > }>` , async function ( ) {
187
+ await testFailure ( iterableOf ( { path : '' , content : iterableOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
188
+ } )
189
+
190
+ it ( `Failure Iterable<{ path: '', content: AsyncIterable<${ name } > }>` , async function ( ) {
191
+ await testFailure ( iterableOf ( { path : '' , content : asyncIterableOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
192
+ } )
193
+
194
+ if ( ReadableStream ) {
195
+ it ( `Failure AsyncIterable<{ path: '', content: ReadableStream<${ name } > }>` , async function ( ) {
196
+ await testFailure ( asyncIterableOf ( { path : '' , content : browserReadableStreamOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
197
+ } )
198
+ }
199
+
200
+ it ( `Failure AsyncIterable<{ path: '', content: Iterable<${ name } > }>` , async function ( ) {
201
+ await testFailure ( asyncIterableOf ( { path : '' , content : iterableOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
202
+ } )
203
+
204
+ it ( `Failure AsyncIterable<{ path: '', content: AsyncIterable<${ name } > }>` , async function ( ) {
205
+ await testFailure ( asyncIterableOf ( { path : '' , content : asyncIterableOf ( content ( ) ) } ) , / U n e x p e c t e d i n p u t / )
206
+ } )
178
207
}
179
208
}
180
209
181
210
describe ( 'String' , ( ) => {
182
- testInputType ( STRING , 'String' , true )
183
- testInputType ( NEWSTRING , 'new String()' , true )
211
+ testInputType ( STRING , 'String' , {
212
+ acceptStream : true ,
213
+ acceptContentStream : true
214
+ } )
215
+ testInputType ( NEWSTRING , 'new String()' , {
216
+ acceptStream : true ,
217
+ acceptContentStream : true
218
+ } )
184
219
} )
185
220
186
221
describe ( 'Buffer' , ( ) => {
187
- testInputType ( BUFFER , 'Buffer' , true )
222
+ testInputType ( BUFFER , 'Buffer' , {
223
+ acceptStream : true ,
224
+ acceptContentStream : true
225
+ } )
188
226
} )
189
227
190
228
describe ( 'Blob' , ( ) => {
191
229
if ( ! Blob ) {
192
230
return
193
231
}
194
232
195
- testInputType ( BLOB , 'Blob' , false )
233
+ testInputType ( BLOB , 'Blob' , {
234
+ acceptStream : true ,
235
+ acceptContentStream : false
236
+ } )
196
237
} )
197
238
198
239
describe ( '@web-std/file' , ( ) => {
199
- it ( 'normalizes File input' , async ( ) => {
200
- const FILE = new File ( [ BUFFER ( ) ] , 'test-file.txt' )
201
-
202
- await testContent ( FILE )
240
+ testInputType ( FILE , 'File' , {
241
+ acceptStream : true ,
242
+ acceptContentStream : false
203
243
} )
204
244
} )
205
245
206
246
describe ( 'Iterable<Number>' , ( ) => {
207
- testInputType ( ARRAY , 'Iterable<Number>' , false )
247
+ testInputType ( ARRAY , 'Iterable<Number>' , {
248
+ acceptStream : true ,
249
+ acceptContentStream : false
250
+ } )
208
251
} )
209
252
210
253
describe ( 'TypedArray' , ( ) => {
211
- testInputType ( TYPEDARRAY , 'TypedArray' , true )
254
+ testInputType ( TYPEDARRAY , 'TypedArray' , {
255
+ acceptStream : true ,
256
+ acceptContentStream : true
257
+ } )
212
258
} )
213
259
214
260
if ( isNode ) {
@@ -226,14 +272,9 @@ describe('normalise-input', function () {
226
272
return fs . createReadStream ( path )
227
273
}
228
274
229
- testInputType ( NODEFSREADSTREAM , 'Node fs.ReadStream' , false )
230
-
231
- it ( 'Iterable<Node fs.ReadStream>' , async function ( ) {
232
- await testContent ( iterableOf ( NODEFSREADSTREAM ( ) ) )
233
- } )
234
-
235
- it ( 'AsyncIterable<Node fs.ReadStream>' , async function ( ) {
236
- await testContent ( asyncIterableOf ( NODEFSREADSTREAM ( ) ) )
275
+ testInputType ( NODEFSREADSTREAM , 'Node fs.ReadStream' , {
276
+ acceptStream : true ,
277
+ acceptContentStream : false
237
278
} )
238
279
} )
239
280
}
0 commit comments