UNPKG

@supernovaio/cli

Version:

Supernova.io Command Line Interface

57 lines (55 loc) 2.35 kB
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="f5bf4a42-9f98-5690-a8e3-d046f2bda418")}catch(e){}}(); import { createReadStream } from "node:fs"; import * as fs from "node:fs/promises"; import { Transform } from "node:stream"; import pRetry from "p-retry"; export async function uploadFileToUrl(input) { const { uploadUrl, filePath, contentType = "application/octet-stream", onProgress } = input; const { size: totalBytes } = await fs.stat(filePath); await pRetry(async () => { let uploadedBytes = 0; onProgress?.({ uploadedBytes, totalBytes, percentage: totalBytes === 0 ? 100 : 0, }); const progressStream = new Transform({ transform(chunk, encoding, callback) { uploadedBytes += Buffer.byteLength(chunk, encoding); onProgress?.({ uploadedBytes, totalBytes, percentage: totalBytes === 0 ? 100 : Math.min((uploadedBytes / totalBytes) * 100, 100), }); callback(null, chunk); }, }); const body = createReadStream(filePath).pipe(progressStream); const response = await fetch(uploadUrl, { method: "PUT", body, duplex: "half", headers: { "Content-Length": totalBytes.toString(), "Content-Type": contentType, }, }); if (!response.ok) { throw new Error(`Signed file upload failed with status ${response.status} ${response.statusText}`); } if (uploadedBytes < totalBytes) { onProgress?.({ uploadedBytes: totalBytes, totalBytes, percentage: 100, }); } }, { retries: 5, onFailedAttempt({ error, attemptNumber, retriesLeft }) { console.error(`Upload attempt ${attemptNumber} failed, ${retriesLeft} retries left: ${error.message}`); }, }); } //# sourceMappingURL=upload-file-to-signed-url.js.map //# debugId=f5bf4a42-9f98-5690-a8e3-d046f2bda418