dolphindb 3.0.224 → 3.0.227
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.d.ts +22 -297
- package/browser.js +39 -464
- package/browser.js.map +1 -1
- package/common.d.ts +300 -0
- package/common.js +465 -0
- package/common.js.map +1 -0
- package/i18n/dict.json +3 -0
- package/index.d.ts +23 -298
- package/index.js +39 -464
- package/index.js.map +1 -1
- package/package.json +4 -4
package/browser.js
CHANGED
|
@@ -1,145 +1,10 @@
|
|
|
1
|
-
import { default as dayjs } from 'dayjs';
|
|
2
|
-
import DayjsCustomParseFormat from 'dayjs/plugin/customParseFormat.js';
|
|
3
|
-
dayjs.extend(DayjsCustomParseFormat);
|
|
4
|
-
import ipaddrjs from 'ipaddr.js';
|
|
5
|
-
const { fromByteArray: buf2ipaddr } = ipaddrjs;
|
|
6
1
|
import 'xshell/prototype.browser.js';
|
|
7
2
|
import { blue, cyan, green, grey, magenta } from 'xshell/chalk.browser.js';
|
|
8
3
|
import { concat, assert, Lock, genid, seq, zip_object, decode, delay, check } from 'xshell/utils.browser.js';
|
|
9
4
|
import { connect_websocket } from 'xshell/net.browser.js';
|
|
10
5
|
import { t } from "./i18n/index.js";
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
int16: -0x80_00, // -32768
|
|
14
|
-
int32: -0x80_00_00_00, // -21_4748_3648
|
|
15
|
-
int64: -0x8000000000000000n, // -922_3372_0368_5477_5808
|
|
16
|
-
int128: -0x80000000000000000000000000000000n, // -170_1411_8346_0469_2317_3168_7303_7158_8410_5728
|
|
17
|
-
float32: -3.4028234663852886e+38,
|
|
18
|
-
/** -Number.MAX_VALUE */
|
|
19
|
-
double: -Number.MAX_VALUE,
|
|
20
|
-
bytes16: Uint8Array.from(new Array(16).fill(0))
|
|
21
|
-
};
|
|
22
|
-
export var DdbForm;
|
|
23
|
-
(function (DdbForm) {
|
|
24
|
-
DdbForm[DdbForm["scalar"] = 0] = "scalar";
|
|
25
|
-
DdbForm[DdbForm["vector"] = 1] = "vector";
|
|
26
|
-
DdbForm[DdbForm["pair"] = 2] = "pair";
|
|
27
|
-
DdbForm[DdbForm["matrix"] = 3] = "matrix";
|
|
28
|
-
DdbForm[DdbForm["set"] = 4] = "set";
|
|
29
|
-
DdbForm[DdbForm["dict"] = 5] = "dict";
|
|
30
|
-
DdbForm[DdbForm["table"] = 6] = "table";
|
|
31
|
-
DdbForm[DdbForm["chart"] = 7] = "chart";
|
|
32
|
-
/** 节点内部通信可能会使用,调用函数执行脚本一般不会返回这种类型 */
|
|
33
|
-
DdbForm[DdbForm["chunk"] = 8] = "chunk";
|
|
34
|
-
/** sysobj */
|
|
35
|
-
DdbForm[DdbForm["object"] = 9] = "object";
|
|
36
|
-
DdbForm[DdbForm["tensor"] = 10] = "tensor";
|
|
37
|
-
})(DdbForm || (DdbForm = {}));
|
|
38
|
-
/** DolphinDB DataType
|
|
39
|
-
对应的 array vector 类型为 64 + 基本类型
|
|
40
|
-
对应的 extended 类型为 128 + 基本类型 */
|
|
41
|
-
export var DdbType;
|
|
42
|
-
(function (DdbType) {
|
|
43
|
-
DdbType[DdbType["void"] = 0] = "void";
|
|
44
|
-
DdbType[DdbType["bool"] = 1] = "bool";
|
|
45
|
-
DdbType[DdbType["char"] = 2] = "char";
|
|
46
|
-
DdbType[DdbType["short"] = 3] = "short";
|
|
47
|
-
DdbType[DdbType["int"] = 4] = "int";
|
|
48
|
-
DdbType[DdbType["long"] = 5] = "long";
|
|
49
|
-
DdbType[DdbType["date"] = 6] = "date";
|
|
50
|
-
DdbType[DdbType["month"] = 7] = "month";
|
|
51
|
-
DdbType[DdbType["time"] = 8] = "time";
|
|
52
|
-
DdbType[DdbType["minute"] = 9] = "minute";
|
|
53
|
-
DdbType[DdbType["second"] = 10] = "second";
|
|
54
|
-
DdbType[DdbType["datetime"] = 11] = "datetime";
|
|
55
|
-
DdbType[DdbType["timestamp"] = 12] = "timestamp";
|
|
56
|
-
DdbType[DdbType["nanotime"] = 13] = "nanotime";
|
|
57
|
-
DdbType[DdbType["nanotimestamp"] = 14] = "nanotimestamp";
|
|
58
|
-
DdbType[DdbType["float"] = 15] = "float";
|
|
59
|
-
DdbType[DdbType["double"] = 16] = "double";
|
|
60
|
-
DdbType[DdbType["symbol"] = 17] = "symbol";
|
|
61
|
-
DdbType[DdbType["string"] = 18] = "string";
|
|
62
|
-
DdbType[DdbType["uuid"] = 19] = "uuid";
|
|
63
|
-
DdbType[DdbType["functiondef"] = 20] = "functiondef";
|
|
64
|
-
DdbType[DdbType["handle"] = 21] = "handle";
|
|
65
|
-
DdbType[DdbType["code"] = 22] = "code";
|
|
66
|
-
DdbType[DdbType["datasource"] = 23] = "datasource";
|
|
67
|
-
DdbType[DdbType["resource"] = 24] = "resource";
|
|
68
|
-
DdbType[DdbType["any"] = 25] = "any";
|
|
69
|
-
DdbType[DdbType["compressed"] = 26] = "compressed";
|
|
70
|
-
DdbType[DdbType["dict"] = 27] = "dict";
|
|
71
|
-
DdbType[DdbType["datehour"] = 28] = "datehour";
|
|
72
|
-
DdbType[DdbType["ipaddr"] = 30] = "ipaddr";
|
|
73
|
-
DdbType[DdbType["int128"] = 31] = "int128";
|
|
74
|
-
DdbType[DdbType["blob"] = 32] = "blob";
|
|
75
|
-
DdbType[DdbType["complex"] = 34] = "complex";
|
|
76
|
-
DdbType[DdbType["point"] = 35] = "point";
|
|
77
|
-
DdbType[DdbType["duration"] = 36] = "duration";
|
|
78
|
-
DdbType[DdbType["decimal32"] = 37] = "decimal32";
|
|
79
|
-
DdbType[DdbType["decimal64"] = 38] = "decimal64";
|
|
80
|
-
DdbType[DdbType["decimal128"] = 39] = "decimal128";
|
|
81
|
-
DdbType[DdbType["object"] = 40] = "object";
|
|
82
|
-
DdbType[DdbType["iotany"] = 41] = "iotany";
|
|
83
|
-
DdbType[DdbType["symbol_extended"] = 145] = "symbol_extended";
|
|
84
|
-
})(DdbType || (DdbType = {}));
|
|
85
|
-
export var DdbFunctionType;
|
|
86
|
-
(function (DdbFunctionType) {
|
|
87
|
-
DdbFunctionType[DdbFunctionType["SystemFunc"] = 0] = "SystemFunc";
|
|
88
|
-
DdbFunctionType[DdbFunctionType["SystemProc"] = 1] = "SystemProc";
|
|
89
|
-
DdbFunctionType[DdbFunctionType["OperatorFunc"] = 2] = "OperatorFunc";
|
|
90
|
-
DdbFunctionType[DdbFunctionType["UserDefinedFunc"] = 3] = "UserDefinedFunc";
|
|
91
|
-
DdbFunctionType[DdbFunctionType["PartialFunc"] = 4] = "PartialFunc";
|
|
92
|
-
DdbFunctionType[DdbFunctionType["DynamicFunc"] = 5] = "DynamicFunc";
|
|
93
|
-
DdbFunctionType[DdbFunctionType["PiecewiseFunc"] = 6] = "PiecewiseFunc";
|
|
94
|
-
DdbFunctionType[DdbFunctionType["JitFunc"] = 7] = "JitFunc";
|
|
95
|
-
DdbFunctionType[DdbFunctionType["JitPartialFunc"] = 8] = "JitPartialFunc";
|
|
96
|
-
})(DdbFunctionType || (DdbFunctionType = {}));
|
|
97
|
-
export var DdbDurationUnit;
|
|
98
|
-
(function (DdbDurationUnit) {
|
|
99
|
-
DdbDurationUnit[DdbDurationUnit["ns"] = 0] = "ns";
|
|
100
|
-
DdbDurationUnit[DdbDurationUnit["us"] = 1] = "us";
|
|
101
|
-
DdbDurationUnit[DdbDurationUnit["ms"] = 2] = "ms";
|
|
102
|
-
DdbDurationUnit[DdbDurationUnit["s"] = 3] = "s";
|
|
103
|
-
DdbDurationUnit[DdbDurationUnit["m"] = 4] = "m";
|
|
104
|
-
DdbDurationUnit[DdbDurationUnit["H"] = 5] = "H";
|
|
105
|
-
DdbDurationUnit[DdbDurationUnit["d"] = 6] = "d";
|
|
106
|
-
DdbDurationUnit[DdbDurationUnit["w"] = 7] = "w";
|
|
107
|
-
DdbDurationUnit[DdbDurationUnit["M"] = 8] = "M";
|
|
108
|
-
DdbDurationUnit[DdbDurationUnit["y"] = 9] = "y";
|
|
109
|
-
DdbDurationUnit[DdbDurationUnit["B"] = 10] = "B";
|
|
110
|
-
})(DdbDurationUnit || (DdbDurationUnit = {}));
|
|
111
|
-
export var DdbChartType;
|
|
112
|
-
(function (DdbChartType) {
|
|
113
|
-
DdbChartType[DdbChartType["area"] = 0] = "area";
|
|
114
|
-
DdbChartType[DdbChartType["bar"] = 1] = "bar";
|
|
115
|
-
DdbChartType[DdbChartType["column"] = 2] = "column";
|
|
116
|
-
DdbChartType[DdbChartType["histogram"] = 3] = "histogram";
|
|
117
|
-
DdbChartType[DdbChartType["line"] = 4] = "line";
|
|
118
|
-
DdbChartType[DdbChartType["pie"] = 5] = "pie";
|
|
119
|
-
DdbChartType[DdbChartType["scatter"] = 6] = "scatter";
|
|
120
|
-
DdbChartType[DdbChartType["trend"] = 7] = "trend";
|
|
121
|
-
DdbChartType[DdbChartType["kline"] = 8] = "kline";
|
|
122
|
-
})(DdbChartType || (DdbChartType = {}));
|
|
123
|
-
// server 实现中区分了 0: NULL (nothing), 1: NULL (null), 2: DFLT (default)
|
|
124
|
-
// Void::serialize()
|
|
125
|
-
// (isNothing() ? 0 : 1) + (isDefault_ ? 2 : 0);
|
|
126
|
-
export var DdbVoidType;
|
|
127
|
-
(function (DdbVoidType) {
|
|
128
|
-
DdbVoidType[DdbVoidType["undefined"] = 0] = "undefined";
|
|
129
|
-
DdbVoidType[DdbVoidType["null"] = 1] = "null";
|
|
130
|
-
DdbVoidType[DdbVoidType["default"] = 2] = "default";
|
|
131
|
-
})(DdbVoidType || (DdbVoidType = {}));
|
|
132
|
-
export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long]);
|
|
133
|
-
/** 工具,取得某个 DdbType 的字节数 */
|
|
134
|
-
export const ddb_tensor_bytes = {
|
|
135
|
-
[DdbType.bool]: 1,
|
|
136
|
-
[DdbType.char]: 1,
|
|
137
|
-
[DdbType.short]: 2,
|
|
138
|
-
[DdbType.int]: 4,
|
|
139
|
-
[DdbType.long]: 8,
|
|
140
|
-
[DdbType.float]: 4,
|
|
141
|
-
[DdbType.double]: 8,
|
|
142
|
-
};
|
|
6
|
+
import { date2str, datehour2str, datetime2str, ddb_tensor_bytes, DdbChartType, DdbDurationUnit, DdbForm, DdbFunctionType, DdbType, DdbVoidType, dictables, generate_array_type, get_big_int_128, get_ddb_time_value, get_duration_unit, get_type_name, int1282str, ipaddr2str, is_decimal_null_value, is_decimal_type, minute2str, month2str, nanotime2str, nanotimestamp2str, nulls, second2str, set_big_int_128, time2str, timestamp2str, uuid2str } from "./common.js";
|
|
7
|
+
export * from "./common.js";
|
|
143
8
|
/** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
|
|
144
9
|
export class DdbObj {
|
|
145
10
|
static dec = new TextDecoder('utf-8');
|
|
@@ -1672,33 +1537,12 @@ export class DdbObj {
|
|
|
1672
1537
|
return obj;
|
|
1673
1538
|
}
|
|
1674
1539
|
}
|
|
1675
|
-
/** 根据 DdbType 获取其名称,array vector type 自动在后面加上 [] */
|
|
1676
|
-
export function get_type_name(type) {
|
|
1677
|
-
return `${DdbType[type] || type}${64 <= type && type < 128 ? '[]' : ''}`;
|
|
1678
|
-
}
|
|
1679
|
-
export function is_decimal_type(type) {
|
|
1680
|
-
return type === DdbType.decimal32 || type === DdbType.decimal64 || type === DdbType.decimal128;
|
|
1681
|
-
}
|
|
1682
|
-
export function is_decimal_null_value(type, value) {
|
|
1683
|
-
return ((value === nulls.int128 && type === DdbType.decimal128) ||
|
|
1684
|
-
(value === nulls.int64 && type === DdbType.decimal64) ||
|
|
1685
|
-
(value === nulls.int32 && type === DdbType.decimal32));
|
|
1686
|
-
}
|
|
1687
|
-
export function get_duration_unit(code) {
|
|
1688
|
-
let str = String.fromCharCode((code >> 24) & 0xff);
|
|
1689
|
-
str += String.fromCharCode((code >> 16) & 0xff);
|
|
1690
|
-
str += String.fromCharCode((code >> 8) & 0xff);
|
|
1691
|
-
str += String.fromCharCode(code & 0xff);
|
|
1692
|
-
return str;
|
|
1693
|
-
}
|
|
1694
1540
|
/** 整数一定用这个 number formatter, InspectOptions.decimals 不传也用这个 */
|
|
1695
1541
|
let default_formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
|
|
1696
1542
|
let _decimals = 20;
|
|
1697
1543
|
let _grouping = true;
|
|
1698
1544
|
/** 缓存,为了优化性能,通常 options.decimals 都是不变的 */
|
|
1699
1545
|
let _formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
|
|
1700
|
-
/** 用来处理时差 To deal with jet lag */
|
|
1701
|
-
let _datetime_formatter = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'short', timeStyle: 'medium', timeZone: 'UTC', hour12: false });
|
|
1702
1546
|
/** 根据 DdbType 格式化单个元素 (value) 为字符串 */
|
|
1703
1547
|
export function format(type, value, le, options = {}) {
|
|
1704
1548
|
const { nullstr = false, colors = false, quote = false, grouping = true, timestamp = 'ms' } = options;
|
|
@@ -2258,33 +2102,6 @@ export class DdbDate extends DdbObj {
|
|
|
2258
2102
|
});
|
|
2259
2103
|
}
|
|
2260
2104
|
}
|
|
2261
|
-
function get_ddb_time_value(classname, value) {
|
|
2262
|
-
if (value === null)
|
|
2263
|
-
return null;
|
|
2264
|
-
if (classname === 'DdbNanoTimeStamp' && typeof value === 'string')
|
|
2265
|
-
return str2nanotimestamp(value);
|
|
2266
|
-
let date;
|
|
2267
|
-
if (value === undefined)
|
|
2268
|
-
date = new Date();
|
|
2269
|
-
else if (typeof value === 'number' || typeof value === 'string')
|
|
2270
|
-
date = new Date(value);
|
|
2271
|
-
else if (value instanceof Date)
|
|
2272
|
-
date = value;
|
|
2273
|
-
else if (dayjs.isDayjs(value))
|
|
2274
|
-
date = new Date(value.valueOf());
|
|
2275
|
-
else
|
|
2276
|
-
throw new Error(t('value 不能转换为 {{classname}}', { classname }));
|
|
2277
|
-
switch (classname) {
|
|
2278
|
-
case 'DdbDateTime':
|
|
2279
|
-
return (date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / 1000;
|
|
2280
|
-
case 'DdbTimeStamp':
|
|
2281
|
-
return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset());
|
|
2282
|
-
case 'DdbNanoTimeStamp':
|
|
2283
|
-
return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset()) * 1000000n;
|
|
2284
|
-
case 'DdbDate':
|
|
2285
|
-
return Math.floor((date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / (1000 * 3600 * 24));
|
|
2286
|
-
}
|
|
2287
|
-
}
|
|
2288
2105
|
export class DdbBlob extends DdbObj {
|
|
2289
2106
|
constructor(value) {
|
|
2290
2107
|
assert(value, t('new DdbBlob 不能传空的 value'));
|
|
@@ -2540,230 +2357,6 @@ export class DdbTable extends DdbObj {
|
|
|
2540
2357
|
});
|
|
2541
2358
|
}
|
|
2542
2359
|
}
|
|
2543
|
-
export function date2ms(date) {
|
|
2544
|
-
// 将 server 的本地时间 (以 ms 为单位,1970.01.01 00:00:00 作为零点) 作为 UTC-0 格式化为字符串,然后根据本地的时区解析这个字符串转换为 UTC-8
|
|
2545
|
-
// 本地的时区与实际的时间值相关,getTimezoneOffset() 可能会受到夏令时 (DST) 的影响,不能使用
|
|
2546
|
-
// 得到的 utc 毫秒数交给 js date 或者 dayjs 去格式化
|
|
2547
|
-
if (date === null || date === nulls.int32)
|
|
2548
|
-
return null;
|
|
2549
|
-
const ms = 1000 * 3600 * 24 * date;
|
|
2550
|
-
return timestamp2ms(ms);
|
|
2551
|
-
}
|
|
2552
|
-
export function date2str(date, format = 'YYYY.MM.DD') {
|
|
2553
|
-
return (date === null || date === nulls.int32) ?
|
|
2554
|
-
'null'
|
|
2555
|
-
:
|
|
2556
|
-
dayjs(date2ms(date)).format(format);
|
|
2557
|
-
}
|
|
2558
|
-
export function month2ms(month) {
|
|
2559
|
-
return (month === null || month === nulls.int32) ?
|
|
2560
|
-
null
|
|
2561
|
-
:
|
|
2562
|
-
dayjs(month2str(month), 'YYYY.MM[M]').valueOf();
|
|
2563
|
-
}
|
|
2564
|
-
export function month2str(month) {
|
|
2565
|
-
if (month === null || month === nulls.int32)
|
|
2566
|
-
return 'null';
|
|
2567
|
-
if (month < 0)
|
|
2568
|
-
return String(month);
|
|
2569
|
-
const _month = month % 12;
|
|
2570
|
-
const year = Math.trunc(month / 12);
|
|
2571
|
-
return `${String(year).padStart(4, '0')}.${String(_month + 1).padStart(2, '0')}M`;
|
|
2572
|
-
}
|
|
2573
|
-
export function time2ms(time) {
|
|
2574
|
-
return (time === null || time === nulls.int32) ?
|
|
2575
|
-
null
|
|
2576
|
-
:
|
|
2577
|
-
timestamp2ms(time);
|
|
2578
|
-
}
|
|
2579
|
-
export function time2str(time, format = 'HH:mm:ss.SSS') {
|
|
2580
|
-
return (time === null || time === nulls.int32) ?
|
|
2581
|
-
'null'
|
|
2582
|
-
:
|
|
2583
|
-
dayjs(time2ms(time)).format(format);
|
|
2584
|
-
}
|
|
2585
|
-
export function minute2ms(minute) {
|
|
2586
|
-
if (minute === null || minute === nulls.int32)
|
|
2587
|
-
return null;
|
|
2588
|
-
const ms = 60 * 1000 * minute;
|
|
2589
|
-
return timestamp2ms(ms);
|
|
2590
|
-
}
|
|
2591
|
-
export function minute2str(minute, format = 'HH:mm[m]') {
|
|
2592
|
-
return (minute === null || minute === nulls.int32) ?
|
|
2593
|
-
'null'
|
|
2594
|
-
:
|
|
2595
|
-
dayjs(minute2ms(minute)).format(format);
|
|
2596
|
-
}
|
|
2597
|
-
export function second2ms(second) {
|
|
2598
|
-
if (second === null || second === nulls.int32)
|
|
2599
|
-
return null;
|
|
2600
|
-
const ms = 1000 * second;
|
|
2601
|
-
return timestamp2ms(ms);
|
|
2602
|
-
}
|
|
2603
|
-
export function second2str(second, format = 'HH:mm:ss') {
|
|
2604
|
-
return (second === null || second === nulls.int32) ?
|
|
2605
|
-
'null'
|
|
2606
|
-
:
|
|
2607
|
-
dayjs(second2ms(second)).format(format);
|
|
2608
|
-
}
|
|
2609
|
-
export function datetime2ms(datetime) {
|
|
2610
|
-
if (datetime === null || datetime === nulls.int32)
|
|
2611
|
-
return null;
|
|
2612
|
-
const date = new Date(1000 * datetime);
|
|
2613
|
-
return new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf();
|
|
2614
|
-
}
|
|
2615
|
-
export function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
|
|
2616
|
-
return (datetime === null || datetime === nulls.int32) ?
|
|
2617
|
-
'null'
|
|
2618
|
-
:
|
|
2619
|
-
dayjs(datetime2ms(datetime)).format(format);
|
|
2620
|
-
}
|
|
2621
|
-
/** _datetime_formatter.format 会在 date 为 Invalid Date 时抛出错误 */
|
|
2622
|
-
export function timestamp2ms(timestamp) {
|
|
2623
|
-
if (timestamp === null || timestamp === nulls.int64)
|
|
2624
|
-
return null;
|
|
2625
|
-
const date = new Date(Number(timestamp));
|
|
2626
|
-
return new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf();
|
|
2627
|
-
}
|
|
2628
|
-
/** format timestamp (bigint) to string
|
|
2629
|
-
- timestamp: bigint value
|
|
2630
|
-
- format?:
|
|
2631
|
-
格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS` format string, default to `YYYY.MM.DD HH:mm:ss.SSS`
|
|
2632
|
-
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
|
|
2633
|
-
export function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
|
|
2634
|
-
return (timestamp === null || timestamp === nulls.int64) ?
|
|
2635
|
-
'null'
|
|
2636
|
-
:
|
|
2637
|
-
dayjs(timestamp2ms(timestamp)).format(format);
|
|
2638
|
-
}
|
|
2639
|
-
export function datehour2ms(datehour) {
|
|
2640
|
-
if (datehour === null || datehour === nulls.int32)
|
|
2641
|
-
return null;
|
|
2642
|
-
const ms = 1000 * 3600 * datehour;
|
|
2643
|
-
return timestamp2ms(ms);
|
|
2644
|
-
}
|
|
2645
|
-
export function datehour2str(datehour, format = 'YYYY.MM.DDTHH') {
|
|
2646
|
-
if (datehour === null || datehour === nulls.int32)
|
|
2647
|
-
return 'null';
|
|
2648
|
-
const ms = 1000 * 3600 * datehour;
|
|
2649
|
-
return dayjs(timestamp2ms(ms)).format(format);
|
|
2650
|
-
}
|
|
2651
|
-
/** parse timestamp string to bigint value
|
|
2652
|
-
- str: timestamp string, 如果为空字符串或 'null' 会返回对应的空值 (nulls.int64)
|
|
2653
|
-
timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
|
|
2654
|
-
- format?:
|
|
2655
|
-
对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS`
|
|
2656
|
-
The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
|
|
2657
|
-
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
|
|
2658
|
-
export function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
|
|
2659
|
-
if (!str || str === 'null')
|
|
2660
|
-
return nulls.int64;
|
|
2661
|
-
assert(str.length === format.length, t('timestamp 字符串长度必须等于格式串长度'));
|
|
2662
|
-
const ms = dayjs(str, format).valueOf();
|
|
2663
|
-
return BigInt(-(1000 * 60 * new Date(ms).getTimezoneOffset()) +
|
|
2664
|
-
ms);
|
|
2665
|
-
}
|
|
2666
|
-
export function nanotime2ns(nanotime) {
|
|
2667
|
-
return nanotimestamp2ns(nanotime);
|
|
2668
|
-
}
|
|
2669
|
-
export function nanotime2str(nanotime, format = 'HH:mm:ss.SSSSSSSSS') {
|
|
2670
|
-
if (nanotime === null || nanotime === nulls.int64)
|
|
2671
|
-
return 'null';
|
|
2672
|
-
if (nanotime < 0n)
|
|
2673
|
-
return String(nanotime);
|
|
2674
|
-
const i_second_start = format.indexOf('ss');
|
|
2675
|
-
assert(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
|
|
2676
|
-
const i_second_end = i_second_start + 2;
|
|
2677
|
-
const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
|
|
2678
|
-
assert(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
|
|
2679
|
-
const ms = Number(nanotime) / 1000000;
|
|
2680
|
-
return (dayjs(timestamp2ms(ms)).format(format.slice(0, i_second_end)) +
|
|
2681
|
-
format.slice(i_second_end, i_nanosecond_start) +
|
|
2682
|
-
String(nanotime % 1000000000n).padStart(9, '0'));
|
|
2683
|
-
}
|
|
2684
|
-
export function nanotimestamp2ns(nanotimestamp) {
|
|
2685
|
-
if (nanotimestamp === null || nanotimestamp === nulls.int64)
|
|
2686
|
-
return null;
|
|
2687
|
-
const date = new Date(Number(nanotimestamp / 1000000n));
|
|
2688
|
-
return BigInt(new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf()) * 1000000n + nanotimestamp % 1000000n;
|
|
2689
|
-
}
|
|
2690
|
-
/** format nanotimestamp value (bigint) to string
|
|
2691
|
-
- nanotimestamp: bigint value
|
|
2692
|
-
- format?:
|
|
2693
|
-
格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSSSSSSSS` format string, default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
|
|
2694
|
-
秒的格式为 ss (必须包含); 纳秒的格式为 SSSSSSSSS (必须包含) Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
|
|
2695
|
-
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
2696
|
-
*/
|
|
2697
|
-
export function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
|
|
2698
|
-
// tests:
|
|
2699
|
-
// nanotimestamp2str(0n)
|
|
2700
|
-
// nanotimestamp2str(-1n)
|
|
2701
|
-
// nanotimestamp2str(-9_9999_9999n)
|
|
2702
|
-
// nanotimestamp2str(-10_0000_0000n)
|
|
2703
|
-
// nanotimestamp2str(-10_0000_0001n)
|
|
2704
|
-
if (nanotimestamp === null || nanotimestamp === nulls.int64)
|
|
2705
|
-
return 'null';
|
|
2706
|
-
const i_second_start = format.indexOf('ss');
|
|
2707
|
-
assert(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
|
|
2708
|
-
const i_second_end = i_second_start + 2;
|
|
2709
|
-
const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
|
|
2710
|
-
assert(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
|
|
2711
|
-
const remainder = nanotimestamp % 1000000000n;
|
|
2712
|
-
const borrow = remainder < 0n;
|
|
2713
|
-
const ms = Number((nanotimestamp - remainder + (borrow ? -1000000000n : 0n)) / 1000000n);
|
|
2714
|
-
return (dayjs(_datetime_formatter.format(new Date(ms))).format(format.slice(0, i_second_end)) +
|
|
2715
|
-
format.slice(i_second_end, i_nanosecond_start) +
|
|
2716
|
-
String(borrow ?
|
|
2717
|
-
(remainder + 1000000000n) % 1000000000n
|
|
2718
|
-
:
|
|
2719
|
-
remainder).padStart(9, '0'));
|
|
2720
|
-
}
|
|
2721
|
-
/** parse nano timestamp string to bigint value
|
|
2722
|
-
- str: nano timestamp string, 如果为空字符串或 'null' 会返回对应的空值 (nulls.int64)
|
|
2723
|
-
nano timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
|
|
2724
|
-
- format?:
|
|
2725
|
-
对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
|
|
2726
|
-
秒的格式为 ss (必须包含); 纳秒的格式为 SSSSSSSSS (必须包含)
|
|
2727
|
-
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
2728
|
-
The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
|
|
2729
|
-
Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included) */
|
|
2730
|
-
export function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
|
|
2731
|
-
if (!str || str === 'null')
|
|
2732
|
-
return nulls.int64;
|
|
2733
|
-
check(str.length === format.length, t('nanotimestamp 字符串长度必须等于格式串长度'));
|
|
2734
|
-
const i_second_start = format.indexOf('ss');
|
|
2735
|
-
check(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
|
|
2736
|
-
const i_second_end = i_second_start + 2;
|
|
2737
|
-
const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
|
|
2738
|
-
check(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
|
|
2739
|
-
const ms = dayjs(str.slice(0, i_second_end), format.slice(0, i_second_end)).valueOf();
|
|
2740
|
-
return (BigInt(-(1000 * 60 * new Date(ms).getTimezoneOffset()) +
|
|
2741
|
-
ms) * 1000000n
|
|
2742
|
-
+
|
|
2743
|
-
BigInt(str.slice(i_nanosecond_start, i_nanosecond_start + 9)));
|
|
2744
|
-
}
|
|
2745
|
-
export function ipaddr2str(buffer, le = true, ipv6) {
|
|
2746
|
-
let buf = buffer;
|
|
2747
|
-
if (le)
|
|
2748
|
-
buf = buffer.slice().reverse();
|
|
2749
|
-
const i_non_zero = buf.findIndex(x => x !== 0);
|
|
2750
|
-
if (ipv6 ||
|
|
2751
|
-
i_non_zero !== -1 && i_non_zero < 12) // ipv6
|
|
2752
|
-
return buf2ipaddr([...buf]).toString();
|
|
2753
|
-
else // ipv4
|
|
2754
|
-
return buf.subarray(12).join('.');
|
|
2755
|
-
}
|
|
2756
|
-
export function uuid2str(buffer, le = true) {
|
|
2757
|
-
const str = int1282str(buffer, le);
|
|
2758
|
-
return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`;
|
|
2759
|
-
}
|
|
2760
|
-
export function int1282str(buffer, le = true) {
|
|
2761
|
-
let buf = buffer;
|
|
2762
|
-
if (le)
|
|
2763
|
-
buf = buffer.slice().reverse();
|
|
2764
|
-
return [...buf].map(x => x.toString(16)
|
|
2765
|
-
.padStart(2, '0')).join('');
|
|
2766
|
-
}
|
|
2767
2360
|
export const winsize = 10000;
|
|
2768
2361
|
export class DdbConnectionError extends Error {
|
|
2769
2362
|
name = 'DdbConnectionError';
|
|
@@ -2850,6 +2443,10 @@ export class DDB {
|
|
|
2850
2443
|
pinvoke;
|
|
2851
2444
|
/** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
|
|
2852
2445
|
pjsrpc;
|
|
2446
|
+
/** 函数定义缓存 */
|
|
2447
|
+
definitions = new Map();
|
|
2448
|
+
/** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
|
|
2449
|
+
pdefinitions = new Map();
|
|
2853
2450
|
get connected() {
|
|
2854
2451
|
return !this.error && this.lwebsocket.resource?.readyState === WebSocket.OPEN;
|
|
2855
2452
|
}
|
|
@@ -2992,7 +2589,7 @@ export class DDB {
|
|
|
2992
2589
|
}
|
|
2993
2590
|
});
|
|
2994
2591
|
}
|
|
2995
|
-
get_rpc_options({ urgent = false, secondary = false, async: _async = false, pickle = false, clear = false, api = false, compress = false, cancellable = true, priority = urgent ? 8 : 4, parallelism =
|
|
2592
|
+
get_rpc_options({ urgent = false, secondary = false, async: _async = false, pickle = false, clear = false, api = false, compress = false, cancellable = true, priority = urgent ? 8 : 4, parallelism = 64, root_id = '', limit, } = {}) {
|
|
2996
2593
|
let flag = 0;
|
|
2997
2594
|
if (urgent)
|
|
2998
2595
|
flag += 1;
|
|
@@ -3105,7 +2702,7 @@ export class DDB {
|
|
|
3105
2702
|
throw error;
|
|
3106
2703
|
}
|
|
3107
2704
|
// this 上的当前配置需要在 message 到达后使用,先保存起来
|
|
3108
|
-
const _listeners = [...this.listeners]
|
|
2705
|
+
const _listeners = [...this.listeners];
|
|
3109
2706
|
// rpc 请求期间需要独占 websocket,所以设计了一个锁,申请之后才能使用
|
|
3110
2707
|
// ddb 世界观:需要等待上一个 rpc 结果从 server 返回之后才能发起下一个调用
|
|
3111
2708
|
// 违反世界观可能造成:
|
|
@@ -3178,8 +2775,8 @@ export class DDB {
|
|
|
3178
2775
|
console.log(buffer);
|
|
3179
2776
|
const message = this.parse_message(buffer, error);
|
|
3180
2777
|
listener?.(message, this);
|
|
3181
|
-
for (
|
|
3182
|
-
|
|
2778
|
+
for (let i = _listeners.length - 1; i >= 0; --i)
|
|
2779
|
+
_listeners[i](message, this);
|
|
3183
2780
|
const { type, data } = message;
|
|
3184
2781
|
switch (type) {
|
|
3185
2782
|
case 'print':
|
|
@@ -3566,6 +3163,35 @@ export class DDB {
|
|
|
3566
3163
|
}
|
|
3567
3164
|
})).data());
|
|
3568
3165
|
}
|
|
3166
|
+
/** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
|
|
3167
|
+
也同时作为返回值
|
|
3168
|
+
- definition: 函数完整定义
|
|
3169
|
+
@example
|
|
3170
|
+
await ddb.invoke(
|
|
3171
|
+
await ddb.define(
|
|
3172
|
+
'def foo (bar) {\n' +
|
|
3173
|
+
' print(bar)\n' +
|
|
3174
|
+
'}\n'),
|
|
3175
|
+
['hello']) */
|
|
3176
|
+
async define(definition) {
|
|
3177
|
+
const matches = /\bdef (\w+)\s?\(/.exec(definition);
|
|
3178
|
+
if (!matches)
|
|
3179
|
+
throw new Error(t('DDB.define 方法传入的 definition 不符合函数定义格式'));
|
|
3180
|
+
const func_name = matches[1];
|
|
3181
|
+
// 已定义成功
|
|
3182
|
+
if (this.definitions.has(func_name))
|
|
3183
|
+
return func_name;
|
|
3184
|
+
// 防止并发调用 define 时多次重复定义
|
|
3185
|
+
let promise = this.pdefinitions.get(func_name);
|
|
3186
|
+
if (!promise)
|
|
3187
|
+
this.pdefinitions.set(func_name, promise = this.execute(definition));
|
|
3188
|
+
await promise;
|
|
3189
|
+
this.definitions.set(func_name, definition);
|
|
3190
|
+
// 成功定义之后,definitions 中有了,就不需要通过 promise 来防止并发了,清理掉
|
|
3191
|
+
// 定义失败时 rejected promise 直接作为后续调用的结果返回
|
|
3192
|
+
this.pdefinitions.delete(func_name);
|
|
3193
|
+
return func_name;
|
|
3194
|
+
}
|
|
3569
3195
|
}
|
|
3570
3196
|
/** https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements
|
|
3571
3197
|
SharedArrayBuffer is disabled by default in most browsers as a security precaution to avoid Spectre attacks.
|
|
@@ -3718,55 +3344,4 @@ Object.defineProperty(BigInt128Array.prototype, Symbol.toStringTag, {
|
|
|
3718
3344
|
enumerable: false,
|
|
3719
3345
|
value: 'BigInt128Array',
|
|
3720
3346
|
});
|
|
3721
|
-
// DataView Extends for bigint 128 operations
|
|
3722
|
-
function get_big_uint_128(dataview, byte_offset, le = true) {
|
|
3723
|
-
let cursor = byte_offset + (le ? 15 : 0);
|
|
3724
|
-
const end = byte_offset + (le ? -1 : 16);
|
|
3725
|
-
const step = le ? -1 : 1;
|
|
3726
|
-
let value = 0n;
|
|
3727
|
-
while (cursor !== end) {
|
|
3728
|
-
value = value << 8n | BigInt(dataview.getUint8(cursor));
|
|
3729
|
-
cursor += step;
|
|
3730
|
-
}
|
|
3731
|
-
return value;
|
|
3732
|
-
}
|
|
3733
|
-
function get_big_int_128(dataview, byte_offset, le = true) {
|
|
3734
|
-
return BigInt.asIntN(128, get_big_uint_128(dataview, byte_offset, le));
|
|
3735
|
-
}
|
|
3736
|
-
function set_big_uint_128(dataView, byte_offset, value, le = true) {
|
|
3737
|
-
let cursor = byte_offset + (le ? 0 : 15);
|
|
3738
|
-
const end = byte_offset + (le ? 16 : -1);
|
|
3739
|
-
const step = le ? 1 : -1;
|
|
3740
|
-
while (cursor !== end) {
|
|
3741
|
-
dataView.setUint8(cursor, Number(value & 0xffn));
|
|
3742
|
-
value = value >> 8n;
|
|
3743
|
-
cursor += step;
|
|
3744
|
-
}
|
|
3745
|
-
}
|
|
3746
|
-
function set_big_int_128(dataview, byte_offset, value, le = true) {
|
|
3747
|
-
set_big_uint_128(dataview, byte_offset, value, le);
|
|
3748
|
-
}
|
|
3749
|
-
function generate_array_type(baseType, dimensions) {
|
|
3750
|
-
let result = baseType;
|
|
3751
|
-
dimensions.forEach(dimension => {
|
|
3752
|
-
result += `[${dimension}]`;
|
|
3753
|
-
});
|
|
3754
|
-
return result;
|
|
3755
|
-
}
|
|
3756
|
-
// 大端
|
|
3757
|
-
// const dataBE = new ArrayBuffer(16)
|
|
3758
|
-
// const dataViewBE = new DataView(dataBE)
|
|
3759
|
-
// set_big_int_128(dataViewBE, 0, -34355n, false)
|
|
3760
|
-
// console.log(dataViewBE.buffer)
|
|
3761
|
-
// const bigInt128BE = get_big_int_128(dataViewBE, 0, false)
|
|
3762
|
-
// const bigUint128BE = get_big_uint_128(dataViewBE, 0, false)
|
|
3763
|
-
// console.log(bigInt128BE.toString(), bigUint128BE.toString())
|
|
3764
|
-
// 小端
|
|
3765
|
-
// const dataLE = new ArrayBuffer(16)
|
|
3766
|
-
// const dataViewLE = new DataView(dataLE)
|
|
3767
|
-
// set_big_int_128(dataViewLE, 0, -34355n, true)
|
|
3768
|
-
// console.log(dataViewLE.buffer)
|
|
3769
|
-
// const bigInt128LE = get_big_int_128(dataViewLE, 0, true)
|
|
3770
|
-
// const bigUint128LE = get_big_uint_128(dataViewLE, 0, true)
|
|
3771
|
-
// console.log(bigInt128LE.toString(), bigUint128LE.toString())
|
|
3772
3347
|
//# sourceMappingURL=browser.js.map
|