@exodus/stasis 1.0.0-beta.9 → 1.0.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/bin/stasis.js +222 -334
- package/package.json +38 -14
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1664 -1001
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +211 -492
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +152 -249
- package/src/resolve-node.js +29 -45
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +70 -97
- package/src/scan.js +240 -282
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +1315 -0
- package/src/vfs-bundle/entries.js +50 -46
- package/src/vfs-bundle/github.js +114 -94
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +418 -277
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +24 -55
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
- package/src/installed-commits.js +0 -182
package/src/babel.js
CHANGED
|
@@ -1,28 +1,10 @@
|
|
|
1
|
-
// Per-file Babel pre-transforms for `stasis build
|
|
2
|
-
//
|
|
3
|
-
// --platform=hermes downlevels every code file to the dialect Hermes (React Native's
|
|
4
|
-
// engine) executes -- no classes, class fields, let/const, arrows, async generators or
|
|
5
|
-
// for-await. Two steps per file, then a constrained final bundle:
|
|
6
|
-
// 1. esbuild.transform lowers class fields / private members / static blocks / `using`
|
|
7
|
-
// into plain class syntax (stripping TS types and compiling JSX on the way), the
|
|
8
|
-
// shape Babel's class transform can consume.
|
|
9
|
-
// 2. A Babel worker pool applies transform-block-scoping + transform-classes -- the two
|
|
10
|
-
// lowerings esbuild refuses to do itself.
|
|
11
|
-
// 3. The final esbuild bundle runs with HERMES_UNSUPPORTED (arrow/async-generator/
|
|
12
|
-
// for-await lowered by esbuild; class and const-and-let declared unsupported both so
|
|
13
|
-
// esbuild's own helpers stay Hermes-safe and as a fail-closed check that none
|
|
14
|
-
// survived Babel).
|
|
15
|
-
//
|
|
16
|
-
// --babel instead runs each code file through the project's own babel.config.* verbatim --
|
|
17
|
-
// no esbuild pre-transform, Babel sees the original source exactly as the project's own
|
|
18
|
-
// toolchain (e.g. Metro) would hand it over.
|
|
19
|
-
//
|
|
20
|
-
// Babel is the PROJECT's dev dependency, not stasis's: @babel/core (and, for hermes, the
|
|
21
|
-
// two transform plugins) resolve from the project directory, so the project pins its own
|
|
22
|
-
// Babel toolchain and config semantics.
|
|
1
|
+
// Per-file Babel pre-transforms for `stasis build` (--platform=hermes, --babel), and the import aliases `stasis bundle
|
|
2
|
+
// --metro --babel` takes from the project's config. Babel is the project's dev dependency, never stasis's own.
|
|
23
3
|
import { existsSync } from 'node:fs'
|
|
24
4
|
import { createRequire } from 'node:module'
|
|
25
|
-
import { join } from 'node:path'
|
|
5
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
6
|
+
|
|
7
|
+
import { memoized } from '@exodus/stasis-core/util'
|
|
26
8
|
|
|
27
9
|
const require = createRequire(import.meta.url)
|
|
28
10
|
|
|
@@ -35,97 +17,152 @@ export const HERMES_UNSUPPORTED = {
|
|
|
35
17
|
'for-await': false,
|
|
36
18
|
}
|
|
37
19
|
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
'class-field'
|
|
42
|
-
'class-
|
|
43
|
-
|
|
44
|
-
'class-private-field': false,
|
|
45
|
-
'class-private-method': false,
|
|
46
|
-
'class-private-static-accessor': false,
|
|
47
|
-
'class-private-static-field': false,
|
|
48
|
-
'class-private-static-method': false,
|
|
49
|
-
'class-static-blocks': false,
|
|
50
|
-
'class-static-field': false,
|
|
51
|
-
'import-attributes': false,
|
|
52
|
-
using: false,
|
|
53
|
-
}
|
|
20
|
+
// What Babel can't take without extra plugins (and Hermes supports none of), lowered into plain classes first.
|
|
21
|
+
const PRE_TRANSFORM_UNSUPPORTED = Object.fromEntries([
|
|
22
|
+
'class-field', 'class-private-accessor', 'class-private-brand-check', 'class-private-field', 'class-private-method',
|
|
23
|
+
'class-private-static-accessor', 'class-private-static-field', 'class-private-static-method', 'class-static-blocks',
|
|
24
|
+
'class-static-field', 'import-attributes', 'using',
|
|
25
|
+
].map((feature) => [feature, false]))
|
|
54
26
|
|
|
55
27
|
const HERMES_BABEL_PLUGINS = ['@babel/plugin-transform-block-scoping', '@babel/plugin-transform-classes']
|
|
56
28
|
|
|
57
|
-
//
|
|
58
|
-
// --loader override like .svg:text) passes through untouched.
|
|
29
|
+
// Anything else the plugin serves (json, or a --loader override like .svg:text) passes through untouched.
|
|
59
30
|
const TRANSFORMABLE_LOADERS = new Set(['js', 'ts', 'jsx', 'tsx'])
|
|
60
31
|
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
function resolveFromProject(projectDir, deps, flag) {
|
|
32
|
+
// On the main thread, so a missing install fails with instructions, not a worker crash mid-build. Absolute paths:
|
|
33
|
+
// the worker loads @babel/core by path, and plugin paths bypass Babel's own name resolution (from the wrong dir).
|
|
34
|
+
function resolveFromProject(projectDir, deps, flag, command = 'stasis build') {
|
|
65
35
|
const projectRequire = createRequire(join(projectDir, 'noop.js'))
|
|
66
36
|
try {
|
|
67
37
|
return deps.map((dep) => projectRequire.resolve(dep))
|
|
68
38
|
} catch (cause) {
|
|
69
|
-
throw new Error(
|
|
70
|
-
`
|
|
71
|
-
`(e.g. \`npm i -D ${deps.join(' ')}\`); resolution failed from ${projectDir}`,
|
|
72
|
-
{ cause }
|
|
73
|
-
)
|
|
39
|
+
throw new Error(`${command}: ${flag} needs ${deps.join(', ')} installed in the project `
|
|
40
|
+
+ `(e.g. \`npm i -D ${deps.join(' ')}\`); resolution failed from ${projectDir}`, { cause })
|
|
74
41
|
}
|
|
75
42
|
}
|
|
76
43
|
|
|
77
|
-
//
|
|
78
|
-
// (source, { path, loader }) => { contents, loader } | undefined.
|
|
44
|
+
// In StasisEsbuild's load-mode `transform` hook shape: (source, { path, loader }) => { contents, loader } | undefined.
|
|
79
45
|
export function createHermesTransform({ projectDir, esbuild }) {
|
|
80
|
-
const [babelPath, ...plugins] = resolveFromProject(
|
|
81
|
-
projectDir, ['@babel/core', ...HERMES_BABEL_PLUGINS], '--platform=hermes'
|
|
82
|
-
)
|
|
46
|
+
const [babelPath, ...plugins] = resolveFromProject(projectDir, ['@babel/core', ...HERMES_BABEL_PLUGINS], '--platform=hermes')
|
|
83
47
|
const { transformAsync } = require('./babel-worker.cjs')
|
|
84
48
|
return async (source, { path, loader }) => {
|
|
85
49
|
if (!TRANSFORMABLE_LOADERS.has(loader)) return undefined
|
|
86
50
|
const { code, warnings } = await esbuild.transform(source, {
|
|
87
|
-
sourcemap: 'inline',
|
|
88
|
-
sourcefile: path,
|
|
89
|
-
loader,
|
|
90
|
-
supported: PRE_TRANSFORM_UNSUPPORTED,
|
|
51
|
+
sourcemap: 'inline', sourcefile: path, loader, supported: PRE_TRANSFORM_UNSUPPORTED,
|
|
91
52
|
})
|
|
92
53
|
if (warnings.length > 0) {
|
|
93
54
|
const formatted = await esbuild.formatMessages(warnings, { kind: 'warning', color: false })
|
|
94
55
|
for (const line of formatted) process.stderr.write(line)
|
|
95
56
|
}
|
|
96
|
-
const result = await transformAsync(babelPath, code, {
|
|
97
|
-
compact: false,
|
|
98
|
-
babelrc: false,
|
|
99
|
-
configFile: false,
|
|
100
|
-
plugins,
|
|
101
|
-
})
|
|
57
|
+
const result = await transformAsync(babelPath, code, { compact: false, babelrc: false, configFile: false, plugins })
|
|
102
58
|
// TS is stripped and JSX compiled by the pre-transform, so the result is always plain js.
|
|
103
59
|
return { contents: result.code, loader: 'js' }
|
|
104
60
|
}
|
|
105
61
|
}
|
|
106
62
|
|
|
107
|
-
//
|
|
108
|
-
// the original source. Its output must be esbuild-parseable js -- a config that leaves TS/JSX
|
|
109
|
-
// in place fails the build in Babel or esbuild, which is the project's config to fix.
|
|
63
|
+
// Its output must be esbuild-parseable js: a config that leaves TS/JSX in place is the project's to fix.
|
|
110
64
|
export function createBabelConfigTransform({ projectDir }) {
|
|
111
|
-
const configFile =
|
|
112
|
-
.map((name) => join(projectDir, name))
|
|
113
|
-
.find((file) => existsSync(file))
|
|
114
|
-
if (!configFile) {
|
|
115
|
-
throw new Error(`stasis build: --babel found no babel.config.(js|cjs|mjs|json) in ${projectDir}`)
|
|
116
|
-
}
|
|
65
|
+
const configFile = findBabelConfig(projectDir, 'stasis build')
|
|
117
66
|
const [babelPath] = resolveFromProject(projectDir, ['@babel/core'], '--babel')
|
|
118
67
|
const { transformAsync } = require('./babel-worker.cjs')
|
|
119
68
|
return async (source, { path, loader }) => {
|
|
120
69
|
if (!TRANSFORMABLE_LOADERS.has(loader)) return undefined
|
|
121
|
-
// filename makes per-file config (overrides, .babelrc lookup) behave as in a real run.
|
|
122
|
-
const result = await transformAsync(babelPath, source, {
|
|
123
|
-
cwd: projectDir,
|
|
124
|
-
root: projectDir,
|
|
125
|
-
configFile,
|
|
126
|
-
filename: path,
|
|
127
|
-
compact: false,
|
|
128
|
-
})
|
|
70
|
+
// `filename` makes per-file config (overrides, .babelrc lookup) behave as in a real run.
|
|
71
|
+
const result = await transformAsync(babelPath, source, { cwd: projectDir, root: projectDir, configFile, filename: path, compact: false })
|
|
129
72
|
return { contents: result.code, loader: 'js' }
|
|
130
73
|
}
|
|
131
74
|
}
|
|
75
|
+
|
|
76
|
+
function findBabelConfig(projectDir, command) {
|
|
77
|
+
const configFile = ['babel.config.js', 'babel.config.cjs', 'babel.config.mjs', 'babel.config.json']
|
|
78
|
+
.map((name) => join(projectDir, name))
|
|
79
|
+
.find((file) => existsSync(file))
|
|
80
|
+
if (!configFile) throw new Error(`${command}: --babel found no babel.config.(js|cjs|mjs|json) in ${projectDir}`)
|
|
81
|
+
return configFile
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Loaded as Metro's transformer loads it (its `metro` caller): the config is project code, run here.
|
|
85
|
+
function projectBabelConfig({ projectDir, platform }, method) {
|
|
86
|
+
const configFile = findBabelConfig(projectDir, 'stasis bundle')
|
|
87
|
+
const [babelPath] = resolveFromProject(projectDir, ['@babel/core'], '--babel', 'stasis bundle')
|
|
88
|
+
const babel = require(babelPath)
|
|
89
|
+
const caller = { name: 'metro', bundler: 'metro', platform }
|
|
90
|
+
return memoized((file) => {
|
|
91
|
+
try {
|
|
92
|
+
return babel[method]({ cwd: projectDir, root: projectDir, configFile, filename: file, caller })
|
|
93
|
+
} catch (cause) {
|
|
94
|
+
throw new Error(`stasis bundle: --babel could not load ${configFile} for ${file}`, { cause })
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const MODULE_RESOLVER = /[\\/]node_modules[\\/]babel-plugin-module-resolver[\\/]/u
|
|
100
|
+
|
|
101
|
+
// The aliases babel-plugin-module-resolver applies under the project's babel.config.*: each instance the config lists
|
|
102
|
+
// counts, in order; one a preset adds doesn't. -> (file, specifier) => the specifier it is rewritten to, else null
|
|
103
|
+
export function createBabelAliases({ projectDir, platform }) {
|
|
104
|
+
const partialConfig = projectBabelConfig({ projectDir, platform }, 'loadPartialConfig')
|
|
105
|
+
// The project's own copy, which a config passing the plugin as a value passes.
|
|
106
|
+
let projectPlugin
|
|
107
|
+
try {
|
|
108
|
+
projectPlugin = createRequire(join(projectDir, 'noop.js'))('babel-plugin-module-resolver')
|
|
109
|
+
} catch {
|
|
110
|
+
projectPlugin = null
|
|
111
|
+
}
|
|
112
|
+
const moduleOf = (plugin) => {
|
|
113
|
+
const resolved = plugin.file?.resolved
|
|
114
|
+
if (resolved !== undefined) return MODULE_RESOLVER.test(resolved) ? require(resolved) : null
|
|
115
|
+
return plugin.value === projectPlugin?.default ? projectPlugin : null
|
|
116
|
+
}
|
|
117
|
+
return (file, specifier) => {
|
|
118
|
+
// No partial config: the config ignores the file.
|
|
119
|
+
for (const plugin of partialConfig(file)?.options.plugins ?? []) {
|
|
120
|
+
const module = moduleOf(plugin)
|
|
121
|
+
if (module === null) continue
|
|
122
|
+
const options = { cwd: projectDir, ...plugin.options }
|
|
123
|
+
const resolvePath = typeof options.resolvePath === 'function' ? options.resolvePath : module.resolvePath
|
|
124
|
+
const mapped = resolvePath(specifier, file, options)
|
|
125
|
+
// The first instance mapping it wins: the plugin marks the path it writes resolved, which later ones leave be.
|
|
126
|
+
if (mapped) return typeof mapped === 'string' && mapped !== specifier ? mapped : null
|
|
127
|
+
}
|
|
128
|
+
return null
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Whether Babel keeps a statement listing types alone (as `import {} from`), as `onlyRemoveTypeImports` has it.
|
|
133
|
+
// No options: the config ignores the file.
|
|
134
|
+
export function createBabelTypeImportsKept({ projectDir, platform }) {
|
|
135
|
+
const options = projectBabelConfig({ projectDir, platform }, 'loadOptions')
|
|
136
|
+
return (file) => (options(file)?.plugins ?? []).some((plugin) => plugin.key === 'transform-typescript' && plugin.options?.onlyRemoveTypeImports === true)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// babel-plugin-module-resolver's path tests, as it writes them.
|
|
140
|
+
const isRelativePath = (path) => /^\.?\.\//u.test(path)
|
|
141
|
+
const toLocalPath = (path) => {
|
|
142
|
+
const local = path.replace(/\/index$/u, '')
|
|
143
|
+
return isRelativePath(local) ? local : `./${local}`
|
|
144
|
+
}
|
|
145
|
+
const escapeRegExp = (string) => string.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&')
|
|
146
|
+
|
|
147
|
+
// --babel-aliases: babel-plugin-module-resolver's `alias` option as data, mapped as the plugin maps it, the first key
|
|
148
|
+
// matching winning. A `./` or `../` value is a path from `cwd`.
|
|
149
|
+
export function createAliases(alias, cwd) {
|
|
150
|
+
const matchers = Object.entries(alias).map(([key, value]) => {
|
|
151
|
+
// As the plugin builds them: a key's own pattern, unflagged (`u` would refuse some it takes).
|
|
152
|
+
if (!key.startsWith('^') && !key.endsWith('$')) return [new RegExp(`^${escapeRegExp(key)}(/.*|)$`), ([, rest]) => `${value}${rest}`]
|
|
153
|
+
const parts = value.split('\\\\')
|
|
154
|
+
return [new RegExp(key), (groups) => parts.map((part) => part.replace(/\\\d+/gu, (n) => groups[n.slice(1)] || '')).join('\\')]
|
|
155
|
+
})
|
|
156
|
+
return (file, specifier) => {
|
|
157
|
+
if (isRelativePath(specifier)) return null
|
|
158
|
+
for (const [regExp, substitute] of matchers) {
|
|
159
|
+
const groups = regExp.exec(specifier)
|
|
160
|
+
if (groups === null) continue
|
|
161
|
+
const mapped = substitute(groups)
|
|
162
|
+
if (!mapped) return null
|
|
163
|
+
const rewritten = isRelativePath(mapped) ? toLocalPath(relative(dirname(file), resolve(cwd, mapped)).replace(/\\/gu, '/')) : mapped
|
|
164
|
+
return rewritten === specifier ? null : rewritten
|
|
165
|
+
}
|
|
166
|
+
return null
|
|
167
|
+
}
|
|
168
|
+
}
|
|
@@ -6,10 +6,8 @@ import { Bundle } from '@exodus/stasis-core/bundle'
|
|
|
6
6
|
import { sha512integrity } from '@exodus/stasis-core/state-util'
|
|
7
7
|
import { moduleFileKey, moduleInfo, narrowExecutable } from '@exodus/stasis-core/util'
|
|
8
8
|
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
// trustworthy as a `stasis run --lock=frozen` replay. `root` is what the lockfile's paths are
|
|
12
|
-
// relative to. Directory captures (format 'directory') are skipped -- not files esbuild can load.
|
|
9
|
+
// An in-memory Bundle of a lockfile, each attested file read from disk under `root` and held to its SRI digest, as a
|
|
10
|
+
// `stasis run --lock=frozen` replay holds it. A directory capture (format 'directory') is no file to load.
|
|
13
11
|
export function bundleFromLockfile(lockfile, { root }) {
|
|
14
12
|
if (!lockfile.imports) throw new Error('stasis: lockfile has no recorded import graph')
|
|
15
13
|
const formats = lockfile.formats ?? new Map()
|
|
@@ -20,27 +18,18 @@ export function bundleFromLockfile(lockfile, { root }) {
|
|
|
20
18
|
const file = moduleFileKey(dir, rel)
|
|
21
19
|
const format = formats.get(file)
|
|
22
20
|
if (format === 'directory') continue
|
|
23
|
-
const abs = resolve(root, file)
|
|
24
21
|
let bytes
|
|
25
22
|
try {
|
|
26
|
-
bytes = readFileSync(
|
|
23
|
+
bytes = readFileSync(resolve(root, file))
|
|
27
24
|
} catch (cause) {
|
|
28
25
|
throw new Error(`stasis: file attested by the lockfile is missing on disk: ${file}`, { cause })
|
|
29
26
|
}
|
|
30
|
-
// The lockfile digests raw on-disk bytes; verify before trusting the file.
|
|
31
27
|
const actual = sha512integrity(bytes)
|
|
32
|
-
if (actual !== integrity) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// content from the verified bytes -- mirror the capture side's isUtf8 guard and fail closed.
|
|
38
|
-
if (format === 'resource:base64') {
|
|
39
|
-
out[rel] = bytes.toString('base64')
|
|
40
|
-
} else {
|
|
41
|
-
if (!isUtf8(bytes)) throw new Error(`stasis: ${file} is recorded as '${format ?? 'code'}' but is not valid UTF-8`)
|
|
42
|
-
out[rel] = bytes.toString('utf8')
|
|
43
|
-
}
|
|
28
|
+
if (actual !== integrity) throw new Error(`stasis: ${file} does not match the lockfile (expected ${integrity}, got ${actual})`)
|
|
29
|
+
// Non-base64 must be UTF-8: a lossy transcode (U+FFFD) would serve other bytes than those verified.
|
|
30
|
+
const base64 = format === 'resource:base64'
|
|
31
|
+
if (!base64 && !isUtf8(bytes)) throw new Error(`stasis: ${file} is recorded as '${format ?? 'code'}' but is not valid UTF-8`)
|
|
32
|
+
out[rel] = bytes.toString(base64 ? 'base64' : 'utf8')
|
|
44
33
|
}
|
|
45
34
|
modules.set(dir, moduleInfo({ ...info, files: out }))
|
|
46
35
|
}
|
|
@@ -50,8 +39,7 @@ export function bundleFromLockfile(lockfile, { root }) {
|
|
|
50
39
|
modules,
|
|
51
40
|
imports: lockfile.imports,
|
|
52
41
|
formats,
|
|
53
|
-
//
|
|
54
|
-
// keeping Bundle.parse's rules true for a hand-built lockfile too.
|
|
42
|
+
// To the files carried (a skipped `directory` drops out), keeping Bundle.parse's rules true for a hand-built lockfile.
|
|
55
43
|
executable: narrowExecutable(lockfile.executable, { modules, formats, scope: lockfile.config.scope }),
|
|
56
44
|
})
|
|
57
45
|
}
|
package/src/bundle-reader.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createReadStream } from 'node:fs'
|
|
|
2
2
|
import { pipeline } from 'node:stream/promises'
|
|
3
3
|
import { createBrotliDecompress } from 'node:zlib'
|
|
4
4
|
|
|
5
|
+
import { trailingJunkError } from '@exodus/stasis-core/brotli'
|
|
5
6
|
import { Bundle } from '@exodus/stasis-core/bundle'
|
|
6
7
|
import { assert } from '@exodus/stasis-core/util'
|
|
7
8
|
|
|
@@ -18,9 +19,8 @@ const untilAborted = (promise, signal) => (signal === undefined ? promise : new
|
|
|
18
19
|
// Left in place of each streamed file; fromJSON's contents-free mode requires one per file.
|
|
19
20
|
const STREAMED = Symbol('streamed')
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
// validated: what it receives (format included) is provisional until readBundle resolves. See doc/file-formats.md.
|
|
22
|
+
// parseBundleBytes(source), without holding the decompressed bytes or the JSON text whole. onFile gets each file in
|
|
23
|
+
// stream order, before the bundle is validated: what it receives is provisional until readBundle resolves.
|
|
24
24
|
export async function readBundle(source, { onFile, signal } = {}) {
|
|
25
25
|
assert(onFile === undefined || typeof onFile === 'function', 'readBundle: onFile must be a function')
|
|
26
26
|
let input = source
|
|
@@ -46,8 +46,24 @@ export async function readBundle(source, { onFile, signal } = {}) {
|
|
|
46
46
|
|
|
47
47
|
// Big chunks let zlib's thread pool decompress ahead of the parser; zlib's 16 KiB default runs about 2x slower.
|
|
48
48
|
const decompress = createBrotliDecompress({ chunkSize: 1024 * 1024, readableHighWaterMark: 4 * 1024 * 1024 })
|
|
49
|
+
// Bytes given past the decoder's bytesWritten are trailing junk. Counting them takes `input` out of pipeline's
|
|
50
|
+
// hands, so this destroys it on pipeline's signal as pipeline would: one waiting for a chunk holds it open.
|
|
51
|
+
let given = 0
|
|
52
|
+
async function* counted({ signal: stop }) {
|
|
53
|
+
const destroy = () => input.destroy?.()
|
|
54
|
+
stop.addEventListener('abort', destroy, { once: true })
|
|
55
|
+
if (stop.aborted) destroy()
|
|
56
|
+
try {
|
|
57
|
+
for await (const chunk of input) {
|
|
58
|
+
given += Buffer.byteLength(chunk)
|
|
59
|
+
yield chunk
|
|
60
|
+
}
|
|
61
|
+
} finally {
|
|
62
|
+
stop.removeEventListener('abort', destroy)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
49
65
|
let drained = false
|
|
50
|
-
await pipeline(
|
|
66
|
+
await pipeline(counted, decompress, async (chunks) => {
|
|
51
67
|
for await (const chunk of chunks) {
|
|
52
68
|
parser.write(chunk)
|
|
53
69
|
for (const [file, contents, format] of pending.splice(0)) {
|
|
@@ -58,15 +74,15 @@ export async function readBundle(source, { onFile, signal } = {}) {
|
|
|
58
74
|
}
|
|
59
75
|
drained = true
|
|
60
76
|
}, { signal }).catch((error) => {
|
|
61
|
-
//
|
|
62
|
-
if (!drained || signal?.aborted) throw error
|
|
77
|
+
// Output that ends before the input does is a premature close to pipeline: refused just below.
|
|
78
|
+
if (!drained || signal?.aborted || decompress.bytesWritten === given) throw error
|
|
63
79
|
})
|
|
80
|
+
if (decompress.bytesWritten !== given) throw trailingJunkError()
|
|
64
81
|
|
|
65
82
|
const bundle = Bundle.fromJSON(parser.end(), { contents: !onFile })
|
|
66
83
|
if (onFile) {
|
|
67
84
|
// fromJSON required a placeholder per file, so onFile got each one; it must have got nothing else.
|
|
68
|
-
|
|
69
|
-
for (const [, info] of bundle.modules) files += Object.keys(info.files).length
|
|
85
|
+
const files = bundle.modules.values().reduce((sum, info) => sum + Object.keys(info.files).length, 0)
|
|
70
86
|
assert(delivered.size === files, 'bundle carries file contents outside its file list')
|
|
71
87
|
// A later `formats` key replaces the one onFile was told about.
|
|
72
88
|
for (const [file, format] of delivered) {
|
package/src/bundle.js
CHANGED
|
@@ -1,4 +1 @@
|
|
|
1
|
-
// Public-export adapter: backs `@exodus/stasis/bundle`. The implementation lives in
|
|
2
|
-
// @exodus/stasis-core; internal tooling and tests import `@exodus/stasis-core/bundle`
|
|
3
|
-
// directly, so this file exists only to back the package's `exports` map.
|
|
4
1
|
export * from '@exodus/stasis-core/bundle'
|