dolphindb 2.0.505 → 2.0.506

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
@@ -252,7 +252,6 @@ export declare class DDB {
252
252
  /** utf-8 text decoder */
253
253
  dec: TextDecoder;
254
254
  enc: TextEncoder;
255
- print_buffer: boolean;
256
255
  /** DolphinDB WebSocket URL
257
256
  e.g. `ws://127.0.0.1:8848/`, `wss://dolphindb.com`
258
257
  */
@@ -270,13 +269,16 @@ export declare class DDB {
270
269
  password: string;
271
270
  /** python session flag (2048) */
272
271
  python: boolean;
272
+ print_message_buffer: boolean;
273
+ print_object_buffer: boolean;
274
+ print_message: boolean;
273
275
  parse_object: boolean;
274
- /** message handler */
275
- on_message({ type, data }: DdbMessage, _this: DDB): void;
276
+ /** message handlers */
277
+ handlers: DdbMessageHandler[];
276
278
  /** resolver, rejector, promise of last rpc */
277
- presolver(message: DdbMessage): void;
279
+ presolver(ddbobj: DdbObj): void;
278
280
  prejector(error: Error): void;
279
- presult: Promise<DdbMessage>;
281
+ presult: Promise<DdbObj<DdbValue>>;
280
282
  get connected(): boolean;
281
283
  /**
282
284
  Initialize an instance of DolphinDB Client using the WebSocket URL
@@ -349,30 +351,30 @@ export declare class DDB {
349
351
  - options:
350
352
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
351
353
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
352
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
354
+ - handler?: 处理本次 rpc 期间的消息
353
355
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
354
356
  不做解析,以便后续转发、序列化
355
357
  */
356
- rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, on_message, parse_object, }: {
358
+ rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, handler, parse_object, }: {
357
359
  script?: string;
358
360
  func?: string;
359
361
  args?: (DdbObj | string | boolean)[];
360
362
  vars?: string[];
361
363
  urgent?: boolean;
362
- on_message?: DDB['on_message'];
364
+ handler?: DdbMessageHandler;
363
365
  parse_object?: boolean;
364
366
  }): Promise<T>;
365
367
  /** eval script through websocket (script command)
366
368
  - script?: 执行的脚本
367
369
  - options?: 执行选项
368
370
  - urgent?: 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞
369
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
371
+ - handler?: 处理本次 rpc 期间的消息
370
372
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
371
373
  不做解析,以便后续转发、序列化
372
374
  */
373
- eval<T extends DdbObj>(script: string, { urgent, on_message, parse_object, }?: {
375
+ eval<T extends DdbObj>(script: string, { urgent, handler, parse_object, }?: {
374
376
  urgent?: boolean;
375
- on_message?: DDB['on_message'];
377
+ handler?: DdbMessageHandler;
376
378
  parse_object?: boolean;
377
379
  }): Promise<T>;
378
380
  /** call function through websocket (function command)
@@ -384,17 +386,17 @@ export declare class DDB {
384
386
  - nodes?: 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法)
385
387
  - func_type?: 设置 node 参数时必传,需指定函数类型,其它情况下不传
386
388
  - add_node_alias?: 设置 nodes 参数时选传,其它情况不传
387
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
389
+ - handler?: 处理本次 rpc 期间的消息
388
390
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
389
391
  不做解析,以便后续转发、序列化
390
392
  */
391
- call<T extends DdbObj>(func: string, args?: (DdbObj | string | boolean)[], { urgent, node, nodes, func_type, add_node_alias, on_message, parse_object, }?: {
393
+ call<T extends DdbObj>(func: string, args?: (DdbObj | string | boolean)[], { urgent, node, nodes, func_type, add_node_alias, handler, parse_object, }?: {
392
394
  urgent?: boolean;
393
395
  node?: string;
394
396
  nodes?: string[];
395
397
  func_type?: DdbFunctionType;
396
398
  add_node_alias?: boolean;
397
- on_message?: DDB['on_message'];
399
+ handler?: DdbMessageHandler;
398
400
  parse_object?: boolean;
399
401
  }): Promise<T>;
400
402
  /** upload variable through websocket (variable command) */
@@ -402,8 +404,8 @@ export declare class DDB {
402
404
  /** Uploaded variables' name */
403
405
  vars: string[],
404
406
  /** Uploaded variables' value */
405
- args: (DdbObj | string | boolean)[], { on_message, parse_object, }?: {
406
- on_message?: DDB['on_message'];
407
+ args: (DdbObj | string | boolean)[], { handler, parse_object, }?: {
408
+ handler?: DdbMessageHandler;
407
409
  parse_object?: boolean;
408
410
  }): Promise<DdbObj<DdbValue>>;
409
411
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
@@ -413,6 +415,9 @@ export declare class DDB {
413
415
  /** 转换 js 数组为 DdbObj[] */
414
416
  to_ddbobjs(values: any[]): DdbObj<DdbValue>[];
415
417
  }
418
+ export interface DdbMessageHandler {
419
+ (message: DdbMessage, _this: DDB): any;
420
+ }
416
421
  export interface DdbPrintMessage {
417
422
  type: 'print';
418
423
  data: string;
@@ -421,5 +426,9 @@ export interface DdbObjectMessage {
421
426
  type: 'object';
422
427
  data: DdbObj;
423
428
  }
424
- export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage;
429
+ export interface DdbErrorMessage {
430
+ type: 'error';
431
+ data: Error;
432
+ }
433
+ export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage | DdbErrorMessage;
425
434
  export default DDB;
package/index.js CHANGED
@@ -884,10 +884,16 @@ class DdbObj {
884
884
  }
885
885
  })();
886
886
  const data = (() => {
887
- if (this.form === DdbForm.pair)
888
- return `${(0, utils_js_1.inspect)(this.value[0], options)}, ${(0, utils_js_1.inspect)(this.value[1], options)}`;
889
- if (this.form === DdbForm.scalar && this.type === DdbType.functiondef)
890
- return (0, utils_js_1.inspect)(this.value.name, options);
887
+ switch (this.form) {
888
+ case DdbForm.pair:
889
+ return `${(0, utils_js_1.inspect)(this.value[0], options)}, ${(0, utils_js_1.inspect)(this.value[1], options)}`;
890
+ case DdbForm.scalar:
891
+ switch (this.type) {
892
+ case DdbType.functiondef:
893
+ return (0, utils_js_1.inspect)(this.value.name, options);
894
+ }
895
+ break;
896
+ }
891
897
  if (this.value instanceof Uint8Array)
892
898
  return (0, utils_js_1.inspect)((0, utils_js_1.typed_array_to_buffer)(this.value), options);
893
899
  return (0, utils_js_1.inspect)(this.value, options);
@@ -1367,7 +1373,6 @@ class DDB {
1367
1373
  /** utf-8 text decoder */
1368
1374
  this.dec = new TextDecoder('utf-8');
1369
1375
  this.enc = new TextEncoder();
1370
- this.print_buffer = false;
1371
1376
  this.websocket = null;
1372
1377
  /** little endian (server) */
1373
1378
  this.le = true;
@@ -1379,7 +1384,12 @@ class DDB {
1379
1384
  this.password = '123456';
1380
1385
  /** python session flag (2048) */
1381
1386
  this.python = false;
1387
+ this.print_message_buffer = false;
1388
+ this.print_object_buffer = false;
1389
+ this.print_message = true;
1382
1390
  this.parse_object = true;
1391
+ /** message handlers */
1392
+ this.handlers = [];
1383
1393
  this.presult = Promise.resolve(null);
1384
1394
  this.url = url;
1385
1395
  if (options.autologin !== undefined)
@@ -1391,20 +1401,8 @@ class DDB {
1391
1401
  if (options.python !== undefined)
1392
1402
  this.python = options.python;
1393
1403
  }
1394
- /** message handler */
1395
- on_message({ type, data }, _this) {
1396
- switch (type) {
1397
- case 'print':
1398
- console.log(data);
1399
- return;
1400
- case 'object':
1401
- if (this.print_buffer)
1402
- console.log((0, utils_js_1.typed_array_to_buffer)(data.buffer));
1403
- return;
1404
- }
1405
- }
1406
1404
  /** resolver, rejector, promise of last rpc */
1407
- presolver(message) { }
1405
+ presolver(ddbobj) { }
1408
1406
  prejector(error) { }
1409
1407
  get connected() {
1410
1408
  return this.websocket?.readyState === ws_1.WebSocket.OPEN;
@@ -1437,35 +1435,66 @@ class DDB {
1437
1435
  },
1438
1436
  on_message: event => {
1439
1437
  const buf = new Uint8Array(event.data);
1440
- try {
1441
- const message = this.parse_message(buf);
1442
- this.on_message(message, this);
1443
- if (message.type === 'print')
1444
- return;
1445
- this.presolver(message);
1438
+ if (this.print_message_buffer)
1439
+ console.log((0, utils_js_1.typed_array_to_buffer)(buf));
1440
+ const message = this.parse_message(buf);
1441
+ if (this.handlers.length) {
1442
+ const _handlers = [...this.handlers].reverse();
1443
+ for (const handler of _handlers)
1444
+ handler(message, this);
1446
1445
  }
1447
- catch (error) {
1448
- this.prejector(error);
1446
+ const { type, data } = message;
1447
+ switch (type) {
1448
+ case 'print':
1449
+ if (this.print_message)
1450
+ console.log(data);
1451
+ return;
1452
+ case 'object':
1453
+ if (this.print_object_buffer)
1454
+ console.log((0, utils_js_1.typed_array_to_buffer)(data.buffer));
1455
+ this.presolver(data);
1456
+ return;
1457
+ case 'error':
1458
+ this.prejector(data);
1459
+ return;
1449
1460
  }
1450
1461
  }
1451
1462
  });
1452
- if (!this.python)
1453
- await this.eval('def pnode_run (nodes, func_name, args, add_node_alias = true) {\n' +
1463
+ await this.eval(this.python ?
1464
+ 'def pnode_run (nodes, func_name, args, add_node_alias):\n' +
1454
1465
  ' nargs = size(args)\n' +
1455
1466
  ' func = funcByName(func_name)\n' +
1456
1467
  ' \n' +
1457
- ' if (!nargs)\n' +
1468
+ ' if not nargs:\n' +
1458
1469
  ' return pnodeRun(func, nodes, add_node_alias)\n' +
1459
1470
  ' \n' +
1460
- ' args_partial = array(any, 1 + nargs, 1 + nargs)\n' +
1461
- ' args_partial[0] = func\n' +
1462
- ' args_partial[1:] = args\n' +
1471
+ ' args_partial = [ ]\n' +
1472
+ ' args_partial.append(func)\n' +
1473
+ ' for a in args:\n' +
1474
+ ' args_partial.append(a)\n' +
1475
+ ' \n' +
1463
1476
  ' return pnodeRun(\n' +
1464
1477
  ' unifiedCall(partial, args_partial),\n' +
1465
1478
  ' nodes,\n' +
1466
1479
  ' add_node_alias\n' +
1467
- ' )\n' +
1468
- '}\n', { urgent: true });
1480
+ ' )\n'
1481
+ :
1482
+ 'def pnode_run (nodes, func_name, args, add_node_alias = true) {\n' +
1483
+ ' nargs = size(args)\n' +
1484
+ ' func = funcByName(func_name)\n' +
1485
+ ' \n' +
1486
+ ' if (!nargs)\n' +
1487
+ ' return pnodeRun(func, nodes, add_node_alias)\n' +
1488
+ ' \n' +
1489
+ ' args_partial = array(any, 1 + nargs, 1 + nargs)\n' +
1490
+ ' args_partial[0] = func\n' +
1491
+ ' args_partial[1:] = args\n' +
1492
+ ' return pnodeRun(\n' +
1493
+ ' unifiedCall(partial, args_partial),\n' +
1494
+ ' nodes,\n' +
1495
+ ' add_node_alias\n' +
1496
+ ' )\n' +
1497
+ '}\n', { urgent: true });
1469
1498
  if (this.autologin)
1470
1499
  if (this.python)
1471
1500
  await this.eval(`login(${this.username.quote('double')}, ${this.password.quote('double')})`, { urgent: true });
@@ -1515,11 +1544,11 @@ class DDB {
1515
1544
  - options:
1516
1545
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
1517
1546
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
1518
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
1547
+ - handler?: 处理本次 rpc 期间的消息
1519
1548
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
1520
1549
  不做解析,以便后续转发、序列化
1521
1550
  */
1522
- async rpc(type, { script, func, args = [], vars = [], urgent, on_message, parse_object, }) {
1551
+ async rpc(type, { script, func, args = [], vars = [], urgent, handler, parse_object, }) {
1523
1552
  if (!this.websocket)
1524
1553
  await this.connect();
1525
1554
  if (!this.connected)
@@ -1570,19 +1599,18 @@ class DDB {
1570
1599
  command,
1571
1600
  ...args.map((arg) => arg.pack())
1572
1601
  ]);
1573
- const _on_message = this.on_message;
1574
1602
  const _parse_object = this.parse_object;
1575
- if (on_message)
1576
- this.on_message = on_message;
1603
+ if (handler)
1604
+ this.handlers.push(handler);
1577
1605
  if (parse_object !== undefined)
1578
1606
  this.parse_object = parse_object;
1579
1607
  try {
1580
1608
  this.websocket.send(message);
1581
- const { data } = (await presult);
1582
- return data;
1609
+ return (await presult);
1583
1610
  }
1584
1611
  finally {
1585
- this.on_message = _on_message;
1612
+ if (handler)
1613
+ this.handlers = this.handlers.filter(h => h !== handler);
1586
1614
  this.parse_object = _parse_object;
1587
1615
  }
1588
1616
  }
@@ -1590,12 +1618,12 @@ class DDB {
1590
1618
  - script?: 执行的脚本
1591
1619
  - options?: 执行选项
1592
1620
  - urgent?: 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞
1593
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
1621
+ - handler?: 处理本次 rpc 期间的消息
1594
1622
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
1595
1623
  不做解析,以便后续转发、序列化
1596
1624
  */
1597
- async eval(script, { urgent, on_message, parse_object, } = {}) {
1598
- return this.rpc('script', { script, urgent, on_message, parse_object });
1625
+ async eval(script, { urgent, handler, parse_object, } = {}) {
1626
+ return this.rpc('script', { script, urgent, handler, parse_object });
1599
1627
  }
1600
1628
  /** call function through websocket (function command)
1601
1629
  - func: 函数名
@@ -1606,13 +1634,11 @@ class DDB {
1606
1634
  - nodes?: 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法)
1607
1635
  - func_type?: 设置 node 参数时必传,需指定函数类型,其它情况下不传
1608
1636
  - add_node_alias?: 设置 nodes 参数时选传,其它情况不传
1609
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
1637
+ - handler?: 处理本次 rpc 期间的消息
1610
1638
  - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
1611
1639
  不做解析,以便后续转发、序列化
1612
1640
  */
1613
- async call(func, args = [], { urgent, node, nodes, func_type, add_node_alias, on_message, parse_object, } = {}) {
1614
- if (this.python)
1615
- throw new Error('call doesn\'t support python session yet');
1641
+ async call(func, args = [], { urgent, node, nodes, func_type, add_node_alias, handler, parse_object, } = {}) {
1616
1642
  if (node) {
1617
1643
  if (typeof func_type === 'undefined')
1618
1644
  throw new Error('指定 node 时必须设置 func_type');
@@ -1642,7 +1668,7 @@ class DDB {
1642
1668
  func,
1643
1669
  args,
1644
1670
  urgent,
1645
- on_message,
1671
+ handler,
1646
1672
  parse_object
1647
1673
  });
1648
1674
  }
@@ -1651,10 +1677,10 @@ class DDB {
1651
1677
  /** Uploaded variables' name */
1652
1678
  vars,
1653
1679
  /** Uploaded variables' value */
1654
- args, { on_message, parse_object, } = {}) {
1680
+ args, { handler, parse_object, } = {}) {
1655
1681
  if (!args.length || args.length !== vars.length)
1656
- throw new Error('variable 指令参数为空或参数名为空,或数量不匹配');
1657
- return this.rpc('variable', { vars, args, on_message, parse_object });
1682
+ throw new Error('variable command parameter is empty or parameter name is empty, or the number does not match');
1683
+ return this.rpc('variable', { vars, args, handler, parse_object });
1658
1684
  }
1659
1685
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
1660
1686
  parse_message(buf, parse_object = this.parse_object) {
@@ -1687,7 +1713,10 @@ class DDB {
1687
1713
  /** 'OK' 表示成功,其它文本表示失败 */
1688
1714
  const message = this.dec.decode(buf.subarray(i_ls_1, i_lf_1));
1689
1715
  if (message !== 'OK')
1690
- throw new Error(message);
1716
+ return {
1717
+ type: 'error',
1718
+ data: new Error(message)
1719
+ };
1691
1720
  const buf_obj = buf.subarray(i_lf_1 + 1);
1692
1721
  return {
1693
1722
  type: 'object',