xjs-node 1.0.4 → 1.0.6

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 CHANGED
@@ -58,7 +58,7 @@ import { HttpResolver, s_clientMode } from "xjs-node";
58
58
  const context = http.newContext();
59
59
  res = await context.get("https://begyyal.net/1");
60
60
  // this request sends with cookies that is set by precedent requests.
61
- // in POST, payload is treated as json if it is an object.
61
+ // in POST, payload is treated as json if it is an object. (but also Stream is acceptable.)
62
62
  res = await context.post("https://begyyal.net/2", { a: "b" });
63
63
  })();
64
64
  ```
package/dist/index.d.ts CHANGED
@@ -2,4 +2,4 @@ export * from "./func/u";
2
2
  export * from "./func/u-file";
3
3
  export { HttpResolver, ClientMode } from "./prcs/http/http-resolver";
4
4
  export { s_clientMode } from "./prcs/http/http-resolver-context";
5
- export { IHttpClient } from "./prcs/http/i-http-client";
5
+ export { IHttpClient, HttpResponse } from "./prcs/http/i-http-client";
@@ -35,6 +35,7 @@ const async_hooks_1 = require("async_hooks");
35
35
  const u_file_1 = require("../../func/u-file");
36
36
  const u_1 = require("../../func/u");
37
37
  const xjs_common_1 = require("xjs-common");
38
+ const stream_1 = require("stream");
38
39
  exports.s_clientMode = {
39
40
  nodejs: { id: 0, cipherOrder: null },
40
41
  chrome: { id: 1, cipherOrder: [2, 0, 1] },
@@ -44,36 +45,36 @@ const s_errCode = 1200;
44
45
  const s_redirectLimit = 5;
45
46
  const s_mode2headers = new Map([
46
47
  [exports.s_clientMode.firefox, (cmv) => ({
47
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
48
- "Accept-Encoding": "gzip, deflate, br",
49
- "Accept-Language": "en-US,en;q=0.5",
50
- "Sec-Fetch-Dest": "document",
51
- "Sec-Fetch-Mode": "navigate",
52
- "Sec-Fetch-Site": "none",
53
- "Sec-Fetch-User": "?1",
54
- "Upgrade-Insecure-Requests": "1",
55
- "User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${cmv}.0) Gecko/20100101 Firefox/${cmv}.0`
48
+ "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
49
+ "accept-encoding": "gzip, deflate, br",
50
+ "accept-language": "en-US,en;q=0.5",
51
+ "sec-fetch-dest": "document",
52
+ "sec-fetch-mode": "navigate",
53
+ "sec-fetch-site": "none",
54
+ "sec-fetch-user": "?1",
55
+ "upgrade-insecure-requests": "1",
56
+ "user-agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${cmv}.0) Gecko/20100101 Firefox/${cmv}.0`
56
57
  })],
57
58
  [exports.s_clientMode.chrome, (cmv) => {
58
59
  const uad = cmv < 130
59
60
  ? `"Not/A)Brand";v="8", "Chromium";v="${cmv}", "Google Chrome";v="${cmv}"`
60
61
  : `"Chromium";v="${cmv}", "Not:A-Brand";v="24", "Google Chrome";v="${cmv}"`;
61
62
  const ch = {
62
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
63
- "Accept-Encoding": "gzip, deflate, br, zstd",
64
- "Accept-Language": "en-US,en;q=0.9",
65
- "Sec-Ch-Ua": uad,
66
- "Sec-Ch-Ua-Mobile": "?0",
67
- "Sec-Ch-Ua-Platform": '"Windows"',
68
- "Sec-Fetch-Dest": "document",
69
- "Sec-Fetch-Mode": "navigate",
70
- "Sec-Fetch-Site": "none",
71
- "Sec-Fetch-User": "?1",
72
- "Upgrade-Insecure-Requests": "1",
73
- "User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${cmv}.0.0.0 Safari/537.36`
63
+ "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
64
+ "accept-encoding": "gzip, deflate, br, zstd",
65
+ "accept-language": "en-US,en;q=0.9",
66
+ "sec-ch-ua": uad,
67
+ "sec-ch-ua-mobile": "?0",
68
+ "sec-ch-ua-platform": '"Windows"',
69
+ "sec-fetch-dest": "document",
70
+ "sec-fetch-mode": "navigate",
71
+ "sec-fetch-site": "none",
72
+ "sec-fetch-user": "?1",
73
+ "upgrade-insecure-requests": "1",
74
+ "user-agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${cmv}.0.0.0 Safari/537.36`
74
75
  };
75
76
  if (cmv >= 124)
76
- ch["Priority"] = "u=0, i";
77
+ ch["priority"] = "u=0, i";
77
78
  return ch;
78
79
  }]
79
80
  ]);
@@ -116,7 +117,7 @@ class HttpResolverContext {
116
117
  return new Promise((resolve, reject) => {
117
118
  const headers = {};
118
119
  if (conf.auth)
119
- headers['Proxy-Authorization'] = `Basic ${Buffer.from(conf.auth.name + ':' + conf.auth.pass).toString('base64')}`;
120
+ headers['proxy-authorization'] = `Basic ${Buffer.from(conf.auth.name + ':' + conf.auth.pass).toString('base64')}`;
120
121
  const req = (0, http_1.request)({
121
122
  host: conf.server,
122
123
  port: conf.port,
@@ -141,19 +142,22 @@ class HttpResolverContext {
141
142
  const params = {};
142
143
  const rc = this._als.getStore();
143
144
  params.method = xjs_common_1.HttpMethod.Get;
144
- params.headers = rc.headers ?? {};
145
+ params.headers = xjs_common_1.UHttp.normalizeHeaders(rc.headers);
145
146
  return await this.reqHttps(u, params);
146
147
  };
147
148
  postIn = async (u, payload) => {
148
149
  const params = {};
149
150
  const rc = this._als.getStore();
150
151
  params.method = xjs_common_1.HttpMethod.Post;
151
- params.headers = rc.headers ?? {};
152
+ params.headers = xjs_common_1.UHttp.normalizeHeaders(rc.headers);
152
153
  let p = payload;
153
- if (xjs_common_1.UType.isObject(payload)) {
154
+ if (p instanceof stream_1.Stream) {
155
+ params.headers["content-type"] ??= "application/octet-stream";
156
+ }
157
+ else if (xjs_common_1.UType.isObject(payload)) {
154
158
  p = JSON.stringify(payload);
155
- params.headers["Content-Length"] = p.length;
156
- params.headers["Content-Type"] = "application/json";
159
+ params.headers["content-length"] = p.length;
160
+ params.headers["content-type"] = "application/json";
157
161
  }
158
162
  return await this.reqHttps(u, params, p);
159
163
  };
@@ -178,7 +182,10 @@ class HttpResolverContext {
178
182
  reject(new xjs_common_1.XjsErr(s_errCode, "The http request timeout, maybe server did not respond."));
179
183
  });
180
184
  if (payload)
181
- req.write(payload);
185
+ if (payload instanceof stream_1.Stream)
186
+ payload.pipe(req);
187
+ else
188
+ req.write(payload);
182
189
  req.end();
183
190
  });
184
191
  }
@@ -70,7 +70,7 @@ export interface IHttpClient {
70
70
  /**
71
71
  * request POST to the url with new context.
72
72
  * @param url target url. (currently https only)
73
- * @param payload request payload. if this is an object, it is treated as json.
73
+ * @param payload request payload. if this is a Stream, pipe will be used, otherwise if an object, this is treated as json.
74
74
  * @param op.headers http headers.
75
75
  * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
76
76
  * @param op.proxy proxy configuration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,6 +29,6 @@
29
29
  "typescript": "^4.9.5"
30
30
  },
31
31
  "dependencies": {
32
- "xjs-common": "^10.1.0"
32
+ "xjs-common": "^10.1.3"
33
33
  }
34
34
  }