@wiajs/request 3.0.0
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/LICENSE +19 -0
- package/README.md +1133 -0
- package/index.js +1 -0
- package/lib/ZlibTransform.js +24 -0
- package/lib/caseless.js +100 -0
- package/lib/index.js +104 -0
- package/lib/request.js +828 -0
- package/lib/utils.js +226 -0
- package/package.json +55 -0
- package/request.js +1553 -0
- package/src/ZlibTransform.js +27 -0
- package/src/caseless.js +118 -0
- package/src/index.js +122 -0
- package/src/request.js +967 -0
- package/src/utils.js +274 -0
package/lib/utils.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils for request
|
|
3
|
+
*/ import url from 'node:url';
|
|
4
|
+
import assert from 'node:assert';
|
|
5
|
+
const { URL } = url;
|
|
6
|
+
// Whether to use the native URL object or the legacy url module
|
|
7
|
+
let useNativeURL = false;
|
|
8
|
+
try {
|
|
9
|
+
assert(new URL(''));
|
|
10
|
+
} catch (error) {
|
|
11
|
+
useNativeURL = error.code === 'ERR_INVALID_URL';
|
|
12
|
+
}
|
|
13
|
+
// URL fields to preserve in copy operations
|
|
14
|
+
const preservedUrlFields = [
|
|
15
|
+
'auth',
|
|
16
|
+
'host',
|
|
17
|
+
'hostname',
|
|
18
|
+
'href',
|
|
19
|
+
'path',
|
|
20
|
+
'pathname',
|
|
21
|
+
'port',
|
|
22
|
+
'protocol',
|
|
23
|
+
'query',
|
|
24
|
+
'search',
|
|
25
|
+
'hash'
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param {*} code
|
|
30
|
+
* @param {*} message
|
|
31
|
+
* @param {*} baseClass
|
|
32
|
+
* @returns
|
|
33
|
+
*/ function createErrorType(code, message, baseClass) {
|
|
34
|
+
// Create constructor
|
|
35
|
+
function CustomError(properties) {
|
|
36
|
+
// istanbul ignore else
|
|
37
|
+
if (isFunction(Error.captureStackTrace)) {
|
|
38
|
+
Error.captureStackTrace(this, this.constructor);
|
|
39
|
+
}
|
|
40
|
+
Object.assign(this, properties || {});
|
|
41
|
+
this.code = code;
|
|
42
|
+
this.message = this.cause ? `${message}: ${this.cause.message}` : message;
|
|
43
|
+
}
|
|
44
|
+
// Attach constructor and set default properties
|
|
45
|
+
CustomError.prototype = new (baseClass || Error)();
|
|
46
|
+
Object.defineProperties(CustomError.prototype, {
|
|
47
|
+
constructor: {
|
|
48
|
+
value: CustomError,
|
|
49
|
+
enumerable: false
|
|
50
|
+
},
|
|
51
|
+
name: {
|
|
52
|
+
value: `Error [${code}]`,
|
|
53
|
+
enumerable: false
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return CustomError;
|
|
57
|
+
}
|
|
58
|
+
const InvalidUrlError = createErrorType('ERR_INVALID_URL', 'Invalid URL', TypeError);
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
const typeOfTest = (type)=>(thing)=>typeof thing === type;
|
|
61
|
+
/**
|
|
62
|
+
* Determine if a value is a String
|
|
63
|
+
*
|
|
64
|
+
* @param {*} val The value to test
|
|
65
|
+
*
|
|
66
|
+
* @returns {boolean} True if value is a String, otherwise false
|
|
67
|
+
*/ const isString = typeOfTest('string');
|
|
68
|
+
/**
|
|
69
|
+
* Determine if a value is an Array
|
|
70
|
+
*
|
|
71
|
+
* @param {Object} val The value to test
|
|
72
|
+
*
|
|
73
|
+
* @returns {boolean} True if value is an Array, otherwise false
|
|
74
|
+
*/ const { isArray } = Array;
|
|
75
|
+
/**
|
|
76
|
+
* Determine if a value is undefined
|
|
77
|
+
*
|
|
78
|
+
* @param {*} val The value to test
|
|
79
|
+
*
|
|
80
|
+
* @returns {boolean} True if the value is undefined, otherwise false
|
|
81
|
+
*/ const isUndefined = typeOfTest('undefined');
|
|
82
|
+
/**
|
|
83
|
+
* Determine if a value is a Buffer
|
|
84
|
+
*
|
|
85
|
+
* @param {*} val The value to test
|
|
86
|
+
*
|
|
87
|
+
* @returns {boolean} True if value is a Buffer, otherwise false
|
|
88
|
+
*/ function isBuffer(val) {
|
|
89
|
+
return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor) && isFunction(val.constructor.isBuffer) && val.constructor.isBuffer(val);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Determine if a value is a Function
|
|
93
|
+
*
|
|
94
|
+
* @param {*} val The value to test
|
|
95
|
+
* @returns {boolean} True if value is a Function, otherwise false
|
|
96
|
+
*/ const isFunction = typeOfTest('function');
|
|
97
|
+
/**
|
|
98
|
+
* Determine if a value is a Number
|
|
99
|
+
*
|
|
100
|
+
* @param {*} val The value to test
|
|
101
|
+
*
|
|
102
|
+
* @returns {boolean} True if value is a Number, otherwise false
|
|
103
|
+
*/ const isNumber = typeOfTest('number');
|
|
104
|
+
/**
|
|
105
|
+
* Determine if a value is an Object
|
|
106
|
+
*
|
|
107
|
+
* @param {*} thing The value to test
|
|
108
|
+
*
|
|
109
|
+
* @returns {boolean} True if value is an Object, otherwise false
|
|
110
|
+
*/ const isObject = (thing)=>thing !== null && typeof thing === 'object';
|
|
111
|
+
/**
|
|
112
|
+
* Determine if a value is a Boolean
|
|
113
|
+
*
|
|
114
|
+
* @param {*} thing The value to test
|
|
115
|
+
* @returns {boolean} True if value is a Boolean, otherwise false
|
|
116
|
+
*/ const isBoolean = (thing)=>thing === true || thing === false;
|
|
117
|
+
const noop = ()=>{};
|
|
118
|
+
/**
|
|
119
|
+
*
|
|
120
|
+
* @param {*} value
|
|
121
|
+
* @returns
|
|
122
|
+
*/ function isURL(value) {
|
|
123
|
+
return URL && value instanceof URL;
|
|
124
|
+
}
|
|
125
|
+
function isReadStream(rs) {
|
|
126
|
+
return rs.readable && rs.path && rs.mode;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
*
|
|
130
|
+
* @param {*} urlObject
|
|
131
|
+
* @param {*} target
|
|
132
|
+
* @returns
|
|
133
|
+
*/ function spreadUrlObject(urlObject, target) {
|
|
134
|
+
const spread = target || {};
|
|
135
|
+
for (const key of preservedUrlFields){
|
|
136
|
+
spread[key] = urlObject[key];
|
|
137
|
+
}
|
|
138
|
+
// Fix IPv6 hostname
|
|
139
|
+
if (spread.hostname.startsWith('[')) {
|
|
140
|
+
spread.hostname = spread.hostname.slice(1, -1);
|
|
141
|
+
}
|
|
142
|
+
// Ensure port is a number
|
|
143
|
+
if (spread.port !== '') {
|
|
144
|
+
spread.port = Number(spread.port);
|
|
145
|
+
}
|
|
146
|
+
// Concatenate path
|
|
147
|
+
spread.path = spread.search ? spread.pathname + spread.search : spread.pathname;
|
|
148
|
+
return spread;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
*
|
|
152
|
+
* @param {*} input
|
|
153
|
+
* @returns
|
|
154
|
+
*/ function parseUrl(input) {
|
|
155
|
+
let parsed;
|
|
156
|
+
// istanbul ignore else
|
|
157
|
+
if (useNativeURL) {
|
|
158
|
+
parsed = new URL(input);
|
|
159
|
+
} else {
|
|
160
|
+
// Ensure the URL is valid and absolute
|
|
161
|
+
parsed = validateUrl(url.parse(input));
|
|
162
|
+
if (!isString(parsed.protocol)) {
|
|
163
|
+
throw new InvalidUrlError({
|
|
164
|
+
input
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return parsed;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
*
|
|
172
|
+
* @param {*} input
|
|
173
|
+
* @returns
|
|
174
|
+
*/ function validateUrl(input) {
|
|
175
|
+
if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
|
|
176
|
+
throw new InvalidUrlError({
|
|
177
|
+
input: input.href || input
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) {
|
|
181
|
+
throw new InvalidUrlError({
|
|
182
|
+
input: input.href || input
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
return input;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
*
|
|
189
|
+
* @param {*} relative
|
|
190
|
+
* @param {*} base
|
|
191
|
+
* @returns
|
|
192
|
+
*/ function resolveUrl(relative, base) {
|
|
193
|
+
// istanbul ignore next
|
|
194
|
+
return useNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative));
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
*
|
|
198
|
+
* @param {string} method
|
|
199
|
+
* @param {number} code
|
|
200
|
+
* @returns
|
|
201
|
+
*/ function noBody(method, code) {
|
|
202
|
+
return method === 'HEAD' || // Informational
|
|
203
|
+
code >= 100 && code < 200 || // No Content
|
|
204
|
+
code === 204 || // Not Modified
|
|
205
|
+
code === 304;
|
|
206
|
+
}
|
|
207
|
+
export default {
|
|
208
|
+
createErrorType,
|
|
209
|
+
InvalidUrlError,
|
|
210
|
+
isString,
|
|
211
|
+
isArray,
|
|
212
|
+
isBuffer,
|
|
213
|
+
isUndefined,
|
|
214
|
+
isNumber,
|
|
215
|
+
isBoolean,
|
|
216
|
+
isFunction,
|
|
217
|
+
isObject,
|
|
218
|
+
isURL,
|
|
219
|
+
isReadStream,
|
|
220
|
+
noop,
|
|
221
|
+
parseUrl,
|
|
222
|
+
spreadUrlObject,
|
|
223
|
+
validateUrl,
|
|
224
|
+
resolveUrl,
|
|
225
|
+
noBody
|
|
226
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wiajs/request",
|
|
3
|
+
"description": "Stream HTTP request client.",
|
|
4
|
+
"keywords": ["http", "simple", "util", "utility"],
|
|
5
|
+
"version": "3.0.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": {
|
|
11
|
+
"require": "./index.d.cts",
|
|
12
|
+
"default": "./index.d.ts"
|
|
13
|
+
},
|
|
14
|
+
"node": {
|
|
15
|
+
"require": "./dist/request.cjs",
|
|
16
|
+
"default": "./index.js"
|
|
17
|
+
},
|
|
18
|
+
"default": {
|
|
19
|
+
"require": "./dist/request.cjs",
|
|
20
|
+
"default": "./index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "cross-env NODE_ENV=production gulp build -f gulpfile.js",
|
|
27
|
+
"lib": "swc --config-file ./.swcrc ./src -d lib -w --strip-leading-paths",
|
|
28
|
+
"test": "npm run lint && npm run test-ci && npm run test-browser",
|
|
29
|
+
"test-ci": "taper tests/test-*.js",
|
|
30
|
+
"test-cov": "nyc --reporter=lcov tape tests/test-*.js",
|
|
31
|
+
"test-browser": "node tests/browser/start.js",
|
|
32
|
+
"lint": "standard"
|
|
33
|
+
},
|
|
34
|
+
"files": ["src/", "lib/", "index.js", "request.js"],
|
|
35
|
+
"author": "Sibyl Yu <sibyl@wia.pub>",
|
|
36
|
+
"contributors": ["Mikeal Rogers <mikeal.rogers@gmail.com>"],
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/request/request.git"
|
|
40
|
+
},
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">= 6"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@wiajs/log": "^4.3.8",
|
|
50
|
+
"mime-types": "^2.1.35"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/mime-types": "^2.1.4"
|
|
54
|
+
}
|
|
55
|
+
}
|