@exodus/stasis-core 1.0.0-beta.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/src/loader.js ADDED
@@ -0,0 +1,6 @@
1
+ // stasis-core's run-time loader entry. Evaluating it -- `stasis-core run`, or
2
+ // `node --import @exodus/stasis-core/loader` -- snapshots the lib's
3
+ // node:fs/path/etc. bindings and registers the module hooks.
4
+ import { install } from './hooks.js'
5
+
6
+ install()
@@ -0,0 +1,152 @@
1
+ import { KNOWN_FORMATS, assert, fileMapToObject, fileSetToObject, fromEntries, isPlainObject, posixPathEscapes, sortPaths } from './util.js'
2
+
3
+ const VERSION = 0
4
+
5
+ const normalize = ({ name, version, ecosystem, files }) => {
6
+ // `ecosystem` (when present) names the package ecosystem a dependency came from
7
+ // (`npm`, `composer`, `soldeer`); the workspace/top-level buckets omit it.
8
+ assert(ecosystem === undefined || typeof ecosystem === 'string')
9
+ return { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: fromEntries(Object.entries(files)) }
10
+ }
11
+
12
+ export class Lockfile {
13
+ static VERSION = VERSION
14
+
15
+ version = VERSION
16
+ config
17
+ entries
18
+ modules
19
+ // Recorded resolutions, mirroring the bundle's `imports` shape:
20
+ // conditions key -> parent file -> specifier -> resolved file. `null` (not an
21
+ // empty Map) when the lockfile predates resolution attestation -- consumers
22
+ // use that to tell "attests no resolutions" apart from "doesn't attest them".
23
+ imports
24
+ // Recorded loader formats: project-relative file -> Node format string
25
+ // (module/commonjs/json/{module,commonjs}-typescript), mirroring the bundle's
26
+ // `formats` map. `null` when the lockfile predates format attestation, same
27
+ // "attests none" vs "doesn't attest" distinction as `imports`.
28
+ formats
29
+
30
+ constructor({ config = { scope: 'full' }, entries, modules, imports, formats } = {}) {
31
+ assert(['node_modules', 'full'].includes(config.scope))
32
+ this.config = config
33
+ this.entries = entries ?? new Set()
34
+ this.modules = modules ?? new Map()
35
+ this.imports = imports ?? null
36
+ this.formats = formats ?? null
37
+ }
38
+
39
+ static parse(text) {
40
+ const json = JSON.parse(text)
41
+ assert(json.version === VERSION)
42
+ assert(['node_modules', 'full'].includes(json.config?.scope))
43
+
44
+ const full = json.config.scope === 'full'
45
+ assert(!!json.entries === full)
46
+ assert(!!json.sources === full)
47
+ assert(json.modules)
48
+
49
+ const modules = new Map()
50
+ for (const [dir, info] of Object.entries(json.modules)) {
51
+ assert(dir.includes('node_modules'))
52
+ assert(info?.name && info.version && info.files)
53
+ modules.set(dir, normalize(info))
54
+ }
55
+
56
+ let entries = new Set()
57
+ if (full) {
58
+ assert(Array.isArray(json.entries))
59
+ entries = new Set(json.entries)
60
+ for (const [dir, info] of Object.entries(json.sources)) {
61
+ assert(!dir.includes('node_modules'))
62
+ modules.set(dir, normalize(info))
63
+ }
64
+ }
65
+
66
+ for (const [dir, { files }] of modules) {
67
+ assert(!posixPathEscapes(dir))
68
+ assert(files)
69
+ for (const name of Object.keys(files)) {
70
+ assert(!posixPathEscapes(name))
71
+ }
72
+ }
73
+
74
+ let imports = null
75
+ if (json.imports !== undefined) {
76
+ assert(isPlainObject(json.imports))
77
+ imports = new Map()
78
+ for (const [conditions, byParent] of Object.entries(json.imports)) {
79
+ assert(isPlainObject(byParent))
80
+ const parents = new Map()
81
+ for (const [parent, specifiers] of Object.entries(byParent)) {
82
+ assert(!posixPathEscapes(parent))
83
+ assert(isPlainObject(specifiers))
84
+ const specs = new Map()
85
+ for (const [specifier, target] of Object.entries(specifiers)) {
86
+ // A file string (the common case) or, for a `--metro` multi-platform
87
+ // lockfile, a { platform: file } map for an edge that resolves
88
+ // differently per platform. Reject any other shape (fail closed).
89
+ if (typeof target === 'string') {
90
+ assert(!posixPathEscapes(target))
91
+ specs.set(specifier, target)
92
+ } else {
93
+ assert(isPlainObject(target) && Object.keys(target).length > 0,
94
+ 'import target must be a file or a non-empty {platform: file} map')
95
+ const byPlatform = new Map()
96
+ for (const [platform, file] of Object.entries(target)) {
97
+ assert(typeof platform === 'string' && platform.length > 0 && !platform.includes('/'), `invalid platform key '${platform}'`)
98
+ assert(typeof file === 'string')
99
+ assert(!posixPathEscapes(file))
100
+ byPlatform.set(platform, file)
101
+ }
102
+ specs.set(specifier, byPlatform)
103
+ }
104
+ }
105
+ parents.set(parent, specs)
106
+ }
107
+ imports.set(conditions, parents)
108
+ }
109
+ }
110
+
111
+ let formats = null
112
+ if (json.formats !== undefined) {
113
+ assert(isPlainObject(json.formats))
114
+ formats = new Map()
115
+ for (const [file, format] of Object.entries(json.formats)) {
116
+ assert(!posixPathEscapes(file))
117
+ // KNOWN_FORMATS is stasis-core's full universe of recognized formats.
118
+ // Rejecting unknowns here -- not at a downstream branch -- means a
119
+ // tampered or forward-incompatible lockfile fails closed at the schema
120
+ // boundary, with a message that names the offending value.
121
+ assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
122
+ formats.set(file, format)
123
+ }
124
+ }
125
+
126
+ return new Lockfile({ config: json.config, entries, modules, imports, formats })
127
+ }
128
+
129
+ serialize() {
130
+ const entries = fileSetToObject(this.entries)
131
+ const modules = []
132
+ const sources = []
133
+ for (const [dir, { name, version, ecosystem, files }] of this.modules) {
134
+ const inNodeModules = dir.includes('node_modules')
135
+ if (inNodeModules) assert(name && version && files)
136
+ const type = inNodeModules ? modules : sources
137
+ const sorted = fromEntries(Object.entries(files).toSorted((a, b) => sortPaths(a[0], b[0])))
138
+ type.push([dir, { name, version, ...(ecosystem === undefined ? {} : { ecosystem }), files: sorted }])
139
+ }
140
+ modules.sort((a, b) => sortPaths(a[0], b[0]))
141
+ sources.sort((a, b) => sortPaths(a[0], b[0]))
142
+
143
+ const store = { version: this.version, config: this.config }
144
+ if (this.config.scope === 'full') Object.assign(store, { entries, sources: fromEntries(sources) })
145
+ Object.assign(store, { modules: fromEntries(modules) })
146
+ // Legacy lockfiles (imports/formats === null) round-trip without the key,
147
+ // so parse-serialize of an old file stays byte-stable.
148
+ if (this.imports !== null) Object.assign(store, { imports: fileMapToObject(this.imports) })
149
+ if (this.formats !== null) Object.assign(store, { formats: fileMapToObject(this.formats) })
150
+ return JSON.stringify(store, undefined, 2) + '\n'
151
+ }
152
+ }
@@ -0,0 +1 @@
1
+ module.exports = require('../package.json')