swpp-backends 1.0.7 → 1.0.11

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.
@@ -0,0 +1,3 @@
1
+ module.exports = () => {
2
+ return import('got')
3
+ }
@@ -29,10 +29,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.replaceRequest = exports.findCache = exports.eachAllLinkInJavaScript = exports.eachAllLinkInCss = exports.eachAllLinkInHtml = exports.eachAllLinkInUrl = exports.buildVersionJson = exports.readMergeVersionMap = exports.readNewVersionJson = exports.readOldVersionJson = exports.submitCacheInfo = exports.loadVersionJson = exports.isStable = exports.isExclude = void 0;
30
30
  const fs_1 = __importDefault(require("fs"));
31
31
  const path_1 = __importDefault(require("path"));
32
- const node_fetch_1 = require("node-fetch");
33
32
  const SwppRules_1 = require("./SwppRules");
34
33
  const crypto = __importStar(require("crypto"));
35
- const buffer_1 = require("buffer");
36
34
  const fast_html_parser_1 = __importDefault(require("fast-html-parser"));
37
35
  const css_1 = __importDefault(require("css"));
38
36
  const Utils_1 = require("./Utils");
@@ -93,12 +91,12 @@ exports.isStable = isStable;
93
91
  */
94
92
  async function loadVersionJson(url) {
95
93
  const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
96
- if (response?.status === 404) {
94
+ if (response?.statusCode === 404) {
97
95
  (0, Utils_1.warn)('LoadVersionJson', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
98
96
  return _oldVersionJson = null;
99
97
  }
100
98
  else {
101
- return _oldVersionJson = (await response.json());
99
+ return _oldVersionJson = (JSON.parse(response.body));
102
100
  }
103
101
  }
104
102
  exports.loadVersionJson = loadVersionJson;
@@ -253,8 +251,8 @@ async function eachAllLinkInUrl(domain, url, result, event) {
253
251
  }
254
252
  }
255
253
  const response = await (0, Utils_1.fetchFile)(url).catch(err => err);
256
- if (![200, 301, 302, 307, 308].includes(response?.status ?? 0)) {
257
- (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response?.status}`);
254
+ if (![200, 301, 302, 307, 308].includes(response?.statusCode ?? 0)) {
255
+ (0, Utils_1.error)('LinkItorInUrl', `拉取文件 [${url}] 时出现错误:${response?.statusCode}`);
258
256
  return;
259
257
  }
260
258
  event?.(url);
@@ -273,15 +271,15 @@ async function eachAllLinkInUrl(domain, url, result, event) {
273
271
  switch (true) {
274
272
  case pathname.endsWith('.html'):
275
273
  case pathname.endsWith('/'):
276
- content = await response.text();
274
+ content = response.body;
277
275
  update();
278
276
  return eachAllLinkInHtml(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
279
277
  case pathname.endsWith('.css'):
280
- content = await response.text();
278
+ content = response.body;
281
279
  update();
282
280
  return eachAllLinkInCss(domain, url.substring(0, url.lastIndexOf('/') + 1), content, result, nextEvent);
283
281
  case pathname.endsWith('.js'):
284
- content = await response.text();
282
+ content = response.body;
285
283
  update();
286
284
  return eachAllLinkInJavaScript(domain, content, result, nextEvent);
287
285
  default:
@@ -289,8 +287,7 @@ async function eachAllLinkInUrl(domain, url, result, event) {
289
287
  result[url] = [];
290
288
  }
291
289
  else {
292
- const buffer = buffer_1.Buffer.from(await response.arrayBuffer());
293
- result[url] = crypto.createHash('md5').update(buffer).digest('hex');
290
+ result[url] = crypto.createHash('md5').update(response.rawBody).digest('hex');
294
291
  }
295
292
  break;
296
293
  }
@@ -496,7 +493,7 @@ function replaceRequest(url) {
496
493
  if (!('modifyRequest' in rules))
497
494
  return url;
498
495
  const { modifyRequest } = rules;
499
- const request = new node_fetch_1.Request(url);
496
+ const request = new Request(url);
500
497
  return modifyRequest?.(request, (0, Utils_1.readEjectData)()?.nodeEject)?.url ?? url;
501
498
  }
502
499
  exports.replaceRequest = replaceRequest;
@@ -20,11 +20,11 @@ async function loadUpdateJson(url) {
20
20
  if (_oldJson !== undefined)
21
21
  return _oldJson;
22
22
  const response = await (0, Utils_1.fetchFile)(url);
23
- if (response.status === 404) {
23
+ if (response?.statusCode === 404) {
24
24
  (0, Utils_1.warn)('LoadUpdateJson', `拉取 ${url} 时出现 404 错误,如果您是第一次构建请忽略这个警告。`);
25
25
  return _oldJson = null;
26
26
  }
27
- return _oldJson = (await response.json());
27
+ return _oldJson = (JSON.parse(response.body));
28
28
  }
29
29
  exports.loadUpdateJson = loadUpdateJson;
30
30
  /**
@@ -1,10 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.replaceDevRequest = exports.fetchFile = exports.getSource = exports.readEjectData = exports.calcEjectValues = exports.warn = exports.error = void 0;
7
- const node_fetch_1 = __importDefault(require("node-fetch"));
8
4
  const SwppRules_1 = require("./SwppRules");
9
5
  const logger = require('hexo-log').default({
10
6
  debug: false,
@@ -140,27 +136,31 @@ exports.getSource = getSource;
140
136
  * **调用该函数前必须调用过 [loadRules]**
141
137
  */
142
138
  async function fetchFile(link) {
139
+ const GotType = await require('../js/Import.js')();
143
140
  const url = replaceDevRequest(link);
144
141
  const timeout = (0, SwppRules_1.readRules)().config.external.timeout;
145
- const controller = new AbortController();
146
- const opts = {
142
+ const options = {
143
+ http2: true,
147
144
  headers: {
148
145
  referer: 'kmar-swpp',
149
146
  'User-Agent': 'kmar-swpp'
150
- }
147
+ },
148
+ timeout: {
149
+ response: timeout
150
+ },
151
+ retry: { limit: 0 }
151
152
  };
152
153
  try {
153
154
  if (typeof url === 'string') {
154
- setTimeout(() => controller.abort('timeout'), timeout + 100);
155
- return await (0, node_fetch_1.default)(url, opts);
155
+ return await GotType.got.get(url, options);
156
156
  }
157
157
  else {
158
- return await fetchSpeed(url, opts, timeout);
158
+ return await fetchSpeed(url, options);
159
159
  }
160
160
  }
161
161
  catch (err) {
162
162
  // @ts-ignore
163
- if (err.name === 'AbortError') {
163
+ if (err instanceof GotType.TimeoutError) {
164
164
  logger.error(`[SWPP FetchFile] 拉取文件 [${link}] 时出现了超时错误,如果您构建所在的位置无法访问该资源,` +
165
165
  "请尝试通过 DevReplace(https://kmar.top/posts/73014407/#4ea71e00)功能解决该问题。");
166
166
  throw 'timeout';
@@ -180,29 +180,22 @@ function replaceDevRequest(link) {
180
180
  }
181
181
  exports.replaceDevRequest = replaceDevRequest;
182
182
  /** 通过 CDN 竞速的方式拉取文件 */
183
- async function fetchSpeed(list, opts, timeout) {
184
- const controllers = new Array(list.length);
185
- let overtime = false;
186
- setTimeout(() => {
187
- overtime = true;
188
- for (let controller of controllers) {
189
- if (controller)
190
- controller.abort('timeout');
183
+ async function fetchSpeed(list, options) {
184
+ const GotType = await require('../js/Import.js')();
185
+ const controllers = list.map(_ => new AbortController());
186
+ const result = await Promise.any(list.map((it, index) => GotType.got.get(it, Object.assign({ signal: controllers[index].signal }, options)).then((response) => {
187
+ if ([200, 301, 302, 307, 308].includes(response.statusCode))
188
+ return { index, response };
189
+ throw response;
190
+ })));
191
+ if ('index' in result && 'response' in result) {
192
+ for (let i = 0; i < controllers.length; i++) {
193
+ if (i !== result.index)
194
+ controllers[i].abort('timeout');
191
195
  }
192
- }, timeout + 200);
193
- const result = await Promise.any(list.map((it, index) => (0, node_fetch_1.default)(it, {
194
- headers: opts.headers,
195
- signal: (controllers[index] = new AbortController()).signal
196
- }).then(response => [200, 301, 302, 307, 308].includes(response.status) ? { index, response } : Promise.reject()))).catch(err => ({ err }));
197
- if ('err' in result) {
198
- if (overtime)
199
- throw { name: 'AbortError' };
200
- else
201
- throw result.err;
196
+ return result.response;
202
197
  }
203
- for (let i = 0; i < controllers.length; i++) {
204
- if (i !== result.index)
205
- controllers[i].abort();
198
+ else {
199
+ throw result;
206
200
  }
207
- return result.response;
208
201
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "swpp-backends",
3
- "version": "1.0.7",
4
- "main": "dist/index.js",
5
- "typings": "types/index.d.ts",
3
+ "version": "1.0.11",
4
+ "main": "dist/ts/index.js",
5
+ "typings": "types/ts/index.d.ts",
6
6
  "description": "Generate a powerful ServiceWorker for your website.",
7
7
  "author": "kmar",
8
8
  "license": "AGPL-3.0",
@@ -13,16 +13,16 @@
13
13
  "devDependencies": {
14
14
  "@types/css": "^0.0.33",
15
15
  "@types/fast-html-parser": "^1.0.2",
16
+ "@types/got": "^9.6.12",
16
17
  "@types/node": "^20.4.5",
17
- "@types/node-fetch": "^2.6.4",
18
18
  "gulp": "^4.0.2",
19
19
  "typescript": "^5.1.6"
20
20
  },
21
21
  "dependencies": {
22
22
  "css": "^3.0.0",
23
23
  "fast-html-parser": "^1.0.1",
24
- "hexo-log": "^4.1.0",
25
- "node-fetch": "^2.6.12"
24
+ "got": "^13.0.0",
25
+ "hexo-log": "^4.1.0"
26
26
  },
27
27
  "repository": {
28
28
  "type": "git",
@@ -1,4 +1,3 @@
1
- import { Request, Response } from 'node-fetch';
2
1
  import { SwppConfig } from './SwppConfig';
3
2
  /**
4
3
  * 读取最后一次构建的 rules
@@ -1,3 +1,4 @@
1
+ import got from 'got';
1
2
  export declare function error(type: string, message: string): void;
2
3
  export declare function warn(type: string, message: string): void;
3
4
  export interface EjectCache {
@@ -32,7 +33,7 @@ export declare function getSource(obj: any, typeChecker?: ((name: string) => boo
32
33
  *
33
34
  * **调用该函数前必须调用过 [loadRules]**
34
35
  */
35
- export declare function fetchFile(link: string): Promise<import("node-fetch").Response>;
36
+ export declare function fetchFile(link: string): Promise<got.Response<any>>;
36
37
  /**
37
38
  * 替换编译期的 URL(CDN 竞速)
38
39
  *
File without changes
File without changes
File without changes
File without changes
File without changes