Skip to content

Commit 3a3824b

Browse files
johnsimerothmikehardy
authored andcommitted
fix: implement uploadBytes for storage modular sdk
1 parent 9f0d6ed commit 3a3824b

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

packages/storage/lib/modular.ts

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import type {
3030
UploadMetadata,
3131
EmulatorMockTokenOptions,
3232
} from './types/storage';
33+
import { TaskEvent, TaskState } from './types/storage';
3334
import type { StorageReferenceInternal, StorageInternal } from './types/internal';
3435

3536
type WithModularDeprecationArg<F> = F extends (...args: infer P) => infer R
@@ -267,18 +268,46 @@ export function updateMetadata(
267268
}
268269

269270
/**
270-
* Uploads data to this object's location. The upload is not resumable.
271-
* @param _storageRef - Storage `Reference` instance.
272-
* @param _data - The data (Blob | Uint8Array | ArrayBuffer) to upload to the storage bucket at the reference location.
273-
* @param _metadata - A Storage `UploadMetadata` instance to update. Optional.
271+
* Uploads data to this object's location. The upload is not resumable. If the upload is canceled,
272+
* the Promise will reject with the TaskSnapshot. If there is an error it will reject with the StorageError
273+
* @param storageRef - Storage `Reference` instance.
274+
* @param data - The data (Blob | Uint8Array | ArrayBuffer) to upload to the storage bucket at the reference location.
275+
* @param metadata - A Storage `UploadMetadata` instance to update. Optional.
274276
* @returns {Promise<TaskResult>}
275277
*/
276278
export async function uploadBytes(
277-
_storageRef: StorageReference,
278-
_data: Blob | Uint8Array | ArrayBuffer,
279-
_metadata?: UploadMetadata,
279+
storageRef: StorageReference,
280+
data: Blob | Uint8Array | ArrayBuffer,
281+
metadata?: UploadMetadata,
280282
): Promise<TaskResult> {
281-
throw new Error('`uploadBytes()` is not implemented');
283+
const task = uploadBytesResumable(storageRef, data, metadata);
284+
return new Promise((resolve, reject) => {
285+
task.on(
286+
TaskEvent.STATE_CHANGED,
287+
taskSnapshot => {
288+
switch (taskSnapshot.state) {
289+
case TaskState.RUNNING:
290+
break;
291+
case TaskState.PAUSED:
292+
task.resume();
293+
break;
294+
case TaskState.SUCCESS:
295+
resolve({ ref: taskSnapshot.ref, metadata: taskSnapshot.metadata });
296+
break;
297+
case TaskState.CANCELED:
298+
// The TaskSnapshot may be useful to have if we reject due to cancel
299+
reject(taskSnapshot);
300+
break;
301+
case TaskState.ERROR:
302+
// this will be handled in the dedicated error listener
303+
break;
304+
default:
305+
throw new Error(`Unhandled task state in uploadBytes: ${taskSnapshot.state}`);
306+
}
307+
},
308+
error => reject(error),
309+
);
310+
});
282311
}
283312

284313
/**

0 commit comments

Comments
 (0)