@capsule-run/sdk 0.4.2 → 0.5.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.
Files changed (66) hide show
  1. package/README.md +13 -24
  2. package/dist/index.d.ts +4 -2
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +4 -2
  5. package/dist/index.js.map +1 -1
  6. package/dist/polyfills/buffer.d.ts +8 -0
  7. package/dist/polyfills/buffer.d.ts.map +1 -0
  8. package/dist/polyfills/buffer.js +9 -0
  9. package/dist/polyfills/buffer.js.map +1 -0
  10. package/dist/polyfills/events.d.ts +8 -0
  11. package/dist/polyfills/events.d.ts.map +1 -0
  12. package/dist/polyfills/events.js +9 -0
  13. package/dist/polyfills/events.js.map +1 -0
  14. package/dist/polyfills/fs-promises.d.ts +7 -0
  15. package/dist/polyfills/fs-promises.d.ts.map +1 -0
  16. package/dist/polyfills/fs-promises.js +7 -0
  17. package/dist/polyfills/fs-promises.js.map +1 -0
  18. package/dist/polyfills/fs.d.ts +82 -0
  19. package/dist/polyfills/fs.d.ts.map +1 -0
  20. package/dist/{files.js → polyfills/fs.js} +100 -20
  21. package/dist/polyfills/fs.js.map +1 -0
  22. package/dist/polyfills/os.d.ts +150 -0
  23. package/dist/polyfills/os.d.ts.map +1 -0
  24. package/dist/polyfills/os.js +151 -0
  25. package/dist/polyfills/os.js.map +1 -0
  26. package/dist/polyfills/path.d.ts +8 -0
  27. package/dist/polyfills/path.d.ts.map +1 -0
  28. package/dist/polyfills/path.js +8 -0
  29. package/dist/polyfills/path.js.map +1 -0
  30. package/dist/polyfills/process.d.ts +109 -0
  31. package/dist/polyfills/process.d.ts.map +1 -0
  32. package/dist/polyfills/process.js +224 -0
  33. package/dist/polyfills/process.js.map +1 -0
  34. package/dist/polyfills/stream-web.d.ts +74 -0
  35. package/dist/polyfills/stream-web.d.ts.map +1 -0
  36. package/dist/polyfills/stream-web.js +23 -0
  37. package/dist/polyfills/stream-web.js.map +1 -0
  38. package/dist/polyfills/stream.d.ts +16 -0
  39. package/dist/polyfills/stream.d.ts.map +1 -0
  40. package/dist/polyfills/stream.js +16 -0
  41. package/dist/polyfills/stream.js.map +1 -0
  42. package/dist/polyfills/url.d.ts +47 -0
  43. package/dist/polyfills/url.d.ts.map +1 -0
  44. package/dist/polyfills/url.js +68 -0
  45. package/dist/polyfills/url.js.map +1 -0
  46. package/package.json +18 -1
  47. package/src/index.ts +4 -2
  48. package/src/polyfills/buffer.ts +11 -0
  49. package/src/polyfills/events.ts +11 -0
  50. package/src/polyfills/fs-promises.ts +7 -0
  51. package/src/polyfills/fs.ts +336 -0
  52. package/src/polyfills/os.ts +222 -0
  53. package/src/polyfills/path.ts +21 -0
  54. package/src/polyfills/process.ts +286 -0
  55. package/src/polyfills/stream-web.ts +24 -0
  56. package/src/polyfills/stream.ts +28 -0
  57. package/src/polyfills/url.ts +73 -0
  58. package/dist/env.d.ts +0 -22
  59. package/dist/env.d.ts.map +0 -1
  60. package/dist/env.js +0 -61
  61. package/dist/env.js.map +0 -1
  62. package/dist/files.d.ts +0 -46
  63. package/dist/files.d.ts.map +0 -1
  64. package/dist/files.js.map +0 -1
  65. package/src/env.ts +0 -67
  66. package/src/files.ts +0 -217
package/dist/env.js DELETED
@@ -1,61 +0,0 @@
1
- /**
2
- * Capsule Environment API for WASM environment variables access.
3
- * Based on wasi:cli/environment@0.2.0
4
- */
5
- /**
6
- * Internal helper to safely get the WASI environment bindings.
7
- */
8
- function getEnvBindings() {
9
- try {
10
- const env = globalThis['wasi:cli/environment'];
11
- if (env && typeof env.getEnvironment === 'function') {
12
- return env;
13
- }
14
- }
15
- catch { }
16
- return null;
17
- }
18
- /**
19
- * Returns all environment variables as an object.
20
- * * @returns An object where keys are variable names and values are their values.
21
- * @example { "PORT": "8080" }
22
- */
23
- export function getAll() {
24
- const bindings = getEnvBindings();
25
- if (!bindings)
26
- return {};
27
- try {
28
- const envList = bindings.getEnvironment();
29
- return Object.fromEntries(envList);
30
- }
31
- catch (e) {
32
- console.error("Failed to fetch environment variables:", e);
33
- return {};
34
- }
35
- }
36
- /**
37
- * Get the value of a specific environment variable.
38
- * * @param key - The name of the environment variable.
39
- * @returns The value of the variable, or undefined if not found.
40
- */
41
- export function get(key) {
42
- const bindings = getEnvBindings();
43
- if (!bindings)
44
- return undefined;
45
- try {
46
- const envList = bindings.getEnvironment();
47
- const entry = envList.find(([k]) => k === key);
48
- return entry ? entry[1] : undefined;
49
- }
50
- catch {
51
- return undefined;
52
- }
53
- }
54
- /**
55
- * Check if an environment variable exists.
56
- * * @param key - The name of the environment variable.
57
- */
58
- export function has(key) {
59
- return get(key) !== undefined;
60
- }
61
- //# sourceMappingURL=env.js.map
package/dist/env.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH;;GAEG;AACH,SAAS,cAAc;IACrB,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,UAAU,CAAC,sBAAsB,CAAC,CAAC;QAC/C,IAAI,GAAG,IAAI,OAAO,GAAG,CAAC,cAAc,KAAK,UAAU,EAAE,CAAC;YACpD,OAAO,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IAAC,MAAM,CAAC,CAAA,CAAC;IACV,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM;IACpB,MAAM,QAAQ,GAAG,cAAc,EAAE,CAAC;IAClC,IAAI,CAAC,QAAQ;QAAE,OAAO,EAAE,CAAC;IAEzB,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,QAAQ,CAAC,cAAc,EAAE,CAAC;QAC1C,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,KAAK,CAAC,wCAAwC,EAAE,CAAC,CAAC,CAAC;QAC3D,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAC,GAAW;IAC7B,MAAM,QAAQ,GAAG,cAAc,EAAE,CAAC;IAClC,IAAI,CAAC,QAAQ;QAAE,OAAO,SAAS,CAAC;IAEhC,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,QAAQ,CAAC,cAAc,EAAE,CAAC;QAC1C,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;QAC/C,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IACtC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,GAAG,CAAC,GAAW;IAC7B,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;AAChC,CAAC"}
package/dist/files.d.ts DELETED
@@ -1,46 +0,0 @@
1
- /**
2
- * Capsule Files API for WASM filesystem access.
3
- */
4
- /**
5
- * Read a file as text.
6
- *
7
- * @param path - The path to read.
8
- * @returns A promise that resolves to a string containing the file contents.
9
- */
10
- export declare function readText(path: string): Promise<string>;
11
- /**
12
- * Read a file as bytes.
13
- *
14
- * @param path - The path to read.
15
- * @returns A promise that resolves to a Uint8Array containing the file contents.
16
- */
17
- export declare function readBytes(path: string): Promise<Uint8Array>;
18
- /**
19
- * Write text content to a file.
20
- *
21
- * @param path - The path to write.
22
- * @param content - The text content to write.
23
- */
24
- export declare function writeText(path: string, content: string): Promise<void>;
25
- /**
26
- * Write bytes to a file.
27
- *
28
- * @param path - The path to write.
29
- * @param data - The bytes to write.
30
- */
31
- export declare function writeBytes(path: string, data: Uint8Array): Promise<void>;
32
- /**
33
- * List files/directories at a path.
34
- *
35
- * @param path - The path to list.
36
- * @returns A promise that resolves to an array of strings representing the files and directories at the specified path.
37
- */
38
- export declare function list(path?: string): Promise<string[]>;
39
- /**
40
- * Check if a file or directory exists.
41
- *
42
- * @param path - The path to check.
43
- * @returns A promise that resolves to a boolean indicating whether the file or directory exists.
44
- */
45
- export declare function exists(path: string): Promise<boolean>;
46
- //# sourceMappingURL=files.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"files.d.ts","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAAA;;GAEG;AAoFH;;;;;GAKG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAG5D;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAkBjE;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG5E;AAED;;;;;GAKG;AACH,wBAAsB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAgB9E;AAED;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,IAAI,GAAE,MAAY,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CA6BhE;AAED;;;;;GAKG;AACH,wBAAsB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAe3D"}
package/dist/files.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"files.js","sourceRoot":"","sources":["../src/files.ts"],"names":[],"mappings":"AAAA;;GAEG;AAyBH,SAAS,aAAa;IACpB,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,UAAU,CAAC,uBAAuB,CAAC,CAAC;QAClD,MAAM,QAAQ,GAAG,UAAU,CAAC,0BAA0B,CAAC,CAAC;QACxD,IAAI,KAAK,IAAI,QAAQ,EA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M,IAAI,KAAK,CAAC,6BAA6B,IAAI,MAAM,CAAC,EAAE,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAC,IAAY;IACvC,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,EAAE,aAAa,EAAE,KAAK,EAAE,CAAC;QAC3C,MAAM,SAAS,GAAG,EAAE,CAAC;QACrB,MAAM,eAAe,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;QACvC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,EAAE,QAAQ,CAAC,YAAY,EAAE,SAAS,EAAE,eAAe,CAAC,CAAC;QAClF,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC"}
package/src/env.ts DELETED
@@ -1,67 +0,0 @@
1
- /**
2
- * Capsule Environment API for WASM environment variables access.
3
- * Based on wasi:cli/environment@0.2.0
4
- */
5
-
6
- declare const globalThis: {
7
- 'wasi:cli/environment': {
8
- getEnvironment(): [string, string][];
9
- };
10
- };
11
-
12
- /**
13
- * Internal helper to safely get the WASI environment bindings.
14
- */
15
- function getEnvBindings() {
16
- try {
17
- const env = globalThis['wasi:cli/environment'];
18
- if (env && typeof env.getEnvironment === 'function') {
19
- return env;
20
- }
21
- } catch {}
22
- return null;
23
- }
24
-
25
- /**
26
- * Returns all environment variables as an object.
27
- * * @returns An object where keys are variable names and values are their values.
28
- * @example { "PORT": "8080" }
29
- */
30
- export function getAll(): Record<string, string> {
31
- const bindings = getEnvBindings();
32
- if (!bindings) return {};
33
-
34
- try {
35
- const envList = bindings.getEnvironment();
36
- return Object.fromEntries(envList);
37
- } catch (e) {
38
- console.error("Failed to fetch environment variables:", e);
39
- return {};
40
- }
41
- }
42
-
43
- /**
44
- * Get the value of a specific environment variable.
45
- * * @param key - The name of the environment variable.
46
- * @returns The value of the variable, or undefined if not found.
47
- */
48
- export function get(key: string): string | undefined {
49
- const bindings = getEnvBindings();
50
- if (!bindings) return undefined;
51
-
52
- try {
53
- const envList = bindings.getEnvironment();
54
- const entry = envList.find(([k]) => k === key);
55
- return entry ? entry[1] : undefined;
56
- } catch {
57
- return undefined;
58
- }
59
- }
60
-
61
- /**
62
- * Check if an environment variable exists.
63
- * * @param key - The name of the environment variable.
64
- */
65
- export function has(key: string): boolean {
66
- return get(key) !== undefined;
67
- }
package/src/files.ts DELETED
@@ -1,217 +0,0 @@
1
- /**
2
- * Capsule Files API for WASM filesystem access.
3
- */
4
-
5
- declare const globalThis: {
6
- 'wasi:filesystem/types': any;
7
- 'wasi:filesystem/preopens': any;
8
- };
9
-
10
- interface Descriptor {
11
- read(length: bigint, offset: bigint): [Uint8Array, boolean];
12
- write(buffer: Uint8Array, offset: bigint): bigint;
13
- stat(): { size: bigint };
14
- readDirectory(): any;
15
- openAt(
16
- pathFlags: { symlinkFollow?: boolean },
17
- path: string,
18
- openFlags: { create?: boolean; directory?: boolean; exclusive?: boolean; truncate?: boolean },
19
- descriptorFlags: { read?: boolean; write?: boolean; mutateDirectory?: boolean }
20
- ): Descriptor;
21
- }
22
-
23
- interface PreopenedDir {
24
- descriptor: Descriptor;
25
- guestPath: string;
26
- }
27
-
28
- function getFsBindings(): { types: any; preopens: any } | null {
29
- try {
30
- const types = globalThis['wasi:filesystem/types'];
31
- const preopens = globalThis['wasi:filesystem/preopens'];
32
- if (types && preopens) {
33
- return { types, preopens };
34
- }
35
- } catch {}
36
- return null;
37
- }
38
-
39
- function getPreopenedDirs(): PreopenedDir[] {
40
- const fs = getFsBindings();
41
- if (!fs) return [];
42
-
43
- try {
44
- const dirs = fs.preopens.getDirectories();
45
- return (dirs || []).map((entry: [Descriptor, string]) => ({
46
- descriptor: entry[0],
47
- guestPath: entry[1]
48
- }));
49
- } catch {
50
- return [];
51
- }
52
- }
53
-
54
- function normalizePath(path: string): string {
55
- if (path.startsWith('./')) {
56
- return path.slice(2);
57
- }
58
- return path;
59
- }
60
-
61
- function resolvePath(path: string): { dir: Descriptor; relativePath: string } | null {
62
- const preopens = getPreopenedDirs();
63
- if (preopens.length === 0) return null;
64
-
65
- const normalizedPath = normalizePath(path);
66
-
67
- for (const { descriptor, guestPath } of preopens) {
68
- const normalizedGuest = normalizePath(guestPath);
69
-
70
- if (normalizedGuest === '.' || normalizedGuest === '') {
71
- return { dir: descriptor, relativePath: normalizedPath };
72
- }
73
-
74
- if (normalizedPath.startsWith(normalizedGuest + '/')) {
75
- const relativePath = normalizedPath.slice(normalizedGuest.length + 1);
76
- return { dir: descriptor, relativePath };
77
- }
78
-
79
- if (normalizedPath === normalizedGuest) {
80
- return { dir: descriptor, relativePath: '.' };
81
- }
82
- }
83
-
84
- return { dir: preopens[0].descriptor, relativePath: normalizedPath };
85
- }
86
-
87
- /**
88
- * Read a file as text.
89
- *
90
- * @param path - The path to read.
91
- * @returns A promise that resolves to a string containing the file contents.
92
- */
93
- export async function readText(path: string): Promise<string> {
94
- const bytes = await readBytes(path);
95
- return new TextDecoder().decode(bytes);
96
- }
97
-
98
- /**
99
- * Read a file as bytes.
100
- *
101
- * @param path - The path to read.
102
- * @returns A promise that resolves to a Uint8Array containing the file contents.
103
- */
104
- export async function readBytes(path: string): Promise<Uint8Array> {
105
- const resolved = resolvePath(path);
106
- if (!resolved) {
107
- throw new Error("Filesystem not available.");
108
- }
109
-
110
- try {
111
- const pathFlags = { symlinkFollow: false };
112
- const openFlags = {};
113
- const descriptorFlags = { read: true };
114
-
115
- const fd = resolved.dir.openAt(pathFlags, resolved.relativePath, openFlags, descriptorFlags);
116
- const stat = fd.stat();
117
- const [data] = fd.read(stat.size, BigInt(0));
118
- return data;
119
- } catch (e) {
120
- throw new Error(`Failed to read file '${path}': ${e}`);
121
- }
122
- }
123
-
124
- /**
125
- * Write text content to a file.
126
- *
127
- * @param path - The path to write.
128
- * @param content - The text content to write.
129
- */
130
- export async function writeText(path: string, content: string): Promise<void> {
131
- const bytes = new TextEncoder().encode(content);
132
- await writeBytes(path, bytes);
133
- }
134
-
135
- /**
136
- * Write bytes to a file.
137
- *
138
- * @param path - The path to write.
139
- * @param data - The bytes to write.
140
- */
141
- export async function writeBytes(path: string, data: Uint8Array): Promise<void> {
142
- const resolved = resolvePath(path);
143
- if (!resolved) {
144
- throw new Error("Filesystem not available.");
145
- }
146
-
147
- try {
148
- const pathFlags = { symlinkFollow: false };
149
- const openFlags = { create: true, truncate: true };
150
- const descriptorFlags = { write: true };
151
-
152
- const fd = resolved.dir.openAt(pathFlags, resolved.relativePath, openFlags, descriptorFlags);
153
- fd.write(data, BigInt(0));
154
- } catch (e) {
155
- throw new Error(`Failed to write file '${path}': ${e}`);
156
- }
157
- }
158
-
159
- /**
160
- * List files/directories at a path.
161
- *
162
- * @param path - The path to list.
163
- * @returns A promise that resolves to an array of strings representing the files and directories at the specified path.
164
- */
165
- export async function list(path: string = "."): Promise<string[]> {
166
- const resolved = resolvePath(path);
167
- if (!resolved) {
168
- throw new Error("Filesystem not available.");
169
- }
170
-
171
- try {
172
- let targetDir = resolved.dir;
173
- if (resolved.relativePath !== ".") {
174
- const pathFlags = { symlinkFollow: false };
175
- const openFlags = { directory: true };
176
- const descriptorFlags = { read: true };
177
- targetDir = resolved.dir.openAt(pathFlags, resolved.relativePath, openFlags, descriptorFlags);
178
- }
179
-
180
- const stream = targetDir.readDirectory();
181
- const entries: string[] = [];
182
-
183
- let entry;
184
- while ((entry = stream.readDirectoryEntry()) && entry) {
185
- if (entry.name) {
186
- entries.push(entry.name);
187
- }
188
- }
189
-
190
- return entries;
191
- } catch (e) {
192
- throw new Error(`Failed to list directory '${path}': ${e}`);
193
- }
194
- }
195
-
196
- /**
197
- * Check if a file or directory exists.
198
- *
199
- * @param path - The path to check.
200
- * @returns A promise that resolves to a boolean indicating whether the file or directory exists.
201
- */
202
- export async function exists(path: string): Promise<boolean> {
203
- const resolved = resolvePath(path);
204
- if (!resolved) {
205
- return false;
206
- }
207
-
208
- try {
209
- const pathFlags = { symlinkFollow: false };
210
- const openFlags = {};
211
- const descriptorFlags = { read: true };
212
- resolved.dir.openAt(pathFlags, resolved.relativePath, openFlags, descriptorFlags);
213
- return true;
214
- } catch {
215
- return false;
216
- }
217
- }