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

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.
Files changed (89) hide show
  1. package/LICENSE-APACHE +202 -0
  2. package/bin/compare.js +132 -0
  3. package/bin/deptree.js +211 -0
  4. package/bin/npmrc.js +21 -0
  5. package/cargo.d.ts +223 -0
  6. package/cargo.js +6 -0
  7. package/npm.d.ts +325 -0
  8. package/npm.js +7 -0
  9. package/package.json +92 -11
  10. package/pnpm.d.ts +401 -81
  11. package/pnpm.js +4 -9
  12. package/soldeer.d.ts +235 -0
  13. package/soldeer.js +6 -0
  14. package/src/cargo/crate.js +183 -0
  15. package/src/cargo/inputs.js +81 -0
  16. package/src/cargo/members.js +90 -0
  17. package/src/cargo/registry.js +65 -0
  18. package/src/cargo/tree.js +125 -0
  19. package/src/cargo/workspace.js +177 -0
  20. package/src/concurrent.js +32 -0
  21. package/src/error.js +8 -10
  22. package/src/glob.js +55 -0
  23. package/src/hash.js +51 -16
  24. package/src/manifest.js +52 -0
  25. package/src/matcher.js +60 -10
  26. package/src/mount.js +124 -42
  27. package/src/npm/bins.js +54 -0
  28. package/src/npm/compat.js +97 -0
  29. package/src/npm/dev-engines.js +52 -0
  30. package/src/npm/graph.js +68 -0
  31. package/src/npm/inputs.js +75 -0
  32. package/src/npm/manifests.js +130 -0
  33. package/src/npm/package.js +96 -0
  34. package/src/npm/ratio.js +34 -0
  35. package/src/npm/settings.js +67 -0
  36. package/src/npm/tree.js +139 -0
  37. package/src/npm/versions.js +20 -0
  38. package/src/npmrc.js +24 -0
  39. package/src/patch.js +26 -45
  40. package/src/pnpm/bins.js +202 -322
  41. package/src/pnpm/checks.js +8 -35
  42. package/src/pnpm/commands.js +230 -0
  43. package/src/pnpm/filename.js +19 -16
  44. package/src/pnpm/frozen.js +29 -73
  45. package/src/pnpm/gitignore.js +138 -0
  46. package/src/pnpm/glob.js +65 -0
  47. package/src/pnpm/graph.js +26 -34
  48. package/src/pnpm/hoist.js +96 -59
  49. package/src/pnpm/hoisted.js +234 -0
  50. package/src/pnpm/hook.js +26 -31
  51. package/src/pnpm/inputs.js +177 -0
  52. package/src/pnpm/install.js +123 -124
  53. package/src/pnpm/local.js +95 -40
  54. package/src/pnpm/minimatch.js +319 -0
  55. package/src/pnpm/nm-hoist-graph.js +299 -0
  56. package/src/pnpm/nm-hoist.js +149 -0
  57. package/src/pnpm/npm-packlist.js +217 -0
  58. package/src/pnpm/overrides.js +11 -51
  59. package/src/pnpm/package.js +123 -0
  60. package/src/pnpm/packlist.js +176 -81
  61. package/src/pnpm/patches.js +18 -25
  62. package/src/pnpm/projects.js +132 -89
  63. package/src/pnpm/readers.js +122 -77
  64. package/src/pnpm/settings.js +74 -95
  65. package/src/pnpm/tree.js +216 -237
  66. package/src/pnpm/uptodate.js +16 -28
  67. package/src/pnpm/workspace.js +152 -126
  68. package/src/project.js +56 -0
  69. package/src/soldeer/config.js +63 -0
  70. package/src/soldeer/git.js +52 -0
  71. package/src/soldeer/inputs.js +30 -0
  72. package/src/soldeer/tree.js +83 -0
  73. package/src/soldeer/zip.js +121 -0
  74. package/src/tarball.js +123 -131
  75. package/src/yarn1/bins.js +28 -0
  76. package/src/yarn1/compat.js +67 -0
  77. package/src/yarn1/hoist.js +249 -0
  78. package/src/yarn1/inputs.js +83 -0
  79. package/src/yarn1/manifest.js +64 -0
  80. package/src/yarn1/package.js +206 -0
  81. package/src/yarn1/peers.js +51 -0
  82. package/src/yarn1/requests.js +74 -0
  83. package/src/yarn1/resolve.js +289 -0
  84. package/src/yarn1/settings.js +95 -0
  85. package/src/yarn1/tree.js +219 -0
  86. package/yarn1.d.ts +368 -0
  87. package/yarn1.js +7 -0
  88. package/src/pnpm/npmrc.js +0 -31
  89. /package/{LICENSE → LICENSE-MIT} +0 -0
package/src/patch.js CHANGED
@@ -1,42 +1,40 @@
1
- // A patch applied to a package's files, as `pnpm patch-commit`
2
- // writes one: a git diff, a `diff --git a/<path> b/<path>` header for each
3
- // file, and unified hunks, a path relative to the package's directory. A
4
- // file is changed, created from /dev/null or deleted; a rename, a copy, a
5
- // change of mode and a binary patch are refused, as is anything else
6
- // between one file's hunks and the next header.
7
- //
8
- // A hunk applies where it says and nowhere else, every line of context and
9
- // every line it removes the same, byte for byte, as the file has there.
10
- // pnpm (@pnpm/patch-package) would also look up to twenty lines away and
11
- // compare lines with their trailing whitespace dropped; a patch that needs
12
- // either is refused here, so a patch applied here leaves the file as pnpm
13
- // would. So is a hunk that inserts after a line and keeps no context,
14
- // which pnpm puts a line early.
1
+ // A patch as `pnpm patch-commit` writes one: a git diff of files changed,
2
+ // created or deleted. A hunk applies where it says, its context and removed
3
+ // lines the same byte for byte. pnpm (@pnpm/patch-package) would also look
4
+ // up to twenty lines away and drop trailing whitespace, and puts a hunk that
5
+ // inserts after a line with no context a line early; a patch that needs any
6
+ // of that is refused, so one applied here leaves the file as pnpm would.
15
7
 
8
+ import { utf8fromString } from '@exodus/bytes/utf8.js'
16
9
  import { applyChangeSet, parseDiff } from '@preventive/diff'
17
10
  import { DeptreeError, quote } from './error.js'
11
+ import { UNSAFE, isInside } from './mount.js'
12
+ import { decodeUtf8 } from './project.js'
18
13
 
19
14
  const HEADER = 'diff --git a/'
20
15
  const MODE = /^(?:new|deleted) file mode (100644|100755)$/u
21
16
  const INDEX = /^index [\da-f]+\.\.[\da-f]+(?: 100644| 100755)?$/u
22
17
  const HUNK_LINE = /^(?:@@ |[ +\\-]|$)/u
23
18
 
24
- // The path a header names on both sides, or undefined where the two differ.
25
19
  function pathOf(header) {
26
20
  const rest = header.slice(HEADER.length)
27
21
  const path = rest.slice(0, (rest.length - 3) / 2)
28
22
  return rest === `${path} b/${path}` ? path : undefined
29
23
  }
30
24
 
25
+ // git quotes a path with a control character, and by default one with any
26
+ // other non-ASCII one. Of what it may leave unquoted, a backslash, a
27
+ // separator on Windows, and what reads otherwise than it is written (a line
28
+ // or paragraph separator, a bidirectional control) are refused, as the
29
+ // lockfile reader refuses them in a path.
31
30
  function checkPath(path, where) {
32
- if (path === undefined || path.startsWith('"') || path.split('/').some((segment) => segment === '' || segment === '.' || segment === '..')) {
31
+ if (path === undefined || path.startsWith('"') || UNSAFE.test(path) || !isInside(path)) {
33
32
  throw new DeptreeError('expected a header naming one relative path on both sides', where)
34
33
  }
35
34
  return path
36
35
  }
37
36
 
38
- // One file's header: what happens to the file, and the `---`/`+++` pair
39
- // naming it, which a patch without hunks may leave out.
37
+ // The `---`/`+++` pair may be left out where the file has no hunks.
40
38
  function readHeader(lines, path, where) {
41
39
  let change = 'modify'
42
40
  let mode = 0o644
@@ -57,9 +55,8 @@ function readHeader(lines, path, where) {
57
55
  return { change, mode, body: i }
58
56
  }
59
57
 
60
- // The hunks of one file, each line of them read: parseDiff passes over
61
- // what it does not read, so every line has to be one a hunk holds, and
62
- // every one of those in a hunk.
58
+ // parseDiff passes over what it does not read, so every line is checked to
59
+ // be one a hunk holds, and every one of those to be in a hunk.
63
60
  function readHunks(lines, where) {
64
61
  if (lines.length === 0) return { hunks: [], blocks: [] }
65
62
  if (!lines.every((line) => HUNK_LINE.test(line))) throw new DeptreeError('expected only hunks after the header', where)
@@ -76,8 +73,7 @@ function readHunks(lines, where) {
76
73
  export function parsePatch(text, where) {
77
74
  if (text.includes('\r')) throw new DeptreeError('a carriage return is not supported', where)
78
75
  if (!text.startsWith(HEADER)) throw new DeptreeError(`expected ${quote(HEADER)} first`, where)
79
- const sections = text.split(/\n(?=diff --git )/u)
80
- return sections.map((section) => {
76
+ return text.split(/\n(?=diff --git )/u).map((section) => {
81
77
  const lines = section.split('\n')
82
78
  if (lines.at(-1) === '') lines.pop()
83
79
  const path = checkPath(pathOf(lines[0]), `${where}: ${quote(lines[0])}`)
@@ -87,8 +83,6 @@ export function parsePatch(text, where) {
87
83
  })
88
84
  }
89
85
 
90
- // Holds each hunk to the lines it names in `text`, and hands back what
91
- // applying them makes of it.
92
86
  function applyTo(text, { hunks, blocks, where }) {
93
87
  const records = text.split(/(?<=\n)/u)
94
88
  let end = 0
@@ -103,43 +97,30 @@ function applyTo(text, { hunks, blocks, where }) {
103
97
  return applyChangeSet(text, blocks)
104
98
  }
105
99
 
106
- const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
107
- const encoder = new TextEncoder()
108
-
109
- function textOf(file, where) {
110
- try {
111
- return decoder.decode(file.data)
112
- } catch {
113
- throw new DeptreeError('changes a file that is not UTF-8', where)
114
- }
115
- }
116
-
117
100
  function bytesOf(text, where) {
118
101
  if (!text.isWellFormed()) throw new DeptreeError('makes a file that is not well-formed text', where)
119
- return encoder.encode(text)
102
+ return utf8fromString(text)
120
103
  }
121
104
 
122
105
  const parentsOf = (path) => path.split('/').slice(0, -1).map((_, index, names) => names.slice(0, index + 1).join('/'))
123
106
 
124
- // Applies a parsed patch to a package's files: a Map of each path in the
125
- // package to a file, `{ data, mode }`, or to `{ directory: true }` for a
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) {
107
+ // `files` maps each path to `{ data, mode }` or `{ directory: true }`. A
108
+ // deleted file leaves its directory, empty or not, as pnpm does; a created
109
+ // one has `createdMode` where given, as pnpm 12 writes one plain.
110
+ export function applyPatch(files, patch, { createdMode } = {}) {
130
111
  const next = new Map(files)
131
112
  for (const file of patch) {
132
113
  const current = next.get(file.path)
133
114
  if (file.change === 'create') {
134
115
  const taken = current !== undefined || [...next.keys()].some((path) => path.startsWith(`${file.path}/`)) || parentsOf(file.path).some((dir) => next.get(dir)?.data !== undefined)
135
116
  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 })
117
+ next.set(file.path, { data: bytesOf(applyTo('', file), file.where), mode: createdMode ?? file.mode })
137
118
  continue
138
119
  }
139
120
  if (current?.data === undefined) throw new DeptreeError('changes a file that is not there', file.where)
140
121
  // A deletion git writes with --irreversible-delete has no hunks, and
141
122
  // says nothing of what it deletes.
142
- const result = file.change === 'delete' && file.hunks.length === 0 ? '' : applyTo(textOf(current, file.where), file)
123
+ const result = file.change === 'delete' && file.hunks.length === 0 ? '' : applyTo(decodeUtf8(current.data, 'changes a file that is not UTF-8', file.where), file)
143
124
  if (file.change === 'delete') {
144
125
  if (result !== '') throw new DeptreeError('deletes a file it does not remove all of', file.where)
145
126
  next.delete(file.path)