dolphindb 3.0.0 → 3.0.2

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/unused.js ADDED
@@ -0,0 +1,473 @@
1
+ // class DdbObj {
2
+ // port = 8848
3
+ export {};
4
+ // /** ddb 连接状态 */
5
+ // state = '未连接'
6
+ // socket = null as Socket
7
+ // clients = new Set<Duplex>()
8
+ // /** dolphindb http rpc
9
+ // - code
10
+ // - options
11
+ // - session_id: 20 分钟内无操作服务端会清空 session,删除所有变量
12
+ // */
13
+ // async rpc_json (
14
+ // code: string,
15
+ // {
16
+ // hostname = '127.0.0.1',
17
+ // // hostname = 'localhost',
18
+ // port = this.port,
19
+ // tls = false,
20
+ // sid = this.sid,
21
+ // // TEST
22
+ // proxy = MyProxy.whistle
23
+ // }: {
24
+ // hostname?: string
25
+ // port?: number
26
+ // tls?: boolean
27
+ // sid?: string
28
+ // proxy?: MyProxy | false
29
+ // } = { }
30
+ // ): Promise<DdbJsonObj> {
31
+ // let result = await request_json(`${tls ? 'https' : 'http'}://${hostname}:${port}/${sid}`, {
32
+ // body: {
33
+ // sessionID: sid,
34
+ // functionName: 'executeCode',
35
+ // params: [{
36
+ // name: 'script',
37
+ // form: 'scalar',
38
+ // type: 'string',
39
+ // value: encodeURIComponent(code)
40
+ // }]
41
+ // },
42
+ // proxy
43
+ // })
44
+ // this.sid = result.sessionID
45
+ // result.resultCode = Number(result.resultCode)
46
+ // // result.userId: 未登录时为 `''`
47
+ // if (result.resultCode)
48
+ // throw Object.assign(
49
+ // // result.msg: 成功时为 `''`; 失败时为错误信息
50
+ // new Error((result.msg as string).red),
51
+ // result
52
+ // )
53
+ // return new DdbJsonObj(result.object[0])
54
+ // }
55
+ // /** 创建 tcp 连接到 ddb,并完成 connect 请求,读到 chunk 后执行 on_chunk 回调
56
+ // 成功完成后 this.socket, this.sid 会被初始化
57
+ // */
58
+ // async connect_socket () {
59
+ // // --- 创建 socket, attach 状态、异常事件处理
60
+ // this.socket = new Socket({
61
+ // allowHalfOpen: false,
62
+ // readable: true,
63
+ // writable: true
64
+ // })
65
+ // this.socket.on('lookup', (error, address, family, host) => {
66
+ // console.log('socket 域名解析 (lookup) 完成', error, address, family, host)
67
+ // })
68
+ // this.socket.on('ready', () => {
69
+ // console.log('socket 准备就绪')
70
+ // })
71
+ // this.socket.on('close', has_error => {
72
+ // console.log('socket 成功关闭,是否出错?', has_error)
73
+ // })
74
+ // this.socket.on('timeout', () => {
75
+ // console.log('socket 超时')
76
+ // })
77
+ // this.socket.on('error', error => {
78
+ // console.log('socket 出错了', error)
79
+ // })
80
+ // // ---
81
+ // console.log('将 socket 连接到 server')
82
+ // await new Promise<void>(resolve => {
83
+ // // callback 会被 socket.once('connect', callback)
84
+ // this.socket.connect({
85
+ // port: this.port,
86
+ // // host: 'localhost',
87
+ // host: '127.0.0.1',
88
+ // family: 0,
89
+ // }, resolve)
90
+ // })
91
+ // console.log(this.state = 'socket 已连接')
92
+ // // ---
93
+ // console.log('发送 connect 请求到 ddb')
94
+ // this.socket.write(
95
+ // `API ${this.sid || '0'} 8\n` +
96
+ // 'connect\n'
97
+ // )
98
+ // console.log(this.state = '已发送 connect 请求')
99
+ // // --- 读取流数据,只等到 connect 完成,后续 chunk 交给 callback
100
+ // await new Promise<void>(async (resolve, reject) => {
101
+ // let last_chunk: Buffer
102
+ // try {
103
+ // for await (const chunk of this.socket) {
104
+ // last_chunk = chunk
105
+ // console.log('ddb.socket 读取到 chunk, 转发给所有 websocket clients')
106
+ // const ck = chunk as Uint8Array
107
+ // if (this.state === '已发送 connect 请求') {
108
+ // // --- 读取 sid
109
+ // const i_sid_end = ck.indexOf(0x20)
110
+ // this.sid = this.dec.decode(
111
+ // ck.subarray(
112
+ // 0,
113
+ // i_sid_end
114
+ // )
115
+ // )
116
+ // console.log('sid = ', this.sid)
117
+ // console.log(this.state = 'connect 完成')
118
+ // resolve()
119
+ // continue
120
+ // }
121
+ // this.on_chunk(ck)
122
+ // }
123
+ // console.log('ddb.socket 完美结束 (no more data to read)')
124
+ // } catch (error) {
125
+ // console.log('捕获到 socket 错误', error)
126
+ // console.log('最后一个 chunk', last_chunk)
127
+ // reject(error)
128
+ // }
129
+ // })
130
+ // }
131
+ // on_chunk (chunk: Uint8Array) {
132
+ // for (let client of this.clients)
133
+ // client.write(chunk)
134
+ // }
135
+ // }
136
+ // class DdbJsonObj {
137
+ // static ddb_null = { name: '', form: 'scalar', type: 'void', value: null } as const
138
+ // static table_config: TableUserConfig = {
139
+ // border: getBorderCharacters('void'),
140
+ // singleLine: true,
141
+ // columnDefault: {
142
+ // paddingLeft: 0,
143
+ // paddingRight: 1,
144
+ // alignment: 'right'
145
+ // },
146
+ // }
147
+ // name: string
148
+ // form: DdbJsonForm
149
+ // type?: DdbJsonType
150
+ // /** 向量有这个属性 */
151
+ // size?: number
152
+ // value: null | boolean | number | string | DdbJsonObj | DdbJsonObj[]
153
+ // constructor (data?: {
154
+ // name: string
155
+ // form: DdbJsonForm
156
+ // type?: DdbJsonType
157
+ // size?: string | number
158
+ // value: any
159
+ // }) {
160
+ // const { name, form, type, size, value } = data || DdbJsonObj.ddb_null as DdbJsonObj
161
+ // this.name = name
162
+ // this.form = form
163
+ // this.type = type
164
+ // if ((typeof size === 'string' && size) || typeof size === 'number')
165
+ // this.size = Number(size)
166
+ // this.value = (() => {
167
+ // if (form === 'scalar') {
168
+ // if (type === 'void')
169
+ // return null
170
+ // if (type === 'int' || type === 'short' || type === 'float' || type === 'double') {
171
+ // if (value === 'pi' || value === 'e')
172
+ // return value
173
+ // return Number(value)
174
+ // }
175
+ // if (type === 'long')
176
+ // return BigInt(value)
177
+ // if (type === 'bool')
178
+ // return Boolean(value)
179
+ // // integral: int128
180
+ // // literal: char | string | uuid
181
+ // // temporal: date | month | time | second | datetime | timestamp | nanotime | nanotimestamp | datehour
182
+ // // system: code | ipaddr | duration
183
+ // // complex | point
184
+ // return value
185
+ // }
186
+ // if (form === 'pair')
187
+ // return value
188
+ // if (form === 'vector')
189
+ // return value
190
+ // if (form === 'set')
191
+ // return value
192
+ // if (form === 'matrix')
193
+ // return value
194
+ // if (form === 'dictionary')
195
+ // return value
196
+ // if (form === 'table')
197
+ // return value
198
+ // })()
199
+ // }
200
+ // [inspect.custom] () {
201
+ // const { form, type, value, name, size } = this
202
+ // if (form === 'scalar')
203
+ // return this.format_scalar(type, value)
204
+ // if (form === 'pair') {
205
+ // const [l, r] = value as [any, any]
206
+ // return `${ l === null ? '' : this.format_scalar(type, l) }:${ r === null ? '' : this.format_scalar(type, r) }`
207
+ // }
208
+ // if (form === 'vector' || form === 'set') {
209
+ // return (name ? `${name} ` : '') +
210
+ // (
211
+ // (value as any[]).map(x =>
212
+ // x && typeof x === 'object' ?
213
+ // inspect(
214
+ // new DdbJsonObj(x),
215
+ // { colors: false }
216
+ // )
217
+ // :
218
+ // this.format_scalar(type, x)
219
+ // ).join(', ') || ' '
220
+ // ).surround(
221
+ // form === 'vector' ? '[' : '{',
222
+ // form === 'vector' ? ']' : '}',
223
+ // )
224
+ // }
225
+ // if (form === 'dictionary') {
226
+ // const [keys, values] = value as [any, any]
227
+ // let obj = { } as Record<string, DdbJsonObj | string>
228
+ // const size = Number(keys.size)
229
+ // if (!size) return `dict(${keys.type}, ${values.type})`
230
+ // for (let i = 0; i < size; i++) {
231
+ // const key = keys.type === 'string' ?
232
+ // keys.value[i]
233
+ // :
234
+ // this.format_scalar(keys.type, keys.value[i])
235
+ // const v = values.value[i]
236
+ // const value = typeof v === 'string' ?
237
+ // v
238
+ // :
239
+ // new DdbJsonObj(v)
240
+ // obj[key] = value
241
+ // }
242
+ // return obj
243
+ // }
244
+ // if (form === 'matrix') {
245
+ // const nrows = Number(value[1].value)
246
+ // const ncols = Number(value[2].value)
247
+ // const values = value[0].value
248
+ // let data = [ ]
249
+ // for (let i = 0; i < nrows; i++)
250
+ // for (let j = 0; j < ncols; j++) {
251
+ // const value = values[nrows * j + i]
252
+ // let row = data[i] || [ ]
253
+ // row[j] = this.format_scalar(type, value)
254
+ // data[i] = row
255
+ // }
256
+ // return `matrix <${type}[${ncols}][${nrows}]>\n` +
257
+ // table(data, DdbJsonObj.table_config)
258
+ // }
259
+ // if (form === 'table') {
260
+ // const nrows = Number(size)
261
+ // const ncols = (value as any[]).length
262
+ // const headers = (value as any[]).map(({ name }) => name)
263
+ // let data = [ ]
264
+ // for (let i = 0; i < nrows; i++)
265
+ // for (let j = 0; j < ncols; j++) {
266
+ // const col = value[j]
267
+ // const v = col.value[i]
268
+ // let row = data[i] || [ ]
269
+ // row[j] = this.format_scalar(col.type, v)
270
+ // data[i] = row
271
+ // }
272
+ // return `table ${ name ? `${name} ` : '' }(${nrows} rows)\n` +
273
+ // table(
274
+ // [
275
+ // headers,
276
+ // ...data
277
+ // ],
278
+ // DdbJsonObj.table_config,
279
+ // )
280
+ // }
281
+ // return this
282
+ // }
283
+ // format_scalar (type: DdbJsonType, value: any) {
284
+ // // 可以被 js 原生类型表示
285
+ // if (
286
+ // type === 'void' || type === 'bool' ||
287
+ // type === 'int' || type === 'short' || type === 'long' || type === 'float' || type === 'double'
288
+ // )
289
+ // return value
290
+ // if (type === 'string')
291
+ // return inspect(value, { colors: false })
292
+ // // 可以直接用 <value> 表示
293
+ // if (
294
+ // type === 'code' || type === 'point' || type === 'complex' ||
295
+ // type === 'second' || type === 'time' || type === 'nanotime' || type === 'datehour' || type === 'duration'
296
+ // )
297
+ // return value || 'null'
298
+ // if (type === 'month') {
299
+ // if (!value)
300
+ // return 'null'
301
+ // return `${value}M`
302
+ // }
303
+ // if (type === 'minute') {
304
+ // if (!value)
305
+ // return 'null'
306
+ // return `${value}m`
307
+ // }
308
+ // // 用 type('<value>') 表示
309
+ // return `${type}(${(value as string).quote()})`
310
+ // }
311
+ // }
312
+ // type DdbJsonForm = 'scalar' | 'pair' | 'vector' | 'set' | 'matrix' | 'dictionary' | 'table'
313
+ // type DdbJsonType =
314
+ // 'void' | 'bool' | 'char' |
315
+ // 'short' | 'int' | 'long' |
316
+ // 'date' | 'month' | 'time' | 'minute' | 'second' | 'datetime' | 'timestamp' | 'nanotime' | 'nanotimestamp' |
317
+ // 'float' | 'double' | 'symbol' | 'string' | 'uuid' | 'functiondef' | 'handle' | 'code' |
318
+ // 'datasource' | 'resource' | 'any' | 'compress' | 'datehour' | 'ipaddr' | 'int128' | 'blob' | 'complex' | 'point' | 'duration'
319
+ // type DdbJsonScalar = number | string | any
320
+ // const ddb_gateway = {
321
+ // server: null as SocketServer,
322
+ // clients: new Set<Socket>(),
323
+ // port: 8849,
324
+ // controllers: {
325
+ // /** alias */
326
+ // c0: {
327
+ // hostname: '127.0.0.1',
328
+ // port: 8850,
329
+ // },
330
+ // c1: {
331
+ // hostname: '127.0.0.1',
332
+ // port: 8851,
333
+ // },
334
+ // c2: {
335
+ // hostname: '127.0.0.1',
336
+ // port: 8852,
337
+ // }
338
+ // },
339
+ // master: '',
340
+ // sids: new Map<string, string>(),
341
+ // state: 'init',
342
+ // async start () {
343
+ // for (const alias in this.controllers) {
344
+ // this.master = alias
345
+ // break
346
+ // }
347
+ // this.server = create_socket_server(
348
+ // {
349
+ // allowHalfOpen: false,
350
+ // pauseOnConnect: false,
351
+ // },
352
+ // this.accept.bind(this)
353
+ // )
354
+ // this.server.on('error', error => {
355
+ // console.log(new Date(), 'gateway 出错:', error)
356
+ // })
357
+ // await new Promise<void>(resolve => {
358
+ // this.server.listen(this.port, resolve)
359
+ // })
360
+ // this.auto_switch_master()
361
+ // console.log(new Date(), 'gateway 成功启动')
362
+ // },
363
+ // async stop () {
364
+ // this.state = 'stopping'
365
+ // console.log(new Date(), '正在停止 auto_switch_master')
366
+ // for (const client of this.clients)
367
+ // client.end()
368
+ // await new Promise<void>((resolve, reject) => {
369
+ // this.server.close(error => {
370
+ // if (error) {
371
+ // reject(error)
372
+ // return
373
+ // }
374
+ // resolve()
375
+ // })
376
+ // })
377
+ // },
378
+ // /** 接收到 client tcp 连接 */
379
+ // async accept (client: Socket) {
380
+ // this.clients.add(client)
381
+ // const port = client.remotePort
382
+ // // ---
383
+ // console.log(port, new Date(), 'gateway accept client 连接,开始建立管道')
384
+ // let controller = new Socket({
385
+ // allowHalfOpen: false,
386
+ // readable: true,
387
+ // writable: true,
388
+ // })
389
+ // controller.on('lookup', (error, address, family, host) => {
390
+ // console.log(port, new Date(), 'controller 域名解析 (lookup) 完成', error, address, family, host)
391
+ // })
392
+ // controller.on('close', has_error => {
393
+ // console.log(port, new Date(), 'controller 关闭连接,是否出错:', has_error)
394
+ // })
395
+ // controller.on('timeout', () => {
396
+ // console.log(port, new Date(), 'controller 超时')
397
+ // })
398
+ // controller.on('error', error => {
399
+ // console.log(port, new Date(), 'controller 出错了:', error)
400
+ // client.end()
401
+ // })
402
+ // // --- client 错误处理
403
+ // client.on('error', error => {
404
+ // console.log(port, new Date(), 'client 出错,关闭与 controller 的连接', error)
405
+ // controller.end()
406
+ // })
407
+ // client.on('close', has_error => {
408
+ // this.clients.delete(client)
409
+ // console.log(port, new Date(), 'client 关闭连接,是否出错:', has_error)
410
+ // })
411
+ // // ---
412
+ // console.log(port, new Date(), 'gateway 开始连接到 controller')
413
+ // const { hostname, port: master_port } = this.controllers[this.master]
414
+ // await new Promise<void>(resolve => {
415
+ // // callback 会被 socket.once('connect', callback)
416
+ // controller.connect({
417
+ // port: master_port,
418
+ // host: hostname,
419
+ // // host: '127.0.0.1',
420
+ // family: 0,
421
+ // }, resolve)
422
+ // })
423
+ // console.log(port, new Date(), 'gateway 已连接到 controller')
424
+ // // ---
425
+ // console.log(port, new Date(), 'gateway 建立 client <-> controller')
426
+ // client.pipe(controller)
427
+ // controller.pipe(client)
428
+ // },
429
+ // /** https://www.dolphindb.cn/cn/help/FunctionsandCommands/FunctionReferences/g/getActiveMaster.html?highlight=getactivemaster */
430
+ // async get_active_master () {
431
+ // for (const alias in this.controllers) {
432
+ // const { hostname, port } = this.controllers[alias] as { hostname: string, port: number }
433
+ // const sid = this.sids.get(alias)
434
+ // try {
435
+ // const master = (
436
+ // await ddb.rpc_json('getActiveMaster()', { hostname, port, sid })
437
+ // ).value as string
438
+ // console.log(new Date(), `${alias}.getActiveMaster() -> ${this.master}`)
439
+ // this.sids.set(alias, sid)
440
+ // if (!master)
441
+ // throw new Error(`${new Date().toString()} getActiveMaster 得到的 master 为空`)
442
+ // return master
443
+ // } catch (error) {
444
+ // console.log(new Date(), error)
445
+ // }
446
+ // }
447
+ // return this.master
448
+ // },
449
+ // async auto_switch_master () {
450
+ // if (this.state === 'stopping') {
451
+ // this.state = 'running'
452
+ // console.log('gateway 恢复 auto_switch_master')
453
+ // return
454
+ // }
455
+ // this.state = 'running'
456
+ // for (; this.state === 'running'; ) {
457
+ // const master = this.master
458
+ // const master_ = await this.get_active_master()
459
+ // if (master === master_) {
460
+ // await delay(10 * 1000)
461
+ // continue
462
+ // }
463
+ // this.master = master_
464
+ // console.log(new Date(), `master 从 ${master} 切换为 ${master_}, 断开所有 clients`)
465
+ // for (const client of this.clients)
466
+ // client.end()
467
+ // await delay(3 * 1000)
468
+ // }
469
+ // console.log(new Date(), `gateway.state === ${this.state}, 停止 auto_switch_master`)
470
+ // this.state = 'stopped'
471
+ // }
472
+ // }
473
+ //# sourceMappingURL=unused.js.map
package/unused.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unused.js","sourceRoot":"","sources":["unused.ts"],"names":[],"mappings":"AAAA,iBAAiB;AACjB,kBAAkB;;AAElB,sBAAsB;AACtB,oBAAoB;AAEpB,8BAA8B;AAE9B,kCAAkC;AAGlC,+BAA+B;AAC/B,iBAAiB;AACjB,oBAAoB;AACpB,2DAA2D;AAC3D,SAAS;AACT,uBAAuB;AACvB,wBAAwB;AACxB,YAAY;AACZ,sCAAsC;AACtC,yCAAyC;AAEzC,gCAAgC;AAChC,2BAA2B;AAC3B,8BAA8B;AAE9B,sBAAsB;AACtB,sCAAsC;AACtC,eAAe;AACf,gCAAgC;AAChC,4BAA4B;AAC5B,4BAA4B;AAC5B,2BAA2B;A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BAAgB;AAEhB,iCAAiC;AACjC,aAAa;AAGb,wCAAwC;AACxC,+CAA+C;AAC/C,yCAAyC;AACzC,+DAA+D;AAC/D,yBAAyB;AACzB,gBAAgB;AAEhB,qCAAqC;AAErC,qDAAqD;AACrD,6CAA6C;AAC7C,iEAAiE;AACjE,4CAA4C;AAC5C,6CAA6C;AAC7C,+BAA+B;AAC/B,oBAAoB;AAEpB,wCAAwC;AACxC,6FAA6F;AAC7F,qDAAqD;AACrD,mCAAmC;AACnC,wCAAwC;AACxC,gBAAgB;AAEhB,gGAAgG;AAChG,qCAAqC;AACrC,QAAQ;AACR,IAAI","sourcesContent":["// class DdbObj {\n// port = 8848\n \n// /** ddb 连接状态 */\n// state = '未连接'\n \n// socket = null as Socket\n \n// clients = new Set<Duplex>()\n \n \n// /** dolphindb http rpc \n// - code\n// - options\n// - session_id: 20 分钟内无操作服务端会清空 session,删除所有变量\n// */\n// async rpc_json (\n// code: string,\n// {\n// hostname = '127.0.0.1',\n// // hostname = 'localhost',\n \n// port = this.port,\n// tls = false,\n// sid = this.sid,\n \n// // TEST\n// proxy = MyProxy.whistle\n// }: {\n// hostname?: string\n// port?: number\n// tls?: boolean\n// sid?: string\n// proxy?: MyProxy | false\n// } = { }\n// ): Promise<DdbJsonObj> {\n// let result = await request_json(`${tls ? 'https' : 'http'}://${hostname}:${port}/${sid}`, {\n// body: {\n// sessionID: sid,\n// functionName: 'executeCode',\n// params: [{\n// name: 'script',\n// form: 'scalar',\n// type: 'string',\n// value: encodeURIComponent(code)\n// }]\n// },\n// proxy\n// })\n \n// this.sid = result.sessionID\n \n// result.resultCode = Number(result.resultCode)\n \n// // result.userId: 未登录时为 `''`\n \n// if (result.resultCode)\n// throw Object.assign(\n// // result.msg: 成功时为 `''`; 失败时为错误信息\n// new Error((result.msg as string).red),\n// result\n// )\n \n \n// return new DdbJsonObj(result.object[0])\n// }\n \n \n// /** 创建 tcp 连接到 ddb,并完成 connect 请求,读到 chunk 后执行 on_chunk 回调\n// 成功完成后 this.socket, this.sid 会被初始化\n// */\n// async connect_socket () {\n// // --- 创建 socket, attach 状态、异常事件处理\n// this.socket = new Socket({\n// allowHalfOpen: false,\n// readable: true,\n// writable: true\n// })\n \n// this.socket.on('lookup', (error, address, family, host) => {\n// console.log('socket 域名解析 (lookup) 完成', error, address, family, host)\n// })\n \n// this.socket.on('ready', () => {\n// console.log('socket 准备就绪')\n// })\n \n// this.socket.on('close', has_error => {\n// console.log('socket 成功关闭,是否出错?', has_error)\n// })\n \n// this.socket.on('timeout', () => {\n// console.log('socket 超时')\n// })\n \n// this.socket.on('error', error => {\n// console.log('socket 出错了', error)\n// })\n \n \n// // ---\n// console.log('将 socket 连接到 server')\n// await new Promise<void>(resolve => {\n// // callback 会被 socket.once('connect', callback)\n// this.socket.connect({\n// port: this.port,\n \n// // host: 'localhost',\n// host: '127.0.0.1',\n \n// family: 0,\n// }, resolve)\n// })\n \n// console.log(this.state = 'socket 已连接')\n \n \n// // ---\n// console.log('发送 connect 请求到 ddb')\n \n// this.socket.write(\n// `API ${this.sid || '0'} 8\\n` +\n// 'connect\\n'\n// )\n \n// console.log(this.state = '已发送 connect 请求')\n \n \n// // --- 读取流数据,只等到 connect 完成,后续 chunk 交给 callback\n// await new Promise<void>(async (resolve, reject) => {\n// let last_chunk: Buffer\n \n// try {\n// for await (const chunk of this.socket) {\n// last_chunk = chunk\n// console.log('ddb.socket 读取到 chunk, 转发给所有 websocket clients')\n \n// const ck = chunk as Uint8Array\n \n// if (this.state === '已发送 connect 请求') {\n// // --- 读取 sid\n// const i_sid_end = ck.indexOf(0x20)\n \n// this.sid = this.dec.decode(\n// ck.subarray(\n// 0,\n// i_sid_end\n// )\n// )\n// console.log('sid = ', this.sid)\n \n// console.log(this.state = 'connect 完成')\n \n// resolve()\n \n// continue\n// }\n \n// this.on_chunk(ck)\n// }\n \n// console.log('ddb.socket 完美结束 (no more data to read)')\n// } catch (error) {\n// console.log('捕获到 socket 错误', error)\n// console.log('最后一个 chunk', last_chunk)\n// reject(error)\n// }\n// })\n// }\n \n \n// on_chunk (chunk: Uint8Array) {\n// for (let client of this.clients)\n// client.write(chunk)\n// }\n// }\n\n\n// class DdbJsonObj {\n// static ddb_null = { name: '', form: 'scalar', type: 'void', value: null } as const\n \n// static table_config: TableUserConfig = {\n// border: getBorderCharacters('void'),\n// singleLine: true,\n// columnDefault: {\n// paddingLeft: 0,\n// paddingRight: 1,\n// alignment: 'right'\n// },\n// }\n \n \n// name: string\n \n// form: DdbJsonForm\n \n// type?: DdbJsonType\n \n// /** 向量有这个属性 */\n// size?: number\n \n// value: null | boolean | number | string | DdbJsonObj | DdbJsonObj[]\n \n// constructor (data?: {\n// name: string\n// form: DdbJsonForm\n// type?: DdbJsonType\n// size?: string | number\n// value: any\n// }) {\n// const { name, form, type, size, value } = data || DdbJsonObj.ddb_null as DdbJsonObj\n \n// this.name = name\n// this.form = form\n// this.type = type\n \n// if ((typeof size === 'string' && size) || typeof size === 'number')\n// this.size = Number(size)\n \n// this.value = (() => {\n// if (form === 'scalar') {\n// if (type === 'void')\n// return null\n \n// if (type === 'int' || type === 'short' || type === 'float' || type === 'double') {\n// if (value === 'pi' || value === 'e')\n// return value\n \n// return Number(value)\n// }\n \n// if (type === 'long')\n// return BigInt(value)\n \n// if (type === 'bool')\n// return Boolean(value)\n \n// // integral: int128\n// // literal: char | string | uuid\n// // temporal: date | month | time | second | datetime | timestamp | nanotime | nanotimestamp | datehour\n// // system: code | ipaddr | duration\n// // complex | point\n// return value\n// }\n \n// if (form === 'pair')\n// return value\n \n// if (form === 'vector')\n// return value\n \n// if (form === 'set')\n// return value\n \n// if (form === 'matrix')\n// return value\n \n// if (form === 'dictionary')\n// return value\n \n// if (form === 'table')\n// return value\n// })()\n// }\n \n \n// [inspect.custom] () {\n// const { form, type, value, name, size } = this\n \n// if (form === 'scalar')\n// return this.format_scalar(type, value)\n \n// if (form === 'pair') {\n// const [l, r] = value as [any, any]\n// return `${ l === null ? '' : this.format_scalar(type, l) }:${ r === null ? '' : this.format_scalar(type, r) }`\n// }\n \n// if (form === 'vector' || form === 'set') {\n// return (name ? `${name} ` : '') +\n// (\n// (value as any[]).map(x => \n// x && typeof x === 'object' ?\n// inspect(\n// new DdbJsonObj(x), \n// { colors: false }\n// )\n// :\n// this.format_scalar(type, x)\n// ).join(', ') || ' '\n// ).surround(\n// form === 'vector' ? '[' : '{',\n// form === 'vector' ? ']' : '}',\n// )\n// }\n \n// if (form === 'dictionary') {\n// const [keys, values] = value as [any, any]\n// let obj = { } as Record<string, DdbJsonObj | string>\n// const size = Number(keys.size)\n// if (!size) return `dict(${keys.type}, ${values.type})`\n \n// for (let i = 0; i < size; i++) {\n// const key = keys.type === 'string' ? \n// keys.value[i]\n// :\n// this.format_scalar(keys.type, keys.value[i])\n \n// const v = values.value[i]\n// const value = typeof v === 'string' ? \n// v\n// :\n// new DdbJsonObj(v)\n// obj[key] = value\n// }\n \n// return obj\n// }\n \n// if (form === 'matrix') {\n// const nrows = Number(value[1].value)\n// const ncols = Number(value[2].value)\n// const values = value[0].value\n \n// let data = [ ]\n// for (let i = 0; i < nrows; i++)\n// for (let j = 0; j < ncols; j++) {\n// const value = values[nrows * j + i]\n// let row = data[i] || [ ]\n// row[j] = this.format_scalar(type, value)\n// data[i] = row\n// }\n \n// return `matrix <${type}[${ncols}][${nrows}]>\\n` +\n// table(data, DdbJsonObj.table_config)\n// }\n \n// if (form === 'table') {\n// const nrows = Number(size)\n// const ncols = (value as any[]).length\n// const headers = (value as any[]).map(({ name }) => name)\n \n// let data = [ ]\n// for (let i = 0; i < nrows; i++)\n// for (let j = 0; j < ncols; j++) {\n// const col = value[j]\n// const v = col.value[i]\n// let row = data[i] || [ ]\n// row[j] = this.format_scalar(col.type, v)\n// data[i] = row\n// }\n \n// return `table ${ name ? `${name} ` : '' }(${nrows} rows)\\n` +\n// table(\n// [\n// headers,\n// ...data\n// ],\n// DdbJsonObj.table_config,\n// )\n// }\n \n// return this\n// }\n \n \n// format_scalar (type: DdbJsonType, value: any) {\n// // 可以被 js 原生类型表示\n// if (\n// type === 'void' || type === 'bool' ||\n// type === 'int' || type === 'short' || type === 'long' || type === 'float' || type === 'double'\n// )\n// return value\n \n// if (type === 'string')\n// return inspect(value, { colors: false })\n \n// // 可以直接用 <value> 表示\n// if (\n// type === 'code' || type === 'point' || type === 'complex' || \n// type === 'second' || type === 'time' || type === 'nanotime' || type === 'datehour' || type === 'duration'\n// )\n// return value || 'null'\n \n// if (type === 'month') {\n// if (!value)\n// return 'null'\n// return `${value}M`\n// }\n \n// if (type === 'minute') {\n// if (!value)\n// return 'null'\n// return `${value}m`\n// }\n \n// // 用 type('<value>') 表示\n// return `${type}(${(value as string).quote()})`\n// }\n// }\n\n\n// type DdbJsonForm = 'scalar' | 'pair' | 'vector' | 'set' | 'matrix' | 'dictionary' | 'table'\n\n// type DdbJsonType = \n// 'void' | 'bool' | 'char' | \n// 'short' | 'int' | 'long' | \n// 'date' | 'month' | 'time' | 'minute' | 'second' | 'datetime' | 'timestamp' | 'nanotime' | 'nanotimestamp' | \n// 'float' | 'double' | 'symbol' | 'string' | 'uuid' | 'functiondef' | 'handle' | 'code' | \n// 'datasource' | 'resource' | 'any' | 'compress' | 'datehour' | 'ipaddr' | 'int128' | 'blob' | 'complex' | 'point' | 'duration'\n\n\n// type DdbJsonScalar = number | string | any\n\n\n// const ddb_gateway = {\n// server: null as SocketServer,\n \n// clients: new Set<Socket>(),\n \n// port: 8849,\n \n// controllers: {\n// /** alias */\n// c0: {\n// hostname: '127.0.0.1',\n// port: 8850,\n// },\n \n// c1: {\n// hostname: '127.0.0.1',\n// port: 8851,\n// },\n \n// c2: {\n// hostname: '127.0.0.1',\n// port: 8852,\n// }\n// },\n \n// master: '',\n \n// sids: new Map<string, string>(),\n \n// state: 'init',\n \n \n// async start () {\n// for (const alias in this.controllers) {\n// this.master = alias\n// break\n// }\n \n// this.server = create_socket_server(\n// {\n// allowHalfOpen: false,\n// pauseOnConnect: false,\n// },\n// this.accept.bind(this)\n// )\n \n// this.server.on('error', error => {\n// console.log(new Date(), 'gateway 出错:', error)\n// })\n \n// await new Promise<void>(resolve => {\n// this.server.listen(this.port, resolve)\n// })\n \n// this.auto_switch_master()\n \n// console.log(new Date(), 'gateway 成功启动')\n// },\n \n \n// async stop () {\n// this.state = 'stopping'\n// console.log(new Date(), '正在停止 auto_switch_master')\n \n// for (const client of this.clients)\n// client.end()\n \n// await new Promise<void>((resolve, reject) => {\n// this.server.close(error => {\n// if (error) {\n// reject(error)\n// return\n// }\n// resolve()\n// })\n// })\n// },\n \n \n// /** 接收到 client tcp 连接 */\n// async accept (client: Socket) {\n// this.clients.add(client)\n \n// const port = client.remotePort\n \n// // --- \n// console.log(port, new Date(), 'gateway accept client 连接,开始建立管道')\n \n// let controller = new Socket({\n// allowHalfOpen: false,\n// readable: true,\n// writable: true,\n// })\n \n// controller.on('lookup', (error, address, family, host) => {\n// console.log(port, new Date(), 'controller 域名解析 (lookup) 完成', error, address, family, host)\n// })\n \n// controller.on('close', has_error => {\n// console.log(port, new Date(), 'controller 关闭连接,是否出错:', has_error)\n// })\n \n// controller.on('timeout', () => {\n// console.log(port, new Date(), 'controller 超时')\n// })\n \n// controller.on('error', error => {\n// console.log(port, new Date(), 'controller 出错了:', error)\n// client.end()\n// })\n \n \n// // --- client 错误处理\n// client.on('error', error => {\n// console.log(port, new Date(), 'client 出错,关闭与 controller 的连接', error)\n// controller.end()\n// })\n \n// client.on('close', has_error => {\n// this.clients.delete(client)\n// console.log(port, new Date(), 'client 关闭连接,是否出错:', has_error)\n// })\n \n \n// // --- \n// console.log(port, new Date(), 'gateway 开始连接到 controller')\n// const { hostname, port: master_port } = this.controllers[this.master]\n// await new Promise<void>(resolve => {\n// // callback 会被 socket.once('connect', callback)\n// controller.connect({\n// port: master_port,\n \n// host: hostname,\n// // host: '127.0.0.1',\n \n// family: 0,\n// }, resolve)\n// })\n// console.log(port, new Date(), 'gateway 已连接到 controller')\n \n// // --- \n// console.log(port, new Date(), 'gateway 建立 client <-> controller')\n// client.pipe(controller)\n// controller.pipe(client)\n// },\n \n \n// /** https://www.dolphindb.cn/cn/help/FunctionsandCommands/FunctionReferences/g/getActiveMaster.html?highlight=getactivemaster */\n// async get_active_master () {\n// for (const alias in this.controllers) {\n// const { hostname, port } = this.controllers[alias] as { hostname: string, port: number }\n// const sid = this.sids.get(alias)\n// try {\n// const master = (\n// await ddb.rpc_json('getActiveMaster()', { hostname, port, sid })\n// ).value as string\n// console.log(new Date(), `${alias}.getActiveMaster() -> ${this.master}`)\n// this.sids.set(alias, sid)\n// if (!master)\n// throw new Error(`${new Date().toString()} getActiveMaster 得到的 master 为空`)\n// return master\n// } catch (error) {\n// console.log(new Date(), error)\n// }\n// }\n \n// return this.master\n// },\n \n \n// async auto_switch_master () {\n// if (this.state === 'stopping') {\n// this.state = 'running'\n// console.log('gateway 恢复 auto_switch_master')\n// return\n// }\n \n// this.state = 'running'\n \n// for (; this.state === 'running'; ) {\n// const master = this.master\n// const master_ = await this.get_active_master()\n// if (master === master_) {\n// await delay(10 * 1000)\n// continue\n// }\n \n// this.master = master_\n// console.log(new Date(), `master 从 ${master} 切换为 ${master_}, 断开所有 clients`)\n// for (const client of this.clients)\n// client.end()\n// await delay(3 * 1000)\n// }\n \n// console.log(new Date(), `gateway.state === ${this.state}, 停止 auto_switch_master`)\n// this.state = 'stopped'\n// }\n// }\n"]}