@ricsam/isolate-fs 0.1.1 → 0.1.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.
@@ -0,0 +1,308 @@
1
+ /**
2
+ * Global Type Definitions for @ricsam/isolate-fs
3
+ *
4
+ * These types define the globals injected by setupFs() into an isolated-vm context.
5
+ * Use these types to typecheck user code that will run inside the V8 isolate.
6
+ *
7
+ * @example
8
+ * // Typecheck isolate code with file system access
9
+ * const root = await getDirectory("/data");
10
+ * const fileHandle = await root.getFileHandle("config.json");
11
+ * const file = await fileHandle.getFile();
12
+ * const content = await file.text();
13
+ */
14
+
15
+ export {};
16
+
17
+ declare global {
18
+ // ============================================
19
+ // getDirectory - Isolate-specific entry point
20
+ // ============================================
21
+
22
+ /**
23
+ * Get a directory handle for the given path.
24
+ *
25
+ * The host controls which paths are accessible. Invalid or unauthorized
26
+ * paths will throw an error.
27
+ *
28
+ * @param path - The path to request from the host
29
+ * @returns A promise resolving to a directory handle
30
+ * @throws If the path is not allowed or doesn't exist
31
+ *
32
+ * @example
33
+ * const root = await getDirectory("/");
34
+ * const dataDir = await getDirectory("/data");
35
+ */
36
+ function getDirectory(path: string): Promise<FileSystemDirectoryHandle>;
37
+
38
+ // ============================================
39
+ // File System Access API
40
+ // ============================================
41
+
42
+ /**
43
+ * Base interface for file system handles.
44
+ */
45
+ interface FileSystemHandle {
46
+ /**
47
+ * The kind of handle: "file" or "directory".
48
+ */
49
+ readonly kind: "file" | "directory";
50
+
51
+ /**
52
+ * The name of the file or directory.
53
+ */
54
+ readonly name: string;
55
+
56
+ /**
57
+ * Compare two handles to check if they reference the same entry.
58
+ *
59
+ * @param other - Another FileSystemHandle to compare against
60
+ * @returns true if both handles reference the same entry
61
+ */
62
+ isSameEntry(other: FileSystemHandle): Promise<boolean>;
63
+ }
64
+
65
+ /**
66
+ * Handle for a file in the file system.
67
+ */
68
+ interface FileSystemFileHandle extends FileSystemHandle {
69
+ /**
70
+ * Always "file" for file handles.
71
+ */
72
+ readonly kind: "file";
73
+
74
+ /**
75
+ * Get the file contents as a File object.
76
+ *
77
+ * @returns A promise resolving to a File object
78
+ *
79
+ * @example
80
+ * const file = await fileHandle.getFile();
81
+ * const text = await file.text();
82
+ */
83
+ getFile(): Promise<File>;
84
+
85
+ /**
86
+ * Create a writable stream for writing to the file.
87
+ *
88
+ * @param options - Options for the writable stream
89
+ * @returns A promise resolving to a writable stream
90
+ *
91
+ * @example
92
+ * const writable = await fileHandle.createWritable();
93
+ * await writable.write("Hello, World!");
94
+ * await writable.close();
95
+ */
96
+ createWritable(options?: {
97
+ /**
98
+ * If true, keeps existing file data. Otherwise, truncates the file.
99
+ */
100
+ keepExistingData?: boolean;
101
+ }): Promise<FileSystemWritableFileStream>;
102
+ }
103
+
104
+ /**
105
+ * Handle for a directory in the file system.
106
+ */
107
+ interface FileSystemDirectoryHandle extends FileSystemHandle {
108
+ /**
109
+ * Always "directory" for directory handles.
110
+ */
111
+ readonly kind: "directory";
112
+
113
+ /**
114
+ * Get a file handle within this directory.
115
+ *
116
+ * @param name - The name of the file
117
+ * @param options - Options for getting the file handle
118
+ * @returns A promise resolving to a file handle
119
+ * @throws If the file doesn't exist and create is not true
120
+ *
121
+ * @example
122
+ * const file = await dir.getFileHandle("data.json");
123
+ * const newFile = await dir.getFileHandle("output.txt", { create: true });
124
+ */
125
+ getFileHandle(
126
+ name: string,
127
+ options?: {
128
+ /**
129
+ * If true, creates the file if it doesn't exist.
130
+ */
131
+ create?: boolean;
132
+ }
133
+ ): Promise<FileSystemFileHandle>;
134
+
135
+ /**
136
+ * Get a subdirectory handle within this directory.
137
+ *
138
+ * @param name - The name of the subdirectory
139
+ * @param options - Options for getting the directory handle
140
+ * @returns A promise resolving to a directory handle
141
+ * @throws If the directory doesn't exist and create is not true
142
+ *
143
+ * @example
144
+ * const subdir = await dir.getDirectoryHandle("logs");
145
+ * const newDir = await dir.getDirectoryHandle("cache", { create: true });
146
+ */
147
+ getDirectoryHandle(
148
+ name: string,
149
+ options?: {
150
+ /**
151
+ * If true, creates the directory if it doesn't exist.
152
+ */
153
+ create?: boolean;
154
+ }
155
+ ): Promise<FileSystemDirectoryHandle>;
156
+
157
+ /**
158
+ * Remove a file or directory within this directory.
159
+ *
160
+ * @param name - The name of the entry to remove
161
+ * @param options - Options for removal
162
+ * @throws If the entry doesn't exist or cannot be removed
163
+ *
164
+ * @example
165
+ * await dir.removeEntry("old-file.txt");
166
+ * await dir.removeEntry("old-dir", { recursive: true });
167
+ */
168
+ removeEntry(
169
+ name: string,
170
+ options?: {
171
+ /**
172
+ * If true, removes directories recursively.
173
+ */
174
+ recursive?: boolean;
175
+ }
176
+ ): Promise<void>;
177
+
178
+ /**
179
+ * Resolve the path from this directory to a descendant handle.
180
+ *
181
+ * @param possibleDescendant - A handle that may be a descendant
182
+ * @returns An array of path segments, or null if not a descendant
183
+ *
184
+ * @example
185
+ * const path = await root.resolve(nestedFile);
186
+ * // ["subdir", "file.txt"]
187
+ */
188
+ resolve(possibleDescendant: FileSystemHandle): Promise<string[] | null>;
189
+
190
+ /**
191
+ * Iterate over entries in this directory.
192
+ *
193
+ * @returns An async iterator of [name, handle] pairs
194
+ *
195
+ * @example
196
+ * for await (const [name, handle] of dir.entries()) {
197
+ * console.log(name, handle.kind);
198
+ * }
199
+ */
200
+ entries(): AsyncIterableIterator<[string, FileSystemHandle]>;
201
+
202
+ /**
203
+ * Iterate over entry names in this directory.
204
+ *
205
+ * @returns An async iterator of names
206
+ *
207
+ * @example
208
+ * for await (const name of dir.keys()) {
209
+ * console.log(name);
210
+ * }
211
+ */
212
+ keys(): AsyncIterableIterator<string>;
213
+
214
+ /**
215
+ * Iterate over handles in this directory.
216
+ *
217
+ * @returns An async iterator of handles
218
+ *
219
+ * @example
220
+ * for await (const handle of dir.values()) {
221
+ * console.log(handle.name, handle.kind);
222
+ * }
223
+ */
224
+ values(): AsyncIterableIterator<FileSystemHandle>;
225
+
226
+ /**
227
+ * Async iterator support for directory entries.
228
+ *
229
+ * @example
230
+ * for await (const [name, handle] of dir) {
231
+ * console.log(name, handle.kind);
232
+ * }
233
+ */
234
+ [Symbol.asyncIterator](): AsyncIterableIterator<[string, FileSystemHandle]>;
235
+ }
236
+
237
+ /**
238
+ * Parameters for write operations on FileSystemWritableFileStream.
239
+ */
240
+ interface WriteParams {
241
+ /**
242
+ * The type of write operation.
243
+ * - "write": Write data at the current position or specified position
244
+ * - "seek": Move the file position
245
+ * - "truncate": Truncate the file to a specific size
246
+ */
247
+ type: "write" | "seek" | "truncate";
248
+
249
+ /**
250
+ * The data to write (for "write" type).
251
+ */
252
+ data?: string | ArrayBuffer | Uint8Array | Blob;
253
+
254
+ /**
255
+ * The position to write at or seek to.
256
+ */
257
+ position?: number;
258
+
259
+ /**
260
+ * The size to truncate to (for "truncate" type).
261
+ */
262
+ size?: number;
263
+ }
264
+
265
+ /**
266
+ * Writable stream for writing to a file.
267
+ * Extends WritableStream with file-specific operations.
268
+ */
269
+ interface FileSystemWritableFileStream extends WritableStream<Uint8Array> {
270
+ /**
271
+ * Write data to the file.
272
+ *
273
+ * @param data - The data to write
274
+ * @returns A promise that resolves when the write completes
275
+ *
276
+ * @example
277
+ * await writable.write("Hello, World!");
278
+ * await writable.write(new Uint8Array([1, 2, 3]));
279
+ * await writable.write({ type: "write", data: "text", position: 0 });
280
+ */
281
+ write(
282
+ data: string | ArrayBuffer | Uint8Array | Blob | WriteParams
283
+ ): Promise<void>;
284
+
285
+ /**
286
+ * Seek to a position in the file.
287
+ *
288
+ * @param position - The byte position to seek to
289
+ * @returns A promise that resolves when the seek completes
290
+ *
291
+ * @example
292
+ * await writable.seek(0); // Seek to beginning
293
+ * await writable.write("Overwrite");
294
+ */
295
+ seek(position: number): Promise<void>;
296
+
297
+ /**
298
+ * Truncate the file to a specific size.
299
+ *
300
+ * @param size - The size to truncate to
301
+ * @returns A promise that resolves when the truncation completes
302
+ *
303
+ * @example
304
+ * await writable.truncate(100); // Keep only first 100 bytes
305
+ */
306
+ truncate(size: number): Promise<void>;
307
+ }
308
+ }
@@ -0,0 +1,24 @@
1
+ import * as nodeFs from "node:fs";
2
+ import type { FileSystemHandler } from "./index.ts";
3
+ export interface NodeFileSystemHandlerOptions {
4
+ /** Custom fs module (e.g., memfs for testing). Defaults to Node.js fs */
5
+ fs?: typeof nodeFs;
6
+ }
7
+ /**
8
+ * Create a FileSystemHandler backed by the Node.js filesystem
9
+ *
10
+ * @param rootPath - Absolute path to the root directory for the sandbox
11
+ * @param options - Optional configuration
12
+ * @returns FileSystemHandler implementation
13
+ *
14
+ * @example
15
+ * import { createNodeFileSystemHandler } from "@ricsam/isolate-fs";
16
+ *
17
+ * const handler = createNodeFileSystemHandler("/tmp/sandbox");
18
+ *
19
+ * // Use with createRuntime
20
+ * const runtime = await createRuntime({
21
+ * fs: { handler }
22
+ * });
23
+ */
24
+ export declare function createNodeFileSystemHandler(rootPath: string, options?: NodeFileSystemHandlerOptions): FileSystemHandler;
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "@ricsam/isolate-fs",
3
- "version": "0.1.1",
4
- "type": "module",
5
- "main": "./src/index.ts",
6
- "types": "./src/index.ts",
3
+ "version": "0.1.2",
4
+ "main": "./dist/cjs/index.cjs",
5
+ "types": "./dist/types/index.d.ts",
7
6
  "exports": {
8
7
  ".": {
9
- "import": "./src/index.ts",
10
- "types": "./src/index.ts"
8
+ "types": "./dist/types/index.d.ts",
9
+ "require": "./dist/cjs/index.cjs",
10
+ "import": "./dist/mjs/index.mjs"
11
+ },
12
+ "./isolate": {
13
+ "types": "./dist/types/isolate.d.ts"
11
14
  }
12
15
  },
13
16
  "scripts": {
@@ -20,14 +23,37 @@
20
23
  "isolated-vm": "^6",
21
24
  "mime-types": "^3.0.2"
22
25
  },
23
- "devDependencies": {
24
- "@ricsam/isolate-test-utils": "*",
25
- "@types/mime-types": "^3.0.1",
26
- "@types/node": "^24",
27
- "memfs": "^4.51.1",
28
- "typescript": "^5"
29
- },
30
26
  "peerDependencies": {
31
27
  "isolated-vm": "^6"
32
- }
33
- }
28
+ },
29
+ "author": "Richard Samuelsson",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/ricsam/isolate.git"
34
+ },
35
+ "bugs": {
36
+ "url": "https://github.com/ricsam/isolate/issues"
37
+ },
38
+ "homepage": "https://github.com/ricsam/isolate#readme",
39
+ "keywords": [
40
+ "isolated-vm",
41
+ "sandbox",
42
+ "javascript",
43
+ "runtime",
44
+ "fetch",
45
+ "filesystem",
46
+ "streams",
47
+ "v8",
48
+ "isolate"
49
+ ],
50
+ "description": "File system API implementation for isolated-vm V8 sandbox",
51
+ "module": "./dist/mjs/index.mjs",
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "files": [
56
+ "dist",
57
+ "README.md"
58
+ ]
59
+ }
package/CHANGELOG.md DELETED
@@ -1,9 +0,0 @@
1
- # @ricsam/isolate-fs
2
-
3
- ## 0.1.1
4
-
5
- ### Patch Changes
6
-
7
- - initial release
8
- - Updated dependencies
9
- - @ricsam/isolate-core@0.1.1
Binary file