koishi-plugin-rina-currency 0.0.3 → 0.0.5
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/lib/index.d.ts +1 -0
- package/lib/index.js +73 -29
- package/lib/providers/common.d.ts +6 -0
- package/lib/scheduler.d.ts +0 -2
- package/package.json +4 -2
- package/readme.md +2 -0
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -36,6 +36,7 @@ __export(src_exports, {
|
|
|
36
36
|
name: () => name
|
|
37
37
|
});
|
|
38
38
|
module.exports = __toCommonJS(src_exports);
|
|
39
|
+
var import_koishi_plugin_cron = require("koishi-plugin-cron");
|
|
39
40
|
|
|
40
41
|
// src/config.ts
|
|
41
42
|
var import_koishi = require("koishi");
|
|
@@ -403,11 +404,35 @@ function extendModel(ctx) {
|
|
|
403
404
|
__name(extendModel, "extendModel");
|
|
404
405
|
|
|
405
406
|
// src/providers/common.ts
|
|
407
|
+
var import_node_http = require("node:http");
|
|
406
408
|
var BOC_SOURCE_URL = "https://www.boc.cn/sourcedb/whpj/index.html";
|
|
407
409
|
var ICBC_SOURCE_URL = "http://papi.icbc.com.cn/exchanges/ns/getLatest";
|
|
408
|
-
|
|
410
|
+
function describeError(error) {
|
|
411
|
+
if (!(error instanceof Error)) return String(error);
|
|
412
|
+
const details = [`${error.name}: ${error.message}`];
|
|
413
|
+
const code = error.code;
|
|
414
|
+
if (code) details.push(`code=${code}`);
|
|
415
|
+
if (error.cause && error.cause !== error) details.push(`cause=${describeError(error.cause)}`);
|
|
416
|
+
return details.join(", ");
|
|
417
|
+
}
|
|
418
|
+
__name(describeError, "describeError");
|
|
419
|
+
async function withRetries(url, retry, operation) {
|
|
409
420
|
let lastError;
|
|
410
421
|
for (let attempt = 1; attempt <= retry.attempts; attempt++) {
|
|
422
|
+
try {
|
|
423
|
+
return await operation();
|
|
424
|
+
} catch (error) {
|
|
425
|
+
lastError = error;
|
|
426
|
+
}
|
|
427
|
+
if (attempt < retry.attempts) {
|
|
428
|
+
await new Promise((resolve) => setTimeout(resolve, retry.retryDelay * attempt));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
throw new Error(`请求 ${url} 失败,已尝试 ${retry.attempts} 次:${describeError(lastError)}`);
|
|
432
|
+
}
|
|
433
|
+
__name(withRetries, "withRetries");
|
|
434
|
+
async function fetchText(url, timeoutMs, init = {}, retry = { attempts: 1, retryDelay: 0 }) {
|
|
435
|
+
return withRetries(url, retry, async () => {
|
|
411
436
|
const controller = new AbortController();
|
|
412
437
|
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
413
438
|
try {
|
|
@@ -416,19 +441,49 @@ async function fetchText(url, timeoutMs, init = {}, retry = { attempts: 1, retry
|
|
|
416
441
|
throw new Error(`${response.status} ${response.statusText}`);
|
|
417
442
|
}
|
|
418
443
|
return await response.text();
|
|
419
|
-
} catch (error) {
|
|
420
|
-
lastError = error;
|
|
421
444
|
} finally {
|
|
422
445
|
clearTimeout(timer);
|
|
423
446
|
}
|
|
424
|
-
|
|
425
|
-
await new Promise((resolve) => setTimeout(resolve, retry.retryDelay * attempt));
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
const reason = lastError instanceof Error ? `${lastError.name}: ${lastError.message}` : String(lastError);
|
|
429
|
-
throw new Error(`请求 ${url} 失败,已尝试 ${retry.attempts} 次:${reason}`);
|
|
447
|
+
});
|
|
430
448
|
}
|
|
431
449
|
__name(fetchText, "fetchText");
|
|
450
|
+
function postJsonText(url, timeoutMs, retry, body = "{}") {
|
|
451
|
+
return withRetries(url, retry, () => new Promise((resolve, reject) => {
|
|
452
|
+
const endpoint = new URL(url);
|
|
453
|
+
const request = (0, import_node_http.request)({
|
|
454
|
+
protocol: endpoint.protocol,
|
|
455
|
+
hostname: endpoint.hostname,
|
|
456
|
+
port: endpoint.port || void 0,
|
|
457
|
+
method: "POST",
|
|
458
|
+
path: `${endpoint.pathname}${endpoint.search}`,
|
|
459
|
+
agent: false,
|
|
460
|
+
headers: {
|
|
461
|
+
"content-type": "application/json",
|
|
462
|
+
"content-length": Buffer.byteLength(body),
|
|
463
|
+
"connection": "close",
|
|
464
|
+
"user-agent": "koishi-plugin-rina-currency/0.1"
|
|
465
|
+
}
|
|
466
|
+
}, (response) => {
|
|
467
|
+
let responseBody = "";
|
|
468
|
+
response.setEncoding("utf8");
|
|
469
|
+
response.on("data", (chunk) => {
|
|
470
|
+
responseBody += chunk;
|
|
471
|
+
});
|
|
472
|
+
response.on("error", reject);
|
|
473
|
+
response.on("end", () => {
|
|
474
|
+
if (response.statusCode == null || response.statusCode < 200 || response.statusCode >= 300) {
|
|
475
|
+
reject(new Error(`${response.statusCode || 0} ${response.statusMessage || ""}`.trim()));
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
resolve(responseBody);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
request.setTimeout(timeoutMs, () => request.destroy(new Error(`请求超时(${timeoutMs}ms)`)));
|
|
482
|
+
request.on("error", reject);
|
|
483
|
+
request.end(body);
|
|
484
|
+
}));
|
|
485
|
+
}
|
|
486
|
+
__name(postJsonText, "postJsonText");
|
|
432
487
|
function asRate(value, field) {
|
|
433
488
|
const parsed = Number.parseFloat(String(value).trim());
|
|
434
489
|
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
@@ -524,14 +579,10 @@ var IcbcProvider = class {
|
|
|
524
579
|
}
|
|
525
580
|
id = "icbc";
|
|
526
581
|
async fetch() {
|
|
527
|
-
const body = await
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
"user-agent": "koishi-plugin-rina-currency/0.1"
|
|
532
|
-
},
|
|
533
|
-
body: "{}"
|
|
534
|
-
}, { attempts: this.attempts, retryDelay: this.retryDelay });
|
|
582
|
+
const body = await postJsonText(ICBC_SOURCE_URL, this.timeoutMs, {
|
|
583
|
+
attempts: this.attempts,
|
|
584
|
+
retryDelay: this.retryDelay
|
|
585
|
+
});
|
|
535
586
|
return parseIcbcJpy(JSON.parse(body));
|
|
536
587
|
}
|
|
537
588
|
};
|
|
@@ -582,19 +633,12 @@ var Scheduler = class {
|
|
|
582
633
|
} else if (this.config.collectOnStart) {
|
|
583
634
|
await this.collectAndAlert();
|
|
584
635
|
}
|
|
585
|
-
this.
|
|
586
|
-
|
|
636
|
+
this.ctx.cron("* * * * *", () => {
|
|
637
|
+
void this.collectIfDue();
|
|
638
|
+
void this.runBroadcasts();
|
|
639
|
+
});
|
|
587
640
|
});
|
|
588
641
|
}
|
|
589
|
-
/** Runs on China-time minute boundaries, with 17:00 as the daily anchor. */
|
|
590
|
-
scheduleAlignedCollections() {
|
|
591
|
-
const now = /* @__PURE__ */ new Date();
|
|
592
|
-
const delay = 6e4 - now.getSeconds() * 1e3 - now.getMilliseconds();
|
|
593
|
-
this.ctx.setTimeout(() => {
|
|
594
|
-
void this.collectIfDue();
|
|
595
|
-
this.ctx.setInterval(() => void this.collectIfDue(), 6e4);
|
|
596
|
-
}, delay);
|
|
597
|
-
}
|
|
598
642
|
getDueCollection(now = /* @__PURE__ */ new Date()) {
|
|
599
643
|
const clock = chinaClock(now);
|
|
600
644
|
const minutesSinceMidnight = clock.hour * 60 + clock.minute;
|
|
@@ -785,7 +829,7 @@ var RateService = class {
|
|
|
785
829
|
|
|
786
830
|
// src/index.ts
|
|
787
831
|
var name = "rina-currency";
|
|
788
|
-
var inject = ["database"];
|
|
832
|
+
var inject = ["database", "cron"];
|
|
789
833
|
var Config2 = Config;
|
|
790
834
|
function apply(ctx, config) {
|
|
791
835
|
extendModel(ctx);
|
|
@@ -10,5 +10,11 @@ export interface RetryOptions {
|
|
|
10
10
|
retryDelay: number;
|
|
11
11
|
}
|
|
12
12
|
export declare function fetchText(url: string, timeoutMs: number, init?: RequestInit, retry?: RetryOptions): Promise<string>;
|
|
13
|
+
/**
|
|
14
|
+
* ICBC's legacy public endpoint is plain HTTP. Keep this request on a fresh
|
|
15
|
+
* Node socket instead of the process-wide fetch pool, which can retain a
|
|
16
|
+
* connection that the upstream has already closed.
|
|
17
|
+
*/
|
|
18
|
+
export declare function postJsonText(url: string, timeoutMs: number, retry: RetryOptions, body?: string): Promise<string>;
|
|
13
19
|
export declare function asRate(value: unknown, field: string): number;
|
|
14
20
|
export declare function chinaTime(date: string, time?: string): Date;
|
package/lib/scheduler.d.ts
CHANGED
|
@@ -10,8 +10,6 @@ export declare class Scheduler {
|
|
|
10
10
|
private lastCollectionSlot?;
|
|
11
11
|
constructor(ctx: Context, logger: Logger, service: RateService, config: Config);
|
|
12
12
|
start(): void;
|
|
13
|
-
/** Runs on China-time minute boundaries, with 17:00 as the daily anchor. */
|
|
14
|
-
private scheduleAlignedCollections;
|
|
15
13
|
private getDueCollection;
|
|
16
14
|
private collectIfDue;
|
|
17
15
|
private getBot;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koishi-plugin-rina-currency",
|
|
3
3
|
"description": "面向购汇决策的日元汇率插件,基于中行和工行数据",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.5",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"typings": "lib/index.d.ts",
|
|
7
7
|
"files": [
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"koishi": "^4.18.7"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"koishi-plugin-cron": "^3.1.0",
|
|
24
25
|
"sharp": "^0.34.2"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
@@ -34,7 +35,8 @@
|
|
|
34
35
|
},
|
|
35
36
|
"service": {
|
|
36
37
|
"required": [
|
|
37
|
-
"database"
|
|
38
|
+
"database",
|
|
39
|
+
"cron"
|
|
38
40
|
]
|
|
39
41
|
}
|
|
40
42
|
}
|
package/readme.md
CHANGED