File
Blob: src/workerd/api/tests/stw-jsrpc-dataclone-tail.js
| 1 | // Copyright (c) 2026 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import * as assert from 'node:assert'; |
| 5 | |
| 6 | let jsrpcResult; |
| 7 | let fetchReturnResult; |
| 8 | let queueOnsetResult; |
| 9 | |
| 10 | async function captureInfo(info, env) { |
| 11 | const result = { |
| 12 | type: info.type, |
| 13 | protoIsObjectPrototype: Object.getPrototypeOf(info) === Object.prototype, |
| 14 | }; |
| 15 | |
| 16 | if ('statusCode' in info) { |
| 17 | result.statusCode = info.statusCode; |
| 18 | } |
| 19 | if ('batchSize' in info) { |
| 20 | result.batchSize = info.batchSize; |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | await env.RECEIVER.capture(info); |
| 25 | result.rpcOk = true; |
| 26 | } catch (err) { |
| 27 | result.rpcOk = false; |
| 28 | result.errorName = err?.name; |
| 29 | result.errorMessage = err?.message; |
| 30 | } |
| 31 | |
| 32 | return result; |
| 33 | } |
| 34 | |
| 35 | export default { |
| 36 | async tailStream(onsetEvent, env) { |
| 37 | const onsetInfo = onsetEvent.event.info; |
| 38 | |
| 39 | if (onsetInfo?.type === 'jsrpc' && jsrpcResult === undefined) { |
| 40 | jsrpcResult = await captureInfo(onsetInfo, env); |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | if (onsetInfo?.type === 'queue' && queueOnsetResult === undefined) { |
| 45 | queueOnsetResult = await captureInfo(onsetInfo, env); |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | return async (event) => { |
| 50 | const info = event.event.info; |
| 51 | if ( |
| 52 | event.event.type === 'return' && |
| 53 | info?.type === 'fetch' && |
| 54 | fetchReturnResult === undefined |
| 55 | ) { |
| 56 | fetchReturnResult = await captureInfo(info, env); |
| 57 | } |
| 58 | }; |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | export const test = { |
| 63 | async test(ctrl, env) { |
| 64 | await env.RECEIVER.reset(); |
| 65 | jsrpcResult = undefined; |
| 66 | fetchReturnResult = undefined; |
| 67 | queueOnsetResult = undefined; |
| 68 | |
| 69 | const response = await env.CALLEE.fetch('http://callee/'); |
| 70 | assert.strictEqual(response.status, 201); |
| 71 | assert.strictEqual(await response.text(), 'ok'); |
| 72 | |
| 73 | const timestamp = new Date(); |
| 74 | const queueResult = await env.CALLEE.queue('stw-jsrpc-dataclone-test', [ |
| 75 | { id: '#0', timestamp, body: 'hello', attempts: 1 }, |
| 76 | ]); |
| 77 | assert.strictEqual(queueResult.outcome, 'ok'); |
| 78 | |
| 79 | assert.strictEqual(await env.CALLEE_RPC.ping(), 'ok'); |
| 80 | await scheduler.wait(100); |
| 81 | |
| 82 | assert.ok(fetchReturnResult, 'missing fetch return info result'); |
| 83 | assert.strictEqual(fetchReturnResult.type, 'fetch'); |
| 84 | assert.strictEqual(fetchReturnResult.statusCode, 201); |
| 85 | assert.strictEqual(fetchReturnResult.protoIsObjectPrototype, true); |
| 86 | assert.strictEqual(fetchReturnResult.rpcOk, true); |
| 87 | |
| 88 | assert.ok(queueOnsetResult, 'missing queue onset.info result'); |
| 89 | assert.strictEqual(queueOnsetResult.type, 'queue'); |
| 90 | assert.strictEqual(queueOnsetResult.batchSize, 1); |
| 91 | assert.strictEqual(queueOnsetResult.protoIsObjectPrototype, true); |
| 92 | assert.strictEqual(queueOnsetResult.rpcOk, true); |
| 93 | |
| 94 | assert.ok(jsrpcResult, 'missing jsrpc onset.info result'); |
| 95 | assert.strictEqual(jsrpcResult.type, 'jsrpc'); |
| 96 | assert.strictEqual( |
| 97 | jsrpcResult.protoIsObjectPrototype, |
| 98 | true, |
| 99 | 'jsrpc onset.info should be a plain object' |
| 100 | ); |
| 101 | assert.strictEqual(jsrpcResult.rpcOk, true); |
| 102 | }, |
| 103 | }; |