1
1
/* eslint-disable init-declarations, @typescript-eslint/no-floating-promises, sort-imports */
2
2
import { deepEqual , ok } from "node:assert/strict" ;
3
- import { File as NodeFile } from "node:buffer" ;
4
3
import { request , Server } from "node:http" ;
5
4
import { afterEach , beforeEach , describe , it } from "node:test" ;
6
5
import { Binden , Context } from "binden" ;
@@ -133,20 +132,20 @@ describe("Multipart", () => {
133
132
body . append ( "name1" , "value11" ) ;
134
133
body . append ( "name1" , "value12" ) ;
135
134
body . append ( "name2" , "value2" ) ;
136
- const file11 = new NodeFile (
135
+ const file11 = new File (
137
136
[ JSON . stringify ( { hello : "world" } ) ] ,
138
137
"file11.json" ,
139
138
{ type : "application/json" } ,
140
139
) ;
141
- const file12 = new NodeFile ( [ "<html></html>" ] , "file12.html" , {
140
+ const file12 = new File ( [ "<html></html>" ] , "file12.html" , {
142
141
type : "plain/html" ,
143
142
} ) ;
144
- const file2 = new NodeFile ( [ "Hello World" ] , "file2.txt" , {
143
+ const file2 = new File ( [ "Hello World" ] , "file2.txt" , {
145
144
type : "plain/txt" ,
146
145
} ) ;
147
- body . append ( "file1" , file11 as File & NodeFile ) ;
148
- body . append ( "file1" , file12 as File & NodeFile ) ;
149
- body . append ( "file2" , file2 as File & NodeFile ) ;
146
+ body . append ( "file1" , file11 ) ;
147
+ body . append ( "file1" , file12 ) ;
148
+ body . append ( "file2" , file2 ) ;
150
149
151
150
const assert = async ( context : Context ) : Promise < void > => {
152
151
deepEqual ( context . request . content_type ?. type , "multipart/form-data" ) ;
@@ -157,19 +156,17 @@ describe("Multipart", () => {
157
156
for ( const key in actual . keys ( ) ) {
158
157
deepEqual ( actual . getAll ( key ) , body . getAll ( key ) ) ;
159
158
}
160
- const files1 = actual . getAll ( "file1" ) ;
161
- deepEqual ( files1 , [ file11 , file12 ] ) ;
162
- const [ actual_file11 , actual_file12 ] = files1 ;
159
+ const [ actual_file11 , actual_file12 ] = actual . getAll ( "file1" ) as File [ ] ;
163
160
deepEqual ( actual_file11 . name , file11 . name ) ;
161
+ deepEqual ( actual_file11 . size , file11 . size ) ;
164
162
deepEqual ( actual_file11 . type , file11 . type ) ;
165
163
deepEqual ( actual_file12 . name , file12 . name ) ;
166
164
deepEqual ( actual_file12 . type , file12 . type ) ;
167
- const files2 = actual . getAll ( "file2" ) ;
168
- deepEqual ( files2 , [ file2 ] ) ;
169
- const [ actual_file2 ] = files2 ;
170
- deepEqual ( actual_file2 . name , file2 . name ) ;
171
- deepEqual ( actual_file2 . type , file2 . type ) ;
172
-
165
+ deepEqual ( actual_file12 . size , file12 . size ) ;
166
+ const [ files2 ] = actual . getAll ( "file2" ) as File [ ] ;
167
+ deepEqual ( files2 . name , file2 . name ) ;
168
+ deepEqual ( files2 . type , file2 . type ) ;
169
+ deepEqual ( files2 . size , file2 . size ) ;
173
170
await context . json ( msg ) ;
174
171
} ;
175
172
@@ -232,14 +229,8 @@ describe("Multipart", () => {
232
229
233
230
it ( "Replies with 415 error when FormData reaches the `files` limit (by default)" , async ( ) => {
234
231
const body = new FormData ( ) ;
235
- body . append (
236
- "file1" ,
237
- new NodeFile ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" ) as File & NodeFile ,
238
- ) ;
239
- body . append (
240
- "file2" ,
241
- new NodeFile ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" ) as File & NodeFile ,
242
- ) ;
232
+ body . append ( "file1" , new File ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" ) ) ;
233
+ body . append ( "file2" , new File ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" ) ) ;
243
234
244
235
app . use ( multipart ( { limits : { files : 1 } } ) ) ;
245
236
@@ -250,12 +241,12 @@ describe("Multipart", () => {
250
241
251
242
it ( "Passes an incomplete FormData when it reaches the `files` limit (`throw_limits = true`)" , async ( ) => {
252
243
const body = new FormData ( ) ;
253
- const file1 = new NodeFile ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" , {
244
+ const file1 = new File ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" , {
254
245
type : "application/octet-stream" ,
255
- } ) as File & NodeFile ;
256
- const file2 = new NodeFile ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" , {
246
+ } ) ;
247
+ const file2 = new File ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" , {
257
248
type : "application/octet-stream" ,
258
- } ) as File & NodeFile ;
249
+ } ) ;
259
250
body . append ( "file1" , file1 ) ;
260
251
body . append ( "file2" , file2 ) ;
261
252
@@ -265,7 +256,7 @@ describe("Multipart", () => {
265
256
const actual_file1 = context . request . body . get ( "file1" ) ;
266
257
const actual_file2 = context . request . body . get ( "file2" ) ;
267
258
268
- ok ( actual_file1 instanceof NodeFile ) ;
259
+ ok ( actual_file1 instanceof File ) ;
269
260
ok ( actual_file2 === null ) ;
270
261
271
262
deepEqual ( actual_file1 . name , file1 . name ) ;
@@ -287,14 +278,8 @@ describe("Multipart", () => {
287
278
288
279
it ( "Replies with 415 error when FormData reaches the `fileSize` limit (by default)" , async ( ) => {
289
280
const body = new FormData ( ) ;
290
- body . append (
291
- "file1" ,
292
- new NodeFile ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" ) as File & NodeFile ,
293
- ) ;
294
- body . append (
295
- "file2" ,
296
- new NodeFile ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" ) as File & NodeFile ,
297
- ) ;
281
+ body . append ( "file1" , new File ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" ) ) ;
282
+ body . append ( "file2" , new File ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" ) ) ;
298
283
299
284
app . use ( multipart ( { limits : { fileSize : 100 } } ) ) ;
300
285
@@ -305,12 +290,12 @@ describe("Multipart", () => {
305
290
306
291
it ( "Passes an incomplete FormData when it reaches the `fileSize` limit (`throw_limits = true`)" , async ( ) => {
307
292
const body = new FormData ( ) ;
308
- const file1 = new NodeFile ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" , {
293
+ const file1 = new File ( [ Buffer . allocUnsafe ( 50 ) ] , "file1.txt" , {
309
294
type : "application/octet-stream" ,
310
- } ) as File & NodeFile ;
311
- const file2 = new NodeFile ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" , {
295
+ } ) ;
296
+ const file2 = new File ( [ Buffer . allocUnsafe ( 150 ) ] , "file2.txt" , {
312
297
type : "application/octet-stream" ,
313
- } ) as File & NodeFile ;
298
+ } ) ;
314
299
body . append ( "file1" , file1 ) ;
315
300
body . append ( "file2" , file2 ) ;
316
301
@@ -320,8 +305,8 @@ describe("Multipart", () => {
320
305
const actual_file1 = context . request . body . get ( "file1" ) ;
321
306
const actual_file2 = context . request . body . get ( "file2" ) ;
322
307
323
- ok ( actual_file1 instanceof NodeFile ) ;
324
- ok ( actual_file2 instanceof NodeFile ) ;
308
+ ok ( actual_file1 instanceof File ) ;
309
+ ok ( actual_file2 instanceof File ) ;
325
310
326
311
deepEqual ( actual_file1 . name , file1 . name ) ;
327
312
deepEqual ( actual_file1 . size , file1 . size ) ;
@@ -349,9 +334,9 @@ describe("Multipart", () => {
349
334
350
335
it ( "Replies with 415 error when FormData reaches the `parts` limit (by default)" , async ( ) => {
351
336
const body = new FormData ( ) ;
352
- const file = new NodeFile ( [ "123" ] , "file.txt" , {
337
+ const file = new File ( [ "123" ] , "file.txt" , {
353
338
type : "plain/txt" ,
354
- } ) as File & NodeFile ;
339
+ } ) ;
355
340
body . append ( "file" , file ) ;
356
341
body . append ( "k1" , "v1" ) ;
357
342
body . append ( "k2" , "v2" ) ;
@@ -365,9 +350,9 @@ describe("Multipart", () => {
365
350
366
351
it ( "Passes an incomplete FormData when it reaches the `parts` limit (`throw_limits = true`)" , async ( ) => {
367
352
const body = new FormData ( ) ;
368
- const file = new NodeFile ( [ "123" ] , "file.txt" , {
353
+ const file = new File ( [ "123" ] , "file.txt" , {
369
354
type : "plain/txt" ,
370
- } ) as File & NodeFile ;
355
+ } ) ;
371
356
body . append ( "file" , file ) ;
372
357
body . append ( "k1" , "v1" ) ;
373
358
body . append ( "k2" , "v2" ) ;
@@ -376,7 +361,7 @@ describe("Multipart", () => {
376
361
ok ( context . request . body instanceof FormData ) ;
377
362
378
363
const actual_file = context . request . body . get ( "file" ) ;
379
- ok ( actual_file instanceof NodeFile ) ;
364
+ ok ( actual_file instanceof File ) ;
380
365
deepEqual ( actual_file . name , file . name ) ;
381
366
deepEqual ( actual_file . size , file . size ) ;
382
367
deepEqual ( actual_file . type , file . type ) ;
0 commit comments