@preventive/upstream 1.0.0-alpha.5 → 1.0.0-alpha.6
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/npm.d.ts +3 -0
- package/package.json +2 -1
- package/src/npm/registry.js +19 -0
- package/src/npm/repos.js +4 -7
- package/src/npm/tarball.js +5 -4
package/npm.d.ts
CHANGED
|
@@ -6,6 +6,9 @@
|
|
|
6
6
|
// cache path, otherwise. Responses are read up to a size limit and within
|
|
7
7
|
// a timeout, as github.d.ts describes, and lookups for many names go to
|
|
8
8
|
// the registry eight at a time.
|
|
9
|
+
//
|
|
10
|
+
// Where NPM_TOKEN is set, it is sent with the requests for a scoped
|
|
11
|
+
// package, so a private one can be read.
|
|
9
12
|
|
|
10
13
|
// Where cached answers live, resolved when set. Unset by default, and
|
|
11
14
|
// unset means no cache: every read misses and every write is skipped.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@preventive/upstream",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.6",
|
|
4
4
|
"description": "Minimal npm, crates.io and Soldeer registry clients, a GitHub client, and a git checkout reader: the calls we use, nothing more",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"src/github/read.js",
|
|
79
79
|
"src/github/write.js",
|
|
80
80
|
"src/http.js",
|
|
81
|
+
"src/npm/registry.js",
|
|
81
82
|
"src/npm/repos.js",
|
|
82
83
|
"src/npm/tarball.js",
|
|
83
84
|
"src/package.js",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
|
|
3
|
+
import { show } from '../args.js'
|
|
4
|
+
import { NPM_REGISTRY, buildUrl, request } from '../http.js'
|
|
5
|
+
|
|
6
|
+
// Private packages are always scoped.
|
|
7
|
+
export function withNpmToken(name, url, options = {}) {
|
|
8
|
+
const method = options.method ?? 'GET'
|
|
9
|
+
assert.ok(method === 'GET' && `${new URL(url)}` === url && url.startsWith(`${NPM_REGISTRY}/${name}/`), `Unexpected request for ${name}: ${method} ${url}`)
|
|
10
|
+
const token = process.env.NPM_TOKEN
|
|
11
|
+
return token && name.startsWith('@') ? { ...options, method, headers: { ...options.headers, Authorization: `Bearer ${token}` } } : options
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function getDocument(method, name, spec) {
|
|
15
|
+
const url = buildUrl(NPM_REGISTRY, [...name.split('/'), spec])
|
|
16
|
+
const json = await request(url, withNpmToken(name, url, { as: 'json' }))
|
|
17
|
+
assert.ok(json?.name === name, `${method}: the registry answered for ${show(json?.name)}, not ${name}`)
|
|
18
|
+
return json
|
|
19
|
+
}
|
package/src/npm/repos.js
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
import assert from 'node:assert/strict'
|
|
2
2
|
|
|
3
|
-
import { assertNames, assertPackageName, assertRepo, isRepo
|
|
3
|
+
import { assertNames, assertPackageName, assertRepo, isRepo } from '../args.js'
|
|
4
4
|
import { readRecord, writeRecord } from '../cache.js'
|
|
5
|
-
import {
|
|
5
|
+
import { isNotFound, recover } from '../http.js'
|
|
6
6
|
import { assertRepoDirectory, getRepo, isRepoDirectory } from '../package.js'
|
|
7
7
|
import { pool } from '../pool.js'
|
|
8
|
+
import { getDocument } from './registry.js'
|
|
8
9
|
|
|
9
10
|
const DIR = 'npm/repos'
|
|
10
11
|
const CONCURRENCY = 8
|
|
11
12
|
|
|
12
13
|
// `latest`, not the full packument, which is megabytes of version history.
|
|
13
|
-
async
|
|
14
|
-
const json = await request(buildUrl(NPM_REGISTRY, [...name.split('/'), 'latest']), { as: 'json' })
|
|
15
|
-
assert.ok(json?.name === name, `${method}: the registry answered for ${show(json?.name)}, not ${name}`)
|
|
16
|
-
return getRepo(json)
|
|
17
|
-
}
|
|
14
|
+
const fetchRepo = async (method, name) => getRepo(await getDocument(method, name, 'latest'))
|
|
18
15
|
|
|
19
16
|
export async function getGitHub(name) {
|
|
20
17
|
assertPackageName('getGitHub', 'name', name)
|
package/src/npm/tarball.js
CHANGED
|
@@ -5,7 +5,8 @@ import { join, resolve } from 'node:path'
|
|
|
5
5
|
|
|
6
6
|
import { assertArgs, assertPackage, assertion, matches, show } from '../args.js'
|
|
7
7
|
import { verifiedDownload } from '../download.js'
|
|
8
|
-
import { NPM_REGISTRY, buildUrl
|
|
8
|
+
import { NPM_REGISTRY, buildUrl } from '../http.js'
|
|
9
|
+
import { getDocument, withNpmToken } from './registry.js'
|
|
9
10
|
|
|
10
11
|
const DIR = 'npm/tarballs' // No expiry: the registry never takes a version twice.
|
|
11
12
|
// One sha512 and nothing else, as the registry writes it: a sha1, a second
|
|
@@ -29,8 +30,8 @@ function checkedDist(method, name, version, dist) {
|
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
async function getDist(method, name, version) {
|
|
32
|
-
const json = await
|
|
33
|
-
assert.ok(json
|
|
33
|
+
const json = await getDocument(method, name, version)
|
|
34
|
+
assert.ok(json.version === version, `${method}: the registry answered for ${name}@${show(json.version)}, not ${name}@${version}`)
|
|
34
35
|
return checkedDist(method, name, version, { tarball: json.dist?.tarball, integrity: json.dist?.integrity })
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -70,5 +71,5 @@ export async function verifyDist(name, version, dist) {
|
|
|
70
71
|
export async function getTarball(name, version, dist) {
|
|
71
72
|
assertPackage('getTarball', name, version)
|
|
72
73
|
const { tarball, integrity } = dist === undefined ? await getDist('getTarball', name, version) : checkedDist('getTarball', name, version, dist)
|
|
73
|
-
return await verifiedDownload({ method: 'getTarball', dir: DIR, what: `${name}@${version}`, ext: 'tgz', algorithm: 'sha512', expected: integrity, local: localPaths(name, version, integrity), locate: () => tarball })
|
|
74
|
+
return await verifiedDownload({ method: 'getTarball', dir: DIR, what: `${name}@${version}`, ext: 'tgz', algorithm: 'sha512', expected: integrity, local: localPaths(name, version, integrity), locate: () => tarball, options: withNpmToken(name, tarball) })
|
|
74
75
|
}
|