@faasjs/request 1.3.0 → 1.3.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/README.md +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +119 -159
- package/dist/index.mjs +117 -141
- package/package.json +3 -3
package/README.md
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
2
|
import { Logger } from '@faasjs/logger';
|
|
3
3
|
|
|
4
4
|
type Request = {
|
|
@@ -117,6 +117,6 @@ declare class ResponseError extends Error {
|
|
|
117
117
|
*
|
|
118
118
|
* @url https://faasjs.com/doc/request.html
|
|
119
119
|
*/
|
|
120
|
-
declare function request<T = any>(url: string,
|
|
120
|
+
declare function request<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
|
|
121
121
|
|
|
122
122
|
export { type Request, type RequestOptions, type Response, ResponseError, querystringify, request, setMock };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
2
|
import { Logger } from '@faasjs/logger';
|
|
3
3
|
|
|
4
4
|
type Request = {
|
|
@@ -117,6 +117,6 @@ declare class ResponseError extends Error {
|
|
|
117
117
|
*
|
|
118
118
|
* @url https://faasjs.com/doc/request.html
|
|
119
119
|
*/
|
|
120
|
-
declare function request<T = any>(url: string,
|
|
120
|
+
declare function request<T = any>(url: string, options?: RequestOptions): Promise<Response<T>>;
|
|
121
121
|
|
|
122
122
|
export { type Request, type RequestOptions, type Response, ResponseError, querystringify, request, setMock };
|
package/dist/index.js
CHANGED
|
@@ -9,26 +9,10 @@ var logger = require('@faasjs/logger');
|
|
|
9
9
|
var zlib = require('zlib');
|
|
10
10
|
var crypto = require('crypto');
|
|
11
11
|
|
|
12
|
-
function
|
|
13
|
-
if (e && e.__esModule) return e;
|
|
14
|
-
var n = Object.create(null);
|
|
15
|
-
if (e) {
|
|
16
|
-
Object.keys(e).forEach(function (k) {
|
|
17
|
-
if (k !== 'default') {
|
|
18
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
-
enumerable: true,
|
|
21
|
-
get: function () { return e[k]; }
|
|
22
|
-
});
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
n.default = e;
|
|
27
|
-
return Object.freeze(n);
|
|
28
|
-
}
|
|
12
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
29
13
|
|
|
30
|
-
var
|
|
31
|
-
var
|
|
14
|
+
var http__default = /*#__PURE__*/_interopDefault(http);
|
|
15
|
+
var https__default = /*#__PURE__*/_interopDefault(https);
|
|
32
16
|
|
|
33
17
|
// src/index.ts
|
|
34
18
|
var mock = null;
|
|
@@ -65,185 +49,161 @@ var ResponseError = class extends Error {
|
|
|
65
49
|
this.body = response.body;
|
|
66
50
|
}
|
|
67
51
|
};
|
|
68
|
-
async function request(url$1, {
|
|
69
|
-
headers,
|
|
70
|
-
method,
|
|
71
|
-
query,
|
|
72
|
-
body,
|
|
73
|
-
timeout,
|
|
74
|
-
auth,
|
|
75
|
-
file,
|
|
76
|
-
downloadStream,
|
|
77
|
-
downloadFile,
|
|
78
|
-
pfx,
|
|
79
|
-
passphrase,
|
|
80
|
-
agent,
|
|
81
|
-
parse,
|
|
82
|
-
logger: logger$1
|
|
83
|
-
} = { headers: {} }) {
|
|
52
|
+
async function request(url$1, options = { headers: {} }) {
|
|
84
53
|
var _a;
|
|
85
|
-
|
|
86
|
-
|
|
54
|
+
const requestId = crypto.randomUUID();
|
|
55
|
+
const logger$1 = options.logger || new logger.Logger(`request][${requestId}`);
|
|
87
56
|
if (mock) {
|
|
88
|
-
logger$1.debug("mock %s %j", url$1,
|
|
89
|
-
|
|
90
|
-
method,
|
|
91
|
-
query,
|
|
92
|
-
body
|
|
93
|
-
});
|
|
94
|
-
return mock(url$1, {
|
|
95
|
-
headers,
|
|
96
|
-
method,
|
|
97
|
-
query,
|
|
98
|
-
body
|
|
99
|
-
});
|
|
57
|
+
logger$1.debug("mock %s %j", url$1, options);
|
|
58
|
+
return mock(url$1, options);
|
|
100
59
|
}
|
|
101
|
-
if (query) {
|
|
60
|
+
if (options.query) {
|
|
102
61
|
if (!url$1.includes("?"))
|
|
103
62
|
url$1 += "?";
|
|
104
63
|
else if (!url$1.endsWith("?"))
|
|
105
64
|
url$1 += "&";
|
|
106
|
-
url$1 += querystringify(query);
|
|
65
|
+
url$1 += querystringify(options.query);
|
|
107
66
|
}
|
|
67
|
+
if (!options.headers)
|
|
68
|
+
options.headers = {};
|
|
108
69
|
const uri = new url.URL(url$1);
|
|
109
|
-
const protocol = uri.protocol === "https:" ?
|
|
70
|
+
const protocol = uri.protocol === "https:" ? https__default.default : http__default.default;
|
|
110
71
|
if (!uri.protocol)
|
|
111
72
|
throw Error("Unknown protocol");
|
|
112
|
-
const
|
|
73
|
+
const requestOptions = {
|
|
113
74
|
headers: {},
|
|
114
75
|
host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
|
|
115
|
-
method: method ? method.toUpperCase() : "GET",
|
|
76
|
+
method: options.method ? options.method.toUpperCase() : "GET",
|
|
116
77
|
path: uri.pathname + uri.search,
|
|
117
78
|
port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
|
|
118
|
-
timeout: timeout || 5e3,
|
|
119
|
-
auth,
|
|
120
|
-
pfx,
|
|
121
|
-
passphrase,
|
|
122
|
-
agent
|
|
79
|
+
timeout: options.timeout || 5e3,
|
|
80
|
+
auth: options.auth,
|
|
81
|
+
pfx: options.pfx,
|
|
82
|
+
passphrase: options.passphrase,
|
|
83
|
+
agent: options.agent
|
|
123
84
|
};
|
|
124
|
-
if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream)
|
|
85
|
+
if (!options.headers["Accept-Encoding"] && !options.downloadFile && !options.downloadStream)
|
|
125
86
|
options.headers["Accept-Encoding"] = "br,gzip";
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
87
|
+
for (const key in options.headers)
|
|
88
|
+
if (typeof options.headers[key] !== "undefined" && options.headers[key] !== null)
|
|
89
|
+
requestOptions.headers[key] = options.headers[key];
|
|
90
|
+
let body = options.body;
|
|
130
91
|
if (body && typeof body !== "string")
|
|
131
92
|
if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
|
|
132
93
|
body = querystringify(body);
|
|
133
94
|
else
|
|
134
95
|
body = JSON.stringify(body);
|
|
135
96
|
if (body && !options.headers["Content-Length"])
|
|
136
|
-
|
|
137
|
-
const requestId = crypto.randomUUID();
|
|
97
|
+
requestOptions.headers["Content-Length"] = Buffer.byteLength(body);
|
|
138
98
|
return await new Promise((resolve, reject) => {
|
|
139
99
|
logger$1.debug("request %j", {
|
|
140
100
|
...options,
|
|
141
101
|
body
|
|
142
102
|
});
|
|
143
|
-
const req = protocol.request(
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
103
|
+
const req = protocol.request(
|
|
104
|
+
requestOptions,
|
|
105
|
+
(res) => {
|
|
106
|
+
if (options.downloadStream) {
|
|
107
|
+
options.downloadStream.on("error", (error) => {
|
|
108
|
+
logger$1.timeEnd(requestId, "response.error %j", error);
|
|
109
|
+
reject(error);
|
|
110
|
+
}).on("finish", () => {
|
|
111
|
+
logger$1.timeEnd(
|
|
112
|
+
requestId,
|
|
113
|
+
"response %s %s",
|
|
114
|
+
res.statusCode,
|
|
115
|
+
res.headers["content-type"]
|
|
116
|
+
);
|
|
117
|
+
options.downloadStream.end();
|
|
118
|
+
resolve(void 0);
|
|
119
|
+
});
|
|
120
|
+
res.pipe(options.downloadStream, { end: true });
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (options.downloadFile) {
|
|
124
|
+
const stream2 = fs.createWriteStream(options.downloadFile).on("error", (error) => {
|
|
125
|
+
logger$1.timeEnd(requestId, "response.error %j", error);
|
|
126
|
+
stream2.destroy();
|
|
127
|
+
reject(error);
|
|
128
|
+
}).on("finish", () => {
|
|
129
|
+
logger$1.timeEnd(
|
|
130
|
+
requestId,
|
|
131
|
+
"response %s %s %s",
|
|
132
|
+
res.statusCode,
|
|
133
|
+
res.headers["content-type"],
|
|
134
|
+
stream2.bytesWritten
|
|
135
|
+
);
|
|
136
|
+
resolve(void 0);
|
|
137
|
+
});
|
|
138
|
+
res.pipe(stream2, { end: true });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
let stream = res;
|
|
142
|
+
switch (res.headers["content-encoding"]) {
|
|
143
|
+
case "br":
|
|
144
|
+
stream = res.pipe(zlib.createBrotliDecompress());
|
|
145
|
+
break;
|
|
146
|
+
case "gzip":
|
|
147
|
+
stream = res.pipe(zlib.createGunzip());
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
const raw = [];
|
|
151
|
+
stream.on("error", (e) => {
|
|
152
|
+
logger$1.timeEnd(requestId, "response.error %j", e);
|
|
153
|
+
reject(e);
|
|
154
|
+
}).on("end", () => {
|
|
155
|
+
const data = Buffer.concat(raw).toString();
|
|
156
|
+
logger$1.timeEnd(
|
|
157
|
+
requestId,
|
|
158
|
+
"response %s %s %s %j",
|
|
159
|
+
res.statusCode,
|
|
160
|
+
res.headers["content-type"],
|
|
161
|
+
res.headers["content-encoding"],
|
|
162
|
+
data
|
|
175
163
|
);
|
|
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
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
202
|
-
response.request = options;
|
|
203
|
-
response.request.body = body;
|
|
204
|
-
response.statusCode = res.statusCode;
|
|
205
|
-
response.statusMessage = res.statusMessage;
|
|
206
|
-
response.headers = res.headers;
|
|
207
|
-
response.body = data;
|
|
208
|
-
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
209
|
-
try {
|
|
210
|
-
response.body = (parse || JSON.parse)(response.body);
|
|
211
|
-
logger$1.debug("response.parse JSON");
|
|
212
|
-
} catch (error) {
|
|
213
|
-
logger$1.warn("response plain body", response.body);
|
|
214
|
-
logger$1.error(error);
|
|
164
|
+
const response = /* @__PURE__ */ Object.create(null);
|
|
165
|
+
response.request = requestOptions;
|
|
166
|
+
response.request.body = body;
|
|
167
|
+
response.statusCode = res.statusCode;
|
|
168
|
+
response.statusMessage = res.statusMessage;
|
|
169
|
+
response.headers = res.headers;
|
|
170
|
+
response.body = data;
|
|
171
|
+
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
172
|
+
try {
|
|
173
|
+
response.body = (options.parse || JSON.parse)(response.body);
|
|
174
|
+
logger$1.debug("response.parse JSON");
|
|
175
|
+
} catch (error) {
|
|
176
|
+
logger$1.warn("response plain body", response.body);
|
|
177
|
+
logger$1.error(error);
|
|
178
|
+
}
|
|
179
|
+
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
180
|
+
resolve(response);
|
|
181
|
+
else {
|
|
182
|
+
logger$1.debug("response.error %j", response);
|
|
183
|
+
reject(
|
|
184
|
+
new ResponseError(
|
|
185
|
+
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
186
|
+
response
|
|
187
|
+
)
|
|
188
|
+
);
|
|
215
189
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
logger$1.debug("response.error %j", response);
|
|
220
|
-
reject(
|
|
221
|
-
new ResponseError(
|
|
222
|
-
`${res.statusMessage || res.statusCode} ${options.host}${options.path}`,
|
|
223
|
-
response
|
|
224
|
-
)
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
stream.on("error", (e) => {
|
|
229
|
-
logger$1.timeEnd(requestId, "response.error %j", e);
|
|
230
|
-
reject(e);
|
|
231
|
-
});
|
|
232
|
-
});
|
|
190
|
+
}).on("data", (chunk) => raw.push(chunk));
|
|
191
|
+
}
|
|
192
|
+
);
|
|
233
193
|
if (body)
|
|
234
194
|
req.write(body);
|
|
235
|
-
if (file) {
|
|
195
|
+
if (options.file) {
|
|
236
196
|
const crlf = "\r\n";
|
|
237
197
|
const boundary = `--${Math.random().toString(16)}`;
|
|
238
198
|
const delimiter = `${crlf}--${boundary}`;
|
|
239
|
-
const
|
|
199
|
+
const headers = [
|
|
240
200
|
`Content-Disposition: form-data; name="file"; filename="${path.basename(
|
|
241
|
-
file
|
|
201
|
+
options.file
|
|
242
202
|
)}"${crlf}`
|
|
243
203
|
];
|
|
244
204
|
const multipartBody = Buffer.concat([
|
|
245
|
-
Buffer.from(delimiter + crlf +
|
|
246
|
-
fs.readFileSync(file),
|
|
205
|
+
Buffer.from(delimiter + crlf + headers.join("") + crlf),
|
|
206
|
+
fs.readFileSync(options.file),
|
|
247
207
|
Buffer.from(`${delimiter}--`)
|
|
248
208
|
]);
|
|
249
209
|
req.setHeader("Content-Type", `multipart/form-data; boundary=${boundary}`);
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import https from 'node:https';
|
|
3
3
|
import { URL } from 'node:url';
|
|
4
4
|
import { createWriteStream, readFileSync } from 'node:fs';
|
|
5
5
|
import { basename } from 'node:path';
|
|
@@ -42,185 +42,161 @@ var ResponseError = class extends Error {
|
|
|
42
42
|
this.body = response.body;
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
|
-
async function request(url, {
|
|
46
|
-
headers,
|
|
47
|
-
method,
|
|
48
|
-
query,
|
|
49
|
-
body,
|
|
50
|
-
timeout,
|
|
51
|
-
auth,
|
|
52
|
-
file,
|
|
53
|
-
downloadStream,
|
|
54
|
-
downloadFile,
|
|
55
|
-
pfx,
|
|
56
|
-
passphrase,
|
|
57
|
-
agent,
|
|
58
|
-
parse,
|
|
59
|
-
logger
|
|
60
|
-
} = { headers: {} }) {
|
|
45
|
+
async function request(url, options = { headers: {} }) {
|
|
61
46
|
var _a;
|
|
62
|
-
|
|
63
|
-
|
|
47
|
+
const requestId = randomUUID();
|
|
48
|
+
const logger = options.logger || new Logger(`request][${requestId}`);
|
|
64
49
|
if (mock) {
|
|
65
|
-
logger.debug("mock %s %j", url,
|
|
66
|
-
|
|
67
|
-
method,
|
|
68
|
-
query,
|
|
69
|
-
body
|
|
70
|
-
});
|
|
71
|
-
return mock(url, {
|
|
72
|
-
headers,
|
|
73
|
-
method,
|
|
74
|
-
query,
|
|
75
|
-
body
|
|
76
|
-
});
|
|
50
|
+
logger.debug("mock %s %j", url, options);
|
|
51
|
+
return mock(url, options);
|
|
77
52
|
}
|
|
78
|
-
if (query) {
|
|
53
|
+
if (options.query) {
|
|
79
54
|
if (!url.includes("?"))
|
|
80
55
|
url += "?";
|
|
81
56
|
else if (!url.endsWith("?"))
|
|
82
57
|
url += "&";
|
|
83
|
-
url += querystringify(query);
|
|
58
|
+
url += querystringify(options.query);
|
|
84
59
|
}
|
|
60
|
+
if (!options.headers)
|
|
61
|
+
options.headers = {};
|
|
85
62
|
const uri = new URL(url);
|
|
86
63
|
const protocol = uri.protocol === "https:" ? https : http;
|
|
87
64
|
if (!uri.protocol)
|
|
88
65
|
throw Error("Unknown protocol");
|
|
89
|
-
const
|
|
66
|
+
const requestOptions = {
|
|
90
67
|
headers: {},
|
|
91
68
|
host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
|
|
92
|
-
method: method ? method.toUpperCase() : "GET",
|
|
69
|
+
method: options.method ? options.method.toUpperCase() : "GET",
|
|
93
70
|
path: uri.pathname + uri.search,
|
|
94
71
|
port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
|
|
95
|
-
timeout: timeout || 5e3,
|
|
96
|
-
auth,
|
|
97
|
-
pfx,
|
|
98
|
-
passphrase,
|
|
99
|
-
agent
|
|
72
|
+
timeout: options.timeout || 5e3,
|
|
73
|
+
auth: options.auth,
|
|
74
|
+
pfx: options.pfx,
|
|
75
|
+
passphrase: options.passphrase,
|
|
76
|
+
agent: options.agent
|
|
100
77
|
};
|
|
101
|
-
if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream)
|
|
78
|
+
if (!options.headers["Accept-Encoding"] && !options.downloadFile && !options.downloadStream)
|
|
102
79
|
options.headers["Accept-Encoding"] = "br,gzip";
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
80
|
+
for (const key in options.headers)
|
|
81
|
+
if (typeof options.headers[key] !== "undefined" && options.headers[key] !== null)
|
|
82
|
+
requestOptions.headers[key] = options.headers[key];
|
|
83
|
+
let body = options.body;
|
|
107
84
|
if (body && typeof body !== "string")
|
|
108
85
|
if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
|
|
109
86
|
body = querystringify(body);
|
|
110
87
|
else
|
|
111
88
|
body = JSON.stringify(body);
|
|
112
89
|
if (body && !options.headers["Content-Length"])
|
|
113
|
-
|
|
114
|
-
const requestId = randomUUID();
|
|
90
|
+
requestOptions.headers["Content-Length"] = Buffer.byteLength(body);
|
|
115
91
|
return await new Promise((resolve, reject) => {
|
|
116
92
|
logger.debug("request %j", {
|
|
117
93
|
...options,
|
|
118
94
|
body
|
|
119
95
|
});
|
|
120
|
-
const req = protocol.request(
|
|
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
|
-
|
|
96
|
+
const req = protocol.request(
|
|
97
|
+
requestOptions,
|
|
98
|
+
(res) => {
|
|
99
|
+
if (options.downloadStream) {
|
|
100
|
+
options.downloadStream.on("error", (error) => {
|
|
101
|
+
logger.timeEnd(requestId, "response.error %j", error);
|
|
102
|
+
reject(error);
|
|
103
|
+
}).on("finish", () => {
|
|
104
|
+
logger.timeEnd(
|
|
105
|
+
requestId,
|
|
106
|
+
"response %s %s",
|
|
107
|
+
res.statusCode,
|
|
108
|
+
res.headers["content-type"]
|
|
109
|
+
);
|
|
110
|
+
options.downloadStream.end();
|
|
111
|
+
resolve(void 0);
|
|
112
|
+
});
|
|
113
|
+
res.pipe(options.downloadStream, { end: true });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
if (options.downloadFile) {
|
|
117
|
+
const stream2 = createWriteStream(options.downloadFile).on("error", (error) => {
|
|
118
|
+
logger.timeEnd(requestId, "response.error %j", error);
|
|
119
|
+
stream2.destroy();
|
|
120
|
+
reject(error);
|
|
121
|
+
}).on("finish", () => {
|
|
122
|
+
logger.timeEnd(
|
|
123
|
+
requestId,
|
|
124
|
+
"response %s %s %s",
|
|
125
|
+
res.statusCode,
|
|
126
|
+
res.headers["content-type"],
|
|
127
|
+
stream2.bytesWritten
|
|
128
|
+
);
|
|
129
|
+
resolve(void 0);
|
|
130
|
+
});
|
|
131
|
+
res.pipe(stream2, { end: true });
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
let stream = res;
|
|
135
|
+
switch (res.headers["content-encoding"]) {
|
|
136
|
+
case "br":
|
|
137
|
+
stream = res.pipe(createBrotliDecompress());
|
|
138
|
+
break;
|
|
139
|
+
case "gzip":
|
|
140
|
+
stream = res.pipe(createGunzip());
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
const raw = [];
|
|
144
|
+
stream.on("error", (e) => {
|
|
145
|
+
logger.timeEnd(requestId, "response.error %j", e);
|
|
146
|
+
reject(e);
|
|
147
|
+
}).on("end", () => {
|
|
148
|
+
const data = Buffer.concat(raw).toString();
|
|
149
|
+
logger.timeEnd(
|
|
150
|
+
requestId,
|
|
151
|
+
"response %s %s %s %j",
|
|
152
|
+
res.statusCode,
|
|
153
|
+
res.headers["content-type"],
|
|
154
|
+
res.headers["content-encoding"],
|
|
155
|
+
data
|
|
152
156
|
);
|
|
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
|
-
const response = /* @__PURE__ */ Object.create(null);
|
|
179
|
-
response.request = options;
|
|
180
|
-
response.request.body = body;
|
|
181
|
-
response.statusCode = res.statusCode;
|
|
182
|
-
response.statusMessage = res.statusMessage;
|
|
183
|
-
response.headers = res.headers;
|
|
184
|
-
response.body = data;
|
|
185
|
-
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
186
|
-
try {
|
|
187
|
-
response.body = (parse || JSON.parse)(response.body);
|
|
188
|
-
logger.debug("response.parse JSON");
|
|
189
|
-
} catch (error) {
|
|
190
|
-
logger.warn("response plain body", response.body);
|
|
191
|
-
logger.error(error);
|
|
157
|
+
const response = /* @__PURE__ */ Object.create(null);
|
|
158
|
+
response.request = requestOptions;
|
|
159
|
+
response.request.body = body;
|
|
160
|
+
response.statusCode = res.statusCode;
|
|
161
|
+
response.statusMessage = res.statusMessage;
|
|
162
|
+
response.headers = res.headers;
|
|
163
|
+
response.body = data;
|
|
164
|
+
if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
|
|
165
|
+
try {
|
|
166
|
+
response.body = (options.parse || JSON.parse)(response.body);
|
|
167
|
+
logger.debug("response.parse JSON");
|
|
168
|
+
} catch (error) {
|
|
169
|
+
logger.warn("response plain body", response.body);
|
|
170
|
+
logger.error(error);
|
|
171
|
+
}
|
|
172
|
+
if (response.statusCode >= 200 && response.statusCode < 400)
|
|
173
|
+
resolve(response);
|
|
174
|
+
else {
|
|
175
|
+
logger.debug("response.error %j", response);
|
|
176
|
+
reject(
|
|
177
|
+
new ResponseError(
|
|
178
|
+
`${res.statusMessage || res.statusCode} ${requestOptions.host}${requestOptions.path}`,
|
|
179
|
+
response
|
|
180
|
+
)
|
|
181
|
+
);
|
|
192
182
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
logger.debug("response.error %j", response);
|
|
197
|
-
reject(
|
|
198
|
-
new ResponseError(
|
|
199
|
-
`${res.statusMessage || res.statusCode} ${options.host}${options.path}`,
|
|
200
|
-
response
|
|
201
|
-
)
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
stream.on("error", (e) => {
|
|
206
|
-
logger.timeEnd(requestId, "response.error %j", e);
|
|
207
|
-
reject(e);
|
|
208
|
-
});
|
|
209
|
-
});
|
|
183
|
+
}).on("data", (chunk) => raw.push(chunk));
|
|
184
|
+
}
|
|
185
|
+
);
|
|
210
186
|
if (body)
|
|
211
187
|
req.write(body);
|
|
212
|
-
if (file) {
|
|
188
|
+
if (options.file) {
|
|
213
189
|
const crlf = "\r\n";
|
|
214
190
|
const boundary = `--${Math.random().toString(16)}`;
|
|
215
191
|
const delimiter = `${crlf}--${boundary}`;
|
|
216
|
-
const
|
|
192
|
+
const headers = [
|
|
217
193
|
`Content-Disposition: form-data; name="file"; filename="${basename(
|
|
218
|
-
file
|
|
194
|
+
options.file
|
|
219
195
|
)}"${crlf}`
|
|
220
196
|
];
|
|
221
197
|
const multipartBody = Buffer.concat([
|
|
222
|
-
Buffer.from(delimiter + crlf +
|
|
223
|
-
readFileSync(file),
|
|
198
|
+
Buffer.from(delimiter + crlf + headers.join("") + crlf),
|
|
199
|
+
readFileSync(options.file),
|
|
224
200
|
Buffer.from(`${delimiter}--`)
|
|
225
201
|
]);
|
|
226
202
|
req.setHeader("Content-Type", `multipart/form-data; boundary=${boundary}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/request",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"peerDependencies": {
|
|
24
|
-
"@faasjs/logger": "1.3.
|
|
24
|
+
"@faasjs/logger": "1.3.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@faasjs/logger": "1.3.
|
|
27
|
+
"@faasjs/logger": "1.3.2"
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
30
|
"npm": ">=9.0.0",
|