dolphindb 2.0.940 → 2.0.944

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
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  import type { InspectOptions as UtilInspectOptions } from 'util';
3
- import { inspect } from 'xshell';
3
+ import { inspect, type WebSocketConnectionError } from 'xshell';
4
4
  export declare enum DdbForm {
5
5
  scalar = 0,
6
6
  vector = 1,
@@ -183,8 +183,8 @@ DdbSymbolExtendedValue | DdbArrayVectorValue | DdbDecimal32VectorValue | DdbDeci
183
183
  export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue;
184
184
  export type DdbStringObj = DdbObj<string>;
185
185
  export type DdbVectorObj<TValue extends DdbVectorValue = DdbVectorValue> = DdbObj<TValue>;
186
- export type DdbVectorAnyObj = DdbObj<DdbObj[]>;
187
- export type DdbVectorStringObj = DdbObj<string[]>;
186
+ export type DdbVectorAnyObj = DdbVectorObj<DdbObj[]>;
187
+ export type DdbVectorStringObj = DdbVectorObj<string[]>;
188
188
  export type DdbTableObj<TColumns extends DdbVectorObj[] = DdbVectorObj[]> = DdbObj<TColumns>;
189
189
  export type DdbDictObj<TKeys extends DdbVectorObj = DdbVectorObj, TValues extends DdbVectorObj = DdbVectorObj> = DdbObj<[TKeys, TValues]>;
190
190
  export type DdbMatrixObj<TValue extends DdbMatrixValue = DdbMatrixValue> = DdbObj<TValue>;
@@ -462,8 +462,9 @@ export interface DdbRpcOptions {
462
462
  }
463
463
  export declare class DdbConnectionError extends Error {
464
464
  name: string;
465
+ cause?: WebSocketConnectionError;
465
466
  ddb: DDB;
466
- constructor(ddb: DDB, error_options?: ErrorOptions);
467
+ constructor(ddb: DDB, error?: WebSocketConnectionError);
467
468
  }
468
469
  export declare class DdbDatabaseError extends Error {
469
470
  name: string;
@@ -497,6 +498,8 @@ export declare class DDB {
497
498
  python: boolean;
498
499
  /** 是否为流数据连接,非流数据这个字段恒为 null Whether it is a streaming data connection, this field is always null for non-streaming data */
499
500
  streaming: StreamingData;
501
+ /** 是否打印每个 rpc 的信息用于调试 */
502
+ verbose: boolean;
500
503
  print_message_buffer: boolean;
501
504
  print_object_buffer: boolean;
502
505
  print_message: boolean;
@@ -518,7 +521,8 @@ export declare class DDB {
518
521
  - password?: DolphinDB 登录密码,默认 `'123456'` DolphinDB password, default `'123456'`
519
522
  - python?: 设置 python session flag,默认 `false` set python session flag, default `false`
520
523
  - streaming?: 设置该选项后,该 WebSocket 连接只用于流数据 When this option is set, the WebSocket connection is only used for streaming data
521
-
524
+ - verbose?: 是否打印每个 rpc 的信息用于调试
525
+
522
526
  @example
523
527
  let ddb = new DDB('ws://127.0.0.1:8848')
524
528
 
@@ -536,8 +540,10 @@ export declare class DDB {
536
540
  password?: string;
537
541
  python?: boolean;
538
542
  streaming?: StreamingParams;
543
+ verbose?: boolean;
539
544
  });
540
545
  private on_message;
546
+ private on_error;
541
547
  /** 建立实际的 WebSocket 连接到 URL 对应的 DolphinDB Establish a actual websocket connection to the DolphindB corresponding to the URL
542
548
  是幂等的,调用后会确保已连接到数据库 (确保 websocket.readyState 为 open),否则报错 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
543
549
  this.autologin 为 true 时自动登录 this.autologin automatically log in when it is true */
package/index.js CHANGED
@@ -4,7 +4,7 @@ import DayjsCustomParseFormat from 'dayjs/plugin/customParseFormat.js';
4
4
  dayjs.extend(DayjsCustomParseFormat);
5
5
  import ipaddrjs from 'ipaddr.js';
6
6
  const { fromByteArray: buf2ipaddr } = ipaddrjs;
7
- import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, WebSocket, WebSocketConnectionError, defer } from 'xshell';
7
+ import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, WebSocket, defer } from 'xshell';
8
8
  import { t } from './i18n/index.js';
9
9
  export var DdbForm;
10
10
  (function (DdbForm) {
@@ -2134,12 +2134,12 @@ export function int1282str(buffer, le = true) {
2134
2134
  export const winsize = 10_0000;
2135
2135
  export class DdbConnectionError extends Error {
2136
2136
  name = 'DdbConnectionError';
2137
+ cause;
2137
2138
  ddb;
2138
- constructor(ddb, error_options) {
2139
- super(error_options?.cause ?
2140
- error_options.cause.message
2141
- :
2142
- `${ddb.url} ${t('连接出错了,可能由于网络原因连接已被关闭,或服务器断开连接')}`, error_options);
2139
+ constructor(ddb, error) {
2140
+ super(error?.message || `${ddb.url} ${t('连接出错了,可能由于网络原因连接已被关闭,或服务器断开连接')}`, { cause: error });
2141
+ if (error)
2142
+ this.cause = error;
2143
2143
  this.ddb = ddb;
2144
2144
  }
2145
2145
  }
@@ -2185,6 +2185,8 @@ export class DDB {
2185
2185
  python = false;
2186
2186
  /** 是否为流数据连接,非流数据这个字段恒为 null Whether it is a streaming data connection, this field is always null for non-streaming data */
2187
2187
  streaming = null;
2188
+ /** 是否打印每个 rpc 的信息用于调试 */
2189
+ verbose = false;
2188
2190
  // --- 内部选项, 状态
2189
2191
  print_message_buffer = false;
2190
2192
  print_object_buffer = false;
@@ -2209,7 +2211,8 @@ export class DDB {
2209
2211
  - password?: DolphinDB 登录密码,默认 `'123456'` DolphinDB password, default `'123456'`
2210
2212
  - python?: 设置 python session flag,默认 `false` set python session flag, default `false`
2211
2213
  - streaming?: 设置该选项后,该 WebSocket 连接只用于流数据 When this option is set, the WebSocket connection is only used for streaming data
2212
-
2214
+ - verbose?: 是否打印每个 rpc 的信息用于调试
2215
+
2213
2216
  @example
2214
2217
  let ddb = new DDB('ws://127.0.0.1:8848')
2215
2218
 
@@ -2223,6 +2226,8 @@ export class DDB {
2223
2226
  */
2224
2227
  constructor(url, options = {}) {
2225
2228
  this.url = url;
2229
+ if (options.verbose !== undefined)
2230
+ this.verbose = options.verbose;
2226
2231
  if (options.autologin !== undefined)
2227
2232
  this.autologin = options.autologin;
2228
2233
  if (options.username !== undefined)
@@ -2234,8 +2239,11 @@ export class DDB {
2234
2239
  if (options.streaming !== undefined)
2235
2240
  this.streaming = options.streaming;
2236
2241
  }
2237
- on_message(buffer) {
2238
- // 这里的实现一定会被 connect 中的实现覆盖
2242
+ on_message(buffer, websocket) {
2243
+ // 这里的实现一定会被 connect, rpc 中的实现覆盖
2244
+ }
2245
+ on_error(error, websocket) {
2246
+ // 这里的实现一定会被 connect, rpc 中的实现覆盖
2239
2247
  }
2240
2248
  /** 建立实际的 WebSocket 连接到 URL 对应的 DolphinDB Establish a actual websocket connection to the DolphindB corresponding to the URL
2241
2249
  是幂等的,调用后会确保已连接到数据库 (确保 websocket.readyState 为 open),否则报错 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
@@ -2248,7 +2256,10 @@ export class DDB {
2248
2256
  await ptail;
2249
2257
  try {
2250
2258
  if (!this.connected) {
2251
- this.on_message = buffer => {
2259
+ this.on_error = (error, websocket) => {
2260
+ pconnect.reject(new DdbConnectionError(this, error));
2261
+ };
2262
+ this.on_message = (buffer, websocket) => {
2252
2263
  assert(false, t('这是在调用 this.rpc 之前默认的 on_message, 不应该被调用到,除非建立连接后 server 先推送了 message'));
2253
2264
  };
2254
2265
  this.presult = defer(null);
@@ -2261,16 +2272,16 @@ export class DDB {
2261
2272
  if (this.python)
2262
2273
  return 'python';
2263
2274
  })(),
2264
- on_message: (buffer) => {
2265
- this.on_message(buffer);
2275
+ on_message: (buffer, websocket) => {
2276
+ this.on_message(buffer, websocket);
2277
+ },
2278
+ on_error: (error, websocket) => {
2279
+ this.on_error(error, websocket);
2266
2280
  }
2267
2281
  });
2268
2282
  }
2269
2283
  catch (error) {
2270
- if (error instanceof WebSocketConnectionError)
2271
- throw new DdbConnectionError(this, { cause: error });
2272
- else
2273
- throw error;
2284
+ throw new DdbConnectionError(this, error);
2274
2285
  }
2275
2286
  if (this.streaming)
2276
2287
  await this.subscribe();
@@ -2399,6 +2410,13 @@ export class DDB {
2399
2410
  }
2400
2411
  catch { }
2401
2412
  // 临界区结束,只有一个 rpc 函数调用运行到这里,可以独占 this.on_message 然后写 WebSocket
2413
+ if (!this.connected) {
2414
+ presult.reject(new DdbConnectionError(this));
2415
+ return presult;
2416
+ }
2417
+ this.on_error = (error, websocket) => {
2418
+ presult.reject(new DdbConnectionError(this, error));
2419
+ };
2402
2420
  this.on_message = buffer => {
2403
2421
  try {
2404
2422
  const buf = new Uint8Array(buffer);
@@ -2430,19 +2448,28 @@ export class DDB {
2430
2448
  const command = this.enc.encode((() => {
2431
2449
  switch (type) {
2432
2450
  case 'function':
2451
+ if (this.verbose)
2452
+ console.log(func + args.map(arg => inspect(arg, { colors: false })).join(', ').bracket());
2433
2453
  return 'function\n' +
2434
2454
  `${func}\n` +
2435
2455
  `${args.length}\n` +
2436
2456
  `${Number(DDB.le_client)}\n`;
2437
2457
  case 'script':
2458
+ if (this.verbose)
2459
+ console.log(script);
2438
2460
  return 'script\n' +
2439
2461
  script;
2440
2462
  case 'variable':
2463
+ if (this.verbose)
2464
+ for (let i = 0; i < vars.length; i++)
2465
+ console.log(`${vars[i]} = ${inspect(args[i], { colors: false })}`);
2441
2466
  return 'variable\n' +
2442
2467
  `${vars.join(',')}\n` +
2443
2468
  `${vars.length}\n` +
2444
2469
  `${Number(DDB.le_client)}\n`;
2445
2470
  case 'connect':
2471
+ if (this.verbose)
2472
+ console.log('connect()');
2446
2473
  return 'connect\n';
2447
2474
  }
2448
2475
  })());