@forge/bridge
5.15.2-next.0-experimental-5b726e65.16.0-next.1
out/object-store/upload.js~
out/object-store/upload.jsModified+26
Index: package/out/object-store/upload.js
===================================================================
--- package/out/object-store/upload.js
+++ package/out/object-store/upload.js
@@ -5,8 +5,11 @@
const errors_1 = require("../errors");
const utils_1 = require("./utils");
const bridge_1 = require("../bridge");
const callBridge = (0, bridge_1.getCallBridge)();
+/**
+ * Convert base64 string to Blob
+ */
const base64ToBlob = (base64, mimeType) => {
const byteCharacters = atob(base64);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
@@ -14,28 +17,43 @@
}
const byteArray = new Uint8Array(byteNumbers);
return new Blob([byteArray], { type: mimeType || 'application/octet-stream' });
};
+/**
+ * Get metadata for a blob object including size and checksum
+ */
const getObjectMetadata = async (blob) => {
const length = blob.size;
+ // Calculate checksum using SubtleCrypto API
const arrayBuffer = await blob.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
const hashArray = new Uint8Array(hashBuffer);
+ // Convert to base64 format (required by OS API)
const checksum = btoa(String.fromCharCode(...hashArray));
const checksumType = 'SHA256';
return {
length,
checksum,
checksumType
};
};
+/**
+ * Start individual upload operations and return array of promises
+ * This allows tracking per-file upload progress
+ *
+ * @param functionKey - Configuration object containing the backend function key to filter and generate presigned URLs
+ * @param objects - Array of Blob objects or Base64Object (with data and optional mimeType) to upload
+ * @returns Array of individual upload promises with their associated index
+ * @throws {BridgeAPIError} When filtering fails or upload encounters an error
+ */
const createUploadPromises = async ({ functionKey, objects }) => {
if (!functionKey || functionKey.length === 0) {
throw new errors_1.BridgeAPIError('functionKey is required to filter and generate presigned URLs');
}
if (!Array.isArray(objects) || objects.length === 0) {
throw new errors_1.BridgeAPIError('objects array is required and must not be empty');
}
+ // Validate and convert objects to Blobs in a single pass
const blobs = objects.map((obj, index) => {
if (obj instanceof Blob) {
return obj;
}
@@ -118,8 +136,16 @@
});
return uploadPromises;
};
exports.createUploadPromises = createUploadPromises;
+/**
+ * Upload multiple objects to pre-signed URLs
+ *
+ * @param functionKey - Configuration object containing the backend function key to filter and generate presigned URLs
+ * @param objects - Array of Blob objects or Base64Object (with data and optional mimeType) to upload
+ * @returns Promise resolving to UploadResult[]
+ * @throws {BridgeAPIError} When filtering fails or upload encounters an error
+ */
const upload = async ({ functionKey, objects }) => {
await (0, utils_1.checkRestrictedEnvironment)();
void callBridge('trackObjectStoreAction', { action: 'upload' });
const uploadPromises = await (0, exports.createUploadPromises)({ functionKey, objects });