utils-lib-js 1.3.0 → 1.3.3

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.
@@ -26,6 +26,4 @@ __exportStar(require("./static.js"), exports);
26
26
  __exportStar(require("./types.js"), exports);
27
27
  __exportStar(require("./request.js"), exports);
28
28
  __exportStar(require("./event.js"), exports);
29
- __exportStar(require("./storage.js"), exports);
30
- console.log(require, typeof Window !== "undefined");
31
- typeof require === "undefined" && typeof Window !== "undefined" && (window.require = function () {});
29
+ __exportStar(require("./storage.js"), exports);
@@ -14,6 +14,7 @@ declare abstract class RequestBase extends Interceptors implements IRequestBase
14
14
  abstract fetch(url: any, opts: any): Promise<void>;
15
15
  abstract http(url: any, opts: any): Promise<void>;
16
16
  chackUrl: (url: string) => boolean;
17
+ checkIsHttps: (url: string) => boolean;
17
18
  fixOrigin: (fixStr: string) => string;
18
19
  envDesc: () => "Window" | "Node";
19
20
  errorFn: (reject: any) => (err: any) => any;
@@ -40,7 +41,7 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
40
41
  headers?: {};
41
42
  body?: any;
42
43
  timeout?: number;
43
- controller?: AbortController;
44
+ controller?: any;
44
45
  type?: string;
45
46
  }) => {
46
47
  url: string;
@@ -48,8 +49,8 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
48
49
  headers: {};
49
50
  body: string;
50
51
  timeout: number;
51
- signal: AbortSignal;
52
- controller: AbortController;
52
+ signal: any;
53
+ controller: any;
53
54
  type: string;
54
55
  timer: any;
55
56
  };
@@ -39,8 +39,19 @@ var __rest = this && this.__rest || function (s, e) {
39
39
  Object.defineProperty(exports, "__esModule", { value: true });
40
40
  exports.Request = void 0;
41
41
  var index_js_1 = require("./index.js");
42
- var http_1 = require("http");
43
- var url_1 = require("url");
42
+ var httpRequest, httpsRequest, parse, CustomAbortController;
43
+ if (typeof require !== "undefined") {
44
+ CustomAbortController = require("abort-controller");
45
+ httpRequest = require("http").request;
46
+ httpsRequest = require("https").request;
47
+ parse = require("url").parse;
48
+ } else if (typeof AbortController !== "undefined") {
49
+ CustomAbortController = AbortController;
50
+ } else {
51
+ CustomAbortController = function () {
52
+ throw new Error('AbortController is not defined');
53
+ };
54
+ }
44
55
  var Interceptors = function () {
45
56
  function Interceptors() {}
46
57
  Interceptors.prototype.use = function (type, fn) {
@@ -87,6 +98,9 @@ var RequestBase = function (_super) {
87
98
  _this.chackUrl = function (url) {
88
99
  return url.startsWith('/');
89
100
  };
101
+ _this.checkIsHttps = function (url) {
102
+ return url.startsWith('https');
103
+ };
90
104
  _this.fixOrigin = function (fixStr) {
91
105
  if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
92
106
  return fixStr;
@@ -174,7 +188,7 @@ var RequestInit = function (_super) {
174
188
  _f = _a.timeout,
175
189
  timeout = _f === void 0 ? 30 * 1000 : _f,
176
190
  _g = _a.controller,
177
- controller = _g === void 0 ? new AbortController() : _g,
191
+ controller = _g === void 0 ? new CustomAbortController() : _g,
178
192
  _h = _a.type,
179
193
  type = _h === void 0 ? "json" : _h,
180
194
  others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
@@ -188,7 +202,7 @@ var RequestInit = function (_super) {
188
202
  _this.initHttpParams = function (url, opts) {
189
203
  var _a, _b;
190
204
  var params = _this.initAbort(_this.initDefaultParams(url, opts));
191
- var options = (0, url_1.parse)(params.url, true);
205
+ var options = parse(params.url, true);
192
206
  return (_b = (_a = _this.reqFn) === null || _a === void 0 ? void 0 : _a.call(_this, __assign(__assign({}, params), options))) !== null && _b !== void 0 ? _b : __assign(__assign({}, params), options);
193
207
  };
194
208
  return _this;
@@ -230,11 +244,13 @@ var Request = function (_super) {
230
244
  resolve = _a.resolve,
231
245
  reject = _a.reject;
232
246
  var params = _this.initHttpParams(_url, _opts);
233
- var signal = params.signal;
247
+ var signal = params.signal,
248
+ url = params.url;
234
249
  promise.finally(function () {
235
250
  return _this.clearTimer(params);
236
251
  });
237
- var req = (0, http_1.request)(params, function (response) {
252
+ var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
253
+ var req = request(params, function (response) {
238
254
  if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
239
255
  var data_1 = "";
240
256
  response.setEncoding('utf8');
package/dist/esm/index.js CHANGED
@@ -7,6 +7,4 @@ export * from "./static.js";
7
7
  export * from "./types.js";
8
8
  export * from "./request.js";
9
9
  export * from "./event.js";
10
- export * from "./storage.js";
11
- console.log(require, typeof Window !== "undefined");
12
- typeof require === "undefined" && typeof Window !== "undefined" && (window.require = function () {});
10
+ export * from "./storage.js";
@@ -14,6 +14,7 @@ declare abstract class RequestBase extends Interceptors implements IRequestBase
14
14
  abstract fetch(url: any, opts: any): Promise<void>;
15
15
  abstract http(url: any, opts: any): Promise<void>;
16
16
  chackUrl: (url: string) => boolean;
17
+ checkIsHttps: (url: string) => boolean;
17
18
  fixOrigin: (fixStr: string) => string;
18
19
  envDesc: () => "Window" | "Node";
19
20
  errorFn: (reject: any) => (err: any) => any;
@@ -40,7 +41,7 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
40
41
  headers?: {};
41
42
  body?: any;
42
43
  timeout?: number;
43
- controller?: AbortController;
44
+ controller?: any;
44
45
  type?: string;
45
46
  }) => {
46
47
  url: string;
@@ -48,8 +49,8 @@ declare abstract class RequestInit extends RequestBase implements IRequestInit {
48
49
  headers: {};
49
50
  body: string;
50
51
  timeout: number;
51
- signal: AbortSignal;
52
- controller: AbortController;
52
+ signal: any;
53
+ controller: any;
53
54
  type: string;
54
55
  timer: any;
55
56
  };
@@ -35,8 +35,19 @@ var __rest = this && this.__rest || function (s, e) {
35
35
  return t;
36
36
  };
37
37
  import { urlJoin, defer, jsonToString, stringToJson } from "./index.js";
38
- import { request } from "http";
39
- import { parse } from "url";
38
+ var httpRequest, httpsRequest, parse, CustomAbortController;
39
+ if (typeof require !== "undefined") {
40
+ CustomAbortController = require("abort-controller");
41
+ httpRequest = require("http").request;
42
+ httpsRequest = require("https").request;
43
+ parse = require("url").parse;
44
+ } else if (typeof AbortController !== "undefined") {
45
+ CustomAbortController = AbortController;
46
+ } else {
47
+ CustomAbortController = function () {
48
+ throw new Error('AbortController is not defined');
49
+ };
50
+ }
40
51
  var Interceptors = function () {
41
52
  function Interceptors() {}
42
53
  Interceptors.prototype.use = function (type, fn) {
@@ -83,6 +94,9 @@ var RequestBase = function (_super) {
83
94
  _this.chackUrl = function (url) {
84
95
  return url.startsWith('/');
85
96
  };
97
+ _this.checkIsHttps = function (url) {
98
+ return url.startsWith('https');
99
+ };
86
100
  _this.fixOrigin = function (fixStr) {
87
101
  if (_this.chackUrl(fixStr)) return _this.origin + fixStr;
88
102
  return fixStr;
@@ -170,7 +184,7 @@ var RequestInit = function (_super) {
170
184
  _f = _a.timeout,
171
185
  timeout = _f === void 0 ? 30 * 1000 : _f,
172
186
  _g = _a.controller,
173
- controller = _g === void 0 ? new AbortController() : _g,
187
+ controller = _g === void 0 ? new CustomAbortController() : _g,
174
188
  _h = _a.type,
175
189
  type = _h === void 0 ? "json" : _h,
176
190
  others = __rest(_a, ["method", "query", "headers", "body", "timeout", "controller", "type"]);
@@ -226,10 +240,12 @@ var Request = function (_super) {
226
240
  resolve = _a.resolve,
227
241
  reject = _a.reject;
228
242
  var params = _this.initHttpParams(_url, _opts);
229
- var signal = params.signal;
243
+ var signal = params.signal,
244
+ url = params.url;
230
245
  promise.finally(function () {
231
246
  return _this.clearTimer(params);
232
247
  });
248
+ var request = _this.checkIsHttps(url) ? httpsRequest : httpRequest;
233
249
  var req = request(params, function (response) {
234
250
  if ((response === null || response === void 0 ? void 0 : response.statusCode) >= 200 && (response === null || response === void 0 ? void 0 : response.statusCode) < 300) {
235
251
  var data_1 = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utils-lib-js",
3
- "version": "1.3.0",
3
+ "version": "1.3.3",
4
4
  "description": "JavaScript工具函数,封装的一些常用的js函数",
5
5
  "main": "./dist/common/index.js",
6
6
  "types": "./dist/common/index.d.ts",
@@ -24,10 +24,13 @@
24
24
  "@types/node": "^18.7.15",
25
25
  "babel-cli": "^6.26.0"
26
26
  },
27
+ "dependencies": {
28
+ "abort-controller": "^3.0.0"
29
+ },
27
30
  "scripts": {
28
31
  "debug:esm": "start cmd /k pnpm run build:hot:esm",
29
32
  "debug:node": "start cmd /k pnpm run build:hot:node & pnpm run node:hot",
30
- "node:hot": "nodemon ./example/server.js",
33
+ "node:hot": "nodemon server.js",
31
34
  "build:hot:esm": "tsc -p tsconfig.es.json -w",
32
35
  "build:hot:node": "tsc -p tsconfig.json -w",
33
36
  "build": "pnpm run tsc:build && pnpm run babel:mjs && pnpm run babel:cjs",
package/pnpm-lock.yaml CHANGED
@@ -2,18 +2,29 @@ lockfileVersion: 5.3
2
2
 
3
3
  specifiers:
4
4
  '@types/node': ^18.7.15
5
+ abort-controller: ^3.0.0
5
6
  babel-cli: ^6.26.0
6
7
 
8
+ dependencies:
9
+ abort-controller: 3.0.0
10
+
7
11
  devDependencies:
8
- '@types/node': 18.7.15
12
+ '@types/node': 18.11.9
9
13
  babel-cli: 6.26.0
10
14
 
11
15
  packages:
12
16
 
13
- /@types/node/18.7.15:
14
- resolution: {integrity: sha512-XnjpaI8Bgc3eBag2Aw4t2Uj/49lLBSStHWfqKvIuXD7FIrZyMLWp8KuAFHAqxMZYTF9l08N1ctUn9YNybZJVmQ==}
17
+ /@types/node/18.11.9:
18
+ resolution: {integrity: sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==}
15
19
  dev: true
16
20
 
21
+ /abort-controller/3.0.0:
22
+ resolution: {integrity: sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==}
23
+ engines: {node: '>=6.5'}
24
+ dependencies:
25
+ event-target-shim: 5.0.1
26
+ dev: false
27
+
17
28
  /ansi-regex/2.1.1:
18
29
  resolution: {integrity: sha512-TIGnTpdo+E3+pCyAluZvtED5p5wCqLdezCyhPZzKPcxvFplEt4i+W7OONCKgeZFT3+y5NZZfOOS/Bdcanm1MYA==}
19
30
  engines: {node: '>=0.10.0'}
@@ -341,7 +352,7 @@ packages:
341
352
  dev: true
342
353
 
343
354
  /chokidar/1.7.0:
344
- resolution: {integrity: sha512-mk8fAWcRUOxY7btlLtitj3A45jOwSAxH4tOFOoEGbVsl6cL6pPMWUy7dwZ/canfj3QEdP6FHSnf/l1c6/WkzVg==}
355
+ resolution: {integrity: sha1-eY5ol3gVHIB2tLNg5e3SjNortGg=}
345
356
  deprecated: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
346
357
  requiresBuild: true
347
358
  dependencies:
@@ -475,6 +486,11 @@ packages:
475
486
  engines: {node: '>=0.10.0'}
476
487
  dev: true
477
488
 
489
+ /event-target-shim/5.0.1:
490
+ resolution: {integrity: sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==}
491
+ engines: {node: '>=6'}
492
+ dev: false
493
+
478
494
  /expand-brackets/0.1.5:
479
495
  resolution: {integrity: sha512-hxx03P2dJxss6ceIeri9cmYOT4SRs3Zk3afZwWpOsRqLqprhTR8u++SlC+sFGsQr7WGFPdMF7Gjc1njDLDK6UA==}
480
496
  engines: {node: '>=0.10.0'}
@@ -615,7 +631,7 @@ packages:
615
631
  dev: true
616
632
 
617
633
  /fsevents/1.2.13:
618
- resolution: {integrity: sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==}
634
+ resolution: {integrity: sha1-8yXLBFVZJCi88Rs4M3DvcOO/zDg=}
619
635
  engines: {node: '>= 4.0'}
620
636
  os: [darwin]
621
637
  deprecated: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.