Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { getCaller as getCaller2 } from "https://esm.town/v/easrng/getCaller";
import { dynamicImport } from "https://esm.town/v/easrng/dynamicImport";
export async function getCaller() {
if (arguments[0])
return;
// val callers are now tracked in an AsyncLocalStorage object
let asyncLocalStorage;
// we need to set up a hook to intercept it,
// since the object is only defined in internals.ts
const { AsyncLocalStorage } = await dynamicImport(
"node:async_hooks",
);
const realGetStore = AsyncLocalStorage.prototype.getStore;
AsyncLocalStorage.prototype.getStore = function (...a) {
asyncLocalStorage = this;
return realGetStore.call(this, ...a);
};
// call a val, so the internals will call .getStore() on their copy,
// triggering the hook
// ______________________________________________________________
// | |
// | WARNING: ONCE THE LIMIT OF 100 LOGGED EXECUTIONS / RUN HITS, |
// | THIS WILL FAIL |
// |______________________________________________________________|
//
// i tried caching the asyncLocalStorage,
// but it seemingly gets replaced with null at the end of the function call
(getCaller2 as any)(true);
AsyncLocalStorage.prototype.getStore = realGetStore;
// rn only this val's reference is returned :/
// maybe they can add a stack trace to the asyncLocalStorage object?
return asyncLocalStorage.getStore();
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023