diff --git a/package-lock.json b/package-lock.json index 961e42c..327071a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nextjs-chunk-upload-action", - "version": "8.1.0", + "version": "8.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nextjs-chunk-upload-action", - "version": "8.1.0", + "version": "8.2.0", "license": "MIT", "devDependencies": { "@types/node": "^20.11.21", diff --git a/package.json b/package.json index cce8243..be544ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nextjs-chunk-upload-action", - "version": "8.1.0", + "version": "8.2.0", "description": "Uploading large files with chunking using server action in Next.js", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index f3213e3..3b2f959 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,12 @@ * [API Reference]: https://github.com/a179346/nextjs-chunk-upload-action/blob/main/docs/api-reference.md */ -export type FileLike = Pick; +export type Promisable = T | PromiseLike; + +export interface FileLike { + readonly size: number; + slice(start?: number, end?: number, contentType?: string): Promisable; +} export type Primitive = string | boolean | number | undefined | null; @@ -28,8 +33,8 @@ export type ChunkUploadHandler = ( metadata: TMetadata ) => Promise; -export interface ChunkUploaderOptions { - file: FileLike; +export interface ChunkUploaderOptions { + file: TFile; /** * The function that defines how the chunk is uploaded to the server. */ @@ -78,8 +83,8 @@ export type ChunkUploaderStatus = | 'complete' | 'error'; -export class ChunkUploader { - constructor(options: ChunkUploaderOptions) { +export class ChunkUploader { + constructor(options: ChunkUploaderOptions) { this._validateOptions(options); this._status = 'pending'; @@ -202,7 +207,7 @@ export class ChunkUploader { protected _position: number; protected _error?: unknown; - protected readonly _file: FileLike; + protected readonly _file: TFile; protected readonly _onChunkUpload: ChunkUploadHandler; protected readonly _chunkBytes: number; protected readonly _metadata: Readonly; @@ -234,7 +239,7 @@ export class ChunkUploader { const isLastChunk = this._position + this._chunkBytes >= this._file.size; const endPosition = isLastChunk ? this._file.size : this._position + this._chunkBytes; - const blob = this._file.slice(this._position, endPosition); + const blob = await this._file.slice(this._position, endPosition); for (let retry = 0; retry <= this._retryDelays.length; retry += 1) { try { @@ -271,7 +276,7 @@ export class ChunkUploader { return isLastChunk; } - protected _validateOptions(options: ChunkUploaderOptions) { + protected _validateOptions(options: ChunkUploaderOptions) { if (!options.file) throw new Error('File is required'); if (typeof options.file.size !== 'number') throw new Error('File size must be a number'); if (typeof options.file.slice !== 'function') throw new Error('File slice must be a function');