js-rpc2 1.2.0 → 1.2.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-rpc2",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "js web websocket http rpc",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -31,4 +31,4 @@
31
31
  "koa-router": "^13.0.1",
32
32
  "ws": "^8.18.0"
33
33
  }
34
- }
34
+ }
package/src/lib.js CHANGED
@@ -554,6 +554,8 @@ export async function rpcRunServerDecodeBuffer(extension, writer, buffer, logger
554
554
  let params = []
555
555
  let time = Date.now()
556
556
  try {
557
+ let store = extension.asyncLocalStorage
558
+ store.enterWith(context)
557
559
  let o = parseRpcData(buffer)
558
560
  dataId = o.id
559
561
  let items = parseRpcItemData(o.data)
@@ -576,17 +578,7 @@ export async function rpcRunServerDecodeBuffer(extension, writer, buffer, logger
576
578
  params.push(p.data)
577
579
  }
578
580
  }
579
- let store = extension.asyncLocalStorage
580
- let ret = await new Promise((resolve, reject) => {
581
- store.run(context, async () => {
582
- try {
583
- let ret = await extension[fnName](...params)
584
- resolve(ret)
585
- } catch (error) {
586
- reject(error)
587
- }
588
- })
589
- })
581
+ let ret = await extension[fnName](...params)
590
582
  if (Array.isArray(ret)) {
591
583
  box = { id: o.id, type: RPC_TYPE_RETURN_ARRAY, data: buildRpcItemData(ret), }
592
584
  } else {
package/src/lib.test.js CHANGED
@@ -286,9 +286,11 @@ test('测试RPC调用-KoaRouter', async () => {
286
286
  test('测试RPC调用-KoaRouter-AsyncLocalStorage', async () => {
287
287
  // node --test-name-pattern="^测试RPC调用-KoaRouter-AsyncLocalStorage$" src/lib.test.js
288
288
  const extension = {
289
+ /** @type{AsyncLocalStorage<Koa.ParameterizedContext<Koa.DefaultState, Koa.DefaultContext, any>>} */
289
290
  asyncLocalStorage: new AsyncLocalStorage(),
290
291
  hello: async function (/** @type {string} */ name) {
291
- console.info('asyncLocalStorage.getStore', this.asyncLocalStorage.getStore())
292
+ let ctx = this.asyncLocalStorage.getStore()
293
+ strictEqual(ctx.path, '/abc')
292
294
  return `hello ${name}`
293
295
  },
294
296
  }
@@ -327,6 +329,7 @@ test('测试RPC调用-KoaRouter-AsyncLocalStorage', async () => {
327
329
  let string = await client.hello('asdfghjkl')
328
330
  console.info(string)
329
331
  strictEqual(string, 'hello asdfghjkl')
332
+ strictEqual(extension.asyncLocalStorage.getStore(), undefined)
330
333
  })
331
334
  console.info('over!')
332
335
  })
@@ -408,6 +411,30 @@ test('async-local-storage', async () => {
408
411
  await Promise.all([p1.promise, p2.promise])
409
412
  })
410
413
 
414
+ test('async-local-storage-enterWith', async () => {
415
+ // node --test-name-pattern="^async-local-storage-enterWith$" src/lib.test.js
416
+ let store = new AsyncLocalStorage()
417
+ let p1 = Promise.withResolvers()
418
+ strictEqual(store.getStore(), undefined)
419
+ store.enterWith('v1')
420
+ strictEqual(store.getStore(), 'v1')
421
+ store.run('v2', async () => {
422
+ strictEqual(store.getStore(), 'v2')
423
+ let p2 = Promise.withResolvers()
424
+ setTimeout(() => {
425
+ strictEqual(store.getStore(), 'v2')
426
+ store.enterWith('v3')
427
+ strictEqual(store.getStore(), 'v3')
428
+ p2.resolve()
429
+ })
430
+ await p2.promise
431
+ strictEqual(store.getStore(), 'v2')
432
+ p1.resolve()
433
+ })
434
+ await p1.promise
435
+ strictEqual(store.getStore(), 'v1')
436
+ })
437
+
411
438
  test('async-local-storage-stream', async () => {
412
439
  // node --test-name-pattern="^async-local-storage-stream$" src/lib.test.js
413
440
  let store = new AsyncLocalStorage()