@wiajs/request 3.0.0 → 3.0.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/CHANGELOG.md +717 -0
- package/dist/request.cjs +1476 -0
- package/dist/request.mjs +1474 -0
- package/package.json +2 -3
- package/request.js +0 -1553
- package/src/ZlibTransform.js +0 -27
- package/src/caseless.js +0 -118
- package/src/index.js +0 -122
- package/src/request.js +0 -967
- package/src/utils.js +0 -274
package/src/ZlibTransform.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import stream from 'node:stream'
|
|
2
|
-
|
|
3
|
-
class ZlibTransform extends stream.Transform {
|
|
4
|
-
__transform(chunk, encoding, callback) {
|
|
5
|
-
this.push(chunk)
|
|
6
|
-
callback()
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
_transform(chunk, encoding, callback) {
|
|
10
|
-
if (chunk.length !== 0) {
|
|
11
|
-
this._transform = this.__transform
|
|
12
|
-
|
|
13
|
-
// Add Default Compression headers if no zlib headers are present
|
|
14
|
-
if (chunk[0] !== 120) {
|
|
15
|
-
// Hex: 78
|
|
16
|
-
const header = Buffer.alloc(2)
|
|
17
|
-
header[0] = 120 // Hex: 78
|
|
18
|
-
header[1] = 156 // Hex: 9C
|
|
19
|
-
this.push(header, encoding)
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
this.__transform(chunk, encoding, callback)
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export default ZlibTransform
|
package/src/caseless.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
export default class Caseless {
|
|
2
|
-
/**
|
|
3
|
-
* @param {*} dict
|
|
4
|
-
*/
|
|
5
|
-
constructor(dict) {
|
|
6
|
-
this.dict = dict || {}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* @param {*} name
|
|
12
|
-
* @param {*} value
|
|
13
|
-
* @param {*} clobber
|
|
14
|
-
* @returns
|
|
15
|
-
*/
|
|
16
|
-
set(name, value, clobber) {
|
|
17
|
-
if (typeof name === 'object') {
|
|
18
|
-
for (const n of name) {
|
|
19
|
-
this.set(n, name[n], value)
|
|
20
|
-
}
|
|
21
|
-
} else {
|
|
22
|
-
if (typeof clobber === 'undefined') clobber = true
|
|
23
|
-
const has = this.has(name)
|
|
24
|
-
|
|
25
|
-
if (!clobber && has) this.dict[has] = this.dict[has] + ',' + value
|
|
26
|
-
else this.dict[has || name] = value
|
|
27
|
-
return has
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
* @param {string} name
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
|
-
has(name) {
|
|
37
|
-
const keys = Object.keys(this.dict)
|
|
38
|
-
name = name.toLowerCase()
|
|
39
|
-
for (let i = 0; i < keys.length; i++) {
|
|
40
|
-
if (keys[i].toLowerCase() === name) return keys[i]
|
|
41
|
-
}
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* @param {string} name
|
|
48
|
-
* @returns
|
|
49
|
-
*/
|
|
50
|
-
get(name) {
|
|
51
|
-
name = name.toLowerCase()
|
|
52
|
-
let result
|
|
53
|
-
let _key
|
|
54
|
-
const headers = this.dict
|
|
55
|
-
for (const key of Object.keys(headers)) {
|
|
56
|
-
_key = key.toLowerCase()
|
|
57
|
-
if (name === _key) result = headers[key]
|
|
58
|
-
}
|
|
59
|
-
return result
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
*
|
|
64
|
-
* @param {string} name
|
|
65
|
-
* @returns
|
|
66
|
-
*/
|
|
67
|
-
swap(name) {
|
|
68
|
-
const has = this.has(name)
|
|
69
|
-
if (has === name) return
|
|
70
|
-
if (!has) throw new Error('There is no header than matches "' + name + '"')
|
|
71
|
-
this.dict[name] = this.dict[has]
|
|
72
|
-
delete this.dict[has]
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
del(name) {
|
|
76
|
-
name = String(name).toLowerCase()
|
|
77
|
-
let deleted = false
|
|
78
|
-
let changed = 0
|
|
79
|
-
const dict = this.dict
|
|
80
|
-
for (const key of Object.keys(this.dict)) {
|
|
81
|
-
if (name === String(key).toLowerCase()) {
|
|
82
|
-
deleted = delete dict[key]
|
|
83
|
-
changed += 1
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return changed === 0 ? true : deleted
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
*
|
|
92
|
-
* @param {*} resp
|
|
93
|
-
* @param {*} headers
|
|
94
|
-
* @returns
|
|
95
|
-
*/
|
|
96
|
-
export function httpify(resp, headers) {
|
|
97
|
-
const c = new Caseless(headers)
|
|
98
|
-
|
|
99
|
-
resp.setHeader = (key, value, clobber) => {
|
|
100
|
-
if (typeof value === 'undefined') return
|
|
101
|
-
return c.set(key, value, clobber)
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
resp.hasHeader = key => {
|
|
105
|
-
return c.has(key)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
resp.getHeader = key => {
|
|
109
|
-
return c.get(key)
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
resp.removeHeader = key => {
|
|
113
|
-
return c.del(key)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
resp.headers = c.dict
|
|
117
|
-
return c
|
|
118
|
-
}
|
package/src/index.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* from 'https://github.com/follow-redirects/follow-redirects'
|
|
3
|
-
* 修改以支持http、https 代理服务器
|
|
4
|
-
* 代理模式下,http or https 请求,取决于 proxy 代理服务器,而不是目的服务器。
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import {log as Log, name} from '@wiajs/log'
|
|
8
|
-
import Request from './request.js'
|
|
9
|
-
import utils from './utils.js'
|
|
10
|
-
|
|
11
|
-
const log = Log({env: `wia:req:${name(__filename)}`})
|
|
12
|
-
|
|
13
|
-
// Preventive platform detection
|
|
14
|
-
// istanbul ignore next
|
|
15
|
-
;(function detectUnsupportedEnvironment() {
|
|
16
|
-
const looksLikeNode = typeof process !== 'undefined'
|
|
17
|
-
const looksLikeBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'
|
|
18
|
-
const looksLikeV8 = utils.isFunction(Error.captureStackTrace)
|
|
19
|
-
if (!looksLikeNode && (looksLikeBrowser || !looksLikeV8)) {
|
|
20
|
-
log.warn('The follow-redirects package should be excluded from browser builds.')
|
|
21
|
-
}
|
|
22
|
-
})()
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* 封装http(s),实现重定向
|
|
26
|
-
* 重定向可能切换http、https
|
|
27
|
-
* 支持隧道及非隧道、http(s)代理
|
|
28
|
-
*/
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 初始化参数
|
|
32
|
-
* @param {*} uri
|
|
33
|
-
* @param {*} options
|
|
34
|
-
* @param {*} callback
|
|
35
|
-
* @returns
|
|
36
|
-
*/
|
|
37
|
-
function init(uri, options, callback) {
|
|
38
|
-
try {
|
|
39
|
-
// Parse parameters, ensuring that input is an object
|
|
40
|
-
if (utils.isURL(uri)) uri = utils.spreadUrlObject(uri)
|
|
41
|
-
else if (utils.isString(uri)) uri = utils.spreadUrlObject(utils.parseUrl(uri))
|
|
42
|
-
else {
|
|
43
|
-
callback = options
|
|
44
|
-
options = utils.validateUrl(uri)
|
|
45
|
-
uri = {}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (utils.isFunction(options)) {
|
|
49
|
-
callback = options
|
|
50
|
-
options = null
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// copy options
|
|
54
|
-
options = {
|
|
55
|
-
...uri,
|
|
56
|
-
...options,
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!utils.isString(options.host) && !utils.isString(options.hostname)) options.hostname = '::1'
|
|
60
|
-
|
|
61
|
-
R = {opts: options, cb: callback}
|
|
62
|
-
} catch (e) {
|
|
63
|
-
log.err(e, 'init')
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return R
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Executes a request, following redirects
|
|
71
|
-
* 替换原 http(s).request,参数类似
|
|
72
|
-
* 注意变参 (options[, callback]) or (url[, options][, callback])
|
|
73
|
-
maxRedirects: _.maxRedirects,
|
|
74
|
-
maxBodyLength: _.maxBodyLength,
|
|
75
|
-
* @param {*} uri/options
|
|
76
|
-
* @param {*} options/callback
|
|
77
|
-
* @param {*} callback/null
|
|
78
|
-
* @returns
|
|
79
|
-
*/
|
|
80
|
-
function request(uri, options, callback) {
|
|
81
|
-
let R = null
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
const {opts, cb} = init(uri, options, callback)
|
|
85
|
-
// log.debug('request', {options})
|
|
86
|
-
R = new Request(opts, cb)
|
|
87
|
-
} catch (e) {
|
|
88
|
-
log.err(e, 'request')
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return R
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 执行简单的非stream数据请求
|
|
96
|
-
* 复杂数据,请使用 @wiajs/req库(fork from axios),该库封装了当前库,提供了更多功能
|
|
97
|
-
* organize params for patch, post, put, head, del
|
|
98
|
-
* @param {string} verb
|
|
99
|
-
* @returns {Request} Duplex 流
|
|
100
|
-
*/
|
|
101
|
-
function fn(verb) {
|
|
102
|
-
const method = verb.toUpperCase()
|
|
103
|
-
return (uri, options, callback) => {
|
|
104
|
-
const {opts, cb} = init(uri, options, callback)
|
|
105
|
-
opts.method = method
|
|
106
|
-
const req = new Request(opts, cb)
|
|
107
|
-
req.end()
|
|
108
|
-
return req
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// define like this to please codeintel/intellisense IDEs
|
|
113
|
-
request.get = fn('get')
|
|
114
|
-
request.head = fn('head')
|
|
115
|
-
request.options = fn('options')
|
|
116
|
-
request.post = fn('post')
|
|
117
|
-
request.put = fn('put')
|
|
118
|
-
request.patch = fn('patch')
|
|
119
|
-
request.del = fn('delete')
|
|
120
|
-
request['delete'] = fn('delete')
|
|
121
|
-
|
|
122
|
-
export default request
|