@sandboxxjs/state 0.0.1

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.
@@ -0,0 +1,241 @@
1
+ /**
2
+ * Type definitions for @sandboxxjs/state
3
+ */
4
+ /**
5
+ * Minimal Sandbox interface that State layer needs
6
+ */
7
+ interface Sandbox {
8
+ shell(command: string): Promise<ShellResult>;
9
+ }
10
+ interface ShellResult {
11
+ success: boolean;
12
+ stdout: string;
13
+ stderr: string;
14
+ exitCode: number;
15
+ executionTime: number;
16
+ }
17
+ /**
18
+ * File system interface
19
+ */
20
+ interface FileSystem {
21
+ read(path: string): Promise<string>;
22
+ write(path: string, data: string): Promise<void>;
23
+ list(path: string): Promise<string[]>;
24
+ exists(path: string): Promise<boolean>;
25
+ delete(path: string): Promise<void>;
26
+ }
27
+ /**
28
+ * Environment variables interface
29
+ */
30
+ interface Environment {
31
+ get(key: string): string | undefined;
32
+ set(key: string, value: string): void;
33
+ has(key: string): boolean;
34
+ delete(key: string): void;
35
+ keys(): string[];
36
+ all(): Record<string, string>;
37
+ }
38
+ /**
39
+ * Key-value storage interface
40
+ */
41
+ interface Storage {
42
+ getItem(key: string): string | null;
43
+ setItem(key: string, value: string): void;
44
+ removeItem(key: string): void;
45
+ clear(): void;
46
+ keys(): string[];
47
+ }
48
+ /**
49
+ * StateLog type (opaque - use buildStateLog to create)
50
+ */
51
+ type StateLog = {
52
+ fs: {
53
+ write(path: string, data: string): StateLog
54
+ delete(path: string): StateLog
55
+ }
56
+ env: {
57
+ set(key: string, value: string): StateLog
58
+ delete(key: string): StateLog
59
+ }
60
+ storage: {
61
+ set(key: string, value: string): StateLog
62
+ delete(key: string): StateLog
63
+ clear(): StateLog
64
+ }
65
+ getEntries(): StateLogEntry[]
66
+ toJSON(): string
67
+ compact(): StateLog
68
+ };
69
+ interface StateLogEntry {
70
+ op: string;
71
+ args: Record<string, unknown>;
72
+ }
73
+ /**
74
+ * State capability (fs + env + storage)
75
+ */
76
+ interface WithState {
77
+ fs: FileSystem;
78
+ env: Environment;
79
+ storage: Storage;
80
+ getStateLog?(): StateLog;
81
+ }
82
+ declare class StateFS implements FileSystem {
83
+ private sandbox;
84
+ constructor(sandbox: Sandbox);
85
+ read(path: string): Promise<string>;
86
+ write(path: string, data: string): Promise<void>;
87
+ exists(path: string): Promise<boolean>;
88
+ delete(path: string): Promise<void>;
89
+ list(path: string): Promise<string[]>;
90
+ }
91
+ declare class StateEnv implements Environment {
92
+ private vars;
93
+ constructor(initial?: Record<string, string>);
94
+ get(key: string): string | undefined;
95
+ set(key: string, value: string): void;
96
+ has(key: string): boolean;
97
+ delete(key: string): void;
98
+ keys(): string[];
99
+ all(): Record<string, string>;
100
+ }
101
+ declare class StateStorage implements Storage {
102
+ private data;
103
+ constructor(initial?: Record<string, string>);
104
+ getItem(key: string): string | null;
105
+ setItem(key: string, value: string): void;
106
+ removeItem(key: string): void;
107
+ clear(): void;
108
+ keys(): string[];
109
+ }
110
+ /**
111
+ * StateLog - Records state operations for replay and persistence
112
+ */
113
+ interface StateLogEntry2 {
114
+ op: string;
115
+ args: Record<string, unknown>;
116
+ }
117
+ declare class StateLog2 {
118
+ private entries;
119
+ /**
120
+ * File system operations
121
+ */
122
+ fs: {
123
+ write: (path: string, data: string) => StateLog2
124
+ delete: (path: string) => StateLog2
125
+ };
126
+ /**
127
+ * Environment variable operations
128
+ */
129
+ env: {
130
+ set: (key: string, value: string) => StateLog2
131
+ delete: (key: string) => StateLog2
132
+ };
133
+ /**
134
+ * Storage operations
135
+ */
136
+ storage: {
137
+ set: (key: string, value: string) => StateLog2
138
+ delete: (key: string) => StateLog2
139
+ clear: () => StateLog2
140
+ };
141
+ /**
142
+ * Record a raw entry (used by recording proxy)
143
+ * @internal
144
+ */
145
+ recordEntry(op: string, args: Record<string, unknown>): StateLog2;
146
+ /**
147
+ * Get all entries
148
+ */
149
+ getEntries(): StateLogEntry2[];
150
+ /**
151
+ * Serialize to JSON string
152
+ */
153
+ toJSON(): string;
154
+ /**
155
+ * Deserialize from JSON string (internal use)
156
+ */
157
+ static fromJSON(json: string): StateLog2;
158
+ /**
159
+ * Create from entries array (internal use)
160
+ */
161
+ static fromEntries(entries: StateLogEntry2[]): StateLog2;
162
+ /**
163
+ * Compact the log by merging redundant operations
164
+ */
165
+ compact(): StateLog2;
166
+ }
167
+ /**
168
+ * Build a new StateLog (functional API)
169
+ */
170
+ declare function buildStateLog(): StateLog2;
171
+ /**
172
+ * Load StateLog from JSON string
173
+ */
174
+ declare function loadStateLog(json: string): StateLog2;
175
+ /**
176
+ * Replay StateLog operations to a target
177
+ */
178
+ declare function replayStateLog(log: StateLog, target: WithState): Promise<void>;
179
+ interface CreateStateOptions {
180
+ /** Sandbox instance for fs operations */
181
+ sandbox: Sandbox;
182
+ /** Initial environment variables */
183
+ env?: Record<string, string>;
184
+ /** Enable recording to StateLog */
185
+ enableRecord?: boolean;
186
+ }
187
+ interface StateResult {
188
+ fs: FileSystem;
189
+ env: Environment;
190
+ storage: Storage;
191
+ stateLog?: StateLog;
192
+ }
193
+ /**
194
+ * Create state instances
195
+ */
196
+ declare function createState(options: CreateStateOptions): StateResult;
197
+ interface StateStore {
198
+ saveLog(key: string, data: string): Promise<void>;
199
+ loadLog(key: string): Promise<string | null>;
200
+ deleteLog(key: string): Promise<void>;
201
+ listLogs(): Promise<string[]>;
202
+ saveBlob(ref: string, data: Buffer): Promise<void>;
203
+ loadBlob(ref: string): Promise<Buffer | null>;
204
+ deleteBlob(ref: string): Promise<void>;
205
+ }
206
+ interface StateStoreOptions {
207
+ type: "memory" | "resourcex";
208
+ }
209
+ /**
210
+ * Create a StateStore instance
211
+ */
212
+ declare function createStateStore(options: StateStoreOptions): StateStore;
213
+ interface AssetsSandbox {
214
+ upload(data: Buffer, remotePath: string): Promise<void>;
215
+ download(remotePath: string): Promise<Buffer>;
216
+ }
217
+ interface StateAssets {
218
+ uploadBuffer(data: Buffer, remotePath: string): Promise<string>;
219
+ downloadBuffer(remotePath: string, options?: {
220
+ persist?: boolean
221
+ }): Promise<Buffer | string>;
222
+ list(): string[];
223
+ }
224
+ interface CreateStateAssetsOptions {
225
+ sandbox: AssetsSandbox;
226
+ store: StateStore;
227
+ }
228
+ /**
229
+ * Create StateAssets instance
230
+ */
231
+ declare function createStateAssets(options: CreateStateAssetsOptions): StateAssets;
232
+ /**
233
+ * Error types for @sandboxxjs/state
234
+ */
235
+ declare class StateError extends Error {
236
+ constructor(message: string);
237
+ }
238
+ declare class FileSystemError extends StateError {
239
+ constructor(message: string);
240
+ }
241
+ export { replayStateLog, loadStateLog, createStateStore, createStateAssets, createState, buildStateLog, WithState, Storage, StateStoreOptions, StateStore, StateStorage, StateResult, StateLogEntry, StateLog, StateFS, StateError, StateEnv, StateAssets, ShellResult, Sandbox, FileSystemError, FileSystem, Environment, CreateStateOptions, AssetsSandbox };