@zwa73/utils 1.0.221 → 1.0.223
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/QuickExport.d.ts +1 -1
- package/dist/QuickExport.js +2 -2
- package/dist/UtilClass/Hbs.d.ts +18 -0
- package/dist/UtilClass/Hbs.js +48 -0
- package/dist/UtilClass/index.d.ts +2 -0
- package/dist/{UtilClass.js → UtilClass/index.js} +16 -2
- package/dist/UtilFileTools.d.ts +23 -0
- package/dist/UtilFileTools.js +62 -12
- package/dist/UtilFunctions.d.ts +7 -5
- package/dist/UtilFunctions.js +18 -3
- package/package.json +3 -3
- package/dist/UtilClass.d.ts +0 -1
package/dist/QuickExport.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { UtilFT } from "./UtilFileTools";
|
|
2
|
-
export declare const outcome: typeof import("@zwa73/js-utils").JsFunc.outcome, success: typeof import("@zwa73/js-utils").JsFunc.success, failed: typeof import("@zwa73/js-utils").JsFunc.failed, match: typeof import("@zwa73/js-utils").JsFunc.match, isSafeNumber: typeof import("@zwa73/js-utils").JsFunc.isSafeNumber, assertType: typeof import("@zwa73/js-utils").JsFunc.assertType, assertLiteral: typeof import("@zwa73/js-utils").JsFunc.assertLiteral, deepClone: typeof import("@zwa73/js-utils").JsFunc.deepClone, sleep: typeof import("@zwa73/js-utils").JsFunc.sleep, stringifyJToken: typeof import("@zwa73/js-utils").JsFunc.stringifyJToken, getTime: typeof import("@zwa73/js-utils").JsFunc.getTime, mapEntries: typeof import("@zwa73/js-utils").JsFunc.mapEntries, dedent: typeof import("@zwa73/js-utils").JsFunc.dedent, throwError: typeof import("@zwa73/js-utils").JsFunc.throwError, eitherize: typeof import("@zwa73/js-utils").JsFunc.eitherize, memoize: typeof import("@zwa73/js-utils").JsFunc.memoize, ivk: typeof import("@zwa73/js-utils").JsFunc.ivk, l2s: typeof import("@zwa73/js-utils").JsFunc.l2s, s2l: typeof import("@zwa73/js-utils").JsFunc.s2l;
|
|
2
|
+
export declare const outcome: typeof import("@zwa73/js-utils").JsFunc.outcome, success: typeof import("@zwa73/js-utils").JsFunc.success, failed: typeof import("@zwa73/js-utils").JsFunc.failed, match: typeof import("@zwa73/js-utils").JsFunc.match, isSafeNumber: typeof import("@zwa73/js-utils").JsFunc.isSafeNumber, assertType: typeof import("@zwa73/js-utils").JsFunc.assertType, assertLiteral: typeof import("@zwa73/js-utils").JsFunc.assertLiteral, deepClone: typeof import("@zwa73/js-utils").JsFunc.deepClone, sleep: typeof import("@zwa73/js-utils").JsFunc.sleep, stringifyJToken: typeof import("@zwa73/js-utils").JsFunc.stringifyJToken, getTime: typeof import("@zwa73/js-utils").JsFunc.getTime, mapEntries: typeof import("@zwa73/js-utils").JsFunc.mapEntries, dedent: typeof import("@zwa73/js-utils").JsFunc.dedent, throwError: typeof import("@zwa73/js-utils").JsFunc.throwError, eitherize: typeof import("@zwa73/js-utils").JsFunc.eitherize, memoize: typeof import("@zwa73/js-utils").JsFunc.memoize, ivk: typeof import("@zwa73/js-utils").JsFunc.ivk, l2s: typeof import("@zwa73/js-utils").JsFunc.l2s, s2l: typeof import("@zwa73/js-utils").JsFunc.s2l, structEqual: typeof import("@zwa73/js-utils").JsFunc.structEqual;
|
|
3
3
|
export declare const stylizePath: typeof UtilFT.stylizePath, posixizePath: typeof UtilFT.posixizePath, win32izePath: typeof UtilFT.win32izePath, currosizePath: typeof UtilFT.currosizePath;
|
|
4
4
|
export declare const when: <S extends import("@zwa73/js-utils").Keyable, T, R>(stat: import("@zwa73/js-utils").Literal<S>, f: (arg: Extract<T, import("@zwa73/js-utils").Matchable<S>>) => R) => (arg: T) => ("E" extends import("@zwa73/js-utils").ProperSubsetCheck<symbol, T> ? T | R : import("@zwa73/js-utils").MatchableFlag<T> extends S ? R : import("@zwa73/js-utils").MatchableFlag<T> extends Exclude<S, import("@zwa73/js-utils").MatchableFlag<T>> ? T : Exclude<T, import("@zwa73/js-utils").Matchable<S>> | R), map: typeof import("@zwa73/js-utils").JsFP.map, flow: typeof import("@zwa73/js-utils").JsFP.flow, pipe: typeof import("@zwa73/js-utils").JsFP.pipe, chain: <T, R>(f: (arg: Extract<T, import("@zwa73/js-utils").Matchable<typeof import("@zwa73/js-utils").Success>>) => R) => (arg: T) => ("E" extends import("@zwa73/js-utils").ProperSubsetCheck<symbol, T> ? T | R : import("@zwa73/js-utils").MatchableFlag<T> extends typeof import("@zwa73/js-utils").Success ? R : import("@zwa73/js-utils").MatchableFlag<T> extends Exclude<typeof import("@zwa73/js-utils").Success, import("@zwa73/js-utils").MatchableFlag<T>> ? T : Exclude<T, import("@zwa73/js-utils").Matchable<typeof import("@zwa73/js-utils").Success>> | R), alt: <T, R>(f: (arg: Extract<T, import("@zwa73/js-utils").Matchable<typeof import("@zwa73/js-utils").Failed>>) => R) => (arg: T) => ("E" extends import("@zwa73/js-utils").ProperSubsetCheck<symbol, T> ? T | R : import("@zwa73/js-utils").MatchableFlag<T> extends typeof import("@zwa73/js-utils").Failed ? R : import("@zwa73/js-utils").MatchableFlag<T> extends Exclude<typeof import("@zwa73/js-utils").Failed, import("@zwa73/js-utils").MatchableFlag<T>> ? T : Exclude<T, import("@zwa73/js-utils").Matchable<typeof import("@zwa73/js-utils").Failed>> | R), tap: typeof import("@zwa73/js-utils").JsFP.tap, curry: typeof import("@zwa73/js-utils").JsFP.curry;
|
package/dist/QuickExport.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.curry = exports.tap = exports.alt = exports.chain = exports.pipe = exports.flow = exports.map = exports.when = exports.currosizePath = exports.win32izePath = exports.posixizePath = exports.stylizePath = exports.s2l = exports.l2s = exports.ivk = exports.memoize = exports.eitherize = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.isSafeNumber = exports.match = exports.failed = exports.success = exports.outcome = void 0;
|
|
3
|
+
exports.curry = exports.tap = exports.alt = exports.chain = exports.pipe = exports.flow = exports.map = exports.when = exports.currosizePath = exports.win32izePath = exports.posixizePath = exports.stylizePath = exports.structEqual = exports.s2l = exports.l2s = exports.ivk = exports.memoize = exports.eitherize = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.isSafeNumber = exports.match = exports.failed = exports.success = exports.outcome = void 0;
|
|
4
4
|
const UtilFileTools_1 = require("./UtilFileTools");
|
|
5
5
|
const UtilFP_1 = require("./UtilFP");
|
|
6
6
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
7
|
-
exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.success = UtilFunctions_1.UtilFunc.success, exports.failed = UtilFunctions_1.UtilFunc.failed, exports.match = UtilFunctions_1.UtilFunc.match, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError, exports.eitherize = UtilFunctions_1.UtilFunc.eitherize, exports.memoize = UtilFunctions_1.UtilFunc.memoize, exports.ivk = UtilFunctions_1.UtilFunc.ivk, exports.l2s = UtilFunctions_1.UtilFunc.l2s, exports.s2l = UtilFunctions_1.UtilFunc.s2l;
|
|
7
|
+
exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.success = UtilFunctions_1.UtilFunc.success, exports.failed = UtilFunctions_1.UtilFunc.failed, exports.match = UtilFunctions_1.UtilFunc.match, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError, exports.eitherize = UtilFunctions_1.UtilFunc.eitherize, exports.memoize = UtilFunctions_1.UtilFunc.memoize, exports.ivk = UtilFunctions_1.UtilFunc.ivk, exports.l2s = UtilFunctions_1.UtilFunc.l2s, exports.s2l = UtilFunctions_1.UtilFunc.s2l, exports.structEqual = UtilFunctions_1.UtilFunc.structEqual;
|
|
8
8
|
exports.stylizePath = UtilFileTools_1.UtilFT.stylizePath, exports.posixizePath = UtilFileTools_1.UtilFT.posixizePath, exports.win32izePath = UtilFileTools_1.UtilFT.win32izePath, exports.currosizePath = UtilFileTools_1.UtilFT.currosizePath;
|
|
9
9
|
exports.when = UtilFP_1.UtilFP.when, exports.map = UtilFP_1.UtilFP.map, exports.flow = UtilFP_1.UtilFP.flow, exports.pipe = UtilFP_1.UtilFP.pipe, exports.chain = UtilFP_1.UtilFP.chain, exports.alt = UtilFP_1.UtilFP.alt, exports.tap = UtilFP_1.UtilFP.tap, exports.curry = UtilFP_1.UtilFP.curry;
|
|
10
10
|
if (false) {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Keyable } from '@zwa73/js-utils';
|
|
2
|
+
import Handlebars from 'handlebars';
|
|
3
|
+
/**文本模板渲染器
|
|
4
|
+
* @template T - 上下文对象的类型,默认为记录类型
|
|
5
|
+
*/
|
|
6
|
+
export declare class Hbs<T extends Record<Keyable, any> = Record<Keyable, any>> {
|
|
7
|
+
hbs: typeof Handlebars;
|
|
8
|
+
context: T;
|
|
9
|
+
/**创建一个新的 Hbs 实例
|
|
10
|
+
* @param context - 上下文对象,用于在渲染模板时提供数据
|
|
11
|
+
*/
|
|
12
|
+
constructor(context?: T);
|
|
13
|
+
/**渲染模板
|
|
14
|
+
* @param template - 要渲染的 Handlebars 模板字符串
|
|
15
|
+
* @returns 渲染后的字符串
|
|
16
|
+
*/
|
|
17
|
+
render(template: string): string;
|
|
18
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Hbs = void 0;
|
|
7
|
+
const UtilLogger_1 = require("../UtilLogger");
|
|
8
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
9
|
+
/**文本模板渲染器
|
|
10
|
+
* @template T - 上下文对象的类型,默认为记录类型
|
|
11
|
+
*/
|
|
12
|
+
class Hbs {
|
|
13
|
+
hbs = handlebars_1.default.create();
|
|
14
|
+
context;
|
|
15
|
+
/**创建一个新的 Hbs 实例
|
|
16
|
+
* @param context - 上下文对象,用于在渲染模板时提供数据
|
|
17
|
+
*/
|
|
18
|
+
constructor(context = {}) {
|
|
19
|
+
this.context = context;
|
|
20
|
+
this.hbs.registerHelper('def', (name, options) => {
|
|
21
|
+
const value = options.fn(this.context).trim();
|
|
22
|
+
this.context[name] = value;
|
|
23
|
+
return '';
|
|
24
|
+
});
|
|
25
|
+
this.hbs.registerHelper('defli', (name, options) => {
|
|
26
|
+
const rawvalue = options.fn(this.context);
|
|
27
|
+
const values = rawvalue
|
|
28
|
+
.split('[[br]]')
|
|
29
|
+
.map(value => value.trim());
|
|
30
|
+
this.context[name] = values;
|
|
31
|
+
return '';
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**渲染模板
|
|
35
|
+
* @param template - 要渲染的 Handlebars 模板字符串
|
|
36
|
+
* @returns 渲染后的字符串
|
|
37
|
+
*/
|
|
38
|
+
render(template) {
|
|
39
|
+
try {
|
|
40
|
+
return this.hbs.compile(template)(this.context);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
UtilLogger_1.SLogger.error(e);
|
|
44
|
+
return '';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.Hbs = Hbs;
|
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
2
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Stream = exports.SmartCache = exports.PromiseQueue = exports.Piper = exports.DLinkedList = exports.
|
|
17
|
+
exports.Stream = exports.SmartCache = exports.PromiseQueue = exports.Piper = exports.DLinkedList = exports.Bridge = void 0;
|
|
18
|
+
__exportStar(require("./Hbs"), exports);
|
|
4
19
|
//#region UtilClass转导
|
|
5
20
|
var js_utils_1 = require("@zwa73/js-utils");
|
|
6
21
|
Object.defineProperty(exports, "Bridge", { enumerable: true, get: function () { return js_utils_1.Bridge; } });
|
|
7
|
-
Object.defineProperty(exports, "Hbs", { enumerable: true, get: function () { return js_utils_1.Hbs; } });
|
|
8
22
|
Object.defineProperty(exports, "DLinkedList", { enumerable: true, get: function () { return js_utils_1.DLinkedList; } });
|
|
9
23
|
Object.defineProperty(exports, "Piper", { enumerable: true, get: function () { return js_utils_1.Piper; } });
|
|
10
24
|
Object.defineProperty(exports, "PromiseQueue", { enumerable: true, get: function () { return js_utils_1.PromiseQueue; } });
|
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { JToken } from "./UtilInterfaces";
|
|
2
|
+
import { HashAlgorithm } from "./UtilFunctions";
|
|
2
3
|
/**创建文件选项 */
|
|
3
4
|
type CreatePathOpt = Partial<{
|
|
4
5
|
/**创建一个目录 默认 false
|
|
@@ -17,6 +18,8 @@ type EnsurePathExistsOpt = Partial<{
|
|
|
17
18
|
type FileSearchGlobOpt = Partial<{
|
|
18
19
|
/**忽略的文件 默认 undefined */
|
|
19
20
|
ingore: string | string[];
|
|
21
|
+
/**忽略目录 默认 true */
|
|
22
|
+
nodir: boolean;
|
|
20
23
|
}>;
|
|
21
24
|
/**regex搜索选项 */
|
|
22
25
|
type FileSearchRegexOpt = Partial<{
|
|
@@ -24,6 +27,8 @@ type FileSearchRegexOpt = Partial<{
|
|
|
24
27
|
recursive: boolean;
|
|
25
28
|
/**无效项 */
|
|
26
29
|
relative: never;
|
|
30
|
+
/**忽略目录 默认 true */
|
|
31
|
+
nodir: boolean;
|
|
27
32
|
}>;
|
|
28
33
|
/**json文件加载选项 */
|
|
29
34
|
type LoadJsonFileOpt<T> = Partial<{
|
|
@@ -187,5 +192,23 @@ export declare namespace UtilFT {
|
|
|
187
192
|
* @param filePath - 需要验证的文件路径
|
|
188
193
|
*/
|
|
189
194
|
function isValidFilePath(filePath: string): boolean;
|
|
195
|
+
/**计算文件的部分md5
|
|
196
|
+
* @param filePath - 目标文件路径
|
|
197
|
+
* @param opt - 可选参数
|
|
198
|
+
* @param opt.algorithm - hash算法 默认md5
|
|
199
|
+
* @param opt.sampled - 抽取部分计算 默认false
|
|
200
|
+
* @param opt.chunkSize - 抽取的块大小 默认1024
|
|
201
|
+
* @param opt.chunkCount - 抽取的块的数量 默认10
|
|
202
|
+
*/
|
|
203
|
+
function calculateHash(filePath: string, opt?: {
|
|
204
|
+
/**hash算法 默认md5 */
|
|
205
|
+
algorithm?: HashAlgorithm;
|
|
206
|
+
/**抽取部分计算 默认false */
|
|
207
|
+
sampled?: boolean;
|
|
208
|
+
/**抽取的块大小 默认1024 */
|
|
209
|
+
chunkSize?: number;
|
|
210
|
+
/**抽取的块的数量 默认10 */
|
|
211
|
+
chunkCount?: number;
|
|
212
|
+
}): Promise<string>;
|
|
190
213
|
}
|
|
191
214
|
export {};
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -34,6 +34,7 @@ const UtilLogger_1 = require("./UtilLogger");
|
|
|
34
34
|
const JSON5 = __importStar(require("json5"));
|
|
35
35
|
const glob_1 = require("glob");
|
|
36
36
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
37
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
37
38
|
/**文件工具 */
|
|
38
39
|
var UtilFT;
|
|
39
40
|
(function (UtilFT) {
|
|
@@ -279,7 +280,7 @@ var UtilFT;
|
|
|
279
280
|
* @returns 文件名路径数组
|
|
280
281
|
*/
|
|
281
282
|
async function fileSearchRegex(dir, traitRegex, opt) {
|
|
282
|
-
const fixopt = Object.assign({ recursive: true }, opt);
|
|
283
|
+
const fixopt = Object.assign({ recursive: true, nodir: true }, opt);
|
|
283
284
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
284
285
|
const regex = new RegExp(traitRegex);
|
|
285
286
|
const recursive = async (curPath) => {
|
|
@@ -289,8 +290,11 @@ var UtilFT;
|
|
|
289
290
|
const subFilePath = pathe_1.default.join(subDir, subFile.name);
|
|
290
291
|
const nextPath = pathe_1.default.join(curPath, subFile.name);
|
|
291
292
|
//判断是否是文件夹,递归调用
|
|
292
|
-
if (subFile.isDirectory() && fixopt.recursive)
|
|
293
|
-
|
|
293
|
+
if (subFile.isDirectory() && fixopt.recursive) {
|
|
294
|
+
if (fixopt.nodir)
|
|
295
|
+
return recursive(nextPath);
|
|
296
|
+
return [nextPath, ...await recursive(nextPath)];
|
|
297
|
+
}
|
|
294
298
|
if (regex.test(nextPath))
|
|
295
299
|
return [subFilePath];
|
|
296
300
|
return [undefined];
|
|
@@ -310,7 +314,7 @@ var UtilFT;
|
|
|
310
314
|
* @returns 文件名路径数组
|
|
311
315
|
*/
|
|
312
316
|
function fileSearchRegexSync(dir, traitRegex, opt) {
|
|
313
|
-
const fixopt = Object.assign({ recursive: true }, opt);
|
|
317
|
+
const fixopt = Object.assign({ recursive: true, nodir: true }, opt);
|
|
314
318
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
315
319
|
const regex = new RegExp(traitRegex);
|
|
316
320
|
const recursive = (curPath) => {
|
|
@@ -320,8 +324,11 @@ var UtilFT;
|
|
|
320
324
|
const subFilePath = pathe_1.default.join(subDir, subFile.name);
|
|
321
325
|
const nextPath = pathe_1.default.join(curPath, subFile.name);
|
|
322
326
|
//判断是否是文件夹,递归调用
|
|
323
|
-
if (subFile.isDirectory() && fixopt.recursive)
|
|
324
|
-
|
|
327
|
+
if (subFile.isDirectory() && fixopt.recursive) {
|
|
328
|
+
if (fixopt.nodir)
|
|
329
|
+
return recursive(nextPath);
|
|
330
|
+
return [nextPath, ...recursive(nextPath)];
|
|
331
|
+
}
|
|
325
332
|
if (regex.test(nextPath))
|
|
326
333
|
return [subFilePath];
|
|
327
334
|
return [undefined];
|
|
@@ -343,9 +350,9 @@ var UtilFT;
|
|
|
343
350
|
async function fileSearchGlob(dir, globPattern, opt) {
|
|
344
351
|
const fixedPath = typeof globPattern === "string"
|
|
345
352
|
? pathe_1.default.join(dir, globPattern)
|
|
346
|
-
: globPattern.map(
|
|
347
|
-
return (await (0, glob_1.glob)(fixedPath, {
|
|
348
|
-
.map(
|
|
353
|
+
: globPattern.map(p => pathe_1.default.join(dir, p));
|
|
354
|
+
return (await (0, glob_1.glob)(fixedPath, { absolute: true, ...(opt ?? {}) }))
|
|
355
|
+
.map(filePath => pathe_1.default.normalize(filePath));
|
|
349
356
|
}
|
|
350
357
|
UtilFT.fileSearchGlob = fileSearchGlob;
|
|
351
358
|
/**搜索符合Glob匹配的文件 同步版本
|
|
@@ -358,9 +365,9 @@ var UtilFT;
|
|
|
358
365
|
function fileSearchGlobSync(dir, globPattern, opt) {
|
|
359
366
|
const fixedPath = typeof globPattern === "string"
|
|
360
367
|
? pathe_1.default.join(dir, globPattern)
|
|
361
|
-
: globPattern.map(
|
|
362
|
-
return (0, glob_1.globSync)(fixedPath, {
|
|
363
|
-
.map(
|
|
368
|
+
: globPattern.map(p => pathe_1.default.join(dir, p));
|
|
369
|
+
return (0, glob_1.globSync)(fixedPath, { absolute: true, ...(opt ?? {}) })
|
|
370
|
+
.map(filePath => pathe_1.default.normalize(filePath));
|
|
364
371
|
}
|
|
365
372
|
UtilFT.fileSearchGlobSync = fileSearchGlobSync;
|
|
366
373
|
/**是一个有效的文件路径
|
|
@@ -377,4 +384,47 @@ var UtilFT;
|
|
|
377
384
|
return true;
|
|
378
385
|
}
|
|
379
386
|
UtilFT.isValidFilePath = isValidFilePath;
|
|
387
|
+
/**计算文件的部分md5
|
|
388
|
+
* @param filePath - 目标文件路径
|
|
389
|
+
* @param opt - 可选参数
|
|
390
|
+
* @param opt.algorithm - hash算法 默认md5
|
|
391
|
+
* @param opt.sampled - 抽取部分计算 默认false
|
|
392
|
+
* @param opt.chunkSize - 抽取的块大小 默认1024
|
|
393
|
+
* @param opt.chunkCount - 抽取的块的数量 默认10
|
|
394
|
+
*/
|
|
395
|
+
async function calculateHash(filePath, opt) {
|
|
396
|
+
const { algorithm = "md5", chunkSize = 1024, chunkCount = 10, sampled = false } = opt ?? {};
|
|
397
|
+
const chash = async (filePath) => new Promise((resolve, reject) => {
|
|
398
|
+
const hash = crypto_1.default.createHash(algorithm);
|
|
399
|
+
const stream = fs.createReadStream(filePath);
|
|
400
|
+
stream.on("data", (data) => hash.update(data));
|
|
401
|
+
stream.on("end", () => resolve(hash.digest("hex")));
|
|
402
|
+
stream.on("error", reject);
|
|
403
|
+
});
|
|
404
|
+
if (!sampled)
|
|
405
|
+
return chash(filePath);
|
|
406
|
+
const stats = await fs.promises.stat(filePath);
|
|
407
|
+
const totalSize = stats.size;
|
|
408
|
+
//抽取量大于文件大小则直接计算完整hash
|
|
409
|
+
if (totalSize < chunkSize * chunkCount)
|
|
410
|
+
return chash(filePath);
|
|
411
|
+
const poslist = Array.from({ length: chunkCount }, (_, i) => Math.floor((totalSize / chunkCount) * i));
|
|
412
|
+
const hash = crypto_1.default.createHash(algorithm);
|
|
413
|
+
for (const pos of poslist) {
|
|
414
|
+
const buffer = await new Promise((resolve, reject) => {
|
|
415
|
+
const stream = fs.createReadStream(filePath, {
|
|
416
|
+
start: pos,
|
|
417
|
+
end: Math.min(pos + chunkSize - 1, totalSize - 1),
|
|
418
|
+
highWaterMark: chunkSize,
|
|
419
|
+
});
|
|
420
|
+
const chunks = [];
|
|
421
|
+
stream.on("data", (chunk) => chunks.push(chunk));
|
|
422
|
+
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
423
|
+
stream.on("error", reject);
|
|
424
|
+
});
|
|
425
|
+
hash.update(buffer);
|
|
426
|
+
}
|
|
427
|
+
return hash.digest("hex");
|
|
428
|
+
}
|
|
429
|
+
UtilFT.calculateHash = calculateHash;
|
|
380
430
|
})(UtilFT || (exports.UtilFT = UtilFT = {}));
|
package/dist/UtilFunctions.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { LogLevel } from "./UtilInterfaces";
|
|
2
2
|
import { JsFunc } from "@zwa73/js-utils";
|
|
3
|
-
import { ComposedClass } from "@zwa73/modular-mixer";
|
|
4
3
|
export { PromiseRetries, PromiseRetryResult } from "@zwa73/js-utils";
|
|
5
|
-
declare const
|
|
6
|
-
type
|
|
4
|
+
declare const HashAlgorithmList: readonly ["RSA-MD5", "RSA-RIPEMD160", "RSA-SHA1", "RSA-SHA1-2", "RSA-SHA224", "RSA-SHA256", "RSA-SHA3-224", "RSA-SHA3-256", "RSA-SHA3-384", "RSA-SHA3-512", "RSA-SHA384", "RSA-SHA512", "RSA-SHA512/224", "RSA-SHA512/256", "RSA-SM3", "blake2b512", "blake2s256", "id-rsassa-pkcs1-v1_5-with-sha3-224", "id-rsassa-pkcs1-v1_5-with-sha3-256", "id-rsassa-pkcs1-v1_5-with-sha3-384", "id-rsassa-pkcs1-v1_5-with-sha3-512", "md5", "md5-sha1", "md5WithRSAEncryption", "ripemd", "ripemd160", "ripemd160WithRSA", "rmd160", "sha1", "sha1WithRSAEncryption", "sha224", "sha224WithRSAEncryption", "sha256", "sha256WithRSAEncryption", "sha3-224", "sha3-256", "sha3-384", "sha3-512", "sha384", "sha384WithRSAEncryption", "sha512", "sha512-224", "sha512-224WithRSAEncryption", "sha512-256", "sha512-256WithRSAEncryption", "sha512WithRSAEncryption", "shake128", "shake256", "sm3", "sm3WithRSAEncryption", "ssl3-md5", "ssl3-sha1"];
|
|
5
|
+
export type HashAlgorithm = typeof HashAlgorithmList[number];
|
|
7
6
|
/**执行选项 */
|
|
8
7
|
type ExecOpt = Partial<{
|
|
9
8
|
outlvl: LogLevel;
|
|
@@ -20,7 +19,10 @@ declare class _UtilFunc {
|
|
|
20
19
|
* @param method - hash算法
|
|
21
20
|
* @returns hash
|
|
22
21
|
*/
|
|
23
|
-
static calcHash(str: string,
|
|
22
|
+
static calcHash(str: string, opt?: {
|
|
23
|
+
/**hash算法 默认md5 */
|
|
24
|
+
algorithm?: HashAlgorithm;
|
|
25
|
+
}): string;
|
|
24
26
|
/**是否需要检查环境 */
|
|
25
27
|
private static checkEnv;
|
|
26
28
|
/**是否有正在运行的exec */
|
|
@@ -58,5 +60,5 @@ declare class _UtilFunc {
|
|
|
58
60
|
*/
|
|
59
61
|
static dynamicImport(moduleName: string): Promise<any>;
|
|
60
62
|
}
|
|
61
|
-
export declare const UtilFunc: ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "getTime" | "initField" | "initObject" | "unifyApply" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "splitToChunk" | "mergeFromChunk" | "
|
|
63
|
+
export declare const UtilFunc: import("@zwa73/modular-mixer").ComposedClass<typeof _UtilFunc, typeof JsFunc, "__jsUtils", "match" | "failed" | "success" | "prototype" | "genUUID" | "range" | "concurrency" | "getTime" | "initField" | "initObject" | "unifyApply" | "sleep" | "getNeverResolvedPromise" | "retryPromise" | "timelimitPromise" | "mapEntries" | "eachField" | "stringifyJToken" | "assertType" | "assertLiteral" | "deepClone" | "isSafeNumber" | "dedent" | "throwError" | "getFuncLoc" | "cachePool" | "memoize" | "asyncize" | "splitToChunk" | "mergeFromChunk" | "structEqual" | "outcome" | "isStatus" | "eitherize" | "parseSrtTime" | "formatSrtTime" | "parseSrt" | "createSrt" | "ivk" | "s2l" | "l2s">;
|
|
62
64
|
export type UtilFunc = typeof UtilFunc;
|
package/dist/UtilFunctions.js
CHANGED
|
@@ -35,7 +35,21 @@ const UtilFileTools_1 = require("./UtilFileTools");
|
|
|
35
35
|
const UtilLogger_1 = require("./UtilLogger");
|
|
36
36
|
const js_utils_1 = require("@zwa73/js-utils");
|
|
37
37
|
const modular_mixer_1 = require("@zwa73/modular-mixer");
|
|
38
|
-
const
|
|
38
|
+
const HashAlgorithmList = [
|
|
39
|
+
'RSA-MD5', 'RSA-RIPEMD160', 'RSA-SHA1', 'RSA-SHA1-2', 'RSA-SHA224',
|
|
40
|
+
'RSA-SHA256', 'RSA-SHA3-224', 'RSA-SHA3-256', 'RSA-SHA3-384', 'RSA-SHA3-512',
|
|
41
|
+
'RSA-SHA384', 'RSA-SHA512', 'RSA-SHA512/224', 'RSA-SHA512/256', 'RSA-SM3',
|
|
42
|
+
'blake2b512', 'blake2s256', 'id-rsassa-pkcs1-v1_5-with-sha3-224',
|
|
43
|
+
'id-rsassa-pkcs1-v1_5-with-sha3-256', 'id-rsassa-pkcs1-v1_5-with-sha3-384',
|
|
44
|
+
'id-rsassa-pkcs1-v1_5-with-sha3-512', 'md5', 'md5-sha1',
|
|
45
|
+
'md5WithRSAEncryption', 'ripemd', 'ripemd160', 'ripemd160WithRSA', 'rmd160',
|
|
46
|
+
'sha1', 'sha1WithRSAEncryption', 'sha224', 'sha224WithRSAEncryption',
|
|
47
|
+
'sha256', 'sha256WithRSAEncryption', 'sha3-224', 'sha3-256', 'sha3-384',
|
|
48
|
+
'sha3-512', 'sha384', 'sha384WithRSAEncryption', 'sha512', 'sha512-224',
|
|
49
|
+
'sha512-224WithRSAEncryption', 'sha512-256', 'sha512-256WithRSAEncryption',
|
|
50
|
+
'sha512WithRSAEncryption', 'shake128', 'shake256', 'sm3',
|
|
51
|
+
'sm3WithRSAEncryption', 'ssl3-md5', 'ssl3-sha1'
|
|
52
|
+
];
|
|
39
53
|
/**常用函数 */
|
|
40
54
|
class _UtilFunc {
|
|
41
55
|
/**计算Hash
|
|
@@ -43,8 +57,9 @@ class _UtilFunc {
|
|
|
43
57
|
* @param method - hash算法
|
|
44
58
|
* @returns hash
|
|
45
59
|
*/
|
|
46
|
-
static calcHash(str,
|
|
47
|
-
|
|
60
|
+
static calcHash(str, opt) {
|
|
61
|
+
const { algorithm = "md5" } = opt ?? {};
|
|
62
|
+
return crypto.createHash(algorithm).update(str).digest('hex');
|
|
48
63
|
}
|
|
49
64
|
/**是否需要检查环境 */
|
|
50
65
|
static checkEnv = true;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.223",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"license": "ISC",
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@deepkit/type": "^1.0.1-alpha.153",
|
|
20
|
-
"@zwa73/js-utils": "
|
|
21
|
-
"@zwa73/modular-mixer": "
|
|
20
|
+
"@zwa73/js-utils": "*",
|
|
21
|
+
"@zwa73/modular-mixer": "*",
|
|
22
22
|
"form-data": "^4.0.2",
|
|
23
23
|
"glob": "^10.4.1",
|
|
24
24
|
"handlebars": "^4.7.8",
|
package/dist/UtilClass.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { BridgeInterface, Bridge, Hbs, DListMiddleNode, DListHeadNode, DListTailNode, DListNode, DListMaybeNode, DLinkedList, Piper, PromoseQueueOption, PromiseQueue, SmartCache, Stream } from "@zwa73/js-utils";
|