@vltpkg/keychain 0.0.0-2

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,15 @@
1
+ Copyright (c) vlt technology, Inc.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ Subject to the terms and conditions of this license, each copyright holder and contributor hereby grants to those receiving rights under this license a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except for failure to satisfy the conditions of this license) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer this software, where such license applies only to those patent claims, already acquired or hereafter acquired, licensable by such copyright holder or contributor that are necessarily infringed by:
8
+
9
+ (a) their Contribution(s) (the licensed copyrights of copyright holders and non-copyrightable additions of contributors, in source or binary form) alone; or
10
+ (b) combination of their Contribution(s) with the work of authorship to which such Contribution(s) was added by such copyright holder or contributor, if, at the time the Contribution is added, such addition causes such combination to be necessarily infringed. The patent license shall not apply to any other combinations which include the Contribution.
11
+ Except as expressly stated above, no rights or licenses from any copyright holder or contributor is granted under this license, whether expressly, by implication, estoppel or otherwise.
12
+
13
+ DISCLAIMER
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # @vltpkg/keychain
2
+
3
+ The filesystem keychain for `@vltpkg/registry-client`
4
+
5
+ **[Usage](#usage)**
6
+
7
+ ## Overview
8
+
9
+ This is a tool to store and retrieve private keys for use in the
10
+ `@vltpkg/registry-client`.
11
+
12
+ ## Usage
13
+
14
+ ```js
15
+ import { Keychain } from '@vltpkg/keychain'
16
+
17
+ // define a keychain with a given application scope
18
+ const kc = new Keychain('vlt/auth')
19
+
20
+ // fetch the auth for a given origin, for example.
21
+ // will load file on demand when first get() called.
22
+ const auth = await kc.get('https://registry.npmjs.org')
23
+
24
+ // set a value like this
25
+ kc.set('https://some-registry.com', 'Bearer newtoken')
26
+
27
+ // will attempt to save on process end if there are pending
28
+ // writes, but only if the file has not been modified since.
29
+ // you can also trigger a write explicitly.
30
+ await kc.save()
31
+ ```
@@ -0,0 +1,15 @@
1
+ export declare class Keychain<V extends string = string> {
2
+ #private;
3
+ constructor(application: string);
4
+ get file(): string;
5
+ get dirty(): boolean;
6
+ get(key: string): Promise<V | undefined>;
7
+ getSync(key: string): V | undefined;
8
+ load(): Promise<void>;
9
+ has(key: string): Promise<boolean>;
10
+ hasSync(key: string): boolean;
11
+ set(key: string, value: V): void;
12
+ delete(key: string): void;
13
+ save(): Promise<void>;
14
+ }
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAiBA,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;IAuBJ,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAIxC,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO;IAW7B,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAKzB,MAAM,CAAC,GAAG,EAAE,MAAM;IAgBZ,IAAI;CASX"}
@@ -0,0 +1,99 @@
1
+ // Note: this class does NOT handle reading from the environment,
2
+ // only the keychain file. Any other source of auth data should be
3
+ // handled before or instead of this utility.
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);
14
+ export class Keychain {
15
+ #data;
16
+ #didExitSave = false;
17
+ #dirty = false;
18
+ #file;
19
+ #xdg;
20
+ constructor(application) {
21
+ this.#xdg = new XDG(application);
22
+ this.#file = this.#xdg.data('keychain.json');
23
+ }
24
+ get file() {
25
+ return this.#file;
26
+ }
27
+ get dirty() {
28
+ return this.#dirty;
29
+ }
30
+ async get(key) {
31
+ return (await this.#load()).getSync(key);
32
+ }
33
+ getSync(key) {
34
+ return this.#data?.[key];
35
+ }
36
+ async load() {
37
+ await this.#load();
38
+ }
39
+ async #load() {
40
+ if (this.#data)
41
+ return this;
42
+ let ok = false;
43
+ try {
44
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
45
+ this.#data = JSON.parse(await readFile(this.#file, 'utf8'));
46
+ ok = !!this.#data;
47
+ }
48
+ finally {
49
+ if (!ok) {
50
+ // just write the file if it failed in any way.
51
+ await this.#mkdir();
52
+ await writeFileAtomic(this.#file, '{}\n');
53
+ this.#data = {};
54
+ }
55
+ // eslint-disable-next-line no-unsafe-finally
56
+ return this;
57
+ }
58
+ }
59
+ async has(key) {
60
+ return (await this.#load()).hasSync(key);
61
+ }
62
+ hasSync(key) {
63
+ return !!hasOwn(this.#data, key);
64
+ }
65
+ async #mkdir() {
66
+ await mkdir(dirname(this.#file), {
67
+ recursive: true,
68
+ mode: 0o700,
69
+ });
70
+ }
71
+ set(key, value) {
72
+ ;
73
+ (this.#data ??= {})[key] = value;
74
+ this.#makeDirty();
75
+ }
76
+ delete(key) {
77
+ if (!this.#data)
78
+ return;
79
+ delete this.#data[key];
80
+ this.#makeDirty();
81
+ }
82
+ #makeDirty() {
83
+ this.#dirty = true;
84
+ if (!this.#didExitSave) {
85
+ this.#didExitSave = true;
86
+ process.once('beforeExit', () => {
87
+ void this.save();
88
+ });
89
+ }
90
+ }
91
+ async save() {
92
+ if (!this.#dirty || !this.#data)
93
+ return;
94
+ this.#dirty = false;
95
+ await this.#mkdir();
96
+ await writeFileAtomic(this.#file, JSON.stringify(this.#data) + '\n');
97
+ }
98
+ }
99
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,aAAa,CAAA;AAChE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAE9B,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G,KAAK,CAAA;QACjC,IAAI,CAAC,UAAU,EAAE,CAAA;IACnB,CAAC;IAED,MAAM,CAAC,GAAW;QAChB,IAAI,CAAC,IAAI,CAAC,KAAK;YAAE,OAAM;QACvB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACtB,IAAI,CAAC,UAAU,EAAE,CAAA;IACnB,CAAC;IAED,UAAU;QACR,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;QAClB,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,IAAI,CAAC,YAAY,GAAG,IAAI,CAAA;YACxB,OAAO,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,MAAM,EAAE,CAAA;QACnB,MAAM,eAAe,CACnB,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,CAClC,CAAA;IACH,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 'fs/promises'\nimport { dirname } from 'path'\n\nconst writeFileAtomic = async (path: string, data: string) => {\n const p = path + String(Math.random())\n await writeFile(p, data, { mode: 0o600 })\n await rename(p, path)\n}\n\nconst { hasOwnProperty } = Object.prototype\nconst hasOwn = (obj: unknown, key: string) =>\n !!obj && typeof obj === 'object' && hasOwnProperty.call(obj, key)\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 let ok = false\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n this.#data = JSON.parse(await readFile(this.#file, 'utf8'))\n ok = !!this.#data\n } finally {\n if (!ok) {\n // just write the file if it failed in any way.\n await this.#mkdir()\n await writeFileAtomic(this.#file, '{}\\n')\n this.#data = {}\n }\n // eslint-disable-next-line no-unsafe-finally\n return this\n }\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 !!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 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.#mkdir()\n await writeFileAtomic(\n this.#file,\n JSON.stringify(this.#data) + '\\n',\n )\n }\n}\n"]}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@vltpkg/keychain",
3
+ "description": "The filesystem keychain for `@vltpkg/registry-client`",
4
+ "version": "0.0.0-2",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/vltpkg/vltpkg.git",
8
+ "directory": "src/keychain"
9
+ },
10
+ "tshy": {
11
+ "selfLink": false,
12
+ "liveDev": true,
13
+ "dialects": [
14
+ "esm"
15
+ ],
16
+ "exports": {
17
+ "./package.json": "./package.json",
18
+ ".": "./src/index.ts"
19
+ }
20
+ },
21
+ "dependencies": {
22
+ "lru-cache": "^11.0.2",
23
+ "rimraf": "^6.0.1",
24
+ "@vltpkg/xdg": "0.0.0-2",
25
+ "@vltpkg/error-cause": "0.0.0-2",
26
+ "@vltpkg/types": "0.0.0-2"
27
+ },
28
+ "devDependencies": {
29
+ "@eslint/js": "^9.20.0",
30
+ "@types/eslint__js": "^8.42.3",
31
+ "@types/node": "^22.13.1",
32
+ "eslint": "^9.20.0",
33
+ "prettier": "^3.4.2",
34
+ "tap": "^21.1.0",
35
+ "tshy": "^3.0.2",
36
+ "typedoc": "0.27.6",
37
+ "typescript": "5.7.3",
38
+ "typescript-eslint": "^8.23.0"
39
+ },
40
+ "license": "BSD-2-Clause-Patent",
41
+ "engines": {
42
+ "node": ">=22"
43
+ },
44
+ "tap": {
45
+ "extends": "../../tap-config.yaml"
46
+ },
47
+ "prettier": "../../.prettierrc.js",
48
+ "module": "./dist/esm/index.js",
49
+ "type": "module",
50
+ "exports": {
51
+ "./package.json": "./package.json",
52
+ ".": {
53
+ "import": {
54
+ "types": "./dist/esm/index.d.ts",
55
+ "default": "./dist/esm/index.js"
56
+ }
57
+ }
58
+ },
59
+ "files": [
60
+ "dist"
61
+ ],
62
+ "scripts": {
63
+ "format": "prettier --write . --log-level warn --ignore-path ../../.prettierignore --cache",
64
+ "format:check": "prettier --check . --ignore-path ../../.prettierignore --cache",
65
+ "lint": "eslint . --fix",
66
+ "lint:check": "eslint .",
67
+ "snap": "tap",
68
+ "test": "tap",
69
+ "posttest": "tsc --noEmit",
70
+ "typecheck": "tsc --noEmit"
71
+ }
72
+ }