@preventive/deptree 1.0.0-alpha.6 → 1.0.0-alpha.7

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/project.js CHANGED
@@ -22,6 +22,15 @@ export function typeOf(project, path, follow = true) {
22
22
  // A BOM is kept, as in text handed in, for the reader to drop as pnpm does.
23
23
  const decoder = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
24
24
 
25
+ // `bytes` as text, refused with `detail` where they are not UTF-8.
26
+ export function decodeUtf8(bytes, detail, where) {
27
+ try {
28
+ return decoder.decode(bytes)
29
+ } catch {
30
+ throw new DeptreeError(detail, where)
31
+ }
32
+ }
33
+
25
34
  export function readBytes(project, path) {
26
35
  const bytes = project.readFile(path)
27
36
  if (!(bytes instanceof Uint8Array)) throw new TypeError('project.readFile must give back bytes')
@@ -32,10 +41,17 @@ export function readText(project, path, where) {
32
41
  const type = typeOf(project, path)
33
42
  if (type === undefined) return undefined
34
43
  if (type !== 'file') throw new DeptreeError(`${quote(path.slice(1))} is a ${type}, not a file`, where)
35
- const bytes = readBytes(project, path)
36
- try {
37
- return decoder.decode(bytes)
38
- } catch {
39
- throw new DeptreeError(`${quote(path.slice(1))} is not UTF-8`, where)
40
- }
44
+ return decodeUtf8(readBytes(project, path), `${quote(path.slice(1))} is not UTF-8`, where)
45
+ }
46
+
47
+ // The checks each manager's checkHost and inputsOf make alike.
48
+ export function checkHostKeys(host, keys, wanted) {
49
+ if (host === null || typeof host !== 'object') throw new TypeError(`host must be an object with ${wanted}`)
50
+ for (const key of keys) if (typeof host[key] !== 'string' || host[key] === '') throw new TypeError(`host.${key} must be a non-empty string`)
51
+ }
52
+ export function checkLeftOut(values, all = 'both') {
53
+ for (const [name, value] of Object.entries(values)) if (value !== undefined) throw new TypeError(`${name} must be left out where lockfile is: ${all} are read from project`)
54
+ }
55
+ export function checkTexts(values) {
56
+ for (const [name, value] of Object.entries(values)) if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
41
57
  }
@@ -1,13 +1,10 @@
1
1
  // What buildSoldeerTree takes, as text or read from the project's root.
2
2
 
3
3
  import { DeptreeError, quote } from '../error.js'
4
- import { checkProject, readText } from '../project.js'
4
+ import { checkHostKeys, checkLeftOut, checkProject, checkTexts, readText } from '../project.js'
5
5
 
6
6
  export function checkHost(host) {
7
- if (host === null || typeof host !== 'object') throw new TypeError('host must be an object with soldeer and os')
8
- for (const key of ['soldeer', 'os']) {
9
- if (typeof host[key] !== 'string' || host[key] === '') throw new TypeError(`host.${key} must be a non-empty string`)
10
- }
7
+ checkHostKeys(host, ['soldeer', 'os'], 'soldeer and os')
11
8
  if (host.soldeer !== '0.12.0') throw new DeptreeError(`Soldeer ${quote(host.soldeer)} is not supported: only Soldeer 0.12.0 is`, 'host.soldeer')
12
9
  if (host.os === 'win32') throw new DeptreeError('Windows is not supported: Soldeer names folders otherwise there', 'host.os')
13
10
  return { soldeer: host.soldeer, os: host.os }
@@ -20,9 +17,7 @@ export function inputsOf(options) {
20
17
  if (lockfile === undefined) {
21
18
  if (project === undefined) throw new TypeError(LOCKFILE)
22
19
  checkProject(project)
23
- for (const [name, value] of Object.entries({ foundry, soldeer })) {
24
- if (value !== undefined) throw new TypeError(`${name} must be left out where lockfile is: all are read from project`)
25
- }
20
+ checkLeftOut({ foundry, soldeer }, 'all')
26
21
  const lock = readText(project, '/soldeer.lock', 'soldeer.lock')
27
22
  if (lock === undefined) throw new DeptreeError('the project has no soldeer.lock, without which Soldeer resolves each dependency anew')
28
23
  const foundryText = readText(project, '/foundry.toml', 'foundry.toml')
@@ -30,8 +25,6 @@ export function inputsOf(options) {
30
25
  }
31
26
  if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
32
27
  if (project !== undefined) throw new TypeError('project must be left out where lockfile is given')
33
- for (const [name, value] of Object.entries({ foundry, soldeer })) {
34
- if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
35
- }
28
+ checkTexts({ foundry, soldeer })
36
29
  return { lockfile, foundry, soldeer }
37
30
  }
@@ -55,13 +55,11 @@ export async function buildSoldeerTree(options) {
55
55
  const { host: given, vfs: into } = options ?? {}
56
56
  if (into !== undefined && !(into instanceof Vfs)) throw new TypeError('vfs must be a Vfs, or left out')
57
57
  const inputs = inputsOf(options ?? {})
58
- const host = checkHost(given)
59
- const folded = host.os === 'darwin'
58
+ const folded = checkHost(given).os === 'darwin'
60
59
  // Refused before anything is fetched; mount checks again.
61
60
  if (into !== undefined) checkNoDependencies(into, folded)
62
61
  const config = configOf(inputs)
63
- const lock = parseSoldeerLockfile(inputs.lockfile, { config })
64
- const dependencies = registryDependencies(lock, config, folded)
62
+ const dependencies = registryDependencies(parseSoldeerLockfile(inputs.lockfile, { config }), config, folded)
65
63
  const extracted = await fetchAll(dependencies)
66
64
  const vfs = new Vfs()
67
65
  makeDirs(vfs, 'dependencies')
package/src/tarball.js CHANGED
@@ -7,6 +7,7 @@ import { getTarball } from '@preventive/upstream/npm.js'
7
7
  import { DeptreeError, quote } from './error.js'
8
8
  import { matchesIntegrity } from './hash.js'
9
9
  import { fold } from './mount.js'
10
+ import { decodeUtf8 } from './project.js'
10
11
 
11
12
  // What a tarball may unpack to, as upstream bounds what it downloads.
12
13
  const MAX_BYTES = 512 * 1024 * 1024
@@ -40,12 +41,30 @@ export function ownTarball(url, name, version, integrity, where) {
40
41
  const YARNPKG = 'https://registry.yarnpkg.com/'
41
42
  export const fromMirror = (url) => (url.startsWith(YARNPKG) ? `${REGISTRY}${url.slice(YARNPKG.length)}` : url)
42
43
 
43
- const sameBytes = (a, b) => a.length === b.length && a.every((byte, i) => byte === b[i])
44
44
  // The name of a package's own node_modules, where npm and yarn install its
45
45
  // dependencies, in any case: macOS takes `Node_Modules` for it.
46
46
  export const isModules = (name) => fold(name) === 'node_modules'
47
47
 
48
- export const sameFile = (a, b) => a.mode === b.mode && sameBytes(a.data, b.data)
48
+ export const sameFile = (a, b) => a.mode === b.mode && a.data.length === b.data.length && a.data.every((byte, i) => byte === b.data[i])
49
+
50
+ // Where a file's first line is a `#!` one ending in a CRLF within its first
51
+ // 2048 bytes, as bin-links' fixBin tells it, the index of its LF; else -1.
52
+ export function crlfShebang(data) {
53
+ if (data[0] !== 0x23 || data[1] !== 0x21) return -1
54
+ const newline = data.subarray(0, 2048).indexOf(0x0a)
55
+ return newline >= 4 && data[newline - 1] === 0x0d ? newline : -1
56
+ }
57
+
58
+ // `data` less such a line's CR; the file is rewritten as UTF-8, else refused with `detail`.
59
+ export function fixShebang(data, detail, where) {
60
+ const newline = crlfShebang(data)
61
+ if (newline === -1) return data
62
+ decodeUtf8(data, detail, where)
63
+ const copy = new Uint8Array(data.length - 1)
64
+ copy.set(data.subarray(0, newline - 1))
65
+ copy.set(data.subarray(newline), newline - 1)
66
+ return copy
67
+ }
49
68
 
50
69
  // Adds to `dirs` each one `files` are in, refusing one that is a file too.
51
70
  export function withDirs(files, where, dirs = new Set()) {
@@ -3,7 +3,7 @@
3
3
  import { valid } from '@preventive/upstream/semver.js'
4
4
  import { DeptreeError, quote } from '../error.js'
5
5
  import { readManifest, readManifests } from '../manifest.js'
6
- import { checkProject, readText, typeOf } from '../project.js'
6
+ import { checkHostKeys, checkLeftOut, checkProject, checkTexts, readText, typeOf } from '../project.js'
7
7
  import { walkWorkspaces } from '../glob.js'
8
8
  import { globsOf } from './manifest.js'
9
9
  import { readSettings } from './settings.js'
@@ -25,10 +25,7 @@ function yarnOf(yarn, root) {
25
25
  }
26
26
 
27
27
  export function checkHost(host, root) {
28
- if (host === null || typeof host !== 'object') throw new TypeError('host must be an object with node, os and cpu, and yarn where it is not pinned')
29
- for (const key of ['node', 'os', 'cpu']) {
30
- if (typeof host[key] !== 'string' || host[key] === '') throw new TypeError(`host.${key} must be a non-empty string`)
31
- }
28
+ checkHostKeys(host, ['node', 'os', 'cpu'], 'node, os and cpu, and yarn where it is not pinned')
32
29
  if (valid(host.node) !== host.node) throw new DeptreeError(`${quote(host.node)} is not an exact version`, 'host.node')
33
30
  if (host.os === 'win32') throw new DeptreeError('Windows is not supported: yarn links bins there with shims, and workspaces with junctions', 'host.os')
34
31
  return { yarn: yarnOf(host.yarn, root), node: host.node, os: host.os, cpu: host.cpu }
@@ -68,9 +65,7 @@ export function inputsOf(options) {
68
65
  if (project !== undefined) checkProject(project)
69
66
  if (lockfile === undefined) {
70
67
  if (project === undefined) throw new TypeError(LOCKFILE)
71
- for (const [name, value] of Object.entries({ manifests, yarnrc, npmrc })) {
72
- if (value !== undefined) throw new TypeError(`${name} must be left out where lockfile is: both are read from project`)
73
- }
68
+ checkLeftOut({ manifests, yarnrc, npmrc })
74
69
  const text = readText(project, '/yarn.lock')
75
70
  if (text === undefined) throw new DeptreeError('the project has no yarn.lock, which a frozen install cannot do without')
76
71
  const read = new Map([['.', readRoot(project)]])
@@ -82,9 +77,7 @@ export function inputsOf(options) {
82
77
  }
83
78
  if (typeof lockfile !== 'string') throw new TypeError(LOCKFILE)
84
79
  if (manifests === null || typeof manifests !== 'object') throw new TypeError('manifests must map each project\'s directory to its package.json')
85
- for (const [name, value] of Object.entries({ yarnrc, npmrc })) {
86
- if (value !== undefined && typeof value !== 'string') throw new TypeError(`${name} must be a string, or left out`)
87
- }
80
+ checkTexts({ yarnrc, npmrc })
88
81
  const settings = readSettings({ yarnrc, npmrc })
89
82
  return { lockfile, manifests: readManifests(manifests), settings, project }
90
83
  }
@@ -101,8 +101,7 @@ class Resolver {
101
101
  // the last of which its pattern names.
102
102
  check(request, info) {
103
103
  const { name, range } = splitPattern(request.pattern)
104
- const solved = validRange(range) ? info.version : range
105
- if (this.exactMatch(name, solved) !== undefined) {
104
+ if (this.exactMatch(name, validRange(range) ? info.version : range) !== undefined) {
106
105
  this.delayed.push(request)
107
106
  return []
108
107
  }
@@ -63,8 +63,7 @@ function readYarnrc(text) {
63
63
  const tokens = line.match(TOKEN) ?? []
64
64
  if (tokens.join(' ') !== line.replace(/\s+/gu, ' ') || tokens.length > 2) throw new DeptreeError('expected a setting and its value', where)
65
65
  const [key, value] = tokens.map((token) => (token.startsWith('"') ? unquote(token, 'a setting', where) : token))
66
- const bare = tokens[1]?.startsWith('"') === false
67
- settings.set(key, { value: bare ? BARE.get(value) : value, where })
66
+ settings.set(key, { value: tokens[1]?.startsWith('"') === false ? BARE.get(value) : value, where })
68
67
  }
69
68
  return settings
70
69
  }
package/yarn1.d.ts CHANGED
@@ -259,9 +259,10 @@ export interface Yarn1Tree {
259
259
  // set order.
260
260
  //
261
261
  // Ranges and versions are read with npm's own semver, borrowed from the
262
- // npm beside node as @preventive/upstream borrows it, without which this
263
- // throws. yarn 1.22 reads them with the semver 5.5.0 it bundles, which
264
- // reads alike all but the edges of what a range may spell.
262
+ // npm beside node as @preventive/upstream borrows it, or with its semver
263
+ // peer where there is no npm, without either of which this throws. yarn
264
+ // 1.22 reads them with the semver 5.5.0 it bundles, which reads alike all
265
+ // but the edges of what a range may spell.
265
266
  //
266
267
  // Nothing is left to a guess: a lockfile the lockfile reader refuses, a
267
268
  // setting this does not know or does not build for, a package from