@thinkbun/plugin 1.0.1 → 1.0.3

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 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/logger.ts","../src/server.ts"],"sourcesContent":[],"mappings":";;;cAOa,cAAc;;;cCGd,cAAc"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/logger.ts","../src/server.ts"],"sourcesContent":[],"mappings":";;;cAOa,cAAc;;;cCEd,cAAc"}
package/dist/index.mjs CHANGED
@@ -2,7 +2,6 @@ import { invariant } from "@thinkbun/core";
2
2
  import pc from "picocolors";
3
3
  import cluster from "node:cluster";
4
4
  import os from "node:os";
5
- import { isNumber } from "lodash-es";
6
5
 
7
6
  //#region src/logger.ts
8
7
  const colorMap = {
@@ -34,7 +33,7 @@ const ServerPlugin = {
34
33
  run(app) {
35
34
  isPrimary = cluster.isPrimary;
36
35
  const { workers: configWorkers } = app.loader.config;
37
- invariant(isNumber(configWorkers), "config.workers must be a number");
36
+ invariant(typeof configWorkers === "number", "config.workers must be a number");
38
37
  const cpuCount = os.cpus().length;
39
38
  const workers = configWorkers === 0 ? cpuCount : Math.max(1, Math.min(cpuCount, configWorkers));
40
39
  if (isPrimary && workers > 1) {
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["LoggerPlugin: Plugin","ServerPlugin: Plugin"],"sources":["../src/logger.ts","../src/server.ts"],"sourcesContent":["import { type Plugin } from '@thinkbun/core';\nimport pc from 'picocolors';\n\nconst colorMap = {\n app: 'yellow',\n PluginManager: 'yellow',\n};\nexport const LoggerPlugin: Plugin = {\n name: 'LoggerPlugin',\n enforce: 'pre',\n setup(app) {\n const genScope = (scope: string | string[]) => (Array.isArray(scope) ? scope.map((el) => `[${el}]`) : `[${scope}]`);\n app.logger = {\n // @ts-ignore\n info: (msg: string, scope = 'app') => console.log(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n error: (msg: string, scope = 'app') => console.error(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n debug: (msg: string, scope = 'app') => console.debug(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n warn: (msg: string, scope = 'app') => console.warn(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n };\n },\n};\n","import cluster from 'node:cluster';\nimport os from 'node:os';\n\nimport { invariant, type Plugin } from '@thinkbun/core';\nimport { isNumber } from 'lodash-es';\n\nconst isTest = typeof Bun !== 'undefined' && Bun.env.NODE_ENV === 'test';\n\nlet shuttingDown = false;\nlet isPrimary = false;\nexport const ServerPlugin: Plugin = {\n name: 'ServerPlugin',\n enforce: 'post',\n run(app) {\n isPrimary = cluster.isPrimary;\n const { workers: configWorkers } = app.loader.config;\n invariant(isNumber(configWorkers), 'config.workers must be a number');\n const cpuCount = os.cpus().length;\n const workers = configWorkers === 0 ? cpuCount : Math.max(1, Math.min(cpuCount, configWorkers));\n if (isPrimary && workers > 1) {\n for (let i = 0; i < workers; i++) {\n cluster.fork();\n }\n\n (cluster as any).on('exit', () => {\n if (shuttingDown) return;\n cluster.fork();\n });\n } else {\n app.server = Bun.serve({\n port: app.loader.config.port,\n hostname: app.loader.config.host,\n reusePort: true,\n routes: app.routes,\n });\n\n process.on('message', async (msg: { type: string }) => {\n if (typeof msg === 'object' && msg !== null && msg.type === 'shutdown') {\n await app.server?.stop?.();\n process.exit(0);\n }\n });\n }\n },\n async close(app) {\n if (isTest) {\n // 只关闭 server,不退出进程\n await app.server?.stop?.();\n return;\n }\n // 只允许 primary 主动关闭 cluster\n if (!isPrimary) {\n await app.server?.stop?.();\n process.exit(0);\n }\n\n if (shuttingDown) return;\n shuttingDown = true;\n\n // 通知所有 worker\n for (const id in cluster.workers) {\n cluster.workers[id]?.send({ type: 'shutdown' });\n }\n\n // 等待 worker 退出 or 强杀\n await new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n for (const id in cluster.workers) {\n cluster.workers[id]?.kill('SIGKILL');\n }\n resolve();\n }, 5000);\n (cluster as any).on('exit', () => {\n if (Object.keys(cluster?.workers || {}).length === 0) {\n clearTimeout(timer);\n resolve();\n }\n });\n });\n\n process.exit(0);\n },\n};\n"],"mappings":";;;;;;;AAGA,MAAM,WAAW;CACf,KAAK;CACL,eAAe;CAChB;AACD,MAAaA,eAAuB;CAClC,MAAM;CACN,SAAS;CACT,MAAM,KAAK;EACT,MAAM,YAAY,UAA8B,MAAM,QAAQ,MAAM,GAAG,MAAM,KAAK,OAAO,IAAI,GAAG,GAAG,GAAG,IAAI,MAAM;AAChH,MAAI,SAAS;GAEX,OAAO,KAAa,QAAQ,UAAU,QAAQ,IAAI,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAExG,QAAQ,KAAa,QAAQ,UAAU,QAAQ,MAAM,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAE3G,QAAQ,KAAa,QAAQ,UAAU,QAAQ,MAAM,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAE3G,OAAO,KAAa,QAAQ,UAAU,QAAQ,KAAK,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAC1G;;CAEJ;;;;ACjBD,MAAM,SAAS,OAAO,QAAQ,eAAe,IAAI,IAAI,aAAa;AAElE,IAAI,eAAe;AACnB,IAAI,YAAY;AAChB,MAAaC,eAAuB;CAClC,MAAM;CACN,SAAS;CACT,IAAI,KAAK;AACP,cAAY,QAAQ;EACpB,MAAM,EAAE,SAAS,kBAAkB,IAAI,OAAO;AAC9C,YAAU,SAAS,cAAc,EAAE,kCAAkC;EACrE,MAAM,WAAW,GAAG,MAAM,CAAC;EAC3B,MAAM,UAAU,kBAAkB,IAAI,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,UAAU,cAAc,CAAC;AAC/F,MAAI,aAAa,UAAU,GAAG;AAC5B,QAAK,IAAI,IAAI,GAAG,IAAI,SAAS,IAC3B,SAAQ,MAAM;AAGhB,GAAC,QAAgB,GAAG,cAAc;AAChC,QAAI,aAAc;AAClB,YAAQ,MAAM;KACd;SACG;AACL,OAAI,SAAS,IAAI,MAAM;IACrB,MAAM,IAAI,OAAO,OAAO;IACxB,UAAU,IAAI,OAAO,OAAO;IAC5B,WAAW;IACX,QAAQ,IAAI;IACb,CAAC;AAEF,WAAQ,GAAG,WAAW,OAAO,QAA0B;AACrD,QAAI,OAAO,QAAQ,YAAY,QAAQ,QAAQ,IAAI,SAAS,YAAY;AACtE,WAAM,IAAI,QAAQ,QAAQ;AAC1B,aAAQ,KAAK,EAAE;;KAEjB;;;CAGN,MAAM,MAAM,KAAK;AACf,MAAI,QAAQ;AAEV,SAAM,IAAI,QAAQ,QAAQ;AAC1B;;AAGF,MAAI,CAAC,WAAW;AACd,SAAM,IAAI,QAAQ,QAAQ;AAC1B,WAAQ,KAAK,EAAE;;AAGjB,MAAI,aAAc;AAClB,iBAAe;AAGf,OAAK,MAAM,MAAM,QAAQ,QACvB,SAAQ,QAAQ,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAIjD,QAAM,IAAI,SAAe,YAAY;GACnC,MAAM,QAAQ,iBAAiB;AAC7B,SAAK,MAAM,MAAM,QAAQ,QACvB,SAAQ,QAAQ,KAAK,KAAK,UAAU;AAEtC,aAAS;MACR,IAAK;AACR,GAAC,QAAgB,GAAG,cAAc;AAChC,QAAI,OAAO,KAAK,SAAS,WAAW,EAAE,CAAC,CAAC,WAAW,GAAG;AACpD,kBAAa,MAAM;AACnB,cAAS;;KAEX;IACF;AAEF,UAAQ,KAAK,EAAE;;CAElB"}
1
+ {"version":3,"file":"index.mjs","names":["LoggerPlugin: Plugin","ServerPlugin: Plugin"],"sources":["../src/logger.ts","../src/server.ts"],"sourcesContent":["import { type Plugin } from '@thinkbun/core';\nimport pc from 'picocolors';\n\nconst colorMap = {\n app: 'yellow',\n PluginManager: 'yellow',\n};\nexport const LoggerPlugin: Plugin = {\n name: 'LoggerPlugin',\n enforce: 'pre',\n setup(app) {\n const genScope = (scope: string | string[]) => (Array.isArray(scope) ? scope.map((el) => `[${el}]`) : `[${scope}]`);\n app.logger = {\n // @ts-ignore\n info: (msg: string, scope = 'app') => console.log(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n error: (msg: string, scope = 'app') => console.error(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n debug: (msg: string, scope = 'app') => console.debug(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n // @ts-ignore\n warn: (msg: string, scope = 'app') => console.warn(pc[colorMap[scope] || 'yellow'](genScope(scope)), msg),\n };\n },\n};\n","import cluster from 'node:cluster';\nimport os from 'node:os';\n\nimport { invariant, type Plugin } from '@thinkbun/core';\n\nconst isTest = typeof Bun !== 'undefined' && Bun.env.NODE_ENV === 'test';\n\nlet shuttingDown = false;\nlet isPrimary = false;\nexport const ServerPlugin: Plugin = {\n name: 'ServerPlugin',\n enforce: 'post',\n run(app) {\n isPrimary = cluster.isPrimary;\n const { workers: configWorkers } = app.loader.config;\n invariant(typeof configWorkers === 'number', 'config.workers must be a number');\n const cpuCount = os.cpus().length;\n const workers = configWorkers === 0 ? cpuCount : Math.max(1, Math.min(cpuCount, configWorkers));\n if (isPrimary && workers > 1) {\n for (let i = 0; i < workers; i++) {\n cluster.fork();\n }\n\n (cluster as any).on('exit', () => {\n if (shuttingDown) return;\n cluster.fork();\n });\n } else {\n app.server = Bun.serve({\n port: app.loader.config.port,\n hostname: app.loader.config.host,\n reusePort: true,\n routes: app.routes,\n });\n\n process.on('message', async (msg: { type: string }) => {\n if (typeof msg === 'object' && msg !== null && msg.type === 'shutdown') {\n await app.server?.stop?.();\n process.exit(0);\n }\n });\n }\n },\n async close(app) {\n if (isTest) {\n // 只关闭 server,不退出进程\n await app.server?.stop?.();\n return;\n }\n // 只允许 primary 主动关闭 cluster\n if (!isPrimary) {\n await app.server?.stop?.();\n process.exit(0);\n }\n\n if (shuttingDown) return;\n shuttingDown = true;\n\n // 通知所有 worker\n for (const id in cluster.workers) {\n cluster.workers[id]?.send({ type: 'shutdown' });\n }\n\n // 等待 worker 退出 or 强杀\n await new Promise<void>((resolve) => {\n const timer = setTimeout(() => {\n for (const id in cluster.workers) {\n cluster.workers[id]?.kill('SIGKILL');\n }\n resolve();\n }, 5000);\n (cluster as any).on('exit', () => {\n if (Object.keys(cluster?.workers || {}).length === 0) {\n clearTimeout(timer);\n resolve();\n }\n });\n });\n\n process.exit(0);\n },\n};\n"],"mappings":";;;;;;AAGA,MAAM,WAAW;CACf,KAAK;CACL,eAAe;CAChB;AACD,MAAaA,eAAuB;CAClC,MAAM;CACN,SAAS;CACT,MAAM,KAAK;EACT,MAAM,YAAY,UAA8B,MAAM,QAAQ,MAAM,GAAG,MAAM,KAAK,OAAO,IAAI,GAAG,GAAG,GAAG,IAAI,MAAM;AAChH,MAAI,SAAS;GAEX,OAAO,KAAa,QAAQ,UAAU,QAAQ,IAAI,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAExG,QAAQ,KAAa,QAAQ,UAAU,QAAQ,MAAM,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAE3G,QAAQ,KAAa,QAAQ,UAAU,QAAQ,MAAM,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAE3G,OAAO,KAAa,QAAQ,UAAU,QAAQ,KAAK,GAAG,SAAS,UAAU,UAAU,SAAS,MAAM,CAAC,EAAE,IAAI;GAC1G;;CAEJ;;;;AClBD,MAAM,SAAS,OAAO,QAAQ,eAAe,IAAI,IAAI,aAAa;AAElE,IAAI,eAAe;AACnB,IAAI,YAAY;AAChB,MAAaC,eAAuB;CAClC,MAAM;CACN,SAAS;CACT,IAAI,KAAK;AACP,cAAY,QAAQ;EACpB,MAA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AM;AACnB,cAAS;;KAEX;IACF;AAEF,UAAQ,KAAK,EAAE;;CAElB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thinkbun/plugin",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dist/index.mjs",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "dependencies": {
22
22
  "picocolors": "^1.1.1",
23
- "@thinkbun/core": "workspace:*"
23
+ "@thinkbun/core": "^1.0.0"
24
24
  },
25
25
  "files": [
26
26
  "dist"