forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
fabricator.ts
177 lines (160 loc) · 5.03 KB
/
fabricator.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
import { spawn, ChildProcessByStdio } from 'child_process';
import { Readable, Writable } from 'stream';
import { log } from './log';
import { Target } from './types';
const script = `
var vm = require('vm');
var module = require('module');
var stdin = Buffer.alloc(0);
process.stdin.on('data', function (data) {
stdin = Buffer.concat([ stdin, data ]);
if (stdin.length >= 4) {
var sizeOfSnap = stdin.readInt32LE(0);
if (stdin.length >= 4 + sizeOfSnap + 4) {
var sizeOfBody = stdin.readInt32LE(4 + sizeOfSnap);
if (stdin.length >= 4 + sizeOfSnap + 4 + sizeOfBody) {
var snap = stdin.toString('utf8', 4, 4 + sizeOfSnap);
var body = Buffer.alloc(sizeOfBody);
var startOfBody = 4 + sizeOfSnap + 4;
stdin.copy(body, 0, startOfBody, startOfBody + sizeOfBody);
stdin = Buffer.alloc(0);
var code = module.wrap(body);
var s = new vm.Script(code, {
filename: snap,
produceCachedData: true,
sourceless: true
});
if (!s.cachedDataProduced) {
console.error('Pkg: Cached data not produced.');
process.exit(2);
}
var h = Buffer.alloc(4);
var b = s.cachedData;
h.writeInt32LE(b.length, 0);
process.stdout.write(h);
process.stdout.write(b);
}
}
}
});
process.stdin.resume();
`;
const children: Record<
string,
ChildProcessByStdio<Writable, Readable, null>
> = {};
export function fabricate(
bakes: string[],
fabricator: Target,
snap: string,
body: Buffer,
cb: (error?: Error, buffer?: Buffer) => void,
) {
const activeBakes = bakes.filter((bake) => {
// list of bakes that don't influence the bytecode
const bake2 = bake.replace(/_/g, '-');
return !['--prof', '--v8-options', '--trace-opt', '--trace-deopt'].includes(
bake2,
);
});
const cmd = fabricator.binaryPath;
const key = JSON.stringify([cmd, activeBakes]);
let child = children[key];
if (!child) {
const stderr = log.debugMode ? process.stdout : 'ignore';
children[key] = spawn(cmd, activeBakes.concat('-e', script), {
stdio: ['pipe', 'pipe', stderr],
env: { PKG_EXECPATH: 'PKG_INVOKE_NODEJS' },
});
child = children[key];
}
function kill() {
delete children[key];
child.kill();
}
let stdout = Buffer.alloc(0);
function onError(error: Error) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
removeListeners();
kill();
cb(
new Error(
`Failed to make bytecode ${fabricator.nodeRange}-${fabricator.arch} for file ${snap} error (${error.message})`,
),
);
}
function onClose(code: number) {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
removeListeners();
kill();
if (code !== 0) {
return cb(
new Error(
`Failed to make bytecode ${fabricator.nodeRange}-${fabricator.arch} for file ${snap}`,
),
);
}
// eslint-disable-next-line no-console
console.log(stdout.toString());
return cb(new Error(`${cmd} closed unexpectedly`));
}
function onData(data: Buffer) {
stdout = Buffer.concat([stdout, data]);
if (stdout.length >= 4) {
const sizeOfBlob = stdout.readInt32LE(0);
if (stdout.length >= 4 + sizeOfBlob) {
const blob = Buffer.alloc(sizeOfBlob);
stdout.copy(blob, 0, 4, 4 + sizeOfBlob);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
removeListeners();
return cb(undefined, blob);
}
}
}
function removeListeners() {
child.removeListener('error', onError);
child.removeListener('close', onClose);
child.stdin.removeListener('error', onError);
child.stdout.removeListener('error', onError);
child.stdout.removeListener('data', onData);
}
child.on('error', onError);
child.on('close', onClose);
child.stdin.on('error', onError);
child.stdout.on('error', onError);
child.stdout.on('data', onData);
const h = Buffer.alloc(4);
let b = Buffer.from(snap);
h.writeInt32LE(b.length, 0);
child.stdin.write(h);
child.stdin.write(b);
b = body;
h.writeInt32LE(b.length, 0);
child.stdin.write(h);
child.stdin.write(b);
}
export function fabricateTwice(
bakes: string[],
fabricator: Target,
snap: string,
body: Buffer,
cb: (error?: Error, buffer?: Buffer) => void,
) {
fabricate(bakes, fabricator, snap, body, (error, buffer) => {
// node0 can not produce second time, even if first time produced fine,
// probably because of 'filename' cache. also, there are weird cases
// when node4 can not compile as well, for example file 'lib/js-yaml/dumper.js'
// of package js-yaml@3.9.0 does not get bytecode second time on node4-win-x64
if (error) return fabricate(bakes, fabricator, snap, body, cb);
cb(undefined, buffer);
});
}
export function shutdown() {
for (const key in children) {
if (children[key]) {
const child = children[key];
delete children[key];
child.kill();
}
}
}