@theholocron/holocron-plugin-npm 0.0.0-stage → 5.0.0-alpha.586

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Newton Koumantzelis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,47 @@
1
- # Temporary Holding Version
1
+ # `@theholocron/holocron-plugin-npm`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ npm plugin for [Holocron](../../cli/cli). Implements the `registry` capability against the npm registry (`whoami`,
4
+ `latestVersion`) and checks the stage-only npm token (`HOLOCRON_NPM_TOKEN`) that `holocron publish package --initial`
5
+ stages new packages with.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add -D @theholocron/holocron-plugin-npm@alpha
11
+ ```
12
+
13
+ ## Auth
14
+
15
+ The token is a stage-only granular access token, made by hand at <https://www.npmjs.com/settings/~/tokens> (Read and
16
+ write, the `@theholocron` scope, 2FA bypass off, a short expiry). Resolution order:
17
+
18
+ 1. `--token <TOKEN>` flag on the holocron invocation
19
+ 2. `HOLOCRON_NPM_TOKEN` env var
20
+ 3. `NPM_TOKEN` env var
21
+ 4. The `credentials` providers' variables (a 1Password Environment)
22
+ 5. Keyring `npm`, set via `holocron new token npm <token>`
23
+
24
+ It is held in the `agent` layer only.
25
+
26
+ ## Config
27
+
28
+ ```ts
29
+ providers: {
30
+ registry: "npm",
31
+ }
32
+ ```
33
+
34
+ The token only ever goes to `https://registry.npmjs.org`. A `baseUrl` option (for a test server) is refused unless
35
+ `unsafeBaseUrl: true` is also set, so a changed config cannot send the token to another host. Each registry request is
36
+ abandoned after 15 seconds.
37
+
38
+ ## Checking the token
39
+
40
+ ```bash
41
+ holocron probe npm token --environment agent
42
+ holocron provider npm whoami
43
+ ```
44
+
45
+ The probe reports that the token is set and that the registry accepts it. npm exposes neither a token's age or expiry
46
+ nor its permissions to the token, so the rotation and permissions rows are `info`, and it cannot tell a stage-only token
47
+ from one that can publish directly.
@@ -0,0 +1,84 @@
1
+ /**
2
+ * `@theholocron/holocron-plugin-npm` — entrypoint.
3
+ *
4
+ * Implements the `registry` capability against the npm registry (`whoami`, `latestVersion`), and exports the token
5
+ * declaration, `verifyToken` and the `token` probe. See the README for auth, config and what an npm token can be
6
+ * checked for.
7
+ */
8
+ import { AuthError, PluginToken, Registry, ResolveTokenInput, VerifyTokenFailure, VerifyTokenResult, VerifyTokenResult as VerifyTokenResult$1, VerifyTokenSuccess } from "@theholocron/cli";
9
+ import { NpmClient, NpmClientOptions, createNpmClient } from "@theholocron/npm-client";
10
+ /**
11
+ * Resolves the npm token from `--token`, `HOLOCRON_NPM_TOKEN`, `NPM_TOKEN` (what `npm` itself reads), the `credentials`
12
+ * providers' variables or the keyring entry `npm`, and throws an `AuthError` when none is set. It is the
13
+ * stage-only granular token `publish package --initial` stages with, not a token that can publish.
14
+ */
15
+ export declare const resolveToken: import("@theholocron/cli").TokenResolver;
16
+ /**
17
+ * Every token this plugin takes, for `holocron doctor tokens`, with the layers that hold it: the `agent` layer only.
18
+ * `page` is where a person makes it (npm has no API for it).
19
+ */
20
+ export declare const tokens: PluginToken[];
21
+ /** The options that can point the plugin at another registry host. */
22
+ interface BaseUrlOptions {
23
+ /** A custom registry host, for tests. The token is only sent to it when `unsafeBaseUrl` is also set. */
24
+ baseUrl?: string;
25
+ /** Allows `baseUrl`. Off by default so a changed config cannot send the npm token to another host. */
26
+ unsafeBaseUrl?: boolean;
27
+ }
28
+ /** The `registry` capability for npm. */
29
+ export declare class NpmRegistry implements Registry {
30
+ private readonly client;
31
+ /** The capability key, always `registry`. */
32
+ readonly key: "registry";
33
+ /** The provider name, `npm`. */
34
+ readonly providerName = "npm";
35
+ /** `client` is a thunk, so the token is resolved on first use and a plugin load never needs one. */
36
+ constructor(client: () => NpmClient);
37
+ whoami(): Promise<{
38
+ username: string;
39
+ }>;
40
+ latestVersion(name: string): Promise<string | undefined>;
41
+ }
42
+ /** What `verifyToken()` needs besides the token: the test overrides of the client. */
43
+ interface VerifyTokenOptions {
44
+ /** Override base URL for tests. */
45
+ baseUrl?: string;
46
+ /** Override `fetch` for tests. */
47
+ fetch?: typeof fetch;
48
+ }
49
+ /** Checks an npm token with `whoami`; the success subject is `user <name>`, the failure carries the error message. */
50
+ export declare function verifyToken(token: string, opts?: VerifyTokenOptions): Promise<VerifyTokenResult$1>;
51
+ /** Options for the npm plugin: token resolution and test overrides. */
52
+ export interface NpmPluginOptions extends ResolveTokenInput, BaseUrlOptions {
53
+ /** Override `fetch` for tests. */
54
+ fetch?: typeof fetch;
55
+ }
56
+ /** What the plugin's capability and probe share: the options and a lazily built client. */
57
+ export interface PluginContext {
58
+ /** The options the plugin was created with. */
59
+ options: NpmPluginOptions;
60
+ /** The resolved token; throws an `AuthError` when none is set. */
61
+ token: () => string;
62
+ /** Builds the client on first call and reuses it; the token is resolved then, not at plugin load. */
63
+ client: () => NpmClient;
64
+ }
65
+ /** Creates the shared context for the given options; no token is resolved until one is asked for. */
66
+ export declare function createContext(options?: NpmPluginOptions): PluginContext;
67
+ /** Builds the `registry` capability from a plugin context. */
68
+ export declare function registry(ctx: PluginContext): Registry;
69
+ /** Creates the npm plugin: the `token` probe, `whoami` and the `registry` capability. */
70
+ export declare function createPlugin(options?: NpmPluginOptions): {
71
+ name: string;
72
+ probes: {
73
+ token: import("@theholocron/cli").Probe;
74
+ };
75
+ whoami: import("@theholocron/cli").ProviderAction;
76
+ capabilities: {
77
+ registry: () => Registry;
78
+ };
79
+ };
80
+ /**
81
+ * One-line hint printed by `holocron new token npm` when no token is supplied or the supplied token is rejected.
82
+ */
83
+ export declare const AUTH_HINT: string;
84
+ export { AuthError, type NpmClient, type NpmClientOptions, type ResolveTokenInput, type VerifyTokenFailure, type VerifyTokenResult, type VerifyTokenSuccess, createNpmClient };
package/dist/index.mjs ADDED
@@ -0,0 +1,243 @@
1
+ import { AuthError, ProviderApiError, createResolveToken, tokenExpectations, tokenRotationRows, whoamiAction } from "@theholocron/cli";
2
+ import { errorMessage } from "@theholocron/misc-utils";
3
+ import { createNpmClient } from "@theholocron/npm-client";
4
+ // src/probes/token/expectations.ts
5
+ /**
6
+ * Where the stage-only npm token (`HOLOCRON_NPM_TOKEN`, #2777) is held: the `agent` layer only, so a person's `local`,
7
+ * `dev`, `ci` and `prod` Environments hold none and a layer that does without being declared fails the check. It is a
8
+ * short-lived token (a stage-only granular token with a short expiry), so it is due after 30 days and flagged 7 days
9
+ * before an expiry. npm reports neither the token's age nor its permissions to the token, so no permissions are
10
+ * declared and the probe says it cannot judge them. This is the plugin's `tokens` declaration, read through
11
+ * `tokenExpectations()`; change it and the npm row in `docs/src/content/docs/tokens.mdx` together.
12
+ */
13
+ const EXPECTED = {
14
+ layers: ["agent"],
15
+ rotation: {
16
+ maxAgeDays: 30,
17
+ warnDays: 7
18
+ }
19
+ };
20
+ // src/auth.ts
21
+ /**
22
+ * Resolves the npm token from `--token`, `HOLOCRON_NPM_TOKEN`, `NPM_TOKEN` (what `npm` itself reads), the `credentials`
23
+ * providers' variables or the keyring entry `npm`, and throws an `AuthError` when none is set. It is the
24
+ * stage-only granular token `publish package --initial` stages with, not a token that can publish.
25
+ */
26
+ const resolveToken = createResolveToken({
27
+ envName: "HOLOCRON_NPM_TOKEN",
28
+ vendorEnvName: "NPM_TOKEN",
29
+ keyringService: "npm",
30
+ errorMessage: "no npm token found. Pass --token <TOKEN>, set HOLOCRON_NPM_TOKEN / NPM_TOKEN, or run: holocron new token npm <TOKEN>"
31
+ });
32
+ /**
33
+ * Every token this plugin takes, for `holocron doctor tokens`, with the layers that hold it: the `agent` layer only.
34
+ * `page` is where a person makes it (npm has no API for it).
35
+ */
36
+ const tokens = [{
37
+ ...resolveToken.token,
38
+ page: "https://www.npmjs.com/settings/~/tokens",
39
+ expect: EXPECTED
40
+ }];
41
+ // src/base-url.ts
42
+ /**
43
+ * The registry host the token goes to: npm's own (`undefined`, which the client defaults) unless `baseUrl` is set, and
44
+ * a `baseUrl` is refused without `unsafeBaseUrl`. A repository's config reaches the plugin as options, so without the
45
+ * second switch a config change could send the Bearer token to any host, plain HTTP included.
46
+ *
47
+ * @throws When `baseUrl` is set and `unsafeBaseUrl` is not.
48
+ */
49
+ function registryBaseUrl(options) {
50
+ if (!options.baseUrl) return void 0;
51
+ if (!options.unsafeBaseUrl) throw new Error("refusing to send the npm token to a custom `baseUrl`; remove it, or set `unsafeBaseUrl` for a test server");
52
+ return options.baseUrl;
53
+ }
54
+ // src/capabilities/registry.ts
55
+ /** The `registry` capability for npm. */
56
+ var NpmRegistry = class {
57
+ client;
58
+ /** The capability key, always `registry`. */
59
+ key = "registry";
60
+ /** The provider name, `npm`. */
61
+ providerName = "npm";
62
+ /** `client` is a thunk, so the token is resolved on first use and a plugin load never needs one. */
63
+ constructor(client) {
64
+ this.client = client;
65
+ }
66
+ async whoami() {
67
+ const { username } = await this.client().whoami();
68
+ return { username };
69
+ }
70
+ latestVersion(name) {
71
+ return this.client().packages.latestVersion(name);
72
+ }
73
+ };
74
+ // src/probes/token/parse-token-args.ts
75
+ /** Reads `--layer <word>` and `--token-name <VAR>` (either spelling) from the words after the probe name. */
76
+ function parseTokenArgs(args) {
77
+ let layer;
78
+ let tokenName;
79
+ for (let i = 0; i < args.length; i++) {
80
+ const arg = args[i];
81
+ if (arg === "--layer") layer = args[++i];
82
+ else if (arg.startsWith("--layer=")) layer = arg.slice(8);
83
+ else if (arg === "--token-name") tokenName = args[++i];
84
+ else if (arg.startsWith("--token-name=")) tokenName = arg.slice(13);
85
+ }
86
+ return {
87
+ ...layer ? { layer } : {},
88
+ ...tokenName ? { tokenName } : {}
89
+ };
90
+ }
91
+ // src/probes/token/token.ts
92
+ /**
93
+ * The `token` probe for npm. It asks the registry who the token is (`GET /-/whoami`), which proves the token is present
94
+ * and still accepted, and reports what npm cannot tell it:
95
+ *
96
+ * - **token**: a token that cannot be resolved is a `fail` row, and the rows that need no token still follow.
97
+ * - **valid**: `whoami` answered with a user name (a refused token, 401 or 403, is a `fail`, and so is the network).
98
+ * - **rotation**: npm does not expose a token's creation date or expiry to the token (listing tokens needs a logged-in
99
+ * session), so `tokenRotationRows(undefined, ...)` says so as an `info` row, never a pass. The policy is the
100
+ * declaration's (30 days, warning 7 days before an expiry).
101
+ * - **permissions**: not readable from npm either, so the probe cannot tell a stage-only token from one that can publish
102
+ * directly, and says so.
103
+ *
104
+ * With `--layer` (the CLI passes it when `--environment` named a layer) it also says whether that layer is one that
105
+ * should hold the token, against the plugin's `tokens` declaration. It never prints the token.
106
+ */
107
+ function tokenProbe(ctx) {
108
+ return {
109
+ summary: "Check the stage-only npm token: that it is set and that the registry accepts it (whoami). npm exposes neither a token's age nor its permissions, so it cannot tell a stage-only token from one that can publish. Read-only.",
110
+ writes: false,
111
+ acceptsToken: true,
112
+ async run(probe) {
113
+ let resolved = false;
114
+ try {
115
+ probe.secret(ctx.token());
116
+ resolved = true;
117
+ probe.say({
118
+ status: "ok",
119
+ label: "token",
120
+ detail: "set"
121
+ });
122
+ } catch (err) {
123
+ probe.say({
124
+ status: "fail",
125
+ label: "token",
126
+ detail: errorMessage(err)
127
+ });
128
+ }
129
+ const { layer, tokenName } = parseTokenArgs(probe.args ?? []);
130
+ const filter = {
131
+ ...layer ? { layer } : {},
132
+ ...tokenName ? { envName: tokenName } : {}
133
+ };
134
+ const declared = tokenExpectations(tokens, filter);
135
+ if (layer) probe.say(declared.length > 0 ? {
136
+ status: "ok",
137
+ label: layer,
138
+ detail: "a layer that should hold this token"
139
+ } : {
140
+ status: "info",
141
+ label: layer,
142
+ detail: "not a layer this token is declared for, so it is only described"
143
+ });
144
+ if (!resolved) probe.say({
145
+ status: "info",
146
+ label: "valid",
147
+ detail: "not checked: there is no token to ask the registry about"
148
+ });
149
+ else try {
150
+ const { username } = await ctx.registry().whoami();
151
+ probe.say({
152
+ status: "ok",
153
+ label: "valid",
154
+ detail: `the registry accepts it: user ${username}`
155
+ });
156
+ } catch (err) {
157
+ const status = err instanceof ProviderApiError ? err.status : void 0;
158
+ probe.say({
159
+ status: "fail",
160
+ label: "valid",
161
+ detail: status === 401 || status === 403 ? `the registry refused the token (${status}): it is expired, revoked or wrong` : errorMessage(err)
162
+ });
163
+ }
164
+ probe.say({
165
+ status: "heading",
166
+ label: "Rotation"
167
+ });
168
+ const { rotation } = declared[0] ?? tokenExpectations(tokens)[0];
169
+ for (const row of tokenRotationRows(void 0, rotation, (ctx.now ?? (() => /* @__PURE__ */ new Date()))())) probe.say(row);
170
+ probe.say({
171
+ status: "info",
172
+ label: "permissions",
173
+ detail: "npm does not report a token's permissions, so they are not checked: this cannot tell a stage-only token from one that can publish directly"
174
+ });
175
+ }
176
+ };
177
+ }
178
+ // src/verify-token.ts
179
+ /** Checks an npm token with `whoami`; the success subject is `user <name>`, the failure carries the error message. */
180
+ async function verifyToken(token, opts = {}) {
181
+ try {
182
+ const { username } = await createNpmClient({
183
+ token,
184
+ baseUrl: opts.baseUrl,
185
+ fetch: opts.fetch
186
+ }).whoami();
187
+ return {
188
+ ok: true,
189
+ subject: `user ${username}`
190
+ };
191
+ } catch (err) {
192
+ return {
193
+ ok: false,
194
+ message: errorMessage(err)
195
+ };
196
+ }
197
+ }
198
+ // src/index.ts
199
+ /** Creates the shared context for the given options; no token is resolved until one is asked for. */
200
+ function createContext(options = {}) {
201
+ let client;
202
+ const token = () => resolveToken(options);
203
+ return {
204
+ options,
205
+ token,
206
+ client: () => client ??= createNpmClient({
207
+ token: token(),
208
+ baseUrl: registryBaseUrl(options),
209
+ fetch: options.fetch
210
+ })
211
+ };
212
+ }
213
+ /** Builds the `registry` capability from a plugin context. */
214
+ function registry(ctx) {
215
+ return new NpmRegistry(ctx.client);
216
+ }
217
+ /** Creates the npm plugin: the `token` probe, `whoami` and the `registry` capability. */
218
+ function createPlugin(options = {}) {
219
+ const ctx = createContext(options);
220
+ return {
221
+ name: "@theholocron/holocron-plugin-npm",
222
+ probes: { token: tokenProbe({
223
+ token: ctx.token,
224
+ registry: () => registry(ctx)
225
+ }) },
226
+ whoami: whoamiAction({
227
+ provider: "npm",
228
+ token: ctx.token,
229
+ verify: (token) => verifyToken(token, {
230
+ baseUrl: registryBaseUrl(options),
231
+ fetch: options.fetch
232
+ }),
233
+ tokens,
234
+ proveWith: "holocron probe npm token --environment agent"
235
+ }),
236
+ capabilities: { registry: () => registry(ctx) }
237
+ };
238
+ }
239
+ /**
240
+ * One-line hint printed by `holocron new token npm` when no token is supplied or the supplied token is rejected.
241
+ */
242
+ const AUTH_HINT = "make a stage-only granular access token at https://www.npmjs.com/settings/~/tokens (Read and write, the @theholocron scope, 2FA bypass off, a short expiry), then run: holocron new token npm <TOKEN>";
243
+ export { AUTH_HINT, AuthError, NpmRegistry, createContext, createNpmClient, createPlugin, registry, resolveToken, tokens, verifyToken };
package/package.json CHANGED
@@ -1,6 +1,69 @@
1
1
  {
2
2
  "name": "@theholocron/holocron-plugin-npm",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "5.0.0-alpha.586",
4
+ "description": "Holocron plugin for the npm registry. Implements the registry capability: who a token is and a package's latest version, and checks the stage-only npm token.",
5
+ "keywords": [
6
+ "holocron",
7
+ "npm",
8
+ "plugin",
9
+ "registry"
10
+ ],
11
+ "homepage": "https://docs.theholocron.dev/holocron/plugins/npm/",
12
+ "bugs": "https://github.com/theholocron/holocron/issues",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/theholocron/holocron.git",
16
+ "directory": "plugins/holocron-plugin-npm"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Newton Koumantzelis",
20
+ "sideEffects": false,
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.mts",
25
+ "import": "./dist/index.mjs",
26
+ "default": "./dist/index.mjs"
27
+ }
28
+ },
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "devDependencies": {
33
+ "@types/node": "^26",
34
+ "@vitest/coverage-v8": "^5.1.1",
35
+ "@vitest/eslint-plugin": "^1.6.28",
36
+ "eslint": "^10.15.0",
37
+ "eslint-plugin-n": "^18.5.0",
38
+ "globals": "^17.14.0",
39
+ "tsdown": "^0.23.7",
40
+ "typescript": "^6.0.3",
41
+ "vitest": "^5.1.1",
42
+ "@theholocron/http-client": "^1.58.0-alpha.71",
43
+ "@theholocron/cli": "5.0.0-alpha.588",
44
+ "@theholocron/misc-utils": "^1.17.0-alpha.9",
45
+ "@theholocron/npm-client": "^1.58.0-alpha.70",
46
+ "@theholocron/tsconfig": "^8.9.0-alpha.24",
47
+ "@theholocron/tsdown-config": "^8.9.0-alpha.24",
48
+ "@theholocron/eslint-config": "^8.9.0-alpha.24",
49
+ "@theholocron/vitest-config": "^8.9.0-alpha.24"
50
+ },
51
+ "peerDependencies": {
52
+ "@theholocron/misc-utils": "^1.16.0",
53
+ "@theholocron/npm-client": "^1.58.0-alpha.70",
54
+ "@theholocron/cli": "5.0.0-alpha.588"
55
+ },
56
+ "engines": {
57
+ "node": ">=22"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public"
61
+ },
62
+ "scripts": {
63
+ "build": "tsdown",
64
+ "lint:code": "eslint .",
65
+ "test:unit": "vitest run --coverage",
66
+ "test:watch": "vitest",
67
+ "typecheck": "tsc --noEmit"
68
+ }
6
69
  }