@wiajs/request 3.0.16 → 3.0.18
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/request.cjs +1 -1
- package/dist/request.mjs +1 -1
- package/lib/index.js +1 -4
- package/lib/request.js +70 -44
- package/package.json +2 -2
package/dist/request.cjs
CHANGED
package/dist/request.mjs
CHANGED
package/lib/index.js
CHANGED
|
@@ -71,11 +71,8 @@ const log = Log({
|
|
|
71
71
|
*/ function request(uri, options, callback) {
|
|
72
72
|
let R = null;
|
|
73
73
|
try {
|
|
74
|
-
log({
|
|
75
|
-
uri,
|
|
76
|
-
options
|
|
77
|
-
}, 'request');
|
|
78
74
|
const { opts, cb } = init(uri, options, callback);
|
|
75
|
+
// log({uri, options, opts}, 'request')
|
|
79
76
|
R = new Request(opts, cb);
|
|
80
77
|
} catch (e) {
|
|
81
78
|
log.err(e, 'request');
|
package/lib/request.js
CHANGED
|
@@ -30,10 +30,39 @@ const brotliOptions = {
|
|
|
30
30
|
finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH
|
|
31
31
|
};
|
|
32
32
|
const isBrotliSupported = utils.isFunction(zlib.createBrotliDecompress);
|
|
33
|
+
// clientRequest 属性转发
|
|
34
|
+
const writeProps = [
|
|
35
|
+
'protocol',
|
|
36
|
+
'method',
|
|
37
|
+
'path',
|
|
38
|
+
'host',
|
|
39
|
+
'reusedSocket',
|
|
40
|
+
'socket',
|
|
41
|
+
'closed',
|
|
42
|
+
'destroyed',
|
|
43
|
+
'writable',
|
|
44
|
+
'writableAborted',
|
|
45
|
+
'writableEnded',
|
|
46
|
+
'writableCorked',
|
|
47
|
+
'errored',
|
|
48
|
+
'writableFinished',
|
|
49
|
+
'writableHighWaterMark',
|
|
50
|
+
'writableLength',
|
|
51
|
+
'writableNeedDrain',
|
|
52
|
+
'writableObjectMode'
|
|
53
|
+
];
|
|
54
|
+
// clientReq 方法转发
|
|
55
|
+
const writeMethods = [
|
|
56
|
+
'cork',
|
|
57
|
+
'flushHeaders',
|
|
58
|
+
'setNoDelay',
|
|
59
|
+
'setSocketKeepAlive'
|
|
60
|
+
];
|
|
33
61
|
// Create handlers that pass events from native requests
|
|
62
|
+
// 在 clientRequest 事件转发
|
|
34
63
|
const writeEvents = [
|
|
35
|
-
'abort',
|
|
36
|
-
'aborted',
|
|
64
|
+
// 'abort', // 弃用
|
|
65
|
+
// 'aborted', // 弃用
|
|
37
66
|
'close',
|
|
38
67
|
'connect',
|
|
39
68
|
'continue',
|
|
@@ -58,7 +87,7 @@ for (const ev of writeEvents)writeEventEmit[ev] = function(...args) {
|
|
|
58
87
|
m.redirectReq.emit(ev, ...args) // req 事情映射到 redirectReq 上触发
|
|
59
88
|
;
|
|
60
89
|
};
|
|
61
|
-
// stream.Readable
|
|
90
|
+
// stream.Readable,在响应流上转发读流取事件
|
|
62
91
|
// data 单独处理
|
|
63
92
|
const readEvents = [
|
|
64
93
|
'close',
|
|
@@ -96,7 +125,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
96
125
|
, /** @type {stream.Writable[]} */ this.pipedests = [] // pipe dest
|
|
97
126
|
, /** @type {*} */ this.startTimer = null;
|
|
98
127
|
const m = this;
|
|
99
|
-
// log({
|
|
128
|
+
// log({opts}, 'constructor');
|
|
100
129
|
// Initialize the request
|
|
101
130
|
m.sanitizeOptions(opts);
|
|
102
131
|
m.opt = opts;
|
|
@@ -121,8 +150,34 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
121
150
|
}));
|
|
122
151
|
}
|
|
123
152
|
};
|
|
124
|
-
//
|
|
153
|
+
// Proxy all other public ClientRequest methods 'getHeader'
|
|
154
|
+
for (const method of writeMethods){
|
|
155
|
+
// @ts-ignore
|
|
156
|
+
m[method] = (a, b)=>{
|
|
157
|
+
log(method, {
|
|
158
|
+
a,
|
|
159
|
+
b
|
|
160
|
+
});
|
|
161
|
+
// @ts-ignore
|
|
162
|
+
m._currentRequest?.[method](a, b);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
// Proxy all public ClientRequest properties
|
|
166
|
+
// 'aborted', 'connection' 弃用
|
|
167
|
+
for (const property of writeProps){
|
|
168
|
+
Object.defineProperty(m, property, {
|
|
169
|
+
get () {
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
const val = m._currentRequest?.[property];
|
|
172
|
+
log('get property', {
|
|
173
|
+
property
|
|
174
|
+
});
|
|
175
|
+
return val;
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
125
179
|
if (opts.stream) {
|
|
180
|
+
// 流模式
|
|
126
181
|
// 被 pipe 作为目标时触发,拷贝 src headers
|
|
127
182
|
m.on('pipe', /** @type {stream.Readable} */ (src)=>{
|
|
128
183
|
// m.ntick &&
|
|
@@ -147,7 +202,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
147
202
|
});
|
|
148
203
|
}
|
|
149
204
|
// Perform the first request
|
|
150
|
-
// m.request(); // 写入数据时执行,否则 pipe时无法写入header
|
|
205
|
+
// m.request(); // 写入数据时执行,否则 pipe 时无法写入header
|
|
151
206
|
}
|
|
152
207
|
/**
|
|
153
208
|
* Executes the next native request (initial or redirect)
|
|
@@ -192,36 +247,6 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
192
247
|
const req = httpModule.request(opt, m._onResponse);
|
|
193
248
|
m._currentRequest = req;
|
|
194
249
|
req.redirectReq = m;
|
|
195
|
-
// Proxy all other public ClientRequest methods
|
|
196
|
-
for (const method of [
|
|
197
|
-
'flushHeaders',
|
|
198
|
-
'setNoDelay',
|
|
199
|
-
'setSocketKeepAlive'
|
|
200
|
-
]){
|
|
201
|
-
m[method] = (a, b)=>{
|
|
202
|
-
log(method, {
|
|
203
|
-
a,
|
|
204
|
-
b
|
|
205
|
-
});
|
|
206
|
-
m._currentRequest[method](a, b);
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
// Proxy all public ClientRequest properties
|
|
210
|
-
for (const property of [
|
|
211
|
-
'aborted',
|
|
212
|
-
'connection',
|
|
213
|
-
'socket'
|
|
214
|
-
]){
|
|
215
|
-
Object.defineProperty(m, property, {
|
|
216
|
-
get () {
|
|
217
|
-
const val = m._currentRequest[property];
|
|
218
|
-
log('get property', {
|
|
219
|
-
property
|
|
220
|
-
});
|
|
221
|
-
return val;
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
250
|
// 启动 startTimer
|
|
226
251
|
if (m.startTimer) m._currentRequest.once('socket', m.startTimer);
|
|
227
252
|
// 接收req事件,转发 到 redirectReq 发射
|
|
@@ -268,8 +293,8 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
268
293
|
*/ destroy(error) {
|
|
269
294
|
const m = this;
|
|
270
295
|
if (!m._ended) m.end();
|
|
271
|
-
|
|
272
|
-
|
|
296
|
+
if (m.response) m.response.destroy();
|
|
297
|
+
if (m.responseStream) m.responseStream.destroy();
|
|
273
298
|
// m.clearTimeout();
|
|
274
299
|
destroyRequest(m._currentRequest, error);
|
|
275
300
|
super.destroy(error);
|
|
@@ -284,11 +309,11 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
284
309
|
* @returns {boolean}
|
|
285
310
|
*/ write(chunk, encoding, cb) {
|
|
286
311
|
const m = this;
|
|
287
|
-
log
|
|
312
|
+
log({
|
|
288
313
|
data: chunk,
|
|
289
314
|
encoding,
|
|
290
315
|
callback: cb
|
|
291
|
-
});
|
|
316
|
+
}, 'write');
|
|
292
317
|
// Writing is not allowed if end has been called
|
|
293
318
|
if (m._ending) throw new WriteAfterEndError();
|
|
294
319
|
// ! 数据写入时连接,pipe 时可设置 header
|
|
@@ -474,6 +499,7 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
474
499
|
* @returns
|
|
475
500
|
*/ processResponse(response) {
|
|
476
501
|
const m = this;
|
|
502
|
+
const { opt } = m;
|
|
477
503
|
// Store the redirected response
|
|
478
504
|
const { statusCode } = response;
|
|
479
505
|
if (m.opt.trackRedirects) {
|
|
@@ -491,20 +517,20 @@ const WriteAfterEndError = utils.createErrorType('ERR_STREAM_WRITE_AFTER_END', '
|
|
|
491
517
|
// even if the specific status code is not understood.
|
|
492
518
|
// If the response is not a redirect; return it as-is
|
|
493
519
|
const { location } = response.headers;
|
|
494
|
-
log(
|
|
520
|
+
log({
|
|
495
521
|
statusCode,
|
|
496
522
|
headers: response.headers
|
|
497
|
-
});
|
|
523
|
+
}, 'processResponse');
|
|
498
524
|
if (!location || m.opt.followRedirects === false || statusCode < 300 || statusCode >= 400) {
|
|
499
525
|
// 非重定向,返回给原始回调处理
|
|
500
526
|
response.responseUrl = m._currentUrl;
|
|
501
527
|
response.redirects = m._redirects;
|
|
502
|
-
m.response = response;
|
|
528
|
+
if (opt.stream) m.response = response;
|
|
503
529
|
// Be a good stream and emit end when the response is finished.
|
|
504
530
|
// Hack to emit end on close because of a core bug that never fires end
|
|
505
531
|
response.on('close', ()=>{
|
|
506
532
|
if (!m._respended) {
|
|
507
|
-
|
|
533
|
+
response.emit('end');
|
|
508
534
|
}
|
|
509
535
|
});
|
|
510
536
|
response.once('end', ()=>{
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@wiajs/request",
|
|
3
3
|
"description": "Stream HTTP request client.",
|
|
4
4
|
"keywords": ["http", "simple", "util", "utility"],
|
|
5
|
-
"version": "3.0.
|
|
5
|
+
"version": "3.0.18",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"exports": {
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"node": ">= 6"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@wiajs/log": "^4.3.
|
|
48
|
+
"@wiajs/log": "^4.3.13",
|
|
49
49
|
"mime-types": "^2.1.35"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|