oox 0.3.0-beta7 → 0.3.0-beta8

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/app.js CHANGED
@@ -4,7 +4,6 @@ exports.execute = exports.call = exports.on = exports.getMethods = exports.setMe
4
4
  const node_events_1 = require("node:events");
5
5
  const node_async_hooks_1 = require("node:async_hooks");
6
6
  const utils_1 = require("./utils");
7
- // import { wrappedActions, actionMiddlewares, middlewares } from './middleware'
8
7
  exports.logger = require("./logger");
9
8
  class Context {
10
9
  // 请求溯源ID
@@ -126,18 +125,6 @@ async function execute(action, params, context) {
126
125
  // ============================= PROXY END =============================
127
126
  // make sure target action execute after all proxies
128
127
  if (target) {
129
- /*
130
- const sourceMethod = wrappedActions.get ( action )
131
-
132
- const middlewareNames = actionMiddlewares.get ( sourceMethod )
133
-
134
- if ( middlewareNames && middlewareNames.length ) for ( const name of middlewareNames ) {
135
-
136
- const middleware = middlewares.get ( name )
137
-
138
- await middleware ( action, params, context )
139
- }
140
- */
141
128
  return await target(...params);
142
129
  }
143
130
  }
package/bin/configurer.js CHANGED
@@ -25,17 +25,30 @@ function mergeFlatEnv(env) {
25
25
  }
26
26
  }
27
27
  }
28
- function configure() {
29
- let env = Object.create(null);
30
- const defaultEnvPath = argv.getEnvArg('default-env');
31
- if (defaultEnvPath && fs.existsSync(defaultEnvPath)) {
32
- Object.assign(env, require(path.resolve(defaultEnvPath)));
28
+ async function readEnvFile(filePath) {
29
+ let env = {};
30
+ if (filePath && fs.existsSync(filePath)) {
31
+ if (filePath.endsWith('.json')) {
32
+ const raw = fs.readFileSync(filePath, 'utf-8');
33
+ env = JSON.parse(raw);
34
+ }
35
+ else {
36
+ const finalPath = path.resolve(filePath).replace(/\\/g, '/');
37
+ env = await eval(`import('file://${finalPath}')`);
38
+ }
33
39
  }
34
- const envPath = argv.getEnvArg('env');
35
- if (envPath && fs.existsSync(envPath)) {
36
- Object.assign(env, require(path.resolve(envPath)));
40
+ else {
41
+ throw new Error('Env file not found: ' + filePath);
37
42
  }
38
- Object.assign(env, argv.getAllEnvArgs());
43
+ return env.default || env;
44
+ }
45
+ async function configure() {
46
+ const env = Object.create(null);
47
+ const defaultEnvPath = argv.getEnvArg('default-env');
48
+ const targetEnvPath = argv.getEnvArg('env');
49
+ const defaultEnv = defaultEnvPath ? await readEnvFile(defaultEnvPath) : {};
50
+ const targetEnv = targetEnvPath ? await readEnvFile(targetEnvPath) : {};
51
+ Object.assign(env, defaultEnv, targetEnv, argv.getAllEnvArgs());
39
52
  mergeFlatEnv(env);
40
53
  if ('string' === typeof env.ignore)
41
54
  env.ignore = env.ignore.split(',');
package/bin/starter.js CHANGED
@@ -28,7 +28,7 @@ async function loadEntry(name, entryPath) {
28
28
  }
29
29
  async function startup() {
30
30
  // 加载环境变量
31
- const env = (0, configurer_1.configure)();
31
+ const env = await (0, configurer_1.configure)();
32
32
  Object.assign(oox.config, env);
33
33
  // 获取服务入口地址
34
34
  const entryFile = getEntryFile(env);
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.on = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
3
+ exports.rpc = exports.setLoadBalancePolicy = exports.removeKeepAliveConnection = exports.addKeepAliveConnection = exports.getKeepAliveConnection = exports.getKeepAliveConnections = exports.keepAliveConnections = exports.RPCKeepAliveConnection = exports.stop = exports.serve = exports.getContext = exports.genContext = exports.genTraceId = exports.setGenTraceIdFunction = exports.config = exports.Config = exports.Context = exports.on = exports.logger = exports.execute = exports.call = exports.sourceKVMethods = exports.kvMethods = exports.getMethods = exports.setMethods = exports.asyncStore = exports.modules = exports.ModuleConfig = exports.Module = void 0;
4
4
  const node_crypto_1 = require("node:crypto");
5
5
  const app = require("./app");
6
6
  const utils_1 = require("./utils");
@@ -9,7 +9,7 @@ exports.Module = module_1.default;
9
9
  Object.defineProperty(exports, "ModuleConfig", { enumerable: true, get: function () { return module_1.ModuleConfig; } });
10
10
  const modules_1 = require("./modules");
11
11
  exports.modules = new modules_1.default;
12
- exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.on = app.on;
12
+ exports.asyncStore = app.asyncStore, exports.setMethods = app.setMethods, exports.getMethods = app.getMethods, exports.kvMethods = app.kvMethods, exports.sourceKVMethods = app.sourceKVMethods, exports.call = app.call, exports.execute = app.execute, exports.logger = app.logger, exports.on = app.on;
13
13
  class Context extends app.Context {
14
14
  // 请求溯源IP
15
15
  sourceIP = '';
package/logger.js CHANGED
@@ -1,20 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.error = exports.warning = exports.info = void 0;
3
+ exports.trace = exports.error = exports.warn = exports.info = void 0;
4
4
  const app_1 = require("./app");
5
- function log(tag, ...msgs) {
6
- const context = app_1.asyncStore.getStore();
7
- app_1.eventHub.emit('log', context, { tag, msgs });
8
- }
9
5
  function info(...msgs) {
10
- return log('info', ...msgs);
6
+ const context = app_1.asyncStore.getStore();
7
+ if (context)
8
+ app_1.eventHub.emit('log', context, 'info', msgs);
9
+ else
10
+ console.info('[INFO]', ...msgs);
11
11
  }
12
12
  exports.info = info;
13
- function warning(...msgs) {
14
- return log('warning', ...msgs);
13
+ function warn(...msgs) {
14
+ const context = app_1.asyncStore.getStore();
15
+ if (context)
16
+ app_1.eventHub.emit('log', context, 'warn', msgs);
17
+ else
18
+ console.warn('[WARN]', ...msgs);
15
19
  }
16
- exports.warning = warning;
20
+ exports.warn = warn;
17
21
  function error(...msgs) {
18
- return log('error', ...msgs);
22
+ const context = app_1.asyncStore.getStore();
23
+ if (context)
24
+ app_1.eventHub.emit('log', context, 'error', msgs);
25
+ else
26
+ console.error('[ERROR]', ...msgs);
19
27
  }
20
28
  exports.error = error;
29
+ function trace(name) {
30
+ const context = app_1.asyncStore.getStore();
31
+ const trace = { stack: '' };
32
+ Error.captureStackTrace(trace);
33
+ const stack = trace.stack
34
+ .replace(/.*\n.*logger.js.*\n/, name || 'Untitle\n');
35
+ if (context)
36
+ app_1.eventHub.emit('log', context, 'trace', stack);
37
+ else
38
+ console.log('[TRACE]', stack);
39
+ }
40
+ exports.trace = trace;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oox",
3
- "version": "0.3.0-beta7",
3
+ "version": "0.3.0-beta8",
4
4
  "description": "Graceful NodeJS distributed application solution",
5
5
  "keywords": [
6
6
  "http",
package/types/app.d.ts CHANGED
@@ -28,7 +28,7 @@ export declare function getMethods(): any;
28
28
  export declare function on(event: 'request', listener: (action: string, params: any[], context: Context) => void): void;
29
29
  export declare function on(event: 'success', listener: (action: string, params: any[], context: Context, result: ReturnsBody) => void): void;
30
30
  export declare function on(event: 'fail', listener: (action: string, params: any[], context: Context, error: Error) => void): void;
31
- export declare function on(event: 'log', listener: (context: Context, ...msgs: any[]) => void): void;
31
+ export declare function on(event: 'log', listener: (context: Context, tag: string, msgs: any[]) => void): void;
32
32
  /**
33
33
  * Call an Function on RPC server
34
34
  * @param action
@@ -1 +1 @@
1
- export declare function configure(): any;
1
+ export declare function configure(): Promise<any>;
package/types/index.d.ts CHANGED
@@ -5,7 +5,7 @@ import Modules from './modules';
5
5
  export { ReturnsBody } from './app';
6
6
  export { Module, ModuleConfig };
7
7
  export declare const modules: Modules;
8
- export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, on: typeof app.on;
8
+ export declare const asyncStore: import("async_hooks").AsyncLocalStorage<app.Context>, setMethods: typeof app.setMethods, getMethods: typeof app.getMethods, kvMethods: Map<string, Function>, sourceKVMethods: Map<string, Function>, call: typeof app.call, execute: typeof app.execute, logger: typeof app.logger, on: typeof app.on;
9
9
  export declare class Context extends app.Context {
10
10
  sourceIP: string;
11
11
  ip: string;
package/types/logger.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare function info(...msgs: any[]): void;
2
- export declare function warning(...msgs: any[]): void;
2
+ export declare function warn(...msgs: any[]): void;
3
3
  export declare function error(...msgs: any[]): void;
4
+ export declare function trace(name?: string): void;