hyperttp 0.2.6 → 3.0.1
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/Hyperttp/Client/HyperClient.d.ts +30 -0
- package/Hyperttp/Client/HyperClient.d.ts.map +1 -0
- package/Hyperttp/Client/HyperClient.js +215 -0
- package/Hyperttp/Client/HyperClient.js.map +1 -0
- package/{dist/Hyperttp → Hyperttp}/Request.d.ts +1 -1
- package/{dist/Hyperttp → Hyperttp}/Request.d.ts.map +1 -1
- package/{dist/Hyperttp → Hyperttp}/Request.js +2 -7
- package/Hyperttp/Request.js.map +1 -0
- package/{dist/Hyperttp → Hyperttp}/UrlExtractor.d.ts +1 -1
- package/{dist/Hyperttp → Hyperttp}/UrlExtractor.d.ts.map +1 -1
- package/{dist/Hyperttp → Hyperttp}/UrlExtractor.js +1 -4
- package/Hyperttp/UrlExtractor.js.map +1 -0
- package/Hyperttp/Utils/RequestBuilder.d.ts +32 -0
- package/Hyperttp/Utils/RequestBuilder.d.ts.map +1 -0
- package/Hyperttp/Utils/RequestBuilder.js +129 -0
- package/Hyperttp/Utils/RequestBuilder.js.map +1 -0
- package/Hyperttp/Utils/index.d.ts +2 -0
- package/Hyperttp/Utils/index.d.ts.map +1 -0
- package/Hyperttp/Utils/index.js +2 -0
- package/Hyperttp/Utils/index.js.map +1 -0
- package/Hyperttp/Utils/merge.d.ts +2 -0
- package/Hyperttp/Utils/merge.d.ts.map +1 -0
- package/Hyperttp/Utils/merge.js +25 -0
- package/Hyperttp/Utils/merge.js.map +1 -0
- package/Hyperttp/defaultConfig.d.ts +3 -0
- package/Hyperttp/defaultConfig.d.ts.map +1 -0
- package/Hyperttp/defaultConfig.js +39 -0
- package/Hyperttp/defaultConfig.js.map +1 -0
- package/Hyperttp/index.d.ts +6 -0
- package/Hyperttp/index.d.ts.map +1 -0
- package/Hyperttp/index.js +6 -0
- package/Hyperttp/index.js.map +1 -0
- package/{dist/Types → Types}/http-client.d.ts +2 -6
- package/Types/http-client.d.ts.map +1 -0
- package/Types/http-client.js +2 -0
- package/Types/index.d.ts +3 -0
- package/Types/index.d.ts.map +1 -0
- package/Types/index.js +3 -0
- package/Types/index.js.map +1 -0
- package/Types/url-extractor.js +2 -0
- package/index.d.ts +13 -0
- package/index.d.ts.map +1 -0
- package/index.js +12 -0
- package/index.js.map +1 -0
- package/package.json +22 -11
- package/LICENSE +0 -21
- package/README.md +0 -195
- package/dist/Hyperttp/Core/CacheManager.d.ts +0 -84
- package/dist/Hyperttp/Core/CacheManager.d.ts.map +0 -1
- package/dist/Hyperttp/Core/CacheManager.js +0 -109
- package/dist/Hyperttp/Core/CacheManager.js.map +0 -1
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts +0 -149
- package/dist/Hyperttp/Core/HttpClientImproved.d.ts.map +0 -1
- package/dist/Hyperttp/Core/HttpClientImproved.js +0 -541
- package/dist/Hyperttp/Core/HttpClientImproved.js.map +0 -1
- package/dist/Hyperttp/Core/InterceptorManager.d.ts +0 -62
- package/dist/Hyperttp/Core/InterceptorManager.d.ts.map +0 -1
- package/dist/Hyperttp/Core/InterceptorManager.js +0 -64
- package/dist/Hyperttp/Core/InterceptorManager.js.map +0 -1
- package/dist/Hyperttp/Core/MetricsManager.d.ts +0 -71
- package/dist/Hyperttp/Core/MetricsManager.d.ts.map +0 -1
- package/dist/Hyperttp/Core/MetricsManager.js +0 -246
- package/dist/Hyperttp/Core/MetricsManager.js.map +0 -1
- package/dist/Hyperttp/Core/QueueManager.d.ts +0 -16
- package/dist/Hyperttp/Core/QueueManager.d.ts.map +0 -1
- package/dist/Hyperttp/Core/QueueManager.js +0 -75
- package/dist/Hyperttp/Core/QueueManager.js.map +0 -1
- package/dist/Hyperttp/Core/RateLimiter.d.ts +0 -63
- package/dist/Hyperttp/Core/RateLimiter.d.ts.map +0 -1
- package/dist/Hyperttp/Core/RateLimiter.js +0 -170
- package/dist/Hyperttp/Core/RateLimiter.js.map +0 -1
- package/dist/Hyperttp/Core/RequestBuilder.d.ts +0 -83
- package/dist/Hyperttp/Core/RequestBuilder.d.ts.map +0 -1
- package/dist/Hyperttp/Core/RequestBuilder.js +0 -169
- package/dist/Hyperttp/Core/RequestBuilder.js.map +0 -1
- package/dist/Hyperttp/Core/RequestExecutor.d.ts +0 -31
- package/dist/Hyperttp/Core/RequestExecutor.d.ts.map +0 -1
- package/dist/Hyperttp/Core/RequestExecutor.js +0 -167
- package/dist/Hyperttp/Core/RequestExecutor.js.map +0 -1
- package/dist/Hyperttp/Core/RequestProfiler.d.ts +0 -10
- package/dist/Hyperttp/Core/RequestProfiler.d.ts.map +0 -1
- package/dist/Hyperttp/Core/RequestProfiler.js +0 -55
- package/dist/Hyperttp/Core/RequestProfiler.js.map +0 -1
- package/dist/Hyperttp/Core/ResponseConverter.d.ts +0 -23
- package/dist/Hyperttp/Core/ResponseConverter.d.ts.map +0 -1
- package/dist/Hyperttp/Core/ResponseConverter.js +0 -369
- package/dist/Hyperttp/Core/ResponseConverter.js.map +0 -1
- package/dist/Hyperttp/Core/index.d.ts +0 -27
- package/dist/Hyperttp/Core/index.d.ts.map +0 -1
- package/dist/Hyperttp/Core/index.js +0 -56
- package/dist/Hyperttp/Core/index.js.map +0 -1
- package/dist/Hyperttp/Request.js.map +0 -1
- package/dist/Hyperttp/UrlExtractor.js.map +0 -1
- package/dist/Hyperttp/index.d.ts +0 -5
- package/dist/Hyperttp/index.d.ts.map +0 -1
- package/dist/Hyperttp/index.js +0 -24
- package/dist/Hyperttp/index.js.map +0 -1
- package/dist/Types/cache.d.ts +0 -10
- package/dist/Types/cache.d.ts.map +0 -1
- package/dist/Types/cache.js +0 -3
- package/dist/Types/cache.js.map +0 -1
- package/dist/Types/errors.d.ts +0 -15
- package/dist/Types/errors.d.ts.map +0 -1
- package/dist/Types/errors.js +0 -34
- package/dist/Types/errors.js.map +0 -1
- package/dist/Types/http-client.d.ts.map +0 -1
- package/dist/Types/http-client.js +0 -3
- package/dist/Types/http.d.ts +0 -5
- package/dist/Types/http.d.ts.map +0 -1
- package/dist/Types/http.js +0 -3
- package/dist/Types/http.js.map +0 -1
- package/dist/Types/index.d.ts +0 -13
- package/dist/Types/index.d.ts.map +0 -1
- package/dist/Types/index.js +0 -29
- package/dist/Types/index.js.map +0 -1
- package/dist/Types/interceptors.d.ts +0 -13
- package/dist/Types/interceptors.d.ts.map +0 -1
- package/dist/Types/interceptors.js +0 -3
- package/dist/Types/interceptors.js.map +0 -1
- package/dist/Types/metrics.d.ts +0 -90
- package/dist/Types/metrics.d.ts.map +0 -1
- package/dist/Types/metrics.js +0 -3
- package/dist/Types/metrics.js.map +0 -1
- package/dist/Types/options.d.ts +0 -233
- package/dist/Types/options.d.ts.map +0 -1
- package/dist/Types/options.js +0 -3
- package/dist/Types/options.js.map +0 -1
- package/dist/Types/queue.d.ts +0 -8
- package/dist/Types/queue.d.ts.map +0 -1
- package/dist/Types/queue.js +0 -3
- package/dist/Types/queue.js.map +0 -1
- package/dist/Types/request.d.ts +0 -189
- package/dist/Types/request.d.ts.map +0 -1
- package/dist/Types/request.js +0 -3
- package/dist/Types/request.js.map +0 -1
- package/dist/Types/response.d.ts +0 -28
- package/dist/Types/response.d.ts.map +0 -1
- package/dist/Types/response.js +0 -3
- package/dist/Types/response.js.map +0 -1
- package/dist/Types/stream.d.ts +0 -39
- package/dist/Types/stream.d.ts.map +0 -1
- package/dist/Types/stream.js +0 -3
- package/dist/Types/stream.js.map +0 -1
- package/dist/Types/url-extractor.js +0 -3
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +0 -1
- /package/{dist/Types → Types}/http-client.js.map +0 -0
- /package/{dist/Types → Types}/url-extractor.d.ts +0 -0
- /package/{dist/Types → Types}/url-extractor.d.ts.map +0 -0
- /package/{dist/Types → Types}/url-extractor.js.map +0 -0
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RequestExecutor = void 0;
|
|
4
|
-
const undici_1 = require("undici");
|
|
5
|
-
const errors_1 = require("../../Types/errors");
|
|
6
|
-
class RequestExecutor {
|
|
7
|
-
agent;
|
|
8
|
-
interceptors;
|
|
9
|
-
options;
|
|
10
|
-
redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
|
|
11
|
-
constructor(agent, interceptors, options) {
|
|
12
|
-
this.agent = agent;
|
|
13
|
-
this.interceptors = interceptors;
|
|
14
|
-
this.options = options;
|
|
15
|
-
}
|
|
16
|
-
calcDelay(attempt) {
|
|
17
|
-
const { baseDelay, maxDelay, jitter } = this.options.retryOptions;
|
|
18
|
-
const base = Math.min(baseDelay * Math.pow(2, attempt), maxDelay);
|
|
19
|
-
return jitter ? base * (0.75 + Math.random() * 0.5) : base;
|
|
20
|
-
}
|
|
21
|
-
sleep(ms) {
|
|
22
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
23
|
-
}
|
|
24
|
-
async drainBody(body) {
|
|
25
|
-
if (!body)
|
|
26
|
-
return;
|
|
27
|
-
try {
|
|
28
|
-
if (typeof body.dump === "function") {
|
|
29
|
-
await body.dump();
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
if (typeof body.resume === "function") {
|
|
33
|
-
body.resume();
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
if (typeof body.destroy === "function") {
|
|
37
|
-
body.destroy();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
/* ignore */
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
async executeCore(method, url, headers, body, metrics, signal, parser) {
|
|
45
|
-
let currentUrl = url;
|
|
46
|
-
let currentMethod = method;
|
|
47
|
-
let currentHeaders = headers;
|
|
48
|
-
let currentBody = body;
|
|
49
|
-
let redirects = 0;
|
|
50
|
-
let attempt = 0;
|
|
51
|
-
const timeoutController = new AbortController();
|
|
52
|
-
const timer = setTimeout(() => timeoutController.abort(), this.options.timeout);
|
|
53
|
-
const abortHandler = () => timeoutController.abort();
|
|
54
|
-
if (signal) {
|
|
55
|
-
if (signal.aborted) {
|
|
56
|
-
clearTimeout(timer);
|
|
57
|
-
throw new errors_1.HttpClientError("Request aborted by user", "ABORTED", 0, undefined, url, method);
|
|
58
|
-
}
|
|
59
|
-
signal.addEventListener("abort", abortHandler, { once: true });
|
|
60
|
-
}
|
|
61
|
-
try {
|
|
62
|
-
while (true) {
|
|
63
|
-
try {
|
|
64
|
-
const config = await this.interceptors.applyRequest({
|
|
65
|
-
url: currentUrl,
|
|
66
|
-
method: currentMethod,
|
|
67
|
-
headers: currentHeaders,
|
|
68
|
-
body: currentBody,
|
|
69
|
-
});
|
|
70
|
-
const res = await (0, undici_1.request)(config.url, {
|
|
71
|
-
method: config.method,
|
|
72
|
-
headers: config.headers,
|
|
73
|
-
body: config.body,
|
|
74
|
-
dispatcher: this.agent,
|
|
75
|
-
signal: timeoutController.signal,
|
|
76
|
-
});
|
|
77
|
-
const status = res.statusCode;
|
|
78
|
-
const resHeaders = res.headers;
|
|
79
|
-
if (this.options.followRedirects &&
|
|
80
|
-
this.redirectStatusCodes.has(status)) {
|
|
81
|
-
if (redirects >= this.options.maxRedirects) {
|
|
82
|
-
await this.drainBody(res.body);
|
|
83
|
-
throw new errors_1.HttpClientError("Too many redirects", "TOO_MANY_REDIRECTS", status);
|
|
84
|
-
}
|
|
85
|
-
const location = resHeaders.location;
|
|
86
|
-
if (location) {
|
|
87
|
-
await this.drainBody(res.body);
|
|
88
|
-
const nextUrl = new URL(location, config.url).toString();
|
|
89
|
-
const nextMethod = status === 303 ? "GET" : currentMethod;
|
|
90
|
-
currentUrl = nextUrl;
|
|
91
|
-
currentMethod = nextMethod;
|
|
92
|
-
currentBody = nextMethod === "GET" ? undefined : currentBody;
|
|
93
|
-
if (nextMethod === "GET") {
|
|
94
|
-
if (currentHeaders["content-type"] ||
|
|
95
|
-
currentHeaders["Content-Type"] ||
|
|
96
|
-
currentHeaders["content-length"] ||
|
|
97
|
-
currentHeaders["Content-Length"]) {
|
|
98
|
-
const nextHeaders = { ...currentHeaders };
|
|
99
|
-
delete nextHeaders["content-type"];
|
|
100
|
-
delete nextHeaders["Content-Type"];
|
|
101
|
-
delete nextHeaders["content-length"];
|
|
102
|
-
delete nextHeaders["Content-Length"];
|
|
103
|
-
currentHeaders = nextHeaders;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
redirects++;
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (status >= 500) {
|
|
111
|
-
if (attempt < this.options.maxRetries) {
|
|
112
|
-
if (metrics)
|
|
113
|
-
metrics.retries += 1;
|
|
114
|
-
await this.drainBody(res.body);
|
|
115
|
-
const delay = this.calcDelay(attempt);
|
|
116
|
-
if (delay > 0)
|
|
117
|
-
await this.sleep(delay);
|
|
118
|
-
attempt++;
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
throw new errors_1.HttpClientError(`HTTP ${status}`, "HTTP_ERROR", status, undefined, config.url, currentMethod);
|
|
122
|
-
}
|
|
123
|
-
const transformed = await this.interceptors.applyResponse({
|
|
124
|
-
status,
|
|
125
|
-
headers: resHeaders,
|
|
126
|
-
body: res.body,
|
|
127
|
-
url: config.url,
|
|
128
|
-
});
|
|
129
|
-
const parsed = await parser(transformed);
|
|
130
|
-
return {
|
|
131
|
-
status: transformed.status,
|
|
132
|
-
headers: transformed.headers,
|
|
133
|
-
body: parsed,
|
|
134
|
-
url: transformed.url,
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
catch (err) {
|
|
138
|
-
if (err?.name === "AbortError") {
|
|
139
|
-
if (signal?.aborted) {
|
|
140
|
-
throw new errors_1.HttpClientError("Request aborted by user", "ABORTED", 0, err, url, method);
|
|
141
|
-
}
|
|
142
|
-
throw new errors_1.TimeoutError(url, this.options.timeout);
|
|
143
|
-
}
|
|
144
|
-
if (attempt < this.options.maxRetries &&
|
|
145
|
-
(err?.code === "ECONNREFUSED" || err?.code === "ETIMEDOUT")) {
|
|
146
|
-
if (metrics)
|
|
147
|
-
metrics.retries += 1;
|
|
148
|
-
await this.sleep(this.calcDelay(attempt));
|
|
149
|
-
attempt++;
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
throw err;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
finally {
|
|
157
|
-
clearTimeout(timer);
|
|
158
|
-
if (signal)
|
|
159
|
-
signal.removeEventListener("abort", abortHandler);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
async execute(method, url, headers, body, metrics, signal) {
|
|
163
|
-
return this.executeCore(method, url, headers, body, metrics, signal, async (res) => res.body);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
exports.RequestExecutor = RequestExecutor;
|
|
167
|
-
//# sourceMappingURL=RequestExecutor.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RequestExecutor.js","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestExecutor.ts"],"names":[],"mappings":";;;AAAA,mCAAwC;AAKxC,+CAAmE;AASnE,MAAa,eAAe;IAIhB;IACA;IACA;IALO,mBAAmB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAE1E,YACU,KAAY,EACZ,YAAgC,EAChC,OAQP;QAVO,UAAK,GAAL,KAAK,CAAO;QACZ,iBAAY,GAAZ,YAAY,CAAoB;QAChC,YAAO,GAAP,OAAO,CAQd;IACA,CAAC;IAEI,SAAS,CAAC,OAAe;QAC/B,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,YAAY,CAAC;QAClE,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,SAAU,GAAG,IAAI,CAAC,GAAG,CA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{
|
|
3
|
-
result: T;
|
|
4
|
-
profile: RequestPerformanceProfile;
|
|
5
|
-
};
|
|
6
|
-
export declare class RequestProfiler {
|
|
7
|
-
static profile<T>(executor: () => Promise<T>): Promise<ProfiledResult<T>>;
|
|
8
|
-
}
|
|
9
|
-
export type { ProfiledResult };
|
|
10
|
-
//# sourceMappingURL=RequestProfiler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RequestProfiler.d.ts","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestProfiler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,yBAAyB,EAC1B,MAAM,wBAAwB,CAAC;AAEhC,KAAK,cAAc,CAAC,CAAC,IAAI;IACvB,MAAM,EAAE,CAAC,CAAC;IACV,OAAO,EAAE,yBAAyB,CAAC;CACpC,CAAC;AAaF,qBAAa,eAAe;WACb,OAAO,CAAC,CAAC,EACpB,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GACzB,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;CA2C9B;AAED,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RequestProfiler = void 0;
|
|
4
|
-
function toMemorySnapshot() {
|
|
5
|
-
const usage = process.memoryUsage();
|
|
6
|
-
return {
|
|
7
|
-
rss: usage.rss,
|
|
8
|
-
heapTotal: usage.heapTotal,
|
|
9
|
-
heapUsed: usage.heapUsed,
|
|
10
|
-
external: usage.external,
|
|
11
|
-
arrayBuffers: usage.arrayBuffers,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
class RequestProfiler {
|
|
15
|
-
static async profile(executor) {
|
|
16
|
-
const startedAt = Date.now();
|
|
17
|
-
const hrStart = process.hrtime.bigint();
|
|
18
|
-
const cpuStart = process.cpuUsage();
|
|
19
|
-
const memoryBefore = toMemorySnapshot();
|
|
20
|
-
const result = await executor();
|
|
21
|
-
const hrEnd = process.hrtime.bigint();
|
|
22
|
-
const finishedAt = Date.now();
|
|
23
|
-
const cpuDiff = process.cpuUsage(cpuStart);
|
|
24
|
-
const memoryAfter = toMemorySnapshot();
|
|
25
|
-
const durationMs = Number(hrEnd - hrStart) / 1_000_000;
|
|
26
|
-
const cpuUserMs = cpuDiff.user / 1000;
|
|
27
|
-
const cpuSystemMs = cpuDiff.system / 1000;
|
|
28
|
-
const cpuTotalMs = cpuUserMs + cpuSystemMs;
|
|
29
|
-
const profile = {
|
|
30
|
-
wallClockStart: startedAt,
|
|
31
|
-
wallClockEnd: finishedAt,
|
|
32
|
-
durationMs,
|
|
33
|
-
cpu: {
|
|
34
|
-
userMs: cpuUserMs,
|
|
35
|
-
systemMs: cpuSystemMs,
|
|
36
|
-
totalMs: cpuTotalMs,
|
|
37
|
-
percent: durationMs > 0 ? (cpuTotalMs / durationMs) * 100 : 0,
|
|
38
|
-
},
|
|
39
|
-
memory: {
|
|
40
|
-
before: memoryBefore,
|
|
41
|
-
after: memoryAfter,
|
|
42
|
-
delta: {
|
|
43
|
-
rss: memoryAfter.rss - memoryBefore.rss,
|
|
44
|
-
heapTotal: memoryAfter.heapTotal - memoryBefore.heapTotal,
|
|
45
|
-
heapUsed: memoryAfter.heapUsed - memoryBefore.heapUsed,
|
|
46
|
-
external: memoryAfter.external - memoryBefore.external,
|
|
47
|
-
arrayBuffers: memoryAfter.arrayBuffers - memoryBefore.arrayBuffers,
|
|
48
|
-
},
|
|
49
|
-
},
|
|
50
|
-
};
|
|
51
|
-
return { result, profile };
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.RequestProfiler = RequestProfiler;
|
|
55
|
-
//# sourceMappingURL=RequestProfiler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"RequestProfiler.js","sourceRoot":"","sources":["../../../src/Hyperttp/Core/RequestProfiler.ts"],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G,EAAE;gBACH,MAAM,EAAE,SAAS;gBACjB,QAAQ,EAAE,WAAW;gBACrB,OAAO,EAAE,UAAU;gBACnB,OAAO,EAAE,UAAU,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,UAAU,GAAG,UAAU,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;aAC9D;YACD,MAAM,EAAE;gBACN,MAAM,EAAE,YAAY;gBACpB,KAAK,EAAE,WAAW;gBAClB,KAAK,EAAE;oBACL,GAAG,EAAE,WAAW,CAAC,GAAG,GAAG,YAAY,CAAC,GAAG;oBACvC,SAAS,EAAE,WAAW,CAAC,SAAS,GAAG,YAAY,CAAC,SAAS;oBACzD,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,YAAY,CAAC,QAAQ;oBACtD,QAAQ,EAAE,WAAW,CAAC,QAAQ,GAAG,YAAY,CAAC,QAAQ;oBACtD,YAAY,EAAE,WAAW,CAAC,YAAY,GAAG,YAAY,CAAC,YAAY;iBACnE;aACF;SACF,CAAC;QAEF,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAC7B,CAAC;CACF;AA9CD,0CA8CC"}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { ResponseType, SourceType } from "../../Types/http";
|
|
2
|
-
import { ConversionMeta } from "../../Types/request";
|
|
3
|
-
import { ResponseConverterOptions, ParsedResponse } from "../../Types/response";
|
|
4
|
-
export declare class ResponseConverter {
|
|
5
|
-
private readonly options;
|
|
6
|
-
private readonly xmlParser;
|
|
7
|
-
private readonly xmlBuilder;
|
|
8
|
-
constructor(options?: ResponseConverterOptions);
|
|
9
|
-
readBody(body: any): Promise<Buffer>;
|
|
10
|
-
decodeBody(body: Buffer, encoding?: string, charset?: BufferEncoding): Promise<string>;
|
|
11
|
-
detectSourceType(contentType?: string, text?: string, url?: string): SourceType;
|
|
12
|
-
convert(body: Buffer, targetType: ResponseType, meta?: ConversionMeta): Promise<ParsedResponse>;
|
|
13
|
-
private toAuto;
|
|
14
|
-
private toJson;
|
|
15
|
-
private toXml;
|
|
16
|
-
private toHtml;
|
|
17
|
-
private htmlToJson;
|
|
18
|
-
private safeJsonParse;
|
|
19
|
-
private normalizeResponseShape;
|
|
20
|
-
private escapeXml;
|
|
21
|
-
toBuffer(input: any): Buffer;
|
|
22
|
-
}
|
|
23
|
-
//# sourceMappingURL=ResponseConverter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ResponseConverter.d.ts","sourceRoot":"","sources":["../../../src/Hyperttp/Core/ResponseConverter.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,wBAAwB,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAWhF,qBAAa,iBAAiB;IAIhB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAHpC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAY;IACtC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAkC;gBAEhC,OAAO,GAAE,wBAA6B;IAgB7D,QAAQ,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC;IAgCpC,UAAU,CACd,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,MAAM,EACjB,OAAO,GAAE,cAAgD,GACxD,OAAO,CAAC,MAAM,CAAC;IAqBlB,gBAAgB,CACd,WAAW,CAAC,EAAE,MAAM,EACpB,IAAI,CAAC,EAAE,MAAM,EACb,GAAG,CAAC,EAAE,MAAM,GACX,UAAU;IA6CP,OAAO,CACX,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,YAAY,EACxB,IAAI,GAAE,cAAmB,GACxB,OAAO,CAAC,cAAc,CAAC;IAgC1B,OAAO,CAAC,MAAM;IAiCd,OAAO,CAAC,MAAM;IAyCd,OAAO,CAAC,KAAK;IAoBb,OAAO,CAAC,MAAM;IAwBd,OAAO,CAAC,UAAU;IAkDlB,OAAO,CAAC,aAAa;IAUrB,OAAO,CAAC,sBAAsB;IAyC9B,OAAO,CAAC,SAAS;IASjB,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM;CAK7B"}
|
|
@@ -1,369 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.ResponseConverter = void 0;
|
|
40
|
-
const zlib = __importStar(require("zlib"));
|
|
41
|
-
const util_1 = require("util");
|
|
42
|
-
const fast_xml_parser_1 = require("fast-xml-parser");
|
|
43
|
-
const fast_xml_builder_1 = __importDefault(require("fast-xml-builder"));
|
|
44
|
-
const cheerio = __importStar(require("cheerio"));
|
|
45
|
-
const gunzip = (0, util_1.promisify)(zlib.gunzip);
|
|
46
|
-
const inflate = (0, util_1.promisify)(zlib.inflate);
|
|
47
|
-
const brotliDecompress = (0, util_1.promisify)(zlib.brotliDecompress);
|
|
48
|
-
function normalizeContentType(ct) {
|
|
49
|
-
if (!ct)
|
|
50
|
-
return undefined;
|
|
51
|
-
return ct.split(";")[0].trim().toLowerCase();
|
|
52
|
-
}
|
|
53
|
-
class ResponseConverter {
|
|
54
|
-
options;
|
|
55
|
-
xmlParser;
|
|
56
|
-
xmlBuilder;
|
|
57
|
-
constructor(options = {}) {
|
|
58
|
-
this.options = options;
|
|
59
|
-
this.xmlParser = new fast_xml_parser_1.XMLParser({
|
|
60
|
-
ignoreAttributes: false,
|
|
61
|
-
attributeNamePrefix: "@_",
|
|
62
|
-
parseTagValue: true,
|
|
63
|
-
parseAttributeValue: true,
|
|
64
|
-
trimValues: true,
|
|
65
|
-
});
|
|
66
|
-
this.xmlBuilder = new fast_xml_builder_1.default({
|
|
67
|
-
ignoreAttributes: false,
|
|
68
|
-
attributeNamePrefix: "@_",
|
|
69
|
-
format: true,
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
async readBody(body) {
|
|
73
|
-
if (!body)
|
|
74
|
-
return Buffer.alloc(0);
|
|
75
|
-
if (Buffer.isBuffer(body)) {
|
|
76
|
-
return body;
|
|
77
|
-
}
|
|
78
|
-
if (typeof body.arrayBuffer === "function") {
|
|
79
|
-
return Buffer.from(await body.arrayBuffer());
|
|
80
|
-
}
|
|
81
|
-
const chunks = [];
|
|
82
|
-
let received = 0;
|
|
83
|
-
const max = this.options.maxBodySize ?? 0;
|
|
84
|
-
for await (const chunk of body) {
|
|
85
|
-
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
86
|
-
received += buf.length;
|
|
87
|
-
if (max > 0 && received > max) {
|
|
88
|
-
if (typeof body.destroy === "function") {
|
|
89
|
-
body.destroy();
|
|
90
|
-
}
|
|
91
|
-
throw new Error(`Response size limit exceeded (${max} bytes)`);
|
|
92
|
-
}
|
|
93
|
-
chunks.push(buf);
|
|
94
|
-
}
|
|
95
|
-
return Buffer.concat(chunks);
|
|
96
|
-
}
|
|
97
|
-
async decodeBody(body, encoding, charset = this.options.charset ?? "utf-8") {
|
|
98
|
-
if (!encoding || body.length === 0) {
|
|
99
|
-
return body.toString(charset);
|
|
100
|
-
}
|
|
101
|
-
try {
|
|
102
|
-
switch (encoding.toLowerCase()) {
|
|
103
|
-
case "gzip":
|
|
104
|
-
return (await gunzip(body)).toString(charset);
|
|
105
|
-
case "deflate":
|
|
106
|
-
return (await inflate(body)).toString(charset);
|
|
107
|
-
case "br":
|
|
108
|
-
return (await brotliDecompress(body)).toString(charset);
|
|
109
|
-
default:
|
|
110
|
-
return body.toString(charset);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
catch {
|
|
114
|
-
return body.toString(charset);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
detectSourceType(contentType, text, url) {
|
|
118
|
-
const ct = normalizeContentType(contentType)?.replace(/\s+/g, "");
|
|
119
|
-
const sample = (text || "").trimStart().slice(0, 512).toLowerCase();
|
|
120
|
-
const isHtml = ct === "text/html" ||
|
|
121
|
-
sample.includes("<!doctype html") ||
|
|
122
|
-
sample.includes("<html") ||
|
|
123
|
-
sample.includes("<head") ||
|
|
124
|
-
sample.includes("<body");
|
|
125
|
-
const isJson = ct === "application/json" ||
|
|
126
|
-
ct?.endsWith("+json") ||
|
|
127
|
-
sample.startsWith("{") ||
|
|
128
|
-
sample.startsWith("[");
|
|
129
|
-
const isXml = ct === "application/xml" ||
|
|
130
|
-
ct === "text/xml" ||
|
|
131
|
-
(sample.startsWith("<") && !isHtml);
|
|
132
|
-
if (ct?.startsWith("image/") ||
|
|
133
|
-
ct?.startsWith("audio/") ||
|
|
134
|
-
ct?.startsWith("video/") ||
|
|
135
|
-
ct === "application/octet-stream") {
|
|
136
|
-
return "buffer";
|
|
137
|
-
}
|
|
138
|
-
if (isJson)
|
|
139
|
-
return "json";
|
|
140
|
-
if (isHtml)
|
|
141
|
-
return "html";
|
|
142
|
-
if (isXml)
|
|
143
|
-
return "xml";
|
|
144
|
-
if (url) {
|
|
145
|
-
const lower = url.toLowerCase();
|
|
146
|
-
if (lower.includes(".json"))
|
|
147
|
-
return "json";
|
|
148
|
-
if (lower.includes(".xml"))
|
|
149
|
-
return "xml";
|
|
150
|
-
if (lower.includes(".html") || lower.includes(".htm"))
|
|
151
|
-
return "html";
|
|
152
|
-
}
|
|
153
|
-
return "text";
|
|
154
|
-
}
|
|
155
|
-
async convert(body, targetType, meta = {}) {
|
|
156
|
-
const text = await this.decodeBody(body, meta.contentEncoding, this.options.charset ?? "utf-8");
|
|
157
|
-
const sourceType = this.detectSourceType(meta.contentType, text, meta.url);
|
|
158
|
-
const trimmed = text.trim();
|
|
159
|
-
switch (targetType) {
|
|
160
|
-
case "buffer":
|
|
161
|
-
return body;
|
|
162
|
-
case "text":
|
|
163
|
-
return text;
|
|
164
|
-
case "json":
|
|
165
|
-
return this.toJson(trimmed, sourceType, meta.url);
|
|
166
|
-
case "xml":
|
|
167
|
-
return this.toXml(trimmed, sourceType);
|
|
168
|
-
case "html":
|
|
169
|
-
return this.toHtml(trimmed, sourceType);
|
|
170
|
-
case "auto":
|
|
171
|
-
default:
|
|
172
|
-
return this.toAuto(trimmed, sourceType, meta.url, body);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
toAuto(text, sourceType, url, rawBody) {
|
|
176
|
-
if (sourceType === "buffer") {
|
|
177
|
-
return rawBody ?? Buffer.alloc(0);
|
|
178
|
-
}
|
|
179
|
-
if (!text)
|
|
180
|
-
return null;
|
|
181
|
-
switch (sourceType) {
|
|
182
|
-
case "json":
|
|
183
|
-
return this.safeJsonParse(text);
|
|
184
|
-
case "xml":
|
|
185
|
-
return this.xmlParser.parse(text);
|
|
186
|
-
case "html":
|
|
187
|
-
return this.htmlToJson(text);
|
|
188
|
-
case "text":
|
|
189
|
-
default:
|
|
190
|
-
if (url &&
|
|
191
|
-
(url.toLowerCase().endsWith(".json") ||
|
|
192
|
-
text.startsWith("{") ||
|
|
193
|
-
text.startsWith("["))) {
|
|
194
|
-
return this.safeJsonParse(text);
|
|
195
|
-
}
|
|
196
|
-
return text;
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
toJson(text, sourceType, url) {
|
|
200
|
-
if (!text)
|
|
201
|
-
return null;
|
|
202
|
-
if (sourceType === "json") {
|
|
203
|
-
return this.safeJsonParse(text);
|
|
204
|
-
}
|
|
205
|
-
if (sourceType === "xml") {
|
|
206
|
-
return this.xmlParser.parse(text);
|
|
207
|
-
}
|
|
208
|
-
if (sourceType === "html") {
|
|
209
|
-
return this.htmlToJson(text);
|
|
210
|
-
}
|
|
211
|
-
if (url && url.toLowerCase().includes("/download-info")) {
|
|
212
|
-
const parsed = this.safeJsonParse(text);
|
|
213
|
-
return this.normalizeResponseShape(parsed, url);
|
|
214
|
-
}
|
|
215
|
-
if (text.startsWith("{") || text.startsWith("[")) {
|
|
216
|
-
const parsed = this.safeJsonParse(text);
|
|
217
|
-
return this.normalizeResponseShape(parsed, url);
|
|
218
|
-
}
|
|
219
|
-
if (text.startsWith("<")) {
|
|
220
|
-
if (text.startsWith("<html") || text.startsWith("<!doctype html")) {
|
|
221
|
-
return this.htmlToJson(text);
|
|
222
|
-
}
|
|
223
|
-
return this.xmlParser.parse(text);
|
|
224
|
-
}
|
|
225
|
-
return {
|
|
226
|
-
data: text,
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
toXml(text, sourceType) {
|
|
230
|
-
if (!text)
|
|
231
|
-
return "";
|
|
232
|
-
if (sourceType === "xml") {
|
|
233
|
-
return text;
|
|
234
|
-
}
|
|
235
|
-
if (sourceType === "json") {
|
|
236
|
-
const parsed = this.safeJsonParse(text);
|
|
237
|
-
return this.xmlBuilder.build(parsed);
|
|
238
|
-
}
|
|
239
|
-
if (sourceType === "html") {
|
|
240
|
-
const json = this.htmlToJson(text);
|
|
241
|
-
return this.xmlBuilder.build(json);
|
|
242
|
-
}
|
|
243
|
-
return `<root>${this.escapeXml(text)}</root>`;
|
|
244
|
-
}
|
|
245
|
-
toHtml(text, sourceType) {
|
|
246
|
-
if (!text)
|
|
247
|
-
return null;
|
|
248
|
-
if (sourceType === "html") {
|
|
249
|
-
return this.htmlToJson(text);
|
|
250
|
-
}
|
|
251
|
-
if (sourceType === "json") {
|
|
252
|
-
const parsed = this.safeJsonParse(text);
|
|
253
|
-
return {
|
|
254
|
-
html: parsed,
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
if (sourceType === "xml") {
|
|
258
|
-
const parsed = this.xmlParser.parse(text);
|
|
259
|
-
return {
|
|
260
|
-
xml: parsed,
|
|
261
|
-
};
|
|
262
|
-
}
|
|
263
|
-
return this.htmlToJson(text);
|
|
264
|
-
}
|
|
265
|
-
htmlToJson(html) {
|
|
266
|
-
if (this.options.parseHTML === false) {
|
|
267
|
-
return html;
|
|
268
|
-
}
|
|
269
|
-
const $ = cheerio.load(html);
|
|
270
|
-
if (this.options.htmlMode === "simple") {
|
|
271
|
-
return {
|
|
272
|
-
title: $("title").text(),
|
|
273
|
-
text: $("body").text().trim(),
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
const result = {
|
|
277
|
-
title: $("title").text() || undefined,
|
|
278
|
-
meta: {},
|
|
279
|
-
body: {
|
|
280
|
-
text: $("body").text().trim(),
|
|
281
|
-
},
|
|
282
|
-
};
|
|
283
|
-
$("meta").each((_, el) => {
|
|
284
|
-
const name = $(el).attr("name") || $(el).attr("property") || $(el).attr("charset");
|
|
285
|
-
const content = $(el).attr("content") || $(el).attr("value") || "";
|
|
286
|
-
if (name) {
|
|
287
|
-
result.meta[name] = content;
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
$("body")
|
|
291
|
-
.children()
|
|
292
|
-
.each((_, el) => {
|
|
293
|
-
const tag = el.tagName?.toLowerCase();
|
|
294
|
-
if (!tag)
|
|
295
|
-
return;
|
|
296
|
-
const text = $(el).text().trim();
|
|
297
|
-
if (!text)
|
|
298
|
-
return;
|
|
299
|
-
if (!result.body[tag]) {
|
|
300
|
-
result.body[tag] = [];
|
|
301
|
-
}
|
|
302
|
-
result.body[tag].push(text);
|
|
303
|
-
});
|
|
304
|
-
return result;
|
|
305
|
-
}
|
|
306
|
-
safeJsonParse(text) {
|
|
307
|
-
if (!text)
|
|
308
|
-
return null;
|
|
309
|
-
try {
|
|
310
|
-
return JSON.parse(text);
|
|
311
|
-
}
|
|
312
|
-
catch {
|
|
313
|
-
return { data: text };
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
normalizeResponseShape(value, url) {
|
|
317
|
-
if (value === null || value === undefined)
|
|
318
|
-
return value;
|
|
319
|
-
if (Array.isArray(value))
|
|
320
|
-
return value;
|
|
321
|
-
if (typeof value !== "object")
|
|
322
|
-
return value;
|
|
323
|
-
const obj = value;
|
|
324
|
-
if (obj.downloadInfo !== undefined) {
|
|
325
|
-
return value;
|
|
326
|
-
}
|
|
327
|
-
if (url?.includes("/download-info")) {
|
|
328
|
-
const candidate = obj.result ?? obj.data ?? obj.response ?? obj;
|
|
329
|
-
if (candidate && typeof candidate === "object") {
|
|
330
|
-
if (Array.isArray(candidate)) {
|
|
331
|
-
return {
|
|
332
|
-
...obj,
|
|
333
|
-
downloadInfo: candidate,
|
|
334
|
-
};
|
|
335
|
-
}
|
|
336
|
-
return {
|
|
337
|
-
...obj,
|
|
338
|
-
...candidate,
|
|
339
|
-
downloadInfo: candidate.downloadInfo ?? candidate,
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
const wrapper = obj.result ?? obj.data ?? obj.response;
|
|
344
|
-
if (wrapper && typeof wrapper === "object" && !Array.isArray(wrapper)) {
|
|
345
|
-
return {
|
|
346
|
-
...obj,
|
|
347
|
-
...wrapper,
|
|
348
|
-
};
|
|
349
|
-
}
|
|
350
|
-
return value;
|
|
351
|
-
}
|
|
352
|
-
escapeXml(value) {
|
|
353
|
-
return value
|
|
354
|
-
.replace(/&/g, "&")
|
|
355
|
-
.replace(/</g, "<")
|
|
356
|
-
.replace(/>/g, ">")
|
|
357
|
-
.replace(/"/g, """)
|
|
358
|
-
.replace(/'/g, "'");
|
|
359
|
-
}
|
|
360
|
-
toBuffer(input) {
|
|
361
|
-
if (Buffer.isBuffer(input))
|
|
362
|
-
return input;
|
|
363
|
-
if (typeof input === "string")
|
|
364
|
-
return Buffer.from(input, "utf-8");
|
|
365
|
-
return Buffer.from(JSON.stringify(input), "utf-8");
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
exports.ResponseConverter = ResponseConverter;
|
|
369
|
-
//# sourceMappingURL=ResponseConverter.js.map
|