nmtjs 0.16.0 → 0.17.0-beta.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.
- package/LICENSE.md +1 -1
- package/README.md +41 -1
- package/dist/index.d.ts +22 -121
- package/dist/index.js +24 -53
- package/dist/index.js.map +1 -1
- package/package.json +11 -63
- package/src/index.ts +71 -91
- package/bin/cli.js +0 -2
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +0 -137
- package/dist/cli.js.map +0 -1
- package/dist/codegen.d.ts +0 -5
- package/dist/codegen.js +0 -18
- package/dist/codegen.js.map +0 -1
- package/dist/config.d.ts +0 -47
- package/dist/config.js +0 -16
- package/dist/config.js.map +0 -1
- package/dist/entrypoints/cli.d.ts +0 -1
- package/dist/entrypoints/cli.js +0 -12
- package/dist/entrypoints/cli.js.map +0 -1
- package/dist/entrypoints/main.d.ts +0 -5
- package/dist/entrypoints/main.js +0 -248
- package/dist/entrypoints/main.js.map +0 -1
- package/dist/entrypoints/thread.d.ts +0 -16
- package/dist/entrypoints/thread.js +0 -182
- package/dist/entrypoints/thread.js.map +0 -1
- package/dist/entrypoints/worker.d.ts +0 -9
- package/dist/entrypoints/worker.js +0 -143
- package/dist/entrypoints/worker.js.map +0 -1
- package/dist/resolver.d.ts +0 -2
- package/dist/resolver.js +0 -7
- package/dist/resolver.js.map +0 -1
- package/dist/runtime/application/index.d.ts +0 -1
- package/dist/runtime/application/index.js +0 -2
- package/dist/runtime/application/index.js.map +0 -1
- package/dist/runtime/constants.d.ts +0 -8
- package/dist/runtime/constants.js +0 -5
- package/dist/runtime/constants.js.map +0 -1
- package/dist/runtime/enums.d.ts +0 -10
- package/dist/runtime/enums.js +0 -13
- package/dist/runtime/enums.js.map +0 -1
- package/dist/runtime/hooks.d.ts +0 -1
- package/dist/runtime/hooks.js +0 -2
- package/dist/runtime/hooks.js.map +0 -1
- package/dist/runtime/index.d.ts +0 -28
- package/dist/runtime/index.js +0 -29
- package/dist/runtime/index.js.map +0 -1
- package/dist/runtime/injectables.d.ts +0 -28
- package/dist/runtime/injectables.js +0 -24
- package/dist/runtime/injectables.js.map +0 -1
- package/dist/runtime/jobs/job.d.ts +0 -146
- package/dist/runtime/jobs/job.js +0 -89
- package/dist/runtime/jobs/job.js.map +0 -1
- package/dist/runtime/jobs/manager.d.ts +0 -131
- package/dist/runtime/jobs/manager.js +0 -309
- package/dist/runtime/jobs/manager.js.map +0 -1
- package/dist/runtime/jobs/router.d.ts +0 -296
- package/dist/runtime/jobs/router.js +0 -458
- package/dist/runtime/jobs/router.js.map +0 -1
- package/dist/runtime/jobs/runner.d.ts +0 -103
- package/dist/runtime/jobs/runner.js +0 -425
- package/dist/runtime/jobs/runner.js.map +0 -1
- package/dist/runtime/jobs/step.d.ts +0 -26
- package/dist/runtime/jobs/step.js +0 -18
- package/dist/runtime/jobs/step.js.map +0 -1
- package/dist/runtime/jobs/types.d.ts +0 -86
- package/dist/runtime/jobs/types.js +0 -5
- package/dist/runtime/jobs/types.js.map +0 -1
- package/dist/runtime/metrics/metric.d.ts +0 -8
- package/dist/runtime/metrics/metric.js +0 -11
- package/dist/runtime/metrics/metric.js.map +0 -1
- package/dist/runtime/metrics/registry.d.ts +0 -3
- package/dist/runtime/metrics/registry.js +0 -5
- package/dist/runtime/metrics/registry.js.map +0 -1
- package/dist/runtime/metrics/server.d.ts +0 -7
- package/dist/runtime/metrics/server.js +0 -53
- package/dist/runtime/metrics/server.js.map +0 -1
- package/dist/runtime/plugin.d.ts +0 -2
- package/dist/runtime/plugin.js +0 -2
- package/dist/runtime/plugin.js.map +0 -1
- package/dist/runtime/runtime.d.ts +0 -27
- package/dist/runtime/runtime.js +0 -77
- package/dist/runtime/runtime.js.map +0 -1
- package/dist/runtime/scheduler/index.d.ts +0 -22
- package/dist/runtime/scheduler/index.js +0 -20
- package/dist/runtime/scheduler/index.js.map +0 -1
- package/dist/runtime/server/applications.d.ts +0 -91
- package/dist/runtime/server/applications.js +0 -171
- package/dist/runtime/server/applications.js.map +0 -1
- package/dist/runtime/server/config.d.ts +0 -148
- package/dist/runtime/server/config.js +0 -33
- package/dist/runtime/server/config.js.map +0 -1
- package/dist/runtime/server/environment.d.ts +0 -43
- package/dist/runtime/server/environment.js +0 -33
- package/dist/runtime/server/environment.js.map +0 -1
- package/dist/runtime/server/error-policy.d.ts +0 -62
- package/dist/runtime/server/error-policy.js +0 -73
- package/dist/runtime/server/error-policy.js.map +0 -1
- package/dist/runtime/server/hmr-coordinator.d.ts +0 -37
- package/dist/runtime/server/hmr-coordinator.js +0 -94
- package/dist/runtime/server/hmr-coordinator.js.map +0 -1
- package/dist/runtime/server/jobs.d.ts +0 -54
- package/dist/runtime/server/jobs.js +0 -176
- package/dist/runtime/server/jobs.js.map +0 -1
- package/dist/runtime/server/lifecycle.d.ts +0 -85
- package/dist/runtime/server/lifecycle.js +0 -177
- package/dist/runtime/server/lifecycle.js.map +0 -1
- package/dist/runtime/server/managed-worker.d.ts +0 -158
- package/dist/runtime/server/managed-worker.js +0 -420
- package/dist/runtime/server/managed-worker.js.map +0 -1
- package/dist/runtime/server/pool-manager.d.ts +0 -85
- package/dist/runtime/server/pool-manager.js +0 -129
- package/dist/runtime/server/pool-manager.js.map +0 -1
- package/dist/runtime/server/proxy.d.ts +0 -21
- package/dist/runtime/server/proxy.js +0 -76
- package/dist/runtime/server/proxy.js.map +0 -1
- package/dist/runtime/server/server.d.ts +0 -60
- package/dist/runtime/server/server.js +0 -163
- package/dist/runtime/server/server.js.map +0 -1
- package/dist/runtime/server/types.d.ts +0 -31
- package/dist/runtime/server/types.js +0 -2
- package/dist/runtime/server/types.js.map +0 -1
- package/dist/runtime/server/worker-pool.d.ts +0 -121
- package/dist/runtime/server/worker-pool.js +0 -171
- package/dist/runtime/server/worker-pool.js.map +0 -1
- package/dist/runtime/store/index.d.ts +0 -3
- package/dist/runtime/store/index.js +0 -23
- package/dist/runtime/store/index.js.map +0 -1
- package/dist/runtime/subscription/manager.d.ts +0 -49
- package/dist/runtime/subscription/manager.js +0 -170
- package/dist/runtime/subscription/manager.js.map +0 -1
- package/dist/runtime/subscription/redis.d.ts +0 -18
- package/dist/runtime/subscription/redis.js +0 -120
- package/dist/runtime/subscription/redis.js.map +0 -1
- package/dist/runtime/types.d.ts +0 -95
- package/dist/runtime/types.js +0 -2
- package/dist/runtime/types.js.map +0 -1
- package/dist/runtime/workers/application.d.ts +0 -29
- package/dist/runtime/workers/application.js +0 -77
- package/dist/runtime/workers/application.js.map +0 -1
- package/dist/runtime/workers/base.d.ts +0 -16
- package/dist/runtime/workers/base.js +0 -46
- package/dist/runtime/workers/base.js.map +0 -1
- package/dist/runtime/workers/cli.d.ts +0 -1
- package/dist/runtime/workers/cli.js +0 -2
- package/dist/runtime/workers/cli.js.map +0 -1
- package/dist/runtime/workers/job.d.ts +0 -20
- package/dist/runtime/workers/job.js +0 -170
- package/dist/runtime/workers/job.js.map +0 -1
- package/dist/vite/builder.d.ts +0 -5
- package/dist/vite/builder.js +0 -79
- package/dist/vite/builder.js.map +0 -1
- package/dist/vite/config.d.ts +0 -28
- package/dist/vite/config.js +0 -22
- package/dist/vite/config.js.map +0 -1
- package/dist/vite/plugins.d.ts +0 -2
- package/dist/vite/plugins.js +0 -43
- package/dist/vite/plugins.js.map +0 -1
- package/dist/vite/runners/worker.d.ts +0 -3
- package/dist/vite/runners/worker.js +0 -34
- package/dist/vite/runners/worker.js.map +0 -1
- package/dist/vite/server.d.ts +0 -3
- package/dist/vite/server.js +0 -36
- package/dist/vite/server.js.map +0 -1
- package/dist/vite/servers/main.d.ts +0 -8
- package/dist/vite/servers/main.js +0 -24
- package/dist/vite/servers/main.js.map +0 -1
- package/dist/vite/servers/worker.d.ts +0 -13
- package/dist/vite/servers/worker.js +0 -124
- package/dist/vite/servers/worker.js.map +0 -1
- package/src/cli.ts +0 -162
- package/src/codegen.ts +0 -30
- package/src/config.ts +0 -69
- package/src/entrypoints/cli.ts +0 -13
- package/src/entrypoints/main.ts +0 -317
- package/src/entrypoints/thread.ts +0 -223
- package/src/entrypoints/worker.ts +0 -174
- package/src/resolver.ts +0 -7
- package/src/runtime/application/index.ts +0 -1
- package/src/runtime/constants.ts +0 -13
- package/src/runtime/enums.ts +0 -12
- package/src/runtime/hooks.ts +0 -1
- package/src/runtime/index.ts +0 -29
- package/src/runtime/injectables.ts +0 -72
- package/src/runtime/jobs/job.ts +0 -467
- package/src/runtime/jobs/manager.ts +0 -454
- package/src/runtime/jobs/router.ts +0 -967
- package/src/runtime/jobs/runner.ts +0 -583
- package/src/runtime/jobs/step.ts +0 -66
- package/src/runtime/jobs/types.ts +0 -116
- package/src/runtime/metrics/metric.ts +0 -18
- package/src/runtime/metrics/registry.ts +0 -11
- package/src/runtime/metrics/server.ts +0 -69
- package/src/runtime/plugin.ts +0 -2
- package/src/runtime/runtime.ts +0 -103
- package/src/runtime/scheduler/index.ts +0 -39
- package/src/runtime/server/applications.ts +0 -264
- package/src/runtime/server/config.ts +0 -199
- package/src/runtime/server/environment.ts +0 -71
- package/src/runtime/server/error-policy.ts +0 -132
- package/src/runtime/server/hmr-coordinator.ts +0 -116
- package/src/runtime/server/jobs.ts +0 -266
- package/src/runtime/server/lifecycle.ts +0 -234
- package/src/runtime/server/managed-worker.ts +0 -546
- package/src/runtime/server/pool-manager.ts +0 -187
- package/src/runtime/server/proxy.ts +0 -119
- package/src/runtime/server/server.ts +0 -218
- package/src/runtime/server/types.ts +0 -32
- package/src/runtime/server/worker-pool.ts +0 -275
- package/src/runtime/store/index.ts +0 -30
- package/src/runtime/subscription/manager.ts +0 -310
- package/src/runtime/subscription/redis.ts +0 -157
- package/src/runtime/types.ts +0 -84
- package/src/runtime/workers/application.ts +0 -133
- package/src/runtime/workers/base.ts +0 -71
- package/src/runtime/workers/cli.ts +0 -0
- package/src/runtime/workers/job.ts +0 -142
- package/src/vite/builder.ts +0 -94
- package/src/vite/config.ts +0 -45
- package/src/vite/plugins.ts +0 -53
- package/src/vite/runners/worker.ts +0 -55
- package/src/vite/server.ts +0 -55
- package/src/vite/servers/main.ts +0 -41
- package/src/vite/servers/worker.ts +0 -177
package/src/vite/config.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
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
|
-
new URL(`../entrypoints/main${ext}`, import.meta.url),
|
|
21
|
-
),
|
|
22
|
-
entrypointWorkerPath: fileURLToPath(
|
|
23
|
-
new URL(`../entrypoints/worker${ext}`, import.meta.url),
|
|
24
|
-
),
|
|
25
|
-
entrypointThreadPath: fileURLToPath(
|
|
26
|
-
new URL(`../entrypoints/thread${ext}`, import.meta.url),
|
|
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
|
-
}
|
package/src/vite/plugins.ts
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs'
|
|
2
|
-
import { readFile } from 'node:fs/promises'
|
|
3
|
-
import { basename, dirname, join } from 'node:path'
|
|
4
|
-
|
|
5
|
-
import type { Plugin } from 'vite'
|
|
6
|
-
|
|
7
|
-
export const plugins: Plugin[] = [
|
|
8
|
-
{
|
|
9
|
-
name: 'neemata:native-addon',
|
|
10
|
-
apply: 'build',
|
|
11
|
-
enforce: 'pre',
|
|
12
|
-
|
|
13
|
-
async load(id) {
|
|
14
|
-
if (id.endsWith('.node') && existsSync(id)) {
|
|
15
|
-
const refId = this.emitFile({
|
|
16
|
-
type: 'asset',
|
|
17
|
-
name: basename(id),
|
|
18
|
-
source: await readFile(id),
|
|
19
|
-
})
|
|
20
|
-
const runtimePath = `./${this.getFileName(refId)}`
|
|
21
|
-
return (
|
|
22
|
-
`const id = ${JSON.stringify(runtimePath)};` +
|
|
23
|
-
`export default require(id);`
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
return null
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: 'neemata:uws-native-addon',
|
|
31
|
-
apply: 'build',
|
|
32
|
-
enforce: 'post',
|
|
33
|
-
async load(id) {
|
|
34
|
-
if (id.includes('uWebSockets.js/uws.js')) {
|
|
35
|
-
const uwsNodeAddonPath = join(
|
|
36
|
-
dirname(id),
|
|
37
|
-
`uws_${process.platform}_${process.arch}_${process.versions.modules}.node`,
|
|
38
|
-
)
|
|
39
|
-
const refId = this.emitFile({
|
|
40
|
-
type: 'asset',
|
|
41
|
-
name: basename(uwsNodeAddonPath),
|
|
42
|
-
source: await readFile(uwsNodeAddonPath),
|
|
43
|
-
})
|
|
44
|
-
const runtimePath = `./${this.getFileName(refId)}`
|
|
45
|
-
return (
|
|
46
|
-
`const id = ${JSON.stringify(runtimePath)};` +
|
|
47
|
-
`export default require(id);`
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
|
-
return null
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
]
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { MessagePort } from 'node:worker_threads'
|
|
2
|
-
import wt, { isMainThread } from 'node:worker_threads'
|
|
3
|
-
|
|
4
|
-
import type { ModuleRunnerTransport } from 'vite/module-runner'
|
|
5
|
-
import { noopFn } from '@nmtjs/common'
|
|
6
|
-
import {
|
|
7
|
-
createNodeImportMeta,
|
|
8
|
-
ESModulesEvaluator,
|
|
9
|
-
ModuleRunner,
|
|
10
|
-
} from 'vite/module-runner'
|
|
11
|
-
|
|
12
|
-
export function createModuleRunner(
|
|
13
|
-
vitePort: MessagePort | undefined,
|
|
14
|
-
mode: 'development' | 'production' = 'development',
|
|
15
|
-
timeoutMs = 5000,
|
|
16
|
-
): ModuleRunner {
|
|
17
|
-
// TODO: bun does not support isInternalThread yet
|
|
18
|
-
if (isMainThread || wt.isInternalThread)
|
|
19
|
-
throw new Error('Module runner can only be created inside worker threads.')
|
|
20
|
-
|
|
21
|
-
if (!vitePort) {
|
|
22
|
-
throw new Error('Module runner requires a dedicated Vite message port.')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const transportTimeout =
|
|
26
|
-
Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 5000
|
|
27
|
-
|
|
28
|
-
const transport: ModuleRunnerTransport = {
|
|
29
|
-
connect({ onMessage, onDisconnection }) {
|
|
30
|
-
vitePort.on('message', onMessage)
|
|
31
|
-
vitePort.on('close', onDisconnection)
|
|
32
|
-
},
|
|
33
|
-
send(data) {
|
|
34
|
-
vitePort.postMessage(data)
|
|
35
|
-
},
|
|
36
|
-
disconnect() {
|
|
37
|
-
vitePort.close()
|
|
38
|
-
},
|
|
39
|
-
timeout: transportTimeout,
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const runner = new ModuleRunner(
|
|
43
|
-
{
|
|
44
|
-
transport,
|
|
45
|
-
createImportMeta: createNodeImportMeta,
|
|
46
|
-
hmr:
|
|
47
|
-
mode === 'development'
|
|
48
|
-
? { logger: { debug: noopFn, error: console.error } }
|
|
49
|
-
: false,
|
|
50
|
-
},
|
|
51
|
-
new ESModulesEvaluator(),
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
return runner
|
|
55
|
-
}
|
package/src/vite/server.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import type { DevEnvironmentOptions, UserConfig } from 'vite'
|
|
2
|
-
import { createServer as createViteServer } from 'vite'
|
|
3
|
-
|
|
4
|
-
import type { ViteConfigOptions } from './config.ts'
|
|
5
|
-
|
|
6
|
-
// Packages that must NOT be externalized to prevent module duplication.
|
|
7
|
-
// When externalized, Vite's ModuleRunner uses native Node import() which has
|
|
8
|
-
// a separate module cache from Vite's cache, causing the same module to be
|
|
9
|
-
// loaded twice with different object references.
|
|
10
|
-
const noExternalPackages = ['nmtjs', /^@nmtjs\/(?!prom-client$)/]
|
|
11
|
-
|
|
12
|
-
export function createServer(
|
|
13
|
-
options: ViteConfigOptions,
|
|
14
|
-
config: UserConfig,
|
|
15
|
-
dev: DevEnvironmentOptions = {},
|
|
16
|
-
) {
|
|
17
|
-
const usePolling = !!process.env.CI
|
|
18
|
-
|
|
19
|
-
return createViteServer({
|
|
20
|
-
...config,
|
|
21
|
-
server: {
|
|
22
|
-
middlewareMode: true,
|
|
23
|
-
ws: false,
|
|
24
|
-
watch: usePolling ? { usePolling: true } : undefined,
|
|
25
|
-
},
|
|
26
|
-
resolve: { tsconfigPaths: true, ...config.resolve },
|
|
27
|
-
environments: {
|
|
28
|
-
neemata: {
|
|
29
|
-
consumer: 'server',
|
|
30
|
-
dev,
|
|
31
|
-
resolve: {
|
|
32
|
-
// Ensure nmtjs packages are not externalized in this environment
|
|
33
|
-
noExternal: noExternalPackages,
|
|
34
|
-
},
|
|
35
|
-
define: {
|
|
36
|
-
__VITE_CONFIG__: JSON.stringify(
|
|
37
|
-
JSON.stringify({ options, mode: config.mode }),
|
|
38
|
-
),
|
|
39
|
-
__APPLICATIONS_CONFIG__: JSON.stringify(
|
|
40
|
-
JSON.stringify(
|
|
41
|
-
Object.fromEntries(
|
|
42
|
-
Object.entries(options.applicationImports).map(
|
|
43
|
-
([appName, { path, type }]) => [
|
|
44
|
-
appName,
|
|
45
|
-
{ specifier: path, type },
|
|
46
|
-
],
|
|
47
|
-
),
|
|
48
|
-
),
|
|
49
|
-
),
|
|
50
|
-
),
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
})
|
|
55
|
-
}
|
package/src/vite/servers/main.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import type { UserConfig, ViteDevServer } from 'vite'
|
|
2
|
-
import type { ModuleRunner } from 'vite/module-runner'
|
|
3
|
-
import { noopFn } from '@nmtjs/common'
|
|
4
|
-
import { createServerModuleRunner, mergeConfig } 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 { plugins } from '../plugins.ts'
|
|
10
|
-
import { createServer } from '../server.ts'
|
|
11
|
-
|
|
12
|
-
export async function createMainServer(
|
|
13
|
-
options: ViteConfigOptions,
|
|
14
|
-
mode: 'development' | 'production',
|
|
15
|
-
{ vite }: NeemataConfig,
|
|
16
|
-
): Promise<{ server: ViteDevServer; runner: ModuleRunner }> {
|
|
17
|
-
const config = createConfig(options)
|
|
18
|
-
const server = await createServer(
|
|
19
|
-
options,
|
|
20
|
-
mergeConfig(
|
|
21
|
-
{
|
|
22
|
-
appType: 'custom',
|
|
23
|
-
clearScreen: false,
|
|
24
|
-
resolve: { alias: config.alias, external: ['@nmtjs/proxy'] },
|
|
25
|
-
mode,
|
|
26
|
-
plugins: [...plugins],
|
|
27
|
-
optimizeDeps: { noDiscovery: true },
|
|
28
|
-
} satisfies UserConfig,
|
|
29
|
-
vite,
|
|
30
|
-
),
|
|
31
|
-
)
|
|
32
|
-
const environment = server.environments.neemata
|
|
33
|
-
|
|
34
|
-
const runner = createServerModuleRunner(environment, {
|
|
35
|
-
hmr:
|
|
36
|
-
mode === 'development'
|
|
37
|
-
? { logger: { debug: noopFn, error: console.error } }
|
|
38
|
-
: false,
|
|
39
|
-
})
|
|
40
|
-
return { server, runner }
|
|
41
|
-
}
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
import type EventEmitter from 'node:events'
|
|
2
|
-
import type { MessagePort, Worker } from 'node:worker_threads'
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
HotChannel,
|
|
6
|
-
HotChannelClient,
|
|
7
|
-
HotChannelListener,
|
|
8
|
-
HotPayload,
|
|
9
|
-
Plugin,
|
|
10
|
-
UserConfig,
|
|
11
|
-
ViteDevServer,
|
|
12
|
-
} from 'vite'
|
|
13
|
-
import { DevEnvironment, mergeConfig } from 'vite'
|
|
14
|
-
|
|
15
|
-
import type { NeemataConfig } from '../../config.ts'
|
|
16
|
-
import type { WorkerRegistration } from '../../runtime/server/types.ts'
|
|
17
|
-
import type { ViteConfigOptions } from '../config.ts'
|
|
18
|
-
import { createConfig } from '../config.ts'
|
|
19
|
-
import { plugins } from '../plugins.ts'
|
|
20
|
-
import { createServer } from '../server.ts'
|
|
21
|
-
|
|
22
|
-
export type WorkerServerEventMap = {
|
|
23
|
-
worker: [WorkerRegistration]
|
|
24
|
-
/** Emitted when an HMR update occurs for an application file (useful for restarting dead workers) */
|
|
25
|
-
'hmr-update': [{ file: string }]
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export async function createViteServer(
|
|
29
|
-
options: ViteConfigOptions,
|
|
30
|
-
mode: 'development' | 'production',
|
|
31
|
-
neemataConfig: NeemataConfig,
|
|
32
|
-
events: EventEmitter<WorkerServerEventMap>,
|
|
33
|
-
): Promise<ViteDevServer> {
|
|
34
|
-
const config = createConfig(options)
|
|
35
|
-
const applicationEntries = Object.values(options.applicationImports).map(
|
|
36
|
-
(v) => v.path,
|
|
37
|
-
)
|
|
38
|
-
|
|
39
|
-
const _injectHmr = `\n\nif(import.meta.hot) { import.meta.hot.accept((module) => globalThis._hotAccept?.(module)) }`
|
|
40
|
-
|
|
41
|
-
const server = await createServer(
|
|
42
|
-
options,
|
|
43
|
-
mergeConfig(neemataConfig.vite, {
|
|
44
|
-
appType: 'custom',
|
|
45
|
-
clearScreen: false,
|
|
46
|
-
resolve: { alias: config.alias, external: ['@nmtjs/proxy'] },
|
|
47
|
-
mode,
|
|
48
|
-
optimizeDeps: { noDiscovery: true },
|
|
49
|
-
plugins: [
|
|
50
|
-
...plugins,
|
|
51
|
-
...(mode === 'development'
|
|
52
|
-
? [
|
|
53
|
-
{
|
|
54
|
-
name: 'neemata-worker-application-hmr',
|
|
55
|
-
transform(code, id, _options) {
|
|
56
|
-
if (applicationEntries.includes(id)) {
|
|
57
|
-
return code + _injectHmr
|
|
58
|
-
}
|
|
59
|
-
},
|
|
60
|
-
handleHotUpdate(ctx) {
|
|
61
|
-
// Emit event when application entry files change
|
|
62
|
-
// This allows restarting failed workers after code fixes
|
|
63
|
-
if (ctx.file && applicationEntries.includes(ctx.file)) {
|
|
64
|
-
events.emit('hmr-update', { file: ctx.file })
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
} satisfies Plugin,
|
|
68
|
-
]
|
|
69
|
-
: []),
|
|
70
|
-
],
|
|
71
|
-
} satisfies UserConfig),
|
|
72
|
-
{
|
|
73
|
-
createEnvironment: async (name, config, context) => {
|
|
74
|
-
const connections = new Map<
|
|
75
|
-
number,
|
|
76
|
-
{ worker: Worker; port: MessagePort; client: HotChannelClient }
|
|
77
|
-
>()
|
|
78
|
-
const clients = new Map<number, HotChannelClient>()
|
|
79
|
-
const handlers = new Map<string, Set<Function>>()
|
|
80
|
-
|
|
81
|
-
const emit = (
|
|
82
|
-
event: string,
|
|
83
|
-
data: unknown,
|
|
84
|
-
client: HotChannelClient,
|
|
85
|
-
) => {
|
|
86
|
-
const listeners = handlers.get(event)
|
|
87
|
-
if (!listeners?.size) return
|
|
88
|
-
for (const listener of listeners) {
|
|
89
|
-
;(listener as HotChannelListener)(data, client)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
events.on('worker', ({ worker, vitePort }) => {
|
|
94
|
-
if (!vitePort) return
|
|
95
|
-
|
|
96
|
-
const client: HotChannelClient = {
|
|
97
|
-
send: (payload: HotPayload) => {
|
|
98
|
-
vitePort.postMessage(payload)
|
|
99
|
-
},
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const handleMessage = (value: HotPayload) => {
|
|
103
|
-
if (value.type === 'custom') {
|
|
104
|
-
emit(value.event, value.data, client)
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
let disconnected = false
|
|
109
|
-
const disconnect = (closePort: boolean) => {
|
|
110
|
-
if (disconnected) return
|
|
111
|
-
disconnected = true
|
|
112
|
-
|
|
113
|
-
emit('vite:client:disconnect', undefined, client)
|
|
114
|
-
|
|
115
|
-
vitePort.off('message', handleMessage)
|
|
116
|
-
worker.off('exit', handleWorkerExit)
|
|
117
|
-
vitePort.off('close', handlePortClose)
|
|
118
|
-
|
|
119
|
-
if (closePort) {
|
|
120
|
-
vitePort.close()
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
connections.delete(worker.threadId)
|
|
124
|
-
clients.delete(worker.threadId)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const handleWorkerExit = () => disconnect(true)
|
|
128
|
-
const handlePortClose = () => disconnect(false)
|
|
129
|
-
|
|
130
|
-
vitePort.on('message', handleMessage)
|
|
131
|
-
worker.once('exit', handleWorkerExit)
|
|
132
|
-
vitePort.once('close', handlePortClose)
|
|
133
|
-
|
|
134
|
-
connections.set(worker.threadId, { worker, port: vitePort, client })
|
|
135
|
-
clients.set(worker.threadId, client)
|
|
136
|
-
|
|
137
|
-
emit('vite:client:connect', undefined, client)
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
const transport: HotChannel = {
|
|
141
|
-
send(data) {
|
|
142
|
-
for (const connection of connections.values()) {
|
|
143
|
-
connection.port.postMessage(data)
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
on(event, handler) {
|
|
147
|
-
let listeners = handlers.get(event)
|
|
148
|
-
if (!listeners) {
|
|
149
|
-
listeners = new Set()
|
|
150
|
-
handlers.set(event, listeners)
|
|
151
|
-
}
|
|
152
|
-
listeners.add(handler)
|
|
153
|
-
},
|
|
154
|
-
off(event, handler) {
|
|
155
|
-
if (!handler) {
|
|
156
|
-
handlers.delete(event)
|
|
157
|
-
return
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const listeners = handlers.get(event)
|
|
161
|
-
if (!listeners) return
|
|
162
|
-
listeners.delete(handler)
|
|
163
|
-
if (!listeners.size) {
|
|
164
|
-
handlers.delete(event)
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return new DevEnvironment(name, config, {
|
|
170
|
-
hot: mode === 'development',
|
|
171
|
-
transport,
|
|
172
|
-
})
|
|
173
|
-
},
|
|
174
|
-
},
|
|
175
|
-
)
|
|
176
|
-
return server
|
|
177
|
-
}
|