@preventive/deptree 1.0.0-alpha.1 → 1.0.0-alpha.3

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/soldeer.d.ts ADDED
@@ -0,0 +1,168 @@
1
+ // Hand-written against soldeer.js; a change to either belongs with the other.
2
+
3
+ import type { NodeType, Vfs } from '@preventive/vfs'
4
+
5
+ export { LockfileError, TomlError } from '@preventive/lockfile/soldeer.js'
6
+
7
+ // Where @preventive/upstream caches what it fetches, zips among them. Zips
8
+ // are fetched through it, and that is the one place anything here touches
9
+ // a filesystem: from its cache, where one is set, which is where it writes
10
+ // each zip it fetches; unset, which it is by default, it has none, and
11
+ // writes nothing. Soldeer's own folders are never read. The project is
12
+ // read only through the view given as `project`, and the tree built in a
13
+ // Vfs.
14
+ export { setCacheDir } from '@preventive/upstream/npm.js'
15
+
16
+ // The machine Soldeer would install on: `soldeer` is the version that
17
+ // installs, on its own or as `forge soldeer`, and has to be 0.12.0, which
18
+ // extracts zips otherwise than 0.11 and older; `os` as Node names it —
19
+ // `linux`, `darwin`. Windows is refused: Soldeer names folders and reads
20
+ // paths in zips otherwise there.
21
+ export interface SoldeerHost {
22
+ soldeer: string
23
+ os: string
24
+ }
25
+
26
+ // A view of the project's root, by paths from `/`: a Vfs, or anything
27
+ // with its readdir, lstat, stat and readFile, such as one of a directory
28
+ // on disk, of which only soldeer.lock, foundry.toml and, where there is
29
+ // no foundry.toml, soldeer.toml are read there, as Soldeer reads them,
30
+ // each as UTF-8, and refused where it is not. Nothing is written to it.
31
+ export interface SoldeerProject {
32
+ readdir(path: string): string[]
33
+ lstat(path: string): { type: NodeType }
34
+ stat(path: string): { type: NodeType }
35
+ readFile(path: string): Uint8Array
36
+ }
37
+
38
+ // The files an install reads at the project's root: soldeer.lock, and
39
+ // foundry.toml and soldeer.toml, where they are.
40
+ //
41
+ // Of the two, the config is the one Soldeer reads: foundry.toml, where it
42
+ // has a [dependencies] table, written as one rather than inline; else
43
+ // soldeer.toml, where there is no foundry.toml. Where neither holds, which
44
+ // is so of a foundry.toml with no such table, whatever soldeer.toml there
45
+ // is, Soldeer asks which file to make its config, and that is refused. Of
46
+ // foundry.toml, what Soldeer fails on as it adds `dependencies` to the
47
+ // default profile's libs is refused: a profile or profile.default that is
48
+ // not a table, or written inline, and libs that are not an array. Of the
49
+ // config's [soldeer] settings, a value of a type Soldeer fails on is
50
+ // refused, and so is recursive_deps = true, which has Soldeer install what
51
+ // each dependency depends on too; the others change nothing beneath
52
+ // dependencies/, and any other key is passed over, as Soldeer passes it
53
+ // over. A config whose `dependencies` is inline, which Soldeer reads none
54
+ // from, is refused.
55
+ //
56
+ // The two ways the files come, one or the other: given, with `lockfile`
57
+ // and, where they are, `foundry` and `soldeer`; or read, with `project`
58
+ // and none of them.
59
+ //
60
+ // `vfs` is a Vfs to mount the tree into, at its root, which is taken to be
61
+ // the project's — it may be `project` too; without one, a new Vfs holds
62
+ // the tree alone. A Vfs that holds a `dependencies` at its root, or on
63
+ // macOS a name that is one there, is refused before anything is fetched,
64
+ // as Soldeer would keep or remove what is in it by what it holds. Nothing
65
+ // there is written over: each directory the tree has is one there or is
66
+ // made, and every file is written where nothing is. The tree is built, and
67
+ // held to every check below, before any of it is written, so a refusal
68
+ // leaves the Vfs as it was.
69
+ export type SoldeerTreeOptions = SoldeerTreeGiven | SoldeerTreeRead
70
+
71
+ export interface SoldeerTreeGiven {
72
+ lockfile: string
73
+ foundry?: string
74
+ soldeer?: string
75
+ project?: undefined
76
+ host: SoldeerHost
77
+ vfs?: Vfs
78
+ }
79
+
80
+ export interface SoldeerTreeRead {
81
+ lockfile?: undefined
82
+ foundry?: undefined
83
+ soldeer?: undefined
84
+ project: SoldeerProject
85
+ host: SoldeerHost
86
+ vfs?: Vfs
87
+ }
88
+
89
+ // What buildSoldeerTree counts, all of it plain numbers: `dependencies`
90
+ // those installed, each a zip fetched; `files` and `bytes` what is written.
91
+ export interface SoldeerTreeStats {
92
+ dependencies: number
93
+ files: number
94
+ bytes: number
95
+ }
96
+
97
+ // `vfs` is the one given, the tree mounted into it, or a new one.
98
+ export interface SoldeerTree {
99
+ vfs: Vfs
100
+ stats: SoldeerTreeStats
101
+ }
102
+
103
+ // The dependencies folder `soldeer install` makes from the lockfile, with
104
+ // host.soldeer, in a project with no dependencies folder yet: rooted at
105
+ // the project's root, it holds `dependencies/<name>-<version>` for each
106
+ // dependency, and nothing else of the project. What Soldeer writes beside
107
+ // it — the lockfile again, remappings, foundry.toml's libs — is not
108
+ // written, and where writing it fails after the install, as Soldeer may on
109
+ // a remappings.txt it does not read, that is not followed.
110
+ //
111
+ // Each dependency's zip is fetched from Soldeer's registry, as Soldeer
112
+ // 0.12 asks the registry for it again rather than taking the lockfile's
113
+ // URL, and held to the lockfile's checksum, its sha256. It is extracted as
114
+ // Soldeer extracts it, with the zip crate, into a folder of its own: no
115
+ // root folder taken off; of two entries of one name, the later alone,
116
+ // where the first was; anything under a `.git` passed over, as Windows
117
+ // reads the name — trailing dots and spaces dropped, ASCII letters in
118
+ // either case; each symlink written as a file holding its target. Each
119
+ // directory is 0o755; each file's mode is its Unix mode as the zip crate
120
+ // reads it — from the upper half of its external attributes, by whatever
121
+ // system made it, or none at all for Unix where that is 0, else for MS-DOS
122
+ // from its read-only bit — without the
123
+ // type, setuid, setgid, sticky and group and other write bits; or 0o644,
124
+ // where the zip crate reads none, or for a symlink, or for a file written
125
+ // again by a name the zip crate reads no mode of, the mode it had. Both
126
+ // are as a umask of 0o022 leaves them. The lockfile's integrity, which
127
+ // Soldeer records anew as it installs and does not check, is not checked.
128
+ //
129
+ // The lockfile is held to what the lockfile reader holds it to, with the
130
+ // config: each dependency has its entry, which Soldeer installs it from,
131
+ // and each entry a dependency. And to more than Soldeer holds it to: a zip
132
+ // is read with @preventive/archive/zip.js, which refuses what it does not
133
+ // read — a `..`, absolute or backslashed name, one starting with a drive
134
+ // letter, zip64, encryption, an entry inside one that is not a directory,
135
+ // two entries of one name that differ, a directory by its name or its
136
+ // attributes and not by the other, entries that come to more than 512
137
+ // MiB — where Soldeer would take some of it;
138
+ // and what the zip crate reads otherwise than the archive reader is
139
+ // refused: a name with a byte past ASCII that is not flagged UTF-8, which
140
+ // it reads as CP437, and an AES or a Unicode comment extra field. What
141
+ // Soldeer fails on is refused: a name with a `:` in it, an NTFS extra
142
+ // field or an extended timestamp the zip crate does not read. A folder
143
+ // Soldeer downloads another dependency's zip into, `<folder>.zip`, is
144
+ // refused. On macOS, two names in one directory that differ only in case
145
+ // or normalization are refused, as they would be one name there, and so
146
+ // is a folder that is one there with another dependency's zip.
147
+ //
148
+ // Dependencies come from Soldeer's registry alone, each by a name and a
149
+ // version the registry takes: a dependency from a git repository, which
150
+ // Soldeer clones with its history, from a URL the config gives, or one the
151
+ // registry keeps private, which it hands out to those signed in alone, is
152
+ // refused.
153
+ //
154
+ // Nothing is left to a guess: a lockfile the lockfile reader refuses, a
155
+ // config this does not read as Soldeer reads it, a dependency from
156
+ // anywhere but the registry, a check above that fails — each is refused
157
+ // with a DeptreeError, a LockfileError or a TomlError that says where. A
158
+ // TypeError is thrown for options of the wrong type.
159
+ export function buildSoldeerTree(options: SoldeerTreeOptions): Promise<SoldeerTree>
160
+
161
+ // `where` is what a refusal is about — `foundry.toml: profile.default`,
162
+ // `dependencies["forge-std"]`, an entry of a zip — or undefined for the
163
+ // call as a whole; the message leads with it. `cause` is what a package
164
+ // beneath refused with, where one did.
165
+ export class DeptreeError extends Error {
166
+ constructor(detail: string, where?: string, options?: { cause?: unknown })
167
+ where: string | undefined
168
+ }
package/soldeer.js ADDED
@@ -0,0 +1,12 @@
1
+ // The dependencies folder built in memory from a soldeer.lock, as Soldeer
2
+ // 0.12 installs it: soldeer.d.ts says what it takes and what comes back.
3
+ // The project is read through the view given, and the tree built in a Vfs;
4
+ // the one filesystem touched is that of the cache @preventive/upstream
5
+ // reads, and writes where it keeps one.
6
+ export { buildSoldeerTree } from './src/soldeer/tree.js'
7
+ export { DeptreeError } from './src/error.js'
8
+ // Where @preventive/upstream caches what it fetches, zips among it; unset,
9
+ // it keeps no cache of its own, and writes nothing.
10
+ export { setCacheDir } from '@preventive/upstream/npm.js'
11
+ // What the lockfile, and the config read with it, are refused with.
12
+ export { LockfileError, TomlError } from '@preventive/lockfile/soldeer.js'
@@ -0,0 +1,24 @@
1
+ // The packages a tree is built of are fetched and unpacked a few at a
2
+ // time, so that a large lockfile does not start a request for each at
3
+ // once.
4
+
5
+ const CONCURRENCY = 8
6
+
7
+ // Runs `job` on each of `items`, a few at a time. The first failure stops
8
+ // the rest from starting, and is thrown once every job started has ended,
9
+ // so that none goes on, fetching or writing a cache, after the call has.
10
+ export async function eachConcurrently(items, job) {
11
+ const queue = [...items]
12
+ let failure
13
+ const worker = async () => {
14
+ while (queue.length > 0 && failure === undefined) {
15
+ try {
16
+ await job(queue.shift())
17
+ } catch (error) {
18
+ failure ??= { error }
19
+ }
20
+ }
21
+ }
22
+ await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker))
23
+ if (failure !== undefined) throw failure.error
24
+ }
package/src/hash.js CHANGED
@@ -21,3 +21,7 @@ export async function sha256Hex(text, where) {
21
21
  // Whether `bytes` are what a subresource integrity names: a sha512, the
22
22
  // only kind taken, in padded base64.
23
23
  export const matchesIntegrity = async (bytes, integrity) => integrity === `sha512-${base64(await digest('SHA-512', bytes))}`
24
+
25
+ // The hex sha1 of bytes, as yarn 1 records a tarball's after the `#` of
26
+ // its URL.
27
+ export const sha1Hex = async (bytes) => hex(await digest('SHA-1', bytes))
package/src/mount.js CHANGED
@@ -1,9 +1,10 @@
1
1
  // A tree built on its own, mounted into a Vfs the caller already has, at
2
2
  // its root: the lockfile's directory. Nothing there is written over or
3
- // removed. A node_modules there already, anywhere, is refused: kept beside
4
- // the tree, Node would read it as the tree's, and removed, it would be the
5
- // caller's lost; neither is safe. Everything is checked before anything
6
- // is written, so a refusal leaves the Vfs as it was.
3
+ // removed. A node_modules there already, anywhere, is refused, or for
4
+ // another tree what its `check` refuses: kept beside the tree, Node would
5
+ // read it as the tree's, and removed, it would be the caller's lost;
6
+ // neither is safe. Everything is checked before anything is written, so a
7
+ // refusal leaves the Vfs as it was.
7
8
 
8
9
  import { basename, dirname } from '@preventive/vfs/path.js'
9
10
  import { DeptreeError, quote } from './error.js'
@@ -37,8 +38,8 @@ function typeAt(vfs, path) {
37
38
  // already or is made, and each file and link is written where nothing is,
38
39
  // and where names are `folded`, none beside a name it would be one with.
39
40
  // Each file is taken out of `tree` as it is written.
40
- export function mount(tree, target, folded) {
41
- checkNoModules(target, folded)
41
+ export function mount(tree, target, folded, check = checkNoModules) {
42
+ check(target, folded)
42
43
  // The tree's entries, parents first, each with what is there already;
43
44
  // under a directory that is not there, nothing is.
44
45
  const absent = new Set()
package/src/patch.js CHANGED
@@ -124,16 +124,17 @@ const parentsOf = (path) => path.split('/').slice(0, -1).map((_, index, names) =
124
124
  // Applies a parsed patch to a package's files: a Map of each path in the
125
125
  // package to a file, `{ data, mode }`, or to `{ directory: true }` for a
126
126
  // directory. A changed file keeps its mode, and a deleted one leaves its
127
- // directory, empty or not, as pnpm leaves them. Hands back a new Map; the
128
- // one given is left as it is.
129
- export function applyPatch(files, patch) {
127
+ // directory, empty or not, as pnpm leaves them. A file made has the mode
128
+ // its header gives, or `createdMode` where given, as pnpm 12 writes one
129
+ // plain. Hands back a new Map; the one given is left as it is.
130
+ export function applyPatch(files, patch, { createdMode } = {}) {
130
131
  const next = new Map(files)
131
132
  for (const file of patch) {
132
133
  const current = next.get(file.path)
133
134
  if (file.change === 'create') {
134
135
  const taken = current !== undefined || [...next.keys()].some((path) => path.startsWith(`${file.path}/`)) || parentsOf(file.path).some((dir) => next.get(dir)?.data !== undefined)
135
136
  if (taken) throw new DeptreeError('creates a file that is there', file.where)
136
- next.set(file.path, { data: bytesOf(applyTo('', file), file.where), mode: file.mode })
137
+ next.set(file.path, { data: bytesOf(applyTo('', file), file.where), mode: createdMode ?? file.mode })
137
138
  continue
138
139
  }
139
140
  if (current?.data === undefined) throw new DeptreeError('changes a file that is not there', file.where)