almostnode 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 (216) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +731 -0
  3. package/dist/__sw__.js +394 -0
  4. package/dist/ai-chatbot-demo-entry.d.ts +6 -0
  5. package/dist/ai-chatbot-demo-entry.d.ts.map +1 -0
  6. package/dist/ai-chatbot-demo.d.ts +42 -0
  7. package/dist/ai-chatbot-demo.d.ts.map +1 -0
  8. package/dist/assets/runtime-worker-D9x_Ddwz.js +60543 -0
  9. package/dist/assets/runtime-worker-D9x_Ddwz.js.map +1 -0
  10. package/dist/convex-app-demo-entry.d.ts +6 -0
  11. package/dist/convex-app-demo-entry.d.ts.map +1 -0
  12. package/dist/convex-app-demo.d.ts +68 -0
  13. package/dist/convex-app-demo.d.ts.map +1 -0
  14. package/dist/cors-proxy.d.ts +46 -0
  15. package/dist/cors-proxy.d.ts.map +1 -0
  16. package/dist/create-runtime.d.ts +42 -0
  17. package/dist/create-runtime.d.ts.map +1 -0
  18. package/dist/demo.d.ts +6 -0
  19. package/dist/demo.d.ts.map +1 -0
  20. package/dist/dev-server.d.ts +97 -0
  21. package/dist/dev-server.d.ts.map +1 -0
  22. package/dist/frameworks/next-dev-server.d.ts +202 -0
  23. package/dist/frameworks/next-dev-server.d.ts.map +1 -0
  24. package/dist/frameworks/vite-dev-server.d.ts +85 -0
  25. package/dist/frameworks/vite-dev-server.d.ts.map +1 -0
  26. package/dist/index.cjs +14965 -0
  27. package/dist/index.cjs.map +1 -0
  28. package/dist/index.d.ts +71 -0
  29. package/dist/index.d.ts.map +1 -0
  30. package/dist/index.mjs +14867 -0
  31. package/dist/index.mjs.map +1 -0
  32. package/dist/next-demo.d.ts +49 -0
  33. package/dist/next-demo.d.ts.map +1 -0
  34. package/dist/npm/index.d.ts +71 -0
  35. package/dist/npm/index.d.ts.map +1 -0
  36. package/dist/npm/registry.d.ts +66 -0
  37. package/dist/npm/registry.d.ts.map +1 -0
  38. package/dist/npm/resolver.d.ts +52 -0
  39. package/dist/npm/resolver.d.ts.map +1 -0
  40. package/dist/npm/tarball.d.ts +29 -0
  41. package/dist/npm/tarball.d.ts.map +1 -0
  42. package/dist/runtime-interface.d.ts +90 -0
  43. package/dist/runtime-interface.d.ts.map +1 -0
  44. package/dist/runtime.d.ts +103 -0
  45. package/dist/runtime.d.ts.map +1 -0
  46. package/dist/sandbox-helpers.d.ts +43 -0
  47. package/dist/sandbox-helpers.d.ts.map +1 -0
  48. package/dist/sandbox-runtime.d.ts +65 -0
  49. package/dist/sandbox-runtime.d.ts.map +1 -0
  50. package/dist/server-bridge.d.ts +89 -0
  51. package/dist/server-bridge.d.ts.map +1 -0
  52. package/dist/shims/assert.d.ts +51 -0
  53. package/dist/shims/assert.d.ts.map +1 -0
  54. package/dist/shims/async_hooks.d.ts +37 -0
  55. package/dist/shims/async_hooks.d.ts.map +1 -0
  56. package/dist/shims/buffer.d.ts +20 -0
  57. package/dist/shims/buffer.d.ts.map +1 -0
  58. package/dist/shims/child_process-browser.d.ts +92 -0
  59. package/dist/shims/child_process-browser.d.ts.map +1 -0
  60. package/dist/shims/child_process.d.ts +93 -0
  61. package/dist/shims/child_process.d.ts.map +1 -0
  62. package/dist/shims/chokidar.d.ts +55 -0
  63. package/dist/shims/chokidar.d.ts.map +1 -0
  64. package/dist/shims/cluster.d.ts +52 -0
  65. package/dist/shims/cluster.d.ts.map +1 -0
  66. package/dist/shims/crypto.d.ts +122 -0
  67. package/dist/shims/crypto.d.ts.map +1 -0
  68. package/dist/shims/dgram.d.ts +34 -0
  69. package/dist/shims/dgram.d.ts.map +1 -0
  70. package/dist/shims/diagnostics_channel.d.ts +80 -0
  71. package/dist/shims/diagnostics_channel.d.ts.map +1 -0
  72. package/dist/shims/dns.d.ts +87 -0
  73. package/dist/shims/dns.d.ts.map +1 -0
  74. package/dist/shims/domain.d.ts +25 -0
  75. package/dist/shims/domain.d.ts.map +1 -0
  76. package/dist/shims/esbuild.d.ts +105 -0
  77. package/dist/shims/esbuild.d.ts.map +1 -0
  78. package/dist/shims/events.d.ts +37 -0
  79. package/dist/shims/events.d.ts.map +1 -0
  80. package/dist/shims/fs.d.ts +115 -0
  81. package/dist/shims/fs.d.ts.map +1 -0
  82. package/dist/shims/fsevents.d.ts +67 -0
  83. package/dist/shims/fsevents.d.ts.map +1 -0
  84. package/dist/shims/http.d.ts +217 -0
  85. package/dist/shims/http.d.ts.map +1 -0
  86. package/dist/shims/http2.d.ts +81 -0
  87. package/dist/shims/http2.d.ts.map +1 -0
  88. package/dist/shims/https.d.ts +36 -0
  89. package/dist/shims/https.d.ts.map +1 -0
  90. package/dist/shims/inspector.d.ts +25 -0
  91. package/dist/shims/inspector.d.ts.map +1 -0
  92. package/dist/shims/module.d.ts +22 -0
  93. package/dist/shims/module.d.ts.map +1 -0
  94. package/dist/shims/net.d.ts +100 -0
  95. package/dist/shims/net.d.ts.map +1 -0
  96. package/dist/shims/os.d.ts +159 -0
  97. package/dist/shims/os.d.ts.map +1 -0
  98. package/dist/shims/path.d.ts +72 -0
  99. package/dist/shims/path.d.ts.map +1 -0
  100. package/dist/shims/perf_hooks.d.ts +50 -0
  101. package/dist/shims/perf_hooks.d.ts.map +1 -0
  102. package/dist/shims/process.d.ts +93 -0
  103. package/dist/shims/process.d.ts.map +1 -0
  104. package/dist/shims/querystring.d.ts +23 -0
  105. package/dist/shims/querystring.d.ts.map +1 -0
  106. package/dist/shims/readdirp.d.ts +52 -0
  107. package/dist/shims/readdirp.d.ts.map +1 -0
  108. package/dist/shims/readline.d.ts +62 -0
  109. package/dist/shims/readline.d.ts.map +1 -0
  110. package/dist/shims/rollup.d.ts +34 -0
  111. package/dist/shims/rollup.d.ts.map +1 -0
  112. package/dist/shims/sentry.d.ts +163 -0
  113. package/dist/shims/sentry.d.ts.map +1 -0
  114. package/dist/shims/stream.d.ts +181 -0
  115. package/dist/shims/stream.d.ts.map +1 -0
  116. package/dist/shims/tls.d.ts +53 -0
  117. package/dist/shims/tls.d.ts.map +1 -0
  118. package/dist/shims/tty.d.ts +30 -0
  119. package/dist/shims/tty.d.ts.map +1 -0
  120. package/dist/shims/url.d.ts +64 -0
  121. package/dist/shims/url.d.ts.map +1 -0
  122. package/dist/shims/util.d.ts +106 -0
  123. package/dist/shims/util.d.ts.map +1 -0
  124. package/dist/shims/v8.d.ts +73 -0
  125. package/dist/shims/v8.d.ts.map +1 -0
  126. package/dist/shims/vfs-adapter.d.ts +126 -0
  127. package/dist/shims/vfs-adapter.d.ts.map +1 -0
  128. package/dist/shims/vm.d.ts +45 -0
  129. package/dist/shims/vm.d.ts.map +1 -0
  130. package/dist/shims/worker_threads.d.ts +66 -0
  131. package/dist/shims/worker_threads.d.ts.map +1 -0
  132. package/dist/shims/ws.d.ts +66 -0
  133. package/dist/shims/ws.d.ts.map +1 -0
  134. package/dist/shims/zlib.d.ts +161 -0
  135. package/dist/shims/zlib.d.ts.map +1 -0
  136. package/dist/transform.d.ts +24 -0
  137. package/dist/transform.d.ts.map +1 -0
  138. package/dist/virtual-fs.d.ts +226 -0
  139. package/dist/virtual-fs.d.ts.map +1 -0
  140. package/dist/vite-demo.d.ts +35 -0
  141. package/dist/vite-demo.d.ts.map +1 -0
  142. package/dist/vite-sw.js +132 -0
  143. package/dist/worker/runtime-worker.d.ts +8 -0
  144. package/dist/worker/runtime-worker.d.ts.map +1 -0
  145. package/dist/worker-runtime.d.ts +50 -0
  146. package/dist/worker-runtime.d.ts.map +1 -0
  147. package/package.json +85 -0
  148. package/src/ai-chatbot-demo-entry.ts +244 -0
  149. package/src/ai-chatbot-demo.ts +509 -0
  150. package/src/convex-app-demo-entry.ts +1107 -0
  151. package/src/convex-app-demo.ts +1316 -0
  152. package/src/cors-proxy.ts +81 -0
  153. package/src/create-runtime.ts +147 -0
  154. package/src/demo.ts +304 -0
  155. package/src/dev-server.ts +274 -0
  156. package/src/frameworks/next-dev-server.ts +2224 -0
  157. package/src/frameworks/vite-dev-server.ts +702 -0
  158. package/src/index.ts +101 -0
  159. package/src/next-demo.ts +1784 -0
  160. package/src/npm/index.ts +347 -0
  161. package/src/npm/registry.ts +152 -0
  162. package/src/npm/resolver.ts +385 -0
  163. package/src/npm/tarball.ts +209 -0
  164. package/src/runtime-interface.ts +103 -0
  165. package/src/runtime.ts +1046 -0
  166. package/src/sandbox-helpers.ts +173 -0
  167. package/src/sandbox-runtime.ts +252 -0
  168. package/src/server-bridge.ts +426 -0
  169. package/src/shims/assert.ts +664 -0
  170. package/src/shims/async_hooks.ts +86 -0
  171. package/src/shims/buffer.ts +75 -0
  172. package/src/shims/child_process-browser.ts +217 -0
  173. package/src/shims/child_process.ts +463 -0
  174. package/src/shims/chokidar.ts +313 -0
  175. package/src/shims/cluster.ts +67 -0
  176. package/src/shims/crypto.ts +830 -0
  177. package/src/shims/dgram.ts +47 -0
  178. package/src/shims/diagnostics_channel.ts +196 -0
  179. package/src/shims/dns.ts +172 -0
  180. package/src/shims/domain.ts +58 -0
  181. package/src/shims/esbuild.ts +805 -0
  182. package/src/shims/events.ts +195 -0
  183. package/src/shims/fs.ts +803 -0
  184. package/src/shims/fsevents.ts +63 -0
  185. package/src/shims/http.ts +904 -0
  186. package/src/shims/http2.ts +96 -0
  187. package/src/shims/https.ts +86 -0
  188. package/src/shims/inspector.ts +30 -0
  189. package/src/shims/module.ts +82 -0
  190. package/src/shims/net.ts +359 -0
  191. package/src/shims/os.ts +195 -0
  192. package/src/shims/path.ts +199 -0
  193. package/src/shims/perf_hooks.ts +92 -0
  194. package/src/shims/process.ts +346 -0
  195. package/src/shims/querystring.ts +97 -0
  196. package/src/shims/readdirp.ts +228 -0
  197. package/src/shims/readline.ts +110 -0
  198. package/src/shims/rollup.ts +80 -0
  199. package/src/shims/sentry.ts +133 -0
  200. package/src/shims/stream.ts +1126 -0
  201. package/src/shims/tls.ts +95 -0
  202. package/src/shims/tty.ts +64 -0
  203. package/src/shims/url.ts +171 -0
  204. package/src/shims/util.ts +312 -0
  205. package/src/shims/v8.ts +113 -0
  206. package/src/shims/vfs-adapter.ts +402 -0
  207. package/src/shims/vm.ts +83 -0
  208. package/src/shims/worker_threads.ts +111 -0
  209. package/src/shims/ws.ts +382 -0
  210. package/src/shims/zlib.ts +289 -0
  211. package/src/transform.ts +313 -0
  212. package/src/types/external.d.ts +67 -0
  213. package/src/virtual-fs.ts +903 -0
  214. package/src/vite-demo.ts +577 -0
  215. package/src/worker/runtime-worker.ts +128 -0
  216. package/src/worker-runtime.ts +145 -0
@@ -0,0 +1,274 @@
1
+ /**
2
+ * Base DevServer class for framework-specific dev servers
3
+ * This is a framework-agnostic base that can be extended for Vite, Next.js, etc.
4
+ */
5
+
6
+ import { EventEmitter } from './shims/events';
7
+ import { VirtualFS } from './virtual-fs';
8
+ import { Buffer } from './shims/stream';
9
+
10
+ export interface DevServerOptions {
11
+ port: number;
12
+ root?: string;
13
+ }
14
+
15
+ export interface ResponseData {
16
+ statusCode: number;
17
+ statusMessage: string;
18
+ headers: Record<string, string>;
19
+ body: Buffer;
20
+ }
21
+
22
+ export interface HMRUpdate {
23
+ type: 'update' | 'full-reload';
24
+ path: string;
25
+ timestamp?: number;
26
+ }
27
+
28
+ /**
29
+ * MIME type mapping for common file extensions
30
+ */
31
+ const MIME_TYPES: Record<string, string> = {
32
+ html: 'text/html; charset=utf-8',
33
+ htm: 'text/html; charset=utf-8',
34
+ css: 'text/css; charset=utf-8',
35
+ js: 'application/javascript; charset=utf-8',
36
+ mjs: 'application/javascript; charset=utf-8',
37
+ cjs: 'application/javascript; charset=utf-8',
38
+ jsx: 'application/javascript; charset=utf-8',
39
+ ts: 'application/javascript; charset=utf-8',
40
+ tsx: 'application/javascript; charset=utf-8',
41
+ json: 'application/json; charset=utf-8',
42
+ png: 'image/png',
43
+ jpg: 'image/jpeg',
44
+ jpeg: 'image/jpeg',
45
+ gif: 'image/gif',
46
+ svg: 'image/svg+xml',
47
+ ico: 'image/x-icon',
48
+ webp: 'image/webp',
49
+ woff: 'font/woff',
50
+ woff2: 'font/woff2',
51
+ ttf: 'font/ttf',
52
+ otf: 'font/otf',
53
+ eot: 'application/vnd.ms-fontobject',
54
+ mp3: 'audio/mpeg',
55
+ mp4: 'video/mp4',
56
+ webm: 'video/webm',
57
+ ogg: 'audio/ogg',
58
+ wav: 'audio/wav',
59
+ pdf: 'application/pdf',
60
+ xml: 'application/xml',
61
+ txt: 'text/plain; charset=utf-8',
62
+ md: 'text/markdown; charset=utf-8',
63
+ wasm: 'application/wasm',
64
+ map: 'application/json',
65
+ };
66
+
67
+ /**
68
+ * Base class for framework-specific dev servers.
69
+ * Extend this to create Vite, Next.js, etc. implementations.
70
+ */
71
+ export abstract class DevServer extends EventEmitter {
72
+ protected vfs: VirtualFS;
73
+ protected port: number;
74
+ protected root: string;
75
+ protected running: boolean = false;
76
+
77
+ constructor(vfs: VirtualFS, options: DevServerOptions) {
78
+ super();
79
+ this.vfs = vfs;
80
+ this.port = options.port;
81
+ this.root = options.root || '/';
82
+ }
83
+
84
+ /**
85
+ * Handle an incoming HTTP request
86
+ * Must be implemented by framework-specific subclass
87
+ */
88
+ abstract handleRequest(
89
+ method: string,
90
+ url: string,
91
+ headers: Record<string, string>,
92
+ body?: Buffer
93
+ ): Promise<ResponseData>;
94
+
95
+ /**
96
+ * Start file watching for HMR
97
+ * Must be implemented by framework-specific subclass
98
+ */
99
+ abstract startWatching(): void;
100
+
101
+ /**
102
+ * Stop the server and cleanup
103
+ */
104
+ stop(): void {
105
+ this.running = false;
106
+ this.emit('close');
107
+ }
108
+
109
+ /**
110
+ * Start the server
111
+ */
112
+ start(): void {
113
+ this.running = true;
114
+ this.startWatching();
115
+ this.emit('listening', this.port);
116
+ }
117
+
118
+ /**
119
+ * Check if server is running
120
+ */
121
+ isRunning(): boolean {
122
+ return this.running;
123
+ }
124
+
125
+ /**
126
+ * Get the server's port
127
+ */
128
+ getPort(): number {
129
+ return this.port;
130
+ }
131
+
132
+ /**
133
+ * Serve a static file from the virtual filesystem
134
+ */
135
+ protected serveFile(filePath: string): ResponseData {
136
+ try {
137
+ // Normalize path
138
+ const normalizedPath = this.resolvePath(filePath);
139
+ const content = this.vfs.readFileSync(normalizedPath);
140
+ // Ensure we have a Buffer
141
+ const buffer = typeof content === 'string'
142
+ ? Buffer.from(content)
143
+ : Buffer.from(content);
144
+
145
+ return {
146
+ statusCode: 200,
147
+ statusMessage: 'OK',
148
+ headers: {
149
+ 'Content-Type': this.getMimeType(filePath),
150
+ 'Content-Length': String(buffer.length),
151
+ 'Cache-Control': 'no-cache',
152
+ },
153
+ body: buffer,
154
+ };
155
+ } catch (error) {
156
+ if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
157
+ return this.notFound(filePath);
158
+ }
159
+ return this.serverError(error);
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Resolve a URL path to a filesystem path
165
+ */
166
+ protected resolvePath(urlPath: string): string {
167
+ // Remove query string and hash
168
+ let path = urlPath.split('?')[0].split('#')[0];
169
+
170
+ // Normalize path
171
+ if (!path.startsWith('/')) {
172
+ path = '/' + path;
173
+ }
174
+
175
+ // Join with root
176
+ if (this.root !== '/') {
177
+ path = this.root + path;
178
+ }
179
+
180
+ return path;
181
+ }
182
+
183
+ /**
184
+ * Create a 404 Not Found response
185
+ */
186
+ protected notFound(path: string): ResponseData {
187
+ const body = `Not found: ${path}`;
188
+ return {
189
+ statusCode: 404,
190
+ statusMessage: 'Not Found',
191
+ headers: {
192
+ 'Content-Type': 'text/plain; charset=utf-8',
193
+ 'Content-Length': String(Buffer.byteLength(body)),
194
+ },
195
+ body: Buffer.from(body),
196
+ };
197
+ }
198
+
199
+ /**
200
+ * Create a 500 Server Error response
201
+ */
202
+ protected serverError(error: unknown): ResponseData {
203
+ const message = error instanceof Error ? error.message : 'Internal Server Error';
204
+ const body = `Server Error: ${message}`;
205
+ return {
206
+ statusCode: 500,
207
+ statusMessage: 'Internal Server Error',
208
+ headers: {
209
+ 'Content-Type': 'text/plain; charset=utf-8',
210
+ 'Content-Length': String(Buffer.byteLength(body)),
211
+ },
212
+ body: Buffer.from(body),
213
+ };
214
+ }
215
+
216
+ /**
217
+ * Create a redirect response
218
+ */
219
+ protected redirect(location: string, status: 301 | 302 | 307 | 308 = 302): ResponseData {
220
+ return {
221
+ statusCode: status,
222
+ statusMessage: status === 301 ? 'Moved Permanently' : 'Found',
223
+ headers: {
224
+ Location: location,
225
+ 'Content-Type': 'text/plain; charset=utf-8',
226
+ 'Content-Length': '0',
227
+ },
228
+ body: Buffer.from(''),
229
+ };
230
+ }
231
+
232
+ /**
233
+ * Get MIME type for a file path
234
+ */
235
+ protected getMimeType(path: string): string {
236
+ const ext = path.split('.').pop()?.toLowerCase() || '';
237
+ return MIME_TYPES[ext] || 'application/octet-stream';
238
+ }
239
+
240
+ /**
241
+ * Check if a path exists in the virtual filesystem
242
+ */
243
+ protected exists(path: string): boolean {
244
+ try {
245
+ this.vfs.statSync(path);
246
+ return true;
247
+ } catch {
248
+ return false;
249
+ }
250
+ }
251
+
252
+ /**
253
+ * Check if a path is a directory
254
+ */
255
+ protected isDirectory(path: string): boolean {
256
+ try {
257
+ return this.vfs.statSync(path).isDirectory();
258
+ } catch {
259
+ return false;
260
+ }
261
+ }
262
+
263
+ /**
264
+ * Emit an HMR update event
265
+ */
266
+ protected emitHMRUpdate(update: HMRUpdate): void {
267
+ this.emit('hmr-update', {
268
+ ...update,
269
+ timestamp: update.timestamp || Date.now(),
270
+ });
271
+ }
272
+ }
273
+
274
+ export default DevServer;