/
builder-cache.ts
430 lines (383 loc) · 11 KB
/
builder-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import execa from 'execa';
import semver from 'semver';
import pipe from 'promisepipe';
import retry from 'async-retry';
import npa from 'npm-package-arg';
import { extract } from 'tar-fs';
import { createHash } from 'crypto';
import { createGunzip } from 'zlib';
import { join, resolve } from 'path';
import { PackageJson } from '@now/build-utils';
import XDGAppPaths from 'xdg-app-paths';
import {
createReadStream,
mkdirp,
readFile,
readJSON,
writeFile,
} from 'fs-extra';
import pkg from '../../../package.json';
import { NoBuilderCacheError } from '../errors-ts';
import wait from '../output/wait';
import { Output } from '../output';
import { getDistTag } from '../get-dist-tag';
import * as staticBuilder from './static-builder';
import { BuilderWithPackage } from './types';
import { getBundledBuilders } from './get-bundled-builders';
declare const __non_webpack_require__: typeof require;
const registryTypes = new Set(['version', 'tag', 'range']);
const localBuilders: { [key: string]: BuilderWithPackage } = {
'@now/static': {
runInProcess: true,
builder: Object.freeze(staticBuilder),
package: Object.freeze({ name: '@now/static', version: '' }),
},
};
const distTag = getDistTag(pkg.version);
export const cacheDirPromise = prepareCacheDir();
export const builderDirPromise = prepareBuilderDir();
export const builderModulePathPromise = prepareBuilderModulePath();
function readFileOrNull(
filePath: string,
encoding?: null
): Promise<Buffer | null>;
function readFileOrNull(
filePath: string,
encoding: string
): Promise<string | null>;
async function readFileOrNull(
filePath: string,
encoding?: string | null
): Promise<Buffer | string | null> {
try {
if (encoding) {
return await readFile(filePath, encoding);
}
return await readFile(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
return null;
}
throw err;
}
}
/**
* Prepare cache directory for installing now-builders
*/
export async function prepareCacheDir() {
const { NOW_BUILDER_CACHE_DIR } = process.env;
const designated = NOW_BUILDER_CACHE_DIR
? resolve(NOW_BUILDER_CACHE_DIR)
: XDGAppPaths('co.zeit.now').cache();
if (!designated) {
throw new NoBuilderCacheError();
}
const cacheDir = join(designated, 'dev');
await mkdirp(cacheDir);
return cacheDir;
}
export async function prepareBuilderDir() {
const builderDir = join(await cacheDirPromise, 'builders');
await mkdirp(builderDir);
// Extract the bundled `builders.tar.gz` file, if necessary
const bundledTarballPath = join(__dirname, '../../../assets/builders.tar.gz');
const existingPackageJson =
(await readFileOrNull(join(builderDir, 'package.json'), 'utf8')) || '{}';
const { dependencies = {} } = JSON.parse(existingPackageJson);
if (!hasBundledBuilders(dependencies)) {
const extractor = extract(builderDir);
await pipe(
createReadStream(bundledTarballPath),
createGunzip(),
extractor
);
}
return builderDir;
}
export async function prepareBuilderModulePath() {
const [builderDir, builderContents] = await Promise.all([
builderDirPromise,
readFile(join(__dirname, 'builder-worker.js')),
]);
let needsWrite = false;
const builderSha = getSha(builderContents);
const cachedBuilderPath = join(builderDir, 'builder.js');
const cachedBuilderContents = await readFileOrNull(cachedBuilderPath);
if (cachedBuilderContents) {
const cachedBuilderSha = getSha(cachedBuilderContents);
if (builderSha !== cachedBuilderSha) {
needsWrite = true;
}
} else {
needsWrite = true;
}
if (needsWrite) {
await writeFile(cachedBuilderPath, builderContents);
}
return cachedBuilderPath;
}
function getNpmVersion(use = ''): string {
const parsed = npa(use);
if (registryTypes.has(parsed.type)) {
return parsed.fetchSpec || '';
}
return '';
}
export function getBuildUtils(packages: string[]): string {
const version = packages
.map(getNpmVersion)
.some(ver => ver.includes('canary'))
? 'canary'
: 'latest';
return `@now/build-utils@${version}`;
}
function parseVersionSafe(rawSpec: string) {
try {
return semver.parse(rawSpec);
} catch (e) {
return null;
}
}
export function filterPackage(
builderSpec: string,
distTag: string,
buildersPkg: PackageJson
) {
if (builderSpec in localBuilders) return false;
const parsed = npa(builderSpec);
const parsedVersion = parseVersionSafe(parsed.rawSpec);
// skip install of already installed runtime
if (
parsed.name &&
parsed.type === 'version' &&
parsedVersion &&
buildersPkg.dependencies &&
parsedVersion.version == buildersPkg.dependencies[parsed.name]
) {
return false;
}
if (
parsed.name &&
parsed.type === 'tag' &&
parsed.fetchSpec === distTag &&
getBundledBuilders().includes(parsed.name) &&
buildersPkg.dependencies
) {
const parsedInstalled = npa(
`${parsed.name}@${buildersPkg.dependencies[parsed.name]}`
);
if (parsedInstalled.type !== 'version') {
return true;
}
const semverInstalled = semver.parse(parsedInstalled.rawSpec);
if (!semverInstalled) {
return true;
}
if (semverInstalled.prerelease.length > 0) {
return semverInstalled.prerelease[0] !== distTag;
}
if (distTag === 'latest') {
return false;
}
}
return true;
}
/**
* Install a list of builders to the cache directory.
*/
export async function installBuilders(
packagesSet: Set<string>,
yarnDir: string,
output: Output,
builderDir?: string
): Promise<void> {
const packages = Array.from(packagesSet);
if (
packages.length === 0 ||
(packages.length === 1 &&
Object.hasOwnProperty.call(localBuilders, packages[0]))
) {
// Static deployment, no builders to install
return;
}
if (!builderDir) {
builderDir = await builderDirPromise;
}
const yarnPath = join(yarnDir, 'yarn');
const buildersPkgPath = join(builderDir, 'package.json');
const buildersPkgBefore = await readJSON(buildersPkgPath);
packages.push(getBuildUtils(packages));
// Filter out any packages that come packaged with `now-cli`
const packagesToInstall = packages.filter(p =>
filterPackage(p, distTag, buildersPkgBefore)
);
if (packagesToInstall.length === 0) {
output.debug('No builders need to be installed');
return;
}
const stopSpinner = wait(
`Installing builders: ${packagesToInstall.sort().join(', ')}`
);
try {
await retry(
() =>
execa(
process.execPath,
[
yarnPath,
'add',
'--exact',
'--no-lockfile',
'--non-interactive',
...packagesToInstall,
],
{
cwd: builderDir,
}
),
{ retries: 2 }
);
} finally {
stopSpinner();
}
const updatedPackages: string[] = [];
const buildersPkgAfter = await readJSON(buildersPkgPath);
for (const [name, version] of Object.entries(buildersPkgAfter.dependencies)) {
if (version !== buildersPkgBefore.dependencies[name]) {
output.debug(`Builder "${name}" updated to version \`${version}\``);
updatedPackages.push(name);
}
}
purgeRequireCache(updatedPackages, builderDir, output);
}
export async function updateBuilders(
packagesSet: Set<string>,
yarnDir: string,
output: Output,
builderDir?: string
): Promise<string[]> {
if (!builderDir) {
builderDir = await builderDirPromise;
}
const packages = Array.from(packagesSet);
const yarnPath = join(yarnDir, 'yarn');
const buildersPkgPath = join(builderDir, 'package.json');
const buildersPkgBefore = await readJSON(buildersPkgPath);
packages.push(getBuildUtils(packages));
await retry(
() =>
execa(
process.execPath,
[
yarnPath,
'add',
'--exact',
'--no-lockfile',
'--non-interactive',
...packages.filter(p => p !== '@now/static'),
],
{
cwd: builderDir,
}
),
{ retries: 2 }
);
const updatedPackages: string[] = [];
const buildersPkgAfter = await readJSON(buildersPkgPath);
for (const [name, version] of Object.entries(buildersPkgAfter.dependencies)) {
if (version !== buildersPkgBefore.dependencies[name]) {
output.debug(`Builder "${name}" updated to version \`${version}\``);
updatedPackages.push(name);
}
}
purgeRequireCache(updatedPackages, builderDir, output);
return updatedPackages;
}
/**
* Get a builder from the cache directory.
*/
export async function getBuilder(
builderPkg: string,
yarnDir: string,
output: Output,
builderDir?: string
): Promise<BuilderWithPackage> {
let builderWithPkg: BuilderWithPackage = localBuilders[builderPkg];
if (!builderWithPkg) {
if (!builderDir) {
builderDir = await builderDirPromise;
}
const parsed = npa(builderPkg);
const buildersPkg = await readJSON(join(builderDir, 'package.json'));
const pkgName = getPackageName(parsed, buildersPkg) || builderPkg;
const dest = join(builderDir, 'node_modules', pkgName);
try {
const mod = require(dest);
const pkg = require(join(dest, 'package.json'));
builderWithPkg = {
builder: Object.freeze(mod),
package: Object.freeze(pkg),
};
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
output.debug(
`Attempted to require ${builderPkg}, but it is not installed`
);
const pkgSet = new Set([builderPkg]);
await installBuilders(pkgSet, yarnDir, output, builderDir);
// Run `getBuilder()` again now that the builder has been installed
return getBuilder(builderPkg, yarnDir, output, builderDir);
}
throw err;
}
}
return builderWithPkg;
}
function getPackageName(
parsed: npa.Result,
buildersPkg: PackageJson
): string | null {
if (registryTypes.has(parsed.type)) {
return parsed.name;
}
const deps = { ...buildersPkg.devDependencies, ...buildersPkg.dependencies };
for (const [name, dep] of Object.entries(deps)) {
if (dep === parsed.raw) {
return name;
}
}
return null;
}
function getSha(buffer: Buffer): string {
const hash = createHash('sha256');
hash.update(buffer);
return hash.digest('hex');
}
function hasBundledBuilders(dependencies: { [name: string]: string }): boolean {
for (const name of getBundledBuilders()) {
if (!(name in dependencies)) {
return false;
}
}
return true;
}
function purgeRequireCache(
packages: string[],
builderDir: string,
output: Output
) {
const _require =
typeof __non_webpack_require__ === 'function'
? __non_webpack_require__
: require;
// The `require()` cache for the builder's assets must be purged
const packagesPaths = packages.map(b => join(builderDir, 'node_modules', b));
for (const id of Object.keys(_require.cache)) {
for (const path of packagesPaths) {
if (id.startsWith(path)) {
output.debug(`Purging require cache for "${id}"`);
delete _require.cache[id];
}
}
}
}