@faasjs/request 1.2.0 → 1.3.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/README.md +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +118 -158
- package/dist/index.mjs +116 -140
- 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
|
-
logger$1 = new logger.Logger("request");
|
|
54
|
+
const logger$1 = options.logger || new logger.Logger("request");
|
|
87
55
|
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
|
-
});
|
|
56
|
+
logger$1.debug("mock %s %j", url$1, options);
|
|
57
|
+
return mock(url$1, options);
|
|
100
58
|
}
|
|
101
|
-
if (query) {
|
|
59
|
+
if (options.query) {
|
|
102
60
|
if (!url$1.includes("?"))
|
|
103
61
|
url$1 += "?";
|
|
104
62
|
else if (!url$1.endsWith("?"))
|
|
105
63
|
url$1 += "&";
|
|
106
|
-
url$1 += querystringify(query);
|
|
64
|
+
url$1 += querystringify(options.query);
|
|
107
65
|
}
|
|
66
|
+
if (!options.headers)
|
|
67
|
+
options.headers = {};
|
|
108
68
|
const uri = new url.URL(url$1);
|
|
109
|
-
const protocol = uri.protocol === "https:" ?
|
|
69
|
+
const protocol = uri.protocol === "https:" ? https__default.default : http__default.default;
|
|
110
70
|
if (!uri.protocol)
|
|
111
71
|
throw Error("Unknown protocol");
|
|
112
|
-
const
|
|
72
|
+
const requestOptions = {
|
|
113
73
|
headers: {},
|
|
114
74
|
host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
|
|
115
|
-
method: method ? method.toUpperCase() : "GET",
|
|
75
|
+
method: options.method ? options.method.toUpperCase() : "GET",
|
|
116
76
|
path: uri.pathname + uri.search,
|
|
117
77
|
port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
|
|
118
|
-
timeout: timeout || 5e3,
|
|
119
|
-
auth,
|
|
120
|
-
pfx,
|
|
121
|
-
passphrase,
|
|
122
|
-
agent
|
|
78
|
+
timeout: options.timeout || 5e3,
|
|
79
|
+
auth: options.auth,
|
|
80
|
+
pfx: options.pfx,
|
|
81
|
+
passphrase: options.passphrase,
|
|
82
|
+
agent: options.agent
|
|
123
83
|
};
|
|
124
|
-
if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream)
|
|
84
|
+
if (!options.headers["Accept-Encoding"] && !options.downloadFile && !options.downloadStream)
|
|
125
85
|
options.headers["Accept-Encoding"] = "br,gzip";
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
86
|
+
for (const key in options.headers)
|
|
87
|
+
if (typeof options.headers[key] !== "undefined" && options.headers[key] !== null)
|
|
88
|
+
requestOptions.headers[key] = options.headers[key];
|
|
89
|
+
let body = options.body;
|
|
130
90
|
if (body && typeof body !== "string")
|
|
131
91
|
if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
|
|
132
92
|
body = querystringify(body);
|
|
133
93
|
else
|
|
134
94
|
body = JSON.stringify(body);
|
|
135
95
|
if (body && !options.headers["Content-Length"])
|
|
136
|
-
|
|
96
|
+
requestOptions.headers["Content-Length"] = Buffer.byteLength(body);
|
|
137
97
|
const requestId = crypto.randomUUID();
|
|
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
|
-
console.warn("response plain body", response.body);
|
|
214
|
-
console.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
|
-
logger = new Logger("request");
|
|
47
|
+
const logger = options.logger || new Logger("request");
|
|
64
48
|
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
|
-
});
|
|
49
|
+
logger.debug("mock %s %j", url, options);
|
|
50
|
+
return mock(url, options);
|
|
77
51
|
}
|
|
78
|
-
if (query) {
|
|
52
|
+
if (options.query) {
|
|
79
53
|
if (!url.includes("?"))
|
|
80
54
|
url += "?";
|
|
81
55
|
else if (!url.endsWith("?"))
|
|
82
56
|
url += "&";
|
|
83
|
-
url += querystringify(query);
|
|
57
|
+
url += querystringify(options.query);
|
|
84
58
|
}
|
|
59
|
+
if (!options.headers)
|
|
60
|
+
options.headers = {};
|
|
85
61
|
const uri = new URL(url);
|
|
86
62
|
const protocol = uri.protocol === "https:" ? https : http;
|
|
87
63
|
if (!uri.protocol)
|
|
88
64
|
throw Error("Unknown protocol");
|
|
89
|
-
const
|
|
65
|
+
const requestOptions = {
|
|
90
66
|
headers: {},
|
|
91
67
|
host: uri.host ? uri.host.replace(/:[0-9]+$/, "") : uri.host,
|
|
92
|
-
method: method ? method.toUpperCase() : "GET",
|
|
68
|
+
method: options.method ? options.method.toUpperCase() : "GET",
|
|
93
69
|
path: uri.pathname + uri.search,
|
|
94
70
|
port: uri.port || (uri.protocol === "https:" ? "443" : "80"),
|
|
95
|
-
timeout: timeout || 5e3,
|
|
96
|
-
auth,
|
|
97
|
-
pfx,
|
|
98
|
-
passphrase,
|
|
99
|
-
agent
|
|
71
|
+
timeout: options.timeout || 5e3,
|
|
72
|
+
auth: options.auth,
|
|
73
|
+
pfx: options.pfx,
|
|
74
|
+
passphrase: options.passphrase,
|
|
75
|
+
agent: options.agent
|
|
100
76
|
};
|
|
101
|
-
if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream)
|
|
77
|
+
if (!options.headers["Accept-Encoding"] && !options.downloadFile && !options.downloadStream)
|
|
102
78
|
options.headers["Accept-Encoding"] = "br,gzip";
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
79
|
+
for (const key in options.headers)
|
|
80
|
+
if (typeof options.headers[key] !== "undefined" && options.headers[key] !== null)
|
|
81
|
+
requestOptions.headers[key] = options.headers[key];
|
|
82
|
+
let body = options.body;
|
|
107
83
|
if (body && typeof body !== "string")
|
|
108
84
|
if ((_a = options.headers["Content-Type"]) == null ? void 0 : _a.toString().includes("application/x-www-form-urlencoded"))
|
|
109
85
|
body = querystringify(body);
|
|
110
86
|
else
|
|
111
87
|
body = JSON.stringify(body);
|
|
112
88
|
if (body && !options.headers["Content-Length"])
|
|
113
|
-
|
|
89
|
+
requestOptions.headers["Content-Length"] = Buffer.byteLength(body);
|
|
114
90
|
const requestId = randomUUID();
|
|
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
|
-
console.warn("response plain body", response.body);
|
|
191
|
-
console.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
|
+
"version": "1.3.1",
|
|
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.
|
|
24
|
+
"@faasjs/logger": "1.3.1"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@faasjs/logger": "1.
|
|
27
|
+
"@faasjs/logger": "1.3.1"
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
30
|
"npm": ">=9.0.0",
|