@zwa73/utils 1.0.219 → 1.0.220
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/UtilFileTools.js +38 -30
- package/dist/UtilHttp.d.ts +37 -5
- package/dist/UtilHttp.js +100 -14
- package/package.json +1 -1
package/dist/UtilFileTools.js
CHANGED
|
@@ -279,23 +279,27 @@ var UtilFT;
|
|
|
279
279
|
* @returns 文件名路径数组
|
|
280
280
|
*/
|
|
281
281
|
async function fileSearchRegex(dir, traitRegex, opt) {
|
|
282
|
-
const
|
|
283
|
-
const outArray = [];
|
|
284
|
-
const subFiles = await fs.promises.readdir(dir, { withFileTypes: true });
|
|
282
|
+
const fixopt = Object.assign({ recursive: true }, opt);
|
|
285
283
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
286
284
|
const regex = new RegExp(traitRegex);
|
|
287
|
-
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
285
|
+
const recursive = async (curPath) => {
|
|
286
|
+
const subDir = pathe_1.default.join(dir, curPath);
|
|
287
|
+
const subFiles = await fs.promises.readdir(subDir, { withFileTypes: true });
|
|
288
|
+
const matchFiles = await Promise.all(subFiles.map(async (subFile) => {
|
|
289
|
+
const subFilePath = pathe_1.default.join(subDir, subFile.name);
|
|
290
|
+
const nextPath = pathe_1.default.join(curPath, subFile.name);
|
|
291
|
+
//判断是否是文件夹,递归调用
|
|
292
|
+
if (subFile.isDirectory() && fixopt.recursive)
|
|
293
|
+
return recursive(nextPath);
|
|
294
|
+
if (regex.test(nextPath))
|
|
295
|
+
return [subFilePath];
|
|
296
|
+
return [undefined];
|
|
297
|
+
}));
|
|
298
|
+
return matchFiles.flat()
|
|
299
|
+
.filter(v => v != undefined)
|
|
300
|
+
.map(fp => pathe_1.default.normalize(fp));
|
|
301
|
+
};
|
|
302
|
+
return recursive('');
|
|
299
303
|
}
|
|
300
304
|
UtilFT.fileSearchRegex = fileSearchRegex;
|
|
301
305
|
/**搜索路径符合正则表达式的文件 同步版本
|
|
@@ -306,23 +310,27 @@ var UtilFT;
|
|
|
306
310
|
* @returns 文件名路径数组
|
|
307
311
|
*/
|
|
308
312
|
function fileSearchRegexSync(dir, traitRegex, opt) {
|
|
309
|
-
const
|
|
310
|
-
const outArray = [];
|
|
311
|
-
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
313
|
+
const fixopt = Object.assign({ recursive: true }, opt);
|
|
312
314
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
313
315
|
const regex = new RegExp(traitRegex);
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
316
|
+
const recursive = (curPath) => {
|
|
317
|
+
const subDir = pathe_1.default.join(dir, curPath);
|
|
318
|
+
const subFiles = fs.readdirSync(subDir, { withFileTypes: true });
|
|
319
|
+
const matchFiles = subFiles.map(subFile => {
|
|
320
|
+
const subFilePath = pathe_1.default.join(subDir, subFile.name);
|
|
321
|
+
const nextPath = pathe_1.default.join(curPath, subFile.name);
|
|
322
|
+
//判断是否是文件夹,递归调用
|
|
323
|
+
if (subFile.isDirectory() && fixopt.recursive)
|
|
324
|
+
return recursive(nextPath);
|
|
325
|
+
if (regex.test(nextPath))
|
|
326
|
+
return [subFilePath];
|
|
327
|
+
return [undefined];
|
|
328
|
+
});
|
|
329
|
+
return matchFiles.flat()
|
|
330
|
+
.filter(v => v != undefined)
|
|
331
|
+
.map(fp => pathe_1.default.normalize(fp));
|
|
332
|
+
};
|
|
333
|
+
return recursive('');
|
|
326
334
|
}
|
|
327
335
|
UtilFT.fileSearchRegexSync = fileSearchRegexSync;
|
|
328
336
|
/**搜索符合Glob匹配的文件
|
package/dist/UtilHttp.d.ts
CHANGED
|
@@ -192,7 +192,7 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
192
192
|
acceptNone(): UtilHttp<D, S, typeof AcceptNoneProc>;
|
|
193
193
|
/**自定的接收数据类型*/
|
|
194
194
|
acceptRaw<AD, AT>(proc: AcceptProc<AD, AT>): UtilHttp<D, S, typeof proc>;
|
|
195
|
-
|
|
195
|
+
/**预设的发送数据类型 */
|
|
196
196
|
send<T extends SendType>(t: T): {
|
|
197
197
|
readonly json: UtilHttp<D & {
|
|
198
198
|
headers: {
|
|
@@ -205,30 +205,62 @@ export declare class UtilHttp<D extends Partial<RequestOption> & Required<Pick<R
|
|
|
205
205
|
"Content-Type": "multipart/form-data";
|
|
206
206
|
};
|
|
207
207
|
}, SendProc<[FormData]>, A>;
|
|
208
|
+
readonly form: UtilHttp<D & {
|
|
209
|
+
headers: {
|
|
210
|
+
"Content-Type": "application/x-www-form-urlencoded";
|
|
211
|
+
};
|
|
212
|
+
}, SendProc<[QueryRequestData]>, A>;
|
|
213
|
+
readonly file: UtilHttp<D & {
|
|
214
|
+
headers: {
|
|
215
|
+
"Content-Type": "multipart/form-data";
|
|
216
|
+
};
|
|
217
|
+
}, SendProc<[string, (string | undefined)?]>, A>;
|
|
208
218
|
readonly none: UtilHttp<D, SendProc<[]>, A>;
|
|
209
219
|
}[T];
|
|
210
|
-
|
|
220
|
+
/**发送json
|
|
221
|
+
* 请求参数为 (token:JToken)
|
|
222
|
+
*/
|
|
211
223
|
sendJson(): UtilHttp<D & {
|
|
212
224
|
headers: {
|
|
213
225
|
"Content-Type": "application/json";
|
|
214
226
|
};
|
|
215
227
|
}, SendProc<[JToken]>, A>;
|
|
216
|
-
/**利用 appendQuery 直接将数据附加在path上发送请求
|
|
228
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求
|
|
229
|
+
* 请求参数为 (form:QueryRequestData)
|
|
230
|
+
*/
|
|
217
231
|
sendQuery(): UtilHttp<D, SendProc<[QueryRequestData]>, A>;
|
|
232
|
+
/**发送表单数据
|
|
233
|
+
* 请求参数为 (formData: FormData)
|
|
234
|
+
*/
|
|
218
235
|
sendFormData(): UtilHttp<D & {
|
|
219
236
|
headers: {
|
|
220
237
|
"Content-Type": "multipart/form-data";
|
|
221
238
|
};
|
|
222
239
|
}, SendProc<[FormData]>, A>;
|
|
240
|
+
/**发送表单
|
|
241
|
+
* 请求参数为 (form:QueryRequestData)
|
|
242
|
+
*/
|
|
243
|
+
sendForm(): UtilHttp<D & {
|
|
244
|
+
headers: {
|
|
245
|
+
"Content-Type": "application/x-www-form-urlencoded";
|
|
246
|
+
};
|
|
247
|
+
}, SendProc<[QueryRequestData]>, A>;
|
|
248
|
+
/**发送文件
|
|
249
|
+
* 请求参数为 (filepath:string, filename?: string)
|
|
250
|
+
*/
|
|
251
|
+
sendFile(): UtilHttp<D & {
|
|
252
|
+
headers: {
|
|
253
|
+
"Content-Type": "multipart/form-data";
|
|
254
|
+
};
|
|
255
|
+
}, SendProc<[string, (string | undefined)?]>, A>;
|
|
223
256
|
sendNone(): UtilHttp<D, typeof SendNoneProc, A>;
|
|
224
|
-
|
|
257
|
+
/**自定的发送数据类型 */
|
|
225
258
|
sendRaw<T extends any[]>(proc: SendProc<T>): UtilHttp<D, typeof proc, A>;
|
|
226
259
|
/**发送请求
|
|
227
260
|
* @param datas - 数据对象
|
|
228
261
|
*/
|
|
229
262
|
once(...datas: {} extends RequiredOnly<PartialOption<RequestOption, D>> ? SendParams<S> : [Error & "RequestOption不完整, 请使用 finalize 完成必要项"]): Promise<ParseResult<A>>;
|
|
230
263
|
/**重复发送网络请求
|
|
231
|
-
* @param option - 网络请求选项
|
|
232
264
|
* @param verify - 有效性验证函数
|
|
233
265
|
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
234
266
|
* @param datas - 数据对象
|
package/dist/UtilHttp.js
CHANGED
|
@@ -10,9 +10,12 @@ const url_1 = require("url");
|
|
|
10
10
|
const UtilLogger_1 = require("./UtilLogger");
|
|
11
11
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
12
12
|
const querystring_1 = __importDefault(require("querystring"));
|
|
13
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
13
14
|
const http_proxy_agent_1 = __importDefault(require("http-proxy-agent"));
|
|
14
15
|
const https_proxy_agent_1 = __importDefault(require("https-proxy-agent"));
|
|
15
16
|
const js_utils_1 = require("@zwa73/js-utils");
|
|
17
|
+
const fs_1 = __importDefault(require("fs"));
|
|
18
|
+
const pathe_1 = __importDefault(require("pathe"));
|
|
16
19
|
const AcceptTypeList = ["json", "string"];
|
|
17
20
|
const SendTypeList = ["json", "query", "formData", "none"];
|
|
18
21
|
const SendNoneProc = {
|
|
@@ -206,27 +209,32 @@ class UtilHttp {
|
|
|
206
209
|
}
|
|
207
210
|
//#endregion
|
|
208
211
|
//#region 发送数据类型
|
|
209
|
-
|
|
212
|
+
/**预设的发送数据类型 */
|
|
210
213
|
send(t) {
|
|
211
214
|
const map = {
|
|
212
215
|
'json': this.sendJson(),
|
|
213
216
|
'query': this.sendQuery(),
|
|
214
217
|
'formData': this.sendFormData(),
|
|
218
|
+
'form': this.sendForm(),
|
|
219
|
+
'file': this.sendFile(),
|
|
215
220
|
'none': this.sendNone(),
|
|
216
221
|
};
|
|
217
222
|
return map[t];
|
|
218
223
|
}
|
|
219
|
-
|
|
224
|
+
/**发送json
|
|
225
|
+
* 请求参数为 (token:JToken)
|
|
226
|
+
*/
|
|
220
227
|
sendJson() {
|
|
221
228
|
const proc = {
|
|
222
|
-
proc: (opt,
|
|
229
|
+
proc: (opt, token) => {
|
|
223
230
|
const { method } = opt;
|
|
224
231
|
const isPost = (method == "POST");
|
|
232
|
+
const data = JSON.stringify(token);
|
|
225
233
|
this._data.headers ??= {};
|
|
226
|
-
this._data.headers['Content-Length'] = Buffer.byteLength(
|
|
234
|
+
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
227
235
|
const procReq = (req) => {
|
|
228
236
|
if (isPost)
|
|
229
|
-
req.write(
|
|
237
|
+
req.write(data);
|
|
230
238
|
req.end();
|
|
231
239
|
};
|
|
232
240
|
return procReq;
|
|
@@ -237,11 +245,13 @@ class UtilHttp {
|
|
|
237
245
|
this._data.headers['Content-Type'] = 'application/json';
|
|
238
246
|
return this;
|
|
239
247
|
}
|
|
240
|
-
/**利用 appendQuery 直接将数据附加在path上发送请求
|
|
248
|
+
/**利用 appendQuery 直接将数据附加在path上发送请求
|
|
249
|
+
* 请求参数为 (form:QueryRequestData)
|
|
250
|
+
*/
|
|
241
251
|
sendQuery() {
|
|
242
252
|
const proc = {
|
|
243
|
-
proc: (opt,
|
|
244
|
-
opt.path = UtilHttp.buildQuery(opt.path ?? '',
|
|
253
|
+
proc: (opt, form) => {
|
|
254
|
+
opt.path = UtilHttp.buildQuery(opt.path ?? '', form);
|
|
245
255
|
const procReq = (req) => void req.end();
|
|
246
256
|
return procReq;
|
|
247
257
|
}
|
|
@@ -249,15 +259,62 @@ class UtilHttp {
|
|
|
249
259
|
this._send = proc;
|
|
250
260
|
return this;
|
|
251
261
|
}
|
|
262
|
+
/**发送表单数据
|
|
263
|
+
* 请求参数为 (formData: FormData)
|
|
264
|
+
*/
|
|
252
265
|
sendFormData() {
|
|
253
266
|
const proc = {
|
|
254
267
|
proc: (opt, formData) => {
|
|
255
268
|
opt.headers = formData.getHeaders();
|
|
256
269
|
const procReq = (req) => {
|
|
257
270
|
formData.pipe(req);
|
|
271
|
+
};
|
|
272
|
+
return procReq;
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
this._send = proc;
|
|
276
|
+
this._data.headers ??= {};
|
|
277
|
+
this._data.headers['Content-Type'] = 'multipart/form-data';
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
/**发送表单
|
|
281
|
+
* 请求参数为 (form:QueryRequestData)
|
|
282
|
+
*/
|
|
283
|
+
sendForm() {
|
|
284
|
+
const proc = {
|
|
285
|
+
proc: (opt, form) => {
|
|
286
|
+
const { method } = opt;
|
|
287
|
+
const isPost = (method == "POST");
|
|
288
|
+
const data = querystring_1.default.stringify(form);
|
|
289
|
+
this._data.headers ??= {};
|
|
290
|
+
this._data.headers['Content-Length'] = Buffer.byteLength(data);
|
|
291
|
+
const procReq = (req) => {
|
|
292
|
+
if (isPost)
|
|
293
|
+
req.write(data);
|
|
258
294
|
req.end();
|
|
259
295
|
};
|
|
260
296
|
return procReq;
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
this._send = proc;
|
|
300
|
+
this._data.headers ??= {};
|
|
301
|
+
this._data.headers['Content-Type'] = 'application/x-www-form-urlencoded';
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
/**发送文件
|
|
305
|
+
* 请求参数为 (filepath:string, filename?: string)
|
|
306
|
+
*/
|
|
307
|
+
sendFile() {
|
|
308
|
+
const proc = {
|
|
309
|
+
proc: (opt, filepath, filename) => {
|
|
310
|
+
const formData = new form_data_1.default();
|
|
311
|
+
filename = filename ?? pathe_1.default.basename(filepath);
|
|
312
|
+
formData.append(filename, fs_1.default.createReadStream(filepath));
|
|
313
|
+
opt.headers = formData.getHeaders();
|
|
314
|
+
const procReq = (req) => {
|
|
315
|
+
formData.pipe(req);
|
|
316
|
+
};
|
|
317
|
+
return procReq;
|
|
261
318
|
},
|
|
262
319
|
};
|
|
263
320
|
this._send = proc;
|
|
@@ -269,7 +326,7 @@ class UtilHttp {
|
|
|
269
326
|
this._send = SendNoneProc;
|
|
270
327
|
return this;
|
|
271
328
|
}
|
|
272
|
-
|
|
329
|
+
/**自定的发送数据类型 */
|
|
273
330
|
sendRaw(proc) {
|
|
274
331
|
this._send = proc;
|
|
275
332
|
return this;
|
|
@@ -286,7 +343,6 @@ class UtilHttp {
|
|
|
286
343
|
return parse(res);
|
|
287
344
|
}
|
|
288
345
|
/**重复发送网络请求
|
|
289
|
-
* @param option - 网络请求选项
|
|
290
346
|
* @param verify - 有效性验证函数
|
|
291
347
|
* @param retries - 重试选项 默认 延迟:1000ms 间隔:180_000ms 重试:3次
|
|
292
348
|
* @param datas - 数据对象
|
|
@@ -387,10 +443,40 @@ class UtilHttp {
|
|
|
387
443
|
exports.UtilHttp = UtilHttp;
|
|
388
444
|
if (false)
|
|
389
445
|
void ((async () => {
|
|
390
|
-
const
|
|
446
|
+
const tool = UtilHttp
|
|
391
447
|
.url('https://httpbin.org/post')
|
|
392
|
-
.
|
|
393
|
-
.finalize({ timeout: 10000 })
|
|
448
|
+
.post()
|
|
449
|
+
.finalize({ timeout: 10000 });
|
|
450
|
+
//json
|
|
451
|
+
const sj = await tool.clone()
|
|
452
|
+
.sendJson()
|
|
453
|
+
.acceptJson()
|
|
454
|
+
.once({ test: 1 });
|
|
455
|
+
console.log(sj);
|
|
456
|
+
//form
|
|
457
|
+
const sf = await tool.clone()
|
|
458
|
+
.sendForm()
|
|
459
|
+
.acceptJson()
|
|
460
|
+
.once({ test: 1 });
|
|
461
|
+
console.log(sf);
|
|
462
|
+
//query
|
|
463
|
+
const sq = await tool.clone()
|
|
464
|
+
.sendQuery()
|
|
465
|
+
.acceptJson()
|
|
394
466
|
.once({ test: 1 });
|
|
395
|
-
console.log(
|
|
467
|
+
console.log(sq);
|
|
468
|
+
const filepath = pathe_1.default.join(__dirname, '..', 'input.wav');
|
|
469
|
+
//formData
|
|
470
|
+
//const form = new FormData();
|
|
471
|
+
//form.append('name', 'input.wav');
|
|
472
|
+
//form.append('file', fs.createReadStream(path.join(__dirname,'..','input.wav')));
|
|
473
|
+
//const sfd = await tool.clone()
|
|
474
|
+
// .sendFormData()
|
|
475
|
+
// .acceptJson()
|
|
476
|
+
// .once(form);
|
|
477
|
+
const sfile = await tool.clone()
|
|
478
|
+
.sendFile()
|
|
479
|
+
.acceptJson()
|
|
480
|
+
.once(filepath);
|
|
481
|
+
console.log(sfile);
|
|
396
482
|
})());
|