Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/huge-rats-stay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nodesecure/scanner": minor
---

feat(scanner): sync config with pacote when config is present
15 changes: 11 additions & 4 deletions workspaces/scanner/src/depWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,24 @@ export async function depWalker(

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

const tokenStore = new RegistryTokenStore(npmRcConfig, NPM_TOKEN.token);

const npmProjectConfig = tokenStore.getConfig(registry);

const pacoteProvider: PacoteProvider = {
async extract(spec, dest, opts): Promise<void> {
await statsCollector.track(
`pacote.extract ${spec}`,
"tarball-scan",
() => pacote.extract(spec, dest, opts)
() => pacote.extract(spec, dest, {
...opts,
...npmProjectConfig
})
);
}
};

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

await using tempDir = await TempDirectory.create();

Expand All @@ -164,10 +170,11 @@ export async function depWalker(
registry,
providers: {
pacote: {
manifest: (spec, opts) => statsCollector.track(`pacote.manifest ${spec}`, "tree-walk", () => pacote.manifest(spec, opts)),
manifest: (spec, opts) => statsCollector.track(`pacote.manifest ${spec}`, "tree-walk", () => pacote.manifest(spec,
{ ...opts, ...npmProjectConfig })),
packument: (spec, opts) => statsCollector.track(`pacote.packument ${spec}`,
"tree-walk",
() => pacote.packument(spec, opts))
() => pacote.packument(spec, { ...opts, ...npmProjectConfig }))
}
}
});
Expand Down
10 changes: 9 additions & 1 deletion workspaces/scanner/src/registry/RegistryTokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ export class RegistryTokenStore implements TokenStore {
return token;
}

getConfig(registry: string) {
return this.#config ? { [this.getKey(registry)]: this.get(registry) } : {};
}

private getTokenKey(registry: string) {
return `${registry.replace(/https:|http:/, "")}:_authToken`;
return `${this.getKey(registry)}:_authToken`;
}

private getKey(registry: string) {
return registry.replace(/https:|http:/, "");
}
}
47 changes: 33 additions & 14 deletions workspaces/scanner/test/RegistryTokenStore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,41 @@ always-auth=true
await tempDir.clear();
});

test("should store and retrieve tokens", async() => {
const store = new RegistryTokenStore(config, undefined);
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
assert.strictEqual(store.get("http://npm.nodescure.github.com/"), "private-token");
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
assert.strictEqual(store.get("unknown"), undefined);
});
describe("get", () => {
test("should store and retrieve tokens", () => {
const store = new RegistryTokenStore(config, undefined);
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
assert.strictEqual(store.get("http://npm.nodescure.github.com/"), "private-token");
assert.strictEqual(store.get("https://registry.npmjs.org/"), "public-token");
assert.strictEqual(store.get("unknown"), undefined);
});

test("should default to token from env when there is one", () => {
const store = new RegistryTokenStore(config, "token-from-env");
assert.strictEqual(store.get("unknown"), "token-from-env");
assert.strictEqual(store.get("unknown"), "token-from-env");
});

test("should default to token from env when there is one", () => {
const store = new RegistryTokenStore(config, "token-from-env");
assert.strictEqual(store.get("unknown"), "token-from-env");
assert.strictEqual(store.get("unknown"), "token-from-env");
test("should always default to token from env when there is no config", () => {
const store = new RegistryTokenStore(undefined, "token-from-env");
assert.strictEqual(store.get("https://registry.npmjs.org/"), "token-from-env");
});
});

test("should always default to token from env when there is no config", () => {
const store = new RegistryTokenStore(undefined, "token-from-env");
assert.strictEqual(store.get("https://registry.npmjs.org/"), "token-from-env");
describe("getConfig", () => {
test("should get no config", () => {
const store = new RegistryTokenStore(undefined, "token-from-env");
assert.deepEqual(store.getConfig("https://registry.npmjs.org/"), {});
});

test("should get the right config by registry", () => {
const store = new RegistryTokenStore(config, "token-from-env");
assert.deepEqual(store.getConfig("https://registry.npmjs.org/"), {
"//registry.npmjs.org/": "public-token"
});
assert.deepEqual(store.getConfig("http://npm.nodescure.github.com/"), {
"//npm.nodescure.github.com/": "private-token"
});
});
});
});