@zhin.js/host-http 1.0.3 → 1.0.4

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.
@@ -17,8 +17,7 @@ export function registerConsoleRestPages(host, ctx, options = {}) {
17
17
  const base = normalizeBase(options.apiBase ?? '/api');
18
18
  const fetchFn = options.fetchFn ?? fetch;
19
19
  const pluginRegistryUrl = options.pluginRegistryUrl ?? 'https://zhin.js.org/plugins.json';
20
- const npmRegistryUrl = (options.npmRegistryUrl ?? 'https://registry.npmmirror.com')
21
- .replace(/\/+$/u, '');
20
+ const npmRegistryUrl = trimTrailingSlashes(options.npmRegistryUrl ?? 'https://registry.npmmirror.com');
22
21
  const disposers = [];
23
22
  const route = (method, path, handler, meta) => {
24
23
  const dispose = host.route(method, path, handler, meta);
@@ -672,10 +671,16 @@ function decodePathParam(raw) {
672
671
  return raw;
673
672
  }
674
673
  }
674
+ function trimTrailingSlashes(s) {
675
+ let i = s.length;
676
+ while (i > 0 && s[i - 1] === '/')
677
+ i--;
678
+ return s.slice(0, i);
679
+ }
675
680
  function normalizeBase(value) {
676
681
  if (!value.startsWith('/'))
677
682
  return `/${value}`;
678
- return value.replace(/\/+$/u, '') || '/api';
683
+ return trimTrailingSlashes(value) || '/api';
679
684
  }
680
685
  function writeJson(response, status, body) {
681
686
  const payload = JSON.stringify(body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhin.js/host-http",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Minimal HTTP and WebSocket host for Plugin Runtime (no Koa)",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -17,13 +17,13 @@
17
17
  "src"
18
18
  ],
19
19
  "dependencies": {
20
- "ws": "^8.21.0",
20
+ "ws": "^8.21.1",
21
21
  "@zhin.js/console-protocol": "1.1.0",
22
- "@zhin.js/logger": "1.0.75",
23
- "@zhin.js/plugin-runtime": "1.1.1"
22
+ "@zhin.js/plugin-runtime": "1.1.1",
23
+ "@zhin.js/logger": "1.0.75"
24
24
  },
25
25
  "devDependencies": {
26
- "@types/node": "^26.1.0",
26
+ "@types/node": "^26.1.2",
27
27
  "@types/ws": "^8.18.1",
28
28
  "typescript": "^6.0.3"
29
29
  },
@@ -128,8 +128,9 @@ export function registerConsoleRestPages(
128
128
  const base = normalizeBase(options.apiBase ?? '/api');
129
129
  const fetchFn = options.fetchFn ?? fetch;
130
130
  const pluginRegistryUrl = options.pluginRegistryUrl ?? 'https://zhin.js.org/plugins.json';
131
- const npmRegistryUrl = (options.npmRegistryUrl ?? 'https://registry.npmmirror.com')
132
- .replace(/\/+$/u, '');
131
+ const npmRegistryUrl = trimTrailingSlashes(
132
+ options.npmRegistryUrl ?? 'https://registry.npmmirror.com',
133
+ );
133
134
  const disposers: Array<() => void> = [];
134
135
  const route: typeof host.route = (method, path, handler, meta) => {
135
136
  const dispose = host.route(method, path, handler, meta);
@@ -874,9 +875,15 @@ function decodePathParam(raw: string): string {
874
875
  }
875
876
  }
876
877
 
878
+ function trimTrailingSlashes(s: string): string {
879
+ let i = s.length;
880
+ while (i > 0 && s[i - 1] === '/') i--;
881
+ return s.slice(0, i);
882
+ }
883
+
877
884
  function normalizeBase(value: string): string {
878
885
  if (!value.startsWith('/')) return `/${value}`;
879
- return value.replace(/\/+$/u, '') || '/api';
886
+ return trimTrailingSlashes(value) || '/api';
880
887
  }
881
888
 
882
889
  function writeJson(response: ServerResponse, status: number, body: unknown): void {