Skip to content

Commit 11f0b30

Browse files
committed
Update FileLike
1 parent 614b67b commit 11f0b30

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nextjs-chunk-upload-action",
3-
"version": "8.1.0",
3+
"version": "8.2.0",
44
"description": "Uploading large files with chunking using server action in Next.js",
55
"main": "dist/index.js",
66
"scripts": {

src/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
* [API Reference]: https://github.com/a179346/nextjs-chunk-upload-action/blob/main/docs/api-reference.md
99
*/
1010

11-
export type FileLike = Pick<File, 'size' | 'slice'>;
11+
export type Promisable<T> = T | PromiseLike<T>;
12+
13+
export interface FileLike {
14+
readonly size: number;
15+
slice(start?: number, end?: number, contentType?: string): Promisable<Blob>;
16+
}
1217

1318
export type Primitive = string | boolean | number | undefined | null;
1419

@@ -28,8 +33,8 @@ export type ChunkUploadHandler<TMetadata extends Metadata = Metadata> = (
2833
metadata: TMetadata
2934
) => Promise<void>;
3035

31-
export interface ChunkUploaderOptions<TMetadata extends Metadata> {
32-
file: FileLike;
36+
export interface ChunkUploaderOptions<TMetadata extends Metadata, TFile extends FileLike> {
37+
file: TFile;
3338
/**
3439
* The function that defines how the chunk is uploaded to the server.
3540
*/
@@ -78,8 +83,8 @@ export type ChunkUploaderStatus =
7883
| 'complete'
7984
| 'error';
8085

81-
export class ChunkUploader<TMetadata extends Metadata> {
82-
constructor(options: ChunkUploaderOptions<TMetadata>) {
86+
export class ChunkUploader<TMetadata extends Metadata, TFile extends FileLike> {
87+
constructor(options: ChunkUploaderOptions<TMetadata, TFile>) {
8388
this._validateOptions(options);
8489

8590
this._status = 'pending';
@@ -202,7 +207,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
202207
protected _position: number;
203208
protected _error?: unknown;
204209

205-
protected readonly _file: FileLike;
210+
protected readonly _file: TFile;
206211
protected readonly _onChunkUpload: ChunkUploadHandler<TMetadata>;
207212
protected readonly _chunkBytes: number;
208213
protected readonly _metadata: Readonly<TMetadata>;
@@ -234,7 +239,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
234239
const isLastChunk = this._position + this._chunkBytes >= this._file.size;
235240
const endPosition = isLastChunk ? this._file.size : this._position + this._chunkBytes;
236241

237-
const blob = this._file.slice(this._position, endPosition);
242+
const blob = await this._file.slice(this._position, endPosition);
238243

239244
for (let retry = 0; retry <= this._retryDelays.length; retry += 1) {
240245
try {
@@ -271,7 +276,7 @@ export class ChunkUploader<TMetadata extends Metadata> {
271276
return isLastChunk;
272277
}
273278

274-
protected _validateOptions(options: ChunkUploaderOptions<TMetadata>) {
279+
protected _validateOptions(options: ChunkUploaderOptions<TMetadata, TFile>) {
275280
if (!options.file) throw new Error('File is required');
276281
if (typeof options.file.size !== 'number') throw new Error('File size must be a number');
277282
if (typeof options.file.slice !== 'function') throw new Error('File slice must be a function');

0 commit comments

Comments
 (0)