Skip to content

Commit d9eda2a

Browse files
committed
Use asyncify
1 parent 604cc2a commit d9eda2a

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

src/upload.ts

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,8 @@ const findUploadedFiles = async (manifest: Manifest, storageBucket: any) => {
3939
return filesToUpload;
4040
};
4141

42-
export async function uploadManifest(
43-
bucket: string,
44-
manifest: Manifest,
45-
force?: boolean,
46-
ttl?: Date
47-
) {
48-
bucket = bucket || DEFAULT_BUCKET; // If bucket is blank.
49-
console.log(`Using storage: ${bucket}/${getBlobPath(manifest.site, '')}`);
50-
51-
const storage = new Storage();
52-
const bar = new cliProgress.SingleBar(
42+
function createProgressBar() {
43+
return new cliProgress.SingleBar(
5344
{
5445
format:
5546
'Uploading ({value}/{total}): ' +
@@ -58,15 +49,25 @@ export async function uploadManifest(
5849
},
5950
cliProgress.Presets.shades_classic
6051
);
52+
}
6153

62-
const storageBucket = storage.bucket(bucket);
63-
64-
// Check whether files exist prior to uploading. Existing files can be skipped.
65-
let filesToUpload = await findUploadedFiles(manifest, storageBucket);
66-
if (force) {
67-
filesToUpload = manifest.files;
68-
}
69-
54+
export async function uploadManifest(
55+
bucket: string,
56+
manifest: Manifest,
57+
force?: boolean,
58+
ttl?: Date
59+
) {
60+
bucket = bucket || DEFAULT_BUCKET;
61+
console.log(`Using storage: ${bucket}/${getBlobPath(manifest.site, '')}`);
62+
const storageBucket = new Storage().bucket(bucket);
63+
const bar = createProgressBar();
64+
65+
// Check for files that have already been uploaded. Files already uploaded do
66+
// not need to be uploaded again, as the GCS path is keyed by the file's
67+
// contents.
68+
const filesToUpload = force
69+
? manifest.files
70+
: await findUploadedFiles(manifest, storageBucket);
7071
const numFiles = filesToUpload.length;
7172
console.log(
7273
`Found new ${filesToUpload.length} files out of ${manifest.files.length} total...`
@@ -80,14 +81,12 @@ export async function uploadManifest(
8081
speed: 0,
8182
});
8283

83-
// Only upload new files.
8484
mapLimit(
8585
filesToUpload,
8686
NUM_CONCURRENT_UPLOADS,
87-
(manifestFile, callback) => {
87+
asyncify(async (manifestFile: ManifestFile) => {
8888
const remotePath = getBlobPath(manifest.site, manifestFile.hash);
8989

90-
// console.log(`Uploading ${manifestFile.cleanPath} -> ${bucket}/${remotePath}`);
9190
// NOTE: This was causing stale responses, even when rewritten by the client-server: 'public, max-age=31536000',
9291
// https://cloud.google.com/storage/docs/gsutil/addlhelp/WorkingWithObjectMetadata#cache-control
9392
// NOTE: In order for GCS to respond extremely fast, it requires a longer cache expiry time.
@@ -100,25 +99,23 @@ export async function uploadManifest(
10099
},
101100
};
102101

103-
// TODO: Handle upload errors and retries.
104-
storageBucket
105-
.upload(manifestFile.path, {
106-
//gzip: true, // gzip: true must *not* be set here, as it interferes with the proxied GCS response.
107-
destination: remotePath,
108-
metadata: metadata,
109-
})
110-
.then((resp: any) => {
111-
totalTransferred += parseInt(resp[1].size);
112-
const elapsed = Math.floor(Date.now() / 1000) - startTime;
113-
bar.update((numProcessed += 1), {
114-
speed: (totalTransferred / elapsed / (1024 * 1024)).toFixed(2),
115-
});
116-
if (numProcessed == numFiles) {
117-
bar.stop();
118-
}
119-
callback();
120-
});
121-
}
102+
// TODO: Veryify this correctly handles errors and retry attempts.
103+
const resp = await storageBucket.upload(manifestFile.path, {
104+
// NOTE: `gzip: true` must *not* be set here. Doing so interferes
105+
// with the proxied GCS response. Despite not setting `gzip: true`,
106+
// the response remains gzipped from the proxy server.
107+
destination: remotePath,
108+
metadata: metadata,
109+
});
110+
totalTransferred += parseInt(resp[1].size);
111+
const elapsed = Math.floor(Date.now() / 1000) - startTime;
112+
bar.update((numProcessed += 1), {
113+
speed: (totalTransferred / elapsed / (1024 * 1024)).toFixed(2),
114+
});
115+
if (numProcessed === numFiles) {
116+
bar.stop();
117+
}
118+
})
122119
);
123120

124121
// @ts-ignore

0 commit comments

Comments
 (0)