phecda-server 7.0.0-alpha.8 → 7.0.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 (154) hide show
  1. package/README.md +10 -10
  2. package/assets/schema.json +12 -9
  3. package/bin/cli.mjs +96 -37
  4. package/dist/chunk-5SUY5UVC.js +258 -0
  5. package/dist/chunk-5SUY5UVC.js.map +1 -0
  6. package/dist/chunk-6JAHCTOT.mjs +334 -0
  7. package/dist/chunk-6JAHCTOT.mjs.map +1 -0
  8. package/dist/{chunk-BBNJVYVX.js → chunk-EDAWJVJ4.js} +42 -25
  9. package/dist/chunk-EDAWJVJ4.js.map +1 -0
  10. package/dist/{chunk-7EMAZGYO.js → chunk-H7OCHDZT.js} +65 -95
  11. package/dist/chunk-H7OCHDZT.js.map +1 -0
  12. package/dist/{chunk-QJIAXSLZ.js → chunk-KRUOHWFR.js} +79 -68
  13. package/dist/chunk-KRUOHWFR.js.map +1 -0
  14. package/dist/chunk-KUGU3UGO.js +334 -0
  15. package/dist/chunk-KUGU3UGO.js.map +1 -0
  16. package/dist/{chunk-SGMQZRP6.mjs → chunk-LBH4RXQQ.mjs} +12 -42
  17. package/dist/chunk-LBH4RXQQ.mjs.map +1 -0
  18. package/dist/{chunk-YKP4ODZA.mjs → chunk-MDPW6WGF.mjs} +32 -6
  19. package/dist/chunk-MDPW6WGF.mjs.map +1 -0
  20. package/dist/{chunk-VSGYTGHY.js → chunk-RLFCI3RG.js} +31 -5
  21. package/dist/chunk-RLFCI3RG.js.map +1 -0
  22. package/dist/{chunk-7NWDKK7D.mjs → chunk-TTHYUNOT.mjs} +18 -1
  23. package/dist/chunk-TTHYUNOT.mjs.map +1 -0
  24. package/dist/chunk-VDOICQ2U.mjs +258 -0
  25. package/dist/chunk-VDOICQ2U.mjs.map +1 -0
  26. package/dist/{chunk-B45BN7LY.mjs → chunk-WTJKTICN.mjs} +65 -54
  27. package/dist/chunk-WTJKTICN.mjs.map +1 -0
  28. package/dist/{core-BGEsBtTG.d.ts → core-BOo3uGFX.d.ts} +5 -4
  29. package/dist/{core-BcKywWLd.d.mts → core-CF45G79J.d.mts} +5 -4
  30. package/dist/core-DbqWh47p.d.mts +50 -0
  31. package/dist/core-Rq7BhPHE.d.ts +50 -0
  32. package/dist/helper.d.mts +7 -8
  33. package/dist/helper.d.ts +7 -8
  34. package/dist/helper.js +5 -3
  35. package/dist/helper.js.map +1 -0
  36. package/dist/helper.mjs +9 -7
  37. package/dist/helper.mjs.map +1 -0
  38. package/dist/http/elysia/index.d.mts +4 -4
  39. package/dist/http/elysia/index.d.ts +4 -4
  40. package/dist/http/elysia/index.js +43 -45
  41. package/dist/http/elysia/index.js.map +1 -0
  42. package/dist/http/elysia/index.mjs +6 -8
  43. package/dist/http/elysia/index.mjs.map +1 -0
  44. package/dist/http/express/index.d.mts +3 -3
  45. package/dist/http/express/index.d.ts +3 -3
  46. package/dist/http/express/index.js +40 -42
  47. package/dist/http/express/index.js.map +1 -0
  48. package/dist/http/express/index.mjs +6 -8
  49. package/dist/http/express/index.mjs.map +1 -0
  50. package/dist/http/fastify/index.d.mts +3 -3
  51. package/dist/http/fastify/index.d.ts +3 -3
  52. package/dist/http/fastify/index.js +41 -43
  53. package/dist/http/fastify/index.js.map +1 -0
  54. package/dist/http/fastify/index.mjs +6 -8
  55. package/dist/http/fastify/index.mjs.map +1 -0
  56. package/dist/http/h3/index.d.mts +3 -3
  57. package/dist/http/h3/index.d.ts +3 -3
  58. package/dist/http/h3/index.js +41 -43
  59. package/dist/http/h3/index.js.map +1 -0
  60. package/dist/http/h3/index.mjs +5 -7
  61. package/dist/http/h3/index.mjs.map +1 -0
  62. package/dist/http/hono/index.d.mts +3 -3
  63. package/dist/http/hono/index.d.ts +3 -3
  64. package/dist/http/hono/index.js +37 -39
  65. package/dist/http/hono/index.js.map +1 -0
  66. package/dist/http/hono/index.mjs +5 -7
  67. package/dist/http/hono/index.mjs.map +1 -0
  68. package/dist/http/hyper-express/index.d.mts +3 -3
  69. package/dist/http/hyper-express/index.d.ts +3 -3
  70. package/dist/http/hyper-express/index.js +38 -40
  71. package/dist/http/hyper-express/index.js.map +1 -0
  72. package/dist/http/hyper-express/index.mjs +5 -7
  73. package/dist/http/hyper-express/index.mjs.map +1 -0
  74. package/dist/http/koa/index.d.mts +3 -3
  75. package/dist/http/koa/index.d.ts +3 -3
  76. package/dist/http/koa/index.js +40 -42
  77. package/dist/http/koa/index.js.map +1 -0
  78. package/dist/http/koa/index.mjs +6 -8
  79. package/dist/http/koa/index.mjs.map +1 -0
  80. package/dist/index.d.mts +76 -12
  81. package/dist/index.d.ts +76 -12
  82. package/dist/index.js +103 -49
  83. package/dist/index.js.map +1 -0
  84. package/dist/index.mjs +68 -14
  85. package/dist/index.mjs.map +1 -0
  86. package/dist/{meta-B_HW7cWh.d.ts → meta-BoS1E-Nz.d.mts} +6 -24
  87. package/dist/{meta-B_HW7cWh.d.mts → meta-BoS1E-Nz.d.ts} +6 -24
  88. package/dist/rpc/bullmq/index.d.mts +4 -6
  89. package/dist/rpc/bullmq/index.d.ts +4 -6
  90. package/dist/rpc/bullmq/index.js +13 -80
  91. package/dist/rpc/bullmq/index.js.map +1 -0
  92. package/dist/rpc/bullmq/index.mjs +6 -73
  93. package/dist/rpc/bullmq/index.mjs.map +1 -0
  94. package/dist/rpc/electron/index.d.mts +13 -0
  95. package/dist/rpc/electron/index.d.ts +13 -0
  96. package/dist/rpc/electron/index.js +73 -0
  97. package/dist/rpc/electron/index.js.map +1 -0
  98. package/dist/rpc/electron/index.mjs +73 -0
  99. package/dist/rpc/electron/index.mjs.map +1 -0
  100. package/dist/rpc/kafka/index.d.mts +4 -9
  101. package/dist/rpc/kafka/index.d.ts +4 -9
  102. package/dist/rpc/kafka/index.js +12 -90
  103. package/dist/rpc/kafka/index.js.map +1 -0
  104. package/dist/rpc/kafka/index.mjs +5 -83
  105. package/dist/rpc/kafka/index.mjs.map +1 -0
  106. package/dist/rpc/nats/index.d.mts +4 -6
  107. package/dist/rpc/nats/index.d.ts +4 -6
  108. package/dist/rpc/nats/index.js +13 -72
  109. package/dist/rpc/nats/index.js.map +1 -0
  110. package/dist/rpc/nats/index.mjs +5 -64
  111. package/dist/rpc/nats/index.mjs.map +1 -0
  112. package/dist/rpc/rabbitmq/index.d.mts +4 -6
  113. package/dist/rpc/rabbitmq/index.d.ts +4 -6
  114. package/dist/rpc/rabbitmq/index.js +14 -79
  115. package/dist/rpc/rabbitmq/index.js.map +1 -0
  116. package/dist/rpc/rabbitmq/index.mjs +6 -71
  117. package/dist/rpc/rabbitmq/index.mjs.map +1 -0
  118. package/dist/rpc/redis/index.d.mts +4 -9
  119. package/dist/rpc/redis/index.d.ts +4 -9
  120. package/dist/rpc/redis/index.js +12 -78
  121. package/dist/rpc/redis/index.js.map +1 -0
  122. package/dist/rpc/redis/index.mjs +6 -72
  123. package/dist/rpc/redis/index.mjs.map +1 -0
  124. package/dist/rpc/web-ext/index.d.mts +2 -0
  125. package/dist/rpc/web-ext/index.d.ts +2 -0
  126. package/dist/rpc/web-ext/index.js +1 -0
  127. package/dist/rpc/web-ext/index.js.map +1 -0
  128. package/dist/rpc/web-ext/index.mjs +1 -0
  129. package/dist/rpc/web-ext/index.mjs.map +1 -0
  130. package/dist/rpc/ws/index.d.mts +14 -0
  131. package/dist/rpc/ws/index.d.ts +14 -0
  132. package/dist/rpc/ws/index.js +74 -0
  133. package/dist/rpc/ws/index.js.map +1 -0
  134. package/dist/rpc/ws/index.mjs +74 -0
  135. package/dist/rpc/ws/index.mjs.map +1 -0
  136. package/dist/test.d.mts +8 -4
  137. package/dist/test.d.ts +8 -4
  138. package/dist/test.js +7 -6
  139. package/dist/test.js.map +1 -0
  140. package/dist/test.mjs +3 -2
  141. package/dist/test.mjs.map +1 -0
  142. package/dist/types-CInz3bD0.d.ts +14 -0
  143. package/dist/types-E-rqlg3-.d.mts +14 -0
  144. package/dist/{types-C2SQ9U9V.d.mts → types-SCJZFCYt.d.mts} +1 -1
  145. package/dist/{types-PFPIQWVF.d.ts → types-xruyYmyu.d.ts} +1 -1
  146. package/package.json +24 -8
  147. package/register/export.mjs +30 -0
  148. package/register/index.mjs +20 -12
  149. package/register/loader.mjs +94 -116
  150. package/register/utils.mjs +36 -6
  151. package/assets/ps.json +0 -24
  152. package/assets/tsconfig.json +0 -21
  153. package/dist/types-CYF0vha6.d.ts +0 -19
  154. package/dist/types-DEQb_e59.d.mts +0 -19
@@ -0,0 +1,14 @@
1
+ import { B as BaseCtx, D as DefaultOptions } from './meta-BoS1E-Nz.mjs';
2
+
3
+ interface RpcServerOptions extends DefaultOptions {
4
+ defaultQueue?: string;
5
+ }
6
+ interface RpcCtx extends BaseCtx {
7
+ args: any[];
8
+ id: string;
9
+ queue: string;
10
+ isEvent?: boolean;
11
+ category: 'rpc';
12
+ }
13
+
14
+ export type { RpcCtx as R, RpcServerOptions as a };
@@ -1,5 +1,5 @@
1
1
  import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
2
- import { B as BaseCtx, D as DefaultOptions } from './meta-B_HW7cWh.mjs';
2
+ import { B as BaseCtx, D as DefaultOptions } from './meta-BoS1E-Nz.mjs';
3
3
 
4
4
  interface HttpOptions extends DefaultOptions {
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'node:http';
2
- import { B as BaseCtx, D as DefaultOptions } from './meta-B_HW7cWh.js';
2
+ import { B as BaseCtx, D as DefaultOptions } from './meta-BoS1E-Nz.js';
3
3
 
4
4
  interface HttpOptions extends DefaultOptions {
5
5
  /**
package/package.json CHANGED
@@ -1,21 +1,21 @@
1
1
  {
2
2
  "name": "phecda-server",
3
- "version": "7.0.0-alpha.8",
3
+ "version": "7.0.0",
4
4
  "description": "server framework that provide IOC/type-reuse/http&rpc-adaptor",
5
5
  "author": "fgsreally",
6
6
  "license": "MIT",
7
7
  "repository": "https://github.com/fgsreally/phecda/tree/main/packages/server",
8
8
  "keywords": [
9
9
  "server",
10
+ "http",
10
11
  "IOC",
11
12
  "rpc",
12
- "types-share"
13
+ "types-safe"
13
14
  ],
14
15
  "exports": {
15
16
  ".": {
16
17
  "require": "./dist/index.js",
17
- "import": "./dist/index.mjs",
18
- "node": "./dist/index.js"
18
+ "import": "./dist/index.mjs"
19
19
  },
20
20
  "./helper": {
21
21
  "require": "./dist/helper.js",
@@ -73,6 +73,18 @@
73
73
  "require": "./dist/rpc/nats/index.js",
74
74
  "import": "./dist/rpc/nats/index.mjs"
75
75
  },
76
+ "./electron": {
77
+ "require": "./dist/rpc/electron/index.js",
78
+ "import": "./dist/rpc/electron/index.mjs"
79
+ },
80
+ "./ws": {
81
+ "require": "./dist/rpc/ws/index.js",
82
+ "import": "./dist/rpc/ws/index.mjs"
83
+ },
84
+ "./web-ext": {
85
+ "require": "./dist/rpc/web-ext/index.js",
86
+ "import": "./dist/rpc/web-ext/index.mjs"
87
+ },
76
88
  "./register": "./register/index.mjs",
77
89
  "./*": "./*"
78
90
  },
@@ -144,27 +156,30 @@
144
156
  "chokidar": "^3.5.3",
145
157
  "debug": "^4.3.4",
146
158
  "fs-extra": "^11.2.0",
147
- "h3": "^1.12.0",
148
159
  "picocolors": "^1.0.0",
149
160
  "reflect-metadata": "^0.1.13",
150
161
  "ts-mixer": "^6.0.4",
151
- "phecda-core": "4.1.0-alpha.0"
162
+ "phecda-core": "4.1.0"
152
163
  },
153
164
  "devDependencies": {
154
165
  "@koa/router": "^12.0.1",
155
166
  "@nucleoidjs/kafkajs-mock": "^0.0.1",
156
167
  "@types/amqplib": "^0.10.1",
168
+ "@types/chrome": "^0.0.309",
157
169
  "@types/debug": "^4.1.12",
158
170
  "@types/express": "^4.17.17",
159
171
  "@types/fs-extra": "^11.0.4",
160
172
  "@types/koa": "^2.13.12",
161
173
  "@types/koa__router": "^12.0.4",
162
174
  "@types/supertest": "^2.0.12",
175
+ "@types/ws": "^8.5.13",
163
176
  "amqplib": "^0.10.3",
164
177
  "bullmq": "^5.7.1",
178
+ "electron": "^33.2.1",
165
179
  "elysia": "^1.0.17",
166
180
  "express": "^4.18.2",
167
181
  "fastify": "^4.25.1",
182
+ "h3": "^1.12.0",
168
183
  "hono": "^4.3.4",
169
184
  "hyper-express": "^6.15.2",
170
185
  "ioredis": "^5.3.2",
@@ -176,8 +191,9 @@
176
191
  "nats": "^2.22.0",
177
192
  "supertest": "^6.3.3",
178
193
  "tsup": "^8.1.0",
179
- "typescript": "^5.4.5",
180
- "unimport": "^3.7.1"
194
+ "typescript": "^5.8.3",
195
+ "unimport": "^3.7.1",
196
+ "ws": "^8.18.0"
181
197
  },
182
198
  "scripts": {
183
199
  "dev": "tsup --watch",
@@ -0,0 +1,30 @@
1
+ export default ['Addon', 'Arg', 'Assign', 'BadGatewayException', 'BadRequestException',
2
+ 'Base', 'Body', 'Clear',
3
+ 'ConflictException', 'Controller',
4
+ 'Ctx', 'CustomResponse',
5
+ 'Delete',
6
+ 'Define',
7
+ 'ERROR_SYMBOL',
8
+ 'Effect', 'Empty', 'Err', 'Exception', 'Expose', 'Factory', 'Filter', 'ForbiddenException',
9
+ 'FrameworkException',
10
+ 'Generator', 'Get', 'Global', 'Guard', 'HTTPGenerator', 'Head', 'Header', 'HttpBase',
11
+ 'If', 'Init', 'Inject', 'Injectable',
12
+ 'InvalidInputException', 'Isolate',
13
+ 'Mixin',
14
+ 'NotFoundException', 'PAddon', 'PExtension', 'PFilter', 'PGuard',
15
+ 'PPipe', 'Param', 'Patch', 'PayloadLargeException', 'Pipe', 'Pipeline', 'Post',
16
+ 'Provide', 'Put', 'Query', 'Queue', 'RPCGenerator', 'Route',
17
+ 'Rpc', 'RpcBase', 'Search', 'ServerBase', 'ServerPhecda', 'ServiceUnavailableException', 'Storage',
18
+ 'Tag', 'TimeoutException', 'TimerException', 'UnauthorizedException', 'UndefinedException',
19
+ 'Unique', 'Unmount', 'UnsupportedMediaTypeException', 'ValidateException',
20
+ 'Watcher', 'WorkerException',
21
+ 'addAddon', 'addDecoToClass',
22
+ 'addFilter', 'addGuard', 'addPipe', 'emitter',
23
+ 'getMergedMeta',
24
+ 'getMeta', 'getMetaKey', 'getMetaParams', 'getOwnMeta', 'getOwnMetaKey', 'getOwnMetaParams',
25
+ 'getTag',
26
+ 'isPhecda', 'log', 'phecdaNamespace', 'runMiddleware',
27
+ 'setInject',
28
+ 'setLogger',
29
+ 'setMeta',
30
+ 'useS', 'wait', 'OneFile', 'ManyFiles']
@@ -1,7 +1,7 @@
1
1
  import { register } from 'node:module'
2
2
  import { MessageChannel } from 'node:worker_threads'
3
- import { log } from '../dist/index.mjs'
4
-
3
+ import { isPhecda, log } from '../dist/index.mjs'
4
+ import { RELAUNCH, RELOAD } from '../dist/helper.mjs'
5
5
  const { port1, port2 } = new MessageChannel()
6
6
 
7
7
  register('./loader.mjs', {
@@ -12,18 +12,26 @@ register('./loader.mjs', {
12
12
 
13
13
  let isRunning = true
14
14
 
15
+ const fileModelMap = new Map()
16
+
15
17
  port1.on('message', async (data) => {
16
18
  const { type, files } = JSON.parse(data)
17
-
18
- if (!isRunning || !globalThis.__PS_HMR__ || type === 'relaunch')
19
- return process.exit(2)// file change -> relaunch
20
-
21
- if (type === 'change') {
22
- log('reload module...')
23
-
24
- for (const cb of globalThis.__PS_HMR__) await cb(files)
25
-
26
- log('reload done')
19
+ if ((!isRunning && type !== 'init') || type === 'relaunch')
20
+ return RELAUNCH()
21
+
22
+ if (type === 'change' || type === 'init') {
23
+ if (!files.length)
24
+ return
25
+ const oldModels = []
26
+ const newModels = []
27
+ for (const file of files) {
28
+ oldModels.push(...fileModelMap.get(file) || [])
29
+ const models = Object.values(await import(file)).filter(isPhecda)
30
+ fileModelMap.set(file, models)
31
+ newModels.push(...models)
32
+ }
33
+ if (type === 'change')
34
+ return RELOAD(oldModels, newModels)
27
35
  }
28
36
  })
29
37
 
@@ -3,31 +3,45 @@ import { writeFile } from 'fs/promises'
3
3
  import {
4
4
  basename,
5
5
  dirname,
6
- extname,
7
6
  isAbsolute,
8
7
  relative,
9
8
  resolve as resolvePath,
10
9
  } from 'path'
11
10
  import { createRequire } from 'module'
12
- import { existsSync } from 'fs'
13
11
  import ts from 'typescript'
14
12
  import chokidar from 'chokidar'
15
- import { IS_DEV, log } from '../dist/index.mjs'
13
+ import Debug from 'debug'
16
14
  import { compile, genUnImportRet, handleClassTypes, slash } from './utils.mjs'
17
15
 
16
+ const require = createRequire(import.meta.url)
17
+ const debug = Debug('phecda-server/loader')
18
+
19
+ const isLowVersion = parseFloat(process.version.slice(1)) < 18.19
20
+ const IS_DEV = process.env.NODE_ENV === 'development'
18
21
  let port
22
+ let config
23
+ const workdir = process.cwd()
24
+ const configPath = resolvePath(
25
+ workdir,
26
+ process.env.PS_CONFIG_FILE || 'ps.json',
27
+ )
28
+
29
+ // unimport
30
+ let unimportRet, customLoad, customResolve
31
+ const dtsPath = process.env.PS_DTS_PATH || 'ps.d.ts'
32
+
33
+ // graph
34
+ const watchFiles = new Set()
35
+ const filesRecord = new Map()
36
+ const moduleGraph = {}
37
+ // ts
19
38
  let tsconfig = {
20
39
  module: ts.ModuleKind.ESNext,
21
40
  moduleResolution: ts.ModuleResolutionKind.NodeNext,
22
41
  }
23
-
24
- const isLowVersion = parseFloat(process.version.slice(1)) < 18.19
25
- // this part is important or not?
26
42
  const EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts]
27
-
28
43
  const tsconfigPath = resolvePath(process.cwd(), 'tsconfig.json')
29
44
  const tsRet = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
30
-
31
45
  if (!tsRet.error) {
32
46
  const { error, options } = ts.parseJsonConfigFileContent(
33
47
  tsRet.config,
@@ -48,34 +62,32 @@ const host = {
48
62
  readFile: ts.sys.readFile,
49
63
  }
50
64
 
51
- let unimportRet
52
- const dtsPath = 'ps.d.ts'
53
-
54
65
  if (isLowVersion)
55
66
  await initialize()
56
67
 
57
- let config
58
-
59
- const workdir = process.cwd()
60
-
61
- const configPath = resolvePath(
62
- workdir,
63
- process.env.PS_CONFIG_FILE || 'ps.json',
64
- )
65
-
66
- const require = createRequire(import.meta.url)
67
68
  export async function initialize(data) {
68
69
  if (data)
69
70
  port = data.port
70
- log('read config...')
71
+
72
+ debug('read config...')
71
73
 
72
74
  config = require(configPath)
73
- if (!config.virtualFile)
74
- config.virtualFile = {}
75
+
75
76
  if (!config.paths)
76
77
  config.paths = {}
77
78
 
78
- if (!process.env.PS_HMR_BAN) {
79
+ const loaderPath = process.env.PS_LOADER_PATH
80
+
81
+ if (loaderPath) {
82
+ const loader = await import(loaderPath.startsWith('.') ? resolvePath(workdir, loaderPath) : loaderPath)
83
+
84
+ if (typeof loader.load === 'function')
85
+ customLoad = loader.load
86
+
87
+ if (typeof loader.resolve === 'function')
88
+ customResolve = loader.resolve
89
+ }
90
+ if (IS_DEV) {
79
91
  chokidar.watch(configPath, { persistent: true }).on('change', () => {
80
92
  port.postMessage(
81
93
  JSON.stringify({
@@ -87,19 +99,21 @@ export async function initialize(data) {
87
99
 
88
100
  if (!config.unimport)
89
101
  return
102
+
90
103
  unimportRet = await genUnImportRet(config.unimport)
91
104
  if (unimportRet) {
92
- log('auto import...')
105
+ debug('auto import...')
93
106
  await unimportRet.init()
94
107
 
108
+ const unimportDtsPath = resolvePath(workdir, dtsPath)
95
109
  writeFile(
96
- resolvePath(workdir, config.unimport.dtsPath || dtsPath),
110
+ unimportDtsPath,
97
111
  handleClassTypes(
98
112
  await unimportRet.generateTypeDeclarations({
99
113
  resolvePath: (i) => {
100
114
  if (i.from.startsWith('.') || isAbsolute(i.from)) {
101
115
  const related = slash(
102
- relative(workdir, i.from).replace(/\.ts(x)?$/, ''),
116
+ relative(dirname(unimportDtsPath), i.from).replace(/\.ts(x)?$/, ''),
103
117
  )
104
118
 
105
119
  return !related.startsWith('.') ? `./${related}` : related
@@ -112,12 +126,6 @@ export async function initialize(data) {
112
126
  }
113
127
  }
114
128
 
115
- const watchFiles = new Set()
116
- const filesRecord = new Map()
117
- const moduleGraph = {}
118
-
119
- let entryUrl
120
-
121
129
  function addUrlToGraph(url, parent) {
122
130
  if (!(url in moduleGraph))
123
131
  moduleGraph[url] = new Set()
@@ -129,63 +137,34 @@ function addUrlToGraph(url, parent) {
129
137
  function getFileMid(file) {
130
138
  const filename = basename(file)
131
139
  const ret = filename.split('.')
132
- if (ret.length === 3)
133
- return ret[1]
134
- else return ''
140
+ if (!['js', 'mjs', 'cjs', 'ts', 'tsx', 'mts', 'cts'].includes(ret.pop()))
141
+ return ''
142
+ if (!ret[0])// .dockerfile
143
+ return ''
144
+
145
+ return ret[1]
135
146
  }
136
147
 
137
148
  export const resolve = async (specifier, context, nextResolve) => {
138
- // virtual file
139
- if (config.virtualFile[specifier]) {
140
- return {
141
- format: 'ts',
142
- url: specifier,
143
- shortCircuit: true,
144
- }
145
- }
146
- // entrypoint
147
- if (!context.parentURL) {
148
- if (/^file:\/\/\//.test(specifier) && existsSync(fileURLToPath(specifier))) {
149
- entryUrl = specifier
150
-
149
+ if (customResolve) {
150
+ const url = await customResolve(specifier, context)
151
+ if (url) {
151
152
  return {
152
- format: EXTENSIONS.some(ext => specifier.endsWith(ext))
153
- ? 'ts'
154
- : undefined,
155
- url: specifier,
153
+ format: 'ts',
154
+ url,
156
155
  shortCircuit: true,
157
156
  }
158
157
  }
159
- else {
160
- // won't resolve virtual file as entry in vite
161
- return nextResolve(specifier)
162
- }
163
- }
164
- // url import
165
- // it seems useless
166
- if (/^file:\/\/\//.test(specifier) && extname(specifier) === '.ts') {
167
- const url = addUrlToGraph(specifier, context.parentURL.split('?')[0])
168
- return {
169
- format: 'ts',
170
- url,
171
- shortCircuit: true,
172
- }
173
- }
174
-
175
- // hmr import
176
- if (
177
- context.parentURL.includes('/node_modules/phecda-server')
178
- && isAbsolute(specifier)
179
- ) {
180
- specifier = relative(fileURLToPath(entryUrl), specifier)
181
- .replace(/\.ts$/, '')
182
- .slice(1)
183
- context.parentURL = entryUrl
184
158
  }
185
159
 
186
- // import/require from external library
187
- if (context.parentURL.includes('/node_modules/'))
160
+ // entrypoint
161
+ if (!context.parentURL)
188
162
  return nextResolve(specifier)
163
+
164
+ // @todo skip resolve to improve performance
165
+ // if (context.parentURL.includes('/node_modules/') && specifier.includes('/node_modules/'))
166
+ // return nextResolve(specifier)
167
+
189
168
  const { resolvedModule } = ts.resolveModuleName(
190
169
  specifier,
191
170
  fileURLToPath(context.parentURL),
@@ -194,7 +173,7 @@ export const resolve = async (specifier, context, nextResolve) => {
194
173
  moduleResolutionCache,
195
174
  )
196
175
 
197
- // import between loacl projects
176
+ // import among files in local project
198
177
  if (
199
178
  resolvedModule
200
179
  && !resolvedModule.resolvedFileName.includes('/node_modules/')
@@ -230,23 +209,24 @@ export const resolve = async (specifier, context, nextResolve) => {
230
209
  const resolveRet = await nextResolve(specifier)
231
210
 
232
211
  // ts resolve fail in some cases
233
- if (resolveRet.url && isAbsolute(resolveRet.url))
234
- resolveRet.url = pathToFileURL(resolveRet.url).href
212
+ if (resolveRet.url && isAbsolute(resolveRet.url)) {
213
+ const [path, query] = resolveRet.url.split('?')
214
+ resolveRet.url = pathToFileURL(path).href + (query ? `?${query}` : '')
215
+ }
235
216
 
236
217
  return resolveRet
237
218
  }
238
219
  // @todo the first params may be url or path, need to distinguish
239
220
 
240
221
  export const load = async (url, context, nextLoad) => {
241
- if (config.virtualFile[url]) {
242
- return {
243
- format: 'module',
244
- source: config.virtualFile[url],
245
- shortCircuit: true,
246
- }
222
+ let mode
223
+ if (context.importAttributes.ps) {
224
+ mode = context.importAttributes.ps
225
+ delete context.importAttributes.ps
247
226
  }
248
227
 
249
228
  url = url.split('?')[0]
229
+
250
230
  if (
251
231
  !url.includes('/node_modules/')
252
232
  && url.startsWith('file://')
@@ -254,32 +234,17 @@ export const load = async (url, context, nextLoad) => {
254
234
  && !isLowVersion
255
235
  ) {
256
236
  watchFiles.add(url)
257
- // watch(
258
- // fileURLToPath(url),
259
- // debounce((type) => {
260
- // if (type === 'change') {
261
- // try {
262
- // const files = [...findTopScope(url, Date.now())].reverse()
263
-
264
- // port.postMessage(
265
- // JSON.stringify({
266
- // type: 'change',
267
- // files,
268
- // }),
269
- // )
270
- // }
271
- // catch (e) {
272
- // port.postMessage(
273
- // JSON.stringify({
274
- // type: 'relaunch',
275
- // }),
276
- // )
277
- // }
278
- // }
279
- // }),
280
- // )
281
-
282
- if (IS_DEV && !process.env.PS_HMR_BAN) {
237
+
238
+ if (IS_DEV && mode !== 'not-hmr') {
239
+ if (isModuleFileUrl(url)) {
240
+ port.postMessage(
241
+ JSON.stringify({
242
+ type: 'init',
243
+ files: [fileURLToPath(url)],
244
+ }),
245
+ )
246
+ }
247
+
283
248
  chokidar.watch(fileURLToPath(url), { persistent: true }).on(
284
249
  'change',
285
250
  debounce(() => {
@@ -304,6 +269,17 @@ export const load = async (url, context, nextLoad) => {
304
269
  )
305
270
  }
306
271
  }
272
+ // after hmr
273
+ if (customLoad) {
274
+ const source = await customLoad(url, context)
275
+ if (source) {
276
+ return {
277
+ format: 'module',
278
+ source,
279
+ shortCircuit: true,
280
+ }
281
+ }
282
+ }
307
283
  // resolveModuleName failed
308
284
  // I don't know why it failed
309
285
  if (!context.format && url.endsWith('.ts'))
@@ -314,10 +290,12 @@ export const load = async (url, context, nextLoad) => {
314
290
 
315
291
  const code
316
292
  = typeof source === 'string' ? source : Buffer.from(source).toString()
293
+
317
294
  const compiled = (await compile(code, url)).replace(/_ts_metadata\(\"design:paramtypes\"\,/g, '_ts_metadata("design:paramtypes",()=>')// handle cycle
318
295
 
319
296
  if (unimportRet) {
320
297
  const { injectImports } = unimportRet
298
+
321
299
  return {
322
300
  format: 'module',
323
301
  source: (
@@ -1,5 +1,6 @@
1
1
  import { basename } from 'path'
2
2
  import { transform } from '@swc-node/core'
3
+ import psExports from './export.mjs'
3
4
  const injectInlineSourceMap = ({ code, map }) => {
4
5
  if (map) {
5
6
  const base64Map = Buffer.from(map, 'utf8').toString('base64')
@@ -19,6 +20,32 @@ export async function compile(sourcecode, filename) {
19
20
  emitDecoratorMetadata: true,
20
21
  experimentalDecorators: true,
21
22
  esModuleInterop: false,
23
+ swc: {
24
+
25
+ jsc: {
26
+ parser: {
27
+ syntax: 'typescript',
28
+ importAttributes: true,
29
+ decorators: true,
30
+ tsx: false,
31
+ dynamicImport: true,
32
+ strictPropertyInitialization: false,
33
+ },
34
+ experimental: {
35
+ keepImportAssertions: true,
36
+ },
37
+ transform: {
38
+ legacyDecorator: true,
39
+ decoratorMetadata: true,
40
+ },
41
+ // parser: {
42
+ // importAttributes: true
43
+ // },
44
+ // experimental: {
45
+ // keepImportAssertions: true
46
+ // }
47
+ },
48
+ },
22
49
  })
23
50
 
24
51
  return injectInlineSourceMap({ code, map })
@@ -26,18 +53,21 @@ export async function compile(sourcecode, filename) {
26
53
 
27
54
  export async function genUnImportRet(opts) {
28
55
  try {
29
- const psExports = Object.keys(await import('../dist/index.mjs'))
30
56
  const { createUnimport } = await import('unimport')
31
57
  return createUnimport({
32
58
  ...opts,
33
- imports: psExports
34
- .map((k) => {
35
- return { name: k, from: 'phecda-server' }
36
- })
37
- .concat(opts.imports || []),
59
+
60
+ presets: [
61
+ {
62
+ from: 'phecda-server',
63
+ imports: psExports,
64
+ },
65
+ ]
66
+ .concat(opts.presets || []),
38
67
  })
39
68
  }
40
69
  catch (e) {
70
+ console.error(e)
41
71
  return false
42
72
  }
43
73
  }
package/assets/ps.json DELETED
@@ -1,24 +0,0 @@
1
- {
2
- "$schema": "https://unpkg.com/phecda-server/assets/schema.json",
3
- "resolve": [
4
- {
5
- "source": "controller",
6
- "importer": "http",
7
- "path": ".ps/http.js"
8
- },
9
- {
10
- "source": "rpc",
11
- "importer": "client",
12
- "path": ".ps/rpc.js"
13
- }
14
- ],
15
- "unimport": {
16
- "dirs": ["."],
17
- "dirsScanOptions": {
18
- "filePatterns": ["*.{service,controller,module,rpc,solo,guard,extension,pipe,filter,addon}.ts"]
19
- }
20
- },
21
- "virtualFile": {},
22
- "moduleFile": []
23
-
24
- }
@@ -1,21 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "esnext",
4
- "useDefineForClassFields": false,
5
- "experimentalDecorators": true,
6
- "emitDecoratorMetadata": true,
7
- "module": "esnext",
8
- "lib": ["esnext", "DOM"],
9
- "strictPropertyInitialization": false,
10
- "moduleResolution": "Node",
11
- "strict": true,
12
- "resolveJsonModule": true,
13
- "esModuleInterop": true,
14
- "noEmit": true,
15
- "noUnusedLocals": true,
16
- "noUnusedParameters": true,
17
- "noImplicitReturns": true,
18
- "skipLibCheck": true
19
- },
20
- "include": [".","./ps.d.ts"]
21
- }
@@ -1,19 +0,0 @@
1
- import { B as BaseCtx, D as DefaultOptions } from './meta-B_HW7cWh.js';
2
-
3
- interface RpcServerOptions extends DefaultOptions {
4
- defaultQueue?: string;
5
- }
6
- interface RpcClientOptions {
7
- key?: string;
8
- timeout?: number;
9
- max?: number;
10
- }
11
- interface RpcCtx extends BaseCtx {
12
- args: any[];
13
- id: string;
14
- queue: string;
15
- isEvent?: boolean;
16
- category: 'rpc';
17
- }
18
-
19
- export type { RpcCtx as R, RpcServerOptions as a, RpcClientOptions as b };