@interopio/gateway-server 0.8.1-beta.2 → 0.9.0-beta.1

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
@@ -13,7 +13,7 @@ import http2 from "node:http";
13
13
  import https from "node:https";
14
14
  import { readFileSync } from "node:fs";
15
15
  import { AsyncLocalStorage as AsyncLocalStorage4 } from "node:async_hooks";
16
- import { IOGateway as IOGateway6 } from "@interopio/gateway";
16
+ import { IOGateway as IOGateway7 } from "@interopio/gateway";
17
17
 
18
18
  // src/logger.ts
19
19
  import { IOGateway } from "@interopio/gateway";
@@ -1371,14 +1371,15 @@ upgradeMatcher.toString = () => "websocket upgrade";
1371
1371
  // src/app/route.ts
1372
1372
  import { IOGateway as IOGateway4 } from "@interopio/gateway";
1373
1373
  async function configure(app, config, routes) {
1374
- const applyCors = (matcher, requestMethod, options) => {
1374
+ const applyCors = (request, options) => {
1375
1375
  if (options?.cors) {
1376
1376
  const cors = options.cors === true ? {
1377
1377
  allowOrigins: options.origins?.allow?.map(IOGateway4.Filtering.regexify),
1378
- allowMethods: requestMethod == void 0 ? void 0 : [requestMethod],
1379
- allowCredentials: options.authorize?.access !== "permitted"
1378
+ allowMethods: request.method === void 0 ? ["*"] : [request.method],
1379
+ allowCredentials: options.authorize?.access !== "permitted" ? true : void 0
1380
1380
  } : options.cors;
1381
- routes.cors.push([matcher, cors]);
1381
+ const path = request.path;
1382
+ routes.cors.push([path, cors]);
1382
1383
  }
1383
1384
  };
1384
1385
  const configurer = new class {
@@ -1388,7 +1389,7 @@ async function configure(app, config, routes) {
1388
1389
  if (options?.authorize) {
1389
1390
  routes.authorize.push([matcher, options.authorize]);
1390
1391
  }
1391
- applyCors(matcher, request.method, options);
1392
+ applyCors(request, options);
1392
1393
  const middleware = async (exchange, next) => {
1393
1394
  const { match: match2, variables } = await matcher(exchange);
1394
1395
  if (match2) {
@@ -1721,210 +1722,62 @@ var matchingCorsConfigSource = (opts) => {
1721
1722
  };
1722
1723
  };
1723
1724
 
1724
- // src/mock/server/exchange.ts
1725
- import stream from "node:stream/web";
1726
- var MockServerHttpRequest = class extends AbstractHttpRequest {
1727
- #url;
1728
- #body;
1729
- upgrade = false;
1730
- constructor(url, method) {
1731
- super(new MapHttpHeaders());
1732
- if (typeof url === "string") {
1733
- if (URL.canParse(url)) {
1734
- url = new URL(url);
1735
- } else if (URL.canParse(url, "http://localhost")) {
1736
- url = new URL(url, "http://localhost");
1737
- } else {
1738
- throw new TypeError("URL cannot parse url");
1739
- }
1740
- }
1741
- this.#url = url;
1742
- this.method = method ?? "GET";
1743
- this.setHeader("Host", this.#url.hostname);
1744
- this.path = this.#url.pathname ?? "/";
1745
- }
1746
- method;
1747
- path;
1748
- get host() {
1749
- return super.parseHost(this.#url.host);
1750
- }
1751
- get protocol() {
1752
- return super.parseProtocol(this.#url.protocol.slice(0, -1));
1753
- }
1754
- get body() {
1755
- if (this.#body !== void 0) {
1756
- const blob = this.#body;
1757
- const asyncIterator = async function* () {
1758
- const bytes = await blob.bytes();
1759
- yield bytes;
1760
- }();
1761
- return stream.ReadableStream.from(asyncIterator);
1762
- }
1763
- }
1764
- blob() {
1765
- const body = this.#body;
1766
- return body ? Promise.resolve(body) : Promise.reject(new Error(`no body set`));
1767
- }
1768
- async text() {
1769
- const blob = await this.blob();
1770
- return await blob.text();
1771
- }
1772
- async formData() {
1773
- const blob = await this.blob();
1774
- const text = await blob.text();
1775
- return new URLSearchParams(text);
1776
- }
1777
- async json() {
1778
- const blob = await this.blob();
1779
- if (blob.size === 0) {
1780
- return void 0;
1781
- }
1782
- const text = await blob.text();
1783
- return JSON.parse(text);
1784
- }
1785
- async writeBody(body) {
1786
- if (body === void 0) {
1787
- this.#body = new Blob([]);
1788
- return;
1789
- }
1790
- if (body instanceof ReadableStream) {
1791
- const chunks = [];
1792
- const reader = body.getReader();
1793
- let done = false;
1794
- while (!done) {
1795
- const { value, done: readDone } = await reader.read();
1796
- if (readDone) {
1797
- done = true;
1798
- } else {
1799
- chunks.push(value);
1800
- }
1801
- }
1802
- this.#body = new Blob(chunks);
1803
- } else {
1804
- body = await body;
1805
- if (typeof body === "string") {
1806
- this.#body = new Blob([body], { type: "text/plain" });
1807
- } else {
1808
- this.#body = new Blob([body]);
1809
- }
1810
- }
1811
- if (!this.headers.has("content-type")) {
1812
- this.setHeader("Content-Type", this.#body.type || "application/octet-stream");
1813
- }
1814
- }
1815
- get URL() {
1816
- return new URL(this.path + this.#url.search + this.#url.hash, `${this.protocol}://${this.host}`);
1817
- }
1818
- addHeader(name, value) {
1819
- this.headers.add(name, value);
1820
- return this;
1821
- }
1822
- setHeader(name, value) {
1823
- this.headers.set(name, value);
1824
- return this;
1825
- }
1826
- };
1827
- var MockServerHttpResponse = class extends AbstractServerHttpResponse {
1828
- #writeHandler;
1829
- #body = () => {
1830
- throw new Error("No content was written to the response body nor end was called on this response.");
1831
- };
1832
- constructor() {
1833
- super(new MapHttpHeaders());
1834
- this.#writeHandler = async (body) => {
1835
- const chunks = [];
1836
- let bodyStream;
1837
- this.#body = () => {
1838
- bodyStream ??= stream.ReadableStream.from(chunks);
1839
- return bodyStream;
1840
- };
1841
- const reader = body.getReader();
1842
- let done = false;
1843
- do {
1844
- const { value, done: readDone } = await reader.read();
1845
- if (readDone) {
1846
- done = true;
1847
- } else {
1848
- chunks.push(value);
1849
- }
1850
- } while (!done);
1851
- return true;
1852
- };
1853
- }
1854
- get statusCode() {
1855
- return super.statusCode;
1856
- }
1857
- set writeHandler(handler) {
1858
- this.#body = () => {
1859
- throw new Error("Not available with custom write handler");
1860
- };
1861
- this.#writeHandler = handler;
1862
- }
1863
- getNativeResponse() {
1864
- throw new Error("This is a mock. No running server, no native response.");
1865
- }
1866
- applyStatusCode() {
1867
- }
1868
- applyHeaders() {
1869
- }
1870
- applyCookies() {
1871
- for (const cookie of this.cookies) {
1872
- this.headers.add("Set-Cookie", super.setCookieValue(cookie));
1873
- }
1874
- }
1875
- bodyInternal(body) {
1876
- const it = async function* () {
1877
- const resolved = await body;
1878
- if (resolved === void 0) {
1879
- return;
1880
- }
1881
- yield resolved;
1882
- }();
1883
- return this.#writeHandler(stream.ReadableStream.from(it));
1884
- }
1885
- async end() {
1886
- return this.doCommit(async () => {
1887
- return await new Promise((resolve, reject) => {
1888
- this.#writeHandler(stream.ReadableStream.from([]));
1889
- });
1890
- });
1891
- }
1892
- getBody() {
1893
- return this.#body();
1894
- }
1895
- };
1896
-
1897
1725
  // src/app/cors.ts
1898
- function mockUpgradeExchange(path) {
1899
- const request = new MockServerHttpRequest(path, "GET");
1900
- request.setHeader("Upgrade", "websocket");
1901
- request.upgrade = true;
1902
- return new DefaultWebExchange(request, new MockServerHttpResponse());
1903
- }
1904
- async function createCorsConfigSource(context) {
1726
+ import { IOGateway as IOGateway6 } from "@interopio/gateway";
1727
+ function createCorsConfigSource(context) {
1905
1728
  const { sockets: routes, cors } = context;
1906
- const defaultCorsConfig = combineCorsConfig(PERMIT_DEFAULT_CONFIG, context.corsConfig);
1729
+ const defaultCorsConfig = context.corsConfig === false ? void 0 : combineCorsConfig(PERMIT_DEFAULT_CONFIG, context.corsConfig);
1907
1730
  const validatedConfigs = [];
1908
1731
  for (const [path, route] of routes) {
1909
1732
  let routeCorsConfig = defaultCorsConfig;
1910
- const upgradeExchange = mockUpgradeExchange(path);
1911
- for (const [matcher, config] of cors) {
1912
- if ((await matcher(upgradeExchange)).match) {
1913
- routeCorsConfig = combineCorsConfig(routeCorsConfig, config);
1733
+ for (const [matcher, config2] of cors) {
1734
+ if (IOGateway6.Filtering.valueMatches(matcher, path)) {
1735
+ if (config2 === void 0) {
1736
+ routeCorsConfig = void 0;
1737
+ } else {
1738
+ routeCorsConfig = routeCorsConfig === void 0 ? config2 : combineCorsConfig(routeCorsConfig, config2);
1739
+ }
1914
1740
  }
1915
1741
  }
1916
- routeCorsConfig = combineCorsConfig(routeCorsConfig, {
1742
+ const config = context.corsConfig === false ? void 0 : {
1917
1743
  allowOrigins: route.originFilters?.allow,
1918
- allowMethods: ["GET", "CONNECT"],
1919
- allowHeaders: ["Upgrade", "Connection", "Origin", "Sec-Websocket-Key", "Sec-Websocket-Version", "Sec-Websocket-Protocol", "Sec-Websocket-Extensions"],
1744
+ allowMethods: ["GET", "CONNECT", "OPTIONS"],
1745
+ allowHeaders: [
1746
+ "Upgrade",
1747
+ "Connection",
1748
+ "Origin",
1749
+ "Sec-Websocket-Key",
1750
+ "Sec-Websocket-Version",
1751
+ "Sec-Websocket-Protocol",
1752
+ "Sec-Websocket-Extensions"
1753
+ ],
1920
1754
  exposeHeaders: ["Sec-Websocket-Accept", "Sec-Websocket-Protocol", "Sec-Websocket-Extensions"],
1921
- allowCredentials: route.authorize?.access !== "permitted"
1922
- });
1923
- validatedConfigs.push([and([upgradeMatcher, pattern(path)]), validateCorsConfig(routeCorsConfig)]);
1755
+ allowCredentials: route.authorize?.access !== "permitted" ? true : void 0
1756
+ };
1757
+ routeCorsConfig = routeCorsConfig === void 0 ? config : combineCorsConfig(routeCorsConfig, config);
1758
+ validatedConfigs.push([
1759
+ and([upgradeMatcher, pattern(path)]),
1760
+ validateCorsConfig(routeCorsConfig)
1761
+ ]);
1924
1762
  }
1763
+ const appConfigs = [];
1925
1764
  for (const [matcher, config] of cors) {
1926
- const routeCorsConfig = combineCorsConfig(defaultCorsConfig, config);
1927
- validatedConfigs.push([matcher, validateCorsConfig(routeCorsConfig)]);
1765
+ let [, routeCorsConfig] = appConfigs.find(([m]) => String(m) === String(matcher)) ?? [matcher, defaultCorsConfig];
1766
+ routeCorsConfig = routeCorsConfig === void 0 ? config : combineCorsConfig(routeCorsConfig, config);
1767
+ let added = false;
1768
+ for (const entry of appConfigs) {
1769
+ if (String(entry[0]) === String(matcher)) {
1770
+ entry[1] = routeCorsConfig;
1771
+ added = true;
1772
+ break;
1773
+ }
1774
+ }
1775
+ if (!added) {
1776
+ appConfigs.push([matcher, routeCorsConfig]);
1777
+ }
1778
+ }
1779
+ for (const [matcher, config] of appConfigs) {
1780
+ validatedConfigs.push([pattern(matcher), validateCorsConfig(config)]);
1928
1781
  }
1929
1782
  validatedConfigs.push([pattern(/\/api\/.*/), validateCorsConfig(defaultCorsConfig)]);
1930
1783
  return matchingCorsConfigSource({ mappings: validatedConfigs });
@@ -2895,6 +2748,9 @@ function createSecurityConfig(context) {
2895
2748
  authorize.push(["any-exchange", defaultAccess]);
2896
2749
  return {
2897
2750
  authorize,
2751
+ cors: {
2752
+ disabled: context.corsConfig === false
2753
+ },
2898
2754
  basic: {
2899
2755
  disabled: context.authConfig?.type !== "basic",
2900
2756
  ...context.authConfig?.basic
@@ -2906,7 +2762,7 @@ function createSecurityConfig(context) {
2906
2762
  };
2907
2763
  }
2908
2764
  async function httpSecurity(context) {
2909
- const corsConfigSource = await createCorsConfigSource(context);
2765
+ const corsConfigSource = createCorsConfigSource(context);
2910
2766
  const config = createSecurityConfig(context);
2911
2767
  const { storage } = context;
2912
2768
  return config_default(config, { storage, corsConfigSource });
@@ -3323,7 +3179,7 @@ var Factory = async (options) => {
3323
3179
  storage: new AsyncLocalStorage4(),
3324
3180
  sockets: /* @__PURE__ */ new Map()
3325
3181
  };
3326
- const gw = IOGateway6.Factory({ ...options.gateway });
3182
+ const gw = IOGateway7.Factory({ ...options.gateway });
3327
3183
  if (options.gateway) {
3328
3184
  const config = options.gateway;
3329
3185
  await configure(async (configurer) => {