Skip to content

Commit 0bee794

Browse files
committed
#353 Add streaming for FormData
1 parent f2ad3a9 commit 0bee794

File tree

4 files changed

+7
-181
lines changed

4 files changed

+7
-181
lines changed

src/version2/issueAttachments.ts

+3-91
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Mime } from 'mime';
2-
import mime from 'mime';
31
import type * as Models from './models';
42
import type * as Parameters from './parameters';
53
import type { Client } from '../clients';
@@ -424,23 +422,9 @@ export class IssueAttachments {
424422
const formData = new FormData();
425423
const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment];
426424

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);
444428
}
445429

446430
const config: RequestConfig = {
@@ -457,76 +441,4 @@ export class IssueAttachments {
457441

458442
return this.client.sendRequest(config, callback);
459443
}
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-
}
532444
}

src/version2/parameters/addAttachment.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface Attachment {
3939
*/
4040
file: Buffer | ReadableStream | Readable | string | Blob | File;
4141

42+
// todo not using?
4243
/**
4344
* Optional MIME type of the attachment. Example values include:
4445
*

src/version3/issueAttachments.ts

+2-90
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { Mime } from 'mime';
2-
import mime from 'mime';
31
import type * as Models from './models';
42
import type * as Parameters from './parameters';
53
import type { Client } from '../clients';
@@ -436,23 +434,9 @@ export class IssueAttachments {
436434
const formData = new FormData();
437435
const attachments = Array.isArray(parameters.attachment) ? parameters.attachment : [parameters.attachment];
438436

439-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
440-
let Readable: typeof import('stream').Readable | undefined;
441-
442-
if (typeof window === 'undefined') {
443-
const { Readable: NodeReadable } = await import('stream');
444-
445-
Readable = NodeReadable;
446-
}
447-
448437
for (const attachment of attachments) {
449-
const file = await this._convertToFile(attachment, mime, Readable);
450-
451-
if (!(file instanceof File || file instanceof Blob)) {
452-
throw new Error(`Unsupported file type for attachment: ${typeof file}`);
453-
}
454-
455-
formData.append('file', file, attachment.filename);
438+
// @ts-expect-error Wrong typings
439+
formData.append('file', attachment.file, attachment.filename);
456440
}
457441

458442
const config: RequestConfig = {
@@ -469,76 +453,4 @@ export class IssueAttachments {
469453

470454
return this.client.sendRequest(config, callback);
471455
}
472-
473-
private async _convertToFile(
474-
attachment: Parameters.Attachment,
475-
mime: Mime,
476-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
477-
Readable?: typeof import('stream').Readable,
478-
): Promise<File | Blob> {
479-
const mimeType = attachment.mimeType ?? (mime.getType(attachment.filename) || undefined);
480-
481-
if (attachment.file instanceof Blob || attachment.file instanceof File) {
482-
return attachment.file;
483-
}
484-
485-
if (typeof attachment.file === 'string') {
486-
return new File([attachment.file], attachment.filename, { type: mimeType });
487-
}
488-
489-
if (Readable && attachment.file instanceof Readable) {
490-
return this._streamToBlob(attachment.file, attachment.filename, mimeType);
491-
}
492-
493-
if (attachment.file instanceof ReadableStream) {
494-
return this._streamToBlob(attachment.file, attachment.filename, mimeType);
495-
}
496-
497-
if (ArrayBuffer.isView(attachment.file) || attachment.file instanceof ArrayBuffer) {
498-
return new File([attachment.file], attachment.filename, { type: mimeType });
499-
}
500-
501-
throw new Error('Unsupported attachment file type.');
502-
}
503-
504-
private async _streamToBlob(
505-
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
506-
stream: import('stream').Readable | ReadableStream,
507-
filename: string,
508-
mimeType?: string,
509-
): Promise<File> {
510-
if (typeof window === 'undefined' && stream instanceof (await import('stream')).Readable) {
511-
return new Promise((resolve, reject) => {
512-
const chunks: Uint8Array[] = [];
513-
514-
stream.on('data', chunk => chunks.push(chunk));
515-
stream.on('end', () => {
516-
const blob = new Blob(chunks, { type: mimeType });
517-
518-
resolve(new File([blob], filename, { type: mimeType }));
519-
});
520-
stream.on('error', reject);
521-
});
522-
}
523-
524-
if (stream instanceof ReadableStream) {
525-
const reader = stream.getReader();
526-
const chunks: Uint8Array[] = [];
527-
528-
let done = false;
529-
530-
while (!done) {
531-
const { value, done: streamDone } = await reader.read();
532-
533-
if (value) chunks.push(value);
534-
done = streamDone;
535-
}
536-
537-
const blob = new Blob(chunks, { type: mimeType });
538-
539-
return new File([blob], filename, { type: mimeType });
540-
}
541-
542-
throw new Error('Unsupported stream type.');
543-
}
544456
}

src/version3/parameters/addAttachment.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface Attachment {
3939
*/
4040
file: Buffer | ReadableStream | Readable | string | Blob | File;
4141

42+
// todo not using?
4243
/**
4344
* Optional MIME type of the attachment. Example values include:
4445
*

0 commit comments

Comments
 (0)