Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Readable instead of ReadStream in uploadV2 #1577

Merged
merged 1 commit into from Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/web-api/src/file-upload.spec.js
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
@@ -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