@stryke/http 0.2.0 → 0.2.2

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/cookie.cjs CHANGED
@@ -7,7 +7,7 @@ exports.parseCookie = parseCookie;
7
7
  exports.parseSetCookie = parseSetCookie;
8
8
  exports.serializeCookie = serializeCookie;
9
9
  exports.splitSetCookieString = splitSetCookieString;
10
- var _typeChecks = require("@stryke/types/type-checks");
10
+ var _typeChecks = require("@stryke/type-checks");
11
11
  function parseCookie(r, c) {
12
12
  if (!(0, _typeChecks.isString)(r)) throw new TypeError("argument str must be a string");
13
13
  const i = {},
@@ -90,7 +90,7 @@ function parseSetCookie(r, c) {
90
90
  }
91
91
  return n;
92
92
  }
93
- const u = /^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;
93
+ const u = /^[\t\u0020-\u007E\u0080-\u00FF]+$/;
94
94
  function serializeCookie(r, c, i) {
95
95
  const e = i ?? {},
96
96
  o = e.encode ?? encodeURIComponent;
@@ -98,23 +98,23 @@ function serializeCookie(r, c, i) {
98
98
  if (!u.test(r)) throw new TypeError("argument name is invalid");
99
99
  const a = o(c);
100
100
  if (a && !u.test(a)) throw new TypeError("argument val is invalid");
101
- let t = r + "=" + a;
101
+ let t = `${r}=${a}`;
102
102
  if (!(0, _typeChecks.isSet)(e.maxAge)) {
103
103
  const n = Number(e.maxAge);
104
104
  if (Number.isNaN(n) || !Number.isFinite(n)) throw new TypeError("option maxAge is invalid");
105
- t += "; Max-Age=" + Math.floor(n);
105
+ t += `; Max-Age=${Math.floor(n)}`;
106
106
  }
107
107
  if (e.domain) {
108
108
  if (!u.test(e.domain)) throw new TypeError("option domain is invalid");
109
- t += "; Domain=" + e.domain;
109
+ t += `; Domain=${e.domain}`;
110
110
  }
111
111
  if (e.path) {
112
112
  if (!u.test(e.path)) throw new TypeError("option path is invalid");
113
- t += "; Path=" + e.path;
113
+ t += `; Path=${e.path}`;
114
114
  }
115
115
  if (e.expires) {
116
116
  if (!(0, _typeChecks.isDate)(e.expires) || Number.isNaN(e.expires.valueOf())) throw new TypeError("option expires is invalid");
117
- t += "; Expires=" + e.expires.toUTCString();
117
+ t += `; Expires=${e.expires.toUTCString()}`;
118
118
  }
119
119
  if (e.httpOnly && (t += "; HttpOnly"), e.secure && (t += "; Secure"), e.priority) switch ((0, _typeChecks.isString)(e.priority) ? e.priority.toLowerCase() : e.priority) {
120
120
  case "low":
package/dist/cookie.mjs CHANGED
@@ -1 +1 @@
1
- import{isDate as h,isFunction as d,isSet as g,isString as p}from"@stryke/types/type-checks";export function parseCookie(r,c){if(!p(r))throw new TypeError("argument str must be a string");const i={},e=c??{},o=e.decode??(t=>t.includes("%")?decodeURIComponent(t):t);let a=0;for(;a<r.length;){const t=r.indexOf("=",a);if(t===-1)break;let n=r.indexOf(";",a);if(n===-1)n=r.length;else if(n<t){a=r.lastIndexOf(";",t-1)+1;continue}const s=r.slice(a,t).trim();if(e?.filter&&!e?.filter(s)){a=n+1;continue}if(i[s]===void 0){let l=r.slice(t+1,n).trim();l.codePointAt(0)===34&&(l=l.slice(1,-1));try{i[s]=o(l)}catch{i[s]=l}}a=n+1}return i}export function parseSetCookie(r,c){const i=(r||"").split(";").filter(s=>p(s)&&!!s.trim()),e=i.shift()||"";let o="",a="";const t=e.split("=");t.length>1?(o=t.shift(),a=t.join("=")):a=e;try{a=c?.decode===!1?a:(c?.decode??decodeURIComponent)(a)}catch{}const n={name:o,value:a};for(const s of i){const l=s.split("="),f=(l.shift()||"").trimStart().toLowerCase(),m=l.join("=");switch(f){case"expires":{n.expires=new Date(m);break}case"max-age":{n.maxAge=Number.parseInt(m,10);break}case"secure":{n.secure=!0;break}case"httponly":{n.httpOnly=!0;break}case"samesite":{n.sameSite=m;break}default:n[f]=m}}return n}const u=/^[\u0009\u0020-\u007E\u0080-\u00FF]+$/;export function serializeCookie(r,c,i){const e=i??{},o=e.encode??encodeURIComponent;if(!d(o))throw new TypeError("option encode is invalid");if(!u.test(r))throw new TypeError("argument name is invalid");const a=o(c);if(a&&!u.test(a))throw new TypeError("argument val is invalid");let t=r+"="+a;if(!g(e.maxAge)){const n=Number(e.maxAge);if(Number.isNaN(n)||!Number.isFinite(n))throw new TypeError("option maxAge is invalid");t+="; Max-Age="+Math.floor(n)}if(e.domain){if(!u.test(e.domain))throw new TypeError("option domain is invalid");t+="; Domain="+e.domain}if(e.path){if(!u.test(e.path))throw new TypeError("option path is invalid");t+="; Path="+e.path}if(e.expires){if(!h(e.expires)||Number.isNaN(e.expires.valueOf()))throw new TypeError("option expires is invalid");t+="; Expires="+e.expires.toUTCString()}if(e.httpOnly&&(t+="; HttpOnly"),e.secure&&(t+="; Secure"),e.priority)switch(p(e.priority)?e.priority.toLowerCase():e.priority){case"low":{t+="; Priority=Low";break}case"medium":{t+="; Priority=Medium";break}case"high":{t+="; Priority=High";break}default:throw new TypeError("option priority is invalid")}if(e.sameSite)switch(p(e.sameSite)?e.sameSite.toLowerCase():e.sameSite){case!0:{t+="; SameSite=Strict";break}case"lax":{t+="; SameSite=Lax";break}case"strict":{t+="; SameSite=Strict";break}case"none":{t+="; SameSite=None";break}default:throw new TypeError("option sameSite is invalid")}return e.partitioned&&(t+="; Partitioned"),t}export function splitSetCookieString(r){if(Array.isArray(r))return r.flatMap(f=>splitSetCookieString(f));if(!p(r))return[];const c=[];let i=0,e,o,a,t,n;const s=()=>{for(;i<r.length&&/\s/.test(r.charAt(i));)i+=1;return i<r.length},l=()=>(o=r.charAt(i),o!=="="&&o!==";"&&o!==",");for(;i<r.length;){for(e=i,n=!1;s();)if(o=r.charAt(i),o===","){for(a=i,i+=1,s(),t=i;i<r.length&&l();)i+=1;i<r.length&&r.charAt(i)==="="?(n=!0,i=t,c.push(r.slice(e,a)),e=i):i=a+1}else i+=1;(!n||i>=r.length)&&c.push(r.slice(e))}return c}
1
+ import{isDate as h,isFunction as d,isSet as g,isString as p}from"@stryke/type-checks";export function parseCookie(r,c){if(!p(r))throw new TypeError("argument str must be a string");const i={},e=c??{},o=e.decode??(t=>t.includes("%")?decodeURIComponent(t):t);let a=0;for(;a<r.length;){const t=r.indexOf("=",a);if(t===-1)break;let n=r.indexOf(";",a);if(n===-1)n=r.length;else if(n<t){a=r.lastIndexOf(";",t-1)+1;continue}const s=r.slice(a,t).trim();if(e?.filter&&!e?.filter(s)){a=n+1;continue}if(i[s]===void 0){let l=r.slice(t+1,n).trim();l.codePointAt(0)===34&&(l=l.slice(1,-1));try{i[s]=o(l)}catch{i[s]=l}}a=n+1}return i}export function parseSetCookie(r,c){const i=(r||"").split(";").filter(s=>p(s)&&!!s.trim()),e=i.shift()||"";let o="",a="";const t=e.split("=");t.length>1?(o=t.shift(),a=t.join("=")):a=e;try{a=c?.decode===!1?a:(c?.decode??decodeURIComponent)(a)}catch{}const n={name:o,value:a};for(const s of i){const l=s.split("="),f=(l.shift()||"").trimStart().toLowerCase(),m=l.join("=");switch(f){case"expires":{n.expires=new Date(m);break}case"max-age":{n.maxAge=Number.parseInt(m,10);break}case"secure":{n.secure=!0;break}case"httponly":{n.httpOnly=!0;break}case"samesite":{n.sameSite=m;break}default:n[f]=m}}return n}const u=/^[\t\u0020-\u007E\u0080-\u00FF]+$/;export function serializeCookie(r,c,i){const e=i??{},o=e.encode??encodeURIComponent;if(!d(o))throw new TypeError("option encode is invalid");if(!u.test(r))throw new TypeError("argument name is invalid");const a=o(c);if(a&&!u.test(a))throw new TypeError("argument val is invalid");let t=`${r}=${a}`;if(!g(e.maxAge)){const n=Number(e.maxAge);if(Number.isNaN(n)||!Number.isFinite(n))throw new TypeError("option maxAge is invalid");t+=`; Max-Age=${Math.floor(n)}`}if(e.domain){if(!u.test(e.domain))throw new TypeError("option domain is invalid");t+=`; Domain=${e.domain}`}if(e.path){if(!u.test(e.path))throw new TypeError("option path is invalid");t+=`; Path=${e.path}`}if(e.expires){if(!h(e.expires)||Number.isNaN(e.expires.valueOf()))throw new TypeError("option expires is invalid");t+=`; Expires=${e.expires.toUTCString()}`}if(e.httpOnly&&(t+="; HttpOnly"),e.secure&&(t+="; Secure"),e.priority)switch(p(e.priority)?e.priority.toLowerCase():e.priority){case"low":{t+="; Priority=Low";break}case"medium":{t+="; Priority=Medium";break}case"high":{t+="; Priority=High";break}default:throw new TypeError("option priority is invalid")}if(e.sameSite)switch(p(e.sameSite)?e.sameSite.toLowerCase():e.sameSite){case!0:{t+="; SameSite=Strict";break}case"lax":{t+="; SameSite=Lax";break}case"strict":{t+="; SameSite=Strict";break}case"none":{t+="; SameSite=None";break}default:throw new TypeError("option sameSite is invalid")}return e.partitioned&&(t+="; Partitioned"),t}export function splitSetCookieString(r){if(Array.isArray(r))return r.flatMap(f=>splitSetCookieString(f));if(!p(r))return[];const c=[];let i=0,e,o,a,t,n;const s=()=>{for(;i<r.length&&/\s/.test(r.charAt(i));)i+=1;return i<r.length},l=()=>(o=r.charAt(i),o!=="="&&o!==";"&&o!==",");for(;i<r.length;){for(e=i,n=!1;s();)if(o=r.charAt(i),o===","){for(a=i,i+=1,s(),t=i;i<r.length&&l();)i+=1;i<r.length&&r.charAt(i)==="="?(n=!0,i=t,c.push(r.slice(e,a)),e=i):i=a+1}else i+=1;(!n||i>=r.length)&&c.push(r.slice(e))}return c}
@@ -5,92 +5,92 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.StormURLBuilder = void 0;
7
7
  var _stormJson = require("@stryke/json/storm-json");
8
- var _isString = require("@stryke/types/type-checks/is-string");
8
+ var _isString = require("@stryke/type-checks/is-string");
9
9
  var _ufo = require("ufo");
10
10
  class StormURLBuilder {
11
- #t;
12
- static create(t, r) {
13
- return new StormURLBuilder(t, r);
14
- }
15
- constructor(t, r) {
16
- const s = r?.decode ?? !0,
17
- o = (0, _isString.isString)(t) ? s ? (0, _ufo.parseURL)((0, _ufo.decode)(t)) : (0, _ufo.parseURL)(t) : t;
18
- this.#t = {
11
+ #r;
12
+ static create(r, t) {
13
+ return new StormURLBuilder(r, t);
14
+ }
15
+ constructor(r, t) {
16
+ const s = t?.decode ?? !0,
17
+ n = (0, _isString.isString)(r) ? s ? (0, _ufo.parseURL)((0, _ufo.decode)(r)) : (0, _ufo.parseURL)(r) : r;
18
+ this.#r = {
19
19
  __typename: "StormURL",
20
20
  query: {},
21
- ...o
22
- }, this.#t.host && this.withHost(this.#t.host), this.#t.auth && this.withAuth(this.#t.auth);
21
+ ...n
22
+ }, this.#r.host && this.withHost(this.#r.host), this.#r.auth && this.withAuth(this.#r.auth);
23
23
  }
24
24
  get _url() {
25
- return this.#t;
25
+ return this.#r;
26
26
  }
27
- withProtocol(t) {
28
- return this.#t.protocol = t, this;
27
+ withProtocol(r) {
28
+ return this.#r.protocol = r, this;
29
29
  }
30
- withHostname(t) {
31
- return this.#t.hostname = t, this;
30
+ withHostname(r) {
31
+ return this.#r.hostname = r, this;
32
32
  }
33
- withPort(t) {
34
- return this.#t.port = String(t), this;
33
+ withPort(r) {
34
+ return this.#r.port = String(r), this;
35
35
  }
36
- withUsername(t) {
37
- return this.#t.username = t, this;
36
+ withUsername(r) {
37
+ return this.#r.username = r, this;
38
38
  }
39
- withPassword(t) {
40
- return this.#t.password = t, this;
39
+ withPassword(r) {
40
+ return this.#r.password = r, this;
41
41
  }
42
- withHost(t) {
43
- if ((0, _isString.isString)(t)) {
44
- this.#t.host = t;
45
- const r = (0, _ufo.parseAuth)(t);
46
- this.#t.username = r.username, this.#t.password = r.password;
47
- } else this.#t.hostname = t.hostname, this.#t.port = t.port, this.#t.auth = `${t.hostname}${t.port ? `:${t.port}` : ""}`;
42
+ withHost(r) {
43
+ if ((0, _isString.isString)(r)) {
44
+ this.#r.host = r;
45
+ const t = (0, _ufo.parseAuth)(r);
46
+ this.#r.username = t.username, this.#r.password = t.password;
47
+ } else this.#r.hostname = r.hostname, this.#r.port = r.port, this.#r.auth = `${r.hostname}${r.port ? `:${r.port}` : ""}`;
48
48
  return this;
49
49
  }
50
- withPath(t) {
51
- const r = (0, _ufo.parsePath)(t);
52
- return this.#t = {
53
- ...this.#t,
54
- ...r
50
+ withPath(r) {
51
+ const t = (0, _ufo.parsePath)(r);
52
+ return this.#r = {
53
+ ...this.#r,
54
+ ...t
55
55
  }, this;
56
56
  }
57
- withHash(t) {
58
- return this.#t.hash = t, this;
57
+ withHash(r) {
58
+ return this.#r.hash = r, this;
59
59
  }
60
- withAuth(t) {
61
- if ((0, _isString.isString)(t)) {
62
- this.#t.auth = t;
63
- const r = (0, _ufo.parseAuth)(t);
64
- this.#t.username = r.username, this.#t.password = r.password;
65
- } else this.#t.username = t.username, this.#t.password = t.password, this.#t.auth = `${t.username}:${t.password}`;
60
+ withAuth(r) {
61
+ if ((0, _isString.isString)(r)) {
62
+ this.#r.auth = r;
63
+ const t = (0, _ufo.parseAuth)(r);
64
+ this.#r.username = t.username, this.#r.password = t.password;
65
+ } else this.#r.username = r.username, this.#r.password = r.password, this.#r.auth = `${r.username}:${r.password}`;
66
66
  return this;
67
67
  }
68
- withQuery(t) {
69
- return this.#t.query = {}, this.addQueryParam(t), this;
68
+ withQuery(r) {
69
+ return this.#r.query = {}, this.addQueryParam(r), this;
70
70
  }
71
- addQueryParam(t) {
72
- if ((0, _isString.isString)(t)) {
73
- const r = (0, _ufo.parseQuery)(t);
74
- for (const s in Object.entries(r)) s[0] && (this.#t.query[s[0]] = this.parseQueryParamValue(s[1]));
75
- } else if (Array.isArray(t) && t.length === 2) this.#t.query[t[0]] = this.parseQueryParamValue(t[1]);else for (const r in Object.entries(t)) r[0] && (this.#t.query[r[0]] = this.parseQueryParamValue(r[1]));
71
+ addQueryParam(r) {
72
+ if ((0, _isString.isString)(r)) {
73
+ const t = (0, _ufo.parseQuery)(r);
74
+ for (const s in Object.entries(t)) s[0] && (this.#r.query[s[0]] = this.parseQueryParamValue(s[1]));
75
+ } else if (Array.isArray(r) && r.length === 2) this.#r.query[r[0]] = this.parseQueryParamValue(r[1]);else for (const t in Object.entries(r)) t[0] && (this.#r.query[t[0]] = this.parseQueryParamValue(t[1]));
76
76
  return this;
77
77
  }
78
78
  build() {
79
- return this.#t;
79
+ return this.#r;
80
80
  }
81
81
  toString() {
82
- return (0, _ufo.cleanDoubleSlashes)((0, _ufo.stringifyParsedURL)(this.#t));
82
+ return (0, _ufo.cleanDoubleSlashes)((0, _ufo.stringifyParsedURL)(this.#r));
83
83
  }
84
84
  toEncoded() {
85
85
  return (0, _ufo.encode)(this.toString());
86
86
  }
87
- parseQueryParamValue(t) {
88
- if (Array.isArray(t)) {
89
- const r = [];
90
- for (const s in t) r.push(this.parseQueryParamValue(s));
91
- return r;
87
+ parseQueryParamValue(r) {
88
+ if (Array.isArray(r)) {
89
+ const t = [];
90
+ for (const s in r) t.push(this.parseQueryParamValue(s));
91
+ return t;
92
92
  }
93
- return _stormJson.StormJSON.parse(t);
93
+ return _stormJson.StormJSON.parse(r);
94
94
  }
95
95
  }
96
96
  exports.StormURLBuilder = StormURLBuilder;
@@ -1,13 +1,13 @@
1
- import { type ParsedAuth, type ParsedHost } from "ufo";
1
+ import type { ParsedAuth, ParsedHost } from "ufo";
2
2
  import type { StormURL } from "./types";
3
- export type StormURLBuilderOptions = {
3
+ export interface StormURLBuilderOptions {
4
4
  /**
5
5
  * Should the URL be decoded
6
6
  *
7
7
  * @defaultValue `true`
8
8
  */
9
9
  decode: boolean;
10
- };
10
+ }
11
11
  /**
12
12
  * A class used to build URLs
13
13
  *
@@ -1 +1 @@
1
- import{StormJSON as a}from"@stryke/json/storm-json";import{isString as i}from"@stryke/types/type-checks/is-string";import{cleanDoubleSlashes as u,decode as h,encode as p,parseAuth as e,parsePath as d,parseQuery as m,parseURL as n,stringifyParsedURL as c}from"ufo";export class StormURLBuilder{#t;static create(t,r){return new StormURLBuilder(t,r)}constructor(t,r){const s=r?.decode??!0,o=i(t)?s?n(h(t)):n(t):t;this.#t={__typename:"StormURL",query:{},...o},this.#t.host&&this.withHost(this.#t.host),this.#t.auth&&this.withAuth(this.#t.auth)}get _url(){return this.#t}withProtocol(t){return this.#t.protocol=t,this}withHostname(t){return this.#t.hostname=t,this}withPort(t){return this.#t.port=String(t),this}withUsername(t){return this.#t.username=t,this}withPassword(t){return this.#t.password=t,this}withHost(t){if(i(t)){this.#t.host=t;const r=e(t);this.#t.username=r.username,this.#t.password=r.password}else this.#t.hostname=t.hostname,this.#t.port=t.port,this.#t.auth=`${t.hostname}${t.port?`:${t.port}`:""}`;return this}withPath(t){const r=d(t);return this.#t={...this.#t,...r},this}withHash(t){return this.#t.hash=t,this}withAuth(t){if(i(t)){this.#t.auth=t;const r=e(t);this.#t.username=r.username,this.#t.password=r.password}else this.#t.username=t.username,this.#t.password=t.password,this.#t.auth=`${t.username}:${t.password}`;return this}withQuery(t){return this.#t.query={},this.addQueryParam(t),this}addQueryParam(t){if(i(t)){const r=m(t);for(const s in Object.entries(r))s[0]&&(this.#t.query[s[0]]=this.parseQueryParamValue(s[1]))}else if(Array.isArray(t)&&t.length===2)this.#t.query[t[0]]=this.parseQueryParamValue(t[1]);else for(const r in Object.entries(t))r[0]&&(this.#t.query[r[0]]=this.parseQueryParamValue(r[1]));return this}build(){return this.#t}toString(){return u(c(this.#t))}toEncoded(){return p(this.toString())}parseQueryParamValue(t){if(Array.isArray(t)){const r=[];for(const s in t)r.push(this.parseQueryParamValue(s));return r}return a.parse(t)}}
1
+ import{StormJSON as a}from"@stryke/json/storm-json";import{isString as i}from"@stryke/type-checks/is-string";import{cleanDoubleSlashes as u,decode as h,encode as p,parseAuth as e,parsePath as d,parseQuery as m,parseURL as o,stringifyParsedURL as c}from"ufo";export class StormURLBuilder{#r;static create(r,t){return new StormURLBuilder(r,t)}constructor(r,t){const s=t?.decode??!0,n=i(r)?s?o(h(r)):o(r):r;this.#r={__typename:"StormURL",query:{},...n},this.#r.host&&this.withHost(this.#r.host),this.#r.auth&&this.withAuth(this.#r.auth)}get _url(){return this.#r}withProtocol(r){return this.#r.protocol=r,this}withHostname(r){return this.#r.hostname=r,this}withPort(r){return this.#r.port=String(r),this}withUsername(r){return this.#r.username=r,this}withPassword(r){return this.#r.password=r,this}withHost(r){if(i(r)){this.#r.host=r;const t=e(r);this.#r.username=t.username,this.#r.password=t.password}else this.#r.hostname=r.hostname,this.#r.port=r.port,this.#r.auth=`${r.hostname}${r.port?`:${r.port}`:""}`;return this}withPath(r){const t=d(r);return this.#r={...this.#r,...t},this}withHash(r){return this.#r.hash=r,this}withAuth(r){if(i(r)){this.#r.auth=r;const t=e(r);this.#r.username=t.username,this.#r.password=t.password}else this.#r.username=r.username,this.#r.password=r.password,this.#r.auth=`${r.username}:${r.password}`;return this}withQuery(r){return this.#r.query={},this.addQueryParam(r),this}addQueryParam(r){if(i(r)){const t=m(r);for(const s in Object.entries(t))s[0]&&(this.#r.query[s[0]]=this.parseQueryParamValue(s[1]))}else if(Array.isArray(r)&&r.length===2)this.#r.query[r[0]]=this.parseQueryParamValue(r[1]);else for(const t in Object.entries(r))t[0]&&(this.#r.query[t[0]]=this.parseQueryParamValue(t[1]));return this}build(){return this.#r}toString(){return u(c(this.#r))}toEncoded(){return p(this.toString())}parseQueryParamValue(r){if(Array.isArray(r)){const t=[];for(const s in r)t.push(this.parseQueryParamValue(s));return t}return a.parse(r)}}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/http",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "A package containing HTTP communication utilities used by Storm Software.",
6
6
  "repository": {
@@ -11,8 +11,8 @@
11
11
  "private": false,
12
12
  "dependencies": {
13
13
  "ufo": "1.5.4",
14
- "@stryke/json": ">=0.2.0",
15
- "@stryke/types": ">=0.4.0"
14
+ "@stryke/json": ">=0.4.3",
15
+ "@stryke/type-checks": ">=0.0.1"
16
16
  },
17
17
  "devDependencies": { "@types/node": "^22.13.1" },
18
18
  "publishConfig": { "access": "public" },
@@ -28,7 +28,6 @@
28
28
  "stryke",
29
29
  "typescript",
30
30
  "utilities",
31
- "storm-stack",
32
31
  "storm-software",
33
32
  "storm",
34
33
  "storm-ops",