@jeniex/utils 1.1.4 → 1.3.0
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/browser/index.cjs +111 -0
- package/browser/index.d.ts +38 -3
- package/browser/index.js +109 -1
- package/node/index.cjs +138 -1
- package/node/index.d.ts +37 -1
- package/node/index.js +132 -3
- package/package.json +1 -1
package/browser/index.cjs
CHANGED
|
@@ -473,6 +473,114 @@ function customizeConsole(debug = false) {
|
|
|
473
473
|
};
|
|
474
474
|
}
|
|
475
475
|
|
|
476
|
+
function createRequest(transport, url, options) {
|
|
477
|
+
const getResponse = async () => {
|
|
478
|
+
return transport(url, options);
|
|
479
|
+
};
|
|
480
|
+
return {
|
|
481
|
+
response: async () => {
|
|
482
|
+
return getResponse();
|
|
483
|
+
},
|
|
484
|
+
blob: async () => {
|
|
485
|
+
const response = await getResponse();
|
|
486
|
+
return response.blob();
|
|
487
|
+
},
|
|
488
|
+
buffer: async () => {
|
|
489
|
+
const response = await getResponse();
|
|
490
|
+
return response.arrayBuffer();
|
|
491
|
+
},
|
|
492
|
+
json: async () => {
|
|
493
|
+
const response = await getResponse();
|
|
494
|
+
return response.json();
|
|
495
|
+
},
|
|
496
|
+
text: async () => {
|
|
497
|
+
const response = await getResponse();
|
|
498
|
+
return response.text();
|
|
499
|
+
},
|
|
500
|
+
document: async () => {
|
|
501
|
+
const response = await getResponse();
|
|
502
|
+
const responseText = await response.text();
|
|
503
|
+
const parser = new DOMParser();
|
|
504
|
+
return parser.parseFromString(responseText, 'text/html');
|
|
505
|
+
},
|
|
506
|
+
multi: async (types) => {
|
|
507
|
+
const response = await getResponse();
|
|
508
|
+
const responses = { response };
|
|
509
|
+
if (types.includes('blob')) {
|
|
510
|
+
responses.blob = await response.clone().blob();
|
|
511
|
+
}
|
|
512
|
+
if (types.includes('buffer')) {
|
|
513
|
+
responses.buffer = await response.clone().arrayBuffer();
|
|
514
|
+
}
|
|
515
|
+
if (types.includes('json')) {
|
|
516
|
+
responses.json = (await response.clone().json());
|
|
517
|
+
}
|
|
518
|
+
if (types.includes('text')) {
|
|
519
|
+
responses.text = await response.clone().text();
|
|
520
|
+
}
|
|
521
|
+
if (types.includes('document')) {
|
|
522
|
+
const responseText = await response.clone().text();
|
|
523
|
+
const parser = new DOMParser();
|
|
524
|
+
responses.document = parser.parseFromString(responseText, 'text/html');
|
|
525
|
+
}
|
|
526
|
+
return responses;
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function isFileURL(url) {
|
|
532
|
+
return url.startsWith('file:///');
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
async function fetchTransport(url, options) {
|
|
536
|
+
const response = await fetch(url, options);
|
|
537
|
+
if (!response.ok && !isFileURL(response.url)) {
|
|
538
|
+
throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
|
|
539
|
+
}
|
|
540
|
+
return response;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function rq(url, options) {
|
|
544
|
+
return createRequest(fetchTransport, url, options);
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
async function gmTransport(url, options) {
|
|
548
|
+
const { method, anonymous, headers, body, timeOut, onProgress } = options ?? {};
|
|
549
|
+
return new Promise((resolve, reject) => {
|
|
550
|
+
GM.xmlHttpRequest({
|
|
551
|
+
url,
|
|
552
|
+
method: method ?? 'GET',
|
|
553
|
+
headers,
|
|
554
|
+
/** Note: To manually set the 'cookie' header, you have to set 'anonymous' to true. */
|
|
555
|
+
anonymous,
|
|
556
|
+
data: body,
|
|
557
|
+
responseType: 'blob',
|
|
558
|
+
timeout: timeOut,
|
|
559
|
+
onprogress: onProgress,
|
|
560
|
+
onload({ response, statusText, status, finalUrl }) {
|
|
561
|
+
const ok = status >= 200 && status < 300;
|
|
562
|
+
if (!ok && !isFileURL(finalUrl)) {
|
|
563
|
+
reject(new Error(`Request to ${url} ended with ${status} status.`));
|
|
564
|
+
return;
|
|
565
|
+
}
|
|
566
|
+
const properResponse = new Response(response, {
|
|
567
|
+
statusText,
|
|
568
|
+
status: isFileURL(finalUrl) ? 200 : status,
|
|
569
|
+
});
|
|
570
|
+
Object.defineProperty(properResponse, 'url', { value: finalUrl });
|
|
571
|
+
resolve(properResponse);
|
|
572
|
+
},
|
|
573
|
+
onerror({ status }) {
|
|
574
|
+
reject(new Error(`Request to ${url} ended with ${status} status.`));
|
|
575
|
+
},
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function rx(url, options) {
|
|
581
|
+
return createRequest(gmTransport, url, options);
|
|
582
|
+
}
|
|
583
|
+
|
|
476
584
|
exports.$ = $;
|
|
477
585
|
exports.$$ = $$;
|
|
478
586
|
exports.addStyle = addStyle;
|
|
@@ -506,6 +614,9 @@ exports.pageLoad = pageLoad;
|
|
|
506
614
|
exports.prompt = prompt;
|
|
507
615
|
exports.remove = remove;
|
|
508
616
|
exports.removeSearchParams = removeSearchParams;
|
|
617
|
+
exports.rq = rq;
|
|
618
|
+
exports.rqc = createRequest;
|
|
619
|
+
exports.rx = rx;
|
|
509
620
|
exports.saveFile = saveFile;
|
|
510
621
|
exports.scriptName = scriptName;
|
|
511
622
|
exports.setSearchParam = setSearchParam;
|
package/browser/index.d.ts
CHANGED
|
@@ -60,9 +60,9 @@ declare function alert(message?: string): void;
|
|
|
60
60
|
declare function confirm(message: string): boolean;
|
|
61
61
|
declare function prompt(message: string, _default?: string): string | null;
|
|
62
62
|
|
|
63
|
-
type Method = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
|
|
63
|
+
type Method$1 = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
|
|
64
64
|
interface FishXOptions {
|
|
65
|
-
method?: Method;
|
|
65
|
+
method?: Method$1;
|
|
66
66
|
anonymous?: boolean;
|
|
67
67
|
headers?: Record<string, string>;
|
|
68
68
|
body?: Blob | FormData | string;
|
|
@@ -168,4 +168,39 @@ interface CustomizedConsole {
|
|
|
168
168
|
|
|
169
169
|
declare function customizeConsole(debug?: boolean): CustomizedConsole;
|
|
170
170
|
|
|
171
|
-
|
|
171
|
+
type Method = 'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
|
|
172
|
+
interface RxOptions {
|
|
173
|
+
method?: Method;
|
|
174
|
+
anonymous?: boolean;
|
|
175
|
+
headers?: Record<string, string>;
|
|
176
|
+
body?: Blob | FormData | string;
|
|
177
|
+
timeOut?: number;
|
|
178
|
+
onProgress?: (response: VMScriptResponseObject<Blob>) => Promise<void> | void;
|
|
179
|
+
}
|
|
180
|
+
interface RqRequest {
|
|
181
|
+
response: () => Promise<Response>;
|
|
182
|
+
blob: () => Promise<Blob>;
|
|
183
|
+
buffer: () => Promise<ArrayBuffer>;
|
|
184
|
+
json: <T = unknown>() => Promise<T>;
|
|
185
|
+
text: () => Promise<string>;
|
|
186
|
+
document: () => Promise<Document>;
|
|
187
|
+
multi: <T = unknown>(types: ResponseType[]) => Promise<Responses<T>>;
|
|
188
|
+
}
|
|
189
|
+
type Transport<TOptions = unknown> = (url: string, options?: TOptions) => Promise<Response>;
|
|
190
|
+
type ResponseType = 'blob' | 'buffer' | 'document' | 'json' | 'text';
|
|
191
|
+
interface Responses<T> {
|
|
192
|
+
response?: Response;
|
|
193
|
+
blob?: Blob;
|
|
194
|
+
buffer?: ArrayBuffer;
|
|
195
|
+
json?: T;
|
|
196
|
+
text?: string;
|
|
197
|
+
document?: Document;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
declare function rq(url: string, options?: RequestInit): RqRequest;
|
|
201
|
+
|
|
202
|
+
declare function rx(url: string, options?: RxOptions): RqRequest;
|
|
203
|
+
|
|
204
|
+
declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
|
|
205
|
+
|
|
206
|
+
export { $, $$, Flag, Fn, Nullable, Prettify, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
|
package/browser/index.js
CHANGED
|
@@ -471,4 +471,112 @@ function customizeConsole(debug = false) {
|
|
|
471
471
|
};
|
|
472
472
|
}
|
|
473
473
|
|
|
474
|
-
|
|
474
|
+
function createRequest(transport, url, options) {
|
|
475
|
+
const getResponse = async () => {
|
|
476
|
+
return transport(url, options);
|
|
477
|
+
};
|
|
478
|
+
return {
|
|
479
|
+
response: async () => {
|
|
480
|
+
return getResponse();
|
|
481
|
+
},
|
|
482
|
+
blob: async () => {
|
|
483
|
+
const response = await getResponse();
|
|
484
|
+
return response.blob();
|
|
485
|
+
},
|
|
486
|
+
buffer: async () => {
|
|
487
|
+
const response = await getResponse();
|
|
488
|
+
return response.arrayBuffer();
|
|
489
|
+
},
|
|
490
|
+
json: async () => {
|
|
491
|
+
const response = await getResponse();
|
|
492
|
+
return response.json();
|
|
493
|
+
},
|
|
494
|
+
text: async () => {
|
|
495
|
+
const response = await getResponse();
|
|
496
|
+
return response.text();
|
|
497
|
+
},
|
|
498
|
+
document: async () => {
|
|
499
|
+
const response = await getResponse();
|
|
500
|
+
const responseText = await response.text();
|
|
501
|
+
const parser = new DOMParser();
|
|
502
|
+
return parser.parseFromString(responseText, 'text/html');
|
|
503
|
+
},
|
|
504
|
+
multi: async (types) => {
|
|
505
|
+
const response = await getResponse();
|
|
506
|
+
const responses = { response };
|
|
507
|
+
if (types.includes('blob')) {
|
|
508
|
+
responses.blob = await response.clone().blob();
|
|
509
|
+
}
|
|
510
|
+
if (types.includes('buffer')) {
|
|
511
|
+
responses.buffer = await response.clone().arrayBuffer();
|
|
512
|
+
}
|
|
513
|
+
if (types.includes('json')) {
|
|
514
|
+
responses.json = (await response.clone().json());
|
|
515
|
+
}
|
|
516
|
+
if (types.includes('text')) {
|
|
517
|
+
responses.text = await response.clone().text();
|
|
518
|
+
}
|
|
519
|
+
if (types.includes('document')) {
|
|
520
|
+
const responseText = await response.clone().text();
|
|
521
|
+
const parser = new DOMParser();
|
|
522
|
+
responses.document = parser.parseFromString(responseText, 'text/html');
|
|
523
|
+
}
|
|
524
|
+
return responses;
|
|
525
|
+
},
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
function isFileURL(url) {
|
|
530
|
+
return url.startsWith('file:///');
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function fetchTransport(url, options) {
|
|
534
|
+
const response = await fetch(url, options);
|
|
535
|
+
if (!response.ok && !isFileURL(response.url)) {
|
|
536
|
+
throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
|
|
537
|
+
}
|
|
538
|
+
return response;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function rq(url, options) {
|
|
542
|
+
return createRequest(fetchTransport, url, options);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
async function gmTransport(url, options) {
|
|
546
|
+
const { method, anonymous, headers, body, timeOut, onProgress } = options ?? {};
|
|
547
|
+
return new Promise((resolve, reject) => {
|
|
548
|
+
GM.xmlHttpRequest({
|
|
549
|
+
url,
|
|
550
|
+
method: method ?? 'GET',
|
|
551
|
+
headers,
|
|
552
|
+
/** Note: To manually set the 'cookie' header, you have to set 'anonymous' to true. */
|
|
553
|
+
anonymous,
|
|
554
|
+
data: body,
|
|
555
|
+
responseType: 'blob',
|
|
556
|
+
timeout: timeOut,
|
|
557
|
+
onprogress: onProgress,
|
|
558
|
+
onload({ response, statusText, status, finalUrl }) {
|
|
559
|
+
const ok = status >= 200 && status < 300;
|
|
560
|
+
if (!ok && !isFileURL(finalUrl)) {
|
|
561
|
+
reject(new Error(`Request to ${url} ended with ${status} status.`));
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const properResponse = new Response(response, {
|
|
565
|
+
statusText,
|
|
566
|
+
status: isFileURL(finalUrl) ? 200 : status,
|
|
567
|
+
});
|
|
568
|
+
Object.defineProperty(properResponse, 'url', { value: finalUrl });
|
|
569
|
+
resolve(properResponse);
|
|
570
|
+
},
|
|
571
|
+
onerror({ status }) {
|
|
572
|
+
reject(new Error(`Request to ${url} ended with ${status} status.`));
|
|
573
|
+
},
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
function rx(url, options) {
|
|
579
|
+
return createRequest(gmTransport, url, options);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
export { $, $$, addStyle, alert, asserted, capitalize, checkVPN, checkVPNRandomly, confirm, customizeConsole, fish, fishResponse, fishX, fishXResponse, getOptions, getSearchParam, imageLoad, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, noop, padZeros, pageLoad, prompt, remove, removeSearchParams, rq, createRequest as rqc, rx, saveFile, scriptName, setSearchParam, sleep, stringOrThrow, tabURL, toString };
|
package/node/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var fs = require('node:fs');
|
|
4
3
|
var fsp = require('node:fs/promises');
|
|
4
|
+
var fs = require('node:fs');
|
|
5
5
|
var path = require('node:path');
|
|
6
6
|
|
|
7
7
|
function noop() { }
|
|
@@ -134,6 +134,64 @@ function capitalize(sentence) {
|
|
|
134
134
|
return capitalizeWord(sentence);
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
+
async function exists(path) {
|
|
138
|
+
try {
|
|
139
|
+
await fsp.access(path);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function existsFile(path) {
|
|
148
|
+
try {
|
|
149
|
+
const stats = await fsp.stat(path);
|
|
150
|
+
return stats.isFile();
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function existsFileSync(path) {
|
|
158
|
+
try {
|
|
159
|
+
return fs.statSync(path).isFile();
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function existsFolder(path) {
|
|
167
|
+
try {
|
|
168
|
+
const stats = await fsp.stat(path);
|
|
169
|
+
return stats.isDirectory();
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function existsFolderSync(path) {
|
|
177
|
+
try {
|
|
178
|
+
return fs.statSync(path).isDirectory();
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function existsSync(path) {
|
|
186
|
+
try {
|
|
187
|
+
fs.accessSync(path);
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
137
195
|
/**
|
|
138
196
|
* Lists the files that are present in the provided folder path.
|
|
139
197
|
* @throws an error if the provided folder path does not exist.
|
|
@@ -244,8 +302,85 @@ async function readJSON(filePath, withComments = false) {
|
|
|
244
302
|
return JSON.parse(stringJSON);
|
|
245
303
|
}
|
|
246
304
|
|
|
305
|
+
function createRequest(transport, url, options) {
|
|
306
|
+
const getResponse = async () => {
|
|
307
|
+
return transport(url, options);
|
|
308
|
+
};
|
|
309
|
+
return {
|
|
310
|
+
response: async () => {
|
|
311
|
+
return getResponse();
|
|
312
|
+
},
|
|
313
|
+
blob: async () => {
|
|
314
|
+
const response = await getResponse();
|
|
315
|
+
return response.blob();
|
|
316
|
+
},
|
|
317
|
+
buffer: async () => {
|
|
318
|
+
const response = await getResponse();
|
|
319
|
+
return response.arrayBuffer();
|
|
320
|
+
},
|
|
321
|
+
json: async () => {
|
|
322
|
+
const response = await getResponse();
|
|
323
|
+
return response.json();
|
|
324
|
+
},
|
|
325
|
+
text: async () => {
|
|
326
|
+
const response = await getResponse();
|
|
327
|
+
return response.text();
|
|
328
|
+
},
|
|
329
|
+
document: async () => {
|
|
330
|
+
const response = await getResponse();
|
|
331
|
+
const responseText = await response.text();
|
|
332
|
+
const parser = new DOMParser();
|
|
333
|
+
return parser.parseFromString(responseText, 'text/html');
|
|
334
|
+
},
|
|
335
|
+
multi: async (types) => {
|
|
336
|
+
const response = await getResponse();
|
|
337
|
+
const responses = { response };
|
|
338
|
+
if (types.includes('blob')) {
|
|
339
|
+
responses.blob = await response.clone().blob();
|
|
340
|
+
}
|
|
341
|
+
if (types.includes('buffer')) {
|
|
342
|
+
responses.buffer = await response.clone().arrayBuffer();
|
|
343
|
+
}
|
|
344
|
+
if (types.includes('json')) {
|
|
345
|
+
responses.json = (await response.clone().json());
|
|
346
|
+
}
|
|
347
|
+
if (types.includes('text')) {
|
|
348
|
+
responses.text = await response.clone().text();
|
|
349
|
+
}
|
|
350
|
+
if (types.includes('document')) {
|
|
351
|
+
const responseText = await response.clone().text();
|
|
352
|
+
const parser = new DOMParser();
|
|
353
|
+
responses.document = parser.parseFromString(responseText, 'text/html');
|
|
354
|
+
}
|
|
355
|
+
return responses;
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function isFileURL(url) {
|
|
361
|
+
return url.startsWith('file:///');
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
async function fetchTransport(url, options) {
|
|
365
|
+
const response = await fetch(url, options);
|
|
366
|
+
if (!response.ok && !isFileURL(response.url)) {
|
|
367
|
+
throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
|
|
368
|
+
}
|
|
369
|
+
return response;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function rq(url, options) {
|
|
373
|
+
return createRequest(fetchTransport, url, options);
|
|
374
|
+
}
|
|
375
|
+
|
|
247
376
|
exports.asserted = asserted;
|
|
248
377
|
exports.capitalize = capitalize;
|
|
378
|
+
exports.exists = exists;
|
|
379
|
+
exports.existsFile = existsFile;
|
|
380
|
+
exports.existsFileSync = existsFileSync;
|
|
381
|
+
exports.existsFolder = existsFolder;
|
|
382
|
+
exports.existsFolderSync = existsFolderSync;
|
|
383
|
+
exports.existsSync = existsSync;
|
|
249
384
|
exports.isBoolean = isBoolean;
|
|
250
385
|
exports.isFalsy = isFalsy;
|
|
251
386
|
exports.isFunction = isFunction;
|
|
@@ -264,6 +399,8 @@ exports.padZeros = padZeros;
|
|
|
264
399
|
exports.readJSON = readJSON;
|
|
265
400
|
exports.remove = remove;
|
|
266
401
|
exports.removeFiles = removeFiles;
|
|
402
|
+
exports.rq = rq;
|
|
403
|
+
exports.rqc = createRequest;
|
|
267
404
|
exports.sleep = sleep;
|
|
268
405
|
exports.stringOrThrow = stringOrThrow;
|
|
269
406
|
exports.toString = toString;
|
package/node/index.d.ts
CHANGED
|
@@ -56,6 +56,18 @@ type Prettify<T> = {
|
|
|
56
56
|
[KeyType in keyof T]: T[KeyType];
|
|
57
57
|
} & {};
|
|
58
58
|
|
|
59
|
+
declare function exists(path: string): Promise<boolean>;
|
|
60
|
+
|
|
61
|
+
declare function existsFile(path: string): Promise<boolean>;
|
|
62
|
+
|
|
63
|
+
declare function existsFileSync(path: string): boolean;
|
|
64
|
+
|
|
65
|
+
declare function existsFolder(path: string): Promise<boolean>;
|
|
66
|
+
|
|
67
|
+
declare function existsFolderSync(path: string): boolean;
|
|
68
|
+
|
|
69
|
+
declare function existsSync(path: string): boolean;
|
|
70
|
+
|
|
59
71
|
interface ListBaseOpt {
|
|
60
72
|
/** The absolute path of the target folder. */
|
|
61
73
|
folderPath: string;
|
|
@@ -110,4 +122,28 @@ declare function removeFiles({ folderPath, filter }: RemoveFilesOpt): Promise<vo
|
|
|
110
122
|
|
|
111
123
|
declare function readJSON<T = object>(filePath: string, withComments?: boolean): Promise<T>;
|
|
112
124
|
|
|
113
|
-
|
|
125
|
+
interface RqRequest {
|
|
126
|
+
response: () => Promise<Response>;
|
|
127
|
+
blob: () => Promise<Blob>;
|
|
128
|
+
buffer: () => Promise<ArrayBuffer>;
|
|
129
|
+
json: <T = unknown>() => Promise<T>;
|
|
130
|
+
text: () => Promise<string>;
|
|
131
|
+
document: () => Promise<Document>;
|
|
132
|
+
multi: <T = unknown>(types: ResponseType[]) => Promise<Responses<T>>;
|
|
133
|
+
}
|
|
134
|
+
type Transport<TOptions = unknown> = (url: string, options?: TOptions) => Promise<Response>;
|
|
135
|
+
type ResponseType = 'blob' | 'buffer' | 'document' | 'json' | 'text';
|
|
136
|
+
interface Responses<T> {
|
|
137
|
+
response?: Response;
|
|
138
|
+
blob?: Blob;
|
|
139
|
+
buffer?: ArrayBuffer;
|
|
140
|
+
json?: T;
|
|
141
|
+
text?: string;
|
|
142
|
+
document?: Document;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
declare function rq(url: string, options?: RequestInit): RqRequest;
|
|
146
|
+
|
|
147
|
+
declare function createRequest<TOptions>(transport: Transport<TOptions>, url: string, options?: TOptions): RqRequest;
|
|
148
|
+
|
|
149
|
+
export { Fn, Nullable, Prettify, asserted, capitalize, exists, existsFile, existsFileSync, existsFolder, existsFolderSync, existsSync, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
|
package/node/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import fsp, { access, stat } from 'node:fs/promises';
|
|
2
|
+
import fs, { statSync, accessSync } from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
|
|
5
5
|
function noop() { }
|
|
@@ -132,6 +132,64 @@ function capitalize(sentence) {
|
|
|
132
132
|
return capitalizeWord(sentence);
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
+
async function exists(path) {
|
|
136
|
+
try {
|
|
137
|
+
await access(path);
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function existsFile(path) {
|
|
146
|
+
try {
|
|
147
|
+
const stats = await stat(path);
|
|
148
|
+
return stats.isFile();
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function existsFileSync(path) {
|
|
156
|
+
try {
|
|
157
|
+
return statSync(path).isFile();
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function existsFolder(path) {
|
|
165
|
+
try {
|
|
166
|
+
const stats = await stat(path);
|
|
167
|
+
return stats.isDirectory();
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function existsFolderSync(path) {
|
|
175
|
+
try {
|
|
176
|
+
return statSync(path).isDirectory();
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function existsSync(path) {
|
|
184
|
+
try {
|
|
185
|
+
accessSync(path);
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
135
193
|
/**
|
|
136
194
|
* Lists the files that are present in the provided folder path.
|
|
137
195
|
* @throws an error if the provided folder path does not exist.
|
|
@@ -242,4 +300,75 @@ async function readJSON(filePath, withComments = false) {
|
|
|
242
300
|
return JSON.parse(stringJSON);
|
|
243
301
|
}
|
|
244
302
|
|
|
245
|
-
|
|
303
|
+
function createRequest(transport, url, options) {
|
|
304
|
+
const getResponse = async () => {
|
|
305
|
+
return transport(url, options);
|
|
306
|
+
};
|
|
307
|
+
return {
|
|
308
|
+
response: async () => {
|
|
309
|
+
return getResponse();
|
|
310
|
+
},
|
|
311
|
+
blob: async () => {
|
|
312
|
+
const response = await getResponse();
|
|
313
|
+
return response.blob();
|
|
314
|
+
},
|
|
315
|
+
buffer: async () => {
|
|
316
|
+
const response = await getResponse();
|
|
317
|
+
return response.arrayBuffer();
|
|
318
|
+
},
|
|
319
|
+
json: async () => {
|
|
320
|
+
const response = await getResponse();
|
|
321
|
+
return response.json();
|
|
322
|
+
},
|
|
323
|
+
text: async () => {
|
|
324
|
+
const response = await getResponse();
|
|
325
|
+
return response.text();
|
|
326
|
+
},
|
|
327
|
+
document: async () => {
|
|
328
|
+
const response = await getResponse();
|
|
329
|
+
const responseText = await response.text();
|
|
330
|
+
const parser = new DOMParser();
|
|
331
|
+
return parser.parseFromString(responseText, 'text/html');
|
|
332
|
+
},
|
|
333
|
+
multi: async (types) => {
|
|
334
|
+
const response = await getResponse();
|
|
335
|
+
const responses = { response };
|
|
336
|
+
if (types.includes('blob')) {
|
|
337
|
+
responses.blob = await response.clone().blob();
|
|
338
|
+
}
|
|
339
|
+
if (types.includes('buffer')) {
|
|
340
|
+
responses.buffer = await response.clone().arrayBuffer();
|
|
341
|
+
}
|
|
342
|
+
if (types.includes('json')) {
|
|
343
|
+
responses.json = (await response.clone().json());
|
|
344
|
+
}
|
|
345
|
+
if (types.includes('text')) {
|
|
346
|
+
responses.text = await response.clone().text();
|
|
347
|
+
}
|
|
348
|
+
if (types.includes('document')) {
|
|
349
|
+
const responseText = await response.clone().text();
|
|
350
|
+
const parser = new DOMParser();
|
|
351
|
+
responses.document = parser.parseFromString(responseText, 'text/html');
|
|
352
|
+
}
|
|
353
|
+
return responses;
|
|
354
|
+
},
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function isFileURL(url) {
|
|
359
|
+
return url.startsWith('file:///');
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
async function fetchTransport(url, options) {
|
|
363
|
+
const response = await fetch(url, options);
|
|
364
|
+
if (!response.ok && !isFileURL(response.url)) {
|
|
365
|
+
throw new Error(`Request to ${response.url} ended with ${response.status} status.`);
|
|
366
|
+
}
|
|
367
|
+
return response;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function rq(url, options) {
|
|
371
|
+
return createRequest(fetchTransport, url, options);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
export { asserted, capitalize, exists, existsFile, existsFileSync, existsFolder, existsFolderSync, existsSync, isBoolean, isFalsy, isFunction, isNotNullish, isNullish, isNumber, isObject, isString, isTruthy, join, listFiles, listFolders, listFoldersSync, noop, padZeros, readJSON, remove, removeFiles, rq, createRequest as rqc, sleep, stringOrThrow, toString };
|