aboutsummaryrefslogtreecommitdiff
path: root/src/worker.ts
blob: d535988362616a93190988c1c9fbc75c25dd8803 (plain) (blame)
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
import { Router, error } from "itty-router";

const router = Router();
// const cache: { [key: string]: { blob: Blob; ts: number } } = {};

const shinobu = async (): Promise<Blob> => {
  // if (cache["shinobu"] && Date.now() - cache["shinobu"].ts < 1000) {
  //   return cache["shinobu"].blob;
  // }

  const blob = await (
    await fetch(
      (
        (await (await fetch(`https://api.waifu.pics/sfw/shinobu`)).json()) as {
          url: string;
        }
      ).url
    )
  ).blob();

  // cache["shinobu"] = {
  //   blob,
  //   ts: Date.now(),
  // };

  return blob;
};

router
  .get(
    "/",
    () =>
      new Response(`/\n
  shinobu`)
  )
  .get("/shinobu", async (_request) => {
    let response = new Response(await shinobu());

    response.headers.set("Cache-Control", "public, max-age=1, s-maxage=1");

    return response;
  })
  .all("*", () => error(404));

export default {
  fetch: (request: Request) => router.handle(request).catch(error),
};