dolphindb 0.0.14 → 2.0.501
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/README.md +36 -7
- package/README.zh.md +35 -6
- package/browser.d.ts +54 -20
- package/browser.js +230 -77
- package/browser.js.map +1 -1
- package/index.d.ts +57 -21
- package/index.js +233 -78
- package/index.js.map +1 -1
- package/package.json +1 -1
package/browser.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ddb = exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.
|
|
3
|
+
exports.ddb = exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.str2timestamp = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbTable = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorDouble = exports.DdbVectorString = exports.DdbVectorInt = exports.DdbFunction = exports.DdbPair = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
|
6
6
|
const customParseFormat_js_1 = tslib_1.__importDefault(require("dayjs/plugin/customParseFormat.js"));
|
|
@@ -72,10 +72,15 @@ var DdbFunctionType;
|
|
|
72
72
|
DdbFunctionType[DdbFunctionType["JitPartialFunc"] = 8] = "JitPartialFunc";
|
|
73
73
|
})(DdbFunctionType = exports.DdbFunctionType || (exports.DdbFunctionType = {}));
|
|
74
74
|
exports.nulls = {
|
|
75
|
+
bool: 0x80,
|
|
75
76
|
char: '\x80',
|
|
76
77
|
int16: -32768,
|
|
77
78
|
int32: -2147483648,
|
|
78
|
-
int64: -0x8000000000000000n,
|
|
79
|
+
int64: -0x8000000000000000n,
|
|
80
|
+
float32: -3.4028234663852886e+38,
|
|
81
|
+
/** -Number.MAX_VALUE */
|
|
82
|
+
double: -Number.MAX_VALUE,
|
|
83
|
+
bytes16: Uint8Array.from(new Array(16).fill(0))
|
|
79
84
|
};
|
|
80
85
|
exports.timezone_offset = 1000 * 60 * new Date().getTimezoneOffset();
|
|
81
86
|
/** 可以表示所有 DolphinDB 数据库中的数据类型 */
|
|
@@ -92,7 +97,7 @@ class DdbObj {
|
|
|
92
97
|
form: DdbForm.scalar,
|
|
93
98
|
type: DdbType.void,
|
|
94
99
|
length: 0,
|
|
95
|
-
value:
|
|
100
|
+
value: null
|
|
96
101
|
});
|
|
97
102
|
const type = buf[0];
|
|
98
103
|
const form = buf[1];
|
|
@@ -241,13 +246,18 @@ class DdbObj {
|
|
|
241
246
|
switch (type) {
|
|
242
247
|
case DdbType.void:
|
|
243
248
|
return [1, null];
|
|
244
|
-
case DdbType.bool:
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
249
|
+
case DdbType.bool: {
|
|
250
|
+
const value = buf[0];
|
|
251
|
+
return [1, value === exports.nulls.bool ? null : Boolean(value)];
|
|
252
|
+
}
|
|
253
|
+
case DdbType.char: {
|
|
254
|
+
const value = String.fromCharCode(buf[0]);
|
|
255
|
+
return [1, value === exports.nulls.char ? null : value];
|
|
256
|
+
}
|
|
248
257
|
case DdbType.short: {
|
|
249
258
|
const dv = new DataView(buf.buffer, buf.byteOffset);
|
|
250
|
-
|
|
259
|
+
const value = dv.getInt16(0, le);
|
|
260
|
+
return [2, value === exports.nulls.int16 ? null : value];
|
|
251
261
|
}
|
|
252
262
|
case DdbType.int:
|
|
253
263
|
// datetime
|
|
@@ -259,15 +269,18 @@ class DdbObj {
|
|
|
259
269
|
case DdbType.datetime:
|
|
260
270
|
case DdbType.datehour: {
|
|
261
271
|
const dv = new DataView(buf.buffer, buf.byteOffset);
|
|
262
|
-
|
|
272
|
+
const value = dv.getInt32(0, le);
|
|
273
|
+
return [4, value === exports.nulls.int32 ? null : value];
|
|
263
274
|
}
|
|
264
275
|
case DdbType.float: {
|
|
265
276
|
const dv = new DataView(buf.buffer, buf.byteOffset);
|
|
266
|
-
|
|
277
|
+
const value = dv.getFloat32(0, le);
|
|
278
|
+
return [4, value === exports.nulls.float32 ? null : value];
|
|
267
279
|
}
|
|
268
280
|
case DdbType.double: {
|
|
269
281
|
const dv = new DataView(buf.buffer, buf.byteOffset);
|
|
270
|
-
|
|
282
|
+
const value = dv.getFloat64(0, le);
|
|
283
|
+
return [8, value === exports.nulls.double ? null : value];
|
|
271
284
|
}
|
|
272
285
|
case DdbType.long:
|
|
273
286
|
// timestamp
|
|
@@ -275,7 +288,8 @@ class DdbObj {
|
|
|
275
288
|
case DdbType.nanotime:
|
|
276
289
|
case DdbType.nanotimestamp: {
|
|
277
290
|
const dv = new DataView(buf.buffer, buf.byteOffset);
|
|
278
|
-
|
|
291
|
+
const value = dv.getBigInt64(0, le);
|
|
292
|
+
return [8, value === exports.nulls.int64 ? null : value];
|
|
279
293
|
}
|
|
280
294
|
case DdbType.string:
|
|
281
295
|
case DdbType.symbol:
|
|
@@ -576,11 +590,26 @@ class DdbObj {
|
|
|
576
590
|
case DdbType.void:
|
|
577
591
|
return [Uint8Array.of(1)];
|
|
578
592
|
case DdbType.bool:
|
|
579
|
-
return [
|
|
593
|
+
return [
|
|
594
|
+
Uint8Array.of(value === null ?
|
|
595
|
+
exports.nulls.bool
|
|
596
|
+
:
|
|
597
|
+
Number(value))
|
|
598
|
+
];
|
|
580
599
|
case DdbType.char:
|
|
581
|
-
return [
|
|
600
|
+
return [
|
|
601
|
+
Uint8Array.of((value === null ?
|
|
602
|
+
exports.nulls.char
|
|
603
|
+
:
|
|
604
|
+
value).charCodeAt(0))
|
|
605
|
+
];
|
|
582
606
|
case DdbType.short:
|
|
583
|
-
return [
|
|
607
|
+
return [
|
|
608
|
+
Int16Array.of(value === null ?
|
|
609
|
+
exports.nulls.int16
|
|
610
|
+
:
|
|
611
|
+
value)
|
|
612
|
+
];
|
|
584
613
|
case DdbType.int:
|
|
585
614
|
// datetime
|
|
586
615
|
case DdbType.date:
|
|
@@ -590,17 +619,37 @@ class DdbObj {
|
|
|
590
619
|
case DdbType.second:
|
|
591
620
|
case DdbType.datetime:
|
|
592
621
|
case DdbType.datehour:
|
|
593
|
-
return [
|
|
622
|
+
return [
|
|
623
|
+
Int32Array.of(value === null ?
|
|
624
|
+
exports.nulls.int32
|
|
625
|
+
:
|
|
626
|
+
value)
|
|
627
|
+
];
|
|
594
628
|
case DdbType.float:
|
|
595
|
-
return [
|
|
629
|
+
return [
|
|
630
|
+
Float32Array.of(value === null ?
|
|
631
|
+
exports.nulls.float32
|
|
632
|
+
:
|
|
633
|
+
value)
|
|
634
|
+
];
|
|
596
635
|
case DdbType.double:
|
|
597
|
-
return [
|
|
636
|
+
return [
|
|
637
|
+
Float64Array.of(value === null ?
|
|
638
|
+
exports.nulls.double
|
|
639
|
+
:
|
|
640
|
+
value)
|
|
641
|
+
];
|
|
598
642
|
case DdbType.long:
|
|
599
643
|
// timestamp
|
|
600
644
|
case DdbType.timestamp:
|
|
601
645
|
case DdbType.nanotime:
|
|
602
646
|
case DdbType.nanotimestamp:
|
|
603
|
-
return [
|
|
647
|
+
return [
|
|
648
|
+
BigInt64Array.of(value === null ?
|
|
649
|
+
exports.nulls.int64
|
|
650
|
+
:
|
|
651
|
+
value)
|
|
652
|
+
];
|
|
604
653
|
case DdbType.string:
|
|
605
654
|
case DdbType.symbol:
|
|
606
655
|
case DdbType.code:
|
|
@@ -660,15 +709,12 @@ class DdbObj {
|
|
|
660
709
|
case DdbForm.table:
|
|
661
710
|
return [
|
|
662
711
|
Uint32Array.of(this.rows, this.cols),
|
|
663
|
-
DdbObj.enc.encode(this.name),
|
|
712
|
+
DdbObj.enc.encode(this.name || ''),
|
|
713
|
+
// column names
|
|
664
714
|
Uint8Array.of(0),
|
|
665
|
-
...DdbObj.pack_vector_body(this.value.map(col => col.name), DdbType.string, this.cols),
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
for (let i = 0; i < cols.length; i++)
|
|
669
|
-
cols[i] = this.value[i].pack();
|
|
670
|
-
return cols;
|
|
671
|
-
})()
|
|
715
|
+
...DdbObj.pack_vector_body(this.value.map((col, i) => col.name || `col${i}`), DdbType.string, this.cols),
|
|
716
|
+
// column vectors
|
|
717
|
+
...this.value.map(col => col.pack())
|
|
672
718
|
];
|
|
673
719
|
case DdbForm.dict:
|
|
674
720
|
return [
|
|
@@ -764,7 +810,7 @@ class DdbObj {
|
|
|
764
810
|
case DdbType.symbol_extended: {
|
|
765
811
|
const { base_id, base, data } = value;
|
|
766
812
|
return [
|
|
767
|
-
Uint32Array.of(base_id, base.length),
|
|
813
|
+
Uint32Array.of(base_id || 0, base.length),
|
|
768
814
|
...this.pack_vector_body(base, DdbType.string, base.length),
|
|
769
815
|
data
|
|
770
816
|
];
|
|
@@ -879,6 +925,7 @@ class DdbVoid extends DdbObj {
|
|
|
879
925
|
form: DdbForm.scalar,
|
|
880
926
|
type: DdbType.void,
|
|
881
927
|
length: 0,
|
|
928
|
+
value: null,
|
|
882
929
|
});
|
|
883
930
|
}
|
|
884
931
|
}
|
|
@@ -911,7 +958,7 @@ class DdbInt extends DdbObj {
|
|
|
911
958
|
form: DdbForm.scalar,
|
|
912
959
|
type: DdbType.int,
|
|
913
960
|
length: 4,
|
|
914
|
-
value
|
|
961
|
+
value,
|
|
915
962
|
});
|
|
916
963
|
}
|
|
917
964
|
}
|
|
@@ -982,86 +1029,143 @@ class DdbNanoTimeStamp extends DdbObj {
|
|
|
982
1029
|
}
|
|
983
1030
|
}
|
|
984
1031
|
exports.DdbNanoTimeStamp = DdbNanoTimeStamp;
|
|
1032
|
+
class DdbPair extends DdbObj {
|
|
1033
|
+
constructor(l, r = null) {
|
|
1034
|
+
super({
|
|
1035
|
+
form: DdbForm.pair,
|
|
1036
|
+
type: DdbType.int,
|
|
1037
|
+
length: 0,
|
|
1038
|
+
rows: 2,
|
|
1039
|
+
cols: 1,
|
|
1040
|
+
value: Int32Array.of(l === null ? exports.nulls.int32 : l, r === null ? exports.nulls.int32 : r)
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
exports.DdbPair = DdbPair;
|
|
1045
|
+
class DdbFunction extends DdbObj {
|
|
1046
|
+
constructor(name, type) {
|
|
1047
|
+
super({
|
|
1048
|
+
form: DdbForm.scalar,
|
|
1049
|
+
type: DdbType.functiondef,
|
|
1050
|
+
length: 0,
|
|
1051
|
+
value: { type, name }
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
1055
|
+
exports.DdbFunction = DdbFunction;
|
|
985
1056
|
class DdbVectorInt extends DdbObj {
|
|
986
|
-
constructor(
|
|
1057
|
+
constructor(ints, name) {
|
|
987
1058
|
super({
|
|
988
1059
|
form: DdbForm.vector,
|
|
989
1060
|
type: DdbType.int,
|
|
990
1061
|
length: 0,
|
|
991
|
-
rows:
|
|
1062
|
+
rows: ints.length,
|
|
992
1063
|
cols: 1,
|
|
993
|
-
value: Int32Array
|
|
1064
|
+
value: ints instanceof Int32Array ?
|
|
1065
|
+
ints
|
|
1066
|
+
:
|
|
1067
|
+
Int32Array.from(ints, v => v === null ?
|
|
1068
|
+
exports.nulls.int32
|
|
1069
|
+
:
|
|
1070
|
+
v),
|
|
994
1071
|
name,
|
|
995
1072
|
});
|
|
996
1073
|
}
|
|
997
1074
|
}
|
|
998
1075
|
exports.DdbVectorInt = DdbVectorInt;
|
|
999
1076
|
class DdbVectorString extends DdbObj {
|
|
1000
|
-
constructor(
|
|
1077
|
+
constructor(strings, name) {
|
|
1001
1078
|
super({
|
|
1002
1079
|
form: DdbForm.vector,
|
|
1003
1080
|
type: DdbType.string,
|
|
1004
1081
|
length: 0,
|
|
1005
|
-
rows:
|
|
1082
|
+
rows: strings.length,
|
|
1006
1083
|
cols: 1,
|
|
1007
|
-
value,
|
|
1084
|
+
value: strings,
|
|
1008
1085
|
name,
|
|
1009
1086
|
});
|
|
1010
1087
|
}
|
|
1011
1088
|
}
|
|
1012
1089
|
exports.DdbVectorString = DdbVectorString;
|
|
1013
1090
|
class DdbVectorDouble extends DdbObj {
|
|
1014
|
-
constructor(
|
|
1091
|
+
constructor(doubles, name) {
|
|
1015
1092
|
super({
|
|
1016
1093
|
form: DdbForm.vector,
|
|
1017
1094
|
type: DdbType.double,
|
|
1018
1095
|
length: 0,
|
|
1019
|
-
rows:
|
|
1096
|
+
rows: doubles.length,
|
|
1020
1097
|
cols: 1,
|
|
1021
|
-
value: Float64Array
|
|
1098
|
+
value: doubles instanceof Float64Array ?
|
|
1099
|
+
doubles
|
|
1100
|
+
:
|
|
1101
|
+
Float64Array.from(doubles, v => v === null ?
|
|
1102
|
+
exports.nulls.double
|
|
1103
|
+
:
|
|
1104
|
+
v),
|
|
1022
1105
|
name,
|
|
1023
1106
|
});
|
|
1024
1107
|
}
|
|
1025
1108
|
}
|
|
1026
1109
|
exports.DdbVectorDouble = DdbVectorDouble;
|
|
1027
1110
|
class DdbVectorAny extends DdbObj {
|
|
1028
|
-
constructor(
|
|
1111
|
+
constructor(objs, name) {
|
|
1029
1112
|
super({
|
|
1030
1113
|
form: DdbForm.vector,
|
|
1031
1114
|
type: DdbType.any,
|
|
1032
1115
|
length: 0,
|
|
1033
|
-
rows:
|
|
1116
|
+
rows: objs.length,
|
|
1034
1117
|
cols: 1,
|
|
1035
|
-
value,
|
|
1118
|
+
value: objs,
|
|
1036
1119
|
name,
|
|
1037
1120
|
});
|
|
1038
1121
|
}
|
|
1039
1122
|
}
|
|
1040
1123
|
exports.DdbVectorAny = DdbVectorAny;
|
|
1041
|
-
class
|
|
1042
|
-
constructor(
|
|
1124
|
+
class DdbVectorSymbol extends DdbObj {
|
|
1125
|
+
constructor(strings, name) {
|
|
1126
|
+
let map = new Map([
|
|
1127
|
+
['', 0]
|
|
1128
|
+
]);
|
|
1129
|
+
let data = new Uint32Array(strings.length);
|
|
1130
|
+
for (let i = 0; i < strings.length; i++) {
|
|
1131
|
+
const x = strings[i];
|
|
1132
|
+
let index = map.get(x);
|
|
1133
|
+
if (index === undefined) {
|
|
1134
|
+
index = map.size;
|
|
1135
|
+
map.set(x, index);
|
|
1136
|
+
}
|
|
1137
|
+
data[i] = index;
|
|
1138
|
+
}
|
|
1043
1139
|
super({
|
|
1044
|
-
form: DdbForm.
|
|
1045
|
-
type: DdbType.
|
|
1140
|
+
form: DdbForm.vector,
|
|
1141
|
+
type: DdbType.symbol_extended,
|
|
1046
1142
|
length: 0,
|
|
1047
|
-
rows:
|
|
1143
|
+
rows: strings.length,
|
|
1048
1144
|
cols: 1,
|
|
1049
|
-
value:
|
|
1145
|
+
value: {
|
|
1146
|
+
base: [...map.keys()],
|
|
1147
|
+
base_id: null,
|
|
1148
|
+
data,
|
|
1149
|
+
},
|
|
1150
|
+
name
|
|
1050
1151
|
});
|
|
1051
1152
|
}
|
|
1052
1153
|
}
|
|
1053
|
-
exports.
|
|
1054
|
-
class
|
|
1055
|
-
constructor(
|
|
1154
|
+
exports.DdbVectorSymbol = DdbVectorSymbol;
|
|
1155
|
+
class DdbTable extends DdbObj {
|
|
1156
|
+
constructor(columns, name = '') {
|
|
1056
1157
|
super({
|
|
1057
|
-
form: DdbForm.
|
|
1058
|
-
type: DdbType.
|
|
1158
|
+
form: DdbForm.table,
|
|
1159
|
+
type: DdbType.void,
|
|
1059
1160
|
length: 0,
|
|
1060
|
-
|
|
1161
|
+
rows: columns[0].rows,
|
|
1162
|
+
cols: columns.length,
|
|
1163
|
+
name,
|
|
1164
|
+
value: columns,
|
|
1061
1165
|
});
|
|
1062
1166
|
}
|
|
1063
1167
|
}
|
|
1064
|
-
exports.
|
|
1168
|
+
exports.DdbTable = DdbTable;
|
|
1065
1169
|
function date2str(date, format = 'YYYY.MM.DD') {
|
|
1066
1170
|
return date === exports.nulls.int32 ?
|
|
1067
1171
|
''
|
|
@@ -1076,34 +1180,79 @@ function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
|
|
|
1076
1180
|
(0, dayjs_1.default)(exports.timezone_offset + 1000 * datetime).format(format);
|
|
1077
1181
|
}
|
|
1078
1182
|
exports.datetime2str = datetime2str;
|
|
1079
|
-
|
|
1183
|
+
/** format timestamp (bigint) to string
|
|
1184
|
+
- timestamp: bigint value
|
|
1185
|
+
- format?:
|
|
1186
|
+
format string, default to `YYYY.MM.DD HH:mm:ss.SSS`
|
|
1187
|
+
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
1188
|
+
*/
|
|
1189
|
+
function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
|
|
1080
1190
|
return timestamp === exports.nulls.int64 ?
|
|
1081
1191
|
''
|
|
1082
1192
|
:
|
|
1083
|
-
(0, dayjs_1.default)(exports.timezone_offset + Number(timestamp)).format(
|
|
1193
|
+
(0, dayjs_1.default)(exports.timezone_offset + Number(timestamp)).format(format);
|
|
1084
1194
|
}
|
|
1085
1195
|
exports.timestamp2str = timestamp2str;
|
|
1086
|
-
|
|
1087
|
-
return
|
|
1088
|
-
|
|
1089
|
-
:
|
|
1090
|
-
|
|
1196
|
+
/** parse timestamp string to bigint value
|
|
1197
|
+
- str: timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
|
|
1198
|
+
- format?:
|
|
1199
|
+
The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSS`
|
|
1200
|
+
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
1201
|
+
*/
|
|
1202
|
+
function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
|
|
1203
|
+
if (!str)
|
|
1204
|
+
return exports.nulls.int64;
|
|
1205
|
+
if (str.length !== format.length)
|
|
1206
|
+
throw new Error('The length of the timestamp string is not equal to the length of the format string');
|
|
1207
|
+
return BigInt(-exports.timezone_offset +
|
|
1208
|
+
(0, dayjs_1.default)(str, format).valueOf());
|
|
1209
|
+
}
|
|
1210
|
+
exports.str2timestamp = str2timestamp;
|
|
1211
|
+
/** format nanotimestamp value (bigint) to string
|
|
1212
|
+
- nanotimestamp: bigint value
|
|
1213
|
+
- format?:
|
|
1214
|
+
format string, default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
|
|
1215
|
+
Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
|
|
1216
|
+
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
1217
|
+
*/
|
|
1218
|
+
function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
|
|
1219
|
+
if (nanotimestamp === exports.nulls.int64)
|
|
1220
|
+
return '';
|
|
1221
|
+
const i_second_start = format.indexOf('ss');
|
|
1222
|
+
if (i_second_start === -1)
|
|
1223
|
+
throw new Error('The format string must contain the format for seconds (ss)');
|
|
1224
|
+
const i_second_end = i_second_start + 2;
|
|
1225
|
+
const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
|
|
1226
|
+
if (i_nanosecond_start === -1)
|
|
1227
|
+
throw new Error('The format string must contain the format for nanoseconds (SSSSSSSSS)');
|
|
1228
|
+
return ((0, dayjs_1.default)(exports.timezone_offset + (Number(nanotimestamp) / 1000000)).format(format.slice(0, i_second_end)) +
|
|
1229
|
+
format.slice(i_second_end, i_nanosecond_start) +
|
|
1230
|
+
String(nanotimestamp % 1000000000n));
|
|
1091
1231
|
}
|
|
1092
1232
|
exports.nanotimestamp2str = nanotimestamp2str;
|
|
1093
1233
|
/** parse nano timestamp string to bigint value
|
|
1094
|
-
- str: nano timestamp string,
|
|
1095
|
-
- format?:
|
|
1234
|
+
- str: nano timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
|
|
1235
|
+
- format?:
|
|
1236
|
+
The format string corresponding to the incoming string, the default is `YYYY.MM.DD HH:mm:ss.SSSSSSSSS`
|
|
1237
|
+
Seconds are in the format ss (must be included); nanoseconds are in the format SSSSSSSSS (must be included)
|
|
1096
1238
|
https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
|
|
1097
1239
|
*/
|
|
1098
|
-
function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss') {
|
|
1240
|
+
function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
|
|
1099
1241
|
if (!str)
|
|
1100
1242
|
return exports.nulls.int64;
|
|
1101
|
-
if (str.length
|
|
1102
|
-
throw new Error('nanotimestamp
|
|
1243
|
+
if (str.length !== format.length)
|
|
1244
|
+
throw new Error('nanotimestamp string length is not equal to format string length');
|
|
1245
|
+
const i_second_start = format.indexOf('ss');
|
|
1246
|
+
if (i_second_start === -1)
|
|
1247
|
+
throw new Error('The format string must contain the format for seconds (ss)');
|
|
1248
|
+
const i_second_end = i_second_start + 2;
|
|
1249
|
+
const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
|
|
1250
|
+
if (i_nanosecond_start === -1)
|
|
1251
|
+
throw new Error('Format string must contain nanosecond format (SSSSSSSSS)');
|
|
1103
1252
|
return (BigInt(-exports.timezone_offset +
|
|
1104
|
-
(0, dayjs_1.default)(str.slice(0, format.
|
|
1253
|
+
(0, dayjs_1.default)(str.slice(0, i_second_end), format.slice(0, i_second_end)).valueOf()) * 1000000n
|
|
1105
1254
|
+
|
|
1106
|
-
BigInt(str.slice(
|
|
1255
|
+
BigInt(str.slice(i_nanosecond_start, i_nanosecond_start + 9)));
|
|
1107
1256
|
}
|
|
1108
1257
|
exports.str2nanotimestamp = str2nanotimestamp;
|
|
1109
1258
|
class DDB {
|
|
@@ -1142,12 +1291,14 @@ class DDB {
|
|
|
1142
1291
|
/** resolver, rejector, promise of last rpc */
|
|
1143
1292
|
presolver(buf) { }
|
|
1144
1293
|
prejector(error) { }
|
|
1294
|
+
get connected() {
|
|
1295
|
+
return this.websocket?.readyState === WebSocket.OPEN;
|
|
1296
|
+
}
|
|
1145
1297
|
/** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL */
|
|
1146
1298
|
async connect({ url = this.url, login = true, username = 'admin', password = '123456', python = false, } = {}) {
|
|
1147
1299
|
this.url = url;
|
|
1148
1300
|
this.python = python;
|
|
1149
|
-
|
|
1150
|
-
this.disconnect();
|
|
1301
|
+
this.disconnect();
|
|
1151
1302
|
let websocket = new WebSocket(url, python ? ['python'] : []);
|
|
1152
1303
|
// https://stackoverflow.com/questions/11821096/what-is-the-difference-between-an-arraybuffer-and-a-blob/39951543
|
|
1153
1304
|
websocket.binaryType = 'arraybuffer';
|
|
@@ -1238,7 +1389,11 @@ class DDB {
|
|
|
1238
1389
|
return `/ ${options.join('_')}`;
|
|
1239
1390
|
}
|
|
1240
1391
|
disconnect() {
|
|
1241
|
-
this.
|
|
1392
|
+
if (this.connected)
|
|
1393
|
+
this.websocket.close(1000);
|
|
1394
|
+
this.presolver = buf => { };
|
|
1395
|
+
this.prejector = error => { };
|
|
1396
|
+
this.presult = Promise.resolve(null);
|
|
1242
1397
|
}
|
|
1243
1398
|
/** rpc through websocket (function/script/variable command)
|
|
1244
1399
|
- type: API 类型: 'script' | 'function' | 'variable'
|
|
@@ -1249,7 +1404,7 @@ class DDB {
|
|
|
1249
1404
|
async rpc(type, { script, func, args = [], vars = [], urgent, }) {
|
|
1250
1405
|
if (!this.websocket)
|
|
1251
1406
|
await this.connect();
|
|
1252
|
-
if (this.
|
|
1407
|
+
if (!this.connected)
|
|
1253
1408
|
throw new Error(`${this.url} is already disconnected`);
|
|
1254
1409
|
// 临界区:保证多个 rpc 并发时形成 promise 链
|
|
1255
1410
|
// ddb 世界观:需要等待上一个 rpc 结果从 server 返回之后才能发起下一个调用
|
|
@@ -1272,7 +1427,7 @@ class DDB {
|
|
|
1272
1427
|
// 临界区结束,只有一个 rpc 请求可以写 WebSocket
|
|
1273
1428
|
if (urgent && type !== 'script' && type !== 'function')
|
|
1274
1429
|
throw new Error('urgent 只适用于 script 和 funciton');
|
|
1275
|
-
this.to_ddbobjs(args);
|
|
1430
|
+
args = this.to_ddbobjs(args);
|
|
1276
1431
|
const command = this.enc.encode((() => {
|
|
1277
1432
|
switch (type) {
|
|
1278
1433
|
case 'function':
|
|
@@ -1414,11 +1569,9 @@ class DDB {
|
|
|
1414
1569
|
throw new Error(`不能自动转换 ${type} 至 DdbObj`);
|
|
1415
1570
|
}
|
|
1416
1571
|
}
|
|
1417
|
-
/** 转换 js 数组为 DdbObj[]
|
|
1572
|
+
/** 转换 js 数组为 DdbObj[] */
|
|
1418
1573
|
to_ddbobjs(values) {
|
|
1419
|
-
|
|
1420
|
-
values[i] = this.to_ddbobj(values[i]);
|
|
1421
|
-
return values;
|
|
1574
|
+
return values.map(value => this.to_ddbobj(value));
|
|
1422
1575
|
}
|
|
1423
1576
|
}
|
|
1424
1577
|
exports.DDB = DDB;
|