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/index.js CHANGED
@@ -1,142 +1,7 @@
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 { concat, assert, inspect, typed_array_to_buffer, connect_websocket, Lock, genid, seq, zip_object, WebSocketOpen, WebSocketClosed, WebSocketClosing, decode, delay, check } from 'xshell';
7
2
  import { t } from "./i18n/index.js";
8
- export const nulls = {
9
- int8: -0x80, // -128
10
- int16: -0x80_00, // -32768
11
- int32: -0x80_00_00_00, // -21_4748_3648
12
- int64: -0x8000000000000000n, // -922_3372_0368_5477_5808
13
- int128: -0x80000000000000000000000000000000n, // -170_1411_8346_0469_2317_3168_7303_7158_8410_5728
14
- float32: -3.4028234663852886e+38,
15
- /** -Number.MAX_VALUE */
16
- double: -Number.MAX_VALUE,
17
- bytes16: Uint8Array.from(new Array(16).fill(0))
18
- };
19
- export var DdbForm;
20
- (function (DdbForm) {
21
- DdbForm[DdbForm["scalar"] = 0] = "scalar";
22
- DdbForm[DdbForm["vector"] = 1] = "vector";
23
- DdbForm[DdbForm["pair"] = 2] = "pair";
24
- DdbForm[DdbForm["matrix"] = 3] = "matrix";
25
- DdbForm[DdbForm["set"] = 4] = "set";
26
- DdbForm[DdbForm["dict"] = 5] = "dict";
27
- DdbForm[DdbForm["table"] = 6] = "table";
28
- DdbForm[DdbForm["chart"] = 7] = "chart";
29
- /** 节点内部通信可能会使用,调用函数执行脚本一般不会返回这种类型 */
30
- DdbForm[DdbForm["chunk"] = 8] = "chunk";
31
- /** sysobj */
32
- DdbForm[DdbForm["object"] = 9] = "object";
33
- DdbForm[DdbForm["tensor"] = 10] = "tensor";
34
- })(DdbForm || (DdbForm = {}));
35
- /** DolphinDB DataType
36
- 对应的 array vector 类型为 64 + 基本类型
37
- 对应的 extended 类型为 128 + 基本类型 */
38
- export var DdbType;
39
- (function (DdbType) {
40
- DdbType[DdbType["void"] = 0] = "void";
41
- DdbType[DdbType["bool"] = 1] = "bool";
42
- DdbType[DdbType["char"] = 2] = "char";
43
- DdbType[DdbType["short"] = 3] = "short";
44
- DdbType[DdbType["int"] = 4] = "int";
45
- DdbType[DdbType["long"] = 5] = "long";
46
- DdbType[DdbType["date"] = 6] = "date";
47
- DdbType[DdbType["month"] = 7] = "month";
48
- DdbType[DdbType["time"] = 8] = "time";
49
- DdbType[DdbType["minute"] = 9] = "minute";
50
- DdbType[DdbType["second"] = 10] = "second";
51
- DdbType[DdbType["datetime"] = 11] = "datetime";
52
- DdbType[DdbType["timestamp"] = 12] = "timestamp";
53
- DdbType[DdbType["nanotime"] = 13] = "nanotime";
54
- DdbType[DdbType["nanotimestamp"] = 14] = "nanotimestamp";
55
- DdbType[DdbType["float"] = 15] = "float";
56
- DdbType[DdbType["double"] = 16] = "double";
57
- DdbType[DdbType["symbol"] = 17] = "symbol";
58
- DdbType[DdbType["string"] = 18] = "string";
59
- DdbType[DdbType["uuid"] = 19] = "uuid";
60
- DdbType[DdbType["functiondef"] = 20] = "functiondef";
61
- DdbType[DdbType["handle"] = 21] = "handle";
62
- DdbType[DdbType["code"] = 22] = "code";
63
- DdbType[DdbType["datasource"] = 23] = "datasource";
64
- DdbType[DdbType["resource"] = 24] = "resource";
65
- DdbType[DdbType["any"] = 25] = "any";
66
- DdbType[DdbType["compressed"] = 26] = "compressed";
67
- DdbType[DdbType["dict"] = 27] = "dict";
68
- DdbType[DdbType["datehour"] = 28] = "datehour";
69
- DdbType[DdbType["ipaddr"] = 30] = "ipaddr";
70
- DdbType[DdbType["int128"] = 31] = "int128";
71
- DdbType[DdbType["blob"] = 32] = "blob";
72
- DdbType[DdbType["complex"] = 34] = "complex";
73
- DdbType[DdbType["point"] = 35] = "point";
74
- DdbType[DdbType["duration"] = 36] = "duration";
75
- DdbType[DdbType["decimal32"] = 37] = "decimal32";
76
- DdbType[DdbType["decimal64"] = 38] = "decimal64";
77
- DdbType[DdbType["decimal128"] = 39] = "decimal128";
78
- DdbType[DdbType["object"] = 40] = "object";
79
- DdbType[DdbType["iotany"] = 41] = "iotany";
80
- DdbType[DdbType["symbol_extended"] = 145] = "symbol_extended";
81
- })(DdbType || (DdbType = {}));
82
- export var DdbFunctionType;
83
- (function (DdbFunctionType) {
84
- DdbFunctionType[DdbFunctionType["SystemFunc"] = 0] = "SystemFunc";
85
- DdbFunctionType[DdbFunctionType["SystemProc"] = 1] = "SystemProc";
86
- DdbFunctionType[DdbFunctionType["OperatorFunc"] = 2] = "OperatorFunc";
87
- DdbFunctionType[DdbFunctionType["UserDefinedFunc"] = 3] = "UserDefinedFunc";
88
- DdbFunctionType[DdbFunctionType["PartialFunc"] = 4] = "PartialFunc";
89
- DdbFunctionType[DdbFunctionType["DynamicFunc"] = 5] = "DynamicFunc";
90
- DdbFunctionType[DdbFunctionType["PiecewiseFunc"] = 6] = "PiecewiseFunc";
91
- DdbFunctionType[DdbFunctionType["JitFunc"] = 7] = "JitFunc";
92
- DdbFunctionType[DdbFunctionType["JitPartialFunc"] = 8] = "JitPartialFunc";
93
- })(DdbFunctionType || (DdbFunctionType = {}));
94
- export var DdbDurationUnit;
95
- (function (DdbDurationUnit) {
96
- DdbDurationUnit[DdbDurationUnit["ns"] = 0] = "ns";
97
- DdbDurationUnit[DdbDurationUnit["us"] = 1] = "us";
98
- DdbDurationUnit[DdbDurationUnit["ms"] = 2] = "ms";
99
- DdbDurationUnit[DdbDurationUnit["s"] = 3] = "s";
100
- DdbDurationUnit[DdbDurationUnit["m"] = 4] = "m";
101
- DdbDurationUnit[DdbDurationUnit["H"] = 5] = "H";
102
- DdbDurationUnit[DdbDurationUnit["d"] = 6] = "d";
103
- DdbDurationUnit[DdbDurationUnit["w"] = 7] = "w";
104
- DdbDurationUnit[DdbDurationUnit["M"] = 8] = "M";
105
- DdbDurationUnit[DdbDurationUnit["y"] = 9] = "y";
106
- DdbDurationUnit[DdbDurationUnit["B"] = 10] = "B";
107
- })(DdbDurationUnit || (DdbDurationUnit = {}));
108
- export var DdbChartType;
109
- (function (DdbChartType) {
110
- DdbChartType[DdbChartType["area"] = 0] = "area";
111
- DdbChartType[DdbChartType["bar"] = 1] = "bar";
112
- DdbChartType[DdbChartType["column"] = 2] = "column";
113
- DdbChartType[DdbChartType["histogram"] = 3] = "histogram";
114
- DdbChartType[DdbChartType["line"] = 4] = "line";
115
- DdbChartType[DdbChartType["pie"] = 5] = "pie";
116
- DdbChartType[DdbChartType["scatter"] = 6] = "scatter";
117
- DdbChartType[DdbChartType["trend"] = 7] = "trend";
118
- DdbChartType[DdbChartType["kline"] = 8] = "kline";
119
- })(DdbChartType || (DdbChartType = {}));
120
- // server 实现中区分了 0: NULL (nothing), 1: NULL (null), 2: DFLT (default)
121
- // Void::serialize()
122
- // (isNothing() ? 0 : 1) + (isDefault_ ? 2 : 0);
123
- export var DdbVoidType;
124
- (function (DdbVoidType) {
125
- DdbVoidType[DdbVoidType["undefined"] = 0] = "undefined";
126
- DdbVoidType[DdbVoidType["null"] = 1] = "null";
127
- DdbVoidType[DdbVoidType["default"] = 2] = "default";
128
- })(DdbVoidType || (DdbVoidType = {}));
129
- export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long]);
130
- /** 工具,取得某个 DdbType 的字节数 */
131
- export const ddb_tensor_bytes = {
132
- [DdbType.bool]: 1,
133
- [DdbType.char]: 1,
134
- [DdbType.short]: 2,
135
- [DdbType.int]: 4,
136
- [DdbType.long]: 8,
137
- [DdbType.float]: 4,
138
- [DdbType.double]: 8,
139
- };
3
+ 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";
4
+ export * from "./common.js";
140
5
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
141
6
  export class DdbObj {
142
7
  static dec = new TextDecoder('utf-8');
@@ -1662,33 +1527,12 @@ export class DdbObj {
1662
1527
  return obj;
1663
1528
  }
1664
1529
  }
1665
- /** 根据 DdbType 获取其名称,array vector type 自动在后面加上 [] */
1666
- export function get_type_name(type) {
1667
- return `${DdbType[type] || type}${64 <= type && type < 128 ? '[]' : ''}`;
1668
- }
1669
- export function is_decimal_type(type) {
1670
- return type === DdbType.decimal32 || type === DdbType.decimal64 || type === DdbType.decimal128;
1671
- }
1672
- export function is_decimal_null_value(type, value) {
1673
- return ((value === nulls.int128 && type === DdbType.decimal128) ||
1674
- (value === nulls.int64 && type === DdbType.decimal64) ||
1675
- (value === nulls.int32 && type === DdbType.decimal32));
1676
- }
1677
- export function get_duration_unit(code) {
1678
- let str = String.fromCharCode((code >> 24) & 0xff);
1679
- str += String.fromCharCode((code >> 16) & 0xff);
1680
- str += String.fromCharCode((code >> 8) & 0xff);
1681
- str += String.fromCharCode(code & 0xff);
1682
- return str;
1683
- }
1684
1530
  /** 整数一定用这个 number formatter, InspectOptions.decimals 不传也用这个 */
1685
1531
  let default_formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1686
1532
  let _decimals = 20;
1687
1533
  let _grouping = true;
1688
1534
  /** 缓存,为了优化性能,通常 options.decimals 都是不变的 */
1689
1535
  let _formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1690
- /** 用来处理时差 To deal with jet lag */
1691
- let _datetime_formatter = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'short', timeStyle: 'medium', timeZone: 'UTC', hour12: false });
1692
1536
  /** 根据 DdbType 格式化单个元素 (value) 为字符串,空值返回 'null' 字符串 */
1693
1537
  export function format(type, value, le, options = {}) {
1694
1538
  const { grouping = true, timestamp = 'ms' } = options;
@@ -2232,33 +2076,6 @@ export class DdbDate extends DdbObj {
2232
2076
  });
2233
2077
  }
2234
2078
  }
2235
- function get_ddb_time_value(classname, value) {
2236
- if (value === null)
2237
- return null;
2238
- if (classname === 'DdbNanoTimeStamp' && typeof value === 'string')
2239
- return str2nanotimestamp(value);
2240
- let date;
2241
- if (value === undefined)
2242
- date = new Date();
2243
- else if (typeof value === 'number' || typeof value === 'string')
2244
- date = new Date(value);
2245
- else if (value instanceof Date)
2246
- date = value;
2247
- else if (dayjs.isDayjs(value))
2248
- date = new Date(value.valueOf());
2249
- else
2250
- throw new Error(t('value 不能转换为 {{classname}}', { classname }));
2251
- switch (classname) {
2252
- case 'DdbDateTime':
2253
- return (date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / 1000;
2254
- case 'DdbTimeStamp':
2255
- return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset());
2256
- case 'DdbNanoTimeStamp':
2257
- return BigInt(date.getTime() - 1000 * 60 * date.getTimezoneOffset()) * 1000000n;
2258
- case 'DdbDate':
2259
- return Math.floor((date.getTime() - 1000 * 60 * date.getTimezoneOffset()) / (1000 * 3600 * 24));
2260
- }
2261
- }
2262
2079
  export class DdbBlob extends DdbObj {
2263
2080
  constructor(value) {
2264
2081
  assert(value, t('new DdbBlob 不能传空的 value'));
@@ -2514,230 +2331,6 @@ export class DdbTable extends DdbObj {
2514
2331
  });
2515
2332
  }
2516
2333
  }
2517
- export function date2ms(date) {
2518
- // 将 server 的本地时间 (以 ms 为单位,1970.01.01 00:00:00 作为零点) 作为 UTC-0 格式化为字符串,然后根据本地的时区解析这个字符串转换为 UTC-8
2519
- // 本地的时区与实际的时间值相关,getTimezoneOffset() 可能会受到夏令时 (DST) 的影响,不能使用
2520
- // 得到的 utc 毫秒数交给 js date 或者 dayjs 去格式化
2521
- if (date === null || date === nulls.int32)
2522
- return null;
2523
- const ms = 1000 * 3600 * 24 * date;
2524
- return timestamp2ms(ms);
2525
- }
2526
- export function date2str(date, format = 'YYYY.MM.DD') {
2527
- return (date === null || date === nulls.int32) ?
2528
- 'null'
2529
- :
2530
- dayjs(date2ms(date)).format(format);
2531
- }
2532
- export function month2ms(month) {
2533
- return (month === null || month === nulls.int32) ?
2534
- null
2535
- :
2536
- dayjs(month2str(month), 'YYYY.MM[M]').valueOf();
2537
- }
2538
- export function month2str(month) {
2539
- if (month === null || month === nulls.int32)
2540
- return 'null';
2541
- if (month < 0)
2542
- return String(month);
2543
- const _month = month % 12;
2544
- const year = Math.trunc(month / 12);
2545
- return `${String(year).padStart(4, '0')}.${String(_month + 1).padStart(2, '0')}M`;
2546
- }
2547
- export function time2ms(time) {
2548
- return (time === null || time === nulls.int32) ?
2549
- null
2550
- :
2551
- timestamp2ms(time);
2552
- }
2553
- export function time2str(time, format = 'HH:mm:ss.SSS') {
2554
- return (time === null || time === nulls.int32) ?
2555
- 'null'
2556
- :
2557
- dayjs(time2ms(time)).format(format);
2558
- }
2559
- export function minute2ms(minute) {
2560
- if (minute === null || minute === nulls.int32)
2561
- return null;
2562
- const ms = 60 * 1000 * minute;
2563
- return timestamp2ms(ms);
2564
- }
2565
- export function minute2str(minute, format = 'HH:mm[m]') {
2566
- return (minute === null || minute === nulls.int32) ?
2567
- 'null'
2568
- :
2569
- dayjs(minute2ms(minute)).format(format);
2570
- }
2571
- export function second2ms(second) {
2572
- if (second === null || second === nulls.int32)
2573
- return null;
2574
- const ms = 1000 * second;
2575
- return timestamp2ms(ms);
2576
- }
2577
- export function second2str(second, format = 'HH:mm:ss') {
2578
- return (second === null || second === nulls.int32) ?
2579
- 'null'
2580
- :
2581
- dayjs(second2ms(second)).format(format);
2582
- }
2583
- export function datetime2ms(datetime) {
2584
- if (datetime === null || datetime === nulls.int32)
2585
- return null;
2586
- const date = new Date(1000 * datetime);
2587
- return new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf();
2588
- }
2589
- export function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
2590
- return (datetime === null || datetime === nulls.int32) ?
2591
- 'null'
2592
- :
2593
- dayjs(datetime2ms(datetime)).format(format);
2594
- }
2595
- /** _datetime_formatter.format 会在 date 为 Invalid Date 时抛出错误 */
2596
- export function timestamp2ms(timestamp) {
2597
- if (timestamp === null || timestamp === nulls.int64)
2598
- return null;
2599
- const date = new Date(Number(timestamp));
2600
- return new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf();
2601
- }
2602
- /** format timestamp (bigint) to string
2603
- - timestamp: bigint value
2604
- - format?:
2605
- 格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS` format string, default to `YYYY.MM.DD HH:mm:ss.SSS`
2606
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
2607
- export function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
2608
- return (timestamp === null || timestamp === nulls.int64) ?
2609
- 'null'
2610
- :
2611
- dayjs(timestamp2ms(timestamp)).format(format);
2612
- }
2613
- export function datehour2ms(datehour) {
2614
- if (datehour === null || datehour === nulls.int32)
2615
- return null;
2616
- const ms = 1000 * 3600 * datehour;
2617
- return timestamp2ms(ms);
2618
- }
2619
- export function datehour2str(datehour, format = 'YYYY.MM.DDTHH') {
2620
- if (datehour === null || datehour === nulls.int32)
2621
- return 'null';
2622
- const ms = 1000 * 3600 * datehour;
2623
- return dayjs(timestamp2ms(ms)).format(format);
2624
- }
2625
- /** parse timestamp string to bigint value
2626
- - str: timestamp string, 如果为空字符串或 'null' 会返回对应的空值 (nulls.int64)
2627
- timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
2628
- - format?:
2629
- 对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSS`
2630
- The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
2631
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens */
2632
- export function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
2633
- if (!str || str === 'null')
2634
- return nulls.int64;
2635
- assert(str.length === format.length, t('timestamp 字符串长度必须等于格式串长度'));
2636
- const ms = dayjs(str, format).valueOf();
2637
- return BigInt(-(1000 * 60 * new Date(ms).getTimezoneOffset()) +
2638
- ms);
2639
- }
2640
- export function nanotime2ns(nanotime) {
2641
- return nanotimestamp2ns(nanotime);
2642
- }
2643
- export function nanotime2str(nanotime, format = 'HH:mm:ss.SSSSSSSSS') {
2644
- if (nanotime === null || nanotime === nulls.int64)
2645
- return 'null';
2646
- if (nanotime < 0n)
2647
- return String(nanotime);
2648
- const i_second_start = format.indexOf('ss');
2649
- assert(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
2650
- const i_second_end = i_second_start + 2;
2651
- const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
2652
- assert(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
2653
- const ms = Number(nanotime) / 1000000;
2654
- return (dayjs(timestamp2ms(ms)).format(format.slice(0, i_second_end)) +
2655
- format.slice(i_second_end, i_nanosecond_start) +
2656
- String(nanotime % 1000000000n).padStart(9, '0'));
2657
- }
2658
- export function nanotimestamp2ns(nanotimestamp) {
2659
- if (nanotimestamp === null || nanotimestamp === nulls.int64)
2660
- return null;
2661
- const date = new Date(Number(nanotimestamp / 1000000n));
2662
- return BigInt(new Date(`${_datetime_formatter.format(date)}.${String(date.getUTCMilliseconds()).padStart(3, '0')}`).valueOf()) * 1000000n + nanotimestamp % 1000000n;
2663
- }
2664
- /** format nanotimestamp value (bigint) to string
2665
- - nanotimestamp: bigint value
2666
- - format?:
2667
- 格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSSSSSSSS` format string, default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
2668
- 秒的格式为 ss (必须包含); 纳秒的格式为 SSSSSSSSS (必须包含) Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
2669
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
2670
- */
2671
- export function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
2672
- // tests:
2673
- // nanotimestamp2str(0n)
2674
- // nanotimestamp2str(-1n)
2675
- // nanotimestamp2str(-9_9999_9999n)
2676
- // nanotimestamp2str(-10_0000_0000n)
2677
- // nanotimestamp2str(-10_0000_0001n)
2678
- if (nanotimestamp === null || nanotimestamp === nulls.int64)
2679
- return 'null';
2680
- const i_second_start = format.indexOf('ss');
2681
- assert(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
2682
- const i_second_end = i_second_start + 2;
2683
- const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
2684
- assert(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
2685
- const remainder = nanotimestamp % 1000000000n;
2686
- const borrow = remainder < 0n;
2687
- const ms = Number((nanotimestamp - remainder + (borrow ? -1000000000n : 0n)) / 1000000n);
2688
- return (dayjs(_datetime_formatter.format(new Date(ms))).format(format.slice(0, i_second_end)) +
2689
- format.slice(i_second_end, i_nanosecond_start) +
2690
- String(borrow ?
2691
- (remainder + 1000000000n) % 1000000000n
2692
- :
2693
- remainder).padStart(9, '0'));
2694
- }
2695
- /** parse nano timestamp string to bigint value
2696
- - str: nano timestamp string, 如果为空字符串或 'null' 会返回对应的空值 (nulls.int64)
2697
- nano timestamp string, If it is an empty string or 'null', it will return the corresponding empty value (nulls.int64)
2698
- - format?:
2699
- 对应传入字符串的格式串,默认是 `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
2700
- 秒的格式为 ss (必须包含); 纳秒的格式为 SSSSSSSSS (必须包含)
2701
- https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
2702
- The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
2703
- Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included) */
2704
- export function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
2705
- if (!str || str === 'null')
2706
- return nulls.int64;
2707
- check(str.length === format.length, t('nanotimestamp 字符串长度必须等于格式串长度'));
2708
- const i_second_start = format.indexOf('ss');
2709
- check(i_second_start !== -1, t('格式串必须包含秒的格式 (ss)'));
2710
- const i_second_end = i_second_start + 2;
2711
- const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
2712
- check(i_nanosecond_start !== -1, t('格式串必须包含纳秒的格式 (SSSSSSSSS)'));
2713
- const ms = dayjs(str.slice(0, i_second_end), format.slice(0, i_second_end)).valueOf();
2714
- return (BigInt(-(1000 * 60 * new Date(ms).getTimezoneOffset()) +
2715
- ms) * 1000000n
2716
- +
2717
- BigInt(str.slice(i_nanosecond_start, i_nanosecond_start + 9)));
2718
- }
2719
- export function ipaddr2str(buffer, le = true, ipv6) {
2720
- let buf = buffer;
2721
- if (le)
2722
- buf = buffer.slice().reverse();
2723
- const i_non_zero = buf.findIndex(x => x !== 0);
2724
- if (ipv6 ||
2725
- i_non_zero !== -1 && i_non_zero < 12) // ipv6
2726
- return buf2ipaddr([...buf]).toString();
2727
- else // ipv4
2728
- return buf.subarray(12).join('.');
2729
- }
2730
- export function uuid2str(buffer, le = true) {
2731
- const str = int1282str(buffer, le);
2732
- return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`;
2733
- }
2734
- export function int1282str(buffer, le = true) {
2735
- let buf = buffer;
2736
- if (le)
2737
- buf = buffer.slice().reverse();
2738
- return [...buf].map(x => x.toString(16)
2739
- .padStart(2, '0')).join('');
2740
- }
2741
2334
  export const winsize = 10000;
2742
2335
  export class DdbConnectionError extends Error {
2743
2336
  name = 'DdbConnectionError';
@@ -2826,6 +2419,10 @@ export class DDB {
2826
2419
  pinvoke;
2827
2420
  /** 首次定义 jsrpc 的 promise,保证并发调用 rpc 时只定义一次 jsrpc */
2828
2421
  pjsrpc;
2422
+ /** 函数定义缓存 */
2423
+ definitions = new Map();
2424
+ /** 函数定义锁,保证并发调用时不重复定义,直接返回第一次定义的结果 */
2425
+ pdefinitions = new Map();
2829
2426
  get connected() {
2830
2427
  return !this.error && this.lwebsocket.resource?.readyState === WebSocketOpen;
2831
2428
  }
@@ -2970,7 +2567,7 @@ export class DDB {
2970
2567
  }
2971
2568
  });
2972
2569
  }
2973
- 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 = 8, root_id = '', limit, } = {}) {
2570
+ 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, } = {}) {
2974
2571
  let flag = 0;
2975
2572
  if (urgent)
2976
2573
  flag += 1;
@@ -3083,7 +2680,7 @@ export class DDB {
3083
2680
  throw error;
3084
2681
  }
3085
2682
  // this 上的当前配置需要在 message 到达后使用,先保存起来
3086
- const _listeners = [...this.listeners].reverse();
2683
+ const _listeners = [...this.listeners];
3087
2684
  // rpc 请求期间需要独占 websocket,所以设计了一个锁,申请之后才能使用
3088
2685
  // ddb 世界观:需要等待上一个 rpc 结果从 server 返回之后才能发起下一个调用
3089
2686
  // 违反世界观可能造成:
@@ -3156,8 +2753,8 @@ export class DDB {
3156
2753
  console.log(typed_array_to_buffer(buffer));
3157
2754
  const message = this.parse_message(buffer, error);
3158
2755
  listener?.(message, this);
3159
- for (const listener of _listeners)
3160
- listener(message, this);
2756
+ for (let i = _listeners.length - 1; i >= 0; --i)
2757
+ _listeners[i](message, this);
3161
2758
  const { type, data } = message;
3162
2759
  switch (type) {
3163
2760
  case 'print':
@@ -3544,6 +3141,35 @@ export class DDB {
3544
3141
  }
3545
3142
  })).data());
3546
3143
  }
3144
+ /** 定义函数并保存,避免下次重复执行定义,会自动提取脚本中的函数名,作为缓存 key,
3145
+ 也同时作为返回值
3146
+ - definition: 函数完整定义
3147
+ @example
3148
+ await ddb.invoke(
3149
+ await ddb.define(
3150
+ 'def foo (bar) {\n' +
3151
+ ' print(bar)\n' +
3152
+ '}\n'),
3153
+ ['hello']) */
3154
+ async define(definition) {
3155
+ const matches = /\bdef (\w+)\s?\(/.exec(definition);
3156
+ if (!matches)
3157
+ throw new Error(t('DDB.define 方法传入的 definition 不符合函数定义格式'));
3158
+ const func_name = matches[1];
3159
+ // 已定义成功
3160
+ if (this.definitions.has(func_name))
3161
+ return func_name;
3162
+ // 防止并发调用 define 时多次重复定义
3163
+ let promise = this.pdefinitions.get(func_name);
3164
+ if (!promise)
3165
+ this.pdefinitions.set(func_name, promise = this.execute(definition));
3166
+ await promise;
3167
+ this.definitions.set(func_name, definition);
3168
+ // 成功定义之后,definitions 中有了,就不需要通过 promise 来防止并发了,清理掉
3169
+ // 定义失败时 rejected promise 直接作为后续调用的结果返回
3170
+ this.pdefinitions.delete(func_name);
3171
+ return func_name;
3172
+ }
3547
3173
  }
3548
3174
  export class BigInt128Array {
3549
3175
  static of(...items) {
@@ -3700,55 +3326,4 @@ Object.defineProperty(BigInt128Array.prototype, Symbol.toStringTag, {
3700
3326
  enumerable: false,
3701
3327
  value: 'BigInt128Array',
3702
3328
  });
3703
- // DataView Extends for bigint 128 operations
3704
- function get_big_uint_128(dataview, byte_offset, le = true) {
3705
- let cursor = byte_offset + (le ? 15 : 0);
3706
- const end = byte_offset + (le ? -1 : 16);
3707
- const step = le ? -1 : 1;
3708
- let value = 0n;
3709
- while (cursor !== end) {
3710
- value = value << 8n | BigInt(dataview.getUint8(cursor));
3711
- cursor += step;
3712
- }
3713
- return value;
3714
- }
3715
- function get_big_int_128(dataview, byte_offset, le = true) {
3716
- return BigInt.asIntN(128, get_big_uint_128(dataview, byte_offset, le));
3717
- }
3718
- function set_big_uint_128(dataView, byte_offset, value, le = true) {
3719
- let cursor = byte_offset + (le ? 0 : 15);
3720
- const end = byte_offset + (le ? 16 : -1);
3721
- const step = le ? 1 : -1;
3722
- while (cursor !== end) {
3723
- dataView.setUint8(cursor, Number(value & 0xffn));
3724
- value = value >> 8n;
3725
- cursor += step;
3726
- }
3727
- }
3728
- function set_big_int_128(dataview, byte_offset, value, le = true) {
3729
- set_big_uint_128(dataview, byte_offset, value, le);
3730
- }
3731
- function generate_array_type(baseType, dimensions) {
3732
- let result = baseType;
3733
- dimensions.forEach(dimension => {
3734
- result += `[${dimension}]`;
3735
- });
3736
- return result;
3737
- }
3738
- // 大端
3739
- // const dataBE = new ArrayBuffer(16)
3740
- // const dataViewBE = new DataView(dataBE)
3741
- // set_big_int_128(dataViewBE, 0, -34355n, false)
3742
- // console.log(dataViewBE.buffer)
3743
- // const bigInt128BE = get_big_int_128(dataViewBE, 0, false)
3744
- // const bigUint128BE = get_big_uint_128(dataViewBE, 0, false)
3745
- // console.log(bigInt128BE.toString(), bigUint128BE.toString())
3746
- // 小端
3747
- // const dataLE = new ArrayBuffer(16)
3748
- // const dataViewLE = new DataView(dataLE)
3749
- // set_big_int_128(dataViewLE, 0, -34355n, true)
3750
- // console.log(dataViewLE.buffer)
3751
- // const bigInt128LE = get_big_int_128(dataViewLE, 0, true)
3752
- // const bigUint128LE = get_big_uint_128(dataViewLE, 0, true)
3753
- // console.log(bigInt128LE.toString(), bigUint128LE.toString())
3754
3329
  //# sourceMappingURL=index.js.map