@vltpkg/keychain 0.0.0-3 → 0.0.0-30
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/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +18 -24
- package/dist/esm/index.js.map +1 -1
- package/package.json +10 -13
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAOA,qBAAa,QAAQ,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM;;gBAOjC,WAAW,EAAE,MAAM;IAK/B,IAAI,IAAI,WAEP;IACD,IAAI,KAAK,YAER;IAEK,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAI9C,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,SAAS;IAI7B,IAAI;IAmBJ,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIxC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAoB7B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAKzB,MAAM,CAAC,GAAG,EAAE,MAAM;IAgBZ,IAAI;CAKX"}
|
package/dist/esm/index.js
CHANGED
|
@@ -2,15 +2,8 @@
|
|
|
2
2
|
// only the keychain file. Any other source of auth data should be
|
|
3
3
|
// handled before or instead of this utility.
|
|
4
4
|
import { XDG } from '@vltpkg/xdg';
|
|
5
|
-
import { mkdir, readFile, rename, writeFile } from 'fs/promises';
|
|
6
|
-
import { dirname } from 'path';
|
|
7
|
-
const writeFileAtomic = async (path, data) => {
|
|
8
|
-
const p = path + String(Math.random());
|
|
9
|
-
await writeFile(p, data, { mode: 0o600 });
|
|
10
|
-
await rename(p, path);
|
|
11
|
-
};
|
|
12
|
-
const { hasOwnProperty } = Object.prototype;
|
|
13
|
-
const hasOwn = (obj, key) => !!obj && typeof obj === 'object' && hasOwnProperty.call(obj, key);
|
|
5
|
+
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { dirname } from 'node:path';
|
|
14
7
|
export class Keychain {
|
|
15
8
|
#data;
|
|
16
9
|
#didExitSave = false;
|
|
@@ -39,28 +32,22 @@ export class Keychain {
|
|
|
39
32
|
async #load() {
|
|
40
33
|
if (this.#data)
|
|
41
34
|
return this;
|
|
42
|
-
let ok = false;
|
|
43
35
|
try {
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
45
36
|
this.#data = JSON.parse(await readFile(this.#file, 'utf8'));
|
|
46
|
-
ok = !!this.#data;
|
|
47
37
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this.#data = {};
|
|
54
|
-
}
|
|
55
|
-
// eslint-disable-next-line no-unsafe-finally
|
|
56
|
-
return this;
|
|
38
|
+
catch { }
|
|
39
|
+
if (!this.#data) {
|
|
40
|
+
// just write the file if it failed in any way.
|
|
41
|
+
this.#data = {};
|
|
42
|
+
await this.#writeFile().catch(() => { });
|
|
57
43
|
}
|
|
44
|
+
return this;
|
|
58
45
|
}
|
|
59
46
|
async has(key) {
|
|
60
47
|
return (await this.#load()).hasSync(key);
|
|
61
48
|
}
|
|
62
49
|
hasSync(key) {
|
|
63
|
-
return !!hasOwn(this.#data, key);
|
|
50
|
+
return !!this.#data && Object.hasOwn(this.#data, key);
|
|
64
51
|
}
|
|
65
52
|
async #mkdir() {
|
|
66
53
|
await mkdir(dirname(this.#file), {
|
|
@@ -68,6 +55,14 @@ export class Keychain {
|
|
|
68
55
|
mode: 0o700,
|
|
69
56
|
});
|
|
70
57
|
}
|
|
58
|
+
async #writeFile() {
|
|
59
|
+
await this.#mkdir();
|
|
60
|
+
const tmp = this.#file + String(Math.random());
|
|
61
|
+
await writeFile(tmp, JSON.stringify(this.#data) + '\n', {
|
|
62
|
+
mode: 0o600,
|
|
63
|
+
});
|
|
64
|
+
await rename(tmp, this.#file);
|
|
65
|
+
}
|
|
71
66
|
set(key, value) {
|
|
72
67
|
;
|
|
73
68
|
(this.#data ??= {})[key] = value;
|
|
@@ -92,8 +87,7 @@ export class Keychain {
|
|
|
92
87
|
if (!this.#dirty || !this.#data)
|
|
93
88
|
return;
|
|
94
89
|
this.#dirty = false;
|
|
95
|
-
await this.#
|
|
96
|
-
await writeFileAtomic(this.#file, JSON.stringify(this.#data) + '\n');
|
|
90
|
+
await this.#writeFile();
|
|
97
91
|
}
|
|
98
92
|
}
|
|
99
93
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,kEAAkE;AAClE,6CAA6C;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,kEAAkE;AAClE,6CAA6C;AAC7C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACjC,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAEnC,MAAM,OAAO,QAAQ;IACnB,KAAK,CAAoB;IACzB,YAAY,GAAG,KAAK,CAAA;IACpB,MAAM,GAAG,KAAK,CAAA;IACd,KAAK,CAAQ;IACb,IAAI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AAO,CAAC,IAAI,CAAC,YAAY,EAAE,GAAG,EAAE;gBAC9B,KAAK,IAAI,CAAC,IAAI,EAAE,CAAA;YAClB,CAAC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAM;QACvC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAA;QACnB,MAAM,IAAI,CAAC,UAAU,EAAE,CAAA;IACzB,CAAC;CACF","sourcesContent":["// Note: this class does NOT handle reading from the environment,\n// only the keychain file. Any other source of auth data should be\n// handled before or instead of this utility.\nimport { XDG } from '@vltpkg/xdg'\nimport { mkdir, readFile, rename, writeFile } from 'node:fs/promises'\nimport { dirname } from 'node:path'\n\nexport class Keychain<V extends string = string> {\n #data?: Record<string, V>\n #didExitSave = false\n #dirty = false\n #file: string\n #xdg: XDG\n\n constructor(application: string) {\n this.#xdg = new XDG(application)\n this.#file = this.#xdg.data('keychain.json')\n }\n\n get file() {\n return this.#file\n }\n get dirty() {\n return this.#dirty\n }\n\n async get(key: string): Promise<V | undefined> {\n return (await this.#load()).getSync(key)\n }\n\n getSync(key: string): V | undefined {\n return this.#data?.[key]\n }\n\n async load() {\n await this.#load()\n }\n\n async #load(): Promise<this> {\n if (this.#data) return this\n try {\n this.#data = JSON.parse(\n await readFile(this.#file, 'utf8'),\n ) as Record<string, V>\n } catch {}\n if (!this.#data) {\n // just write the file if it failed in any way.\n this.#data = {}\n await this.#writeFile().catch(() => {})\n }\n return this\n }\n\n async has(key: string): Promise<boolean> {\n return (await this.#load()).hasSync(key)\n }\n\n hasSync(key: string): boolean {\n return !!this.#data && Object.hasOwn(this.#data, key)\n }\n\n async #mkdir() {\n await mkdir(dirname(this.#file), {\n recursive: true,\n mode: 0o700,\n })\n }\n\n async #writeFile() {\n await this.#mkdir()\n const tmp = this.#file + String(Math.random())\n await writeFile(tmp, JSON.stringify(this.#data) + '\\n', {\n mode: 0o600,\n })\n await rename(tmp, this.#file)\n }\n\n set(key: string, value: V) {\n ;(this.#data ??= {})[key] = value\n this.#makeDirty()\n }\n\n delete(key: string) {\n if (!this.#data) return\n delete this.#data[key]\n this.#makeDirty()\n }\n\n #makeDirty() {\n this.#dirty = true\n if (!this.#didExitSave) {\n this.#didExitSave = true\n process.once('beforeExit', () => {\n void this.save()\n })\n }\n }\n\n async save() {\n if (!this.#dirty || !this.#data) return\n this.#dirty = false\n await this.#writeFile()\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vltpkg/keychain",
|
|
3
3
|
"description": "The filesystem keychain for `@vltpkg/registry-client`",
|
|
4
|
-
"version": "0.0.0-
|
|
4
|
+
"version": "0.0.0-30",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/vltpkg/vltpkg.git",
|
|
8
8
|
"directory": "src/keychain"
|
|
9
9
|
},
|
|
10
|
+
"author": "vlt technology inc. <support@vlt.sh> (http://vlt.sh)",
|
|
10
11
|
"tshy": {
|
|
11
12
|
"selfLink": false,
|
|
12
13
|
"liveDev": true,
|
|
@@ -19,23 +20,18 @@
|
|
|
19
20
|
}
|
|
20
21
|
},
|
|
21
22
|
"dependencies": {
|
|
22
|
-
"
|
|
23
|
-
"rimraf": "^6.0.1",
|
|
24
|
-
"@vltpkg/error-cause": "0.0.0-3",
|
|
25
|
-
"@vltpkg/xdg": "0.0.0-3",
|
|
26
|
-
"@vltpkg/types": "0.0.0-3"
|
|
23
|
+
"@vltpkg/xdg": "0.0.0-30"
|
|
27
24
|
},
|
|
28
25
|
"devDependencies": {
|
|
29
|
-
"@eslint/js": "^9.
|
|
30
|
-
"@types/
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"prettier": "^3.4.2",
|
|
26
|
+
"@eslint/js": "^9.34.0",
|
|
27
|
+
"@types/node": "^22.17.2",
|
|
28
|
+
"eslint": "^9.34.0",
|
|
29
|
+
"prettier": "^3.6.2",
|
|
34
30
|
"tap": "^21.1.0",
|
|
35
31
|
"tshy": "^3.0.2",
|
|
36
|
-
"typedoc": "0.27.
|
|
32
|
+
"typedoc": "~0.27.9",
|
|
37
33
|
"typescript": "5.7.3",
|
|
38
|
-
"typescript-eslint": "^8.
|
|
34
|
+
"typescript-eslint": "^8.40.0"
|
|
39
35
|
},
|
|
40
36
|
"license": "BSD-2-Clause-Patent",
|
|
41
37
|
"engines": {
|
|
@@ -67,6 +63,7 @@
|
|
|
67
63
|
"snap": "tap",
|
|
68
64
|
"test": "tap",
|
|
69
65
|
"posttest": "tsc --noEmit",
|
|
66
|
+
"tshy": "tshy",
|
|
70
67
|
"typecheck": "tsc --noEmit"
|
|
71
68
|
}
|
|
72
69
|
}
|