/
pack.js
351 lines (316 loc) · 9.61 KB
/
pack.js
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
// modified from browser-pack
// changes:
// - breakout generateModuleInitializer
// - breakout serializeModule
// - breakout generateBundleLoaderInitial
// - lavamoat: init bundle with loadBundle call
// - lavamoat: expect policy as an argument
// - lavamoat: make prelude optional
// - cleanup: var -> const/let
// - cleanup/refactor
const fs = require('node:fs')
const path = require('node:path')
const assert = require('node:assert')
const JSONStream = require('JSONStream')
const through = require('through2')
const umd = require('umd')
// eslint-disable-next-line n/prefer-global/buffer
const { Buffer } = require('node:buffer')
const combineSourceMap = require('combine-source-map')
const convertSourceMap = require('convert-source-map')
const jsonStringify = require('json-stable-stringify')
const defaultPreludePath = path.join(__dirname, '_prelude.js')
function newlinesIn(src) {
if (!src) {
return 0
}
const newlines = src.match(/\n/g)
return newlines ? newlines.length : 0
}
module.exports = createPacker
function createPacker({
// hook for applying sourcemaps
sourcePathForModule = (row) => row.sourceFile,
// input is (true:) objects (false:) json strings
raw = false,
standalone = false,
standaloneModule = false,
// since we always define a global loadModule fn, we ignore this legacy bify option
hasExports = false,
basedir = process.cwd(),
// include prelude in bundle
includePrelude = true,
// must be specified
prelude = fs.readFileSync(path.join(__dirname, 'runtime.js'), 'utf8'),
// prelude path for sourcemaps
preludePath = path.relative(basedir, defaultPreludePath).replace(/\\/g, '/'),
// capabilities policy enforced by prelude
policy = {},
// prune policy to only include packages used in the bundle
prunePolicy = false,
// XXX: what is this?
// eslint-disable-next-line no-unused-vars
externalRequireName,
sourceRoot,
sourceMapPrefix,
bundleWithPrecompiledModules = true,
scuttleGlobalThis = {},
scuttleGlobalThisExceptions,
} = {}) {
// stream/parser wrapping incase raw: false
const parser = raw ? through.obj() : JSONStream.parse([true])
const stream = through.obj(
function (buf, _, next) {
parser.write(buf)
next()
},
function () {
parser.end()
}
)
// these are decorated for some reason
stream.standaloneModule = standaloneModule
stream.hasExports = hasExports
let first = true
let entryFiles = []
const packages = new Set()
let lineno = 1
let sourcemap
if (includePrelude) {
assert(
prelude,
'LavaMoat CustomPack: must specify a prelude if "includePrelude" is true (default: true)'
)
}
assert(policy, 'must specify a policy')
if (scuttleGlobalThisExceptions) {
console.warn(
'Lavamoat - "scuttleGlobalThisExceptions" is deprecated. Use "scuttleGlobalThis.exceptions" instead.'
)
if (scuttleGlobalThis === true) {
scuttleGlobalThis = { enabled: true }
}
}
const exceptions =
scuttleGlobalThis?.exceptions || scuttleGlobalThisExceptions
scuttleGlobalThis.exceptions = exceptions
// toString regexps if there's any
if (exceptions) {
for (let i = 0; i < exceptions.length; i++) {
exceptions[i] = String(exceptions[i])
}
}
prelude = prelude.replace(
'__lavamoatSecurityOptions__',
JSON.stringify({
scuttleGlobalThis,
})
)
// note: pack stream cant started emitting data until its received its first module
// this is because the browserify pipeline is leaky until its finished being setup
parser.pipe(through.obj(onModule, onDone))
return stream
function onModule(moduleData, _, next) {
if (first && standalone) {
const pre = umd.prelude(standalone).trim()
stream.push(Buffer.from(pre + 'return ', 'utf8'))
}
// start of modules
if (first) {
const loaderInitial = generateBundleLoaderInitial()
lineno += newlinesIn(loaderInitial.toString())
stream.push(loaderInitial)
}
// initialize sourcemap
if (!sourcemap) {
sourcemap = combineSourceMap.create(null, sourceRoot)
if (includePrelude) {
sourcemap.addFile(
{ sourceFile: preludePath, source: prelude },
{ line: 0 }
)
}
}
const moduleEntryPrefix = first ? '' : ',\n'
lineno += newlinesIn(moduleEntryPrefix)
stream.push(Buffer.from(moduleEntryPrefix, 'utf8'))
if (prunePolicy) {
const { packageName } = moduleData
packages.add(packageName)
}
const sourceMeta = prepareModuleInitializer(
moduleData,
sourcePathForModule,
bundleWithPrecompiledModules
)
const wrappedSource = serializeModule(moduleData, sourceMeta)
if (moduleData.sourceFile && !moduleData.nomap) {
// add current file to the sourcemap
sourcemap.addFile(
{ sourceFile: moduleData.sourceFile, source: moduleData.source },
{ line: lineno + sourceMeta.offset }
)
}
stream.push(Buffer.from(wrappedSource, 'utf8'))
lineno += newlinesIn(wrappedSource)
first = false
if (moduleData.entry && moduleData.order !== undefined) {
entryFiles[moduleData.order] = moduleData.id
} else if (moduleData.entry) {
entryFiles.push(moduleData.id)
}
next()
}
function onDone() {
if (first) {
stream.push(generateBundleLoaderInitial())
}
entryFiles = entryFiles.filter(function (x) {
return x !== undefined
})
// filter the policy removing packages that arent included
let minimalPolicy = { resources: {} }
if (prunePolicy) {
Object.entries(policy.resources || {})
.filter(([packageName]) => packages.has(packageName))
.forEach(([packageName, packagePolicy]) => {
minimalPolicy.resources[packageName] = packagePolicy
})
} else {
minimalPolicy = policy
}
// close the loadBundle request
stream.push(
Buffer.from(
`],${JSON.stringify(entryFiles)},${JSON.stringify(minimalPolicy)})`,
'utf8'
)
)
if (standalone && !first) {
stream.push(
Buffer.from(
'(' +
JSON.stringify(stream.standaloneModule) +
')' +
umd.postlude(standalone),
'utf8'
)
)
}
if (sourcemap) {
let comment = sourcemap.comment()
if (sourceMapPrefix) {
comment = comment.replace(/^\/\/#/, function () {
return sourceMapPrefix
})
}
stream.push(Buffer.from('\n' + comment + '\n', 'utf8'))
}
if (!sourcemap && !standalone) {
stream.push(Buffer.from(';\n', 'utf8'))
}
stream.push(null)
}
function generateBundleLoaderInitial() {
let output = ''
// append prelude if requested
if (includePrelude) {
output += `${prelude};\n`
}
// append start of loadBundle call
output += 'LavaPack.loadBundle([\n'
return Buffer.from(output, 'utf8')
}
function serializeModule(moduleData, sourceMeta) {
const { id, packageName, deps, file } = moduleData
const relativeFilePath = file && path.relative(basedir, file)
// for now, ignore new sourcemap and just append original filename
// serialize final module entry
const jsonSerializeableData = {
// id,
package: packageName,
file: relativeFilePath,
// deps,
// source: sourceMeta.code
}
const moduleInitializer = sourceMeta.code
let serializedEntry = `[${jsonStringify(id)}, ${jsonStringify(
deps
)}, ${moduleInitializer}, {`
// add metadata
Object.entries(jsonSerializeableData).forEach(([key, value]) => {
// skip missing values
if (value === undefined) {
return
}
serializedEntry += `${key}:${jsonStringify(value)},`
})
serializedEntry += '}]'
return serializedEntry
}
}
function prepareModuleInitializer(
moduleData,
sourcePathForModule,
bundleWithPrecompiledModules
) {
const normalizedSource = moduleData.source.split('\r\n').join('\n')
// extract sourcemaps
const sourceMeta = extractSourceMaps(normalizedSource)
// create wrapper + update sourcemaps
const newSourceMeta = wrapInModuleInitializer(
moduleData,
sourceMeta,
sourcePathForModule,
bundleWithPrecompiledModules
)
return newSourceMeta
}
function extractSourceMaps(sourceCode) {
const converter = convertSourceMap.fromSource(sourceCode)
// if (!converter) throw new Error('Unable to find original inlined sourcemap')
const maps = converter && converter.toObject()
const code = convertSourceMap.removeComments(sourceCode)
return { code, maps }
}
function assertValidJS(code) {
try {
new Function(code)
} catch (err) {
throw new Error(`Invalid JavaScript: ${err.message}`)
}
}
function wrapInModuleInitializer(
moduleData,
sourceMeta,
sourcePathForModule,
bundleWithPrecompiledModules
) {
// additional layer of syntax checking independent of browserify
assertValidJS(sourceMeta.code)
const filename = encodeURI(String(moduleData.file))
let moduleWrapperSource
if (bundleWithPrecompiledModules) {
moduleWrapperSource = `function(){
with (this.scopeTerminator) {
with (this.globalThis) {
return function() {
'use strict';
// source: ${filename}
return function (require, module, exports) {
__MODULE_CONTENT__
};
};
}
}
}`
} else {
moduleWrapperSource = `// source: ${filename}\nfunction(require, module, exports){\n__MODULE_CONTENT__\n}`
}
const [start, end] = moduleWrapperSource.split('__MODULE_CONTENT__')
// im not entirely sure why the offset is 2 and not 1
const offset = start.split('\n').length - 2
const code = `${start}${sourceMeta.code}${end}`
const newSourceMeta = { code, offset }
return newSourceMeta
}