From 003f000c6413f3ab473530a5f5370e65995ab90b Mon Sep 17 00:00:00 2001 From: Yoni Mekuria Date: Wed, 15 Feb 2023 01:33:42 +0900 Subject: [PATCH] Add timeout to warm gittar cache step (#362) * feat: add gittar fetch in time limit helper function * feat: add gittar timeout promise * feat: log when an npm package is going to be added to the cache * feat: log when fetching project template * feat: add gittar fetch and gittar timeout promise to to promise.race * feat: clear timeout * feat: add timeout to fetch project template step * feat: log when warm cache step is skipped after timeout * refactor: remove unused code * feat: increase timeout limit to 15s * refactor: make timeout function general * feat: add timeout to add npm package to cache step * refactor: make timeout function general purpose * feat: remove npm cache timeout * chore: bump version to 0.6.4 --- package-lock.json | 4 ++-- package.json | 2 +- src/postinstall.js | 31 ++++++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9b742b23..1ae98cbb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "zkapp-cli", - "version": "0.6.3", + "version": "0.6.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "zkapp-cli", - "version": "0.6.3", + "version": "0.6.4", "hasInstallScript": true, "license": "Apache-2.0", "dependencies": { diff --git a/package.json b/package.json index 2cdc3182..f15cbf66 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zkapp-cli", - "version": "0.6.3", + "version": "0.6.4", "description": "CLI to create a zkApp (\"zero-knowledge app\") for Mina Protocol.", "keywords": [ "cli", diff --git a/src/postinstall.js b/src/postinstall.js index 6e47a1ea..d8a2b3ac 100644 --- a/src/postinstall.js +++ b/src/postinstall.js @@ -43,6 +43,7 @@ async function warmNpmCache() { try { for await (const pkgWithVersion of toCache) { + console.log(` Adding ${pkgWithVersion} to the cache.`); await shExec(`npm cache add ${pkgWithVersion}`); } console.log(' Done.'); @@ -55,9 +56,37 @@ async function warmGittarCache() { console.log(' Warm cache for project template.'); try { + const fetchMaxTimeLimit = 15000; const src = 'github:o1-labs/zkapp-cli#main'; - await gittar.fetch(src, { force: true }); + console.log(' Fetching project template.'); + // Skips "Warm cache for project template" step if operation takes + // longer than the fetch max time limit. + const response = await executeInTimeLimit( + gittar.fetch(src, { force: true }), + fetchMaxTimeLimit + ); + + if (response === null) { + console.log(' Skip warm cache for project template'); + return; + } } catch (err) { console.error(err); } } + +async function executeInTimeLimit(operation, maxTimeLimit) { + let timeout; + + const timeoutPromise = new Promise((resolve, reject) => { + timeout = setTimeout(() => { + resolve(null); + }, maxTimeLimit); + }); + + const response = await Promise.race([operation, timeoutPromise]); + + if (timeout) clearTimeout(timeout); + + return response; +}