for await()

in packages/media-export/src/googleDrive.ts [51:93]


	for await (const chunk of fileStream) {
		// pause the stream to prevent node from buffering any more data whilst we upload
		fileStream.pause();
		const chunkSize = chunk.length;
		const range = `bytes ${offset}-${offset + chunkSize - 1}/${fileSize}`;

		logger.info(
			`Uploading chunk: ${range} (Upload ${Math.floor((offset / fileSize) * 100)}% complete)`,
		);

		const response = await fetch(uploadUrl, {
			method: 'PUT',
			headers: {
				'Content-Range': range,
				'Content-Length': chunkSize,
			},
			body: chunk,
		});

		if (response.ok) {
			// Response status is 308 until the final chunk. Final response includes file metadata
			const jsonResp = await response.json();
			const validationResult = DriveUploadResponse.safeParse(jsonResp);
			if (!validationResult.success) {
				throw new Error(
					`Failed to parse response from google drive, resp:${jsonResp}, error: ${validationResult.error}`,
				);
			}
			return validationResult.data.id;
		}
		if (response.status === 308) {
			//continue
		} else {
			const text = await response.text();
			logger.error(`Received ${response.status} from google, error: ${text}`);
			throw new Error(
				`Failed to upload chunk: ${response.status} ${response.statusText}`,
			);
		}

		offset += chunkSize;
		fileStream.resume();
	}