@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
@@ -0,0 +1,222 @@
1
+ /**
2
+ * OS polyfill
3
+ * Provides Node.js-compatible os object
4
+ */
5
+
6
+ interface CpuInfo {
7
+ model: string;
8
+ speed: number;
9
+ times: {
10
+ user: number;
11
+ nice: number;
12
+ sys: number;
13
+ idle: number;
14
+ irq: number;
15
+ };
16
+ }
17
+
18
+ interface NetworkInterface {
19
+ address: string;
20
+ netmask: string;
21
+ family: string;
22
+ mac: string;
23
+ internal: boolean;
24
+ cidr: string | null;
25
+ }
26
+
27
+ const os = {
28
+ /**
29
+ * Returns the operating system platform
30
+ * WASI is POSIX-like, so we return 'linux'
31
+ */
32
+ platform(): 'aix' | 'darwin' | 'freebsd' | 'linux' | 'openbsd' | 'sunos' | 'win32' {
33
+ return 'linux';
34
+ },
35
+
36
+ /**
37
+ * Returns the CPU architecture
38
+ * WASM runs on wasm32 architecture
39
+ */
40
+ arch(): string {
41
+ return 'wasm32';
42
+ },
43
+
44
+ /**
45
+ * Returns the operating system's default directory for temporary files
46
+ */
47
+ tmpdir(): string {
48
+ return '/tmp';
49
+ },
50
+
51
+ /**
52
+ * Returns the home directory of the current user
53
+ */
54
+ homedir(): string {
55
+ return '/home/capsule';
56
+ },
57
+
58
+ /**
59
+ * Returns the system uptime in seconds
60
+ * Not available in WASI, returns 0
61
+ */
62
+ uptime(): number {
63
+ return 0;
64
+ },
65
+
66
+ /**
67
+ * Returns the amount of free system memory in bytes
68
+ * Not available in WASI, returns 0
69
+ */
70
+ freemem(): number {
71
+ return 0;
72
+ },
73
+
74
+ /**
75
+ * Returns the total amount of system memory in bytes
76
+ * Not available in WASI, returns 0
77
+ */
78
+ totalmem(): number {
79
+ return 0;
80
+ },
81
+
82
+ /**
83
+ * Returns an array of objects containing information about each CPU/core
84
+ * Not available in WASI, returns empty array
85
+ */
86
+ cpus(): CpuInfo[] {
87
+ return [];
88
+ },
89
+
90
+ /**
91
+ * Returns the hostname of the operating system
92
+ */
93
+ hostname(): string {
94
+ return 'capsule-wasm';
95
+ },
96
+
97
+ /**
98
+ * Returns the operating system release
99
+ */
100
+ release(): string {
101
+ return 'wasi';
102
+ },
103
+
104
+ /**
105
+ * Returns the operating system type
106
+ */
107
+ type(): string {
108
+ return 'WASI';
109
+ },
110
+
111
+ /**
112
+ * Returns the endianness of the CPU
113
+ * WASM is little-endian
114
+ */
115
+ endianness(): 'BE' | 'LE' {
116
+ return 'LE';
117
+ },
118
+
119
+ /**
120
+ * Returns an object containing network interfaces
121
+ * Not available in WASI, returns empty object
122
+ */
123
+ networkInterfaces(): { [index: string]: NetworkInterface[] } {
124
+ return {};
125
+ },
126
+
127
+ /**
128
+ * Returns the operating system's default directory for user data
129
+ */
130
+ userInfo(options?: { encoding: 'buffer' }): {
131
+ username: string;
132
+ uid: number;
133
+ gid: number;
134
+ shell: string | null;
135
+ homedir: string;
136
+ } {
137
+ return {
138
+ username: 'capsule',
139
+ uid: 1000,
140
+ gid: 1000,
141
+ shell: null,
142
+ homedir: '/home/capsule',
143
+ };
144
+ },
145
+
146
+ /**
147
+ * Returns an array of objects containing information about the load average
148
+ * Not available in WASI, returns [0, 0, 0]
149
+ */
150
+ loadavg(): number[] {
151
+ return [0, 0, 0];
152
+ },
153
+
154
+ /**
155
+ * Returns the operating system's version
156
+ */
157
+ version(): string {
158
+ return 'WASI 0.2';
159
+ },
160
+
161
+ /**
162
+ * Returns the machine type
163
+ */
164
+ machine(): string {
165
+ return 'wasm32';
166
+ },
167
+
168
+ /**
169
+ * Platform-specific path segment separator
170
+ */
171
+ get EOL(): string {
172
+ return '\n';
173
+ },
174
+
175
+ /**
176
+ * Platform-specific constants
177
+ */
178
+ constants: {
179
+ signals: {},
180
+ errno: {},
181
+ priority: {},
182
+ },
183
+
184
+ /**
185
+ * Returns the number of logical CPU cores
186
+ * Not available in WASI, returns 1
187
+ */
188
+ availableParallelism(): number {
189
+ return 1;
190
+ },
191
+
192
+ /**
193
+ * Returns the string path of the current user's home directory
194
+ */
195
+ devNull: '/dev/null',
196
+ };
197
+
198
+ export default os;
199
+
200
+ export const {
201
+ platform,
202
+ arch,
203
+ tmpdir,
204
+ homedir,
205
+ uptime,
206
+ freemem,
207
+ totalmem,
208
+ cpus,
209
+ hostname,
210
+ release,
211
+ type,
212
+ endianness,
213
+ networkInterfaces,
214
+ userInfo,
215
+ loadavg,
216
+ version,
217
+ machine,
218
+ EOL,
219
+ constants,
220
+ availableParallelism,
221
+ devNull,
222
+ } = os;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Path polyfill
3
+ * Provides Node.js-compatible path object
4
+ */
5
+
6
+ import path from 'path-browserify';
7
+
8
+ export default path;
9
+ export const {
10
+ resolve,
11
+ join,
12
+ dirname,
13
+ basename,
14
+ extname,
15
+ format,
16
+ parse,
17
+ sep,
18
+ delimiter,
19
+ posix,
20
+ win32
21
+ } = path;
@@ -0,0 +1,286 @@
1
+ /**
2
+ * Process polyfill for WASI environment
3
+ * Provides Node.js-compatible process object with WASI-backed values
4
+ */
5
+
6
+ declare const globalThis: {
7
+ 'wasi:cli/environment': {
8
+ getEnvironment(): [string, string][];
9
+ getArguments(): string[];
10
+ initialCwd(): string | null;
11
+ };
12
+ };
13
+
14
+ /**
15
+ * Internal helper to safely get the WASI environment bindings.
16
+ */
17
+ function getEnvBindings() {
18
+ try {
19
+ const env = globalThis['wasi:cli/environment'];
20
+
21
+ if (env && typeof env.getEnvironment === 'function') {
22
+ return env;
23
+ }
24
+ } catch {}
25
+ return null;
26
+ }
27
+
28
+ /**
29
+ * Lazy-loaded environment variables from WASI
30
+ */
31
+ function getEnv(): Record<string, string> {
32
+ const bindings = getEnvBindings();
33
+ if (!bindings) {
34
+ return {};
35
+ }
36
+
37
+ try {
38
+ const envPairs = bindings.getEnvironment();
39
+ return Object.fromEntries(envPairs);
40
+ } catch (e) {
41
+ console.error("Failed to fetch environment variables:", e);
42
+ return {};
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Lazy-loaded command-line arguments from WASI
48
+ */
49
+ function getArgv(): string[] {
50
+ const bindings = getEnvBindings();
51
+ if (!bindings || typeof bindings.getArguments !== 'function') {
52
+ return ['capsule'];
53
+ }
54
+
55
+ try {
56
+ return bindings.getArguments();
57
+ } catch {
58
+ return ['capsule'];
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Get current working directory from WASI
64
+ */
65
+ function getCwd(): string {
66
+ const bindings = getEnvBindings();
67
+ if (!bindings || typeof bindings.initialCwd !== 'function') {
68
+ return '/';
69
+ }
70
+
71
+ try {
72
+ const cwd = bindings.initialCwd();
73
+ return cwd || '/';
74
+ } catch {
75
+ return '/';
76
+ }
77
+ }
78
+
79
+ const process = {
80
+ /**
81
+ * Environment variables
82
+ */
83
+ get env(): Record<string, string> {
84
+ return getEnv();
85
+ },
86
+
87
+ /**
88
+ * Command-line arguments
89
+ */
90
+ get argv(): string[] {
91
+ return getArgv();
92
+ },
93
+
94
+ /**
95
+ * Returns the current working directory
96
+ */
97
+ cwd(): string {
98
+ return getCwd();
99
+ },
100
+
101
+ /**
102
+ * Change directory (not supported in WASI)
103
+ */
104
+ chdir(directory: string): void {
105
+ throw new Error('process.chdir() is not supported in WASI environment');
106
+ },
107
+
108
+ /**
109
+ * Platform identifier
110
+ */
111
+ get platform(): 'linux' {
112
+ return 'linux';
113
+ },
114
+
115
+ /**
116
+ * Architecture identifier
117
+ */
118
+ get arch(): 'wasm32' {
119
+ return 'wasm32';
120
+ },
121
+
122
+ /**
123
+ * Process ID (not available in WASI)
124
+ */
125
+ get pid(): number {
126
+ return 1;
127
+ },
128
+
129
+ /**
130
+ * Parent process ID (not available in WASI)
131
+ */
132
+ get ppid(): number {
133
+ return 0;
134
+ },
135
+
136
+ /**
137
+ * Process uptime in seconds
138
+ */
139
+ uptime(): number {
140
+ return 0;
141
+ },
142
+
143
+ /**
144
+ * Node.js version (WASI doesn't have Node.js)
145
+ */
146
+ get version(): string {
147
+ return 'v0.0.0-wasi';
148
+ },
149
+
150
+ /**
151
+ * Node.js versions object
152
+ */
153
+ get versions(): Record<string, string> {
154
+ return {
155
+ wasi: '0.2.0',
156
+ };
157
+ },
158
+
159
+ /**
160
+ * Exit the process (not fully supported in WASI)
161
+ */
162
+ exit(code?: number): never {
163
+ throw new Error(`Process exit requested with code ${code ?? 0}`);
164
+ },
165
+
166
+ /**
167
+ * Abort the process
168
+ */
169
+ abort(): never {
170
+ throw new Error('Process aborted');
171
+ },
172
+
173
+ /**
174
+ * Queue a microtask (uses native queueMicrotask)
175
+ */
176
+ nextTick(callback: (...args: any[]) => void, ...args: any[]): void {
177
+ queueMicrotask(() => callback(...args));
178
+ },
179
+
180
+ /**
181
+ * Standard streams (not fully implemented)
182
+ */
183
+ get stdout(): any {
184
+ return {
185
+ write: (data: string) => console.log(data),
186
+ isTTY: false,
187
+ };
188
+ },
189
+
190
+ get stderr(): any {
191
+ return {
192
+ write: (data: string) => console.error(data),
193
+ isTTY: false,
194
+ };
195
+ },
196
+
197
+ get stdin(): any {
198
+ return {
199
+ isTTY: false,
200
+ };
201
+ },
202
+
203
+ /**
204
+ * Memory usage (not available in WASI)
205
+ */
206
+ memoryUsage(): {
207
+ rss: number;
208
+ heapTotal: number;
209
+ heapUsed: number;
210
+ external: number;
211
+ arrayBuffers: number;
212
+ } {
213
+ return {
214
+ rss: 0,
215
+ heapTotal: 0,
216
+ heapUsed: 0,
217
+ external: 0,
218
+ arrayBuffers: 0,
219
+ };
220
+ },
221
+
222
+ /**
223
+ * CPU usage (not available in WASI)
224
+ */
225
+ cpuUsage(previousValue?: { user: number; system: number }): {
226
+ user: number;
227
+ system: number;
228
+ } {
229
+ return {
230
+ user: 0,
231
+ system: 0,
232
+ };
233
+ },
234
+
235
+ /**
236
+ * High-resolution time
237
+ */
238
+ hrtime: {
239
+ bigint(): bigint {
240
+ return BigInt(Date.now() * 1000000);
241
+ },
242
+ },
243
+
244
+ /**
245
+ * Execute path
246
+ */
247
+ get execPath(): string {
248
+ return '/usr/bin/capsule';
249
+ },
250
+
251
+ /**
252
+ * Execution arguments
253
+ */
254
+ get execArgv(): string[] {
255
+ return [];
256
+ },
257
+
258
+ /**
259
+ * Title (not settable in WASI)
260
+ */
261
+ get title(): string {
262
+ return 'capsule';
263
+ },
264
+
265
+ };
266
+
267
+ export default process;
268
+
269
+ export const {
270
+ env,
271
+ argv,
272
+ cwd,
273
+ platform,
274
+ arch,
275
+ pid,
276
+ ppid,
277
+ version,
278
+ versions,
279
+ exit,
280
+ nextTick,
281
+ stdout,
282
+ stderr,
283
+ stdin,
284
+ } = process;
285
+
286
+ (globalThis as any).process = process;
@@ -0,0 +1,24 @@
1
+ /**
2
+ * stream/web polyfill for WASI environment
3
+ * Exports the native Web Streams API which is built into StarlingMonkey
4
+ */
5
+
6
+ export const ReadableStream = globalThis.ReadableStream;
7
+ export const WritableStream = globalThis.WritableStream;
8
+ export const TransformStream = globalThis.TransformStream;
9
+ export const ReadableStreamDefaultReader = globalThis.ReadableStreamDefaultReader;
10
+ export const ReadableStreamBYOBReader = (globalThis as any).ReadableStreamBYOBReader;
11
+ export const WritableStreamDefaultWriter = globalThis.WritableStreamDefaultWriter;
12
+ export const ByteLengthQueuingStrategy = globalThis.ByteLengthQueuingStrategy;
13
+ export const CountQueuingStrategy = globalThis.CountQueuingStrategy;
14
+
15
+ export default {
16
+ ReadableStream,
17
+ WritableStream,
18
+ TransformStream,
19
+ ReadableStreamDefaultReader,
20
+ ReadableStreamBYOBReader,
21
+ WritableStreamDefaultWriter,
22
+ ByteLengthQueuingStrategy,
23
+ CountQueuingStrategy,
24
+ };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Stream polyfill for WASI environment
3
+ * Provides Node.js-compatible streams using the readable-stream package
4
+ */
5
+
6
+ export {
7
+ Readable,
8
+ Writable,
9
+ Duplex,
10
+ Transform,
11
+ PassThrough,
12
+ Stream,
13
+ pipeline,
14
+ finished,
15
+ } from 'readable-stream';
16
+
17
+ import { Readable, Writable, Duplex, Transform, PassThrough, Stream } from 'readable-stream';
18
+
19
+ const stream = {
20
+ Readable,
21
+ Writable,
22
+ Duplex,
23
+ Transform,
24
+ PassThrough,
25
+ Stream,
26
+ };
27
+
28
+ export default stream;
@@ -0,0 +1,73 @@
1
+ /**
2
+ * URL polyfill for WASI environment
3
+ * Aliases Node.js 'url' module to native Web URL APIs
4
+ */
5
+
6
+ export const URL = globalThis.URL;
7
+
8
+ export const URLSearchParams = globalThis.URLSearchParams;
9
+
10
+ /**
11
+ * Legacy Node.js url.parse()
12
+ */
13
+ export function parse(urlString: string, parseQueryString?: boolean): any {
14
+ try {
15
+ const url = new URL(urlString);
16
+ return {
17
+ href: url.href,
18
+ protocol: url.protocol,
19
+ host: url.host,
20
+ hostname: url.hostname,
21
+ port: url.port,
22
+ pathname: url.pathname,
23
+ search: url.search,
24
+ hash: url.hash,
25
+ query: parseQueryString ? Object.fromEntries(url.searchParams) : url.search.slice(1),
26
+ };
27
+ } catch (e) {
28
+ return null;
29
+ }
30
+ }
31
+
32
+ /**
33
+ * Legacy Node.js url.format()
34
+ */
35
+ export function format(urlObject: any): string {
36
+ if (typeof urlObject === 'string') {
37
+ return urlObject;
38
+ }
39
+
40
+ try {
41
+ const protocol = urlObject.protocol || 'http:';
42
+ const hostname = urlObject.hostname || urlObject.host || 'localhost';
43
+ const port = urlObject.port ? `:${urlObject.port}` : '';
44
+ const pathname = urlObject.pathname || '/';
45
+ const search = urlObject.search || '';
46
+ const hash = urlObject.hash || '';
47
+
48
+ return `${protocol}//${hostname}${port}${pathname}${search}${hash}`;
49
+ } catch (e) {
50
+ return '';
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Legacy Node.js url.resolve()
56
+ */
57
+ export function resolve(from: string, to: string): string {
58
+ try {
59
+ return new URL(to, from).href;
60
+ } catch (e) {
61
+ return to;
62
+ }
63
+ }
64
+
65
+ const url = {
66
+ URL,
67
+ URLSearchParams,
68
+ parse,
69
+ format,
70
+ resolve,
71
+ };
72
+
73
+ export default url;
package/dist/env.d.ts DELETED
@@ -1,22 +0,0 @@
1
- /**
2
- * Capsule Environment API for WASM environment variables access.
3
- * Based on wasi:cli/environment@0.2.0
4
- */
5
- /**
6
- * Returns all environment variables as an object.
7
- * * @returns An object where keys are variable names and values are their values.
8
- * @example { "PORT": "8080" }
9
- */
10
- export declare function getAll(): Record<string, string>;
11
- /**
12
- * Get the value of a specific environment variable.
13
- * * @param key - The name of the environment variable.
14
- * @returns The value of the variable, or undefined if not found.
15
- */
16
- export declare function get(key: string): string | undefined;
17
- /**
18
- * Check if an environment variable exists.
19
- * * @param key - The name of the environment variable.
20
- */
21
- export declare function has(key: string): boolean;
22
- //# sourceMappingURL=env.d.ts.map
package/dist/env.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAqBH;;;;GAIG;AACH,wBAAgB,MAAM,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAW/C;AAED;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAWnD;AAED;;;GAGG;AACH,wBAAgB,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAExC"}