@nocobase/server 0.17.0-alpha.4 → 0.17.0-alpha.6

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.
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  /// <reference types="koa-bodyparser" />
3
4
  import { ACL } from '@nocobase/acl';
4
5
  import { AuthManager, AuthManagerOptions } from '@nocobase/auth';
@@ -18,6 +19,7 @@ import { ApplicationVersion } from './helpers/application-version';
18
19
  import { Locale } from './locale';
19
20
  import { Plugin } from './plugin';
20
21
  import { InstallOptions, PluginManager } from './plugin-manager';
22
+ import { RecordableHistogram } from 'node:perf_hooks';
21
23
  export type PluginType = string | typeof Plugin;
22
24
  export type PluginConfiguration = PluginType | [PluginType, any];
23
25
  export interface ResourcerOptions {
@@ -38,6 +40,7 @@ export interface ApplicationOptions {
38
40
  pmSock?: string;
39
41
  name?: string;
40
42
  authManager?: AuthManagerOptions;
43
+ perfHooks?: boolean;
41
44
  }
42
45
  export interface DefaultState extends KoaDefaultState {
43
46
  currentUser?: any;
@@ -80,6 +83,7 @@ export declare class Application<StateT = DefaultState, ContextT = DefaultContex
80
83
  name: string;
81
84
  };
82
85
  running: boolean;
86
+ perfHistograms: Map<string, RecordableHistogram>;
83
87
  protected plugins: Map<string, Plugin<any>>;
84
88
  protected _appSupervisor: AppSupervisor;
85
89
  protected _started: boolean;
@@ -67,6 +67,7 @@ const _Application = class _Application extends import_koa.default {
67
67
  rawOptions;
68
68
  activatedCommand = null;
69
69
  running = false;
70
+ perfHistograms = /* @__PURE__ */ new Map();
70
71
  plugins = /* @__PURE__ */ new Map();
71
72
  _appSupervisor = import_app_supervisor.AppSupervisor.getInstance();
72
73
  _started;
@@ -395,6 +396,9 @@ const _Application = class _Application extends import_koa.default {
395
396
  } catch (e) {
396
397
  this.log.error(e);
397
398
  }
399
+ if (this._cacheManager) {
400
+ await this._cacheManager.close();
401
+ }
398
402
  await this.emitAsync("afterStop", this, options);
399
403
  this.stopped = true;
400
404
  this.log.info(`${this.name} is stopped`);
@@ -526,6 +530,9 @@ const _Application = class _Application extends import_koa.default {
526
530
  this._resourcer.use(this._acl.middleware(), { tag: "acl", after: ["auth"] });
527
531
  }
528
532
  this._locales = new import_locale.Locale((0, import_helper.createAppProxy)(this));
533
+ if (options.perfHooks) {
534
+ (0, import_helper.enablePerfHooks)(this);
535
+ }
529
536
  (0, import_helper.registerMiddlewares)(this, options);
530
537
  if (options.registerActions !== false) {
531
538
  (0, import_actions.registerActions)(this);
package/lib/helper.d.ts CHANGED
@@ -9,3 +9,4 @@ export declare function registerMiddlewares(app: Application, options: Applicati
9
9
  export declare const createAppProxy: (app: Application) => Application<import("./application").DefaultState, import("./application").DefaultContext>;
10
10
  export declare const getCommandFullName: (command: Command) => string;
11
11
  export declare const tsxRerunning: () => Promise<void>;
12
+ export declare const enablePerfHooks: (app: Application) => void;
package/lib/helper.js CHANGED
@@ -32,6 +32,7 @@ __export(helper_exports, {
32
32
  createDatabase: () => createDatabase,
33
33
  createI18n: () => createI18n,
34
34
  createResourcer: () => createResourcer,
35
+ enablePerfHooks: () => enablePerfHooks,
35
36
  getCommandFullName: () => getCommandFullName,
36
37
  registerMiddlewares: () => registerMiddlewares,
37
38
  tsxRerunning: () => tsxRerunning
@@ -50,6 +51,7 @@ var import_data_template = require("./middlewares/data-template");
50
51
  var import_data_wrapping = require("./middlewares/data-wrapping");
51
52
  var import_db2resource = require("./middlewares/db2resource");
52
53
  var import_i18n = require("./middlewares/i18n");
54
+ var import_perf_hooks = require("perf_hooks");
53
55
  function createI18n(options) {
54
56
  const instance = import_i18next.default.createInstance();
55
57
  instance.init({
@@ -111,7 +113,10 @@ function registerMiddlewares(app, options) {
111
113
  if (options.dataWrapping !== false) {
112
114
  app.use((0, import_data_wrapping.dataWrapping)(), { tag: "dataWrapping", after: "i18n" });
113
115
  }
114
- app.resourcer.use(import_middlewares.parseVariables, { tag: "parseVariables", after: "acl" });
116
+ app.resourcer.use(import_middlewares.parseVariables, {
117
+ tag: "parseVariables",
118
+ after: "acl"
119
+ });
115
120
  app.resourcer.use(import_data_template.dateTemplate, { tag: "dateTemplate", after: "acl" });
116
121
  app.use(import_db2resource.db2resource, { tag: "db2resource", after: "dataWrapping" });
117
122
  app.use(app.resourcer.restApiMiddleware(), { tag: "restApi", after: "db2resource" });
@@ -147,12 +152,42 @@ const tsxRerunning = /* @__PURE__ */ __name(async () => {
147
152
  const file = (0, import_path.resolve)(process.cwd(), "storage/app.watch.ts");
148
153
  await import_fs.default.promises.writeFile(file, `export const watchId = '${(0, import_utils.uid)()}';`, "utf-8");
149
154
  }, "tsxRerunning");
155
+ const enablePerfHooks = /* @__PURE__ */ __name((app) => {
156
+ app.context.getPerfHistogram = (name) => {
157
+ if (!app.perfHistograms.has(name)) {
158
+ app.perfHistograms.set(name, (0, import_perf_hooks.createHistogram)());
159
+ }
160
+ return app.perfHistograms.get(name);
161
+ };
162
+ app.resourcer.define({
163
+ name: "perf",
164
+ actions: {
165
+ view: async (ctx, next) => {
166
+ const result = {};
167
+ const histograms = ctx.app.perfHistograms;
168
+ const sortedHistograms = [...histograms.entries()].sort(([i, a], [j, b]) => b.mean - a.mean);
169
+ sortedHistograms.forEach(([name, histogram]) => {
170
+ result[name] = histogram;
171
+ });
172
+ ctx.body = result;
173
+ await next();
174
+ },
175
+ reset: async (ctx, next) => {
176
+ const histograms = ctx.app.perfHistograms;
177
+ histograms.forEach((histogram) => histogram.reset());
178
+ await next();
179
+ }
180
+ }
181
+ });
182
+ app.acl.allow("perf", "*", "public");
183
+ }, "enablePerfHooks");
150
184
  // Annotate the CommonJS export names for ESM import in node:
151
185
  0 && (module.exports = {
152
186
  createAppProxy,
153
187
  createDatabase,
154
188
  createI18n,
155
189
  createResourcer,
190
+ enablePerfHooks,
156
191
  getCommandFullName,
157
192
  registerMiddlewares,
158
193
  tsxRerunning
@@ -6,6 +6,7 @@ export declare class Locale {
6
6
  defaultLang: string;
7
7
  localeFn: Map<any, any>;
8
8
  resourceCached: Map<any, any>;
9
+ i18nInstances: Map<any, any>;
9
10
  constructor(app: Application);
10
11
  load(): Promise<void>;
11
12
  setLocaleFn(name: string, fn: (lang: string) => Promise<any>): void;
@@ -16,4 +17,5 @@ export declare class Locale {
16
17
  loadResourcesByLang(lang: string): Promise<void>;
17
18
  getCacheResources(lang: string): Promise<any>;
18
19
  getResources(lang: string): {};
20
+ getI18nInstance(lang: string): Promise<any>;
19
21
  }
@@ -29,6 +29,7 @@ const _Locale = class _Locale {
29
29
  defaultLang = "en-US";
30
30
  localeFn = /* @__PURE__ */ new Map();
31
31
  resourceCached = /* @__PURE__ */ new Map();
32
+ i18nInstances = /* @__PURE__ */ new Map();
32
33
  constructor(app) {
33
34
  this.app = app;
34
35
  this.app.on("afterLoad", async () => {
@@ -105,6 +106,17 @@ const _Locale = class _Locale {
105
106
  });
106
107
  return resources;
107
108
  }
109
+ async getI18nInstance(lang) {
110
+ if (lang === "*" || !lang) {
111
+ return this.app.i18n.cloneInstance({ initImmediate: false });
112
+ }
113
+ let instance = this.i18nInstances.get(lang);
114
+ if (!instance) {
115
+ instance = this.app.i18n.cloneInstance({ initImmediate: false });
116
+ this.i18nInstances.set(lang, instance);
117
+ }
118
+ return instance;
119
+ }
108
120
  };
109
121
  __name(_Locale, "Locale");
110
122
  let Locale = _Locale;
@@ -22,17 +22,18 @@ __export(i18n_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(i18n_exports);
24
24
  async function i18n(ctx, next) {
25
- const i18n2 = ctx.app.i18n.cloneInstance({ initImmediate: false });
26
- ctx.i18n = i18n2;
27
- ctx.t = i18n2.t.bind(i18n2);
28
25
  ctx.getCurrentLocale = () => {
29
26
  const lng2 = ctx.get("X-Locale") || ctx.request.query.locale || ctx.app.i18n.language || ctx.acceptsLanguages().shift() || "en-US";
30
27
  return lng2;
31
28
  };
32
29
  const lng = ctx.getCurrentLocale();
30
+ const localeManager = ctx.app.localeManager;
31
+ const i18n2 = await localeManager.getI18nInstance(lng);
32
+ ctx.i18n = i18n2;
33
+ ctx.t = i18n2.t.bind(i18n2);
33
34
  if (lng !== "*" && lng) {
34
35
  i18n2.changeLanguage(lng);
35
- await ctx.app.localeManager.loadResourcesByLang(lng);
36
+ await localeManager.loadResourcesByLang(lng);
36
37
  }
37
38
  await next();
38
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/server",
3
- "version": "0.17.0-alpha.4",
3
+ "version": "0.17.0-alpha.6",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "Apache-2.0",
@@ -10,16 +10,16 @@
10
10
  "@koa/cors": "^3.1.0",
11
11
  "@koa/multer": "^3.0.2",
12
12
  "@koa/router": "^9.4.0",
13
- "@nocobase/acl": "0.17.0-alpha.4",
14
- "@nocobase/actions": "0.17.0-alpha.4",
15
- "@nocobase/auth": "0.17.0-alpha.4",
16
- "@nocobase/cache": "0.17.0-alpha.4",
17
- "@nocobase/database": "0.17.0-alpha.4",
18
- "@nocobase/evaluators": "0.17.0-alpha.4",
19
- "@nocobase/logger": "0.17.0-alpha.4",
20
- "@nocobase/resourcer": "0.17.0-alpha.4",
21
- "@nocobase/sdk": "0.17.0-alpha.4",
22
- "@nocobase/utils": "0.17.0-alpha.4",
13
+ "@nocobase/acl": "0.17.0-alpha.6",
14
+ "@nocobase/actions": "0.17.0-alpha.6",
15
+ "@nocobase/auth": "0.17.0-alpha.6",
16
+ "@nocobase/cache": "0.17.0-alpha.6",
17
+ "@nocobase/database": "0.17.0-alpha.6",
18
+ "@nocobase/evaluators": "0.17.0-alpha.6",
19
+ "@nocobase/logger": "0.17.0-alpha.6",
20
+ "@nocobase/resourcer": "0.17.0-alpha.6",
21
+ "@nocobase/sdk": "0.17.0-alpha.6",
22
+ "@nocobase/utils": "0.17.0-alpha.6",
23
23
  "@types/decompress": "4.2.4",
24
24
  "@types/ini": "^1.3.31",
25
25
  "@types/koa-send": "^4.1.3",
@@ -52,5 +52,5 @@
52
52
  "@types/serve-handler": "^6.1.1",
53
53
  "@types/ws": "^8.5.5"
54
54
  },
55
- "gitHead": "663b03a3799a70ba1a2bc6a0d686e679331a50ad"
55
+ "gitHead": "f92f8bcdf6d5baf07566381e9425ebca11e19626"
56
56
  }