Open Acconut opened 3 years ago
Is here any consistent solution or example for Node.js environment?
When I try to use fileData
as Blob
or Buffer
from Node.js receive an error:
source object may only be an instance of Buffer or Readable in this environment
tus-js-client does not accept Blob
s when used in Node.js. But Buffer
s should work as long as they pass Buffer.isBuffer
:
Does that apply to your fileData
?
@Acconut, thank you for your answer!
I was able to use NodeJS Buffer
. But it was incompatible with TypeScript out of the box.
The solution I ended up with is:
tus-js-client.d.ts
index.d.ts
from tus-js-client
initial file and extended typing to Buffer
:
/* eslint-disable @typescript-eslint/no-explicit-any */
// file: 'types/tus-js-client.d.ts'
// Type definitions for tus-js-client
declare module 'tus-js-client' { export const isSupported: boolean; export const canStoreURLs: boolean; export const defaultOptions: UploadOptions;
// TODO: Consider using { read: () => Promise<{ done: boolean; value?: any; }>; } as type export class Upload { // Override: Added Buffer type constructor(file: File | Buffer | Blob | Pick<ReadableStreamDefaultReader, 'read'>, options: UploadOptions);
// Override: Added Buffer type
file: File | Buffer | Blob | Pick<ReadableStreamDefaultReader, 'read'>;
options: UploadOptions;
url: string | null;
static terminate(url: string, options?: UploadOptions): Promise<void>;
start(): void;
abort(shouldTerminate?: boolean): Promise<void>;
findPreviousUploads(): Promise<PreviousUpload[]>;
resumeFromPreviousUpload(previousUpload: PreviousUpload): void;
}
// Copy the rest tus-js-client index.d.ts file...
I see, thanks for the update. Our current type definitions are focused on the use in browser, which is not ideal, I agree. We should have type definitions that work in browsers and in Node.js.
Currently, the definitions reference browser-specific types, which do not exist in other environments, e.g. in Node.js: https://github.com/tus/tus-js-client/blob/239343a75da5aaedeefbc3bf2438062554b5db8d/lib/index.d.ts#L8
It would be better to replace them with interfaces, so we are independent of the environment (pseudo-code):
This could prevent issues like https://github.com/tus/tus-js-client/issues/252