vona-core 5.0.106 → 5.0.108

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/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import '@cabloy/set';
2
2
  import '@cabloy/json5';
3
- import { combineApiPathControllerAndActionRaw, combineApiPathControllerAndAction, combineApiPath, combineResourceName, sleep, isUndefined, isClass, isNilOrEmptyString, evaluateExpressions, catchErrorSync, isEmptyObject, hashkey } from '@cabloy/utils';
3
+ import { combineApiPathControllerAndActionRaw, combineApiPathControllerAndAction, combineApiPath, combineResourceName, sleep, isUndefined, isClass, isNilOrEmptyString, evaluateExpressions, catchErrorSync, isEmptyObject, catchError, hashkey } from '@cabloy/utils';
4
4
  import { toLowerCaseFirstChar, splitWords } from '@cabloy/word-utils';
5
5
  import path from 'node:path';
6
6
  import crypto from 'node:crypto';
@@ -348,7 +348,7 @@ async function saveJSONFile(fileName, json) {
348
348
  function useApp() {
349
349
  return globalThis.__app__;
350
350
  }
351
- async function closeApp(terminate) {
351
+ async function closeApp() {
352
352
  while (globalThis.__closing__) {
353
353
  await sleep(50);
354
354
  }
@@ -361,9 +361,6 @@ async function closeApp(terminate) {
361
361
  } finally {
362
362
  globalThis.__closing__ = false;
363
363
  }
364
- if (terminate) {
365
- process.kill(process.pid, 'SIGTERM');
366
- }
367
364
  }
368
365
  async function createGeneralApp(projectPath, envRuntime) {
369
366
  if (envRuntime) {
@@ -2434,8 +2431,8 @@ class VonaApplication extends KoaApplication {
2434
2431
  const response = res ?? new ResponseMock();
2435
2432
  return this.createContext(request, response);
2436
2433
  }
2437
- async close(terminate) {
2438
- await closeApp(terminate);
2434
+ async close() {
2435
+ await closeApp();
2439
2436
  }
2440
2437
  }
2441
2438
 
@@ -2969,20 +2966,72 @@ function prepareAppInfo(env) {
2969
2966
  };
2970
2967
  }
2971
2968
 
2972
- function handleProcessWork() {
2973
- process.once('SIGUSR2', async () => {
2974
- // console.log('------------SIGUSR2');
2975
- await closeApp(true);
2969
+ let __closing$1 = false;
2970
+ let __timeout;
2971
+ async function _closeInner$1() {
2972
+ __timeout = setTimeout(() => {
2973
+ // eslint-disable-next-line no-console
2974
+ console.log('Cleanup timed out. Forcing termination...');
2975
+ process.exit(1);
2976
+ }, 5000);
2977
+ }
2978
+ function handleProcessMaster(workers) {
2979
+ ['SIGINT', 'SIGUSR2'].forEach(signal => {
2980
+ process.on(signal, async () => {
2981
+ if (__closing$1) return;
2982
+ __closing$1 = true;
2983
+ await _closeInner$1();
2984
+ });
2976
2985
  });
2977
- process.once('SIGINT', async () => {
2978
- // console.log('------------SIGINT');
2979
- await closeApp(true);
2986
+ for (let i = 0; i < workers; i++) {
2987
+ cluster.fork();
2988
+ }
2989
+ cluster.on('message', (worker, message) => {
2990
+ if (message === 'reload-worker') {
2991
+ if (!__closing$1) {
2992
+ cluster.fork();
2993
+ }
2994
+ worker.process.kill('SIGTERM');
2995
+ }
2996
+ });
2997
+ cluster.on('exit', (_worker, _code, _signal) => {
2998
+ // console.log(`----------------- worker ${_worker.process.pid} died`, _code, _signal);
2999
+ if (cluster.workers && Object.keys(cluster.workers).length === 0) {
3000
+ clearTimeout(__timeout);
3001
+ process.exit(0);
3002
+ }
3003
+ });
3004
+ }
3005
+
3006
+ let __closing = false;
3007
+ async function _closeInner() {
3008
+ const timeout = setTimeout(() => {
3009
+ // eslint-disable-next-line no-console
3010
+ console.log('Cleanup timed out. Forcing termination...');
3011
+ process.exit(1);
3012
+ }, 5000);
3013
+ const [_, err] = await catchError(() => {
3014
+ return closeApp();
3015
+ });
3016
+ if (err) {
3017
+ console.error(err);
3018
+ }
3019
+ clearTimeout(timeout);
3020
+ process.exit(err ? 1 : 0);
3021
+ }
3022
+ function handleProcessWork() {
3023
+ ['SIGINT', 'SIGUSR2'].forEach(signal => {
3024
+ process.on(signal, async () => {
3025
+ if (__closing) return;
3026
+ __closing = true;
3027
+ await _closeInner();
3028
+ });
2980
3029
  });
2981
3030
  process.on('uncaughtException', async err => {
2982
3031
  const app = useApp();
2983
3032
  if (!app) {
2984
3033
  console.error(err);
2985
- process.kill(process.pid, 'SIGTERM');
3034
+ process.exit(1);
2986
3035
  } else {
2987
3036
  const [logger] = catchErrorSync(() => {
2988
3037
  return app.meta.logger.get();
@@ -2994,62 +3043,34 @@ function handleProcessWork() {
2994
3043
  }
2995
3044
  if (!app.meta.appStarted) {
2996
3045
  await app.meta.logger.dispose();
2997
- process.kill(process.pid, 'SIGTERM');
3046
+ process.exit(1);
2998
3047
  }
2999
3048
  }
3000
3049
  });
3001
3050
  }
3002
- function handleProcessMaster() {
3003
- process.once('SIGUSR2', () => {
3004
- // should not kill master self by manual
3005
- // process.kill(process.pid, 'SIGTERM');
3006
- });
3007
- // should not kill master self by manual
3008
- // process.once('SIGINT', () => {
3009
- // process.kill(process.pid, 'SIGTERM');
3010
- // });
3011
- }
3012
3051
 
3013
3052
  async function startCluster(workers, bootstrapOptions) {
3014
3053
  if (cluster.isPrimary) {
3015
- handleProcessMaster();
3054
+ handleProcessMaster(workers);
3016
3055
  createAppMaster(bootstrapOptions);
3017
-
3018
- // console.log(`Primary ${process.pid} is running`);
3019
-
3020
- // Fork workers.
3021
- for (let i = 0; i < workers; i++) {
3022
- cluster.fork();
3023
- }
3024
- cluster.on('message', (worker, message) => {
3025
- if (message === 'reload-worker') {
3026
- worker.process.kill('SIGTERM');
3027
- cluster.fork();
3028
- }
3029
- });
3030
- cluster.on('exit', (_worker, _code, _signal) => {
3031
- // console.log(`worker ${worker.process.pid} died`, code, signal);
3032
- // should not kill master self by manual
3033
- // master -> worker, rather than worker -> master
3034
- // if (cluster.workers && Object.keys(cluster.workers).length === 0) {
3035
- // process.kill(process.pid, 'SIGTERM');
3036
- // }
3037
- });
3038
3056
  } else {
3039
- handleProcessWork();
3040
- await createApp(bootstrapOptions);
3041
- // console.log(`Worker ${process.pid} started`);
3057
+ await startWorker(bootstrapOptions);
3042
3058
  }
3043
3059
  }
3060
+ async function startWorker(bootstrapOptions) {
3061
+ handleProcessWork();
3062
+ return await createApp(bootstrapOptions);
3063
+ }
3044
3064
 
3045
3065
  async function bootstrap(bootstrapOptions) {
3046
3066
  const env = prepareEnv(bootstrapOptions.env);
3047
3067
  const workers = Number.parseInt(env.SERVER_WORKERS);
3048
- if (workers === 1 && process.env.META_MODE !== 'dev') {
3049
- handleProcessWork();
3050
- return await createApp(bootstrapOptions);
3068
+ const alwaysCluster = process.env.META_MODE === 'dev' || process.platform.startsWith('win');
3069
+ if (workers > 1 || alwaysCluster) {
3070
+ await startCluster(workers, bootstrapOptions);
3071
+ } else {
3072
+ return await startWorker(bootstrapOptions);
3051
3073
  }
3052
- await startCluster(workers, bootstrapOptions);
3053
3074
  }
3054
3075
 
3055
3076
  function ExtendClass(classRef) {
@@ -3195,4 +3216,4 @@ function prepareNativeBinding(nativeBinding) {
3195
3216
 
3196
3217
  zodExtendOpenApi();
3197
3218
 
3198
- export { $Class, $customKey, $localeScope, AppHmr, AppHmrDeps, AppLocale, AppLogger, AppMeta, AppMetadata, AppResource, AppUtil, BeanAopBase, BeanAopMethodBase, BeanBase, BeanBaseSimple, BeanContainer, BeanInfo, BeanScopeBase, BeanScopeContainer, BeanScopeError, BeanScopeErrorImpl, BeanScopeLocale, BeanScopeScene, BeanScopeUtil, BeanSimple, EnumAppEvent, ErrorClass, LocaleModuleNameSeparator, PickClassInner, ProxyDisable, SymbolBeanContainerInstances, SymbolBeanFullName, SymbolBeanInstanceKey, SymbolBeanInstancePropsLazy, SymbolCacheAopChains, SymbolCacheAopChainsKey, SymbolDecoratorBeanFullName, SymbolDecoratorBeanInfo, SymbolDecoratorProxyDisable, SymbolDecoratorUse, SymbolDecoratorVirtual, SymbolHmrStateLoad, SymbolHmrStateSave, SymbolMappedClassMetadataKeys, SymbolModuleBelong, SymbolModuleName, Use, Virtual, VonaApplication, __prepareInjectSelectorInfo, appHmrDeps, appMetadata, appResource, beanFullNameFromOnionName, bootstrap, cast, closeApp, combineConfigDefault, combineFilePathSafe, compose, copyMetadataOfClasses, copyProperties, copyPropertiesOfClasses, copySqlite3NativeBinding, createApp, createAppMaster, createBeanDecorator, createGeneralApp, createHash, deepExtend, disposeInstance, errorsInternal, filterHeaders, formatLoggerAxiosError, formatLoggerConsole, formatLoggerCtx, formatLoggerDummy, formatLoggerFilter, functionNoop, getLoggerPathPhysicalRoot, getMappedClassMetadataKeys, getPublicPathPhysicalRoot, getRuntimePathPhysicalRoot, getSqlite3DatabaseNameDefault, getSqlite3NativeBinding, handleProcessMaster, handleProcessWork, instanceDesp, loadJSONFile, localeDefault, onionNameFromBeanFullName, pathToHref, polyfillDispose, prepareEnv, registerMappedClassMetadataKey, requireDynamic, retry, saveJSONFile, setMappedClassMetadataKeys, useApp, usePrepareArg, usePrepareArgs, uuidv4 };
3219
+ export { $Class, $customKey, $localeScope, AppHmr, AppHmrDeps, AppLocale, AppLogger, AppMeta, AppMetadata, AppResource, AppUtil, BeanAopBase, BeanAopMethodBase, BeanBase, BeanBaseSimple, BeanContainer, BeanInfo, BeanScopeBase, BeanScopeContainer, BeanScopeError, BeanScopeErrorImpl, BeanScopeLocale, BeanScopeScene, BeanScopeUtil, BeanSimple, EnumAppEvent, ErrorClass, LocaleModuleNameSeparator, PickClassInner, ProxyDisable, SymbolBeanContainerInstances, SymbolBeanFullName, SymbolBeanInstanceKey, SymbolBeanInstancePropsLazy, SymbolCacheAopChains, SymbolCacheAopChainsKey, SymbolDecoratorBeanFullName, SymbolDecoratorBeanInfo, SymbolDecoratorProxyDisable, SymbolDecoratorUse, SymbolDecoratorVirtual, SymbolHmrStateLoad, SymbolHmrStateSave, SymbolMappedClassMetadataKeys, SymbolModuleBelong, SymbolModuleName, Use, Virtual, VonaApplication, __prepareInjectSelectorInfo, appHmrDeps, appMetadata, appResource, beanFullNameFromOnionName, bootstrap, cast, closeApp, combineConfigDefault, combineFilePathSafe, compose, copyMetadataOfClasses, copyProperties, copyPropertiesOfClasses, copySqlite3NativeBinding, createApp, createAppMaster, createBeanDecorator, createGeneralApp, createHash, deepExtend, disposeInstance, errorsInternal, filterHeaders, formatLoggerAxiosError, formatLoggerConsole, formatLoggerCtx, formatLoggerDummy, formatLoggerFilter, functionNoop, getLoggerPathPhysicalRoot, getMappedClassMetadataKeys, getPublicPathPhysicalRoot, getRuntimePathPhysicalRoot, getSqlite3DatabaseNameDefault, getSqlite3NativeBinding, instanceDesp, loadJSONFile, localeDefault, onionNameFromBeanFullName, pathToHref, polyfillDispose, prepareEnv, registerMappedClassMetadataKey, requireDynamic, retry, saveJSONFile, setMappedClassMetadataKeys, useApp, usePrepareArg, usePrepareArgs, uuidv4 };
@@ -27,5 +27,5 @@ export declare class VonaApplication extends KoaApplication {
27
27
  /** get specific module's scope */
28
28
  scope<K extends TypeBeanScopeRecordKeys>(moduleScope: K): IBeanScopeRecord[K];
29
29
  createAnonymousContext(req?: any, res?: any): VonaContext;
30
- close(terminate?: boolean): Promise<void>;
30
+ close(): Promise<void>;
31
31
  }
@@ -2,3 +2,4 @@ export * from './constructable.ts';
2
2
  export * from './containerScope.ts';
3
3
  export * from './function.ts';
4
4
  export * from './injectionScope.ts';
5
+ export * from './string.ts';
@@ -0,0 +1 @@
1
+ export type PickString<T> = T extends string ? T : never;
@@ -1,2 +1,3 @@
1
1
  import type { BootstrapOptions } from '../../types/interface/bootstrap.ts';
2
2
  export declare function startCluster(workers: number, bootstrapOptions: BootstrapOptions): Promise<void>;
3
+ export declare function startWorker(bootstrapOptions: BootstrapOptions): Promise<any>;
@@ -1,4 +1,3 @@
1
1
  export * from './bootstrap.ts';
2
2
  export * from './createApp.ts';
3
- export * from './process.ts';
4
3
  export * from './useApp.ts';
@@ -0,0 +1 @@
1
+ export declare function handleProcessMaster(workers: number): void;
@@ -0,0 +1 @@
1
+ export declare function handleProcessWork(): void;
@@ -1,4 +1,4 @@
1
1
  import type { VonaApplication } from '../core/application.ts';
2
2
  export declare function useApp(): VonaApplication;
3
- export declare function closeApp(terminate?: boolean): Promise<void>;
3
+ export declare function closeApp(): Promise<void>;
4
4
  export declare function createGeneralApp(projectPath: string, envRuntime?: Partial<NodeJS.ProcessEnv>): Promise<any>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vona-core",
3
3
  "type": "module",
4
- "version": "5.0.106",
4
+ "version": "5.0.108",
5
5
  "description": "vona",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -1,2 +0,0 @@
1
- export declare function handleProcessWork(): void;
2
- export declare function handleProcessMaster(): void;