@jayfong/x-utils 1.0.0-202510061015 → 3.4.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.
Files changed (143) hide show
  1. package/lib/ai.d.mts +103 -0
  2. package/lib/ai.mjs +75 -0
  3. package/lib/cache.d.mts +464 -0
  4. package/lib/cache.mjs +531 -0
  5. package/lib/captcha.d.mts +259 -0
  6. package/lib/captcha.mjs +174 -0
  7. package/lib/case.d.mts +184 -0
  8. package/lib/case.mjs +120 -0
  9. package/lib/chinese.d.mts +57 -0
  10. package/lib/chinese.mjs +79 -0
  11. package/lib/cli.d.mts +52 -0
  12. package/lib/cli.mjs +62 -0
  13. package/lib/clipboard.d.mts +50 -0
  14. package/lib/clipboard.mjs +122 -0
  15. package/lib/cmd.d.mts +38 -0
  16. package/lib/cmd.mjs +52 -0
  17. package/lib/crawler.d.mts +330 -0
  18. package/lib/crawler.mjs +313 -0
  19. package/lib/crypto.d.mts +158 -0
  20. package/lib/crypto.mjs +213 -0
  21. package/lib/csv.d.mts +67 -0
  22. package/lib/csv.mjs +76 -0
  23. package/lib/data_set.d.mts +76 -0
  24. package/lib/data_set.mjs +104 -0
  25. package/lib/db_value.d.mts +213 -0
  26. package/lib/db_value.mjs +208 -0
  27. package/lib/dispose.d.mts +98 -0
  28. package/lib/dispose.mjs +107 -0
  29. package/lib/file_store.d.mts +167 -0
  30. package/lib/file_store.mjs +120 -0
  31. package/lib/fs.d.mts +111 -0
  32. package/lib/fs.mjs +127 -0
  33. package/lib/html.d.mts +103 -0
  34. package/lib/html.mjs +112 -0
  35. package/lib/json.d.mts +50 -0
  36. package/lib/json.mjs +74 -0
  37. package/lib/jwt.d.mts +128 -0
  38. package/lib/jwt.mjs +104 -0
  39. package/lib/mail.d.mts +125 -0
  40. package/lib/mail.mjs +108 -0
  41. package/lib/pay.d.mts +394 -0
  42. package/lib/pay.mjs +343 -0
  43. package/lib/puppeteer.d.mts +88 -0
  44. package/lib/puppeteer.mjs +219 -0
  45. package/lib/random.d.mts +71 -0
  46. package/lib/random.mjs +92 -0
  47. package/lib/redis.d.mts +32 -0
  48. package/lib/redis.mjs +24 -0
  49. package/lib/runner.d.mts +80 -0
  50. package/lib/runner.mjs +66 -0
  51. package/lib/sensitive_words.d.mts +82 -0
  52. package/lib/sensitive_words.mjs +73 -0
  53. package/lib/sms.d.mts +133 -0
  54. package/lib/sms.mjs +100 -0
  55. package/lib/task.d.mts +37 -0
  56. package/lib/task.mjs +43 -0
  57. package/lib/text.d.mts +84 -0
  58. package/lib/text.mjs +141 -0
  59. package/lib/yaml.d.mts +51 -0
  60. package/lib/yaml.mjs +64 -0
  61. package/package.json +82 -164
  62. package/_cjs/ai.js +0 -80
  63. package/_cjs/alipan.js +0 -173
  64. package/_cjs/cli.js +0 -51
  65. package/_cjs/clipboard.js +0 -65
  66. package/_cjs/cmd.js +0 -42
  67. package/_cjs/compressor.js +0 -47
  68. package/_cjs/crawler.js +0 -341
  69. package/_cjs/csv.js +0 -97
  70. package/_cjs/data_set.js +0 -115
  71. package/_cjs/env.js +0 -82
  72. package/_cjs/fs.js +0 -86
  73. package/_cjs/html.js +0 -62
  74. package/_cjs/http_server.js +0 -79
  75. package/_cjs/json.js +0 -49
  76. package/_cjs/ksb.js +0 -218
  77. package/_cjs/number.js +0 -39
  78. package/_cjs/ocr.js +0 -74
  79. package/_cjs/package.json +0 -3
  80. package/_cjs/path.js +0 -35
  81. package/_cjs/pinyin.js +0 -37
  82. package/_cjs/puppeteer.js +0 -271
  83. package/_cjs/runner.js +0 -88
  84. package/_cjs/task.js +0 -41
  85. package/_cjs/text.js +0 -136
  86. package/_cjs/unicode/lib_unicode.js +0 -58643
  87. package/_cjs/unicode/unicode.js +0 -441
  88. package/_cjs/upload.js +0 -101
  89. package/_cjs/yaml.js +0 -52
  90. package/ai.d.ts +0 -26
  91. package/ai.js +0 -60
  92. package/alipan.d.ts +0 -209
  93. package/alipan.js +0 -153
  94. package/cli.d.ts +0 -14
  95. package/cli.js +0 -21
  96. package/clipboard.d.ts +0 -6
  97. package/clipboard.js +0 -35
  98. package/cmd.d.ts +0 -5
  99. package/cmd.js +0 -22
  100. package/compressor.d.ts +0 -5
  101. package/compressor.js +0 -17
  102. package/crawler.d.ts +0 -162
  103. package/crawler.js +0 -332
  104. package/csv.d.ts +0 -14
  105. package/csv.js +0 -67
  106. package/data_set.d.ts +0 -22
  107. package/data_set.js +0 -85
  108. package/env.d.ts +0 -6
  109. package/env.js +0 -52
  110. package/fs.d.ts +0 -23
  111. package/fs.js +0 -56
  112. package/html.d.ts +0 -7
  113. package/html.js +0 -45
  114. package/http_server.d.ts +0 -13
  115. package/http_server.js +0 -49
  116. package/json.d.ts +0 -6
  117. package/json.js +0 -19
  118. package/ksb.d.ts +0 -37
  119. package/ksb.js +0 -198
  120. package/number.d.ts +0 -3
  121. package/number.js +0 -9
  122. package/ocr.d.ts +0 -17
  123. package/ocr.js +0 -54
  124. package/path.d.ts +0 -2
  125. package/path.js +0 -5
  126. package/pinyin.d.ts +0 -8
  127. package/pinyin.js +0 -17
  128. package/puppeteer.d.ts +0 -38
  129. package/puppeteer.js +0 -241
  130. package/runner.d.ts +0 -13
  131. package/runner.js +0 -58
  132. package/task.d.ts +0 -6
  133. package/task.js +0 -21
  134. package/text.d.ts +0 -63
  135. package/text.js +0 -119
  136. package/unicode/lib_unicode.d.ts +0 -4
  137. package/unicode/lib_unicode.js +0 -58623
  138. package/unicode/unicode.d.ts +0 -347
  139. package/unicode/unicode.js +0 -426
  140. package/upload.d.ts +0 -34
  141. package/upload.js +0 -71
  142. package/yaml.d.ts +0 -6
  143. package/yaml.js +0 -22
package/puppeteer.js DELETED
@@ -1,241 +0,0 @@
1
- import { xr } from "@jayfong/x-request";
2
- import { asyncExitHook } from "exit-hook";
3
- import puppeteer from "puppeteer";
4
- import { isFunction, isRegExp, memoize } from "vtils";
5
- import { XClipboard } from "./clipboard.js";
6
- class XPuppeteer {
7
- browser;
8
- async connectSystemBrowser() {
9
- const {
10
- data: { webSocketDebuggerUrl }
11
- } = await xr.getJson({
12
- url: "http://localhost:9222/json/version"
13
- });
14
- const browser = await puppeteer.connect({
15
- browserWSEndpoint: webSocketDebuggerUrl,
16
- // 都设为 0 以保持原有的宽高
17
- defaultViewport: {
18
- width: 0,
19
- height: 0
20
- }
21
- });
22
- this.browser = browser;
23
- asyncExitHook(() => this.disconnect(), {
24
- wait: 1e3
25
- });
26
- return browser;
27
- }
28
- async openBrowser(options = {}) {
29
- const browser = await puppeteer.launch({
30
- headless: options.headful ? false : true,
31
- defaultViewport: {
32
- width: 1440,
33
- height: 900
34
- },
35
- userDataDir: options.dataDir,
36
- args: options.proxy ? [`--proxy-server=${options.proxy}`] : void 0
37
- });
38
- this.browser = browser;
39
- asyncExitHook(() => this.close(), {
40
- wait: 1e3
41
- });
42
- return browser;
43
- }
44
- async openPage(urlOrHtml, options = {}) {
45
- const isUrl = /^https?:\/\//i.test(urlOrHtml);
46
- const pages = await this.browser.pages();
47
- if (isUrl) {
48
- const inputUrl = new URL(urlOrHtml);
49
- const foundPage = pages.find((item) => {
50
- const pageUrl = item.url();
51
- const url = new URL(pageUrl);
52
- return inputUrl.origin === url.origin && (pageUrl === urlOrHtml || !!options.match && (typeof options.match === "string" ? `${url.pathname}${url.search}`.includes(options.match) : options.match.test(`${url.pathname}${url.search}`)));
53
- });
54
- if (foundPage) {
55
- return foundPage;
56
- }
57
- }
58
- const page2 = await this.browser.newPage();
59
- await page2.setUserAgent(
60
- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
61
- );
62
- if (options.blockAssets) {
63
- await page2.setRequestInterception(true);
64
- page2.on("request", (req) => {
65
- const resourceType = req.resourceType();
66
- if (resourceType === "stylesheet" || resourceType === "font" || resourceType === "image") {
67
- req.abort();
68
- } else {
69
- req.continue();
70
- }
71
- });
72
- }
73
- if (isUrl) {
74
- await page2.goto(urlOrHtml, {
75
- referer: options.referer || urlOrHtml
76
- });
77
- } else {
78
- await page2.setContent(urlOrHtml);
79
- }
80
- return page2;
81
- }
82
- async input(page2, selector, textOrHtml, frameIndex) {
83
- const mainPage = page2;
84
- const keyboard = page2.keyboard;
85
- if (frameIndex != null) {
86
- page2 = page2.frames()[frameIndex];
87
- }
88
- const el2 = await page2.$(selector);
89
- const isText = await el2.evaluate((el3) => {
90
- const isInput = el3.tagName === "INPUT" || el3.tagName === "TEXTAREA";
91
- const isText2 = isInput || el3.tagName === "SPAN";
92
- return isText2;
93
- });
94
- if (isText) {
95
- await XClipboard.writeText(textOrHtml);
96
- } else {
97
- await XClipboard.writeHtml(textOrHtml);
98
- }
99
- await el2.click();
100
- await this.selectAll(page2);
101
- await keyboard.press("Backspace");
102
- await this.paste(mainPage);
103
- }
104
- async selectAll(page2) {
105
- await page2.evaluate(() => {
106
- const isMac = /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
107
- document.activeElement.dispatchEvent(
108
- new KeyboardEvent("keydown", {
109
- bubbles: true,
110
- cancelable: true,
111
- key: isMac ? "Meta" : "Control",
112
- code: isMac ? "MetaLeft" : "ControlLeft",
113
- location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT,
114
- // @ts-ignore
115
- getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"),
116
- ctrlKey: !isMac,
117
- metaKey: isMac,
118
- charCode: 0,
119
- keyCode: isMac ? 93 : 17,
120
- which: isMac ? 93 : 17
121
- })
122
- );
123
- const preventableEvent = new KeyboardEvent("keydown", {
124
- bubbles: true,
125
- cancelable: true,
126
- key: "a",
127
- code: "KeyA",
128
- location: window.KeyboardEvent.DOM_KEY_LOCATION_STANDARD,
129
- // @ts-ignore
130
- getModifierState: (keyArg) => keyArg === (isMac ? "Meta" : "Control"),
131
- ctrlKey: !isMac,
132
- metaKey: isMac,
133
- charCode: 0,
134
- keyCode: 65,
135
- which: 65
136
- });
137
- const wasPrevented = !document.activeElement.dispatchEvent(preventableEvent) || preventableEvent.defaultPrevented;
138
- if (!wasPrevented) {
139
- document.execCommand("selectall", false, null);
140
- }
141
- document.activeElement.dispatchEvent(
142
- new KeyboardEvent("keyup", {
143
- bubbles: true,
144
- cancelable: true,
145
- key: isMac ? "Meta" : "Control",
146
- code: isMac ? "MetaLeft" : "ControlLeft",
147
- location: window.KeyboardEvent.DOM_KEY_LOCATION_LEFT,
148
- // @ts-ignore
149
- getModifierState: () => false,
150
- charCode: 0,
151
- keyCode: isMac ? 93 : 17,
152
- which: isMac ? 93 : 17
153
- })
154
- );
155
- });
156
- }
157
- async paste(page2) {
158
- await page2.keyboard.down("Shift");
159
- await page2.keyboard.press("Insert");
160
- await page2.keyboard.up("Shift");
161
- }
162
- async queryByText(page, text) {
163
- const el = await page.evaluateHandle(
164
- (text, isRegExp, isFunction) => {
165
- const textRe = isRegExp ? (() => {
166
- const m = text.match(/^\/(.+)\/([^/]*)$/);
167
- return new RegExp(m[1], m[2] || void 0);
168
- })() : void 0;
169
- const textFn = isFunction ? eval(`(() => {return ${text}})()`) : void 0;
170
- const walker = document.createTreeWalker(
171
- document.body,
172
- NodeFilter.SHOW_TEXT,
173
- {
174
- acceptNode: (node) => {
175
- return (isRegExp ? textRe.test(node.textContent) : isFunction ? textFn(node) : node.textContent.includes(text)) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
176
- }
177
- }
178
- );
179
- walker.nextNode();
180
- return walker.currentNode.parentElement;
181
- },
182
- String(text),
183
- isRegExp(text),
184
- isFunction(text)
185
- );
186
- return el;
187
- }
188
- async disconnect() {
189
- await this.browser.disconnect();
190
- }
191
- async close() {
192
- await this.browser.close();
193
- }
194
- static async openBrowser(options = {}) {
195
- const xp = new XPuppeteer();
196
- await xp.openBrowser(options);
197
- return xp;
198
- }
199
- static async openPage(urlOrHtml, options = {}) {
200
- const xp = new XPuppeteer();
201
- await xp.openBrowser(options);
202
- const page2 = await xp.openPage(urlOrHtml, options);
203
- page2.close = () => xp.close();
204
- return page2;
205
- }
206
- static getPageHtmlXp = memoize(
207
- async (options) => {
208
- const xp = new XPuppeteer();
209
- await (options.useSystemBrowser ? xp.connectSystemBrowser() : xp.openBrowser(options));
210
- return xp;
211
- },
212
- (options) => options.instanceId
213
- );
214
- static async getPageHtml(url, options = {}) {
215
- const hasInstanceId = !!options.instanceId;
216
- options.instanceId ||= Date.now();
217
- const xp = await XPuppeteer.getPageHtmlXp(options);
218
- try {
219
- const page2 = await xp.openPage(url, options);
220
- if (options.afterOpenPage) {
221
- await options.afterOpenPage({
222
- browser: xp,
223
- page: page2
224
- });
225
- }
226
- const html = await page2.content();
227
- return html;
228
- } finally {
229
- if (!hasInstanceId) {
230
- if (options.useSystemBrowser) {
231
- await xp.disconnect();
232
- } else {
233
- await xp.close();
234
- }
235
- }
236
- }
237
- }
238
- }
239
- export {
240
- XPuppeteer
241
- };
package/runner.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { DistinctQuestion } from 'inquirer';
2
- import { MsValue } from 'vtils';
3
- export type XRunnerOptions<TConfig extends Record<any, any>> = {
4
- configFile: string;
5
- delay: MsValue | ((retryCount: number) => MsValue);
6
- questions: Array<DistinctQuestion<TConfig>>;
7
- run: (config: TConfig) => any;
8
- };
9
- export declare class XRunner<TConfig extends Record<any, any>> {
10
- private options;
11
- constructor(options: XRunnerOptions<TConfig>);
12
- run(): Promise<void>;
13
- }
package/runner.js DELETED
@@ -1,58 +0,0 @@
1
- import inquirer from "inquirer";
2
- import { ms, wait } from "vtils";
3
- import { XFs } from "./fs.js";
4
- class XRunner {
5
- constructor(options) {
6
- this.options = options;
7
- }
8
- async run() {
9
- await XFs.ensureFile(this.options.configFile);
10
- const savedConfig = await XFs.readJson(this.options.configFile, { throws: false }) || {};
11
- const config = await inquirer.prompt(
12
- this.options.questions.map((item) => {
13
- return {
14
- ...item,
15
- default: savedConfig[item.name] ?? item.default
16
- };
17
- })
18
- );
19
- await XFs.writeJson(this.options.configFile, config, { spaces: 2 });
20
- let retryCount = 0;
21
- while (true) {
22
- try {
23
- await this.options.run(config);
24
- retryCount = 0;
25
- } catch (err) {
26
- const delay = typeof this.options.delay === "function" ? this.options.delay(retryCount) : this.options.delay;
27
- console.log(err);
28
- console.log(`\u6267\u884C\u62A5\u9519, \u7B49\u5F85${delay}`);
29
- await wait(ms(delay));
30
- retryCount++;
31
- }
32
- }
33
- }
34
- }
35
- new XRunner({
36
- configFile: "./temp/test.json",
37
- delay: "1s",
38
- questions: [
39
- {
40
- name: "perCount",
41
- message: "\u6BCF\u8F6E\u5904\u7406\u6761\u6570",
42
- type: "number",
43
- default: 500
44
- },
45
- {
46
- name: "concurrencyCount",
47
- message: "\u6BCF\u8F6E\u5E76\u884C\u5904\u7406\u6761\u6570",
48
- type: "number",
49
- default: 1
50
- }
51
- ],
52
- run: (config) => {
53
- console.log(config);
54
- }
55
- }).run();
56
- export {
57
- XRunner
58
- };
package/task.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export declare abstract class XTask {
2
- log: import("consola/core").ConsolaInstance;
3
- abstract start(): Promise<void>;
4
- dispose(): Promise<void>;
5
- run(): Promise<void>;
6
- }
package/task.js DELETED
@@ -1,21 +0,0 @@
1
- import { consola as log } from "consola";
2
- import { asyncExitHook, gracefulExit } from "exit-hook";
3
- class XTask {
4
- log = log;
5
- async dispose() {
6
- }
7
- run() {
8
- asyncExitHook(() => this.dispose(), {
9
- wait: 3e3
10
- });
11
- return this.start().then(() => {
12
- gracefulExit(0);
13
- }).catch((err) => {
14
- log.error(err);
15
- gracefulExit(1);
16
- });
17
- }
18
- }
19
- export {
20
- XTask
21
- };
package/text.d.ts DELETED
@@ -1,63 +0,0 @@
1
- export declare class XText {
2
- private text;
3
- /**
4
- * 将字符串分割为字符数组。
5
- *
6
- * @see https://stackoverflow.com/a/71619350
7
- */
8
- static split(text: string): string[];
9
- /**
10
- * 纯粹为了书写 HTML 有提示。
11
- */
12
- static html(literals: any, ...interpolations: any): string;
13
- /**
14
- * 切割字符串。
15
- */
16
- static cut(text: string, start: string, end: string): string;
17
- /**
18
- * 移除全部空白字符。
19
- */
20
- static stripWhitespace(text: string): string;
21
- /**
22
- * 移除首尾空白字符。
23
- */
24
- static stripMarginWhitespace(text: string): string;
25
- /**
26
- * HTML 实体编码。
27
- */
28
- static encodeHtmlEntity(text: string): string;
29
- /**
30
- * HTML 实体解码。
31
- */
32
- static decodeHtmlEntity(text: string): string;
33
- /**
34
- * 移除 HTML 代码。
35
- */
36
- static stripHtml(text: string): string;
37
- constructor(text: string);
38
- /**
39
- * 切割字符串。
40
- */
41
- cut(start: string, end: string): this;
42
- /**
43
- * 移除全部空白字符。
44
- */
45
- stripWhitespace(): this;
46
- /**
47
- * 移除首尾空白字符。
48
- */
49
- stripMarginWhitespace(): this;
50
- /**
51
- * HTML 实体编码。
52
- */
53
- encodeHtmlEntity(): this;
54
- /**
55
- * HTML 实体解码。
56
- */
57
- decodeHtmlEntity(): this;
58
- /**
59
- * 移除 HTML 代码。
60
- */
61
- stripHtml(): this;
62
- toString(): string;
63
- }
package/text.js DELETED
@@ -1,119 +0,0 @@
1
- import {
2
- decode as decodeHtmlEntity,
3
- encode as encodeHtmlEntity
4
- } from "html-entities";
5
- class XText {
6
- constructor(text) {
7
- this.text = text;
8
- }
9
- /**
10
- * 将字符串分割为字符数组。
11
- *
12
- * @see https://stackoverflow.com/a/71619350
13
- */
14
- static split(text) {
15
- return [...new Intl.Segmenter().segment(text)].map((x) => x.segment);
16
- }
17
- /**
18
- * 纯粹为了书写 HTML 有提示。
19
- */
20
- static html(literals, ...interpolations) {
21
- let result = "";
22
- for (let i = 0; i < interpolations.length; i++) {
23
- result += literals[i];
24
- result += interpolations[i];
25
- }
26
- result += literals[literals.length - 1];
27
- return result;
28
- }
29
- /**
30
- * 切割字符串。
31
- */
32
- static cut(text, start, end) {
33
- text = text.split(start)[1].split(end)[0];
34
- return text;
35
- }
36
- /**
37
- * 移除全部空白字符。
38
- */
39
- static stripWhitespace(text) {
40
- text = text.replace(/\s+/g, "");
41
- return text;
42
- }
43
- /**
44
- * 移除首尾空白字符。
45
- */
46
- static stripMarginWhitespace(text) {
47
- text = text.trim();
48
- return text;
49
- }
50
- /**
51
- * HTML 实体编码。
52
- */
53
- static encodeHtmlEntity(text) {
54
- text = encodeHtmlEntity(text);
55
- return text;
56
- }
57
- /**
58
- * HTML 实体解码。
59
- */
60
- static decodeHtmlEntity(text) {
61
- text = decodeHtmlEntity(text);
62
- return text;
63
- }
64
- /**
65
- * 移除 HTML 代码。
66
- */
67
- static stripHtml(text) {
68
- text = text.replace(/<script[\s>].+?<\/script>/g, "").replace(/<style[\s>].+?<\/style>/g, "").replace(/<(.+?)>/g, "");
69
- return text;
70
- }
71
- /**
72
- * 切割字符串。
73
- */
74
- cut(start, end) {
75
- this.text = XText.cut(this.text, start, end);
76
- return this;
77
- }
78
- /**
79
- * 移除全部空白字符。
80
- */
81
- stripWhitespace() {
82
- this.text = XText.stripWhitespace(this.text);
83
- return this;
84
- }
85
- /**
86
- * 移除首尾空白字符。
87
- */
88
- stripMarginWhitespace() {
89
- this.text = XText.stripMarginWhitespace(this.text);
90
- return this;
91
- }
92
- /**
93
- * HTML 实体编码。
94
- */
95
- encodeHtmlEntity() {
96
- this.text = XText.encodeHtmlEntity(this.text);
97
- return this;
98
- }
99
- /**
100
- * HTML 实体解码。
101
- */
102
- decodeHtmlEntity() {
103
- this.text = XText.decodeHtmlEntity(this.text);
104
- return this;
105
- }
106
- /**
107
- * 移除 HTML 代码。
108
- */
109
- stripHtml() {
110
- this.text = XText.stripHtml(this.text);
111
- return this;
112
- }
113
- toString() {
114
- return this.text;
115
- }
116
- }
117
- export {
118
- XText
119
- };
@@ -1,4 +0,0 @@
1
- export const BlockEnd: any;
2
- export const GetBlockStart: any;
3
- export const GetCharacterName: any;
4
- export const ToCharacterNames: any;