Public
Script
Readme

This val default exports a function returning ValInfo.

The val information comes from the Deno stack trace induced by throw new Error().

type ValInfo = {
  stack: string[]; // mostly for debugging
  endpoint: string; // val endpoint URL, empty if it is not an HTTP val
  user: string; // val.town user name
  name: string; // val name
};

Here is an example program:

import thisval from "https://esm.town/v/begoon/thisval";
const val = thisval();
export default async function(req: Request): Promise<Response> {
  return Response.json(val);
}

when invoked, it returns:

{
  "stack": [
    "Error",
    "    at info (https://esm.town/v/begoon/thisval?v=12:10:11)",
    "    at https://esm.town/v/begoon/thisvaltest?v=2:3:13"
  ],
  "endpoint": "begoon-thisvaltest.web.val.run",
  "user": "begoon",
  "name": "thisvaltest"
}
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
export type ValInfo = {
stack: string[];
endpoint: string;
user: string;
name: string;
};
function info(): ValInfo {
try {
throw new Error();
} catch (e) {
console.error(e);
const stack = e.stack.split("\n");
const [kind, user, name] = stack
.filter(v => v.includes("esm.town"))
.at(-1)
.split("esm.town/")
.at(1)
?.split("?")
.at(0)
?.split("/");
const endpoint = kind == "v" ? `${user}-${name}.web.val.run` : "";
return { stack, endpoint, user, name };
}
}
export default info;
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!
September 11, 2024