Skip to content

Commit

Permalink
Allow aborting uploads midway
Browse files Browse the repository at this point in the history
  • Loading branch information
ghengeveld committed Sep 22, 2023
1 parent 7f1b68b commit 83d48f1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
8 changes: 7 additions & 1 deletion node-src/lib/uploadFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default async function uploadFiles(
files: File[],
onProgress: (progress: number) => void
) {
const { experimental_abortSignal: signal } = ctx.options;
const limitConcurrency = pLimit(10);
let totalProgress = 0;

Expand All @@ -27,7 +28,11 @@ export default async function uploadFiles(

return limitConcurrency(() =>
retry(
async () => {
async (bail) => {
if (signal?.aborted) {
return bail(signal.reason || new Error('Aborted'));
}

const progressStream = progress();

progressStream.on('progress', ({ delta }) => {
Expand All @@ -46,6 +51,7 @@ export default async function uploadFiles(
'content-length': contentLength.toString(),
'cache-control': 'max-age=31536000',
},
signal,
},
{ retries: 0 } // already retrying the whole operation
);
Expand Down
22 changes: 17 additions & 5 deletions node-src/lib/uploadZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ export async function uploadZip(
contentLength: number,
onProgress: (progress: number) => void
) {
const { experimental_abortSignal: signal } = ctx.options;
let totalProgress = 0;

ctx.log.debug(`Uploading ${contentLength} bytes for '${path}' to '${url}'`);

return retry(
async () => {
async (bail) => {
if (signal?.aborted) {
return bail(signal.reason || new Error('Aborted'));
}

const progressStream = progress();

progressStream.on('progress', ({ delta }) => {
Expand All @@ -38,6 +43,7 @@ export async function uploadZip(
'content-type': 'application/zip',
'content-length': contentLength.toString(),
},
signal,
},
{ retries: 0 } // already retrying the whole operation
);
Expand All @@ -60,24 +66,30 @@ export async function uploadZip(
}

export async function waitForUnpack(ctx: Context, url: string) {
const { experimental_abortSignal: signal } = ctx.options;

ctx.log.debug(`Waiting for zip unpack sentinel file to appear at '${url}'`);

return retry(
async (bail: (reason: Error) => void) => {
async (bail) => {
if (signal?.aborted) {
return bail(signal.reason || new Error('Aborted'));
}

let res: Response;
try {
res = await ctx.http.fetch(url, {}, { retries: 0, noLogErrorBody: true });
res = await ctx.http.fetch(url, { signal }, { retries: 0, noLogErrorBody: true });
} catch (e) {
const { response = {} } = e;
if (response.status === 403) {
bail(new Error('Provided signature expired.'));
return bail(new Error('Provided signature expired.'));
}
throw new Error('Sentinel file not present.');
}

const result = await res.text();
if (result !== SENTINEL_SUCCESS_VALUE) {
bail(new Error('Zip file failed to unpack remotely.'));
return bail(new Error('Zip file failed to unpack remotely.'));
} else {
ctx.log.debug(`Sentinel file present, continuing.`);
}
Expand Down

0 comments on commit 83d48f1

Please sign in to comment.