Skip to content

Commit 82c5113

Browse files
fix(cache): restore slashes in package names (#465)
1 parent 809f4e0 commit 82c5113

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/cache.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const kConfigCache = "___config";
1414
const kPayloadsCache = "___payloads";
1515
const kPayloadsPath = path.join(os.homedir(), ".nsecure", "payloads");
1616
const kMaxPayloads = 3;
17+
const kSlashReplaceToken = "______";
1718

1819
export const CACHE_PATH = path.join(os.tmpdir(), "nsecure-cli");
1920
export const DEFAULT_PAYLOAD_PATH = path.join(process.cwd(), "nsecure-result.json");
@@ -37,12 +38,16 @@ class _AppCache {
3738
}
3839

3940
updatePayload(pkg, payload) {
40-
fs.writeFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), JSON.stringify(payload));
41+
if (pkg.includes(kSlashReplaceToken)) {
42+
throw new Error(`Invalid package name: ${pkg}`);
43+
}
44+
45+
fs.writeFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", kSlashReplaceToken)), JSON.stringify(payload));
4146
}
4247

4348
getPayload(pkg) {
4449
try {
45-
return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll("/", "-")), "utf-8"));
50+
return JSON.parse(fs.readFileSync(path.join(kPayloadsPath, pkg.replaceAll(kSlashReplaceToken, "/")), "utf-8"));
4651
}
4752
catch (err) {
4853
logger.error(`[cache|get](pkg: ${pkg}|cache: not found)`);
@@ -52,7 +57,9 @@ class _AppCache {
5257
}
5358

5459
availablePayloads() {
55-
return fs.readdirSync(kPayloadsPath);
60+
return fs
61+
.readdirSync(kPayloadsPath)
62+
.map((filename) => filename.replaceAll(kSlashReplaceToken, "/"));
5663
}
5764

5865
getPayloadOrNull(pkg) {

test/cache.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ describe("appCache", () => {
4444

4545
appCache.updatePayload("foo/bar", { foo: "bar" });
4646

47-
assert.equal(writePath, path.join(kPayloadsPath, "foo-bar"));
47+
assert.equal(writePath, path.join(kPayloadsPath, "foo______bar"));
4848
assert.equal(writeValue, JSON.stringify({ foo: "bar" }));
4949
});
5050

51+
it("should throw given a package name that contains the slash replace token", () => {
52+
assert.throws(() => appCache.updatePayload("foo______bar", { foo: "bar" }), {
53+
message: "Invalid package name: foo______bar"
54+
});
55+
});
56+
5157
it("getPayload should return the payload", (t) => {
5258
t.mock.method(fs, "readFileSync", () => JSON.stringify({ foo: "bar" }));
5359

0 commit comments

Comments
 (0)