@zwa73/utils 1.0.239 → 1.0.241

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.
@@ -1,3 +1,3 @@
1
1
  export * from './Hbs';
2
- export type { PromoseQueueOption, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, BridgeInterface } from "@zwa73/js-utils";
3
- export { Stream, Spool, SmartCache, PromiseQueue, Piper, DLinkedList, Bridge } from "@zwa73/js-utils";
2
+ export type { PromoseQueueOption, EventData, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, BridgeInterface } from "@zwa73/js-utils";
3
+ export { Stream, Spool, SmartCache, PromiseQueue, Piper, EventSystem, DLinkedList, Bridge } from "@zwa73/js-utils";
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.Bridge = exports.DLinkedList = exports.Piper = exports.PromiseQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
17
+ exports.Bridge = exports.DLinkedList = exports.EventSystem = exports.Piper = exports.PromiseQueue = exports.SmartCache = exports.Spool = exports.Stream = void 0;
18
18
  __exportStar(require("./Hbs"), exports);
19
19
  var js_utils_1 = require("@zwa73/js-utils");
20
20
  Object.defineProperty(exports, "Stream", { enumerable: true, get: function () { return js_utils_1.Stream; } });
@@ -22,6 +22,7 @@ Object.defineProperty(exports, "Spool", { enumerable: true, get: function () { r
22
22
  Object.defineProperty(exports, "SmartCache", { enumerable: true, get: function () { return js_utils_1.SmartCache; } });
23
23
  Object.defineProperty(exports, "PromiseQueue", { enumerable: true, get: function () { return js_utils_1.PromiseQueue; } });
24
24
  Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { return js_utils_1.Piper; } });
25
+ Object.defineProperty(exports, "EventSystem", { enumerable: true, get: function () { return js_utils_1.EventSystem; } });
25
26
  Object.defineProperty(exports, "DLinkedList", { enumerable: true, get: function () { return js_utils_1.DLinkedList; } });
26
27
  Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return js_utils_1.Bridge; } });
27
28
  //#endregion
@@ -47,6 +47,11 @@ type WriteJsonFileOpt = Partial<{
47
47
  compressThreshold: number;
48
48
  /**不自动修改扩展名为json */
49
49
  forceExt: boolean;
50
+ /**检查重复, 避免重复写入
51
+ * 只在第一次尝试写入时读取一次文件内容, 后续将会使用缓存判断是否重复
52
+ * 若是其他进程改动了文件内容, 将无法检测到
53
+ */
54
+ checkDuplicate: boolean;
50
55
  }>;
51
56
  /**文件工具 */
52
57
  export declare namespace UtilFT {
@@ -129,8 +134,10 @@ export declare namespace UtilFT {
129
134
  * @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
130
135
  * @param opt.forceExt - 不自动修改扩展名为json
131
136
  * @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
137
+ * @param opt.checkDuplicate - 检查重复输出 避免重复输出
138
+ * @returns 是否完成写入, 出错或检出重复均会返回false
132
139
  */
133
- function writeJSONFile(filePath: string, token: JToken, opt?: WriteJsonFileOpt): Promise<void>;
140
+ function writeJSONFile(filePath: string, token: JToken, opt?: WriteJsonFileOpt): Promise<boolean>;
134
141
  /**保证路径为某个风格
135
142
  * @param filePath - 输入路径
136
143
  * @param style - 目标风格 undefined 时不改变
@@ -188,6 +188,7 @@ var UtilFT;
188
188
  }
189
189
  }
190
190
  UtilFT.loadJSONFile = loadJSONFile;
191
+ const writeJSONFileDupMap = {};
191
192
  /**写入JSON文件
192
193
  * @async
193
194
  * @param filePath - 文件路径
@@ -196,6 +197,8 @@ var UtilFT;
196
197
  * @param opt.compress - 使用紧凑风格 将会用/\uF121\uF122\uF123.+\uF121\uF122\uF123/作为特殊标记, 原始文本内若出现相同格式将会产生错误
197
198
  * @param opt.forceExt - 不自动修改扩展名为json
198
199
  * @param opt.compressThreshold - 紧凑风格 压缩阈值 默认100
200
+ * @param opt.checkDuplicate - 检查重复输出 避免重复输出
201
+ * @returns 是否完成写入, 出错或检出重复均会返回false
199
202
  */
200
203
  async function writeJSONFile(filePath, token, opt) {
201
204
  const str = UtilFunctions_1.UtilFunc.stringifyJToken(token, opt);
@@ -204,13 +207,24 @@ var UtilFT;
204
207
  // 判断文件路径是否存在 不存在则创建
205
208
  if (!(await pathExists(filePath)))
206
209
  await createPath(filePath);
210
+ // 检查内容是否重复
211
+ if (opt?.checkDuplicate) {
212
+ if (writeJSONFileDupMap[filePath] === undefined)
213
+ writeJSONFileDupMap[filePath] = await fs.promises.readFile(filePath, 'utf-8');
214
+ if (writeJSONFileDupMap[filePath] == str)
215
+ return false;
216
+ else
217
+ writeJSONFileDupMap[filePath] = str;
218
+ }
207
219
  // 写入文件
208
220
  try {
209
221
  await fs.promises.writeFile(filePath, str);
210
222
  UtilLogger_1.SLogger.verbose(`${filePath} writeJSONFile 成功`);
223
+ return true;
211
224
  }
212
225
  catch (err) {
213
226
  UtilLogger_1.SLogger.error(`${filePath} writeJSONFile 错误`, err);
227
+ return false;
214
228
  }
215
229
  }
216
230
  UtilFT.writeJSONFile = writeJSONFile;
@@ -45,5 +45,5 @@ declare class _UtilFunc {
45
45
  /**获取当前公网ipv4 */
46
46
  static getPublicIpv4(): Promise<string>;
47
47
  }
48
- export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "outcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
48
+ export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "getTime" | "initField" | "initObject" | "afterward" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "lazyFunction" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "concurrent" | "dynamicImport" | "createInjectable" | "outcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
49
49
  export type UtilFunc = typeof UtilFunc;
@@ -14,8 +14,9 @@ export type RequestResult<T> = {
14
14
  };
15
15
  /**网络请求选项 */
16
16
  export type RequestOption = {
17
- /**请求之间的间隔, 单位毫秒 默认 1
18
- * 如果用promise并发请求可能会导致req.write提前中断, 为避免此问题需要设置请求间隔
17
+ /**请求之间的间隔, 单位毫秒 默认 0
18
+ * 如果用promise并发请求可能会因缓冲区溢出导致req.write提前中断
19
+ * 为避免此问题需要设置请求间隔
19
20
  */
20
21
  interval?: number;
21
22
  /**请求协议 */
package/dist/UtilHttp.js CHANGED
@@ -242,9 +242,12 @@ class UtilHttp {
242
242
  this._data.headers ??= {};
243
243
  this._data.headers['Content-Length'] = Buffer.byteLength(data);
244
244
  return {
245
- proc: req => {
246
- if (isPost)
247
- req.write(data);
245
+ proc: async (req) => {
246
+ if (isPost) {
247
+ const flushed = req.write(data);
248
+ if (!flushed)
249
+ await new Promise((resolve) => req.once('drain', resolve));
250
+ }
248
251
  req.end();
249
252
  }
250
253
  };
@@ -294,9 +297,12 @@ class UtilHttp {
294
297
  this._data.headers ??= {};
295
298
  this._data.headers['Content-Length'] = Buffer.byteLength(data);
296
299
  return {
297
- proc: req => {
298
- if (isPost)
299
- req.write(data);
300
+ proc: async (req) => {
301
+ if (isPost) {
302
+ const flushed = req.write(data);
303
+ if (!flushed)
304
+ await new Promise((resolve) => req.once('drain', resolve));
305
+ }
300
306
  req.end();
301
307
  }
302
308
  };
@@ -376,14 +382,14 @@ class UtilHttp {
376
382
  static async request(option, sendProc, acceptProc) {
377
383
  const { reduce: reqReduce, init: reqInit } = acceptProc;
378
384
  const { proc: reqProc } = sendProc;
379
- const { protocol, timeout = 0, interval = 1, ...baseReqOpt } = option;
385
+ const { protocol, timeout = 0, interval = 0, ...baseReqOpt } = option;
380
386
  const plusTimeout = timeout + 1000;
381
387
  const hasTimeLimit = timeout >= 10_000;
382
388
  const flagName = `UtilCom.request ${protocol}${baseReqOpt.method} ${UtilFunctions_1.UtilFunc.genUUID()}`;
383
389
  const reduceQueue = new js_utils_1.PromiseQueue();
384
390
  let dataPromise = null;
385
391
  //等待间隔
386
- if (UtilHttp.requestQueue.length > 0)
392
+ if (UtilHttp.requestQueue.length > 0 && interval > 0)
387
393
  await UtilHttp.requestQueue.enqueue(async () => UtilFunctions_1.UtilFunc.sleep(interval));
388
394
  return new Promise(async (resolve, rejecte) => {
389
395
  const finallyTimeout = hasTimeLimit
@@ -1 +1 @@
1
- export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize } from "@zwa73/js-utils";
1
+ export type { JToken, JValue, JArray, JObject, IJData, AnyFunc, Keyable, DeepReadonly, PartialOption, ProperSubset, ProperSubsetCheck, IncludeCheck, UnionCheck, Literal, LiteralCheck, AllExtends, AssignObject, Writeable, Inverted, FixedLengthTuple, Increment, RangeTuple, AnyString, UnionToIntersection, ExclusiveRecord, ExclusiveJObject, PromiseStatus, StatusVerifyFn, Await, FuncPropNames, ExtendThen, RequiredOnly, WithPrefix, Outcome, Either, Matchable, MatchableFlag, ExtractOutcome, ExtractMatchable, NeedInit, PRecord, MPromise, SPromise, SMPromise, NotPromise, AnyRecord, MReturnType, CmtTuple, Flasy, SrtSegment, ILogger, LogLevel, Asyncize } from "@zwa73/js-utils";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.239",
3
+ "version": "1.0.241",
4
4
  "description": "my utils",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {