xjs-node 1.0.7 → 2.0.1-alpha.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/{dist → build}/func/u-file.d.ts +44 -45
- package/{dist → build}/func/u-file.js +171 -161
- package/{dist → build}/func/u.d.ts +3 -3
- package/{dist → build}/func/u.js +51 -42
- package/{dist → build}/index.d.ts +5 -5
- package/{dist → build}/index.js +23 -23
- package/{dist → build}/prcs/http/http-resolver-context.d.ts +59 -60
- package/{dist → build}/prcs/http/http-resolver-context.js +337 -327
- package/{dist → build}/prcs/http/http-resolver.d.ts +50 -51
- package/{dist → build}/prcs/http/http-resolver.js +53 -53
- package/{dist → build}/prcs/http/i-http-client.d.ts +88 -90
- package/{dist → build}/prcs/http/i-http-client.js +2 -2
- package/package.json +18 -11
|
@@ -1,327 +1,337 @@
|
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
ch
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
params
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
.
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
this.
|
|
325
|
-
|
|
326
|
-
}
|
|
327
|
-
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.HttpResolverContext = exports.s_clientMode = void 0;
|
|
37
|
+
const tls = __importStar(require("tls"));
|
|
38
|
+
const zlib = __importStar(require("zlib"));
|
|
39
|
+
const fs = __importStar(require("fs"));
|
|
40
|
+
const path = __importStar(require("path"));
|
|
41
|
+
const url_1 = require("url");
|
|
42
|
+
const https_1 = require("https");
|
|
43
|
+
const http_1 = require("http");
|
|
44
|
+
const async_hooks_1 = require("async_hooks");
|
|
45
|
+
const u_file_1 = require("../../func/u-file");
|
|
46
|
+
const u_1 = require("../../func/u");
|
|
47
|
+
const xjs_common_1 = require("xjs-common");
|
|
48
|
+
const stream_1 = require("stream");
|
|
49
|
+
exports.s_clientMode = {
|
|
50
|
+
nodejs: { id: 0, cipherOrder: null },
|
|
51
|
+
chrome: { id: 1, cipherOrder: [2, 0, 1] },
|
|
52
|
+
firefox: { id: 2, cipherOrder: [2, 1, 0] }
|
|
53
|
+
};
|
|
54
|
+
const s_errCode = 1200;
|
|
55
|
+
const s_redirectLimit = 5;
|
|
56
|
+
const s_mode2headers = new Map([
|
|
57
|
+
[exports.s_clientMode.firefox, (cmv) => ({
|
|
58
|
+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
|
59
|
+
"accept-encoding": "gzip, deflate, br",
|
|
60
|
+
"accept-language": "en-US,en;q=0.5",
|
|
61
|
+
"sec-fetch-dest": "document",
|
|
62
|
+
"sec-fetch-mode": "navigate",
|
|
63
|
+
"sec-fetch-site": "none",
|
|
64
|
+
"sec-fetch-user": "?1",
|
|
65
|
+
"upgrade-insecure-requests": "1",
|
|
66
|
+
"user-agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${cmv}.0) Gecko/20100101 Firefox/${cmv}.0`
|
|
67
|
+
})],
|
|
68
|
+
[exports.s_clientMode.chrome, (cmv) => {
|
|
69
|
+
const uad = cmv < 130
|
|
70
|
+
? `"Not/A)Brand";v="8", "Chromium";v="${cmv}", "Google Chrome";v="${cmv}"`
|
|
71
|
+
: `"Chromium";v="${cmv}", "Not:A-Brand";v="24", "Google Chrome";v="${cmv}"`;
|
|
72
|
+
const ch = {
|
|
73
|
+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
74
|
+
"accept-encoding": "gzip, deflate, br, zstd",
|
|
75
|
+
"accept-language": "en-US,en;q=0.9",
|
|
76
|
+
"sec-ch-ua": uad,
|
|
77
|
+
"sec-ch-ua-mobile": "?0",
|
|
78
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
79
|
+
"sec-fetch-dest": "document",
|
|
80
|
+
"sec-fetch-mode": "navigate",
|
|
81
|
+
"sec-fetch-site": "none",
|
|
82
|
+
"sec-fetch-user": "?1",
|
|
83
|
+
"upgrade-insecure-requests": "1",
|
|
84
|
+
"user-agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${cmv}.0.0.0 Safari/537.36`
|
|
85
|
+
};
|
|
86
|
+
if (cmv >= 124)
|
|
87
|
+
ch["priority"] = "u=0, i";
|
|
88
|
+
return ch;
|
|
89
|
+
}]
|
|
90
|
+
]);
|
|
91
|
+
class HttpResolverContext {
|
|
92
|
+
cmv;
|
|
93
|
+
_l;
|
|
94
|
+
_als = new async_hooks_1.AsyncLocalStorage();
|
|
95
|
+
_mode;
|
|
96
|
+
_ciphers;
|
|
97
|
+
_proxyConfig;
|
|
98
|
+
_chHeaders;
|
|
99
|
+
_cookies;
|
|
100
|
+
get clientMode() { return Object.keys(exports.s_clientMode).find((_, i) => i === this._mode.id); }
|
|
101
|
+
constructor(cmv, op, _l = console) {
|
|
102
|
+
this.cmv = cmv;
|
|
103
|
+
this._l = _l;
|
|
104
|
+
this._mode = op?.mode ?? xjs_common_1.UArray.randomPick([exports.s_clientMode.chrome, exports.s_clientMode.firefox]);
|
|
105
|
+
this._proxyConfig = op?.proxy;
|
|
106
|
+
if (this._mode.id > 0) {
|
|
107
|
+
this._ciphers = this.createCiphers(this._mode);
|
|
108
|
+
this._chHeaders = s_mode2headers.get(this._mode)(this.cmv);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async get(url, op) {
|
|
112
|
+
const u = new url_1.URL(url);
|
|
113
|
+
const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
|
|
114
|
+
const rc = { redirectCount: op?.outerRedirectCount ?? 0, proxyAgent };
|
|
115
|
+
Object.assign(rc, op);
|
|
116
|
+
return await this._als.run(rc, this.getIn, u).finally(() => proxyAgent?.destroy());
|
|
117
|
+
}
|
|
118
|
+
async post(url, payload, op) {
|
|
119
|
+
const u = new url_1.URL(url);
|
|
120
|
+
const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
|
|
121
|
+
const rc = { redirectCount: 0, proxyAgent };
|
|
122
|
+
Object.assign(rc, op);
|
|
123
|
+
return await this._als.run(rc, this.postIn, u, payload).finally(() => proxyAgent?.destroy());
|
|
124
|
+
}
|
|
125
|
+
createProxyAgent(u) {
|
|
126
|
+
const conf = this._proxyConfig;
|
|
127
|
+
return new Promise((resolve, reject) => {
|
|
128
|
+
const headers = {};
|
|
129
|
+
if (conf.auth)
|
|
130
|
+
headers['proxy-authorization'] = `Basic ${Buffer.from(conf.auth.name + ':' + conf.auth.pass).toString('base64')}`;
|
|
131
|
+
const req = (0, http_1.request)({
|
|
132
|
+
host: conf.server,
|
|
133
|
+
port: conf.port,
|
|
134
|
+
method: xjs_common_1.HttpMethod.Connect,
|
|
135
|
+
path: `${u.hostname}:443`,
|
|
136
|
+
headers
|
|
137
|
+
}).on('connect', (res, socket) => {
|
|
138
|
+
if (res.statusCode === 200)
|
|
139
|
+
resolve(new https_1.Agent({ socket, keepAlive: true }));
|
|
140
|
+
else
|
|
141
|
+
reject(new xjs_common_1.XjsErr(s_errCode, "Could not connect to proxy."));
|
|
142
|
+
});
|
|
143
|
+
req.on('error', reject);
|
|
144
|
+
req.on('timeout', () => {
|
|
145
|
+
req.destroy();
|
|
146
|
+
reject(new xjs_common_1.XjsErr(s_errCode, "The http request timeout, maybe server did not respond."));
|
|
147
|
+
});
|
|
148
|
+
req.end();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
getIn = async (u) => {
|
|
152
|
+
const params = {};
|
|
153
|
+
const rc = this._als.getStore();
|
|
154
|
+
params.method = xjs_common_1.HttpMethod.Get;
|
|
155
|
+
params.headers = xjs_common_1.UHttp.normalizeHeaders(rc.headers);
|
|
156
|
+
return await this.reqHttps(u, params);
|
|
157
|
+
};
|
|
158
|
+
postIn = async (u, payload) => {
|
|
159
|
+
const params = {};
|
|
160
|
+
const rc = this._als.getStore();
|
|
161
|
+
params.method = xjs_common_1.HttpMethod.Post;
|
|
162
|
+
params.headers = xjs_common_1.UHttp.normalizeHeaders(rc.headers);
|
|
163
|
+
let p = payload;
|
|
164
|
+
if (p instanceof stream_1.Stream) {
|
|
165
|
+
params.headers["content-type"] ??= "application/octet-stream";
|
|
166
|
+
}
|
|
167
|
+
else if (xjs_common_1.UType.isObject(payload)) {
|
|
168
|
+
p = JSON.stringify(payload);
|
|
169
|
+
params.headers["content-length"] = p.length;
|
|
170
|
+
params.headers["content-type"] = "application/json";
|
|
171
|
+
}
|
|
172
|
+
return await this.reqHttps(u, params, p);
|
|
173
|
+
};
|
|
174
|
+
reqHttps(u, params, payload) {
|
|
175
|
+
const rc = this._als.getStore();
|
|
176
|
+
params.timeout = rc.timeout ?? 0;
|
|
177
|
+
params.protocol = u.protocol;
|
|
178
|
+
params.host = u.host;
|
|
179
|
+
params.path = (rc.ignoreQuery || !u.search) ? u.pathname : `${u.pathname}${u.search}`;
|
|
180
|
+
params.agent = rc.proxyAgent;
|
|
181
|
+
if (this._mode.id > 0) {
|
|
182
|
+
params.ciphers = this._ciphers;
|
|
183
|
+
params.headers = params.headers ? Object.assign(params.headers, this._chHeaders) : this._chHeaders;
|
|
184
|
+
}
|
|
185
|
+
if (this._cookies)
|
|
186
|
+
this.setCookies(params.headers);
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
const req = (0, https_1.request)(params, (res) => this.processResponse(resolve, reject, rc, params.host, res));
|
|
189
|
+
req.on('error', reject);
|
|
190
|
+
req.on('timeout', () => {
|
|
191
|
+
req.destroy();
|
|
192
|
+
reject(new xjs_common_1.XjsErr(s_errCode, "The http request timeout, maybe server did not respond."));
|
|
193
|
+
});
|
|
194
|
+
if (payload instanceof stream_1.Stream)
|
|
195
|
+
payload.pipe(req, { end: true });
|
|
196
|
+
else {
|
|
197
|
+
if (payload)
|
|
198
|
+
req.write(payload);
|
|
199
|
+
req.end();
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
processResponse(resolve, reject, rc, host, res) {
|
|
204
|
+
if (res.headers["set-cookie"])
|
|
205
|
+
this.storeCookies(res.headers["set-cookie"]);
|
|
206
|
+
const sc = xjs_common_1.UHttp.statusCategoryOf(res.statusCode);
|
|
207
|
+
if (sc === 3) {
|
|
208
|
+
this.handleRedirect(res, host).then(resolve).catch(reject).finally(() => res.destroy());
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (res.headers["content-disposition"]?.trim().startsWith("attachment")) {
|
|
212
|
+
try {
|
|
213
|
+
const dest = this.resolveDownloadPath(rc.downloadPath, res.headers["content-disposition"]);
|
|
214
|
+
const stream = fs.createWriteStream(dest);
|
|
215
|
+
res.pipe(stream);
|
|
216
|
+
stream.on("finish", () => stream.close());
|
|
217
|
+
stream.on("close", () => resolve({ headers: res.headers }));
|
|
218
|
+
stream.on("error", reject);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
if (e instanceof xjs_common_1.XjsErr)
|
|
223
|
+
reject(e);
|
|
224
|
+
else {
|
|
225
|
+
this.error(e);
|
|
226
|
+
reject(new xjs_common_1.XjsErr(s_errCode, "Failed to download a file."));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const bfs = [];
|
|
231
|
+
const contentEncofing = res.headers["content-encoding"]?.toLocaleLowerCase();
|
|
232
|
+
res.on('data', chunk => bfs.push(chunk));
|
|
233
|
+
res.on('end', () => {
|
|
234
|
+
try {
|
|
235
|
+
let retBuf = Buffer.concat(bfs);
|
|
236
|
+
if (contentEncofing == "gzip")
|
|
237
|
+
retBuf = zlib.gunzipSync(retBuf);
|
|
238
|
+
else if (contentEncofing == "br")
|
|
239
|
+
retBuf = zlib.brotliDecompressSync(retBuf);
|
|
240
|
+
const data = rc.responseType === "buffer" ? retBuf : retBuf.toString("utf8");
|
|
241
|
+
if (sc !== 2) {
|
|
242
|
+
if (xjs_common_1.UType.isString(data) && data.trim())
|
|
243
|
+
this.warn(data);
|
|
244
|
+
reject(new xjs_common_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
|
|
245
|
+
}
|
|
246
|
+
else
|
|
247
|
+
resolve({ payload: data, headers: res.headers });
|
|
248
|
+
}
|
|
249
|
+
catch (e) {
|
|
250
|
+
reject(e);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
resolveDownloadPath(opPath, disposition) {
|
|
255
|
+
const appendFname = (d) => {
|
|
256
|
+
const fname = disposition.split(";")
|
|
257
|
+
.find(f => f.trim().startsWith("filename"))
|
|
258
|
+
?.replace(/^\s+filename\s+=/, "").trim()
|
|
259
|
+
?? u_file_1.UFile.reserveFilePath(d, `xjs-download_${xjs_common_1.UString.simpleTime()}`);
|
|
260
|
+
return (0, u_1.joinPath)(d, fname);
|
|
261
|
+
};
|
|
262
|
+
if (opPath) {
|
|
263
|
+
const st = u_file_1.UFile.status(opPath);
|
|
264
|
+
if (!st || st.isFile()) {
|
|
265
|
+
if (!u_file_1.UFile.exists(path.dirname(opPath)))
|
|
266
|
+
throw new xjs_common_1.XjsErr(s_errCode, "Directory of the download file was not found.");
|
|
267
|
+
return opPath;
|
|
268
|
+
}
|
|
269
|
+
if (st.isDirectory()) {
|
|
270
|
+
if (!u_file_1.UFile.exists(opPath))
|
|
271
|
+
throw new xjs_common_1.XjsErr(s_errCode, "Directory of the download path was not found.");
|
|
272
|
+
return appendFname(opPath);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return appendFname("./");
|
|
276
|
+
}
|
|
277
|
+
async handleRedirect(res, host) {
|
|
278
|
+
const rc = this._als.getStore();
|
|
279
|
+
if (!res.headers.location)
|
|
280
|
+
throw new xjs_common_1.XjsErr(s_errCode, "Received http redirection, but no location header found.");
|
|
281
|
+
if (rc.redirectCount++ > s_redirectLimit)
|
|
282
|
+
throw new xjs_common_1.XjsErr(s_errCode, "Count of http redirection exceeds limit.");
|
|
283
|
+
this.log(`Redirect to ${res.headers.location}. (count is ${rc.redirectCount})`);
|
|
284
|
+
const dest = res.headers.location.startsWith("http") ? res.headers.location : `https://${host}${res.headers.location}`;
|
|
285
|
+
if (rc.outerRedirectCount)
|
|
286
|
+
throw new xjs_common_1.XjsErr(-1, dest);
|
|
287
|
+
const u = new url_1.URL(dest);
|
|
288
|
+
// consider for proxy which implements reverse proxy.
|
|
289
|
+
if (rc.proxyAgent) {
|
|
290
|
+
rc.proxyAgent?.destroy();
|
|
291
|
+
rc.proxyAgent = await this.createProxyAgent(u);
|
|
292
|
+
}
|
|
293
|
+
return await this.getIn(u);
|
|
294
|
+
}
|
|
295
|
+
createCiphers(mode) {
|
|
296
|
+
const defaultCiphers = tls.DEFAULT_CIPHERS.split(':');
|
|
297
|
+
return [
|
|
298
|
+
defaultCiphers[mode.cipherOrder[0]],
|
|
299
|
+
defaultCiphers[mode.cipherOrder[1]],
|
|
300
|
+
defaultCiphers[mode.cipherOrder[2]],
|
|
301
|
+
...xjs_common_1.UArray.shuffle(defaultCiphers.slice(3))
|
|
302
|
+
].join(':');
|
|
303
|
+
}
|
|
304
|
+
setCookies(headers) {
|
|
305
|
+
const exp = this._cookies["expires"];
|
|
306
|
+
if (exp && new Date(exp).getTime() <= Date.now()) {
|
|
307
|
+
this._cookies = null;
|
|
308
|
+
this.log("Cookies was cleared due to an expiraion.");
|
|
309
|
+
}
|
|
310
|
+
else
|
|
311
|
+
headers.cookie = Object.keys(this._cookies)
|
|
312
|
+
.filter(ckk => !["expires", "max-age"].includes(ckk))
|
|
313
|
+
.map(ckk => `${ckk}=${this._cookies[ckk]};`).join(" ");
|
|
314
|
+
}
|
|
315
|
+
storeCookies(cookies) {
|
|
316
|
+
this._cookies ??= {};
|
|
317
|
+
cookies.filter(c => c).flatMap(c => c.split(";"))
|
|
318
|
+
.map(c => {
|
|
319
|
+
const idx = c.indexOf("=");
|
|
320
|
+
return idx !== -1 && [c.substring(0, idx).toLowerCase().trim(), c.substring(idx + 1)];
|
|
321
|
+
})
|
|
322
|
+
.filter(cp => cp && cp[0] && !["secure", "path", "domain", "samesite"].includes(cp[0]))
|
|
323
|
+
.forEach(cp => this._cookies[cp[0]] = cp[1]);
|
|
324
|
+
this.log("Store cookies from set-cookie headers.");
|
|
325
|
+
this.log(JSON.stringify(this._cookies));
|
|
326
|
+
}
|
|
327
|
+
log(msg) {
|
|
328
|
+
this._l.log(`[http-resolver] ${msg}`);
|
|
329
|
+
}
|
|
330
|
+
warn(msg) {
|
|
331
|
+
this._l.warn(`[http-resolver] ${msg}`);
|
|
332
|
+
}
|
|
333
|
+
error(msg) {
|
|
334
|
+
this._l.error(`[http-resolver] ${msg}`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
exports.HttpResolverContext = HttpResolverContext;
|