-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (60 loc) · 1.83 KB
/
Copy pathindex.ts
File metadata and controls
65 lines (60 loc) · 1.83 KB
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
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Bind resources to your worker in `wrangler.toml`. After adding bindings, a type definition for the
* `Env` object can be regenerated with `npm run cf-typegen`.
*
* Learn more at https://developers.cloudflare.com/workers/
*/
import "./wasm_exec.js";
// @ts-expect-error
import module from "./readability.wasm";
// @ts-expect-error
const go = new Go();
WebAssembly.instantiate(module, go.importObject)
.then((instance) => {
go.run(instance);
})
.catch((err) => {
console.error(err);
});
declare global {
function readabilityTextContent(
html: string,
url: string,
):
| { readonly error: string; readonly content: undefined }
| { readonly error: undefined; readonly content: string };
}
export default {
async fetch(request, env, ctx): Promise<Response> {
const url = parseUrl(new URL(request.url).searchParams.get("url") ?? "");
if (!url) {
return new Response("Invalid URL", { status: 400 });
}
const res = await fetch(url);
if (res.status < 200 || res.status >= 300) {
return new Response(`Failed to fetch ${url}`, { status: 500 });
}
const result = readabilityTextContent(await res.text(), url.toString());
console.log(result);
if (result.error) {
return new Response(result.error, { status: 500 });
}
return new Response(result.content, {
status: 200,
headers: { "content-type": "text/plain; charset=utf-8" },
});
},
} satisfies ExportedHandler<Env>;
function parseUrl(url: string): URL | null {
try {
return new URL(url);
} catch (e) {
return null;
}
}