Skip to content

Commit

Permalink
Use Readable instead of ReadStream in uploadV2 (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
redneb committed Feb 1, 2023
1 parent acef8cf commit a4c530d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/web-api/src/file-upload.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('file-upload', () => {
const res = await getFileData(invalidFileUpload);
assert.fail(res, "an error", "Was expecting an error, but got a result");
} catch (err) {
assert.equal(err.message, 'file must be a valid string path, buffer or ReadStream');
assert.equal(err.message, 'file must be a valid string path, buffer or Readable');
assert.equal(err.code, ErrorCode.FileUploadInvalidArgumentsError);
}

Expand Down
13 changes: 7 additions & 6 deletions packages/web-api/src/file-upload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger } from '@slack/logger';
import { readFileSync, ReadStream } from 'fs';
import { readFileSync } from 'fs';
import { Readable } from 'stream';
import { errorWithCode, ErrorCode } from './errors';
import { FilesCompleteUploadExternalArguments, FilesUploadV2Arguments, FileUploadV2, FileUploadV2Job } from './methods';

Expand Down Expand Up @@ -126,8 +127,8 @@ export async function getFileData(options: FilesUploadV2Arguments | FileUploadV2
}
}

// try to handle as ReadStream
const data = await getFileDataAsStream(file as ReadStream);
// try to handle as Readable
const data = await getFileDataAsStream(file as Readable);
if (data) return data;
}
if (content) return Buffer.from(content);
Expand All @@ -149,7 +150,7 @@ export function getFileDataLength(data: Buffer): number {
);
}

export async function getFileDataAsStream(readable: ReadStream): Promise<Buffer> {
export async function getFileDataAsStream(readable: Readable): Promise<Buffer> {
const chunks: Uint8Array[] = [];

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -259,9 +260,9 @@ export function errorIfInvalidOrMissingFileData(options: FilesUploadV2Arguments
);
}
/* eslint-disable @typescript-eslint/no-explicit-any */
if (file && !(typeof file === 'string' || Buffer.isBuffer(file) || (file as any) instanceof ReadStream)) {
if (file && !(typeof file === 'string' || Buffer.isBuffer(file) || (file as any) instanceof Readable)) {
throw errorWithCode(
new Error('file must be a valid string path, buffer or ReadStream'),
new Error('file must be a valid string path, buffer or Readable'),
ErrorCode.FileUploadInvalidArgumentsError,
);
}
Expand Down

0 comments on commit a4c530d

Please sign in to comment.