nmtjs 0.15.0-beta.2 → 0.15.0-beta.20

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 (254) hide show
  1. package/dist/cli.d.ts +2 -0
  2. package/dist/cli.js +3 -2
  3. package/dist/cli.js.map +1 -0
  4. package/dist/config.d.ts +51 -0
  5. package/dist/config.js +1 -0
  6. package/dist/config.js.map +1 -0
  7. package/dist/entrypoints/cli.d.ts +1 -0
  8. package/dist/entrypoints/cli.js +1 -0
  9. package/dist/entrypoints/cli.js.map +1 -0
  10. package/dist/entrypoints/main.d.ts +5 -0
  11. package/dist/entrypoints/main.js +83 -15
  12. package/dist/entrypoints/main.js.map +1 -0
  13. package/dist/entrypoints/thread.d.ts +14 -0
  14. package/dist/entrypoints/thread.js +130 -24
  15. package/dist/entrypoints/thread.js.map +1 -0
  16. package/dist/entrypoints/worker.d.ts +3 -0
  17. package/dist/entrypoints/worker.js +4 -3
  18. package/dist/entrypoints/worker.js.map +1 -0
  19. package/dist/index.d.ts +69 -0
  20. package/dist/{_exports/index.js → index.js} +9 -5
  21. package/dist/index.js.map +1 -0
  22. package/dist/resolver.d.ts +2 -0
  23. package/dist/resolver.js +1 -0
  24. package/dist/resolver.js.map +1 -0
  25. package/dist/runtime/application/api/api.d.ts +49 -0
  26. package/dist/runtime/application/api/api.js +193 -0
  27. package/dist/runtime/application/api/api.js.map +1 -0
  28. package/dist/runtime/application/api/constants.d.ts +14 -0
  29. package/dist/runtime/application/api/constants.js +8 -0
  30. package/dist/runtime/application/api/constants.js.map +1 -0
  31. package/dist/runtime/application/api/filters.d.ts +14 -0
  32. package/dist/runtime/application/api/filters.js +11 -0
  33. package/dist/runtime/application/api/filters.js.map +1 -0
  34. package/dist/runtime/application/api/guards.d.ts +13 -0
  35. package/dist/runtime/application/api/guards.js +8 -0
  36. package/dist/runtime/application/api/guards.js.map +1 -0
  37. package/dist/runtime/application/api/index.d.ts +8 -0
  38. package/dist/runtime/application/api/index.js +9 -0
  39. package/dist/runtime/application/api/index.js.map +1 -0
  40. package/dist/runtime/application/api/middlewares.d.ts +14 -0
  41. package/dist/runtime/application/api/middlewares.js +12 -0
  42. package/dist/runtime/application/api/middlewares.js.map +1 -0
  43. package/dist/runtime/application/api/procedure.d.ts +67 -0
  44. package/dist/runtime/application/api/procedure.js +50 -0
  45. package/dist/runtime/application/api/procedure.js.map +1 -0
  46. package/dist/runtime/application/api/router.d.ts +71 -0
  47. package/dist/runtime/application/api/router.js +51 -0
  48. package/dist/runtime/application/api/router.js.map +1 -0
  49. package/dist/runtime/application/api/types.d.ts +32 -0
  50. package/dist/runtime/application/api/types.js +2 -0
  51. package/dist/runtime/application/api/types.js.map +1 -0
  52. package/dist/runtime/application/config.d.ts +26 -0
  53. package/dist/runtime/application/config.js +21 -0
  54. package/dist/runtime/application/config.js.map +1 -0
  55. package/dist/runtime/application/constants.d.ts +2 -0
  56. package/dist/runtime/application/constants.js +2 -0
  57. package/dist/runtime/application/constants.js.map +1 -0
  58. package/dist/runtime/application/hook.d.ts +19 -0
  59. package/dist/runtime/application/hook.js +11 -0
  60. package/dist/runtime/application/hook.js.map +1 -0
  61. package/dist/runtime/application/hooks.d.ts +3 -0
  62. package/dist/runtime/application/hooks.js +4 -0
  63. package/dist/runtime/application/hooks.js.map +1 -0
  64. package/dist/runtime/application/index.d.ts +5 -0
  65. package/dist/runtime/application/index.js +6 -0
  66. package/dist/runtime/application/index.js.map +1 -0
  67. package/dist/runtime/constants.d.ts +8 -0
  68. package/dist/runtime/constants.js +5 -0
  69. package/dist/runtime/constants.js.map +1 -0
  70. package/dist/runtime/core/hooks.d.ts +4 -0
  71. package/dist/runtime/core/hooks.js +4 -0
  72. package/dist/runtime/core/hooks.js.map +1 -0
  73. package/dist/runtime/core/plugin.d.ts +8 -0
  74. package/dist/runtime/core/plugin.js +4 -0
  75. package/dist/runtime/core/plugin.js.map +1 -0
  76. package/dist/runtime/core/runtime.d.ts +27 -0
  77. package/dist/runtime/core/runtime.js +81 -0
  78. package/dist/runtime/core/runtime.js.map +1 -0
  79. package/dist/runtime/enums.d.ts +21 -0
  80. package/dist/runtime/enums.js +26 -0
  81. package/dist/runtime/enums.js.map +1 -0
  82. package/dist/runtime/index.d.ts +21 -0
  83. package/dist/runtime/index.js +22 -0
  84. package/dist/runtime/index.js.map +1 -0
  85. package/dist/runtime/injectables.d.ts +23 -0
  86. package/dist/runtime/injectables.js +20 -0
  87. package/dist/runtime/injectables.js.map +1 -0
  88. package/dist/runtime/jobs/job.d.ts +132 -0
  89. package/dist/runtime/jobs/job.js +68 -0
  90. package/dist/runtime/jobs/job.js.map +1 -0
  91. package/dist/runtime/jobs/manager.d.ts +103 -0
  92. package/dist/runtime/jobs/manager.js +200 -0
  93. package/dist/runtime/jobs/manager.js.map +1 -0
  94. package/dist/runtime/jobs/router.d.ts +251 -0
  95. package/dist/runtime/jobs/router.js +396 -0
  96. package/dist/runtime/jobs/router.js.map +1 -0
  97. package/dist/runtime/jobs/runner.d.ts +64 -0
  98. package/dist/runtime/jobs/runner.js +256 -0
  99. package/dist/runtime/jobs/runner.js.map +1 -0
  100. package/dist/runtime/jobs/step.d.ts +22 -0
  101. package/dist/runtime/jobs/step.js +18 -0
  102. package/dist/runtime/jobs/step.js.map +1 -0
  103. package/dist/runtime/jobs/ui.d.ts +3 -0
  104. package/dist/runtime/jobs/ui.js +17 -0
  105. package/dist/runtime/jobs/ui.js.map +1 -0
  106. package/dist/runtime/pubsub/manager.d.ts +48 -0
  107. package/dist/runtime/pubsub/manager.js +119 -0
  108. package/dist/runtime/pubsub/manager.js.map +1 -0
  109. package/dist/runtime/pubsub/redis.d.ts +16 -0
  110. package/dist/runtime/pubsub/redis.js +98 -0
  111. package/dist/runtime/pubsub/redis.js.map +1 -0
  112. package/dist/runtime/scheduler/index.d.ts +22 -0
  113. package/dist/runtime/scheduler/index.js +20 -0
  114. package/dist/runtime/scheduler/index.js.map +1 -0
  115. package/dist/runtime/server/applications.d.ts +52 -0
  116. package/dist/runtime/server/applications.js +133 -0
  117. package/dist/runtime/server/applications.js.map +1 -0
  118. package/dist/runtime/server/config.d.ts +121 -0
  119. package/dist/runtime/server/config.js +33 -0
  120. package/dist/runtime/server/config.js.map +1 -0
  121. package/dist/runtime/server/jobs.d.ts +41 -0
  122. package/dist/runtime/server/jobs.js +181 -0
  123. package/dist/runtime/server/jobs.js.map +1 -0
  124. package/dist/runtime/server/pool.d.ts +54 -0
  125. package/dist/runtime/server/pool.js +194 -0
  126. package/dist/runtime/server/pool.js.map +1 -0
  127. package/dist/runtime/server/proxy.d.ts +21 -0
  128. package/dist/runtime/server/proxy.js +79 -0
  129. package/dist/runtime/server/proxy.js.map +1 -0
  130. package/dist/runtime/server/server.d.ts +53 -0
  131. package/dist/runtime/server/server.js +90 -0
  132. package/dist/runtime/server/server.js.map +1 -0
  133. package/dist/runtime/store/index.d.ts +3 -0
  134. package/dist/runtime/store/index.js +23 -0
  135. package/dist/runtime/store/index.js.map +1 -0
  136. package/dist/runtime/types.d.ts +103 -0
  137. package/dist/runtime/types.js +2 -0
  138. package/dist/runtime/types.js.map +1 -0
  139. package/dist/runtime/workers/application.d.ts +47 -0
  140. package/dist/runtime/workers/application.js +162 -0
  141. package/dist/runtime/workers/application.js.map +1 -0
  142. package/dist/runtime/workers/base.d.ts +16 -0
  143. package/dist/runtime/workers/base.js +46 -0
  144. package/dist/runtime/workers/base.js.map +1 -0
  145. package/dist/runtime/workers/cli.d.ts +1 -0
  146. package/dist/runtime/workers/cli.js +2 -0
  147. package/dist/runtime/workers/cli.js.map +1 -0
  148. package/dist/runtime/workers/job.d.ts +20 -0
  149. package/dist/runtime/workers/job.js +172 -0
  150. package/dist/runtime/workers/job.js.map +1 -0
  151. package/dist/typings.d.ts +5 -0
  152. package/dist/typings.js +4 -3
  153. package/dist/typings.js.map +1 -0
  154. package/dist/vite/builder.d.ts +5 -0
  155. package/dist/vite/builder.js +5 -1
  156. package/dist/vite/builder.js.map +1 -0
  157. package/dist/vite/config.d.ts +28 -0
  158. package/dist/vite/config.js +1 -0
  159. package/dist/vite/config.js.map +1 -0
  160. package/dist/vite/plugins.d.ts +2 -0
  161. package/dist/vite/plugins.js +1 -0
  162. package/dist/vite/plugins.js.map +1 -0
  163. package/dist/vite/runners/worker.d.ts +4 -0
  164. package/dist/vite/runners/worker.js +1 -0
  165. package/dist/vite/runners/worker.js.map +1 -0
  166. package/dist/vite/server.d.ts +3 -0
  167. package/dist/vite/server.js +6 -1
  168. package/dist/vite/server.js.map +1 -0
  169. package/dist/vite/servers/main.d.ts +8 -0
  170. package/dist/vite/servers/main.js +1 -0
  171. package/dist/vite/servers/main.js.map +1 -0
  172. package/dist/vite/servers/worker.d.ts +11 -0
  173. package/dist/vite/servers/worker.js +28 -0
  174. package/dist/vite/servers/worker.js.map +1 -0
  175. package/package.json +31 -18
  176. package/src/cli.ts +144 -0
  177. package/src/config.ts +64 -0
  178. package/src/entrypoints/cli.ts +13 -0
  179. package/src/entrypoints/main.ts +200 -0
  180. package/src/entrypoints/thread.ts +184 -0
  181. package/src/entrypoints/worker.ts +48 -0
  182. package/src/index.ts +82 -0
  183. package/src/resolver.ts +16 -0
  184. package/src/runtime/application/api/api.ts +265 -0
  185. package/src/runtime/application/api/constants.ts +22 -0
  186. package/src/runtime/application/api/filters.ts +39 -0
  187. package/src/runtime/application/api/guards.ts +29 -0
  188. package/src/runtime/application/api/index.ts +8 -0
  189. package/src/runtime/application/api/middlewares.ts +37 -0
  190. package/src/runtime/application/api/procedure.ts +229 -0
  191. package/src/runtime/application/api/router.ts +193 -0
  192. package/src/runtime/application/api/types.ts +124 -0
  193. package/src/runtime/application/config.ts +69 -0
  194. package/src/runtime/application/constants.ts +4 -0
  195. package/src/runtime/application/hook.ts +51 -0
  196. package/src/runtime/application/hooks.ts +3 -0
  197. package/src/runtime/application/index.ts +5 -0
  198. package/src/runtime/constants.ts +13 -0
  199. package/src/runtime/core/hooks.ts +5 -0
  200. package/src/runtime/core/plugin.ts +13 -0
  201. package/src/runtime/core/runtime.ts +109 -0
  202. package/src/runtime/enums.ts +24 -0
  203. package/src/runtime/index.ts +21 -0
  204. package/src/runtime/injectables.ts +61 -0
  205. package/src/runtime/jobs/job.ts +370 -0
  206. package/src/runtime/jobs/manager.ts +332 -0
  207. package/src/runtime/jobs/router.ts +835 -0
  208. package/src/runtime/jobs/runner.ts +320 -0
  209. package/src/runtime/jobs/step.ts +65 -0
  210. package/src/runtime/jobs/ui.ts +21 -0
  211. package/src/runtime/pubsub/manager.ts +211 -0
  212. package/src/runtime/pubsub/redis.ts +108 -0
  213. package/src/runtime/scheduler/index.ts +39 -0
  214. package/src/runtime/server/applications.ts +210 -0
  215. package/src/runtime/server/config.ts +158 -0
  216. package/src/runtime/server/jobs.ts +250 -0
  217. package/src/runtime/server/pool.ts +260 -0
  218. package/src/runtime/server/proxy.ts +118 -0
  219. package/src/runtime/server/server.ts +155 -0
  220. package/src/runtime/store/index.ts +30 -0
  221. package/src/runtime/types.ts +93 -0
  222. package/src/runtime/workers/application.ts +209 -0
  223. package/src/runtime/workers/base.ts +68 -0
  224. package/src/runtime/workers/cli.ts +0 -0
  225. package/src/runtime/workers/job.ts +153 -0
  226. package/src/typings.ts +30 -0
  227. package/src/vite/builder.ts +122 -0
  228. package/src/vite/config.ts +45 -0
  229. package/src/vite/plugins.ts +26 -0
  230. package/src/vite/runners/worker.ts +57 -0
  231. package/src/vite/server.ts +39 -0
  232. package/src/vite/servers/main.ts +34 -0
  233. package/src/vite/servers/worker.ts +143 -0
  234. package/dist/_exports/application.js +0 -1
  235. package/dist/_exports/common.js +0 -1
  236. package/dist/_exports/contract.js +0 -2
  237. package/dist/_exports/core.js +0 -1
  238. package/dist/_exports/gateway.js +0 -1
  239. package/dist/_exports/http-transport/bun.js +0 -1
  240. package/dist/_exports/http-transport/deno.js +0 -1
  241. package/dist/_exports/http-transport/node.js +0 -1
  242. package/dist/_exports/http-transport.js +0 -1
  243. package/dist/_exports/json-format.js +0 -1
  244. package/dist/_exports/protocol/client.js +0 -1
  245. package/dist/_exports/protocol/server.js +0 -1
  246. package/dist/_exports/protocol.js +0 -1
  247. package/dist/_exports/runtime/types.js +0 -1
  248. package/dist/_exports/runtime.js +0 -1
  249. package/dist/_exports/type.js +0 -2
  250. package/dist/_exports/ws-transport/bun.js +0 -1
  251. package/dist/_exports/ws-transport/deno.js +0 -1
  252. package/dist/_exports/ws-transport/node.js +0 -1
  253. package/dist/_exports/ws-transport.js +0 -1
  254. package/dist/command.js +0 -30
package/src/typings.ts ADDED
@@ -0,0 +1,30 @@
1
+ import { mkdir, writeFile } from 'node:fs/promises'
2
+ import { resolve } from 'node:path'
3
+
4
+ import dedent from 'dedent'
5
+
6
+ export async function generateTypings(
7
+ applicationImports: Record<
8
+ string,
9
+ { path: string; specifier: string; type: 'neemata' | 'custom' }
10
+ >,
11
+ ) {
12
+ await mkdir('.neemata', { recursive: true }).catch(() => {})
13
+ await writeFile(
14
+ resolve('.neemata', 'types.d.ts'),
15
+ dedent`
16
+ /// <reference types="nmtjs/runtime/types" />
17
+
18
+ declare module 'nmtjs/runtime/types' {
19
+ interface Applications {
20
+ ${Object.entries(applicationImports)
21
+ .map(
22
+ ([appName, { specifier, type }]) =>
23
+ `'${appName}': { type: '${type}'; definition: typeof import('${specifier}').default }`,
24
+ )
25
+ .join('\n')}
26
+ }
27
+ }
28
+ `,
29
+ )
30
+ }
@@ -0,0 +1,122 @@
1
+ import { isBuiltin } from 'node:module'
2
+ import { resolve } from 'node:path'
3
+
4
+ import { build as viteBuild } from 'vite'
5
+
6
+ import type { NeemataConfig } from '../config.ts'
7
+ import type { ViteConfigOptions } from './config.ts'
8
+ // import pkgJson from '../../package.json' with { type: 'json' }
9
+ import { createConfig } from './config.ts'
10
+ import { buildPlugins } from './plugins.ts'
11
+
12
+ export async function createBuilder(
13
+ configOptions: ViteConfigOptions,
14
+ neemataConfig: NeemataConfig,
15
+ ) {
16
+ const config = createConfig(configOptions)
17
+ async function build(): Promise<any> {
18
+ const packageJson = await import(resolve('./package.json'), {
19
+ with: { type: 'json' },
20
+ }).then((mod) => mod.default)
21
+
22
+ // techinically it's possible to do the same with rolldown directly,
23
+ // but vite handles a lot of things, like defines substitutions, etc.
24
+ // also, since during dev the code is processed via vite anyway,
25
+ // using vite for build as well ensures consistency between dev and prod
26
+ return await viteBuild({
27
+ appType: 'custom',
28
+ clearScreen: false,
29
+ resolve: { alias: config.alias },
30
+ ssr: { noExternal: true },
31
+ plugins: [...buildPlugins, ...neemataConfig.plugins],
32
+ build: {
33
+ lib: { entry: config.entries, formats: ['es'] },
34
+ ssr: true,
35
+ target: 'node20',
36
+ sourcemap: true,
37
+ outDir: resolve(neemataConfig.build.outDir),
38
+ minify: neemataConfig.build.minify,
39
+ emptyOutDir: true,
40
+ rolldownOptions: {
41
+ platform: 'node',
42
+ external: (id) => {
43
+ if (neemataConfig.externalDependencies === 'all') return true
44
+ if (
45
+ isBuiltin(id) ||
46
+ id.includes('vite/server') ||
47
+ id.endsWith('.node')
48
+ )
49
+ return true
50
+
51
+ if (neemataConfig.externalDependencies === 'prod') {
52
+ const prodDeps = Object.keys(packageJson.dependencies ?? {})
53
+ if (prodDeps.includes(id)) return true
54
+ }
55
+
56
+ if (Array.isArray(neemataConfig.externalDependencies)) {
57
+ for (const dep of neemataConfig.externalDependencies) {
58
+ if (typeof dep === 'string' && dep === id) return true
59
+ if (dep instanceof RegExp && dep.test(id)) return true
60
+ }
61
+ }
62
+
63
+ return false
64
+ },
65
+ transform: {
66
+ define: {
67
+ __VITE_CONFIG__: '""',
68
+ __APPLICATIONS_CONFIG__: JSON.stringify(
69
+ JSON.stringify(
70
+ Object.fromEntries(
71
+ Object.entries(configOptions.applicationImports).map(
72
+ ([appName, { type }]) => [
73
+ appName,
74
+ { type, specifier: `./application.${appName}.js` },
75
+ ],
76
+ ),
77
+ ),
78
+ ),
79
+ ),
80
+ __dirname: 'new URL(".", import.meta.url).pathname',
81
+ __filename: 'new URL(import.meta.url).pathname',
82
+ },
83
+ },
84
+ output: {
85
+ entryFileNames: '[name].js',
86
+ chunkFileNames: 'chunks/[name]-[hash].js',
87
+ advancedChunks: {
88
+ groups: [
89
+ {
90
+ name: 'ioredis',
91
+ test: /node_modules[\\/](@ioredis|ioredis|redis)/,
92
+ priority: 4,
93
+ },
94
+ {
95
+ name: 'bullmq',
96
+ test: /node_modules[\\/]bullmq/,
97
+ priority: 2,
98
+ },
99
+ { name: 'zod', test: /node_modules[\\/]zod/, priority: 2 },
100
+ { name: 'pino', test: /node_modules[\\/]pino/, priority: 2 },
101
+ {
102
+ name: '@nmtjs-runtime',
103
+ test: /node_modules[\\/](@nmtjs[\\/]runtime)/,
104
+ priority: 2,
105
+ },
106
+ {
107
+ name: '@nmtjs-common',
108
+ test: /node_modules[\\/]@nmtjs[\\/](?=[^runtime|nmtjs])/,
109
+ priority: 1,
110
+ },
111
+ { name: 'vendor', test: /node_modules/, priority: 0 },
112
+ ],
113
+ },
114
+ minify: neemataConfig.build.minify,
115
+ },
116
+ },
117
+ chunkSizeWarningLimit: 10_000,
118
+ },
119
+ })
120
+ }
121
+ return { build }
122
+ }
@@ -0,0 +1,45 @@
1
+ import { fileURLToPath } from 'node:url'
2
+
3
+ export type ViteConfigOptions = {
4
+ applicationImports: Record<
5
+ string,
6
+ { path: string; specifier: string; type: 'neemata' | 'custom' }
7
+ >
8
+ serverEntryPath: string
9
+ entrypointMainPath: string
10
+ entrypointWorkerPath: string
11
+ entrypointThreadPath: string
12
+ // entrypointCLIPath: string
13
+ configPath: string
14
+ }
15
+
16
+ const ext = new URL(import.meta.url).pathname.endsWith('.ts') ? '.ts' : '.js'
17
+
18
+ export const baseViteConfigOptions = {
19
+ entrypointMainPath: fileURLToPath(
20
+ import.meta.resolve(`../entrypoints/main${ext}`),
21
+ ),
22
+ entrypointWorkerPath: fileURLToPath(
23
+ import.meta.resolve(`../entrypoints/worker${ext}`),
24
+ ),
25
+ entrypointThreadPath: fileURLToPath(
26
+ import.meta.resolve(`../entrypoints/thread${ext}`),
27
+ ),
28
+ // entrypointCLIPath: fileURLToPath(import.meta.resolve('../entrypoints/cli')),
29
+ } satisfies Partial<ViteConfigOptions>
30
+
31
+ export function createConfig(options: ViteConfigOptions) {
32
+ const alias = { '#server': options.serverEntryPath }
33
+ const entries = {
34
+ server: options.serverEntryPath,
35
+ main: options.entrypointMainPath,
36
+ thread: options.entrypointThreadPath,
37
+ worker: options.entrypointWorkerPath,
38
+ }
39
+
40
+ for (const [name, { path }] of Object.entries(options.applicationImports)) {
41
+ entries[`application.${name}`] = path
42
+ }
43
+
44
+ return { alias, entries }
45
+ }
@@ -0,0 +1,26 @@
1
+ import { dirname, join } from 'node:path'
2
+
3
+ import type { Plugin } from 'vite'
4
+ import type { Target } from 'vite-plugin-static-copy'
5
+ import { viteStaticCopy } from 'vite-plugin-static-copy'
6
+
7
+ import { resolver } from '../resolver.ts'
8
+
9
+ const targets: Target[] = []
10
+
11
+ try {
12
+ const { path } = resolver.sync(process.cwd(), '@nmtjs/proxy')
13
+ if (path) {
14
+ targets.push({
15
+ src: join(
16
+ dirname(path),
17
+ `neemata-proxy.${process.platform}-${process.arch}*.node`,
18
+ ),
19
+ dest: './chunks/',
20
+ })
21
+ }
22
+ } catch {}
23
+
24
+ export const buildPlugins: Plugin[] = [
25
+ ...(targets.length ? viteStaticCopy({ targets }) : []),
26
+ ]
@@ -0,0 +1,57 @@
1
+ import {
2
+ BroadcastChannel,
3
+ isInternalThread,
4
+ isMainThread,
5
+ parentPort,
6
+ threadId,
7
+ } from 'node:worker_threads'
8
+
9
+ import type { HotPayload } from 'vite'
10
+ import type { ModuleRunnerTransport } from 'vite/module-runner'
11
+ import { noopFn } from '@nmtjs/common'
12
+ import {
13
+ createNodeImportMeta,
14
+ ESModulesEvaluator,
15
+ ModuleRunner,
16
+ } from 'vite/module-runner'
17
+
18
+ export function createBroadcastChannel(threadId: number): BroadcastChannel {
19
+ return new BroadcastChannel(`nmtjs:vite:${threadId}`)
20
+ }
21
+
22
+ export function createModuleRunner(
23
+ mode: 'development' | 'production' = 'development',
24
+ ): ModuleRunner {
25
+ if (isMainThread || isInternalThread)
26
+ throw new Error('Module runner can only be created inside worker threads.')
27
+
28
+ const channel = createBroadcastChannel(threadId)
29
+
30
+ const transport: ModuleRunnerTransport = {
31
+ connect({ onMessage, onDisconnection }) {
32
+ // @ts-expect-error
33
+ channel.onmessage = (event: MessageEvent<HotPayload>) => {
34
+ onMessage(event.data)
35
+ }
36
+ parentPort!.on('close', onDisconnection)
37
+ },
38
+ send(data) {
39
+ channel.postMessage(data)
40
+ },
41
+ timeout: 5000,
42
+ }
43
+
44
+ const runner = new ModuleRunner(
45
+ {
46
+ transport,
47
+ createImportMeta: createNodeImportMeta,
48
+ hmr:
49
+ mode === 'development'
50
+ ? { logger: { debug: noopFn, error: console.error } }
51
+ : false,
52
+ },
53
+ new ESModulesEvaluator(),
54
+ )
55
+
56
+ return runner
57
+ }
@@ -0,0 +1,39 @@
1
+ import type { DevEnvironmentOptions, UserConfig } from 'vite'
2
+ import { createServer as createViteServer } from 'vite'
3
+
4
+ import type { ViteConfigOptions } from './config.ts'
5
+
6
+ export function createServer(
7
+ options: ViteConfigOptions,
8
+ config: UserConfig,
9
+ dev: DevEnvironmentOptions = {},
10
+ ) {
11
+ return createViteServer({
12
+ ...config,
13
+ server: { middlewareMode: true, ws: false },
14
+ resolve: { tsconfigPaths: true, ...config.resolve },
15
+ environments: {
16
+ neemata: {
17
+ consumer: 'server',
18
+ dev,
19
+ define: {
20
+ __VITE_CONFIG__: JSON.stringify(
21
+ JSON.stringify({ options, mode: config.mode }),
22
+ ),
23
+ __APPLICATIONS_CONFIG__: JSON.stringify(
24
+ JSON.stringify(
25
+ Object.fromEntries(
26
+ Object.entries(options.applicationImports).map(
27
+ ([appName, { path, type }]) => [
28
+ appName,
29
+ { specifier: path, type },
30
+ ],
31
+ ),
32
+ ),
33
+ ),
34
+ ),
35
+ },
36
+ },
37
+ },
38
+ })
39
+ }
@@ -0,0 +1,34 @@
1
+ import type { ViteDevServer } from 'vite'
2
+ import type { ModuleRunner } from 'vite/module-runner'
3
+ import { noopFn } from '@nmtjs/common'
4
+ import { createServerModuleRunner } from 'vite'
5
+
6
+ import type { NeemataConfig } from '../../config.ts'
7
+ import type { ViteConfigOptions } from '../config.ts'
8
+ import { createConfig } from '../config.ts'
9
+ import { buildPlugins } from '../plugins.ts'
10
+ import { createServer } from '../server.ts'
11
+
12
+ export async function createMainServer(
13
+ options: ViteConfigOptions,
14
+ mode: 'development' | 'production',
15
+ neemataConfig: NeemataConfig,
16
+ ): Promise<{ server: ViteDevServer; runner: ModuleRunner }> {
17
+ const config = createConfig(options)
18
+ const server = await createServer(options, {
19
+ appType: 'custom',
20
+ clearScreen: false,
21
+ resolve: { alias: config.alias },
22
+ mode,
23
+ plugins: [...buildPlugins, ...neemataConfig.plugins],
24
+ })
25
+ const environment = server.environments.neemata
26
+
27
+ const runner = createServerModuleRunner(environment, {
28
+ hmr:
29
+ mode === 'development'
30
+ ? { logger: { debug: noopFn, error: console.error } }
31
+ : false,
32
+ })
33
+ return { server, runner }
34
+ }
@@ -0,0 +1,143 @@
1
+ import type EventEmitter from 'node:events'
2
+ import type { BroadcastChannel, Worker } from 'node:worker_threads'
3
+
4
+ import type {
5
+ HotChannel,
6
+ HotChannelClient,
7
+ HotChannelListener,
8
+ HotPayload,
9
+ ViteDevServer,
10
+ } from 'vite'
11
+ import { DevEnvironment } from 'vite'
12
+
13
+ import type { NeemataConfig } from '../../config.ts'
14
+ import type { ViteConfigOptions } from '../config.ts'
15
+ import { createConfig } from '../config.ts'
16
+ import { buildPlugins } from '../plugins.ts'
17
+ import { createBroadcastChannel } from '../runners/worker.ts'
18
+ import { createServer } from '../server.ts'
19
+
20
+ export type WorkerServerEventMap = {
21
+ worker: [Worker]
22
+ 'worker-error': [unknown]
23
+ 'worker-ready': [unknown]
24
+ }
25
+
26
+ export async function createWorkerServer(
27
+ options: ViteConfigOptions,
28
+ mode: 'development' | 'production',
29
+ neemataConfig: NeemataConfig,
30
+ events: EventEmitter<WorkerServerEventMap>,
31
+ ): Promise<ViteDevServer> {
32
+ const config = createConfig(options)
33
+ const applicationEntries = Object.values(options.applicationImports).map(
34
+ (v) => v.path,
35
+ )
36
+
37
+ const _injectHmr = `\n\nif(import.meta.hot) { import.meta.hot.accept(globalThis._hotAccept) }`
38
+
39
+ const server = await createServer(
40
+ options,
41
+ {
42
+ appType: 'custom',
43
+ clearScreen: false,
44
+ resolve: { alias: config.alias },
45
+ mode,
46
+ plugins: [
47
+ ...buildPlugins,
48
+ ...neemataConfig.plugins,
49
+ mode === 'development'
50
+ ? [
51
+ {
52
+ name: 'neemata-worker-application-hmr',
53
+ transform(code, id, options) {
54
+ if (applicationEntries.includes(id)) {
55
+ return code + _injectHmr
56
+ }
57
+ },
58
+ },
59
+ ]
60
+ : [],
61
+ ],
62
+ },
63
+ {
64
+ createEnvironment: async (name, config, context) => {
65
+ const channels = new Map<number, BroadcastChannel>()
66
+ const clients = new Map<BroadcastChannel, HotChannelClient>()
67
+ const handlers = new Map<string, HotChannelListener>()
68
+
69
+ events.on('worker', (worker) => {
70
+ const channel = createBroadcastChannel(worker.threadId)
71
+ channel.onmessage = (event) => {
72
+ const value = event.data
73
+ const handler = handlers.get(value.event)
74
+ if (handler) handler(value.data, client)
75
+ }
76
+ channels.set(worker.threadId, channel)
77
+ const client = {
78
+ send: (payload: HotPayload) => {
79
+ channel.postMessage(payload)
80
+ },
81
+ }
82
+ clients.set(channel, client)
83
+ worker.on('exit', () => {
84
+ const handler = handlers.get('vite:client:disconnect')
85
+ if (handler) handler(undefined, client)
86
+ })
87
+ })
88
+
89
+ const transport: HotChannel = {
90
+ send(data) {
91
+ for (const channel of channels.values()) {
92
+ channel.postMessage(data)
93
+ }
94
+ },
95
+ on(event, handler) {
96
+ handlers.set(event, handler)
97
+ },
98
+ off(event) {
99
+ handlers.delete(event)
100
+ },
101
+ }
102
+
103
+ let lastError: any
104
+
105
+ events.on('worker-error', (payload: any) => {
106
+ lastError = payload?.error ?? payload
107
+ const error =
108
+ lastError instanceof Error
109
+ ? lastError
110
+ : new Error(String(lastError))
111
+ const message = {
112
+ type: 'error',
113
+ err: {
114
+ message: error.message,
115
+ stack: error.stack ?? '',
116
+ plugin: 'neemata:worker',
117
+ },
118
+ } satisfies HotPayload
119
+ for (const client of clients.values()) {
120
+ client.send(message)
121
+ }
122
+ })
123
+
124
+ events.on('worker-ready', () => {
125
+ if (!lastError) return
126
+ lastError = undefined
127
+ const message: HotPayload = { type: 'full-reload' }
128
+ for (const client of clients.values()) {
129
+ client.send(message)
130
+ }
131
+ })
132
+
133
+ const environment = new DevEnvironment(name, config, {
134
+ hot: mode === 'development',
135
+ transport,
136
+ })
137
+
138
+ return environment
139
+ },
140
+ },
141
+ )
142
+ return server
143
+ }
@@ -1 +0,0 @@
1
- export * from '@nmtjs/runtime/application';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/common';
@@ -1,2 +0,0 @@
1
- export * from '@nmtjs/contract';
2
- export { default } from '@nmtjs/contract';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/core';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/gateway';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/http-transport/bun';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/http-transport/deno';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/http-transport/node';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/http-transport';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/json-format/server';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/protocol/client';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/protocol/server';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/protocol';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/runtime/types';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/runtime';
@@ -1,2 +0,0 @@
1
- export * from '@nmtjs/type';
2
- export { default } from '@nmtjs/type';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/ws-transport/bun';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/ws-transport/deno';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/ws-transport/node';
@@ -1 +0,0 @@
1
- export * from '@nmtjs/ws-transport';
package/dist/command.js DELETED
@@ -1,30 +0,0 @@
1
- // import type { ApplicationWorker } from '@nmtjs/runtime/worker'
2
- // import { typeToString } from '@nmtjs/type'
3
- // import { defineCommand } from 'citty'
4
- export {};
5
- // export default (worker: ApplicationWorker) =>
6
- // defineCommand({
7
- // meta: { description: 'Application CLI' },
8
- // subCommands: {
9
- // list: defineCommand({
10
- // async run(ctx) {
11
- // worker.app.initializeCore()
12
- // const commands = Array.from(worker.app.registry.commands).map(
13
- // ([name, command]) => ({
14
- // command: name,
15
- // args: typeToString(command.args),
16
- // kwargs: typeToString(command.kwargs),
17
- // }),
18
- // )
19
- // console.table(commands, ['command', 'args', 'kwargs'])
20
- // },
21
- // }),
22
- // execute: defineCommand({
23
- // async run(ctx) {
24
- // const { _: positionals, ...kwargs } = ctx.args
25
- // const [commandName, ...args] = positionals
26
- // await worker.runCommand(commandName, args, kwargs)
27
- // },
28
- // }),
29
- // },
30
- // })