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

feat(remix-node): callback functions that enable the integration of upload progress tracking #8329

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,4 @@
- signed
- souredoutlook
- tmcw
- akoenig
41 changes: 39 additions & 2 deletions packages/remix-node/upload/fileUploadHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,30 @@ export type FileUploadHandlerOptions = {
* @param name
*/
filter?(args: FileUploadHandlerFilterArgs): boolean | Promise<boolean>;

/**
* Callback function invoked upon the successful uploading of a file chunk.
* @param args
* @returns
*/
onProgress?: (args: {
name: string;
filename: string;
contentType: string;
uploadedBytes: number;
}) => void;

/**
* Callback function triggered upon completion of the entire file upload process.
* @param args
* @returns
*/
onDone?: (args: {
name: string;
filename: string;
contentType: string;
uploadedBytes: number;
}) => void;
};

let defaultFilePathResolver: FileUploadHandlerPathResolver = ({ filename }) => {
Expand Down Expand Up @@ -95,6 +119,8 @@ export function createFileUploadHandler({
file = defaultFilePathResolver,
filter,
maxPartSize = 3000000,
onProgress,
onDone,
}: FileUploadHandlerOptions = {}): UploadHandler {
return async ({ name, filename, contentType, data }) => {
if (
Expand Down Expand Up @@ -132,14 +158,21 @@ export function createFileUploadHandler({
let writeFileStream = createWriteStream(filepath);
let size = 0;
let deleteFile = false;

let onWrite = () => {
if (onProgress) {
onProgress({ name, filename, contentType, uploadedBytes: size });
}
};

try {
for await (let chunk of data) {
size += chunk.byteLength;
if (size > maxPartSize) {
deleteFile = true;
throw new MaxPartSizeExceededError(name, maxPartSize);
}
writeFileStream.write(chunk);
writeFileStream.write(chunk, onWrite);
}
} finally {
writeFileStream.end();
Expand All @@ -150,6 +183,9 @@ export function createFileUploadHandler({
}
}

if (onDone) {
onDone({ name, filename, contentType, uploadedBytes: size });
}
// TODO: remove this typecast once TS fixed File class regression
// https://github.com/microsoft/TypeScript/issues/52166
return new NodeOnDiskFile(filepath, contentType) as unknown as File;
Expand Down Expand Up @@ -224,6 +260,7 @@ export class NodeOnDiskFile implements Omit<File, "constructor"> {

stream(): ReadableStream<any>;
stream(): NodeJS.ReadableStream;

stream(): ReadableStream<any> | NodeJS.ReadableStream {
let stream: Readable = createReadStream(this.filepath);
if (this.slicer) {
Expand All @@ -248,4 +285,4 @@ export class NodeOnDiskFile implements Omit<File, "constructor"> {
getFilePath(): string {
return this.filepath;
}
}
}