-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfurl.js
247 lines (221 loc) · 6.29 KB
/
unfurl.js
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* @template Value
* @typedef {Object} GoodResult
* @property {true} ok - The success status.
* @property {Value} value - The data extracted from the URL.
*/
/**
* @template Error
* @typedef {Object} BadResult
* @property {false} ok - The success status.
* @property {Error} error - The error
*/
/**
* @template Value, Error
* @typedef {GoodResult<Value> | BadResult<Error>} Result
*/
/**
* @typedef {Object} UnfurledData
* @property {string} [title] - The title extracted from the URL.
* @property {string} [description] - The description extracted from the URL.
* @property {string} [image] - The image URL extracted from the URL.
* @property {string} [favicon] - The favicon URL extracted from the URL.
*/
/**
* @typedef {'bad-param' | 'failed-fetch'} UnfurlError
*/
const validContentTypes = [
"text/html",
"application/xhtml+xml",
"application/xml",
"image/*",
];
/**
*
* @param {string} contentType
* @returns {boolean}
*/
function isValidContentType(contentType) {
return (
// allow unspecified, try to parse it anyway
!contentType ||
contentType.startsWith("image/") ||
validContentTypes.some((valid) => contentType.startsWith(valid))
);
}
/**
* Handles the unfurling of a URL by extracting metadata such as title, description, image, and favicon.
* @param {string} url - The URL to unfurl.
* @returns {Promise<Result<UnfurledData, UnfurlError>>} - A promise that resolves to an object containing the extracted metadata, or null if an error occurs.
*/
export async function unfurl(url) {
if (typeof url !== "string" || !url.match(/^https?:\/\//)) {
return { ok: false, error: "bad-param" };
}
// cloudflare has a built-in HTML parser/rewriter called HTMLRewriter. in order to use it, we
// need to define classes that act as event handlers for certain elements, attributes, etc.
// see https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/
const meta$ = new MetaExtractor();
const title$ = new TextExtractor();
const icon$ = new IconExtractor();
try {
const headers = new Headers();
for (const contentType of validContentTypes) {
headers.append("accept", contentType);
}
const res = await fetch(url, { headers });
if (!res.ok || !isValidContentType(res.headers.get("content-type") ?? "")) {
return { ok: false, error: "failed-fetch" };
}
if (res.headers.get("content-type")?.startsWith("image/")) {
return {
ok: true,
value: {
image: url,
title: new URL(url).pathname.split("/").pop() || undefined,
},
};
}
await new HTMLRewriter()
.on("meta", meta$)
.on("title", title$)
.on("link", icon$)
.transform(res)
.blob();
} catch {
return { ok: false, error: "failed-fetch" };
}
// we don't know exactly what we'll end up with, so this is a best-effort extraction
const { og, twitter } = meta$;
const title =
og["og:title"] ?? twitter["twitter:title"] ?? title$.string ?? undefined;
const description =
og["og:description"] ??
twitter["twitter:description"] ??
meta$.description ??
undefined;
let image =
og["og:image:secure_url"] ??
og["og:image"] ??
twitter["twitter:image"] ??
undefined;
let favicon = icon$.appleIcon ?? icon$.icon ?? undefined;
if (image && !image?.startsWith("http")) {
image = new URL(image, url).href;
}
if (favicon && !favicon?.startsWith("http")) {
favicon = new URL(favicon, url).href;
}
return {
ok: true,
value: {
title,
description,
image,
favicon,
},
};
}
/**
* Implements a handler for a GET request where the uri is passed in as a search param called `url`.
*
* e.g. GET /foo/bar?url=https://example.com
*
* @param {Request} request
* @returns {Promise<Response>}
*/
export async function handleUnfurlRequest(request) {
const url = new URL(request.url).searchParams.get("url");
if (!url) {
return new Response("Missing URL query parameter.", { status: 400 });
}
const result = await unfurl(url);
if (result.ok) {
return new Response(JSON.stringify(result.value), {
headers: { "Content-Type": "application/json" },
});
} else if (result.error === "bad-param") {
return new Response("Bad URL query parameter.", { status: 400 });
} else {
return new Response("Failed to fetch URL.", { status: 422 });
}
}
/**
* Extracts text from HTML elements.
*/
class TextExtractor {
/**
* The accumulated text extracted from elements.
* @type {string}
*/
string = "";
/**
* Handles an incoming piece of text.
* @param {Object} param - The text object.
* @param {string} param.text - The incoming text.
*/
text({ text }) {
this.string += text;
}
}
/**
* Extracts metadata from HTML elements.
*/
class MetaExtractor {
/**
* The Open Graph (og) metadata extracted from elements.
* @type {Object.<string, string|null>}
*/
og = {};
/**
* The Twitter metadata extracted from elements.
* @type {Object.<string, string|null>}
*/
twitter = {};
/**
* The description extracted from elements.
* @type {string|null}
*/
description = null;
/**
* Handles an incoming element.
* @param {Element} element - The incoming element.
*/
element(element) {
const property = element.getAttribute("property");
const name = element.getAttribute("name");
if (property && property.startsWith("og:")) {
this.og[property] = element.getAttribute("content");
} else if (name && name.startsWith("twitter:")) {
this.twitter[name] = element.getAttribute("content");
} else if (name === "description") {
this.description = element.getAttribute("content");
}
}
}
/**
* Extracts favicon URLs from HTML elements.
*/
class IconExtractor {
/**
* The Apple touch icon URL extracted from elements.
* @type {string|null}
*/
appleIcon = null;
/**
* The favicon URL extracted from elements.
* @type {string|null}
*/
icon = null;
/**
* Handles an incoming element.
* @param {Element} element - The incoming element.
*/
element(element) {
if (element.getAttribute("rel") === "icon") {
this.icon = element.getAttribute("href");
} else if (element.getAttribute("rel") === "apple-touch-icon") {
this.appleIcon = element.getAttribute("href");
}
}
}