forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathindex.ts
More file actions
719 lines (572 loc) · 16.1 KB
/
index.ts
File metadata and controls
719 lines (572 loc) · 16.1 KB
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import assert from 'assert';
import { existsSync, readFileSync, copyFileSync } from 'fs';
import { mkdir, readFile, rm, stat } from 'fs/promises';
import minimist from 'minimist';
import { need, system } from '@yao-pkg/pkg-fetch';
import path from 'path';
import { log, wasReported } from './log';
import help from './help';
import { isPackageJson } from './common';
import packer from './packer';
import { plusx } from './chmod';
import producer from './producer';
import refine from './refiner';
import { shutdown } from './fabricator';
import walk, { Marker, WalkerParams } from './walker';
import { Target, NodeTarget, SymLinks } from './types';
import { CompressType } from './compress_type';
import { signMachOExecutable } from './mach-o';
import pkgOptions from './options';
import sea, { seaEnhanced, signMacOSIfNeeded } from './sea';
const { version } = JSON.parse(
readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
);
function isConfiguration(file: string) {
return isPackageJson(file) || file.endsWith('.config.json');
}
function buildMarker(
configJson: Record<string, unknown> | undefined,
config: string,
inputJson: Record<string, unknown> | undefined,
input: string,
): Marker {
const marker: Marker = configJson
? { config: configJson, base: path.dirname(config), configPath: config }
: {
config: inputJson || {},
base: path.dirname(input),
configPath: input,
};
marker.toplevel = true;
return marker;
}
// http://www.openwall.com/lists/musl/2012/12/08/4
const {
hostArch,
hostPlatform,
isValidNodeRange,
knownArchs,
knownPlatforms,
toFancyArch,
toFancyPlatform,
} = system;
const hostNodeRange = `node${process.version.match(/^v(\d+)/)![1]}`;
function parseTargets(items: string[]): NodeTarget[] {
// [ 'node6-macos-x64', 'node6-linux-x64' ]
const targets: NodeTarget[] = [];
for (const item of items) {
const target = {
nodeRange: hostNodeRange,
platform: hostPlatform,
arch: hostArch,
};
if (item !== 'host') {
for (const token of item.split('-')) {
if (!token) {
continue;
}
if (isValidNodeRange(token)) {
target.nodeRange = token;
continue;
}
const p = toFancyPlatform(token);
if (knownPlatforms.indexOf(p) >= 0) {
target.platform = p;
continue;
}
const a = toFancyArch(token);
if (knownArchs.indexOf(a) >= 0) {
target.arch = a;
continue;
}
throw wasReported(`Unknown token '${token}' in '${item}'`);
}
}
targets.push(target as NodeTarget);
}
return targets;
}
function stringifyTarget(target: NodeTarget) {
const { nodeRange, platform, arch } = target;
return `${nodeRange}-${platform}-${arch}`;
}
interface DifferentResult {
nodeRange?: boolean;
platform?: boolean;
arch?: boolean;
}
function differentParts(targets: NodeTarget[]): DifferentResult {
const nodeRanges: Record<string, boolean> = {};
const platforms: Record<string, boolean> = {};
const archs: Record<string, boolean> = {};
for (const target of targets) {
nodeRanges[target.nodeRange] = true;
platforms[target.platform] = true;
archs[target.arch] = true;
}
const result: DifferentResult = {};
if (Object.keys(nodeRanges).length > 1) {
result.nodeRange = true;
}
if (Object.keys(platforms).length > 1) {
result.platform = true;
}
if (Object.keys(archs).length > 1) {
result.arch = true;
}
return result;
}
function stringifyTargetForOutput(
output: string,
target: NodeTarget,
different: DifferentResult,
) {
const a = [output];
if (different.nodeRange) {
a.push(target.nodeRange);
}
if (different.platform) {
a.push(target.platform);
}
if (different.arch) {
a.push(target.arch);
}
return a.join('-');
}
function fabricatorForTarget({ nodeRange, arch }: NodeTarget) {
let fabPlatform = hostPlatform;
if (
hostArch !== arch &&
(hostPlatform === 'linux' || hostPlatform === 'alpine')
) {
// With linuxstatic, it is possible to generate bytecode for different
// arch with simple QEMU configuration instead of the entire sysroot.
fabPlatform = 'linuxstatic';
}
return {
nodeRange,
platform: fabPlatform,
arch,
};
}
const dryRunResults: Record<string, boolean> = {};
async function needWithDryRun({
forceBuild,
nodeRange,
platform,
arch,
}: NodeTarget) {
const result = await need({
dryRun: true,
forceBuild,
nodeRange,
platform,
arch,
});
assert(['exists', 'fetched', 'built'].indexOf(result) >= 0);
dryRunResults[result] = true;
}
const targetsCache: Record<string, string> = {};
async function needViaCache(target: NodeTarget) {
const s = stringifyTarget(target);
let c = targetsCache[s];
if (c) {
return c;
}
const { forceBuild, nodeRange, platform, arch } = target;
c = await need({
forceBuild,
nodeRange,
platform,
arch,
});
targetsCache[s] = c;
return c;
}
export async function exec(argv2: string[]) {
const argv = minimist(argv2, {
boolean: [
'b',
'build',
'bytecode',
'native-build',
'd',
'debug',
'h',
'help',
'public',
'v',
'version',
'signature',
'sea',
],
string: [
'_',
'c',
'config',
'o',
'options',
'output',
'outdir',
'out-dir',
'out-path',
'public-packages',
'no-dict',
't',
'target',
'targets',
'C',
'compress',
],
default: { bytecode: true, 'native-build': true, signature: true },
});
if (argv.h || argv.help) {
help();
return;
}
// version
if (argv.v || argv.version) {
console.log(version);
return;
}
log.info(`pkg@${version}`);
// debug
log.debugMode = argv.d || argv.debug;
// forceBuild
const forceBuild = argv.b || argv.build;
// doCompress
const algo = argv.C || argv.compress || 'None';
let doCompress: CompressType = CompressType.None;
switch (algo.toLowerCase()) {
case 'brotli':
case 'br':
doCompress = CompressType.Brotli;
break;
case 'gzip':
case 'gz':
doCompress = CompressType.GZip;
break;
case 'none':
break;
default:
throw wasReported(
`Invalid compression algorithm ${algo} ( should be None, Brotli or Gzip)`,
);
}
if (doCompress !== CompressType.None) {
console.log('compression: ', CompressType[doCompress]);
}
// _
if (!argv._.length) {
throw wasReported('Entry file/directory is expected', [
'Pass --help to see usage information',
]);
}
if (argv._.length > 1) {
throw wasReported('Not more than one entry file/directory is expected');
}
// input
let input = path.resolve(argv._[0]);
if (!existsSync(input)) {
throw wasReported('Input file does not exist', [input]);
}
if ((await stat(input)).isDirectory()) {
input = path.join(input, 'package.json');
if (!existsSync(input)) {
throw wasReported('Input file does not exist', [input]);
}
}
// inputJson
let inputJson;
let inputJsonName;
if (isConfiguration(input)) {
inputJson = JSON.parse(await readFile(input, 'utf-8'));
inputJsonName = inputJson.name;
if (inputJsonName) {
inputJsonName = inputJsonName.split('/').pop(); // @org/foo
}
}
// inputBin
let inputBin;
if (inputJson) {
let { bin } = inputJson;
if (bin) {
if (typeof bin === 'object') {
if (bin[inputJsonName]) {
bin = bin[inputJsonName];
} else {
bin = bin[Object.keys(bin)[0]]; // TODO multiple inputs to pkg them all?
}
}
inputBin = path.resolve(path.dirname(input), bin);
if (!existsSync(inputBin)) {
throw wasReported(
'Bin file does not exist (taken from package.json ' +
"'bin' property)",
[inputBin],
);
}
}
}
if (inputJson && !inputBin) {
throw wasReported("Property 'bin' does not exist in", [input]);
}
// inputFin
const inputFin = inputBin || input;
// config
let config = argv.c || argv.config;
if (inputJson && config) {
throw wasReported("Specify either 'package.json' or config. Not both");
}
// configJson
let configJson;
if (config) {
config = path.resolve(config);
if (!existsSync(config)) {
throw wasReported('Config file does not exist', [config]);
}
// eslint-disable-next-line @typescript-eslint/no-require-imports
configJson = require(config); // may be either json or js
if (
!configJson.name &&
!configJson.files &&
!configJson.dependencies &&
!configJson.pkg
) {
// package.json not detected
configJson = { pkg: configJson };
}
}
// output, outputPath
let output = argv.o || argv.output;
let outputPath = argv['out-path'] || argv.outdir || argv['out-dir'];
let autoOutput = false;
if (output && outputPath) {
throw wasReported("Specify either 'output' or 'out-path'. Not both");
}
if (!output) {
let name;
if (inputJson) {
name = inputJsonName;
if (!name) {
throw wasReported("Property 'name' does not exist in", [argv._[0]]);
}
} else if (configJson) {
name = configJson.name;
}
if (!name) {
name = path.basename(inputFin);
}
if (!outputPath) {
if (inputJson && inputJson.pkg) {
outputPath = inputJson.pkg.outputPath;
} else if (configJson && configJson.pkg) {
outputPath = configJson.pkg.outputPath;
}
outputPath = outputPath || '';
}
autoOutput = true;
const ext = path.extname(name);
output = name.slice(0, -ext.length || undefined);
output = path.resolve(outputPath || '', output);
} else {
output = path.resolve(output);
}
// targets
const sTargets = argv.t || argv.target || argv.targets || '';
if (typeof sTargets !== 'string') {
throw wasReported(`Something is wrong near ${JSON.stringify(sTargets)}`);
}
let targets = parseTargets(
sTargets.split(',').filter((t) => t),
) as unknown as Array<NodeTarget & Partial<Target>>;
if (!targets.length) {
let jsonTargets;
if (inputJson && inputJson.pkg) {
jsonTargets = inputJson.pkg.targets;
} else if (configJson && configJson.pkg) {
jsonTargets = configJson.pkg.targets;
}
if (jsonTargets) {
targets = parseTargets(jsonTargets);
}
}
if (!targets.length) {
if (!autoOutput) {
targets = parseTargets(['host']);
assert(targets.length === 1);
} else {
targets = parseTargets(['linux', 'macos', 'win']);
}
log.info(
'Targets not specified. Assuming:',
`${targets.map((t) => stringifyTarget(t)).join(', ')}`,
);
}
// differentParts
const different = differentParts(targets);
// targets[].output
for (const target of targets) {
let file;
if (targets.length === 1) {
file = output;
} else {
file = stringifyTargetForOutput(output, target, different);
}
if (target.platform === 'win' && path.extname(file) !== '.exe') {
file += '.exe';
}
target.output = file;
}
// bakes
const bakes = ((argv.options || '') as string)
.split(',')
.filter((bake) => bake)
.map((bake) => `--${bake}`);
// check if input is going
// to be overwritten by output
for (const target of targets) {
if (target.output === inputFin) {
if (autoOutput) {
target.output += `-${target.platform}`;
} else {
throw wasReported('Refusing to overwrite input file', [inputFin]);
}
}
}
if (argv.sea) {
const minTargetMajor = Math.min(
...targets.map((t) => {
const v = parseInt(t.nodeRange.replace('node', ''), 10);
// Treat unparseable ranges (e.g. "latest") as current host major
return Number.isNaN(v) ? parseInt(process.version.slice(1), 10) : v;
}),
);
if ((inputJson || configJson) && minTargetMajor >= 22) {
// Enhanced SEA mode — use walker pipeline
const marker = buildMarker(configJson, config, inputJson, input);
pkgOptions.set(configJson?.pkg ?? inputJson?.pkg);
await seaEnhanced(inputFin, {
targets,
signature: argv.signature,
marker,
params: { seaMode: true },
addition: isConfiguration(input) ? input : undefined,
});
} else {
// Simple SEA mode (backward compat: plain .js file or Node < 22)
await sea(inputFin, {
targets,
signature: argv.signature,
});
}
return;
}
// fetch targets
const { bytecode } = argv;
const nativeBuild = argv['native-build'];
for (const target of targets) {
target.forceBuild = forceBuild;
await needWithDryRun(target);
target.fabricator = fabricatorForTarget(target) as Target;
if (bytecode) {
await needWithDryRun({
...target.fabricator,
forceBuild,
});
}
}
if (dryRunResults.fetched && !dryRunResults.built) {
log.info('Fetching base Node.js binaries to PKG_CACHE_PATH');
}
for (const target of targets) {
target.binaryPath = await needViaCache(target);
const f = target.fabricator;
if (f && bytecode) {
f.binaryPath = await needViaCache(f as NodeTarget);
if (f.platform === 'macos') {
// ad-hoc sign the base binary temporarily to generate bytecode
// due to the new mandatory signing requirement
const signedBinaryPath = `${f.binaryPath}-signed`;
await rm(signedBinaryPath, { recursive: true, force: true });
copyFileSync(f.binaryPath, signedBinaryPath);
try {
signMachOExecutable(signedBinaryPath);
} catch {
throw wasReported('Cannot generate bytecode', [
'pkg fails to run "codesign" utility. Due to the mandatory signing',
'requirement of macOS, executables must be signed. Please ensure the',
'utility is installed and properly configured.',
]);
}
f.binaryPath = signedBinaryPath;
}
if (f.platform !== 'win') {
await plusx(f.binaryPath);
}
}
}
// marker
let marker: Marker;
pkgOptions.set(configJson?.pkg ?? inputJson?.pkg);
marker = buildMarker(configJson, config, inputJson, input);
// public
const params: WalkerParams = {};
if (argv.public) {
params.publicToplevel = true;
}
if (argv['public-packages']) {
params.publicPackages = argv['public-packages'].split(',');
if (params.publicPackages?.indexOf('*') !== -1) {
params.publicPackages = ['*'];
}
}
if (argv['no-dict']) {
params.noDictionary = argv['no-dict'].split(',');
if (params.noDictionary?.indexOf('*') !== -1) {
params.noDictionary = ['*'];
}
}
// records
let records;
let entrypoint = inputFin;
let symLinks: SymLinks;
const addition = isConfiguration(input) ? input : undefined;
const walkResult = await walk(marker, entrypoint, addition, params);
entrypoint = walkResult.entrypoint;
records = walkResult.records;
symLinks = walkResult.symLinks;
const refineResult = refine(records, entrypoint, symLinks);
entrypoint = refineResult.entrypoint;
records = refineResult.records;
symLinks = refineResult.symLinks;
const backpack = packer({ records, entrypoint, bytecode, symLinks });
log.debug('Targets:', JSON.stringify(targets, null, 2));
for (const target of targets) {
if (target.output && existsSync(target.output)) {
if ((await stat(target.output)).isFile()) {
await rm(target.output, { recursive: true, force: true });
} else {
throw wasReported('Refusing to overwrite non-file output', [
target.output,
]);
}
} else if (target.output) {
await mkdir(path.dirname(target.output), { recursive: true });
}
await producer({
backpack,
bakes,
slash: target.platform === 'win' ? '\\' : '/',
target: target as Target,
symLinks,
doCompress,
nativeBuild,
});
if (target.platform !== 'win' && target.output) {
await signMacOSIfNeeded(target.output, target, argv.signature);
await plusx(target.output);
}
}
shutdown();
}