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

typescript: Replace file types with interfaces #289

Open
Acconut opened this issue Oct 18, 2021 · 4 comments
Open

typescript: Replace file types with interfaces #289

Acconut opened this issue Oct 18, 2021 · 4 comments
Assignees

Comments

@Acconut
Copy link
Member

Acconut commented Oct 18, 2021

Currently, the definitions reference browser-specific types, which do not exist in other environments, e.g. in Node.js:

constructor(file: File | Blob | Pick<ReadableStreamDefaultReader, "read">, options: UploadOptions);

It would be better to replace them with interfaces, so we are independent of the environment (pseudo-code):

interface IBlob {
  slice(start, end): IBlob;
}

interface IReader {
  read(): Promise<IBlob>;
}

This could prevent issues like #252

@DennySlick
Copy link
Contributor

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

@Acconut
Copy link
Member Author

Acconut commented Mar 14, 2024

tus-js-client does not accept Blobs when used in Node.js. But Buffers should work as long as they pass Buffer.isBuffer:

if (Buffer.isBuffer(input)) {
return Promise.resolve(new BufferSource(input))
}

Does that apply to your fileData?

@DennySlick
Copy link
Contributor

DennySlick commented Mar 15, 2024

@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:

  1. Defined type augmentation file in my project: tus-js-client.d.ts
  2. Copied 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...

@Acconut
Copy link
Member Author

Acconut commented Mar 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants