Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to warm gittar cache step #362

Merged
merged 16 commits into from
Feb 14, 2023
Merged
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/postinstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const shExec = util.promisify(sh.exec);
*/
async function warmNpmCache() {
console.log(' Warm NPM cache for project template deps.');

// cwd is the root dir where zkapp-cli's package.json is located.
const tsProj = fs.readFileSync(
path.join('templates', 'project-ts', 'package.json'),
Expand All @@ -43,6 +42,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.');
Expand All @@ -55,9 +55,35 @@ async function warmGittarCache() {
console.log(' Warm cache for project template.');

try {
const maxTimeLimit = 15000;
const src = 'github:o1-labs/zkapp-cli#main';
await gittar.fetch(src, { force: true });
console.log(' Fetching project template.');
const response = await gittarFetchInTimeLimit(
gittar.fetch(src, { force: true }),
maxTimeLimit
);

if (response === null) {
console.log(' Skip warm cache for project template');
return;
}
} catch (err) {
console.error(err);
}
}

async function gittarFetchInTimeLimit(gittarFetch, maxTimeLimit) {
let gittarTimeout;

const gittarTimeoutPromise = new Promise((resolve, reject) => {
gittarTimeout = setTimeout(() => {
resolve(null);
}, maxTimeLimit);
});

const response = await Promise.race([gittarFetch, gittarTimeoutPromise]);

if (gittarTimeout) clearTimeout(gittarTimeout);

return response;
}