Avatar

@healeycodes

17 public vals
Joined February 2, 2023
# hm
Create valhm <! -- a -->
Readme
1
2
export const randomString = (length: number) =>
[...Array(length)].map(() => Math.random().toString(36)[2]).join("");
1
2
3
4
5
6
7
8
9
10
11
12
export let bskyEmptyMessagesBug = (async () => {
const ws = new WebSocketStream(
"wss://bsky.social/xrpc/com.atproto.sync.subscribeRepos",
);
const { readable } = await ws.connection;
const reader = readable.getReader();
for (let i = 0; i < 200; i++) {
console.log(i);
const { value, done } = await reader.read();
console.log({ value, done });
}
})();
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Initially forked from @lukas.sampleFirehose
export let firehoseReadMessagesExample = (async () => {
const cborx = await import("https://deno.land/x/cbor@v1.5.2/index.js");
const multiformats = await import("npm:multiformats");
const uint8arrays = await import("npm:uint8arrays");
const { z } = await import("https://deno.land/x/zod@v3.21.4/mod.ts");
const xrpc = await import("npm:@atproto/xrpc");
const cborCodec = await import(
"https://cdn.jsdelivr.net/npm/@ipld/dag-cbor/+esm"
);
const cborEncode = cborCodec.encode;
enum FrameType {
Message = 1,
Error = -1,
}
const messageFrameHeader = z.object({
op: z.literal(FrameType.Message),
t: z.string().optional(), // Message body type discriminator
});
type MessageFrameHeader = z.infer<typeof messageFrameHeader>;
const errorFrameHeader = z.object({
op: z.literal(FrameType.Error),
});
const errorFrameBody = z.object({
error: z.string(),
message: z.string().optional(), // Error message
});
type ErrorFrameHeader = z.infer<typeof errorFrameHeader>;
type ErrorFrameBody<T extends string = string> = {
error: T;
} & z.infer<typeof errorFrameBody>;
const frameHeader = z.union([messageFrameHeader, errorFrameHeader]);
type FrameHeader = z.infer<typeof frameHeader>;
abstract class Frame {
header: FrameHeader;
body: unknown;
get op(): FrameType {
return this.header.op;
}
toBytes(): Uint8Array {
return uint8arrays.concat([
cborEncode(this.header),
cborEncode(this.body),
]);
}
isMessage(): this is MessageFrame<unknown> {
return this.op === FrameType.Message;
}
isError(): this is ErrorFrame {
return this.op === FrameType.Error;
}
static fromBytes(bytes: Uint8Array) {
const decoded = cborDecodeMulti(bytes);
if (decoded.length > 2) {
throw new Error("Too many CBOR data items in frame");
}
const header = decoded[0];
let body: unknown = kUnset;
if (decoded.length > 1) {
body = decoded[1];
}
const parsedHeader = frameHeader.safeParse(header);
if (!parsedHeader.success) {
throw new Error(`Invalid frame header: ${parsedHeader.error.message}`);
}
if (body === kUnset) {
throw new Error("Missing frame body");
}
const frameOp = parsedHeader.data.op;
if (frameOp === FrameType.Message) {
return new MessageFrame(body, {
type: parsedHeader.data.t,
});
}
else if (frameOp === FrameType.Error) {
const parsedBody = errorFrameBody.safeParse(body);
if (!parsedBody.success) {
throw new Error(
`Invalid error frame body: ${parsedBody.error.message}`,
);
}
return new ErrorFrame(parsedBody.data);
}
else {
const exhaustiveCheck: never = frameOp;
throw new Error(`Unknown frame op: ${exhaustiveCheck}`);
}
}
}
class MessageFrame<T = Record<string, unknown>> extends Frame {
header: MessageFrameHeader;
body: T;
constructor(body: T, opts?: {
type?: string;
}) {
super();
this.header = opts?.type !== undefined
? { op: FrameType.Message, t: opts?.type }
: { op: FrameType.Message };
this.body = body;
1
2
3
4
export let apiLog = (req, res) => {
console.log(req, res);
res.send(JSON.stringify({ req, res }));
};
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
let { _hits } = await import("https://esm.town/v/healeycodes/_hits");
// Tiny web page with a hit counter
export let websiteHitCounter = (
req: express.Request,
res: express.Response
) => {
// track hits
if (_hits === undefined) {
_hits = 1;
} else {
_hits++;
}
// return some html
res.send(`
<html>
<body>
<h1>hit counter :)</h1>
<p>this website has received ${_hits} hits! try refreshing!</p>
<p>see the source code: <a href="https://www.val.town/v/healeycodes.websiteHitCounter">@healeycodes.websiteHitCounter</a></p>
<small>nice.</small>
</body>
</html>
`);
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { fetch } from "https://esm.town/v/std/fetch";
export const valEval = async function (
expr: string | TemplateStringsArray
): Promise<[value: any, error: undefined | string]> {
const encoded = encodeURIComponent(expr.toString());
try {
const res = await fetch(`https://api.val.town/eval/${encoded}`);
return (await res.json()).data;
} catch (e) {
console.log(e.toString());
throw e;
}
};
1
2
3
4
// set by healeycodes.untitled_q2nnNUYi at 2023-04-20T13:58:41.058Z
export let hnTopStoryLatest = {
"foo": "bar"
};
1
2
3
4
export const htmlHello = (req: express.Request, res: express.Response) => {
console.log(req, res);
return res.send(decodeURIComponent(req["options"]["headers"]["cookie"]));
};
1
2
3
4
5
6
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON?v=41";
// Activity suggestions for when you're bored
export let boredActivities = fetchJSON(
"https://www.boredapi.com/api/activity"
);
1
export const test = "test :)";