@interopio/gateway-server 0.6.0-beta → 0.6.2-beta

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
@@ -159,8 +159,7 @@ var HttpServerRequest = class {
159
159
  return void 0;
160
160
  }
161
161
  const text = await blob.text();
162
- const json = JSON.parse(text);
163
- return json;
162
+ return JSON.parse(text);
164
163
  });
165
164
  }
166
165
  };
@@ -275,15 +274,19 @@ var HttpServerResponse = class {
275
274
  }
276
275
  end(chunk) {
277
276
  if (!this._res.headersSent) {
278
- return new Promise((resolve) => {
279
- if (chunk === void 0) {
280
- this._res.end(() => {
281
- resolve(true);
282
- });
283
- } else {
284
- this._res.end(chunk, () => {
285
- resolve(true);
286
- });
277
+ return new Promise((resolve, reject) => {
278
+ try {
279
+ if (chunk === void 0) {
280
+ this._res.end(() => {
281
+ resolve(true);
282
+ });
283
+ } else {
284
+ this._res.end(chunk, () => {
285
+ resolve(true);
286
+ });
287
+ }
288
+ } catch (e) {
289
+ reject(e instanceof Error ? e : new Error(`end failed: ${e}`));
287
290
  }
288
291
  });
289
292
  } else {
@@ -771,9 +774,10 @@ function routes(connections, config, authorize) {
771
774
  } else {
772
775
  const nodes = json;
773
776
  const result = connections.announce(nodes);
774
- const body = new Blob([JSON.stringify(result)], { type: "application/json" });
777
+ const buffer = Buffer.from(JSON.stringify(result), "utf-8");
775
778
  ctx.response.statusCode = 200;
776
- await ctx.response.end(body);
779
+ ctx.response.headers.set("Content-Type", "application/json").set("Content-Length", buffer.length);
780
+ await ctx.response.end(buffer);
777
781
  }
778
782
  } else {
779
783
  await next();
@@ -1257,9 +1261,9 @@ var localIp = (() => {
1257
1261
  return a.length > 0 ? a[0] : void 0;
1258
1262
  }
1259
1263
  const addresses = Object.values(networkInterfaces()).flatMap((details) => {
1260
- return (details ?? []).filter((info) => info.family === "IPv4");
1261
- }).reduce((acc, info) => {
1262
- acc[info.internal ? "internal" : "external"].push(info);
1264
+ return (details ?? []).filter((info2) => info2.family === "IPv4");
1265
+ }).reduce((acc, info2) => {
1266
+ acc[info2.internal ? "internal" : "external"].push(info2);
1263
1267
  return acc;
1264
1268
  }, { internal: [], external: [] });
1265
1269
  return (first(addresses.internal) ?? first(addresses.external))?.address;
@@ -1479,16 +1483,17 @@ function regexifyOriginFilters(originFilters) {
1479
1483
  }
1480
1484
 
1481
1485
  // src/server/server-header.ts
1486
+ import info from "@interopio/gateway-server/package.json" with { type: "json" };
1482
1487
  var serverHeader = (server) => {
1483
- const enabled = typeof server === "string";
1488
+ server ??= `${info.name} - v${info.version}`;
1484
1489
  return async ({ response }, next) => {
1485
- if (server != false && !response.headers.has("server")) {
1490
+ if (server !== false && !response.headers.has("server")) {
1486
1491
  response.headers.set("Server", server);
1487
1492
  }
1488
1493
  await next();
1489
1494
  };
1490
1495
  };
1491
- var server_header_default = (server = "gateway-server") => serverHeader(server);
1496
+ var server_header_default = (server) => serverHeader(server);
1492
1497
 
1493
1498
  // src/app/route.ts
1494
1499
  function findSocketRoute({ request }, { sockets: routes3 }) {
@@ -1841,14 +1846,25 @@ function authenticationFilter(opts) {
1841
1846
  // src/server/security/oauth2/token-error.ts
1842
1847
  var BearerTokenErrorCodes = {
1843
1848
  invalid_request: "invalid_request",
1844
- invalid_token: "invalid_token"
1849
+ invalid_token: "invalid_token",
1850
+ insufficient_scope: "insufficient_scope"
1845
1851
  };
1846
1852
  var DEFAULT_URI = "https://tools.ietf.org/html/rfc6750#section-3.1";
1847
1853
  function invalidToken(message) {
1848
- return { errorCode: BearerTokenErrorCodes.invalid_token, httpStatus: 401, description: message, uri: DEFAULT_URI };
1854
+ return {
1855
+ errorCode: BearerTokenErrorCodes.invalid_token,
1856
+ httpStatus: 401,
1857
+ description: message,
1858
+ uri: DEFAULT_URI
1859
+ };
1849
1860
  }
1850
1861
  function invalidRequest(message) {
1851
- return { errorCode: BearerTokenErrorCodes.invalid_request, httpStatus: 400, description: message, uri: DEFAULT_URI };
1862
+ return {
1863
+ errorCode: BearerTokenErrorCodes.invalid_request,
1864
+ httpStatus: 400,
1865
+ description: message,
1866
+ uri: DEFAULT_URI
1867
+ };
1852
1868
  }
1853
1869
 
1854
1870
  // src/server/security/oauth2/token-converter.ts
@@ -2133,7 +2149,7 @@ function httpBasic(opts) {
2133
2149
  const preferredMatcher = or([xhrMatcher, restNoHtmlMatcher]);
2134
2150
  opts.defaultEntryPoints.push([preferredMatcher, entryPoint]);
2135
2151
  const failureHandler = opts.failureHandler ?? serverAuthenticationEntryPointFailureHandler({ entryPoint });
2136
- const successHandler = delegatingSuccessHandler(opts.successHandlers === void 0 ? opts.defaultSuccessHandlers : opts.successHandlers);
2152
+ const successHandler = delegatingSuccessHandler(opts.successHandlers ?? opts.defaultSuccessHandlers);
2137
2153
  const converter = httpBasicAuthenticationConverter({});
2138
2154
  return authenticationFilter({
2139
2155
  storage: opts.storage,
@@ -2740,8 +2756,9 @@ async function createCorsConfigSource(context) {
2740
2756
  // src/app/auth.ts
2741
2757
  function createSecurityConfig(context) {
2742
2758
  const authorize = [];
2759
+ const defaultAccess = { access: context.authConfig?.type !== "none" ? "authenticated" : "permitted" };
2743
2760
  for (const [path, route] of context.sockets) {
2744
- const rule = route.authorize ?? { access: "authenticated" };
2761
+ const rule = route.authorize ?? defaultAccess;
2745
2762
  let matcher = pattern(path, { method: "GET" });
2746
2763
  matcher = and([upgradeMatcher, matcher]);
2747
2764
  authorize.push([matcher, rule]);
@@ -2752,7 +2769,7 @@ function createSecurityConfig(context) {
2752
2769
  if (context.authorize.length > 0) {
2753
2770
  authorize.push(...context.authorize);
2754
2771
  }
2755
- authorize.push(["any-exchange", { access: "authenticated" }]);
2772
+ authorize.push(["any-exchange", defaultAccess]);
2756
2773
  return {
2757
2774
  authorize,
2758
2775
  basic: {
@@ -2796,23 +2813,23 @@ async function createListener(middleware, context, onSocketError) {
2796
2813
  if ((request.method === "GET" || request.method === "CONNECT") && upgradeMatchResult.match) {
2797
2814
  const socket = request.socket;
2798
2815
  const host = request.host;
2799
- const info = socketKey(request._req.socket);
2816
+ const info2 = socketKey(request._req.socket);
2800
2817
  if (route.wss) {
2801
2818
  socket.removeListener("error", onSocketError);
2802
2819
  const wss = route.wss;
2803
2820
  if (route.maxConnections !== void 0 && wss.clients?.size >= route.maxConnections) {
2804
- logger10.warn(`${info} dropping ws connection request from ${host} on ${path}. max connections exceeded.`);
2821
+ logger10.warn(`${info2} dropping ws connection request from ${host} on ${path}. max connections exceeded.`);
2805
2822
  socket.destroy();
2806
2823
  return;
2807
2824
  }
2808
2825
  const origin = request.headers.one("origin");
2809
2826
  if (!acceptsOrigin(origin, route.originFilters)) {
2810
- logger10.info(`${info} dropping ws connection request from ${host} on ${path}. origin ${origin ?? "<missing>"}`);
2827
+ logger10.info(`${info2} dropping ws connection request from ${host} on ${path}. origin ${origin ?? "<missing>"}`);
2811
2828
  socket.destroy();
2812
2829
  return;
2813
2830
  }
2814
2831
  if (logger10.enabledFor("debug")) {
2815
- logger10.debug(`${info} accepted new ws connection request from ${host} on ${path}`);
2832
+ logger10.debug(`${info2} accepted new ws connection request from ${host} on ${path}`);
2816
2833
  }
2817
2834
  wss.handleUpgrade(request._req, socket, request._req["_upgradeHead"], (ws) => {
2818
2835
  response._res["_header"] = true;
@@ -2822,7 +2839,7 @@ async function createListener(middleware, context, onSocketError) {
2822
2839
  wss.emit("connection", ws, request._req);
2823
2840
  });
2824
2841
  } else {
2825
- logger10.warn(`${info} rejected upgrade request from ${host} on ${path}`);
2842
+ logger10.warn(`${info2} rejected upgrade request from ${host} on ${path}`);
2826
2843
  socket.destroy();
2827
2844
  }
2828
2845
  } else {
@@ -2976,12 +2993,12 @@ var Factory = async (options) => {
2976
2993
  }
2977
2994
  });
2978
2995
  server2.on("listening", async () => {
2979
- const info = server2.address();
2996
+ const info2 = server2.address();
2980
2997
  for (const [path, route] of context.sockets) {
2981
2998
  try {
2982
2999
  logger10.info(`creating ws server for [${path}]. max connections: ${route.maxConnections ?? "<unlimited>"}, origin filters: ${route.originFilters ? JSON.stringify(route.originFilters, regexAwareReplacer) : "<none>"}`);
2983
3000
  const wss = new WebSocketServer({ noServer: true });
2984
- const endpoint = `${ssl ? "wss" : "ws"}://${localIp}:${info.port}${path}`;
3001
+ const endpoint = `${ssl ? "wss" : "ws"}://${localIp}:${info2.port}${path}`;
2985
3002
  const handler = await route.factory({ endpoint, wss, storage: context.storage });
2986
3003
  const pingInterval = route.ping;
2987
3004
  if (pingInterval) {
@@ -3004,7 +3021,7 @@ var Factory = async (options) => {
3004
3021
  logger10.warn(`failed to init route ${path}`, e);
3005
3022
  }
3006
3023
  }
3007
- logger10.info(`http server listening on ${info.address}:${info.port}`);
3024
+ logger10.info(`http server listening on ${info2.address}:${info2.port}`);
3008
3025
  resolve(server2);
3009
3026
  });
3010
3027
  server2.on("upgrade", (req, socket, head) => {