1
- import type { Mime } from 'mime' ;
2
- import mime from 'mime' ;
3
1
import type * as Models from './models' ;
4
2
import type * as Parameters from './parameters' ;
5
3
import type { Client } from '../clients' ;
@@ -424,23 +422,9 @@ export class IssueAttachments {
424
422
const formData = new FormData ( ) ;
425
423
const attachments = Array . isArray ( parameters . attachment ) ? parameters . attachment : [ parameters . attachment ] ;
426
424
427
- // eslint-disable-next-line @typescript-eslint/consistent-type-imports
428
- let Readable : typeof import ( 'stream' ) . Readable | undefined ;
429
-
430
- if ( typeof window === 'undefined' ) {
431
- const { Readable : NodeReadable } = await import ( 'stream' ) ;
432
-
433
- Readable = NodeReadable ;
434
- }
435
-
436
- for await ( const attachment of attachments ) {
437
- const file = await this . _convertToFile ( attachment , mime , Readable ) ;
438
-
439
- if ( ! ( file instanceof File || file instanceof Blob ) ) {
440
- throw new Error ( `Unsupported file type for attachment: ${ typeof file } ` ) ;
441
- }
442
-
443
- formData . append ( 'file' , file , attachment . filename ) ;
425
+ for ( const attachment of attachments ) {
426
+ // @ts -expect-error Wrong typings
427
+ formData . append ( 'file' , attachment . file , attachment . filename ) ;
444
428
}
445
429
446
430
const config : RequestConfig = {
@@ -457,76 +441,4 @@ export class IssueAttachments {
457
441
458
442
return this . client . sendRequest ( config , callback ) ;
459
443
}
460
-
461
- private async _convertToFile (
462
- attachment : Parameters . Attachment ,
463
- mime : Mime ,
464
- // eslint-disable-next-line @typescript-eslint/consistent-type-imports
465
- Readable ?: typeof import ( 'stream' ) . Readable ,
466
- ) : Promise < File | Blob > {
467
- const mimeType = attachment . mimeType ?? ( mime . getType ( attachment . filename ) || undefined ) ;
468
-
469
- if ( attachment . file instanceof Blob || attachment . file instanceof File ) {
470
- return attachment . file ;
471
- }
472
-
473
- if ( typeof attachment . file === 'string' ) {
474
- return new File ( [ attachment . file ] , attachment . filename , { type : mimeType } ) ;
475
- }
476
-
477
- if ( Readable && attachment . file instanceof Readable ) {
478
- return this . _streamToBlob ( attachment . file , attachment . filename , mimeType ) ;
479
- }
480
-
481
- if ( attachment . file instanceof ReadableStream ) {
482
- return this . _streamToBlob ( attachment . file , attachment . filename , mimeType ) ;
483
- }
484
-
485
- if ( ArrayBuffer . isView ( attachment . file ) || attachment . file instanceof ArrayBuffer ) {
486
- return new File ( [ attachment . file ] , attachment . filename , { type : mimeType } ) ;
487
- }
488
-
489
- throw new Error ( 'Unsupported attachment file type.' ) ;
490
- }
491
-
492
- private async _streamToBlob (
493
- // eslint-disable-next-line @typescript-eslint/consistent-type-imports
494
- stream : import ( 'stream' ) . Readable | ReadableStream ,
495
- filename : string ,
496
- mimeType ?: string ,
497
- ) : Promise < File > {
498
- if ( typeof window === 'undefined' && stream instanceof ( await import ( 'stream' ) ) . Readable ) {
499
- return new Promise ( ( resolve , reject ) => {
500
- const chunks : Uint8Array [ ] = [ ] ;
501
-
502
- stream . on ( 'data' , chunk => chunks . push ( chunk ) ) ;
503
- stream . on ( 'end' , ( ) => {
504
- const blob = new Blob ( chunks , { type : mimeType } ) ;
505
-
506
- resolve ( new File ( [ blob ] , filename , { type : mimeType } ) ) ;
507
- } ) ;
508
- stream . on ( 'error' , reject ) ;
509
- } ) ;
510
- }
511
-
512
- if ( stream instanceof ReadableStream ) {
513
- const reader = stream . getReader ( ) ;
514
- const chunks : Uint8Array [ ] = [ ] ;
515
-
516
- let done = false ;
517
-
518
- while ( ! done ) {
519
- const { value, done : streamDone } = await reader . read ( ) ;
520
-
521
- if ( value ) chunks . push ( value ) ;
522
- done = streamDone ;
523
- }
524
-
525
- const blob = new Blob ( chunks , { type : mimeType } ) ;
526
-
527
- return new File ( [ blob ] , filename , { type : mimeType } ) ;
528
- }
529
-
530
- throw new Error ( 'Unsupported stream type.' ) ;
531
- }
532
444
}
0 commit comments