Skip to content

Commit 2c4c08c

Browse files
committed
feat(scanner): sync config with pacote when config is present
1 parent fbc6023 commit 2c4c08c

4 files changed

Lines changed: 58 additions & 19 deletions

File tree

.changeset/huge-rats-stay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nodesecure/scanner": minor
3+
---
4+
5+
feat(scanner): sync config with pacote when config is present

workspaces/scanner/src/depWalker.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,24 @@ export async function depWalker(
128128

129129
const collectables = kCollectableTypes.map((type) => new DefaultCollectableSet<Metadata>(type));
130130

131+
const tokenStore = new RegistryTokenStore(npmRcConfig, NPM_TOKEN.token);
132+
133+
const npmProjectConfig = tokenStore.getConfig(registry);
134+
131135
const pacoteProvider: PacoteProvider = {
132136
async extract(spec, dest, opts): Promise<void> {
133137
await statsCollector.track(
134138
`pacote.extract ${spec}`,
135139
"tarball-scan",
136-
() => pacote.extract(spec, dest, opts)
140+
() => pacote.extract(spec, dest, {
141+
...opts,
142+
...npmProjectConfig
143+
})
137144
);
138145
}
139146
};
140147

141148
const isRemoteScanning = typeof location === "undefined";
142-
const tokenStore = new RegistryTokenStore(npmRcConfig, NPM_TOKEN.token);
143149

144150
await using tempDir = await TempDirectory.create();
145151

@@ -164,10 +170,11 @@ export async function depWalker(
164170
registry,
165171
providers: {
166172
pacote: {
167-
manifest: (spec, opts) => statsCollector.track(`pacote.manifest ${spec}`, "tree-walk", () => pacote.manifest(spec, opts)),
173+
manifest: (spec, opts) => statsCollector.track(`pacote.manifest ${spec}`, "tree-walk", () => pacote.manifest(spec,
174+
{ ...opts, ...npmProjectConfig })),
168175
packument: (spec, opts) => statsCollector.track(`pacote.packument ${spec}`,
169176
"tree-walk",
170-
() => pacote.packument(spec, opts))
177+
() => pacote.packument(spec, { ...opts, ...npmProjectConfig }))
171178
}
172179
}
173180
});

workspaces/scanner/src/registry/RegistryTokenStore.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ export class RegistryTokenStore implements TokenStore {
2626
return token;
2727
}
2828

29+
getConfig(registry: string) {
30+
return this.#config ? { [this.getKey(registry)]: this.get(registry) } : {};
31+
}
32+
2933
private getTokenKey(registry: string) {
30-
return `${registry.replace(/https:|http:/, "")}:_authToken`;
34+
return `${this.getKey(registry)}:_authToken`;
35+
}
36+
37+
private getKey(registry: string) {
38+
return registry.replace(/https:|http:/, "");
3139
}
3240
}

workspaces/scanner/test/RegistryTokenStore.spec.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,41 @@ always-auth=true
4848
await tempDir.clear();
4949
});
5050

51-
test("should store and retrieve tokens", async() => {
52-
const store = new RegistryTokenStore(config, undefined);
53-
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
54-
assert.strictEqual(store.get("http://npm.nodescure.github.com/"), "private-token");
55-
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
56-
assert.strictEqual(store.get("unknown"), undefined);
57-
});
51+
describe("get", () => {
52+
test("should store and retrieve tokens", () => {
53+
const store = new RegistryTokenStore(config, undefined);
54+
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
55+
assert.strictEqual(store.get("http://npm.nodescure.github.com/"), "private-token");
56+
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
57+
assert.strictEqual(store.get("unknown"), undefined);
58+
});
59+
60+
test("should default to token from env when there is one", () => {
61+
const store = new RegistryTokenStore(config, "token-from-env");
62+
assert.strictEqual(store.get("unknown"), "token-from-env");
63+
assert.strictEqual(store.get("unknown"), "token-from-env");
64+
});
5865

59-
test("should default to token from env when there is one", () => {
60-
const store = new RegistryTokenStore(config, "token-from-env");
61-
assert.strictEqual(store.get("unknown"), "token-from-env");
62-
assert.strictEqual(store.get("unknown"), "token-from-env");
66+
test("should always default to token from env when there is no config", () => {
67+
const store = new RegistryTokenStore(undefined, "token-from-env");
68+
assert.strictEqual(store.get("https://registry.npmjs.org/"), "token-from-env");
69+
});
6370
});
6471

65-
test("should always default to token from env when there is no config", () => {
66-
const store = new RegistryTokenStore(undefined, "token-from-env");
67-
assert.strictEqual(store.get("https://registry.npmjs.org/"), "token-from-env");
72+
describe("getConfig", () => {
73+
test("should get no config", () => {
74+
const store = new RegistryTokenStore(undefined, "token-from-env");
75+
assert.deepEqual(store.getConfig("https://registry.npmjs.org/"), {});
76+
});
77+
78+
test("should get the right config by registry", () => {
79+
const store = new RegistryTokenStore(config, "token-from-env");
80+
assert.deepEqual(store.getConfig("https://registry.npmjs.org/"), {
81+
"//registry.npmjs.org/": "public-token"
82+
});
83+
assert.deepEqual(store.getConfig("http://npm.nodescure.github.com/"), {
84+
"//npm.nodescure.github.com/": "private-token"
85+
});
86+
});
6887
});
6988
});

0 commit comments

Comments
 (0)