@stryke/http 0.2.1 → 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 +7 -7
- package/dist/cookie.mjs +1 -1
- package/dist/url-builder.cjs +56 -56
- package/dist/url-builder.d.ts +3 -3
- package/dist/url-builder.mjs +1 -1
- package/package.json +3 -4
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/
|
|
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 = /^[\
|
|
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
|
|
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 +=
|
|
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 +=
|
|
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 +=
|
|
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 +=
|
|
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/
|
|
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}
|
package/dist/url-builder.cjs
CHANGED
|
@@ -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/
|
|
8
|
+
var _isString = require("@stryke/type-checks/is-string");
|
|
9
9
|
var _ufo = require("ufo");
|
|
10
10
|
class StormURLBuilder {
|
|
11
|
-
#
|
|
12
|
-
static create(
|
|
13
|
-
return new StormURLBuilder(
|
|
14
|
-
}
|
|
15
|
-
constructor(
|
|
16
|
-
const s =
|
|
17
|
-
|
|
18
|
-
this.#
|
|
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
|
-
...
|
|
22
|
-
}, this.#
|
|
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.#
|
|
25
|
+
return this.#r;
|
|
26
26
|
}
|
|
27
|
-
withProtocol(
|
|
28
|
-
return this.#
|
|
27
|
+
withProtocol(r) {
|
|
28
|
+
return this.#r.protocol = r, this;
|
|
29
29
|
}
|
|
30
|
-
withHostname(
|
|
31
|
-
return this.#
|
|
30
|
+
withHostname(r) {
|
|
31
|
+
return this.#r.hostname = r, this;
|
|
32
32
|
}
|
|
33
|
-
withPort(
|
|
34
|
-
return this.#
|
|
33
|
+
withPort(r) {
|
|
34
|
+
return this.#r.port = String(r), this;
|
|
35
35
|
}
|
|
36
|
-
withUsername(
|
|
37
|
-
return this.#
|
|
36
|
+
withUsername(r) {
|
|
37
|
+
return this.#r.username = r, this;
|
|
38
38
|
}
|
|
39
|
-
withPassword(
|
|
40
|
-
return this.#
|
|
39
|
+
withPassword(r) {
|
|
40
|
+
return this.#r.password = r, this;
|
|
41
41
|
}
|
|
42
|
-
withHost(
|
|
43
|
-
if ((0, _isString.isString)(
|
|
44
|
-
this.#
|
|
45
|
-
const
|
|
46
|
-
this.#
|
|
47
|
-
} else this.#
|
|
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(
|
|
51
|
-
const
|
|
52
|
-
return this.#
|
|
53
|
-
...this.#
|
|
54
|
-
...
|
|
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(
|
|
58
|
-
return this.#
|
|
57
|
+
withHash(r) {
|
|
58
|
+
return this.#r.hash = r, this;
|
|
59
59
|
}
|
|
60
|
-
withAuth(
|
|
61
|
-
if ((0, _isString.isString)(
|
|
62
|
-
this.#
|
|
63
|
-
const
|
|
64
|
-
this.#
|
|
65
|
-
} else this.#
|
|
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(
|
|
69
|
-
return this.#
|
|
68
|
+
withQuery(r) {
|
|
69
|
+
return this.#r.query = {}, this.addQueryParam(r), this;
|
|
70
70
|
}
|
|
71
|
-
addQueryParam(
|
|
72
|
-
if ((0, _isString.isString)(
|
|
73
|
-
const
|
|
74
|
-
for (const s in Object.entries(
|
|
75
|
-
} else if (Array.isArray(
|
|
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.#
|
|
79
|
+
return this.#r;
|
|
80
80
|
}
|
|
81
81
|
toString() {
|
|
82
|
-
return (0, _ufo.cleanDoubleSlashes)((0, _ufo.stringifyParsedURL)(this.#
|
|
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(
|
|
88
|
-
if (Array.isArray(
|
|
89
|
-
const
|
|
90
|
-
for (const s in
|
|
91
|
-
return
|
|
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(
|
|
93
|
+
return _stormJson.StormJSON.parse(r);
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
exports.StormURLBuilder = StormURLBuilder;
|
package/dist/url-builder.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ParsedAuth, ParsedHost } from "ufo";
|
|
2
2
|
import type { StormURL } from "./types";
|
|
3
|
-
export
|
|
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
|
*
|
package/dist/url-builder.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{StormJSON as a}from"@stryke/json/storm-json";import{isString as i}from"@stryke/
|
|
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.
|
|
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.4.
|
|
15
|
-
"@stryke/
|
|
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",
|