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/browser.d.ts CHANGED
@@ -271,13 +271,16 @@ export declare class DDB {
271
271
  password: string;
272
272
  /** python session flag (2048) */
273
273
  python: boolean;
274
+ print_message_buffer: boolean;
275
+ print_object_buffer: boolean;
276
+ print_message: boolean;
274
277
  parse_object: boolean;
275
- /** message handler */
276
- on_message({ type, data }: DdbMessage, _this: DDB): void;
278
+ /** message handlers */
279
+ handlers: DdbMessageHandler[];
277
280
  /** resolver, rejector, promise of last rpc */
278
- presolver(message: DdbMessage): void;
281
+ presolver(ddbobj: DdbObj): void;
279
282
  prejector(error: Error): void;
280
- presult: Promise<DdbMessage>;
283
+ presult: Promise<DdbObj<DdbValue>>;
281
284
  get connected(): boolean;
282
285
  /**
283
286
  Initialize an instance of DolphinDB Client using the WebSocket URL
@@ -292,7 +295,7 @@ export declare class DDB {
292
295
  @example
293
296
  let ddb = new DDB('ws://127.0.0.1:8848')
294
297
 
295
- // 使用 HTTPS 加密
298
+ // Encrypt with HTTPS
296
299
  let ddbsecure = new DDB('wss://dolphindb.com', {
297
300
  autologin: true,
298
301
  username: 'admin',
@@ -350,31 +353,31 @@ export declare class DDB {
350
353
  - options:
351
354
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
352
355
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
353
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
354
- - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
355
- 不做解析,以便后续转发、序列化
356
+ - handler?: 处理本次 rpc 期间的消息
357
+ - parse_object?: 在本次 rpc 期间设置 parse_object, 结束后恢复原有
358
+ 为 false 时返回的 DdbObj 仅含有 buffer 和 le,不做解析,以便后续转发、序列化
356
359
  */
357
- rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, on_message, parse_object, }: {
360
+ rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, handler, parse_object, }: {
358
361
  script?: string;
359
362
  func?: string;
360
363
  args?: (DdbObj | string | boolean)[];
361
364
  vars?: string[];
362
365
  urgent?: boolean;
363
- on_message?: DDB['on_message'];
366
+ handler?: DdbMessageHandler;
364
367
  parse_object?: boolean;
365
368
  }): Promise<T>;
366
369
  /** eval script through websocket (script command)
367
370
  - script?: Script to execute
368
371
  - options?: execution options
369
372
  - urgent?: Urgent flag to ensure that submitted scripts are processed by urgent workers to prevent being blocked by other jobs
370
- - on_message?: Set the message handler during this rpc, and restore the original after the end
373
+ - handler?: Process messages during this rpc
371
374
  - parse_object?: Set parse_object during this rpc, and restore the original after the end.
372
375
  When it is false, the returned DdbObj only contains buffer and le without parsing,
373
376
  so as to facilitate subsequent forwarding and serialization
374
377
  */
375
- eval<T extends DdbObj>(script: string, { urgent, on_message, parse_object, }?: {
378
+ eval<T extends DdbObj>(script: string, { urgent, handler, parse_object, }?: {
376
379
  urgent?: boolean;
377
- on_message?: DDB['on_message'];
380
+ handler?: DdbMessageHandler;
378
381
  parse_object?: boolean;
379
382
  }): Promise<T>;
380
383
  /** call function through websocket (function command)
@@ -386,18 +389,18 @@ export declare class DDB {
386
389
  - nodes?: When setting multiple node aliases, send them to the corresponding multiple nodes in the cluster for execution (using the pnodeRun method in DolphinDB)
387
390
  - func_type?: It must be passed when setting the node parameter, the function type needs to be specified, and it is not passed in other cases
388
391
  - add_node_alias?: Select to pass when setting the nodes parameter, otherwise not pass
389
- - on_message?: Set the message handler during this rpc, and restore the original after the end
392
+ - handler?: Process messages during this rpc
390
393
  - parse_object?: Set parse_object during this rpc, and restore the original after the end.
391
394
  When it is false, the returned DdbObj only contains buffer and le without parsing,
392
395
  so as to facilitate subsequent forwarding and serialization
393
396
  */
394
- call<T extends DdbObj>(func: string, args?: (DdbObj | string | boolean)[], { urgent, node, nodes, func_type, add_node_alias, on_message, parse_object, }?: {
397
+ call<T extends DdbObj>(func: string, args?: (DdbObj | string | boolean)[], { urgent, node, nodes, func_type, add_node_alias, handler, parse_object, }?: {
395
398
  urgent?: boolean;
396
399
  node?: string;
397
400
  nodes?: string[];
398
401
  func_type?: DdbFunctionType;
399
402
  add_node_alias?: boolean;
400
- on_message?: DDB['on_message'];
403
+ handler?: DdbMessageHandler;
401
404
  parse_object?: boolean;
402
405
  }): Promise<T>;
403
406
  /** upload variable through websocket (variable command) */
@@ -405,8 +408,8 @@ export declare class DDB {
405
408
  /** Uploaded variables' name */
406
409
  vars: string[],
407
410
  /** Uploaded variables' value */
408
- args: (DdbObj | string | boolean)[], { on_message, parse_object, }?: {
409
- on_message?: DDB['on_message'];
411
+ args: (DdbObj | string | boolean)[], { handler, parse_object, }?: {
412
+ handler?: DdbMessageHandler;
410
413
  parse_object?: boolean;
411
414
  }): Promise<DdbObj<DdbValue>>;
412
415
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
@@ -416,6 +419,9 @@ export declare class DDB {
416
419
  /** 转换 js 数组为 DdbObj[] */
417
420
  to_ddbobjs(values: any[]): DdbObj<DdbValue>[];
418
421
  }
422
+ export interface DdbMessageHandler {
423
+ (message: DdbMessage, _this: DDB): any;
424
+ }
419
425
  export interface DdbPrintMessage {
420
426
  type: 'print';
421
427
  data: string;
@@ -424,6 +430,10 @@ export interface DdbObjectMessage {
424
430
  type: 'object';
425
431
  data: DdbObj;
426
432
  }
427
- export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage;
433
+ export interface DdbErrorMessage {
434
+ type: 'error';
435
+ data: Error;
436
+ }
437
+ export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage | DdbErrorMessage;
428
438
  export declare let ddb: DDB;
429
439
  export default ddb;
package/browser.js CHANGED
@@ -1342,7 +1342,7 @@ class DDB {
1342
1342
  @example
1343
1343
  let ddb = new DDB('ws://127.0.0.1:8848')
1344
1344
 
1345
- // 使用 HTTPS 加密
1345
+ // Encrypt with HTTPS
1346
1346
  let ddbsecure = new DDB('wss://dolphindb.com', {
1347
1347
  autologin: true,
1348
1348
  username: 'admin',
@@ -1367,7 +1367,12 @@ class DDB {
1367
1367
  this.password = '123456';
1368
1368
  /** python session flag (2048) */
1369
1369
  this.python = false;
1370
+ this.print_message_buffer = false;
1371
+ this.print_object_buffer = false;
1372
+ this.print_message = true;
1370
1373
  this.parse_object = true;
1374
+ /** message handlers */
1375
+ this.handlers = [];
1371
1376
  this.presult = Promise.resolve(null);
1372
1377
  if (!url) {
1373
1378
  const _url = new URL(location.href);
@@ -1385,16 +1390,8 @@ class DDB {
1385
1390
  if (options.python !== undefined)
1386
1391
  this.python = options.python;
1387
1392
  }
1388
- /** message handler */
1389
- on_message({ type, data }, _this) {
1390
- switch (type) {
1391
- case 'print':
1392
- console.log(data);
1393
- return;
1394
- }
1395
- }
1396
1393
  /** resolver, rejector, promise of last rpc */
1397
- presolver(message) { }
1394
+ presolver(ddbobj) { }
1398
1395
  prejector(error) { }
1399
1396
  get connected() {
1400
1397
  return this.websocket?.readyState === WebSocket.OPEN;
@@ -1427,35 +1424,66 @@ class DDB {
1427
1424
  },
1428
1425
  on_message: event => {
1429
1426
  const buf = new Uint8Array(event.data);
1430
- try {
1431
- const message = this.parse_message(buf);
1432
- this.on_message(message, this);
1433
- if (message.type === 'print')
1434
- return;
1435
- this.presolver(message);
1427
+ if (this.print_message_buffer)
1428
+ console.log(buf);
1429
+ const message = this.parse_message(buf);
1430
+ if (this.handlers.length) {
1431
+ const _handlers = [...this.handlers].reverse();
1432
+ for (const handler of _handlers)
1433
+ handler(message, this);
1436
1434
  }
1437
- catch (error) {
1438
- this.prejector(error);
1435
+ const { type, data } = message;
1436
+ switch (type) {
1437
+ case 'print':
1438
+ if (this.print_message)
1439
+ console.log(data);
1440
+ return;
1441
+ case 'object':
1442
+ if (this.print_object_buffer)
1443
+ console.log(data.buffer);
1444
+ this.presolver(data);
1445
+ return;
1446
+ case 'error':
1447
+ this.prejector(data);
1448
+ return;
1439
1449
  }
1440
1450
  }
1441
1451
  });
1442
- if (!this.python)
1443
- await this.eval('def pnode_run (nodes, func_name, args, add_node_alias = true) {\n' +
1452
+ await this.eval(this.python ?
1453
+ 'def pnode_run (nodes, func_name, args, add_node_alias):\n' +
1444
1454
  ' nargs = size(args)\n' +
1445
1455
  ' func = funcByName(func_name)\n' +
1446
1456
  ' \n' +
1447
- ' if (!nargs)\n' +
1457
+ ' if not nargs:\n' +
1448
1458
  ' return pnodeRun(func, nodes, add_node_alias)\n' +
1449
1459
  ' \n' +
1450
- ' args_partial = array(any, 1 + nargs, 1 + nargs)\n' +
1451
- ' args_partial[0] = func\n' +
1452
- ' args_partial[1:] = args\n' +
1460
+ ' args_partial = [ ]\n' +
1461
+ ' args_partial.append(func)\n' +
1462
+ ' for a in args:\n' +
1463
+ ' args_partial.append(a)\n' +
1464
+ ' \n' +
1453
1465
  ' return pnodeRun(\n' +
1454
1466
  ' unifiedCall(partial, args_partial),\n' +
1455
1467
  ' nodes,\n' +
1456
1468
  ' add_node_alias\n' +
1457
- ' )\n' +
1458
- '}\n', { urgent: true });
1469
+ ' )\n'
1470
+ :
1471
+ 'def pnode_run (nodes, func_name, args, add_node_alias = true) {\n' +
1472
+ ' nargs = size(args)\n' +
1473
+ ' func = funcByName(func_name)\n' +
1474
+ ' \n' +
1475
+ ' if (!nargs)\n' +
1476
+ ' return pnodeRun(func, nodes, add_node_alias)\n' +
1477
+ ' \n' +
1478
+ ' args_partial = array(any, 1 + nargs, 1 + nargs)\n' +
1479
+ ' args_partial[0] = func\n' +
1480
+ ' args_partial[1:] = args\n' +
1481
+ ' return pnodeRun(\n' +
1482
+ ' unifiedCall(partial, args_partial),\n' +
1483
+ ' nodes,\n' +
1484
+ ' add_node_alias\n' +
1485
+ ' )\n' +
1486
+ '}\n', { urgent: true });
1459
1487
  if (this.autologin)
1460
1488
  if (this.python)
1461
1489
  await this.eval(`login(${this.username.quote('double')}, ${this.password.quote('double')})`, { urgent: true });
@@ -1505,11 +1533,11 @@ class DDB {
1505
1533
  - options:
1506
1534
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
1507
1535
  - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
1508
- - on_message?: 在该次 rpc 期间设置 message handler, 结束后恢复原有
1509
- - parse_object?: 在该次 rpc 期间设置 parse_object, 结束后恢复原有,为 false 时返回的 DdbObj 仅含有 buffer 和 le,
1510
- 不做解析,以便后续转发、序列化
1536
+ - handler?: 处理本次 rpc 期间的消息
1537
+ - parse_object?: 在本次 rpc 期间设置 parse_object, 结束后恢复原有
1538
+ 为 false 时返回的 DdbObj 仅含有 buffer 和 le,不做解析,以便后续转发、序列化
1511
1539
  */
1512
- async rpc(type, { script, func, args = [], vars = [], urgent, on_message, parse_object, }) {
1540
+ async rpc(type, { script, func, args = [], vars = [], urgent, handler, parse_object, }) {
1513
1541
  if (!this.websocket)
1514
1542
  await this.connect();
1515
1543
  if (!this.connected)
@@ -1560,19 +1588,18 @@ class DDB {
1560
1588
  command,
1561
1589
  ...args.map((arg) => arg.pack())
1562
1590
  ]);
1563
- const _on_message = this.on_message;
1564
1591
  const _parse_object = this.parse_object;
1565
- if (on_message)
1566
- this.on_message = on_message;
1592
+ if (handler)
1593
+ this.handlers.push(handler);
1567
1594
  if (parse_object !== undefined)
1568
1595
  this.parse_object = parse_object;
1569
1596
  try {
1570
1597
  this.websocket.send(message);
1571
- const { data } = (await presult);
1572
- return data;
1598
+ return (await presult);
1573
1599
  }
1574
1600
  finally {
1575
- this.on_message = _on_message;
1601
+ if (handler)
1602
+ this.handlers = this.handlers.filter(h => h !== handler);
1576
1603
  this.parse_object = _parse_object;
1577
1604
  }
1578
1605
  }
@@ -1580,13 +1607,13 @@ class DDB {
1580
1607
  - script?: Script to execute
1581
1608
  - options?: execution options
1582
1609
  - urgent?: Urgent flag to ensure that submitted scripts are processed by urgent workers to prevent being blocked by other jobs
1583
- - on_message?: Set the message handler during this rpc, and restore the original after the end
1610
+ - handler?: Process messages during this rpc
1584
1611
  - parse_object?: Set parse_object during this rpc, and restore the original after the end.
1585
1612
  When it is false, the returned DdbObj only contains buffer and le without parsing,
1586
1613
  so as to facilitate subsequent forwarding and serialization
1587
1614
  */
1588
- async eval(script, { urgent, on_message, parse_object, } = {}) {
1589
- return this.rpc('script', { script, urgent, on_message, parse_object });
1615
+ async eval(script, { urgent, handler, parse_object, } = {}) {
1616
+ return this.rpc('script', { script, urgent, handler, parse_object });
1590
1617
  }
1591
1618
  /** call function through websocket (function command)
1592
1619
  - func: function name
@@ -1597,14 +1624,12 @@ class DDB {
1597
1624
  - nodes?: When setting multiple node aliases, send them to the corresponding multiple nodes in the cluster for execution (using the pnodeRun method in DolphinDB)
1598
1625
  - func_type?: It must be passed when setting the node parameter, the function type needs to be specified, and it is not passed in other cases
1599
1626
  - add_node_alias?: Select to pass when setting the nodes parameter, otherwise not pass
1600
- - on_message?: Set the message handler during this rpc, and restore the original after the end
1627
+ - handler?: Process messages during this rpc
1601
1628
  - parse_object?: Set parse_object during this rpc, and restore the original after the end.
1602
1629
  When it is false, the returned DdbObj only contains buffer and le without parsing,
1603
1630
  so as to facilitate subsequent forwarding and serialization
1604
1631
  */
1605
- async call(func, args = [], { urgent, node, nodes, func_type, add_node_alias, on_message, parse_object, } = {}) {
1606
- if (this.python)
1607
- throw new Error('call doesn\'t support python session yet');
1632
+ async call(func, args = [], { urgent, node, nodes, func_type, add_node_alias, handler, parse_object, } = {}) {
1608
1633
  if (node) {
1609
1634
  if (typeof func_type === 'undefined')
1610
1635
  throw new Error('指定 node 时必须设置 func_type');
@@ -1634,7 +1659,7 @@ class DDB {
1634
1659
  func,
1635
1660
  args,
1636
1661
  urgent,
1637
- on_message,
1662
+ handler,
1638
1663
  parse_object
1639
1664
  });
1640
1665
  }
@@ -1643,10 +1668,10 @@ class DDB {
1643
1668
  /** Uploaded variables' name */
1644
1669
  vars,
1645
1670
  /** Uploaded variables' value */
1646
- args, { on_message, parse_object, } = {}) {
1671
+ args, { handler, parse_object, } = {}) {
1647
1672
  if (!args.length || args.length !== vars.length)
1648
1673
  throw new Error('variable 指令参数为空或参数名为空,或数量不匹配');
1649
- return this.rpc('variable', { vars, args, on_message, parse_object });
1674
+ return this.rpc('variable', { vars, args, handler, parse_object });
1650
1675
  }
1651
1676
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
1652
1677
  parse_message(buf, parse_object = this.parse_object) {
@@ -1679,7 +1704,10 @@ class DDB {
1679
1704
  /** 'OK' 表示成功,其它文本表示失败 */
1680
1705
  const message = this.dec.decode(buf.subarray(i_ls_1, i_lf_1));
1681
1706
  if (message !== 'OK')
1682
- throw new Error(message);
1707
+ return {
1708
+ type: 'error',
1709
+ data: new Error(message)
1710
+ };
1683
1711
  const buf_obj = buf.subarray(i_lf_1 + 1);
1684
1712
  return {
1685
1713
  type: 'object',