@qaick/http-executor 0.1.0-alpha.1 → 0.1.0-alpha.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/http-action-executor.d.ts +28 -0
- package/dist/http-action-executor.js +134 -1
- package/package.json +41 -10
|
@@ -13,6 +13,34 @@ export interface HttpTransport {
|
|
|
13
13
|
timeoutMs?: number;
|
|
14
14
|
}): Promise<HttpResponse>;
|
|
15
15
|
}
|
|
16
|
+
export interface HttpCookie {
|
|
17
|
+
name: string;
|
|
18
|
+
value: string;
|
|
19
|
+
domain: string;
|
|
20
|
+
path: string;
|
|
21
|
+
hostOnly: boolean;
|
|
22
|
+
secure: boolean;
|
|
23
|
+
expiresAt?: number;
|
|
24
|
+
}
|
|
25
|
+
/** Cookie state shared by HTTP Actions during one execution. */
|
|
26
|
+
export declare class HttpCookieJar {
|
|
27
|
+
private readonly cookies;
|
|
28
|
+
store(url: string, setCookie: string | string[] | undefined): void;
|
|
29
|
+
header(url: string): string | undefined;
|
|
30
|
+
}
|
|
31
|
+
/** HTTP-specific session boundary. One instance must be created per QAick execution. */
|
|
32
|
+
export declare class HttpExecutionSession implements HttpTransport {
|
|
33
|
+
private readonly transport;
|
|
34
|
+
readonly cookies: HttpCookieJar;
|
|
35
|
+
constructor(transport: HttpTransport, cookies?: HttpCookieJar);
|
|
36
|
+
request(url: string, init: {
|
|
37
|
+
method: string;
|
|
38
|
+
body?: unknown;
|
|
39
|
+
headers?: Record<string, string>;
|
|
40
|
+
signal: AbortSignal;
|
|
41
|
+
timeoutMs?: number;
|
|
42
|
+
}): Promise<HttpResponse>;
|
|
43
|
+
}
|
|
16
44
|
export type HttpActionErrorCode = 'HTTP_INVALID_URL' | 'HTTP_REQUEST_CONSTRUCTION_ERROR' | 'HTTP_ABORTED' | 'HTTP_TIMEOUT' | 'HTTP_NETWORK_ERROR' | 'HTTP_RESPONSE_PARSE_ERROR' | 'HTTP_STATUS_ERROR';
|
|
17
45
|
export declare class HttpActionError extends Error {
|
|
18
46
|
readonly code: HttpActionErrorCode;
|
|
@@ -1,3 +1,59 @@
|
|
|
1
|
+
/** Cookie state shared by HTTP Actions during one execution. */
|
|
2
|
+
export class HttpCookieJar {
|
|
3
|
+
cookies = new Map();
|
|
4
|
+
store(url, setCookie) {
|
|
5
|
+
if (!setCookie)
|
|
6
|
+
return;
|
|
7
|
+
const parsedUrl = new URL(url);
|
|
8
|
+
for (const header of (Array.isArray(setCookie) ? setCookie : splitSetCookieHeader(setCookie))) {
|
|
9
|
+
const cookie = parseSetCookie(header, parsedUrl);
|
|
10
|
+
if (!cookie)
|
|
11
|
+
continue;
|
|
12
|
+
const key = cookieKey(cookie);
|
|
13
|
+
if (cookie.expiresAt !== undefined && cookie.expiresAt <= Date.now())
|
|
14
|
+
this.cookies.delete(key);
|
|
15
|
+
else
|
|
16
|
+
this.cookies.set(key, cookie);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
header(url) {
|
|
20
|
+
const parsedUrl = new URL(url);
|
|
21
|
+
const now = Date.now();
|
|
22
|
+
const applicable = [];
|
|
23
|
+
for (const [key, cookie] of this.cookies) {
|
|
24
|
+
if (cookie.expiresAt !== undefined && cookie.expiresAt <= now) {
|
|
25
|
+
this.cookies.delete(key);
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
if (!cookieMatches(cookie, parsedUrl))
|
|
29
|
+
continue;
|
|
30
|
+
applicable.push(cookie);
|
|
31
|
+
}
|
|
32
|
+
applicable.sort((left, right) => right.path.length - left.path.length);
|
|
33
|
+
return applicable.length ? applicable.map(cookie => `${cookie.name}=${cookie.value}`).join('; ') : undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** HTTP-specific session boundary. One instance must be created per QAick execution. */
|
|
37
|
+
export class HttpExecutionSession {
|
|
38
|
+
transport;
|
|
39
|
+
cookies;
|
|
40
|
+
constructor(transport, cookies = new HttpCookieJar()) {
|
|
41
|
+
this.transport = transport;
|
|
42
|
+
this.cookies = cookies;
|
|
43
|
+
}
|
|
44
|
+
async request(url, init) {
|
|
45
|
+
const headers = { ...(init.headers ?? {}) };
|
|
46
|
+
const cookieHeader = headerName(headers, 'cookie');
|
|
47
|
+
const managed = this.cookies.header(url);
|
|
48
|
+
const merged = mergeCookieHeaders(cookieHeader ? headers[cookieHeader] : undefined, managed);
|
|
49
|
+
if (merged)
|
|
50
|
+
headers[cookieHeader ?? 'cookie'] = merged;
|
|
51
|
+
const response = await this.transport.request(url, { ...init, headers });
|
|
52
|
+
const setCookie = response.headers && headerValue(response.headers, 'set-cookie');
|
|
53
|
+
this.cookies.store(url, setCookie);
|
|
54
|
+
return response;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
1
57
|
export class HttpActionError extends Error {
|
|
2
58
|
code;
|
|
3
59
|
details;
|
|
@@ -8,6 +64,83 @@ export class HttpActionError extends Error {
|
|
|
8
64
|
this.name = 'HttpActionError';
|
|
9
65
|
}
|
|
10
66
|
}
|
|
67
|
+
function headerName(headers, expected) {
|
|
68
|
+
return Object.keys(headers).find(name => name.toLowerCase() === expected);
|
|
69
|
+
}
|
|
70
|
+
function headerValue(headers, expected) {
|
|
71
|
+
const name = Object.keys(headers).find(key => key.toLowerCase() === expected);
|
|
72
|
+
return name ? headers[name] : undefined;
|
|
73
|
+
}
|
|
74
|
+
function mergeCookieHeaders(explicit, managed) {
|
|
75
|
+
const cookies = new Map();
|
|
76
|
+
for (const value of [managed, explicit]) {
|
|
77
|
+
for (const item of value?.split(';') ?? []) {
|
|
78
|
+
const separator = item.indexOf('=');
|
|
79
|
+
if (separator <= 0)
|
|
80
|
+
continue;
|
|
81
|
+
cookies.set(item.slice(0, separator).trim(), item.slice(separator + 1).trim());
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return cookies.size ? [...cookies.entries()].map(([name, value]) => `${name}=${value}`).join('; ') : undefined;
|
|
85
|
+
}
|
|
86
|
+
function splitSetCookieHeader(value) {
|
|
87
|
+
return value.split(/\r?\n|,(?=\s*[^;,=\s]+=[^;,]*)/).map(item => item.trim()).filter(Boolean);
|
|
88
|
+
}
|
|
89
|
+
function parseSetCookie(header, requestUrl) {
|
|
90
|
+
const parts = header.split(';').map(part => part.trim());
|
|
91
|
+
const pair = parts.shift();
|
|
92
|
+
if (!pair)
|
|
93
|
+
return undefined;
|
|
94
|
+
const separator = pair.indexOf('=');
|
|
95
|
+
if (separator <= 0)
|
|
96
|
+
return undefined;
|
|
97
|
+
const name = pair.slice(0, separator).trim();
|
|
98
|
+
const value = pair.slice(separator + 1).trim();
|
|
99
|
+
let domain = requestUrl.hostname.toLowerCase();
|
|
100
|
+
let hostOnly = true;
|
|
101
|
+
let path = defaultCookiePath(requestUrl.pathname);
|
|
102
|
+
let secure = false;
|
|
103
|
+
let expiresAt;
|
|
104
|
+
for (const attribute of parts) {
|
|
105
|
+
const [rawName, ...rawValue] = attribute.split('=');
|
|
106
|
+
const attributeName = rawName.trim().toLowerCase();
|
|
107
|
+
const attributeValue = rawValue.join('=').trim();
|
|
108
|
+
if (attributeName === 'domain' && attributeValue) {
|
|
109
|
+
domain = attributeValue.replace(/^\./, '').toLowerCase();
|
|
110
|
+
hostOnly = false;
|
|
111
|
+
}
|
|
112
|
+
else if (attributeName === 'path' && attributeValue.startsWith('/'))
|
|
113
|
+
path = attributeValue;
|
|
114
|
+
else if (attributeName === 'secure')
|
|
115
|
+
secure = true;
|
|
116
|
+
else if (attributeName === 'max-age') {
|
|
117
|
+
const seconds = Number(attributeValue);
|
|
118
|
+
if (Number.isFinite(seconds))
|
|
119
|
+
expiresAt = Date.now() + seconds * 1000;
|
|
120
|
+
}
|
|
121
|
+
else if (attributeName === 'expires') {
|
|
122
|
+
const timestamp = Date.parse(attributeValue);
|
|
123
|
+
if (Number.isFinite(timestamp))
|
|
124
|
+
expiresAt = timestamp;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return { name, value, domain, path, hostOnly, secure, ...(expiresAt !== undefined ? { expiresAt } : {}) };
|
|
128
|
+
}
|
|
129
|
+
function defaultCookiePath(pathname) {
|
|
130
|
+
if (!pathname || !pathname.startsWith('/'))
|
|
131
|
+
return '/';
|
|
132
|
+
const lastSlash = pathname.lastIndexOf('/');
|
|
133
|
+
return lastSlash <= 0 ? '/' : pathname.slice(0, lastSlash);
|
|
134
|
+
}
|
|
135
|
+
function cookieMatches(cookie, url) {
|
|
136
|
+
if (cookie.secure && url.protocol !== 'https:')
|
|
137
|
+
return false;
|
|
138
|
+
const host = url.hostname.toLowerCase();
|
|
139
|
+
if (cookie.hostOnly ? host !== cookie.domain : !(host === cookie.domain || host.endsWith(`.${cookie.domain}`)))
|
|
140
|
+
return false;
|
|
141
|
+
return url.pathname === cookie.path || url.pathname.startsWith(cookie.path.endsWith('/') ? cookie.path : `${cookie.path}/`);
|
|
142
|
+
}
|
|
143
|
+
function cookieKey(cookie) { return `${cookie.name};${cookie.domain};${cookie.path}`; }
|
|
11
144
|
export class FetchHttpTransport {
|
|
12
145
|
async request(url, init) {
|
|
13
146
|
const headers = { ...init.headers };
|
|
@@ -25,7 +158,7 @@ export class FetchHttpTransport {
|
|
|
25
158
|
const timeout = init.timeoutMs === undefined ? undefined : setTimeout(() => controller.abort(), init.timeoutMs);
|
|
26
159
|
let response;
|
|
27
160
|
try {
|
|
28
|
-
response = await fetch(url, { method: init.method, headers, body, signal: controller.signal });
|
|
161
|
+
response = await fetch(url, { method: init.method, headers, body, signal: controller.signal, credentials: 'include' });
|
|
29
162
|
}
|
|
30
163
|
catch (error) {
|
|
31
164
|
if (init.signal.aborted)
|
package/package.json
CHANGED
|
@@ -1,19 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qaick/http-executor",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.2",
|
|
4
4
|
"description": "Reusable QAick HTTP Action execution with browser and native Node transports.",
|
|
5
|
-
"keywords": [
|
|
5
|
+
"keywords": [
|
|
6
|
+
"qaick",
|
|
7
|
+
"http",
|
|
8
|
+
"executor",
|
|
9
|
+
"automation"
|
|
10
|
+
],
|
|
6
11
|
"type": "module",
|
|
7
|
-
"files": [
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=20"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"tag": "alpha"
|
|
22
|
+
},
|
|
10
23
|
"main": "./dist/index.js",
|
|
11
24
|
"types": "./dist/index.d.ts",
|
|
12
25
|
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
|
|
26
|
+
".": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"import": "./dist/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./node": {
|
|
31
|
+
"types": "./dist/node-http-transport.d.ts",
|
|
32
|
+
"import": "./dist/node-http-transport.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.json",
|
|
37
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"prepublishOnly": "npm run build"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@qaick/executor": ">=0.1.0-alpha.1 <0.2.0"
|
|
15
43
|
},
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@qaick/executor": "0.1.0-alpha.1",
|
|
46
|
+
"@types/node": "^24.13.3",
|
|
47
|
+
"typescript": "~6.0.2",
|
|
48
|
+
"vitest": "^5.0.1"
|
|
49
|
+
}
|
|
19
50
|
}
|