@faasjs/request 0.0.4-beta.15 → 0.0.4-beta.17

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/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import * as http from 'http';
1
+ import * as http from 'node:http';
2
2
  import { Logger } from '@faasjs/logger';
3
3
 
4
4
  type Request = {
@@ -118,4 +118,4 @@ declare class ResponseError extends Error {
118
118
  */
119
119
  declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, downloadFile, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
120
120
 
121
- export { Request, RequestOptions, Response, ResponseError, querystringify, request, setMock };
121
+ export { type Request, type RequestOptions, type Response, ResponseError, querystringify, request, setMock };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import * as http from 'http';
1
+ import * as http from 'node:http';
2
2
  import { Logger } from '@faasjs/logger';
3
3
 
4
4
  type Request = {
@@ -118,4 +118,4 @@ declare class ResponseError extends Error {
118
118
  */
119
119
  declare function request<T = any>(url: string, { headers, method, query, body, timeout, auth, file, downloadStream, downloadFile, pfx, passphrase, agent, parse, logger, }?: RequestOptions): Promise<Response<T>>;
120
120
 
121
- export { Request, RequestOptions, Response, ResponseError, querystringify, request, setMock };
121
+ export { type Request, type RequestOptions, type Response, ResponseError, querystringify, request, setMock };
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ var url = require('url');
6
6
  var fs = require('fs');
7
7
  var path = require('path');
8
8
  var logger = require('@faasjs/logger');
9
+ var zlib = require('zlib');
10
+ var crypto = require('crypto');
9
11
 
10
12
  function _interopNamespace(e) {
11
13
  if (e && e.__esModule) return e;
@@ -119,6 +121,9 @@ async function request(url$1, {
119
121
  passphrase,
120
122
  agent
121
123
  };
124
+ if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream) {
125
+ options.headers["Accept-Encoding"] = "br,gzip";
126
+ }
122
127
  for (const key in headers)
123
128
  if (typeof headers[key] !== "undefined" && headers[key] !== null)
124
129
  options.headers[key] = headers[key];
@@ -129,61 +134,101 @@ async function request(url$1, {
129
134
  body = JSON.stringify(body);
130
135
  if (body && !options.headers["Content-Length"])
131
136
  options.headers["Content-Length"] = Buffer.byteLength(body);
132
- return await new Promise(function(resolve, reject) {
137
+ const requestId = crypto.randomUUID();
138
+ return await new Promise((resolve, reject) => {
133
139
  logger$1.debug("request %j", {
134
140
  ...options,
135
141
  body
136
142
  });
137
- const req = protocol.request(options, function(res) {
143
+ const req = protocol.request(options, (res) => {
138
144
  if (downloadStream) {
139
- res.pipe(downloadStream);
140
- downloadStream.on("finish", () => resolve(void 0));
141
- } else if (downloadFile) {
142
- const stream = fs.createWriteStream(downloadFile);
143
- res.pipe(stream);
144
- stream.on("finish", () => resolve(void 0));
145
- } else {
146
- const raw = [];
147
- res.on("data", (chunk) => {
148
- raw.push(chunk);
145
+ res.pipe(downloadStream, { end: true });
146
+ downloadStream.on("finish", () => {
147
+ res.destroy();
148
+ downloadStream.end();
149
+ resolve(void 0);
149
150
  });
151
+ return;
152
+ }
153
+ if (downloadFile) {
154
+ logger$1.debug("downloadFile");
155
+ const stream2 = fs.createWriteStream(downloadFile, { autoClose: true });
156
+ res.pipe(stream2, { end: true });
150
157
  res.on("end", () => {
151
- const data = Buffer.concat(raw).toString();
152
- logger$1.timeEnd(
153
- url$1,
154
- "response %s %s %s",
155
- res.statusCode,
156
- res.headers["content-type"],
157
- data
158
+ logger$1.debug("end");
159
+ res.destroy();
160
+ });
161
+ stream2.on("finish", () => {
162
+ stream2.end();
163
+ stream2.close();
164
+ logger$1.debug("finish");
165
+ });
166
+ stream2.on("close", () => {
167
+ logger$1.debug(
168
+ "finish",
169
+ res.closed,
170
+ res.destroyed,
171
+ stream2.destroyed,
172
+ stream2.closed,
173
+ req.closed,
174
+ req.destroyed
158
175
  );
159
- const response = /* @__PURE__ */ Object.create(null);
160
- response.request = options;
161
- response.request.body = body;
162
- response.statusCode = res.statusCode;
163
- response.statusMessage = res.statusMessage;
164
- response.headers = res.headers;
165
- response.body = data;
166
- if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
167
- try {
168
- response.body = (parse || JSON.parse)(response.body);
169
- logger$1.debug("response.parse JSON");
170
- } catch (error) {
171
- console.warn("response plain body", response.body);
172
- console.error(error);
173
- }
174
- if (response.statusCode >= 200 && response.statusCode < 400)
175
- resolve(response);
176
- else {
177
- logger$1.debug("response.error %j", response);
178
- reject(
179
- new ResponseError(
180
- `${res.statusMessage || res.statusCode} ${options.host}${options.path}`,
181
- response
182
- )
183
- );
184
- }
176
+ resolve(void 0);
185
177
  });
178
+ return;
186
179
  }
180
+ let stream = res;
181
+ switch (res.headers["content-encoding"]) {
182
+ case "br":
183
+ stream = res.pipe(zlib.createBrotliDecompress());
184
+ break;
185
+ case "gzip":
186
+ stream = res.pipe(zlib.createGunzip());
187
+ break;
188
+ }
189
+ const raw = [];
190
+ stream.on("data", (chunk) => raw.push(chunk));
191
+ stream.on("end", () => {
192
+ const data = Buffer.concat(raw).toString();
193
+ logger$1.timeEnd(
194
+ requestId,
195
+ "response %s %s %s %j",
196
+ res.statusCode,
197
+ res.headers["content-type"],
198
+ res.headers["content-encoding"],
199
+ data
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);
215
+ }
216
+ if (response.statusCode >= 200 && response.statusCode < 400)
217
+ resolve(response);
218
+ else {
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
+ });
187
232
  });
188
233
  if (body)
189
234
  req.write(body);
@@ -205,11 +250,16 @@ async function request(url$1, {
205
250
  req.setHeader("Content-Length", multipartBody.length);
206
251
  req.write(multipartBody);
207
252
  }
208
- req.on("error", function(e) {
209
- logger$1.timeEnd(url$1, "response.error %j", e);
253
+ req.on("error", (e) => {
254
+ logger$1.timeEnd(requestId, "response.error %j", e);
210
255
  reject(e);
211
256
  });
212
- logger$1.time(url$1);
257
+ req.on("timeout", () => {
258
+ logger$1.timeEnd(requestId, "response.timeout");
259
+ req.destroy();
260
+ reject(Error(`Timeout ${url$1}`));
261
+ });
262
+ logger$1.time(requestId);
213
263
  req.end();
214
264
  });
215
265
  }
package/dist/index.mjs CHANGED
@@ -1,9 +1,11 @@
1
- import * as http from 'http';
2
- import * as https from 'https';
3
- import { URL } from 'url';
4
- import { createWriteStream, readFileSync } from 'fs';
5
- import { basename } from 'path';
1
+ import * as http from 'node:http';
2
+ import * as https from 'node:https';
3
+ import { URL } from 'node:url';
4
+ import { createWriteStream, readFileSync } from 'node:fs';
5
+ import { basename } from 'node:path';
6
6
  import { Logger } from '@faasjs/logger';
7
+ import { createGunzip, createBrotliDecompress } from 'node:zlib';
8
+ import { randomUUID } from 'node:crypto';
7
9
 
8
10
  // src/index.ts
9
11
  var mock = null;
@@ -96,6 +98,9 @@ async function request(url, {
96
98
  passphrase,
97
99
  agent
98
100
  };
101
+ if (!options.headers["Accept-Encoding"] && !downloadFile && !downloadStream) {
102
+ options.headers["Accept-Encoding"] = "br,gzip";
103
+ }
99
104
  for (const key in headers)
100
105
  if (typeof headers[key] !== "undefined" && headers[key] !== null)
101
106
  options.headers[key] = headers[key];
@@ -106,61 +111,101 @@ async function request(url, {
106
111
  body = JSON.stringify(body);
107
112
  if (body && !options.headers["Content-Length"])
108
113
  options.headers["Content-Length"] = Buffer.byteLength(body);
109
- return await new Promise(function(resolve, reject) {
114
+ const requestId = randomUUID();
115
+ return await new Promise((resolve, reject) => {
110
116
  logger.debug("request %j", {
111
117
  ...options,
112
118
  body
113
119
  });
114
- const req = protocol.request(options, function(res) {
120
+ const req = protocol.request(options, (res) => {
115
121
  if (downloadStream) {
116
- res.pipe(downloadStream);
117
- downloadStream.on("finish", () => resolve(void 0));
118
- } else if (downloadFile) {
119
- const stream = createWriteStream(downloadFile);
120
- res.pipe(stream);
121
- stream.on("finish", () => resolve(void 0));
122
- } else {
123
- const raw = [];
124
- res.on("data", (chunk) => {
125
- raw.push(chunk);
122
+ res.pipe(downloadStream, { end: true });
123
+ downloadStream.on("finish", () => {
124
+ res.destroy();
125
+ downloadStream.end();
126
+ resolve(void 0);
126
127
  });
128
+ return;
129
+ }
130
+ if (downloadFile) {
131
+ logger.debug("downloadFile");
132
+ const stream2 = createWriteStream(downloadFile, { autoClose: true });
133
+ res.pipe(stream2, { end: true });
127
134
  res.on("end", () => {
128
- const data = Buffer.concat(raw).toString();
129
- logger.timeEnd(
130
- url,
131
- "response %s %s %s",
132
- res.statusCode,
133
- res.headers["content-type"],
134
- data
135
+ logger.debug("end");
136
+ res.destroy();
137
+ });
138
+ stream2.on("finish", () => {
139
+ stream2.end();
140
+ stream2.close();
141
+ logger.debug("finish");
142
+ });
143
+ stream2.on("close", () => {
144
+ logger.debug(
145
+ "finish",
146
+ res.closed,
147
+ res.destroyed,
148
+ stream2.destroyed,
149
+ stream2.closed,
150
+ req.closed,
151
+ req.destroyed
135
152
  );
136
- const response = /* @__PURE__ */ Object.create(null);
137
- response.request = options;
138
- response.request.body = body;
139
- response.statusCode = res.statusCode;
140
- response.statusMessage = res.statusMessage;
141
- response.headers = res.headers;
142
- response.body = data;
143
- if (response.body && response.headers["content-type"] && response.headers["content-type"].includes("application/json"))
144
- try {
145
- response.body = (parse || JSON.parse)(response.body);
146
- logger.debug("response.parse JSON");
147
- } catch (error) {
148
- console.warn("response plain body", response.body);
149
- console.error(error);
150
- }
151
- if (response.statusCode >= 200 && response.statusCode < 400)
152
- resolve(response);
153
- else {
154
- logger.debug("response.error %j", response);
155
- reject(
156
- new ResponseError(
157
- `${res.statusMessage || res.statusCode} ${options.host}${options.path}`,
158
- response
159
- )
160
- );
161
- }
153
+ resolve(void 0);
162
154
  });
155
+ return;
163
156
  }
157
+ let stream = res;
158
+ switch (res.headers["content-encoding"]) {
159
+ case "br":
160
+ stream = res.pipe(createBrotliDecompress());
161
+ break;
162
+ case "gzip":
163
+ stream = res.pipe(createGunzip());
164
+ break;
165
+ }
166
+ const raw = [];
167
+ stream.on("data", (chunk) => raw.push(chunk));
168
+ stream.on("end", () => {
169
+ const data = Buffer.concat(raw).toString();
170
+ logger.timeEnd(
171
+ requestId,
172
+ "response %s %s %s %j",
173
+ res.statusCode,
174
+ res.headers["content-type"],
175
+ res.headers["content-encoding"],
176
+ data
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);
192
+ }
193
+ if (response.statusCode >= 200 && response.statusCode < 400)
194
+ resolve(response);
195
+ else {
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
+ });
164
209
  });
165
210
  if (body)
166
211
  req.write(body);
@@ -182,11 +227,16 @@ async function request(url, {
182
227
  req.setHeader("Content-Length", multipartBody.length);
183
228
  req.write(multipartBody);
184
229
  }
185
- req.on("error", function(e) {
186
- logger.timeEnd(url, "response.error %j", e);
230
+ req.on("error", (e) => {
231
+ logger.timeEnd(requestId, "response.error %j", e);
187
232
  reject(e);
188
233
  });
189
- logger.time(url);
234
+ req.on("timeout", () => {
235
+ logger.timeEnd(requestId, "response.timeout");
236
+ req.destroy();
237
+ reject(Error(`Timeout ${url}`));
238
+ });
239
+ logger.time(requestId);
190
240
  req.end();
191
241
  });
192
242
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faasjs/request",
3
- "version": "0.0.4-beta.15",
3
+ "version": "0.0.4-beta.17",
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": "0.0.4-beta.15"
24
+ "@faasjs/logger": "0.0.4-beta.17"
25
25
  },
26
26
  "devDependencies": {
27
- "@faasjs/logger": "0.0.4-beta.15"
27
+ "@faasjs/logger": "0.0.4-beta.17"
28
28
  },
29
29
  "engines": {
30
30
  "npm": ">=9.0.0",