phecda-server 7.0.0-alpha.1 → 7.0.0-alpha.11

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 (86) hide show
  1. package/assets/ps.json +1 -1
  2. package/assets/schema.json +55 -55
  3. package/bin/cli.mjs +33 -25
  4. package/dist/chunk-4LLLQOMF.js +152 -0
  5. package/dist/{chunk-7YQ57BQS.js → chunk-6OQ2SB5W.js} +137 -69
  6. package/dist/{chunk-GSPBHZBH.js → chunk-G7JFKM2M.js} +96 -80
  7. package/dist/{chunk-MBCHNDAY.js → chunk-KCPBITYZ.js} +24 -24
  8. package/dist/{chunk-HMVLXNV3.mjs → chunk-MUI6MTUO.mjs} +1 -1
  9. package/dist/chunk-NQ55PA2X.mjs +152 -0
  10. package/dist/{chunk-XYVMNY2X.mjs → chunk-YCES6ABN.mjs} +124 -56
  11. package/dist/{chunk-V7EI76QQ.mjs → chunk-Z7NAAE4M.mjs} +48 -32
  12. package/dist/{core-BIcUwV18.d.mts → core-CUTbX_IS.d.ts} +5 -3
  13. package/dist/{core-CYwEPfN4.d.ts → core-DIfgUKIU.d.mts} +5 -3
  14. package/dist/helper.d.mts +8 -6
  15. package/dist/helper.d.ts +8 -6
  16. package/dist/helper.js +8 -3
  17. package/dist/helper.mjs +10 -5
  18. package/dist/http/elysia/index.d.mts +4 -4
  19. package/dist/http/elysia/index.d.ts +4 -4
  20. package/dist/http/elysia/index.js +43 -44
  21. package/dist/http/elysia/index.mjs +9 -10
  22. package/dist/http/express/index.d.mts +4 -4
  23. package/dist/http/express/index.d.ts +4 -4
  24. package/dist/http/express/index.js +40 -41
  25. package/dist/http/express/index.mjs +9 -10
  26. package/dist/http/fastify/index.d.mts +4 -4
  27. package/dist/http/fastify/index.d.ts +4 -4
  28. package/dist/http/fastify/index.js +41 -42
  29. package/dist/http/fastify/index.mjs +9 -10
  30. package/dist/http/h3/index.d.mts +4 -4
  31. package/dist/http/h3/index.d.ts +4 -4
  32. package/dist/http/h3/index.js +41 -42
  33. package/dist/http/h3/index.mjs +9 -10
  34. package/dist/http/hono/index.d.mts +4 -4
  35. package/dist/http/hono/index.d.ts +4 -4
  36. package/dist/http/hono/index.js +37 -39
  37. package/dist/http/hono/index.mjs +5 -7
  38. package/dist/http/hyper-express/index.d.mts +4 -4
  39. package/dist/http/hyper-express/index.d.ts +4 -4
  40. package/dist/http/hyper-express/index.js +38 -39
  41. package/dist/http/hyper-express/index.mjs +8 -9
  42. package/dist/http/koa/index.d.mts +4 -4
  43. package/dist/http/koa/index.d.ts +4 -4
  44. package/dist/http/koa/index.js +40 -41
  45. package/dist/http/koa/index.mjs +9 -10
  46. package/dist/index.d.mts +44 -38
  47. package/dist/index.d.ts +44 -38
  48. package/dist/index.js +48 -42
  49. package/dist/index.mjs +21 -15
  50. package/dist/{meta-BXKLFTgG.d.mts → meta-CR-D39hQ.d.mts} +7 -5
  51. package/dist/{meta-BXKLFTgG.d.ts → meta-CR-D39hQ.d.ts} +7 -5
  52. package/dist/rpc/bullmq/index.d.mts +5 -5
  53. package/dist/rpc/bullmq/index.d.ts +5 -5
  54. package/dist/rpc/bullmq/index.js +15 -14
  55. package/dist/rpc/bullmq/index.mjs +4 -3
  56. package/dist/rpc/kafka/index.d.mts +4 -4
  57. package/dist/rpc/kafka/index.d.ts +4 -4
  58. package/dist/rpc/kafka/index.js +15 -14
  59. package/dist/rpc/kafka/index.mjs +3 -2
  60. package/dist/rpc/nats/index.d.mts +4 -4
  61. package/dist/rpc/nats/index.d.ts +4 -4
  62. package/dist/rpc/nats/index.js +14 -13
  63. package/dist/rpc/nats/index.mjs +4 -3
  64. package/dist/rpc/rabbitmq/index.d.mts +4 -4
  65. package/dist/rpc/rabbitmq/index.d.ts +4 -4
  66. package/dist/rpc/rabbitmq/index.js +16 -15
  67. package/dist/rpc/rabbitmq/index.mjs +4 -3
  68. package/dist/rpc/redis/index.d.mts +4 -4
  69. package/dist/rpc/redis/index.d.ts +4 -4
  70. package/dist/rpc/redis/index.js +14 -13
  71. package/dist/rpc/redis/index.mjs +4 -3
  72. package/dist/test.d.mts +3 -3
  73. package/dist/test.d.ts +3 -3
  74. package/dist/test.js +6 -6
  75. package/dist/test.mjs +2 -2
  76. package/dist/{types-h40T3cRG.d.mts → types-BF1TDbFV.d.ts} +4 -3
  77. package/dist/{types-BtbL49Zs.d.mts → types-DYRu0vic.d.ts} +4 -3
  78. package/dist/{types-VFzEM7LL.d.ts → types-DYviSl5B.d.mts} +4 -3
  79. package/dist/{types-m3IEDKjP.d.ts → types-DqH1qA-q.d.mts} +4 -3
  80. package/package.json +8 -6
  81. package/register/export.mjs +30 -0
  82. package/register/index.mjs +50 -40
  83. package/register/loader.mjs +110 -74
  84. package/register/utils.mjs +86 -56
  85. package/dist/chunk-J5CFUN4V.js +0 -73
  86. package/dist/chunk-WHJ5FALK.mjs +0 -73
@@ -2,26 +2,57 @@ import { fileURLToPath, pathToFileURL } from 'url'
2
2
  import { writeFile } from 'fs/promises'
3
3
  import {
4
4
  basename,
5
- extname,
5
+ dirname,
6
6
  isAbsolute,
7
7
  relative,
8
8
  resolve as resolvePath,
9
9
  } from 'path'
10
+ import { existsSync } from 'fs'
10
11
  import { createRequire } from 'module'
11
12
  import ts from 'typescript'
12
13
  import chokidar from 'chokidar'
13
- import { log } from '../dist/index.mjs'
14
+ import Debug from 'debug'
14
15
  import { compile, genUnImportRet, handleClassTypes, slash } from './utils.mjs'
15
16
 
16
- let port
17
+ const require = createRequire(import.meta.url)
18
+ const debug = Debug('phecda-server/loader')
17
19
 
18
20
  const isLowVersion = parseFloat(process.version.slice(1)) < 18.19
19
- // this part is important or not?
20
- const EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts]
21
- const tsconfig = {
21
+ const IS_DEV = process.env.NODE_ENV === 'development'
22
+ let port
23
+ let config
24
+ const workdir = process.cwd()
25
+ const configPath = resolvePath(
26
+ workdir,
27
+ process.env.PS_CONFIG_FILE || 'ps.json',
28
+ )
29
+
30
+ // unimport
31
+ let unimportRet
32
+ const dtsPath = process.env.PS_DTS_PATH || 'ps.d.ts'
33
+
34
+ // graph
35
+ let entryUrl
36
+ const watchFiles = new Set()
37
+ const filesRecord = new Map()
38
+ const moduleGraph = {}
39
+ // ts
40
+ let tsconfig = {
22
41
  module: ts.ModuleKind.ESNext,
23
42
  moduleResolution: ts.ModuleResolutionKind.NodeNext,
24
43
  }
44
+ const EXTENSIONS = [ts.Extension.Ts, ts.Extension.Tsx, ts.Extension.Mts]
45
+ const tsconfigPath = resolvePath(process.cwd(), 'tsconfig.json')
46
+ const tsRet = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
47
+ if (!tsRet.error) {
48
+ const { error, options } = ts.parseJsonConfigFileContent(
49
+ tsRet.config,
50
+ ts.sys,
51
+ dirname(tsconfigPath),
52
+ )
53
+ if (!error)
54
+ tsconfig = options
55
+ }
25
56
  const moduleResolutionCache = ts.createModuleResolutionCache(
26
57
  ts.sys.getCurrentDirectory(),
27
58
  x => x,
@@ -32,32 +63,32 @@ const host = {
32
63
  readFile: ts.sys.readFile,
33
64
  }
34
65
 
35
- let unimportRet
36
- const dtsPath = 'ps.d.ts'
37
-
38
66
  if (isLowVersion)
39
67
  await initialize()
40
68
 
41
- let config
42
-
43
- const workdir = process.env.PS_WORKDIR || process.cwd()
44
-
45
- const configPath = resolvePath(
46
- workdir,
47
- process.env.PS_CONFIG_FILE || 'ps.json',
48
- )
49
-
50
- const require = createRequire(import.meta.url)
51
69
  export async function initialize(data) {
52
70
  if (data)
53
71
  port = data.port
54
- log('read config...')
72
+
73
+ debug('read config...')
55
74
 
56
75
  config = require(configPath)
76
+ // const unconfigRet = await loadConfig({
77
+ // sources: [
78
+ // {
79
+ // files: configPath,
80
+ // extensions: ['ts', 'mts', 'cts', 'js', 'mjs', 'cjs', 'json', ''],
81
+ // },
82
+ // ],
83
+ // merge: false,
84
+ // })
85
+ // config = unconfigRet.config
57
86
  if (!config.virtualFile)
58
87
  config.virtualFile = {}
88
+ if (!config.paths)
89
+ config.paths = {}
59
90
 
60
- if (!process.env.PS_HMR_BAN) {
91
+ if (IS_DEV) {
61
92
  chokidar.watch(configPath, { persistent: true }).on('change', () => {
62
93
  port.postMessage(
63
94
  JSON.stringify({
@@ -71,17 +102,18 @@ export async function initialize(data) {
71
102
  return
72
103
  unimportRet = await genUnImportRet(config.unimport)
73
104
  if (unimportRet) {
74
- log('auto import...')
105
+ debug('auto import...')
75
106
  await unimportRet.init()
76
107
 
108
+ const unimportDtsPath = resolvePath(workdir, dtsPath)
77
109
  writeFile(
78
- resolvePath(workdir, config.unimport.dtsPath || dtsPath),
110
+ unimportDtsPath,
79
111
  handleClassTypes(
80
112
  await unimportRet.generateTypeDeclarations({
81
113
  resolvePath: (i) => {
82
114
  if (i.from.startsWith('.') || isAbsolute(i.from)) {
83
115
  const related = slash(
84
- relative(workdir, i.from).replace(/\.ts(x)?$/, ''),
116
+ relative(dirname(unimportDtsPath), i.from).replace(/\.ts(x)?$/, ''),
85
117
  )
86
118
 
87
119
  return !related.startsWith('.') ? `./${related}` : related
@@ -94,12 +126,6 @@ export async function initialize(data) {
94
126
  }
95
127
  }
96
128
 
97
- const watchFiles = new Set()
98
- const filesRecord = new Map()
99
- const moduleGraph = {}
100
-
101
- let entryUrl
102
-
103
129
  function addUrlToGraph(url, parent) {
104
130
  if (!(url in moduleGraph))
105
131
  moduleGraph[url] = new Set()
@@ -127,25 +153,33 @@ export const resolve = async (specifier, context, nextResolve) => {
127
153
  }
128
154
  // entrypoint
129
155
  if (!context.parentURL) {
130
- entryUrl = specifier
131
- return {
132
- format: EXTENSIONS.some(ext => specifier.endsWith(ext))
133
- ? 'ts'
134
- : undefined,
135
- url: specifier,
136
- shortCircuit: true,
156
+ if (/^file:\/\/\//.test(specifier) && existsSync(fileURLToPath(specifier))) {
157
+ entryUrl = specifier
158
+
159
+ return {
160
+ format: EXTENSIONS.some(ext => specifier.endsWith(ext))
161
+ ? 'ts'
162
+ : undefined,
163
+ url: specifier,
164
+ shortCircuit: true,
165
+ }
166
+ }
167
+ else {
168
+ // won't resolve virtual file as entry in vite
169
+ return nextResolve(specifier)
137
170
  }
138
171
  }
139
172
  // url import
140
173
  // it seems useless
141
- if (/^file:\/\/\//.test(specifier) && extname(specifier) === '.ts') {
142
- const url = addUrlToGraph(specifier, context.parentURL.split('?')[0])
143
- return {
144
- format: 'ts',
145
- url,
146
- shortCircuit: true,
147
- }
148
- }
174
+ // if (/^file:\/\/\//.test(specifier) && extname(specifier) === '.ts') {
175
+ // const url = addUrlToGraph(specifier, context.parentURL.split('?')[0])
176
+ // return {
177
+ // format: 'ts',
178
+ // url,
179
+ // shortCircuit: true,
180
+ // }
181
+ // }
182
+
149
183
 
150
184
  // hmr import
151
185
  if (
@@ -169,7 +203,7 @@ export const resolve = async (specifier, context, nextResolve) => {
169
203
  moduleResolutionCache,
170
204
  )
171
205
 
172
- // import between loacl projects
206
+ // import among files in local project
173
207
  if (
174
208
  resolvedModule
175
209
  && !resolvedModule.resolvedFileName.includes('/node_modules/')
@@ -202,6 +236,7 @@ export const resolve = async (specifier, context, nextResolve) => {
202
236
  }
203
237
  }
204
238
 
239
+
205
240
  const resolveRet = await nextResolve(specifier)
206
241
 
207
242
  // ts resolve fail in some cases
@@ -213,6 +248,9 @@ export const resolve = async (specifier, context, nextResolve) => {
213
248
  // @todo the first params may be url or path, need to distinguish
214
249
 
215
250
  export const load = async (url, context, nextLoad) => {
251
+
252
+
253
+
216
254
  if (config.virtualFile[url]) {
217
255
  return {
218
256
  format: 'module',
@@ -221,6 +259,14 @@ export const load = async (url, context, nextLoad) => {
221
259
  }
222
260
  }
223
261
 
262
+ let mode
263
+ if (context.importAttributes.ps) {
264
+ mode = context.importAttributes.ps
265
+ delete context.importAttributes.ps
266
+ }
267
+
268
+
269
+
224
270
  url = url.split('?')[0]
225
271
  if (
226
272
  !url.includes('/node_modules/')
@@ -229,32 +275,17 @@ export const load = async (url, context, nextLoad) => {
229
275
  && !isLowVersion
230
276
  ) {
231
277
  watchFiles.add(url)
232
- // watch(
233
- // fileURLToPath(url),
234
- // debounce((type) => {
235
- // if (type === 'change') {
236
- // try {
237
- // const files = [...findTopScope(url, Date.now())].reverse()
238
-
239
- // port.postMessage(
240
- // JSON.stringify({
241
- // type: 'change',
242
- // files,
243
- // }),
244
- // )
245
- // }
246
- // catch (e) {
247
- // port.postMessage(
248
- // JSON.stringify({
249
- // type: 'relaunch',
250
- // }),
251
- // )
252
- // }
253
- // }
254
- // }),
255
- // )
256
-
257
- if (!process.env.PS_HMR_BAN) {
278
+
279
+ if (IS_DEV && mode !== 'not-hmr') {
280
+ if (isModuleFileUrl(url)) {
281
+ port.postMessage(
282
+ JSON.stringify({
283
+ type: 'init',
284
+ files: [fileURLToPath(url)],
285
+ }),
286
+ )
287
+ }
288
+
258
289
  chokidar.watch(fileURLToPath(url), { persistent: true }).on(
259
290
  'change',
260
291
  debounce(() => {
@@ -289,9 +320,14 @@ export const load = async (url, context, nextLoad) => {
289
320
 
290
321
  const code
291
322
  = typeof source === 'string' ? source : Buffer.from(source).toString()
292
- const compiled = await compile(code, url)
323
+
324
+ const compiled = (await compile(code, url)).replace(/_ts_metadata\(\"design:paramtypes\"\,/g, '_ts_metadata("design:paramtypes",()=>')// handle cycle
325
+
293
326
  if (unimportRet) {
327
+
328
+
294
329
  const { injectImports } = unimportRet
330
+
295
331
  return {
296
332
  format: 'module',
297
333
  source: (
@@ -357,7 +393,7 @@ export function isModuleFileUrl(url) {
357
393
  'addon',
358
394
  'filter',
359
395
  'pipe',
360
- 'edge',
396
+ 'solo',
361
397
  ].includes(midName)
362
398
  )
363
399
  return true
@@ -1,56 +1,86 @@
1
- import { basename } from 'path'
2
- import { transform } from '@swc-node/core'
3
- const injectInlineSourceMap = ({ code, map }) => {
4
- if (map) {
5
- const base64Map = Buffer.from(map, 'utf8').toString('base64')
6
- const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
7
- return `${code}\n${sourceMapContent}`
8
- }
9
- return code
10
- }
11
-
12
- export async function compile(sourcecode, filename) {
13
- if (filename.endsWith('.d.ts'))
14
- return ''
15
-
16
- const { code, map } = await transform(sourcecode, filename, {
17
- sourcemap: true,
18
- module: 'es6',
19
- emitDecoratorMetadata: true,
20
- experimentalDecorators: true,
21
- esModuleInterop: false,
22
- })
23
-
24
- return injectInlineSourceMap({ code, map })
25
- }
26
-
27
- export async function genUnImportRet(opts) {
28
- try {
29
- const psExports = Object.keys(await import('../dist/index.mjs'))
30
- const { createUnimport } = await import('unimport')
31
- return createUnimport({
32
- ...opts,
33
- imports: psExports
34
- .map((k) => {
35
- return { name: k, from: 'phecda-server' }
36
- })
37
- .concat(opts.imports || []),
38
- })
39
- }
40
- catch (e) {
41
- return false
42
- }
43
- }
44
-
45
- export function handleClassTypes(input) {
46
- return input.replace(/const\s+(\w+)\s*:\s*typeof\s+import\(['"](.+)['"]\)\['(\w+)'\]/g, (_, n, p, e) => {
47
- // TestController in test.controller
48
- if (p.startsWith('./') && (basename(p).replace('.', '').toLowerCase() === e.toLowerCase()))
49
- return `${_}\n type ${n} = InstanceType<typeof import('${p}')['${e}']>`
50
-
51
- return _
52
- })
53
- }
54
- export function slash(str) {
55
- return str.replace(/\\/g, '/')
56
- }
1
+ import { basename } from 'path'
2
+ import { transform } from '@swc-node/core'
3
+ import psExports from './export.mjs'
4
+ const injectInlineSourceMap = ({ code, map }) => {
5
+ if (map) {
6
+ const base64Map = Buffer.from(map, 'utf8').toString('base64')
7
+ const sourceMapContent = `//# sourceMappingURL=data:application/json;charset=utf-8;base64,${base64Map}`
8
+ return `${code}\n${sourceMapContent}`
9
+ }
10
+ return code
11
+ }
12
+
13
+ export async function compile(sourcecode, filename) {
14
+ if (filename.endsWith('.d.ts'))
15
+ return ''
16
+
17
+ const { code, map } = await transform(sourcecode, filename, {
18
+ sourcemap: true,
19
+ module: 'es6',
20
+ emitDecoratorMetadata: true,
21
+ experimentalDecorators: true,
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
+ }
49
+ })
50
+
51
+ return injectInlineSourceMap({ code, map })
52
+ }
53
+
54
+ export async function genUnImportRet(opts) {
55
+ try {
56
+ const { createUnimport } = await import('unimport')
57
+ return createUnimport({
58
+ ...opts,
59
+
60
+ presets: [
61
+ {
62
+ from: 'phecda-server',
63
+ imports: psExports,
64
+ },
65
+ ]
66
+ .concat(opts.presets || []),
67
+ })
68
+ }
69
+ catch (e) {
70
+ console.error(e)
71
+ return false
72
+ }
73
+ }
74
+
75
+ export function handleClassTypes(input) {
76
+ return input.replace(/const\s+(\w+)\s*:\s*typeof\s+import\(['"](.+)['"]\)\['(\w+)'\]/g, (_, n, p, e) => {
77
+ // TestController in test.controller
78
+ if (p.startsWith('./') && (basename(p).replace('.', '').toLowerCase() === e.toLowerCase()))
79
+ return `${_}\n type ${n} = InstanceType<typeof import('${p}')['${e}']>`
80
+
81
+ return _
82
+ })
83
+ }
84
+ export function slash(str) {
85
+ return str.replace(/\\/g, '/')
86
+ }
@@ -1,73 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/common.ts
5
- var ERROR_SYMBOL = "__PS_ERROR__";
6
- var IS_HMR = process.env.NODE_ENV === "development";
7
- var IS_ONLY_GENERATE = !!process.env.PS_GENERATE;
8
- var IS_STRICT = !!process.env.PS_STRICT;
9
- var LOG_LEVEL = Number(process.env.PS_LOG_LEVEL || 0);
10
- var PS_EXIT_CODE;
11
- (function(PS_EXIT_CODE2) {
12
- PS_EXIT_CODE2[PS_EXIT_CODE2["RELAUNCH"] = 2] = "RELAUNCH";
13
- PS_EXIT_CODE2[PS_EXIT_CODE2["CODE"] = 4] = "CODE";
14
- })(PS_EXIT_CODE || (PS_EXIT_CODE = exports.PS_EXIT_CODE = {}));
15
-
16
- // src/utils.ts
17
- var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
18
- var _tsmixer = require('ts-mixer');
19
- var time;
20
- var internalLogger;
21
- function setLogger(logger) {
22
- internalLogger = logger;
23
- }
24
- __name(setLogger, "setLogger");
25
- function getLogger() {
26
- return internalLogger;
27
- }
28
- __name(getLogger, "getLogger");
29
- function log(msg, level = "log") {
30
- if (internalLogger) internalLogger[level](msg);
31
- const logLevel = {
32
- debug: -1,
33
- info: 0,
34
- log: 1,
35
- warn: 2,
36
- error: 3
37
- }[level];
38
- if (logLevel < LOG_LEVEL) return;
39
- const color = {
40
- debug: "bgMagenta",
41
- error: "red",
42
- info: "gray",
43
- warn: "yellow",
44
- log: "green"
45
- }[level];
46
- const date = /* @__PURE__ */ new Date();
47
- const current = Date.now();
48
- const interval = time && current - time ? `+${current - time}` : "";
49
- time = current;
50
- console[level](`${_picocolors2.default.magenta("[phecda-server]")} ${_picocolors2.default.gray(`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`)} ${_picocolors2.default[color](msg)} ${_picocolors2.default.gray(interval)}`);
51
- }
52
- __name(log, "log");
53
- function runMiddleware(ctx, middleware) {
54
- return new Promise((resolve) => {
55
- middleware(ctx.getRequest(), ctx.getResponse(), resolve);
56
- });
57
- }
58
- __name(runMiddleware, "runMiddleware");
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
- exports.__name = __name; exports.ERROR_SYMBOL = ERROR_SYMBOL; exports.IS_HMR = IS_HMR; exports.IS_ONLY_GENERATE = IS_ONLY_GENERATE; exports.IS_STRICT = IS_STRICT; exports.LOG_LEVEL = LOG_LEVEL; exports.PS_EXIT_CODE = PS_EXIT_CODE; exports.setLogger = setLogger; exports.getLogger = getLogger; exports.log = log; exports.runMiddleware = runMiddleware; exports.Mixin = _tsmixer.Mixin;
@@ -1,73 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/common.ts
5
- var ERROR_SYMBOL = "__PS_ERROR__";
6
- var IS_HMR = process.env.NODE_ENV === "development";
7
- var IS_ONLY_GENERATE = !!process.env.PS_GENERATE;
8
- var IS_STRICT = !!process.env.PS_STRICT;
9
- var LOG_LEVEL = Number(process.env.PS_LOG_LEVEL || 0);
10
- var PS_EXIT_CODE;
11
- (function(PS_EXIT_CODE2) {
12
- PS_EXIT_CODE2[PS_EXIT_CODE2["RELAUNCH"] = 2] = "RELAUNCH";
13
- PS_EXIT_CODE2[PS_EXIT_CODE2["CODE"] = 4] = "CODE";
14
- })(PS_EXIT_CODE || (PS_EXIT_CODE = {}));
15
-
16
- // src/utils.ts
17
- import pc from "picocolors";
18
- import { Mixin } from "ts-mixer";
19
- var time;
20
- var internalLogger;
21
- function setLogger(logger) {
22
- internalLogger = logger;
23
- }
24
- __name(setLogger, "setLogger");
25
- function getLogger() {
26
- return internalLogger;
27
- }
28
- __name(getLogger, "getLogger");
29
- function log(msg, level = "log") {
30
- if (internalLogger) internalLogger[level](msg);
31
- const logLevel = {
32
- debug: -1,
33
- info: 0,
34
- log: 1,
35
- warn: 2,
36
- error: 3
37
- }[level];
38
- if (logLevel < LOG_LEVEL) return;
39
- const color = {
40
- debug: "bgMagenta",
41
- error: "red",
42
- info: "gray",
43
- warn: "yellow",
44
- log: "green"
45
- }[level];
46
- const date = /* @__PURE__ */ new Date();
47
- const current = Date.now();
48
- const interval = time && current - time ? `+${current - time}` : "";
49
- time = current;
50
- console[level](`${pc.magenta("[phecda-server]")} ${pc.gray(`${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`)} ${pc[color](msg)} ${pc.gray(interval)}`);
51
- }
52
- __name(log, "log");
53
- function runMiddleware(ctx, middleware) {
54
- return new Promise((resolve) => {
55
- middleware(ctx.getRequest(), ctx.getResponse(), resolve);
56
- });
57
- }
58
- __name(runMiddleware, "runMiddleware");
59
-
60
- export {
61
- __name,
62
- ERROR_SYMBOL,
63
- IS_HMR,
64
- IS_ONLY_GENERATE,
65
- IS_STRICT,
66
- LOG_LEVEL,
67
- PS_EXIT_CODE,
68
- setLogger,
69
- getLogger,
70
- log,
71
- runMiddleware,
72
- Mixin
73
- };