@hardkas/sessions 0.4.0-alpha
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 +21 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.js +105 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Javier Rodriguez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
interface HardkasSession {
|
|
2
|
+
readonly schema: "hardkas.session.v1";
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly l1: {
|
|
5
|
+
readonly wallet: string;
|
|
6
|
+
readonly address?: string;
|
|
7
|
+
};
|
|
8
|
+
readonly l2: {
|
|
9
|
+
readonly account: string;
|
|
10
|
+
readonly address?: string;
|
|
11
|
+
};
|
|
12
|
+
readonly bridge: {
|
|
13
|
+
readonly mode: "local-simulated" | "mainnet-multisig";
|
|
14
|
+
};
|
|
15
|
+
readonly createdAt: string;
|
|
16
|
+
}
|
|
17
|
+
interface SessionStore {
|
|
18
|
+
readonly activeSession?: string;
|
|
19
|
+
readonly sessions: Record<string, HardkasSession>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare const SESSION_FILE = ".hardkas/sessions.json";
|
|
23
|
+
declare function loadSessionStore(cwd?: string): SessionStore;
|
|
24
|
+
declare function loadSessionStoreStrict(cwd?: string): SessionStore;
|
|
25
|
+
declare function loadSessionStoreWithDiagnostics(cwd?: string): {
|
|
26
|
+
store: SessionStore;
|
|
27
|
+
diagnostics: string[];
|
|
28
|
+
};
|
|
29
|
+
declare function saveSessionStore(store: SessionStore, cwd?: string): Promise<void>;
|
|
30
|
+
declare function createSession(session: HardkasSession, cwd?: string): Promise<void>;
|
|
31
|
+
declare function setActiveSession(name: string, cwd?: string): Promise<void>;
|
|
32
|
+
declare function getActiveSession(cwd?: string): HardkasSession | undefined;
|
|
33
|
+
|
|
34
|
+
export { type HardkasSession, SESSION_FILE, type SessionStore, createSession, getActiveSession, loadSessionStore, loadSessionStoreStrict, loadSessionStoreWithDiagnostics, saveSessionStore, setActiveSession };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// src/store.ts
|
|
2
|
+
import { readFileSync, existsSync, mkdirSync } from "fs";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
import { writeFileAtomic, withLock } from "@hardkas/core";
|
|
5
|
+
var SESSION_FILE = ".hardkas/sessions.json";
|
|
6
|
+
function loadSessionStore(cwd = process.cwd()) {
|
|
7
|
+
const { store } = loadSessionStoreWithDiagnostics(cwd);
|
|
8
|
+
return store;
|
|
9
|
+
}
|
|
10
|
+
function loadSessionStoreStrict(cwd = process.cwd()) {
|
|
11
|
+
const { store, diagnostics } = loadSessionStoreWithDiagnostics(cwd);
|
|
12
|
+
if (diagnostics.length > 0) {
|
|
13
|
+
throw new Error(`Invalid session store: ${diagnostics.join("; ")}`);
|
|
14
|
+
}
|
|
15
|
+
return store;
|
|
16
|
+
}
|
|
17
|
+
function loadSessionStoreWithDiagnostics(cwd = process.cwd()) {
|
|
18
|
+
const path = join(cwd, SESSION_FILE);
|
|
19
|
+
if (!existsSync(path)) {
|
|
20
|
+
return { store: { sessions: {} }, diagnostics: [] };
|
|
21
|
+
}
|
|
22
|
+
let raw;
|
|
23
|
+
try {
|
|
24
|
+
raw = JSON.parse(readFileSync(path, "utf-8"));
|
|
25
|
+
} catch (e) {
|
|
26
|
+
return { store: { sessions: {} }, diagnostics: [`Malformed JSON in ${SESSION_FILE}`] };
|
|
27
|
+
}
|
|
28
|
+
const diagnostics = [];
|
|
29
|
+
if (!raw || typeof raw !== "object") {
|
|
30
|
+
diagnostics.push("Session store must be a JSON object");
|
|
31
|
+
return { store: { sessions: {} }, diagnostics };
|
|
32
|
+
}
|
|
33
|
+
if (!raw.sessions || typeof raw.sessions !== "object") {
|
|
34
|
+
diagnostics.push("Session store missing 'sessions' object");
|
|
35
|
+
return { store: { sessions: {} }, diagnostics };
|
|
36
|
+
}
|
|
37
|
+
if (raw.activeSession && !raw.sessions[raw.activeSession]) {
|
|
38
|
+
diagnostics.push(`Active session '${raw.activeSession}' not found in sessions list`);
|
|
39
|
+
}
|
|
40
|
+
for (const [name, session] of Object.entries(raw.sessions)) {
|
|
41
|
+
const s = session;
|
|
42
|
+
if (s.schema !== "hardkas.session.v1") {
|
|
43
|
+
diagnostics.push(`Session '${name}' has invalid or missing schema version`);
|
|
44
|
+
}
|
|
45
|
+
if (!s.l1 || !s.l1.wallet) {
|
|
46
|
+
diagnostics.push(`Session '${name}' missing L1 wallet configuration`);
|
|
47
|
+
}
|
|
48
|
+
if (!s.l2 || !s.l2.account) {
|
|
49
|
+
diagnostics.push(`Session '${name}' missing L2 account configuration`);
|
|
50
|
+
}
|
|
51
|
+
if (!s.bridge || !s.bridge.mode) {
|
|
52
|
+
diagnostics.push(`Session '${name}' missing bridge configuration`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return { store: raw, diagnostics };
|
|
56
|
+
}
|
|
57
|
+
async function saveSessionStore(store, cwd = process.cwd()) {
|
|
58
|
+
const path = join(cwd, SESSION_FILE);
|
|
59
|
+
const dir = join(cwd, ".hardkas");
|
|
60
|
+
if (!existsSync(dir)) {
|
|
61
|
+
mkdirSync(dir, { recursive: true });
|
|
62
|
+
}
|
|
63
|
+
await writeFileAtomic(path, JSON.stringify(store, null, 2), {
|
|
64
|
+
fsyncParent: true
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async function createSession(session, cwd = process.cwd()) {
|
|
68
|
+
await withLock({ rootDir: cwd, name: "workspace", wait: true, timeoutMs: 5e3 }, async () => {
|
|
69
|
+
const store = loadSessionStoreStrict(cwd);
|
|
70
|
+
const newStore = {
|
|
71
|
+
...store,
|
|
72
|
+
sessions: {
|
|
73
|
+
...store.sessions,
|
|
74
|
+
[session.name]: session
|
|
75
|
+
},
|
|
76
|
+
activeSession: store.activeSession || session.name
|
|
77
|
+
};
|
|
78
|
+
await saveSessionStore(newStore, cwd);
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
async function setActiveSession(name, cwd = process.cwd()) {
|
|
82
|
+
await withLock({ rootDir: cwd, name: "workspace", wait: true, timeoutMs: 5e3 }, async () => {
|
|
83
|
+
const store = loadSessionStoreStrict(cwd);
|
|
84
|
+
if (!store.sessions[name]) {
|
|
85
|
+
throw new Error(`Session "${name}" not found.`);
|
|
86
|
+
}
|
|
87
|
+
const newStore = { ...store, activeSession: name };
|
|
88
|
+
await saveSessionStore(newStore, cwd);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
function getActiveSession(cwd = process.cwd()) {
|
|
92
|
+
const store = loadSessionStore(cwd);
|
|
93
|
+
if (!store.activeSession) return void 0;
|
|
94
|
+
return store.sessions[store.activeSession];
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
SESSION_FILE,
|
|
98
|
+
createSession,
|
|
99
|
+
getActiveSession,
|
|
100
|
+
loadSessionStore,
|
|
101
|
+
loadSessionStoreStrict,
|
|
102
|
+
loadSessionStoreWithDiagnostics,
|
|
103
|
+
saveSessionStore,
|
|
104
|
+
setActiveSession
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hardkas/sessions",
|
|
3
|
+
"version": "0.4.0-alpha",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@hardkas/core": "0.4.0-alpha"
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"tsup": "^8.3.5",
|
|
15
|
+
"typescript": "^5.7.2",
|
|
16
|
+
"vitest": "^2.1.8"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"author": "Javier Rodriguez",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup src/index.ts --format esm --dts --clean",
|
|
26
|
+
"test": "vitest run --passWithNoTests",
|
|
27
|
+
"typecheck": "tsc --noEmit"
|
|
28
|
+
}
|
|
29
|
+
}
|