aboutsummaryrefslogtreecommitdiff
path: root/source/main.js
blob: 1eb55d3a11e6a2f1da0f62b37f168e4c3c7830a3 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import puppeteer from "@cloudflare/puppeteer";

export default {
  /*
    Fetch event handler, this function will be called whenever a request is made to the worker.
    The function will parse the request and return a response based on the request path.

    @param {Request} request - the incoming request object
    @param {Environment} environment - the environment object
    @param {Context} context - the context object

    @returns {Response} a new Response object
  */
  async fetch(request, environment, context) {
    const { pathname } = new URL(request.url);

    switch (pathname) {
      case "/":
        return (() => {
          return new Response(
            JSON.stringify({
              message: "Hello, World! :3",
            }),
            {
              headers: {
                "Access-Control-Allow-Origin": "*",
                "Content-Type": "application/json",
              },
            }
          );
        })();
      case "/status":
        return (() => {
          const url = new URL(request.url);
          const service = url.searchParams.get("service");

          const acceptedServices = [
            "dns",
            "load-balancer",
            "cdn",
            "functions",
            "mysql-cluster",
            "mongodb-cluster",
            "redis-cluster",
            "elasticsearch-cluster",
            "git-connector",
            "job-runners",
            "container-registry",
            "kubernetes-cluster",
            "bare-metal-servers",
            "game-server-api",
            "anti-ddos-protection",
            "anti-cheat-api",
          ];

          if (acceptedServices.includes(service))
            return new Response(
              JSON.stringify({
                status: "ok",
                service,
              }),
              {
                headers: {
                  "Access-Control-Allow-Origin": "*",
                  "Content-Type": "application/json",
                },
              }
            );

          return new Response(
            JSON.stringify({
              error: `service not found`,
            }),
            {
              headers: {
                "Access-Control-Allow-Origin": "*",
                "Content-Type": "application/json",
              },
            }
          );
        })();
      case "/96/twitch/streaming":
        return (async () => {
          const url = new URL(request.url);
          const channel = url.searchParams.get("channel");

          if (!channel)
            return new Response(
              JSON.stringify({
                error: `channel not provided`,
              }),
              {
                headers: {
                  "Access-Control-Allow-Origin": "*",
                  "Content-Type": "application/json",
                },
              }
            );

          const channelWhitelist = ["@vtsweets"];

          if (!channelWhitelist.includes(channel))
            return new Response(
              JSON.stringify({
                error: `channel not allowed`,
              }),
              {
                headers: {
                  "Access-Control-Allow-Origin": "*",
                  "Content-Type": "application/json",
                },
              }
            );

          return new Response(
            JSON.stringify({
              latestVideoId,
            }),
            {
              headers: {
                "Access-Control-Allow-Origin": "*",
                "Content-Type": "application/json",
              },
            }
          );
        })();
      case "/browser-rendering/screenshot":
        return (async () => {
          const { searchParams } = new URL(request.url);
          const url = searchParams.get("url");
          const type = searchParams.get("type") || "webp";
          const fullPage = searchParams.get("fullPage") !== null;
          const width = parseInt(searchParams.get("width"), 10) || 1920;
          const height = parseInt(searchParams.get("height"), 10) || 1080;
          const delay = parseInt(searchParams.get("delay"), 10) || 0;

          if (!url)
            return router.respond({
              error: "URL parameter is required",
            });

          if (!/^https?:\/\//.test(url))
            return router.respond({
              error: "Invalid URL format",
            });

          const browser = await puppeteer.launch(environment.MYBROWSER);
          const page = await browser.newPage();
          await page.setViewport({ width, height });
          await page.goto(url, {
            waitUntil: "networkidle2",
          });
          if (delay > 0) {
            await new Promise((resolve) => setTimeout(resolve, delay));
          }
          const screenshot = await page.screenshot({
            type,
            fullPage,
          });
          await browser.close();

          return new Response(screenshot, {
            headers: {
              "content-type": `image/${type}`,
            },
          });
        })();
      default:
        return new Response(
          JSON.stringify({
            error: `path not found`,
          }),
          {
            headers: {
              "Access-Control-Allow-Origin": "*",
              "Content-Type": "application/json",
            },
          }
        );
    }
  },

  /*
    Email event handler, this function will be called whenever an email is sent to the worker.
    The function will parse the email message and forward it to a specified email address.

    @param {Message} message - the incoming email message object
    @param {Environment} environment - the environment object
    @param {Context} context - the context object

    @returns {void}
  */
  async email(message, environment, context) {
    message.forward("alex@zue.dev");
  },

  /*
    Scheduled event handler, this function will be called whenever a scheduled event is triggered.
    The function will perform a task and return a response based on the task outcome.

    @param {Event} event - the incoming event object
    @param {Environment} environment - the environment object
    @param {Context} context - the context object

    @returns {void}
  */
  async scheduled(event, environment, context) {
    console.log("Scheduled event triggered!");
  },
};