@run0/jiki 0.1.0

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 (152) hide show
  1. package/dist/browser-bundle.d.ts +40 -0
  2. package/dist/builtins.d.ts +22 -0
  3. package/dist/code-transform.d.ts +7 -0
  4. package/dist/config/cdn.d.ts +13 -0
  5. package/dist/container.d.ts +101 -0
  6. package/dist/dev-server.d.ts +69 -0
  7. package/dist/errors.d.ts +19 -0
  8. package/dist/frameworks/code-transforms.d.ts +32 -0
  9. package/dist/frameworks/next-api-handler.d.ts +72 -0
  10. package/dist/frameworks/next-dev-server.d.ts +141 -0
  11. package/dist/frameworks/next-html-generator.d.ts +36 -0
  12. package/dist/frameworks/next-route-resolver.d.ts +19 -0
  13. package/dist/frameworks/next-shims.d.ts +78 -0
  14. package/dist/frameworks/remix-dev-server.d.ts +47 -0
  15. package/dist/frameworks/sveltekit-dev-server.d.ts +43 -0
  16. package/dist/frameworks/vite-dev-server.d.ts +50 -0
  17. package/dist/fs-errors.d.ts +36 -0
  18. package/dist/index.cjs +14916 -0
  19. package/dist/index.cjs.map +1 -0
  20. package/dist/index.d.ts +61 -0
  21. package/dist/index.mjs +14898 -0
  22. package/dist/index.mjs.map +1 -0
  23. package/dist/kernel.d.ts +48 -0
  24. package/dist/memfs.d.ts +144 -0
  25. package/dist/metrics.d.ts +78 -0
  26. package/dist/module-resolver.d.ts +60 -0
  27. package/dist/network-interceptor.d.ts +71 -0
  28. package/dist/npm/cache.d.ts +76 -0
  29. package/dist/npm/index.d.ts +60 -0
  30. package/dist/npm/lockfile-reader.d.ts +32 -0
  31. package/dist/npm/pnpm.d.ts +18 -0
  32. package/dist/npm/registry.d.ts +45 -0
  33. package/dist/npm/resolver.d.ts +39 -0
  34. package/dist/npm/sync-installer.d.ts +18 -0
  35. package/dist/npm/tarball.d.ts +4 -0
  36. package/dist/npm/workspaces.d.ts +46 -0
  37. package/dist/persistence.d.ts +94 -0
  38. package/dist/plugin.d.ts +156 -0
  39. package/dist/polyfills/assert.d.ts +30 -0
  40. package/dist/polyfills/child_process.d.ts +116 -0
  41. package/dist/polyfills/chokidar.d.ts +18 -0
  42. package/dist/polyfills/crypto.d.ts +49 -0
  43. package/dist/polyfills/events.d.ts +28 -0
  44. package/dist/polyfills/fs.d.ts +82 -0
  45. package/dist/polyfills/http.d.ts +147 -0
  46. package/dist/polyfills/module.d.ts +29 -0
  47. package/dist/polyfills/net.d.ts +53 -0
  48. package/dist/polyfills/os.d.ts +91 -0
  49. package/dist/polyfills/path.d.ts +96 -0
  50. package/dist/polyfills/perf_hooks.d.ts +21 -0
  51. package/dist/polyfills/process.d.ts +99 -0
  52. package/dist/polyfills/querystring.d.ts +15 -0
  53. package/dist/polyfills/readdirp.d.ts +18 -0
  54. package/dist/polyfills/readline.d.ts +32 -0
  55. package/dist/polyfills/stream.d.ts +106 -0
  56. package/dist/polyfills/stubs.d.ts +737 -0
  57. package/dist/polyfills/tty.d.ts +25 -0
  58. package/dist/polyfills/url.d.ts +41 -0
  59. package/dist/polyfills/util.d.ts +61 -0
  60. package/dist/polyfills/v8.d.ts +43 -0
  61. package/dist/polyfills/vm.d.ts +76 -0
  62. package/dist/polyfills/worker-threads.d.ts +77 -0
  63. package/dist/polyfills/ws.d.ts +32 -0
  64. package/dist/polyfills/zlib.d.ts +87 -0
  65. package/dist/runtime-helpers.d.ts +4 -0
  66. package/dist/runtime-interface.d.ts +39 -0
  67. package/dist/sandbox.d.ts +69 -0
  68. package/dist/server-bridge.d.ts +55 -0
  69. package/dist/shell-commands.d.ts +2 -0
  70. package/dist/shell.d.ts +101 -0
  71. package/dist/transpiler.d.ts +47 -0
  72. package/dist/type-checker.d.ts +57 -0
  73. package/dist/types/package-json.d.ts +17 -0
  74. package/dist/utils/binary-encoding.d.ts +4 -0
  75. package/dist/utils/hash.d.ts +6 -0
  76. package/dist/utils/safe-path.d.ts +6 -0
  77. package/dist/worker-runtime.d.ts +34 -0
  78. package/package.json +59 -0
  79. package/src/browser-bundle.ts +498 -0
  80. package/src/builtins.ts +222 -0
  81. package/src/code-transform.ts +183 -0
  82. package/src/config/cdn.ts +17 -0
  83. package/src/container.ts +343 -0
  84. package/src/dev-server.ts +322 -0
  85. package/src/errors.ts +604 -0
  86. package/src/frameworks/code-transforms.ts +667 -0
  87. package/src/frameworks/next-api-handler.ts +366 -0
  88. package/src/frameworks/next-dev-server.ts +1252 -0
  89. package/src/frameworks/next-html-generator.ts +585 -0
  90. package/src/frameworks/next-route-resolver.ts +521 -0
  91. package/src/frameworks/next-shims.ts +1084 -0
  92. package/src/frameworks/remix-dev-server.ts +163 -0
  93. package/src/frameworks/sveltekit-dev-server.ts +197 -0
  94. package/src/frameworks/vite-dev-server.ts +370 -0
  95. package/src/fs-errors.ts +118 -0
  96. package/src/index.ts +188 -0
  97. package/src/kernel.ts +381 -0
  98. package/src/memfs.ts +1006 -0
  99. package/src/metrics.ts +140 -0
  100. package/src/module-resolver.ts +511 -0
  101. package/src/network-interceptor.ts +143 -0
  102. package/src/npm/cache.ts +172 -0
  103. package/src/npm/index.ts +377 -0
  104. package/src/npm/lockfile-reader.ts +105 -0
  105. package/src/npm/pnpm.ts +108 -0
  106. package/src/npm/registry.ts +120 -0
  107. package/src/npm/resolver.ts +339 -0
  108. package/src/npm/sync-installer.ts +217 -0
  109. package/src/npm/tarball.ts +136 -0
  110. package/src/npm/workspaces.ts +255 -0
  111. package/src/persistence.ts +235 -0
  112. package/src/plugin.ts +293 -0
  113. package/src/polyfills/assert.ts +164 -0
  114. package/src/polyfills/child_process.ts +535 -0
  115. package/src/polyfills/chokidar.ts +52 -0
  116. package/src/polyfills/crypto.ts +433 -0
  117. package/src/polyfills/events.ts +178 -0
  118. package/src/polyfills/fs.ts +297 -0
  119. package/src/polyfills/http.ts +478 -0
  120. package/src/polyfills/module.ts +97 -0
  121. package/src/polyfills/net.ts +123 -0
  122. package/src/polyfills/os.ts +108 -0
  123. package/src/polyfills/path.ts +169 -0
  124. package/src/polyfills/perf_hooks.ts +30 -0
  125. package/src/polyfills/process.ts +349 -0
  126. package/src/polyfills/querystring.ts +66 -0
  127. package/src/polyfills/readdirp.ts +72 -0
  128. package/src/polyfills/readline.ts +80 -0
  129. package/src/polyfills/stream.ts +610 -0
  130. package/src/polyfills/stubs.ts +600 -0
  131. package/src/polyfills/tty.ts +43 -0
  132. package/src/polyfills/url.ts +97 -0
  133. package/src/polyfills/util.ts +173 -0
  134. package/src/polyfills/v8.ts +62 -0
  135. package/src/polyfills/vm.ts +111 -0
  136. package/src/polyfills/worker-threads.ts +189 -0
  137. package/src/polyfills/ws.ts +73 -0
  138. package/src/polyfills/zlib.ts +244 -0
  139. package/src/runtime-helpers.ts +83 -0
  140. package/src/runtime-interface.ts +46 -0
  141. package/src/sandbox.ts +178 -0
  142. package/src/server-bridge.ts +473 -0
  143. package/src/service-worker.ts +153 -0
  144. package/src/shell-commands.ts +708 -0
  145. package/src/shell.ts +795 -0
  146. package/src/transpiler.ts +282 -0
  147. package/src/type-checker.ts +241 -0
  148. package/src/types/package-json.ts +17 -0
  149. package/src/utils/binary-encoding.ts +38 -0
  150. package/src/utils/hash.ts +24 -0
  151. package/src/utils/safe-path.ts +38 -0
  152. package/src/worker-runtime.ts +42 -0
@@ -0,0 +1,101 @@
1
+ import { MemFS } from "./memfs";
2
+ import { Kernel } from "./kernel";
3
+ import { PackageManager } from "./npm/index";
4
+ import { EventEmitter } from "./polyfills/events";
5
+ export interface ShellOptions {
6
+ cwd?: string;
7
+ env?: Record<string, string>;
8
+ onStdout?: (data: string) => void;
9
+ onStderr?: (data: string) => void;
10
+ onExit?: (code: number) => void;
11
+ pnpmPm?: PackageManager;
12
+ lazyPnpmPm?: () => PackageManager;
13
+ }
14
+ export interface ShellProcess {
15
+ stdin: {
16
+ write(data: string): void;
17
+ };
18
+ stdout: EventEmitter;
19
+ stderr: EventEmitter;
20
+ kill(signal?: string): void;
21
+ wait(): Promise<number>;
22
+ pid: number;
23
+ }
24
+ export interface ShellResult {
25
+ stdout: string;
26
+ stderr: string;
27
+ exitCode: number;
28
+ }
29
+ export interface ShellContext {
30
+ vfs: MemFS;
31
+ runtime: Kernel;
32
+ pm: PackageManager;
33
+ pnpmPm?: PackageManager;
34
+ cwd: string;
35
+ env: Record<string, string>;
36
+ stdinData?: string;
37
+ setCwd(dir: string): void;
38
+ exec(command: string, options?: ShellOptions): Promise<ShellResult>;
39
+ write?(text: string): void;
40
+ writeErr?(text: string): void;
41
+ }
42
+ export type CommandHandler = (args: string[], ctx: ShellContext) => ShellResult | Promise<ShellResult>;
43
+ /** Command history with up/down navigation. */
44
+ export declare class ShellHistory {
45
+ private entries;
46
+ private cursor;
47
+ private maxSize;
48
+ constructor(maxSize?: number);
49
+ push(command: string): void;
50
+ up(): string | undefined;
51
+ down(): string | undefined;
52
+ reset(): void;
53
+ getAll(): string[];
54
+ get length(): number;
55
+ }
56
+ export declare class Shell {
57
+ private vfs;
58
+ private runtime;
59
+ private packageManager;
60
+ private pnpmPm?;
61
+ private lazyPnpmPm?;
62
+ private binCache;
63
+ private cwd;
64
+ private env;
65
+ private commands;
66
+ /** Command history for this shell session. */
67
+ readonly history: ShellHistory;
68
+ /** Background jobs spawned with `&`. */
69
+ private backgroundJobs;
70
+ private nextJobId;
71
+ constructor(vfs: MemFS, runtime: Kernel, packageManager: PackageManager, options?: ShellOptions);
72
+ registerCommand(name: string, handler: CommandHandler): void;
73
+ /**
74
+ * Tab completion for a partial input string.
75
+ * Returns sorted candidate completions.
76
+ */
77
+ complete(partial: string): string[];
78
+ private completeFilePath;
79
+ exec(command: string, options?: ShellOptions): Promise<ShellResult>;
80
+ private execPipeline;
81
+ private splitControlFlow;
82
+ private parseRedirects;
83
+ private execSingle;
84
+ private expandVar;
85
+ private parseCommand;
86
+ /** Consume a $VAR or ${VAR} starting at position i (pointing at '$'), returning expanded value and end index */
87
+ private consumeVariable;
88
+ private expandGlob;
89
+ /** Expand brace patterns like `{a,b}` into multiple strings. */
90
+ private expandBraces;
91
+ /** Expand `**` recursive glob patterns. */
92
+ private expandRecursiveGlob;
93
+ private matchGlobSegments;
94
+ private resolveBin;
95
+ clearBinCache(): void;
96
+ getCwd(): string;
97
+ setCwd(dir: string): void;
98
+ getEnv(): Record<string, string>;
99
+ setEnv(key: string, value: string): void;
100
+ }
101
+ export declare function createShell(vfs: MemFS, runtime: Kernel, pm: PackageManager, options?: ShellOptions): Shell;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * TypeScript/TSX/JSX transpiler and bundler powered by esbuild-wasm.
3
+ * Provides lazy initialization with singleton pattern for the WASM runtime.
4
+ */
5
+ import * as esbuild from "esbuild-wasm";
6
+ import type { MemFS } from "./memfs";
7
+ export interface TranspileOptions {
8
+ jsx?: "transform" | "preserve" | "automatic";
9
+ jsxFactory?: string;
10
+ jsxFragment?: string;
11
+ jsxImportSource?: string;
12
+ target?: string;
13
+ sourcemap?: boolean;
14
+ }
15
+ export interface BundleOptions {
16
+ entryPoint: string;
17
+ format?: "esm" | "cjs" | "iife";
18
+ platform?: "browser" | "node" | "neutral";
19
+ minify?: boolean;
20
+ sourcemap?: boolean;
21
+ target?: string;
22
+ external?: string[];
23
+ outfile?: string;
24
+ loader?: Record<string, string>;
25
+ define?: Record<string, string>;
26
+ }
27
+ export interface BundleResult {
28
+ code: string;
29
+ map?: string;
30
+ errors: esbuild.Message[];
31
+ warnings: esbuild.Message[];
32
+ }
33
+ export interface InitOptions {
34
+ wasmURL?: string | URL;
35
+ /** When true, esbuild runs transpilation in a Web Worker (browser only). */
36
+ useWorker?: boolean;
37
+ }
38
+ export declare function setWasmURL(url: string | URL): void;
39
+ export declare function initTranspiler(options?: InitOptions): Promise<void>;
40
+ export declare function isInitialized(): boolean;
41
+ export declare function needsTranspilation(filename: string): boolean;
42
+ export declare function transpile(code: string, filename: string, options?: TranspileOptions): Promise<string>;
43
+ export declare function hasSyncSupport(): boolean;
44
+ export declare function transpileSync(code: string, filename: string, options?: TranspileOptions): string;
45
+ export declare function bundle(vfs: MemFS, options: BundleOptions): Promise<BundleResult>;
46
+ export declare function stopTranspiler(): Promise<void>;
47
+ export { esbuild };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Lightweight TypeScript type-checker for jiki.
3
+ *
4
+ * Provides on-demand batch type checking by analyzing TypeScript files
5
+ * in the VFS. Uses a simple heuristic-based approach that catches common
6
+ * type errors without requiring the full TypeScript compiler.
7
+ *
8
+ * For full type checking, the TypeScript compiler can be installed as an
9
+ * npm package and run via the shell: `npx tsc --noEmit`.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const checker = new TypeChecker(container.vfs);
14
+ * const diagnostics = checker.check(['/src/app.ts', '/src/utils.ts']);
15
+ * for (const d of diagnostics) {
16
+ * console.log(`${d.file}:${d.line} - ${d.message}`);
17
+ * }
18
+ * ```
19
+ */
20
+ import type { MemFS } from "./memfs";
21
+ export interface Diagnostic {
22
+ file: string;
23
+ line: number;
24
+ column: number;
25
+ message: string;
26
+ severity: "error" | "warning" | "info";
27
+ code?: string;
28
+ }
29
+ export interface TypeCheckerOptions {
30
+ /** Strict mode (default: true). */
31
+ strict?: boolean;
32
+ /** Check for unused variables (default: false). */
33
+ noUnusedLocals?: boolean;
34
+ /** Check for implicit any (default: true in strict mode). */
35
+ noImplicitAny?: boolean;
36
+ }
37
+ /**
38
+ * Lightweight TypeScript type-checker that catches common errors
39
+ * using pattern matching and heuristics. Not a full type system,
40
+ * but catches the most common mistakes quickly.
41
+ */
42
+ export declare class TypeChecker {
43
+ private vfs;
44
+ private opts;
45
+ constructor(vfs: MemFS, options?: TypeCheckerOptions);
46
+ /**
47
+ * Check a list of TypeScript files for common errors.
48
+ * Returns diagnostics sorted by file and line number.
49
+ */
50
+ check(files: string[]): Diagnostic[];
51
+ /**
52
+ * Discover all .ts/.tsx files in a directory and check them.
53
+ */
54
+ checkAll(dir?: string): Diagnostic[];
55
+ private discoverFiles;
56
+ private checkFile;
57
+ }
@@ -0,0 +1,17 @@
1
+ export interface PackageJson {
2
+ name?: string;
3
+ version?: string;
4
+ main?: string;
5
+ module?: string;
6
+ browser?: string | Record<string, string | false>;
7
+ exports?: unknown;
8
+ imports?: Record<string, unknown>;
9
+ bin?: string | Record<string, string>;
10
+ scripts?: Record<string, string>;
11
+ dependencies?: Record<string, string>;
12
+ devDependencies?: Record<string, string>;
13
+ peerDependencies?: Record<string, string>;
14
+ optionalDependencies?: Record<string, string>;
15
+ type?: "module" | "commonjs";
16
+ [key: string]: unknown;
17
+ }
@@ -0,0 +1,4 @@
1
+ export declare function uint8ToBase64(bytes: Uint8Array): string;
2
+ export declare function base64ToUint8(base64: string): Uint8Array;
3
+ export declare function uint8ToHex(bytes: Uint8Array): string;
4
+ export declare function uint8ToBinaryString(bytes: Uint8Array): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * FNV-1a hash producing a 64-bit result (two 32-bit halves).
3
+ * Returns base36 string (~12 chars). Much lower collision probability
4
+ * than the previous 32-bit djb2 (~6 chars).
5
+ */
6
+ export declare function simpleHash(str: string): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Resolve a URL path safely within a root directory.
3
+ * Strips query/hash, decodes, normalizes, and ensures the result
4
+ * never escapes outside root via .. traversal.
5
+ */
6
+ export declare function safePath(root: string, urlPath: string): string;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Worker runtime configuration.
3
+ *
4
+ * When `worker` mode is enabled, CPU-intensive operations (transpilation via
5
+ * esbuild-wasm) are offloaded to a Web Worker. This prevents UI freezes
6
+ * during TypeScript/JSX compilation.
7
+ *
8
+ * The `worker` option on Container controls this:
9
+ * - `false` (default) — everything runs on the main thread
10
+ * - `true` — enable worker-based transpilation
11
+ * - `'auto'` — use workers when `Worker` is available (browser), skip in Node.js
12
+ *
13
+ * Full kernel isolation (executing `require()` and `new Function()` in a
14
+ * worker) is planned for a future release and requires SharedArrayBuffer
15
+ * for synchronous VFS access from the worker.
16
+ *
17
+ * @module
18
+ */
19
+ export type WorkerMode = boolean | "auto";
20
+ /**
21
+ * Determine whether to use worker-based transpilation.
22
+ * Returns `true` if workers should be enabled based on the mode
23
+ * and the current runtime environment.
24
+ */
25
+ export declare function shouldUseWorker(mode: WorkerMode): boolean;
26
+ /**
27
+ * Configuration for the worker runtime.
28
+ * Currently only affects transpilation; future versions will support
29
+ * full kernel isolation.
30
+ */
31
+ export interface WorkerRuntimeConfig {
32
+ /** Whether to use Web Workers for transpilation. */
33
+ useWorker: boolean;
34
+ }
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@run0/jiki",
3
+ "version": "0.1.0",
4
+ "description": "Lightweight browser-based Node.js runtime with virtual filesystem, npm support, and dev servers.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "engines": {
8
+ "node": ">=20.0.0"
9
+ },
10
+ "bugs": "https://github.com/prxm-labs/jiki/issues",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/prxm-labs/jiki.git",
14
+ "directory": "packages/jiki"
15
+ },
16
+ "homepage": "https://jiki.sh",
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.mjs",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "import": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.mjs"
25
+ },
26
+ "require": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.cjs"
29
+ }
30
+ }
31
+ },
32
+ "files": [
33
+ "src",
34
+ "dist"
35
+ ],
36
+ "sideEffects": false,
37
+ "dependencies": {
38
+ "acorn": "^8.14.0",
39
+ "acorn-jsx": "^5.3.2",
40
+ "comlink": "^4.4.2",
41
+ "esbuild-wasm": "^0.27.3",
42
+ "pako": "^2.1.0",
43
+ "resolve.exports": "^2.0.3"
44
+ },
45
+ "devDependencies": {
46
+ "@types/node": "^22.10.0",
47
+ "@types/pako": "^2.0.3",
48
+ "typescript": "^5.7.0",
49
+ "vite": "^5.4.0",
50
+ "vitest": "^2.1.0"
51
+ },
52
+ "scripts": {
53
+ "build": "vite build && tsc --project tsconfig.build.json",
54
+ "format": "prettier --write .",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest",
57
+ "type-check": "tsc --noEmit"
58
+ }
59
+ }