forked from vercel/pkg
-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathpacker.ts
More file actions
194 lines (172 loc) · 5.47 KB
/
packer.ts
File metadata and controls
194 lines (172 loc) · 5.47 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
import assert from 'assert';
import { readFileSync } from 'fs';
import path from 'path';
import {
STORE_BLOB,
STORE_CONTENT,
STORE_LINKS,
STORE_STAT,
isDotJS,
isDotJSON,
} from './common';
import { log, wasReported } from './log';
import { FileRecord, FileRecords, SymLinks } from './types';
const { version } = JSON.parse(
readFileSync(path.join(__dirname, '../package.json'), 'utf-8'),
);
const bootstrapText = readFileSync(
require.resolve('../prelude/bootstrap.js'),
'utf8',
).replace('%VERSION%', version);
const commonText = readFileSync(require.resolve('./common'), 'utf8');
const sharedText = readFileSync(
require.resolve('../prelude/bootstrap-shared.js'),
'utf8',
);
const diagnosticText = readFileSync(
require.resolve('../prelude/diagnostic.js'),
'utf8',
);
function itemsToText<T>(items: T[]) {
const len = items.length;
return len.toString() + (len % 10 === 1 ? ' item' : ' items');
}
function hasAnyStore(record: FileRecord) {
// discarded records like native addons
for (const store of [STORE_BLOB, STORE_CONTENT, STORE_LINKS, STORE_STAT]) {
if (record[store]) return true;
}
return false;
}
interface PackerOptions {
records: FileRecords;
entrypoint: string;
bytecode: boolean;
symLinks: SymLinks;
}
export interface Stripe {
snap: string;
skip?: boolean;
store: number;
file?: string;
buffer?: Buffer;
}
export default function packer({
records,
entrypoint,
bytecode,
}: PackerOptions) {
const stripes: Stripe[] = [];
// If the entrypoint was a .mjs file that got transformed, update its extension
if (records[entrypoint]?.wasTransformed && entrypoint.endsWith('.mjs')) {
entrypoint = `${entrypoint.slice(0, -4)}.js`;
}
for (let snap in records) {
if (records[snap]) {
const record = records[snap];
const { file } = record;
if (!hasAnyStore(record)) {
continue;
}
// If .mjs file was transformed to CJS, rename it to .js in the snapshot
// This prevents Node.js from treating it as an ES module
if (record.wasTransformed && snap.endsWith('.mjs')) {
snap = `${snap.slice(0, -4)}.js`;
}
assert(record[STORE_STAT], 'packer: no STORE_STAT');
assert(
record[STORE_BLOB] ||
record[STORE_CONTENT] ||
record[STORE_LINKS] ||
record[STORE_STAT],
);
if (record[STORE_BLOB] && !bytecode) {
delete record[STORE_BLOB];
if (!record[STORE_CONTENT]) {
// TODO make a test for it?
throw wasReported(
'--no-bytecode and no source breaks final executable',
[
file,
'Please run with "-d" and without "--no-bytecode" first, and make',
'sure that debug log does not contain "was included as bytecode".',
],
);
}
}
for (const store of [
STORE_BLOB,
STORE_CONTENT,
STORE_LINKS,
STORE_STAT,
]) {
const value = record[store];
if (!value) {
continue;
}
if (store === STORE_BLOB || store === STORE_CONTENT) {
if (record.body === undefined) {
stripes.push({ snap, store, file });
} else if (Buffer.isBuffer(record.body)) {
stripes.push({ snap, store, buffer: record.body });
} else if (typeof record.body === 'string') {
stripes.push({ snap, store, buffer: Buffer.from(record.body) });
} else {
assert(false, 'packer: bad STORE_BLOB/STORE_CONTENT');
}
} else if (store === STORE_LINKS) {
if (Array.isArray(value)) {
const dedupedValue = [...new Set(value)];
log.debug('files & folders deduped = ', dedupedValue);
const buffer = Buffer.from(JSON.stringify(dedupedValue));
stripes.push({ snap, store, buffer });
} else {
assert(false, 'packer: bad STORE_LINKS');
}
} else if (store === STORE_STAT) {
if (typeof value === 'object') {
const newStat = { ...value };
const buffer = Buffer.from(JSON.stringify(newStat));
stripes.push({ snap, store, buffer });
} else {
assert(false, 'packer: unknown store');
}
}
if (record[STORE_CONTENT]) {
const disclosed = isDotJS(file) || isDotJSON(file);
log.debug(
disclosed
? 'The file was included as DISCLOSED code (with sources)'
: 'The file was included as asset content',
file,
);
} else if (record[STORE_BLOB]) {
log.debug('The file was included as bytecode (no sources)', file);
} else if (record[STORE_LINKS]) {
const link = record[STORE_LINKS];
log.debug(
`The directory files list was included (${itemsToText(link)})`,
file,
);
}
}
}
}
const prelude =
`return (function (REQUIRE_COMMON, REQUIRE_SHARED, VIRTUAL_FILESYSTEM, DEFAULT_ENTRYPOINT, SYMLINKS, DICT, DOCOMPRESS) {
${bootstrapText}${
log.debugMode ? diagnosticText : ''
}\n})(function (exports) {\n${commonText}\n},\n` +
`(function () { var module = { exports: {} };\n${sharedText}\nreturn module.exports; })(),\n` +
`%VIRTUAL_FILESYSTEM%` +
`\n,\n` +
`%DEFAULT_ENTRYPOINT%` +
`\n,\n` +
`%SYMLINKS%` +
'\n,\n' +
'%DICT%' +
'\n,\n' +
'%DOCOMPRESS%' +
`\n);`;
return { prelude, entrypoint, stripes };
}