dolphindb 2.0.800 → 2.0.801

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.d.ts CHANGED
@@ -255,7 +255,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
255
255
  ];
256
256
  pack(): Uint8Array;
257
257
  static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
258
- [inspect.custom](depth: number, options: InspectOptions, _inspect: any): string;
258
+ [inspect.custom](depth: number, _options: InspectOptions, _inspect: any): string;
259
259
  inspect_type(): string;
260
260
  /** 自动转换 js string, boolean 为 DdbObj */
261
261
  static to_ddbobj(value: DdbObj | string | boolean): DdbObj;
@@ -439,6 +439,10 @@ export interface StreamingData extends StreamingParams {
439
439
  };
440
440
  }
441
441
  export declare const winsize: 40;
442
+ export declare class ConnectionError extends Error {
443
+ ddb: DDB;
444
+ constructor(ddb: DDB);
445
+ }
442
446
  export declare class DDB {
443
447
  /** 当前的 session id (http 或 tcp) */
444
448
  sid: string;
@@ -505,23 +509,10 @@ export declare class DDB {
505
509
  streaming?: StreamingParams;
506
510
  });
507
511
  private on_message;
508
- /** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL
509
- - options?:
510
- - url?: DolphinDB WebSocket URL. By default, the WebSocket URL passed in when the instance is initialized is used
511
- - autologin?: Whether to log in automatically after establishing a connection, default `true`
512
- - username?: DolphinDB username, default `'admin'`
513
- - password?: DolphinDB password, default `'123456'`
514
- - python?: set python session flag, default `false`
515
- - streaming?: When this option is set, the WebSocket connection is only used for streaming data
516
- */
517
- connect({ url, autologin, username, password, python, streaming }?: {
518
- url?: string;
519
- autologin?: boolean;
520
- username?: string;
521
- password?: string;
522
- python?: boolean;
523
- streaming?: StreamingParams;
524
- }): Promise<void>;
512
+ /** Establish a actual websocket connection to the DolphindB corresponding to the URL
513
+ It is a power, and after calling, it will ensure that it has been connected to the database (ensure that websocket.ReadyState is open), otherwise an error will be reported
514
+ this.autologin automatically log in when it is true */
515
+ connect(): Promise<void>;
525
516
  get_rpc_options({ urgent, secondary, async: _async, pickle, clear, api, compress, cancellable, priority, parallelism, root_id, limit, }?: {
526
517
  urgent?: boolean;
527
518
  /** API 提交的任务, secondary 必须为 false */
@@ -547,6 +538,7 @@ export declare class DDB {
547
538
  }): string;
548
539
  disconnect(): void;
549
540
  /** rpc through websocket (function/script/variable command)
541
+ When the DDB is not connected, the call will be automatically connected. When the connection is disconnected, the call will throw the Connectionerror
550
542
  - type: API 类型: 'script' | 'function' | 'variable'
551
543
  - options:
552
544
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
package/index.js CHANGED
@@ -1017,7 +1017,8 @@ export class DdbObj {
1017
1017
  throw new Error(`vector ${DdbType[type]} 暂不支持序列化`);
1018
1018
  }
1019
1019
  }
1020
- [inspect.custom](depth, options, _inspect) {
1020
+ [inspect.custom](depth, _options, _inspect) {
1021
+ const options = { nullstr: true, quote: true, ..._options };
1021
1022
  const type = this.inspect_type();
1022
1023
  const data = (() => {
1023
1024
  switch (this.form) {
@@ -1217,8 +1218,8 @@ let _decimals = 20;
1217
1218
  let _formatter = Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1218
1219
  /** Formats a single element (value) as a string according to DdbType, null returns a 'null' string */
1219
1220
  export function format(type, value, le, options = {}) {
1221
+ const { decimals } = options;
1220
1222
  const formatter = (() => {
1221
- const { decimals } = options;
1222
1223
  if (decimals === undefined || decimals === null)
1223
1224
  return default_formatter;
1224
1225
  if (decimals !== _decimals) {
@@ -1272,7 +1273,7 @@ export function format(type, value, le, options = {}) {
1272
1273
  inspect(null, options)
1273
1274
  :
1274
1275
  options.colors ?
1275
- date2str(value).green
1276
+ date2str(value).magenta
1276
1277
  :
1277
1278
  date2str(value);
1278
1279
  case DdbType.month:
@@ -1280,7 +1281,7 @@ export function format(type, value, le, options = {}) {
1280
1281
  inspect(null, options)
1281
1282
  :
1282
1283
  options.colors ?
1283
- month2str(value).green
1284
+ month2str(value).magenta
1284
1285
  :
1285
1286
  month2str(value);
1286
1287
  case DdbType.time:
@@ -1288,7 +1289,7 @@ export function format(type, value, le, options = {}) {
1288
1289
  inspect(null, options)
1289
1290
  :
1290
1291
  options.colors ?
1291
- time2str(value).green
1292
+ time2str(value).magenta
1292
1293
  :
1293
1294
  time2str(value);
1294
1295
  case DdbType.minute:
@@ -1296,7 +1297,7 @@ export function format(type, value, le, options = {}) {
1296
1297
  inspect(null, options)
1297
1298
  :
1298
1299
  options.colors ?
1299
- minute2str(value).green
1300
+ minute2str(value).magenta
1300
1301
  :
1301
1302
  minute2str(value);
1302
1303
  case DdbType.second:
@@ -1304,7 +1305,7 @@ export function format(type, value, le, options = {}) {
1304
1305
  inspect(null, options)
1305
1306
  :
1306
1307
  options.colors ?
1307
- second2str(value).green
1308
+ second2str(value).magenta
1308
1309
  :
1309
1310
  second2str(value);
1310
1311
  case DdbType.datetime:
@@ -1312,7 +1313,7 @@ export function format(type, value, le, options = {}) {
1312
1313
  inspect(null, options)
1313
1314
  :
1314
1315
  options.colors ?
1315
- datetime2str(value).green
1316
+ datetime2str(value).magenta
1316
1317
  :
1317
1318
  datetime2str(value);
1318
1319
  case DdbType.timestamp:
@@ -1320,7 +1321,7 @@ export function format(type, value, le, options = {}) {
1320
1321
  inspect(null, options)
1321
1322
  :
1322
1323
  options.colors ?
1323
- timestamp2str(value).green
1324
+ timestamp2str(value).magenta
1324
1325
  :
1325
1326
  timestamp2str(value);
1326
1327
  case DdbType.nanotime:
@@ -1328,7 +1329,7 @@ export function format(type, value, le, options = {}) {
1328
1329
  inspect(null, options)
1329
1330
  :
1330
1331
  options.colors ?
1331
- nanotime2str(value).green
1332
+ nanotime2str(value).magenta
1332
1333
  :
1333
1334
  nanotime2str(value);
1334
1335
  case DdbType.nanotimestamp:
@@ -1336,7 +1337,7 @@ export function format(type, value, le, options = {}) {
1336
1337
  inspect(null, options)
1337
1338
  :
1338
1339
  options.colors ?
1339
- nanotimestamp2str(value).green
1340
+ nanotimestamp2str(value).magenta
1340
1341
  :
1341
1342
  nanotimestamp2str(value);
1342
1343
  case DdbType.float:
@@ -1360,7 +1361,7 @@ export function format(type, value, le, options = {}) {
1360
1361
  return inspect(value, options);
1361
1362
  case DdbType.uuid:
1362
1363
  return options.colors ?
1363
- uuid2str(value, le).green
1364
+ uuid2str(value, le).cyan
1364
1365
  :
1365
1366
  uuid2str(value, le);
1366
1367
  case DdbType.functiondef:
@@ -1374,12 +1375,12 @@ export function format(type, value, le, options = {}) {
1374
1375
  'null'
1375
1376
  :
1376
1377
  options.colors ?
1377
- datehour2str(value).green
1378
+ datehour2str(value).magenta
1378
1379
  :
1379
1380
  datehour2str(value);
1380
1381
  case DdbType.ipaddr:
1381
1382
  return options.colors ?
1382
- ipaddr2str(value, le).green
1383
+ ipaddr2str(value, le).cyan
1383
1384
  :
1384
1385
  ipaddr2str(value, le);
1385
1386
  case DdbType.int128:
@@ -1403,7 +1404,7 @@ export function format(type, value, le, options = {}) {
1403
1404
  case DdbType.duration: {
1404
1405
  const { data, unit } = value;
1405
1406
  const str = `${data}${DdbDurationUnit[unit]}`;
1406
- return options.colors ? str.green : str;
1407
+ return options.colors ? str.magenta : str;
1407
1408
  }
1408
1409
  case DdbType.decimal32:
1409
1410
  case DdbType.decimal64: {
@@ -1455,12 +1456,9 @@ export function formati(obj, index, options = {}) {
1455
1456
  }
1456
1457
  }
1457
1458
  switch (obj.type) {
1458
- case DdbType.string:
1459
- case DdbType.symbol:
1460
- return obj.value[index];
1461
1459
  case DdbType.symbol_extended: {
1462
1460
  const { base, data } = obj.value;
1463
- return base[data[index]];
1461
+ return format(DdbType.string, base[data[index]], obj.le, options);
1464
1462
  }
1465
1463
  case DdbType.uuid:
1466
1464
  case DdbType.int128:
@@ -2004,6 +2002,13 @@ export function int1282str(buffer, le = true) {
2004
2002
  .padStart(2, '0')).join('');
2005
2003
  }
2006
2004
  export const winsize = 40;
2005
+ export class ConnectionError extends Error {
2006
+ ddb;
2007
+ constructor(ddb) {
2008
+ super(`websocket is disconnected: ${ddb.url}`);
2009
+ this.ddb = ddb;
2010
+ }
2011
+ }
2007
2012
  export class DDB {
2008
2013
  /** 当前的 session id (http 或 tcp) */
2009
2014
  sid = '0';
@@ -2079,52 +2084,50 @@ export class DDB {
2079
2084
  this.streaming = options.streaming;
2080
2085
  }
2081
2086
  on_message(event) {
2082
- console.log('This is the default on_message before calling this.rpc, it should not be called unless the server pushes the message first after the connection is established');
2083
- console.log(event.data);
2087
+ // 这里的实现一定会被 connect 中的实现覆盖
2084
2088
  }
2085
- /** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL
2086
- - options?:
2087
- - url?: DolphinDB WebSocket URL. By default, the WebSocket URL passed in when the instance is initialized is used
2088
- - autologin?: Whether to log in automatically after establishing a connection, default `true`
2089
- - username?: DolphinDB username, default `'admin'`
2090
- - password?: DolphinDB password, default `'123456'`
2091
- - python?: set python session flag, default `false`
2092
- - streaming?: When this option is set, the WebSocket connection is only used for streaming data
2093
- */
2094
- async connect({ url, autologin, username, password, python, streaming } = {}) {
2095
- if (url !== undefined)
2096
- this.url = url;
2097
- if (autologin !== undefined)
2098
- this.autologin = autologin;
2099
- if (username !== undefined)
2100
- this.username = username;
2101
- if (password !== undefined)
2102
- this.password = password;
2103
- if (python !== undefined)
2104
- this.python = python;
2105
- if (streaming !== undefined)
2106
- this.streaming = streaming;
2107
- this.disconnect();
2108
- await connect_websocket(this.url, {
2109
- protocols: (() => {
2110
- if (this.streaming)
2111
- return 'streaming';
2112
- if (this.python)
2113
- return 'python';
2114
- })(),
2115
- on_open: async (event, websocket) => {
2116
- this.websocket = websocket;
2089
+ /** Establish a actual websocket connection to the DolphindB corresponding to the URL
2090
+ It is a power, and after calling, it will ensure that it has been connected to the database (ensure that websocket.ReadyState is open), otherwise an error will be reported
2091
+ this.autologin automatically log in when it is true */
2092
+ async connect() {
2093
+ if (this.connected)
2094
+ return;
2095
+ const ptail = this.pconnect;
2096
+ let resolve;
2097
+ this.pconnect = new Promise((_resolve, _reject) => {
2098
+ resolve = _resolve;
2099
+ });
2100
+ await ptail;
2101
+ try {
2102
+ if (!this.connected) {
2103
+ this.on_message = (event) => {
2104
+ assert(false, '这是在调用 this.rpc 之前默认的 on_message, 不应该被调用到,除非建立连接后 server 先推送了 message');
2105
+ };
2106
+ this.presult = Promise.resolve(null);
2107
+ this.pnode_run_defined = false;
2108
+ this.websocket = await connect_websocket(this.url, {
2109
+ protocols: (() => {
2110
+ if (this.streaming)
2111
+ return 'streaming';
2112
+ if (this.python)
2113
+ return 'python';
2114
+ })(),
2115
+ on_message: (event) => {
2116
+ this.on_message(event);
2117
+ }
2118
+ });
2117
2119
  if (this.streaming)
2118
2120
  await this.subscribe();
2119
- else
2121
+ else {
2120
2122
  await this.rpc('connect', {});
2121
- },
2122
- on_message: (event) => {
2123
- this.on_message(event);
2123
+ if (this.autologin)
2124
+ await this.call('login', [this.username, this.password], { urgent: true });
2125
+ }
2124
2126
  }
2125
- });
2126
- if (!this.streaming && this.autologin)
2127
- await this.call('login', [this.username, this.password], { urgent: true });
2127
+ }
2128
+ finally {
2129
+ resolve();
2130
+ }
2128
2131
  }
2129
2132
  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, } = {}) {
2130
2133
  let flag = 0;
@@ -2160,12 +2163,9 @@ export class DDB {
2160
2163
  disconnect() {
2161
2164
  if (this.connected)
2162
2165
  this.websocket.close(1000);
2163
- this.on_message = () => { };
2164
- this.presult = Promise.resolve(null);
2165
- this.pconnect = Promise.resolve();
2166
- this.pnode_run_defined = false;
2167
2166
  }
2168
2167
  /** rpc through websocket (function/script/variable command)
2168
+ When the DDB is not connected, the call will be automatically connected. When the connection is disconnected, the call will throw the Connectionerror
2169
2169
  - type: API 类型: 'script' | 'function' | 'variable'
2170
2170
  - options:
2171
2171
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
@@ -2175,24 +2175,12 @@ export class DDB {
2175
2175
  为 false 时返回的 DdbObj 仅含有 buffer 和 le,不做解析,以便后续转发、序列化
2176
2176
  */
2177
2177
  async rpc(type, { script, func, args = [], vars = [], urgent, listener, parse_object, }) {
2178
- if (!this.websocket) {
2179
- const ptail = this.pconnect;
2180
- let resolve;
2181
- this.pconnect = new Promise((_resolve, _reject) => {
2182
- resolve = _resolve;
2183
- });
2184
- await ptail;
2185
- try {
2186
- if (!this.websocket)
2187
- await this.connect();
2188
- }
2189
- finally {
2190
- resolve();
2191
- }
2192
- }
2178
+ if (!this.websocket)
2179
+ await this.connect();
2193
2180
  if (!this.connected)
2194
- throw new Error(`${this.url} is already disconnected`);
2181
+ throw new ConnectionError(this);
2195
2182
  if (func === 'pnode_run' && !this.pnode_run_defined) {
2183
+ // 保证并发调用 rpc 时只定义一次 pnode_run
2196
2184
  const ptail = this.ppnoderun;
2197
2185
  let resolve;
2198
2186
  this.ppnoderun = new Promise((_resolve, _reject) => {
@@ -2522,7 +2510,7 @@ export class DDB {
2522
2510
  // 是 column 片段组成的 any vector
2523
2511
  const data = streaming.data = DdbObj.parse(buf.subarray(i_topic_end + 1), this.le);
2524
2512
  let { window: win } = streaming;
2525
- win.rows += data.rows;
2513
+ win.rows += data.value[0].rows;
2526
2514
  win.segments.push(data);
2527
2515
  if (win.rows >= winsize * 2 && win.segments.length >= 2) {
2528
2516
  let winsize_ = 0;