ink-framework-store 0.0.0-stage → 0.1.0-alpha.10

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,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vandam Dinh
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.
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 1,
3
+ "modules": {
4
+ "*": []
5
+ }
6
+ }
package/package.json CHANGED
@@ -1,6 +1,21 @@
1
1
  {
2
2
  "name": "ink-framework-store",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.10",
4
+ "exports": {
5
+ ".": "./src/index.ts"
6
+ },
7
+ "peerDependencies": {
8
+ "ink": "npm:ink-framework@0.1.0-alpha.10"
9
+ },
10
+ "license": "MIT",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/No-Scrolling/ink.git",
14
+ "directory": "packages/store"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "*.json",
19
+ "*.ts"
20
+ ]
21
+ }
@@ -0,0 +1,28 @@
1
+ import { callNative } from "ink/native";
2
+
3
+ export type SqlValue = string | number | null;
4
+ export type SqlRow = Readonly<Record<string, SqlValue>>;
5
+
6
+ export async function openDatabase(asset: string) {
7
+ const id = await callNative("sqlite", "open", { asset });
8
+ let closed = false;
9
+ let closing: Promise<void> | undefined;
10
+ return {
11
+ async query(sql: string, parameters: readonly SqlValue[] = [], options: { signal?: AbortSignal } = {}): Promise<readonly SqlRow[]> {
12
+ if (closed || closing) throw new Error("Database is closed");
13
+ if (parameters.some(value => value !== null && typeof value !== "string" && (typeof value !== "number" || !Number.isFinite(value)))) throw new TypeError("SQL parameters must be strings, finite numbers or null");
14
+ const result: unknown = JSON.parse(await callNative("sqlite", "query", { id, sql, parameters }, options));
15
+ if (!Array.isArray(result) || result.some(row => typeof row !== "object" || row === null || Array.isArray(row)
16
+ || Object.values(row).some(value => value !== null && typeof value !== "string" && typeof value !== "number"))) {
17
+ throw new Error("Invalid database result");
18
+ }
19
+ return result;
20
+ },
21
+ async close(): Promise<void> {
22
+ if (closed) return;
23
+ if (closing) return closing;
24
+ closing = callNative("sqlite", "close", { id }).then(() => { closed = true; });
25
+ try { await closing; } finally { closing = undefined; }
26
+ },
27
+ };
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,149 @@
1
+ import type { Snapshot, SnapshotSource } from "ink";
2
+ export { openDatabase, type SqlRow, type SqlValue } from "./database";
3
+ import { callNative, onNativeMessage } from "ink/native";
4
+
5
+ export interface Store<T> extends SnapshotSource<T> {
6
+ get(): Promise<T>;
7
+ set(value: T): Promise<void>;
8
+ update(transform: (value: T) => T): Promise<void>;
9
+ reset(): Promise<void>;
10
+ }
11
+
12
+ type Options<T> = {
13
+ key: string;
14
+ version: number;
15
+ initial: T;
16
+ decode(value: unknown): T;
17
+ migrate?: (value: unknown, version: number) => unknown;
18
+ };
19
+ type Entry = { revision: number; version: number; value: string };
20
+ const observers = new Map<string, Set<() => void>>();
21
+ onNativeMessage("store-changed", message => {
22
+ if (typeof message.key !== "string") throw new Error("Invalid store change event");
23
+ for (const observer of observers.get(message.key) ?? []) observer();
24
+ });
25
+
26
+ async function read(key: string): Promise<Entry | null> {
27
+ const result: unknown = JSON.parse(await callNative("store", "get", { key }));
28
+ if (result === null) return null;
29
+ if (typeof result !== "object" || !("revision" in result) || !("version" in result)
30
+ || !("value" in result) || typeof result.revision !== "number"
31
+ || !Number.isSafeInteger(result.revision) || result.revision < 1
32
+ || typeof result.version !== "number" || !Number.isSafeInteger(result.version)
33
+ || typeof result.value !== "string") throw new Error("Invalid stored entry");
34
+ return { revision: result.revision, version: result.version, value: result.value };
35
+ }
36
+
37
+ async function write(key: string, revision: number | null, version: number, value: unknown): Promise<boolean> {
38
+ const encoded = JSON.stringify(value);
39
+ if (encoded === undefined) throw new Error("Store values must be JSON data");
40
+ const result: unknown = JSON.parse(await callNative("store", revision === null ? "replace" : "write", { key, revision, version, value: encoded }));
41
+ if (typeof result !== "object" || result === null || !("committed" in result)
42
+ || typeof result.committed !== "boolean") throw new Error("Invalid store write result");
43
+ return result.committed;
44
+ }
45
+
46
+ export function createStore<T>(options: Options<T>): Store<T> {
47
+ const { key, version, decode } = options;
48
+ if (!key || key.length > 256 || !Number.isSafeInteger(version) || version < 1) {
49
+ throw new Error("Invalid store key or version");
50
+ }
51
+ let snapshot: Snapshot<T> = { status: "loading" };
52
+ const listeners = new Set<() => void>();
53
+ let loading: Promise<T> | undefined;
54
+ let generation = 0;
55
+
56
+ function publish(next: Snapshot<T>) {
57
+ snapshot = next;
58
+ for (const listener of listeners) listener();
59
+ }
60
+
61
+ function decodeEntry(entry: Entry | null): T {
62
+ if (!entry) return decode(options.initial);
63
+ if (entry.version > version) throw new Error(`Store ${key} uses a newer version`);
64
+ const value: unknown = JSON.parse(entry.value);
65
+ if (entry.version === version) return decode(value);
66
+ if (!options.migrate) throw new Error(`Store ${key} requires a migration`);
67
+ return decode(options.migrate(value, entry.version));
68
+ }
69
+
70
+ async function load(): Promise<T> {
71
+ for (let attempt = 0; attempt < 16; attempt++) {
72
+ const entry = await read(key);
73
+ const value = decodeEntry(entry);
74
+ if (!entry || entry.version === version) return value;
75
+ if (await write(key, entry.revision, version, value)) return value;
76
+ }
77
+ throw new Error(`Store ${key} changed too often; try again`);
78
+ }
79
+
80
+ function get(): Promise<T> {
81
+ if (loading) return loading;
82
+ const current = generation;
83
+ const promise = load().then(data => {
84
+ if (current === generation) publish({ status: "ready", data });
85
+ return data;
86
+ }, error => {
87
+ if (current === generation) publish({ status: "error", error: error instanceof Error ? error : new Error(String(error)) });
88
+ throw error;
89
+ }).finally(() => { if (loading === promise) loading = undefined; });
90
+ loading = promise;
91
+ return loading;
92
+ }
93
+
94
+ function refresh() {
95
+ generation++;
96
+ loading = undefined;
97
+ void get().catch(() => {});
98
+ }
99
+
100
+ function changed(value: T) {
101
+ generation++;
102
+ loading = undefined;
103
+ publish({ status: "ready", data: value });
104
+ for (const observer of observers.get(key) ?? []) observer();
105
+ }
106
+
107
+ async function replace(value: T) {
108
+ const decoded = decode(value);
109
+ if (await write(key, null, version, decoded)) changed(decoded);
110
+ }
111
+
112
+ async function mutate(transform: (entry: Entry | null) => T) {
113
+ for (let attempt = 0; attempt < 16; attempt++) {
114
+ const entry = await read(key);
115
+ if (entry && entry.version > version) throw new Error(`Store ${key} uses a newer version`);
116
+ const value = decode(transform(entry));
117
+ if (await write(key, entry?.revision ?? 0, version, value)) {
118
+ changed(value);
119
+ return;
120
+ }
121
+ }
122
+ throw new Error(`Store ${key} changed too often; try again`);
123
+ }
124
+
125
+ return {
126
+ get,
127
+ set: replace,
128
+ update: transform => mutate(entry => transform(decodeEntry(entry))),
129
+ reset: () => replace(options.initial),
130
+ getSnapshot: () => snapshot,
131
+ subscribe(listener) {
132
+ listeners.add(listener);
133
+ if (listeners.size === 1) {
134
+ let group = observers.get(key);
135
+ if (!group) observers.set(key, group = new Set());
136
+ group.add(refresh);
137
+ refresh();
138
+ }
139
+ return () => {
140
+ listeners.delete(listener);
141
+ if (!listeners.size) {
142
+ const group = observers.get(key);
143
+ group?.delete(refresh);
144
+ if (!group?.size) observers.delete(key);
145
+ }
146
+ };
147
+ },
148
+ };
149
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.