foxts 4.4.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,11 @@
1
+ /**
2
+ * The only use case I have thought of so far would be @typescript-eslint/await-thenable banning
3
+ *
4
+ * `await Promise.all(Array<Promise | null | undefined>)`
5
+ *
6
+ * So instead of `.filter(not('nullish'))` which need to iterate through array, we use this when pushing
7
+ * things to array in the first place
8
+ */
9
+ declare function arrayPushNonNullish<T>(arr: T[], itemOrItems: T | null | undefined | Array<T | null | undefined>): void;
10
+
11
+ export { arrayPushNonNullish };
@@ -0,0 +1 @@
1
+ "use strict";exports.arrayPushNonNullish=function(s,l){if(Array.isArray(l))for(let r=0,u=l.length;r<u;r++){const u=l[r];null!=u&&s.push(u)}else null!=l&&s.push(l)};
@@ -0,0 +1 @@
1
+ function l(l,r){if(Array.isArray(r))for(let n=0,s=r.length;n<s;n++){const s=r[n];null!=s&&l.push(s)}else null!=r&&l.push(r)}export{l as arrayPushNonNullish};
@@ -1 +1 @@
1
- "use strict";function r(r){switch(r){case null:return r=>null!==r;case void 0:return r=>void 0!==r;case!1:return r=>!1!==r;case"nullish":return r=>null!=r;case"falsy":return r=>!!r;default:l(r,"argument")}}function n(r){switch(r){case null:return r=>null===r;case void 0:return r=>void 0===r;case!1:return r=>!1===r;case"nullish":return r=>null==r;case"falsy":return r=>!r;case"truthy":return r=>!!r;default:l(r,"argument")}}const e=n("truthy"),t=n("falsy"),u=r(null),s=r("nullish");function l(r,n="value"){throw TypeError(`Unexpected ${n}: ${r} as ${JSON.stringify(typeof r)}, should be "never"`)}exports.invariant=function(r,n='[foxts/invariant] "value" is null or undefined'){if(null==r)throw TypeError(n)},exports.is=n,exports.isFalsy=t,exports.isNonNull=u,exports.isNonNullish=s,exports.isTruthy=e,exports.never=l,exports.not=r,exports.nullthrow=function(r,n='[foxts/invariant] "value" is null or undefined'){if(null==r)throw TypeError(n);return r};
1
+ "use strict";const r=r=>null!==r,e=r=>void 0!==r,n=r=>!1!==r,t=r=>null!=r,u=r=>!!r,s=r=>null===r,l=r=>void 0===r,o=r=>!1===r,i=r=>null==r,a=r=>!r,c=r=>!!r;function f(r,e="value"){throw TypeError(`Unexpected ${e}: ${r} as ${JSON.stringify(typeof r)}, should be "never"`)}exports.invariant=function(r,e='[foxts/invariant] "value" is null or undefined'){if(null==r)throw TypeError(e)},exports.is=function(r){switch(r){case null:return s;case void 0:return l;case!1:return o;case"nullish":return i;case"falsy":return a;case"truthy":return c;default:f(r,"argument")}},exports.isFalsy=a,exports.isNonNull=r,exports.isNonNullish=t,exports.isTruthy=c,exports.never=f,exports.not=function(s){switch(s){case null:return r;case void 0:return e;case!1:return n;case"nullish":return t;case"falsy":return u;default:f(s,"argument")}},exports.nullthrow=function(r,e='[foxts/invariant] "value" is null or undefined'){if(null==r)throw TypeError(e);return r};
@@ -1 +1 @@
1
- function n(n){switch(n){case null:return n=>null!==n;case void 0:return n=>void 0!==n;case!1:return n=>!1!==n;case"nullish":return n=>null!=n;case"falsy":return n=>!!n;default:a(n,"argument")}}function r(n){switch(n){case null:return n=>null===n;case void 0:return n=>void 0===n;case!1:return n=>!1===n;case"nullish":return n=>null==n;case"falsy":return n=>!n;case"truthy":return n=>!!n;default:a(n,"argument")}}const e=r("truthy"),u=r("falsy"),l=n(null),t=n("nullish");function i(n,r='[foxts/invariant] "value" is null or undefined'){if(null==n)throw TypeError(r);return n}function s(n,r='[foxts/invariant] "value" is null or undefined'){if(null==n)throw TypeError(r)}function a(n,r="value"){throw TypeError(`Unexpected ${r}: ${n} as ${JSON.stringify(typeof n)}, should be "never"`)}export{s as invariant,r as is,u as isFalsy,l as isNonNull,t as isNonNullish,e as isTruthy,a as never,n as not,i as nullthrow};
1
+ const n=n=>null!==n,r=n=>void 0!==n,e=n=>!1!==n,u=n=>null!=n,t=n=>!!n;function l(l){switch(l){case null:return n;case void 0:return r;case!1:return e;case"nullish":return u;case"falsy":return t;default:x(l,"argument")}}const s=n=>null===n,i=n=>void 0===n,o=n=>!1===n,a=n=>null==n,c=n=>!n,f=n=>!!n;function d(n){switch(n){case null:return s;case void 0:return i;case!1:return o;case"nullish":return a;case"falsy":return c;case"truthy":return f;default:x(n,"argument")}}const h=f,v=c,y=n,p=u;function w(n,r='[foxts/invariant] "value" is null or undefined'){if(null==n)throw TypeError(r);return n}function N(n,r='[foxts/invariant] "value" is null or undefined'){if(null==n)throw TypeError(r)}function x(n,r="value"){throw TypeError(`Unexpected ${r}: ${n} as ${JSON.stringify(typeof n)}, should be "never"`)}export{N as invariant,d as is,v as isFalsy,y as isNonNull,p as isNonNullish,h as isTruthy,x as never,l as not,w as nullthrow};
@@ -0,0 +1,3 @@
1
+ declare function joinReadableStreams<T>(streams: Array<ReadableStream<T>>): ReadableStream<T>;
2
+
3
+ export { joinReadableStreams };
@@ -0,0 +1 @@
1
+ "use strict";exports.joinReadableStreams=function(e){return new ReadableStream({async start(r){try{for(const a of e){const e=a.getReader();try{for(;;){const a=await e.read();if(a.done)break;r.enqueue(a.value)}}finally{e.releaseLock()}}r.close()}catch(e){r.error(e)}}})};
@@ -0,0 +1 @@
1
+ function e(e){return new ReadableStream({async start(r){try{for(const a of e){const e=a.getReader();try{for(;;){const a=await e.read();if(a.done)break;r.enqueue(a.value)}}finally{e.releaseLock()}}r.close()}catch(e){r.error(e)}}})}export{e as joinReadableStreams};
@@ -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.4.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"
@@ -40,6 +40,12 @@
40
40
  "require": "./dist/append-set-elements-to-array/index.js",
41
41
  "default": "./dist/append-set-elements-to-array/index.js"
42
42
  },
43
+ "./array-push-non-nullish": {
44
+ "types": "./dist/array-push-non-nullish/index.d.ts",
45
+ "import": "./dist/array-push-non-nullish/index.mjs",
46
+ "require": "./dist/array-push-non-nullish/index.js",
47
+ "default": "./dist/array-push-non-nullish/index.js"
48
+ },
43
49
  "./async-retry": {
44
50
  "types": "./dist/async-retry/index.d.ts",
45
51
  "import": "./dist/async-retry/index.mjs",
@@ -196,6 +202,12 @@
196
202
  "require": "./dist/is-probably-ip/index.js",
197
203
  "default": "./dist/is-probably-ip/index.js"
198
204
  },
205
+ "./join-readablestreams": {
206
+ "types": "./dist/join-readablestreams/index.d.ts",
207
+ "import": "./dist/join-readablestreams/index.mjs",
208
+ "require": "./dist/join-readablestreams/index.js",
209
+ "default": "./dist/join-readablestreams/index.js"
210
+ },
199
211
  "./lazy-promise": {
200
212
  "types": "./dist/lazy-promise/index.d.ts",
201
213
  "import": "./dist/lazy-promise/index.mjs",
@@ -256,6 +268,12 @@
256
268
  "require": "./dist/shuffle-array/index.js",
257
269
  "default": "./dist/shuffle-array/index.js"
258
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
+ },
259
277
  "./simple-string-hash": {
260
278
  "types": "./dist/simple-string-hash/index.d.ts",
261
279
  "import": "./dist/simple-string-hash/index.mjs",