@h-ai/serv 0.1.0-alpha.15 → 0.1.0-alpha.18

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
@@ -651,6 +651,8 @@ function createApp(options) {
651
651
  return cachedSpec;
652
652
  };
653
653
  app.use("*", securityHeaders());
654
+ if (options.middlewares)
655
+ mountCustomMiddlewares(app, options.middlewares);
654
656
  if (options.transport) {
655
657
  const mgr = options.transport.crypto.transport.createServer({ maxClients: options.transport.maxClients });
656
658
  if (!mgr.success) {
@@ -663,8 +665,6 @@ function createApp(options) {
663
665
  const keyExchangePath = `${http.apiPrefix}${options.transport.keyExchangePath ?? "/_hai/key-exchange"}`;
664
666
  app.use("*", createTransportMiddleware(mgr.data, keyExchangePath, options.transport.excludePaths));
665
667
  }
666
- if (options.middlewares)
667
- mountCustomMiddlewares(app, options.middlewares);
668
668
  if (http.health !== false)
669
669
  mountHealthRoutes(app, http.health);
670
670
  if (options.refreshCookie)
@@ -776,6 +776,90 @@ function isOrpcValidationErrorPayload(value) {
776
776
  return Array.isArray(issues);
777
777
  }
778
778
 
779
+ // src/serv-cors.ts
780
+ var DEFAULT_CORS_METHODS = ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE", "OPTIONS"];
781
+ var DEFAULT_CORS_ALLOWED_HEADERS = ["Authorization", "Content-Type", "X-Requested-With"];
782
+ var DEFAULT_CORS_MAX_AGE = 86400;
783
+ function cors(config = {}) {
784
+ const methods = [...config.methods ?? DEFAULT_CORS_METHODS];
785
+ const allowedHeaders = [...config.allowedHeaders ?? DEFAULT_CORS_ALLOWED_HEADERS];
786
+ const exposedHeaders = [...config.exposedHeaders ?? []];
787
+ const credentials = config.credentials ?? false;
788
+ const maxAge = config.maxAge ?? DEFAULT_CORS_MAX_AGE;
789
+ const originConfig = config.origin ?? "*";
790
+ return async (c, next) => {
791
+ const allowedOrigin = resolveCorsOrigin(c.req.header("Origin"), originConfig);
792
+ if (c.req.method === "OPTIONS") {
793
+ return new Response(null, {
794
+ status: 204,
795
+ headers: buildCorsHeaders({
796
+ allowedOrigin,
797
+ allowedHeaders,
798
+ credentials,
799
+ exposedHeaders,
800
+ maxAge,
801
+ methods,
802
+ request: c.req.raw
803
+ })
804
+ });
805
+ }
806
+ await next();
807
+ const corsHeaders = buildCorsHeaders({
808
+ allowedOrigin,
809
+ allowedHeaders,
810
+ credentials,
811
+ exposedHeaders,
812
+ maxAge,
813
+ methods,
814
+ request: c.req.raw
815
+ });
816
+ for (const [key, value] of corsHeaders) {
817
+ if (key.toLowerCase() === "vary") {
818
+ c.res.headers.append(key, value);
819
+ continue;
820
+ }
821
+ c.res.headers.set(key, value);
822
+ }
823
+ return void 0;
824
+ };
825
+ }
826
+ function resolveCorsOrigin(origin, configOrigin) {
827
+ if (!origin)
828
+ return null;
829
+ if (configOrigin === "*")
830
+ return "*";
831
+ if (typeof configOrigin === "string")
832
+ return origin === configOrigin ? origin : null;
833
+ if (Array.isArray(configOrigin))
834
+ return configOrigin.includes(origin) ? origin : null;
835
+ if (typeof configOrigin === "function")
836
+ return configOrigin(origin) ? origin : null;
837
+ return null;
838
+ }
839
+ function buildCorsHeaders(options) {
840
+ const headers = new Headers();
841
+ if (!options.allowedOrigin)
842
+ return headers;
843
+ const requestedHeaders = options.request.headers.get("Access-Control-Request-Headers");
844
+ headers.set("Access-Control-Allow-Origin", options.allowedOrigin);
845
+ if (options.credentials)
846
+ headers.set("Access-Control-Allow-Credentials", "true");
847
+ if (options.allowedOrigin !== "*") {
848
+ headers.set(
849
+ "Vary",
850
+ requestedHeaders ? "Origin, Access-Control-Request-Headers" : "Origin"
851
+ );
852
+ }
853
+ if (options.exposedHeaders.length > 0)
854
+ headers.set("Access-Control-Expose-Headers", options.exposedHeaders.join(", "));
855
+ if (options.request.method === "OPTIONS") {
856
+ headers.set("Access-Control-Allow-Methods", options.methods.join(", "));
857
+ headers.set("Access-Control-Allow-Headers", requestedHeaders ?? options.allowedHeaders.join(", "));
858
+ headers.set("Access-Control-Max-Age", String(options.maxAge));
859
+ }
860
+ return headers;
861
+ }
862
+
779
863
  // src/serv-main.ts
780
864
  var serv = {
781
865
  // 应用与上下文
@@ -786,6 +870,7 @@ var serv = {
786
870
  // 运行时适配器
787
871
  listen,
788
872
  toFetch,
873
+ cors,
789
874
  // OpenAPI / 导出
790
875
  generateSpec,
791
876
  // Procedure 装配(contract 路由定义统一使用 `apiContract.route`)。