foxts 4.5.0 → 4.6.0

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.
@@ -0,0 +1,29 @@
1
+ interface SimpleCorsOptions {
2
+ origin: string | string[] | ((origin: string) => Promise<string | undefined | null> | string | undefined | null);
3
+ allowMethods?: string[] | ((origin: string) => Promise<string[]> | string[]);
4
+ allowHeaders?: string[];
5
+ maxAge?: number;
6
+ credentials?: boolean;
7
+ exposeHeaders?: string[];
8
+ }
9
+ /**
10
+ * A very simple CORS implementation for using in simple serverless workers
11
+ *
12
+ * Example usage:
13
+ *
14
+ * ```ts
15
+ * const simpleCors = createSimpleCors({});
16
+ *
17
+ * export function fetch(req: Request) {
18
+ * if (req.method === 'OPTIONS') {
19
+ * return simpleCors(req, new Response(null, { status: 204 });
20
+ * }
21
+ * const resp = Response.json({ message: 'Hello, world!' });
22
+ * return simpleCors(req, resp);
23
+ * }
24
+ * ```
25
+ */
26
+ declare function createSimpleCors(options?: SimpleCorsOptions): (request: Request, response: Response) => Promise<Response>;
27
+
28
+ export { createSimpleCors };
29
+ export type { SimpleCorsOptions };
@@ -0,0 +1 @@
1
+ "use strict";var e=require("../fast-string-array-join/index.js");exports.createSimpleCors=function(s){let t,r;const n={origin:"*",allowMethods:["GET","HEAD","PUT","POST","DELETE","PATCH"],allowHeaders:[],exposeHeaders:[],...s},o=n.origin;if("string"==typeof o)t="*"===o?()=>"*":e=>o===e?e:null;else if("function"==typeof o)t=o;else{const e=new Set(o);t=s=>e.has(s)?s:null}const a=n.allowMethods;r="function"==typeof a?a:Array.isArray(a)?()=>a:()=>[];const l="*"!==o;return async function(s,o){let a=t(s.headers.get("Origin")||"");if(a&&"object"==typeof a&&"then"in a&&(a=await a),a&&o.headers.set("Access-Control-Allow-Origin",a),l){const e=s.headers.get("Vary");e?o.headers.set("Vary",e):o.headers.set("Vary","Origin")}n.credentials&&o.headers.set("Access-Control-Allow-Credentials","true"),n.exposeHeaders?.length&&o.headers.set("Access-Control-Expose-Headers",e.fastStringArrayJoin(n.exposeHeaders,","));let i=r(s.headers.get("origin")||"");if("then"in i&&(i=await i),i.length&&o.headers.set("Access-Control-Allow-Methods",e.fastStringArrayJoin(i,",")),"OPTIONS"===s.method){null!=n.maxAge&&o.headers.set("Access-Control-Max-Age",n.maxAge.toString());let t=n.allowHeaders;if(!t?.length){const e=s.headers.get("Access-Control-Request-Headers");e&&(t=e.split(/\s*,\s*/))}t?.length&&(o.headers.set("Access-Control-Allow-Headers",e.fastStringArrayJoin(t,",")),o.headers.append("Vary","Access-Control-Request-Headers"))}return o}};
@@ -0,0 +1 @@
1
+ import{fastStringArrayJoin as e}from"../fast-string-array-join/index.mjs";function s(s){let t,r;const o={origin:"*",allowMethods:["GET","HEAD","PUT","POST","DELETE","PATCH"],allowHeaders:[],exposeHeaders:[],...s},n=o.origin;if("string"==typeof n)t="*"===n?()=>"*":e=>n===e?e:null;else if("function"==typeof n)t=n;else{const e=new Set(n);t=s=>e.has(s)?s:null}const a=o.allowMethods;r="function"==typeof a?a:Array.isArray(a)?()=>a:()=>[];const l="*"!==n;return async function(s,n){let a=t(s.headers.get("Origin")||"");if(a&&"object"==typeof a&&"then"in a&&(a=await a),a&&n.headers.set("Access-Control-Allow-Origin",a),l){const e=s.headers.get("Vary");e?n.headers.set("Vary",e):n.headers.set("Vary","Origin")}o.credentials&&n.headers.set("Access-Control-Allow-Credentials","true"),o.exposeHeaders?.length&&n.headers.set("Access-Control-Expose-Headers",e(o.exposeHeaders,","));let i=r(s.headers.get("origin")||"");if("then"in i&&(i=await i),i.length&&n.headers.set("Access-Control-Allow-Methods",e(i,",")),"OPTIONS"===s.method){null!=o.maxAge&&n.headers.set("Access-Control-Max-Age",o.maxAge.toString());let t=o.allowHeaders;if(!t?.length){const e=s.headers.get("Access-Control-Request-Headers");e&&(t=e.split(/\s*,\s*/))}t?.length&&(n.headers.set("Access-Control-Allow-Headers",e(t,",")),n.headers.append("Vary","Access-Control-Request-Headers"))}return n}}export{s as createSimpleCors};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foxts",
3
- "version": "4.5.0",
3
+ "version": "4.6.0",
4
4
  "description": "Opinionated collection of common TypeScript utils by @SukkaW",
5
5
  "repository": {
6
6
  "url": "https://github.com/SukkaW/foxts"
@@ -268,6 +268,12 @@
268
268
  "require": "./dist/shuffle-array/index.js",
269
269
  "default": "./dist/shuffle-array/index.js"
270
270
  },
271
+ "./simple-cors": {
272
+ "types": "./dist/simple-cors/index.d.ts",
273
+ "import": "./dist/simple-cors/index.mjs",
274
+ "require": "./dist/simple-cors/index.js",
275
+ "default": "./dist/simple-cors/index.js"
276
+ },
271
277
  "./simple-string-hash": {
272
278
  "types": "./dist/simple-string-hash/index.d.ts",
273
279
  "import": "./dist/simple-string-hash/index.mjs",