Skip to content
File

Blob: samples/nodejs-compat-diagnosticschannel/worker.js

javascript48 lines
1import {
2 subscribe,
3 channel,
4 tracingChannel,
5 Channel,
6} from "node:diagnostics_channel";
7import { doSomething } from "library";
8 
9import { AsyncLocalStorage } from "node:async_hooks";
10 
11// The library will send a diagnostic message when the doSomething
12// method is called. We listen for that event here. When using
13// Tail Workers, the event will also be included in the tail events.
14subscribe('test', (message) => {
15 console.log('Diagnostic message received:', message);
16});
17 
18const als1 = new AsyncLocalStorage();
19const als2 = new AsyncLocalStorage();
20const c = channel('bar');
21c.bindStore(als1);
22c.bindStore(als2, (data) => {return { data }});
23 
24const tc = tracingChannel('foo');
25tc.start.bindStore(als1);
26tc.start.subscribe(() => console.log('start', als1.getStore()));
27tc.end.subscribe(() => console.log('end'));
28tc.asyncStart.subscribe(() => console.log('async start'));
29tc.asyncEnd.subscribe(() => console.log('async end'));
30 
31export default {
32 async fetch(request) {
33 doSomething();
34 
35 // prettier-ignore
36 console.log(c.runStores(1, (...args) => {
37 console.log(this, ...args, als1.getStore(), als2.getStore());
38 return 1;
39 }, {}, 1, 2, 3));
40 
41 await tc.tracePromise(async function() {
42 console.log('....', this);
43 }, {a:1});
44 
45 return new Response("ok");
46 }
47};