-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
index.ts
488 lines (411 loc) · 13.3 KB
/
index.ts
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import { URL } from "url";
import { Parser } from "htmlparser2";
import fetch from "node-fetch";
import UnexpectedError from "./unexpectedError";
import { schema, keys } from "./schema";
import { Metadata, Opts } from "./types";
import { decode as he_decode } from "he";
import { decode as iconv_decode } from "iconv-lite";
type ParserContext = {
isHtml?: boolean;
isOembed?: boolean;
favicon?: string;
text: string;
title?: string;
tagName?: string;
canonical_url?: string;
};
const defaultHeaders = {
Accept: "text/html, application/xhtml+xml",
"User-Agent": "facebookexternalhit",
};
function unfurl(url: string, opts?: Opts): Promise<Metadata> {
if (opts === undefined) {
opts = {};
}
if (opts.constructor.name !== "Object") {
throw new UnexpectedError(UnexpectedError.BAD_OPTIONS);
}
typeof opts.oembed === "boolean" || (opts.oembed = true);
typeof opts.compress === "boolean" || (opts.compress = true);
typeof opts.headers === "object" || (opts.headers = defaultHeaders);
Number.isInteger(opts.follow) || (opts.follow = 50);
Number.isInteger(opts.timeout) || (opts.timeout = 0);
Number.isInteger(opts.size) || (opts.size = 0);
return getPage(url, opts)
.then(getMetadata(url, opts))
.then(getRemoteMetadata(url))
.then(parse(url));
}
async function getPage(url: string, opts: Opts) {
const res = await (opts.fetch
? opts.fetch(url)
: fetch(new URL(url), {
headers: opts.headers,
size: opts.size,
follow: opts.follow,
timeout: opts.timeout,
}));
const buf = Buffer.from(await res.arrayBuffer());
const contentType = res.headers.get("Content-Type");
const contentLength = res.headers.get("Content-Length");
if (res.status !== 200) {
throw new UnexpectedError({
...UnexpectedError.BAD_HTTP_STATUS,
info: {
url,
httpStatus: res.status,
},
});
}
if (/text\/html|application\/xhtml+xml/.test(contentType) === false) {
throw new UnexpectedError({
...UnexpectedError.EXPECTED_HTML,
info: {
url,
contentType,
contentLength,
},
});
}
// no charset in content type, peek at response body for at most 1024 bytes
const str = buf.slice(0, 1024).toString();
let rg;
if (contentType) {
rg = /charset=([^;]*)/i.exec(contentType);
}
// html 5
if (!rg && str) {
rg = /<meta.+?charset=(['"])(.+?)\1/i.exec(str);
}
// html 4
if (!rg && str) {
rg = /<meta.+?content=["'].+;\s?charset=(.+?)["']/i.exec(str);
}
// found charset
if (rg) {
const supported = [
"CP932",
"CP936",
"CP949",
"CP950",
"GB2312",
"GBK",
"GB18030",
"BIG5",
"SHIFT_JIS",
"EUC-JP",
];
const charset = rg.pop().toUpperCase();
if (supported.includes(charset)) {
return iconv_decode(buf, charset).toString();
}
}
return buf.toString();
}
function getRemoteMetadata(url: string) {
return async function ({ oembed, metadata }) {
if (!oembed) {
return metadata;
}
const target = new URL(he_decode(oembed.href), url);
let res = await fetch(target.href);
let contentType = res.headers.get("Content-Type");
const status = res.status;
if (status === 403 && target.protocol === "http:") {
// try again using HTTPS
target.protocol = "https:";
res = await fetch(target.href);
contentType = res.headers.get("Content-Type");
}
let ret;
if (
oembed.type === "application/json+oembed" &&
/application\/json/.test(contentType)
) {
ret = await res.json();
} else if (
oembed.type === "text/xml+oembed" &&
/(text|application)\/xml/.test(contentType)
) {
const data = (await res.text())
.replace(/>/g, ">")
.replace(/</g, "<");
const content: { [key: string]: string } = {};
const parserContext: ParserContext = { text: "" };
ret = await new Promise((resolve) => {
const parser = new Parser(
{
oncdataend: () => {
if (
!content.html &&
parserContext.text.trim().startsWith("<") &&
parserContext.text.trim().endsWith(">")
) {
content.html = parserContext.text.trim();
}
},
// eslint-disable-next-line
onopentag: function (name: string, attribs: any) {
if (parserContext.isHtml) {
if (!content.html) {
content.html = "";
}
content.html += `<${name} `;
content.html += Object.keys(attribs)
.reduce(
(str, k) =>
str +
(attribs[k] ? `${k}="${attribs[k]}"` : `${k}`) +
" ",
""
)
.trim();
content.html += ">";
}
if (name === "html") {
parserContext.isHtml = true;
}
parserContext.tagName = name;
},
ontext: function (text: string) {
parserContext.text += text;
},
onclosetag: function (tagname: string) {
if (tagname === "oembed") {
return;
}
if (tagname === "html") {
parserContext.isHtml = false;
return;
}
if (parserContext.isHtml) {
content.html += parserContext.text.trim();
content.html += `</${tagname}>`;
}
content[tagname] = parserContext.text.trim();
parserContext.tagName = "";
parserContext.text = "";
},
onend: function () {
resolve(content);
},
},
{
recognizeCDATA: true,
}
);
parser.write(data);
parser.end();
});
}
if (!ret) {
return metadata;
}
const oEmbedMetadata = Object.keys(ret)
.map((k) => ["oEmbed:" + k, ret[k]])
.filter(([k]) => keys.includes(String(k)));
metadata.push(...oEmbedMetadata);
return metadata;
};
}
function getMetadata(url: string, opts: Opts) {
return function (text: string) {
const metadata = [];
const parserContext: ParserContext = { text: "" };
let oembed: { type?: string; href?: string };
let distanceFromRoot = 0;
return new Promise((resolve) => {
const parser = new Parser({
onend: function () {
if (parserContext.favicon === undefined) {
metadata.push(["favicon", new URL("/favicon.ico", url).href]);
} else {
metadata.push([
"favicon",
new URL(parserContext.favicon, url).href,
]);
}
if (parserContext.canonical_url) {
metadata.push([
"canonical_url",
new URL(parserContext.canonical_url, url).href,
]);
}
resolve({ oembed, metadata });
},
onopentagname: function (tag: string) {
parserContext.tagName = tag;
},
ontext: function (text: string) {
if (parserContext.tagName === "title") {
// makes sure we haven't already seen the title
if (parserContext.title !== null) {
if (parserContext.title === undefined) {
parserContext.title = "";
}
parserContext.title += text;
}
}
},
onopentag: function (
tagname: string,
attribs: { [key: string]: string }
) {
distanceFromRoot++;
if (opts.oembed && attribs.href) {
// handle XML and JSON with a preference towards JSON since its more efficient for us
if (
tagname === "link" &&
(attribs.type === "text/xml+oembed" ||
attribs.type === "application/json+oembed")
) {
if (!oembed || oembed.type === "text/xml+oembed") {
// prefer json
oembed = attribs;
}
}
}
if (
tagname === "link" &&
attribs.href &&
(attribs.rel === "icon" || attribs.rel === "shortcut icon")
) {
parserContext.favicon = attribs.href;
}
if (
tagname === "link" &&
attribs.href &&
attribs.rel === "canonical"
) {
parserContext.canonical_url = attribs.href;
}
let pair: [string, string | string[]];
if (tagname === "meta") {
if (attribs.name === "description" && attribs.content) {
pair = ["description", attribs.content];
} else if (attribs.name === "author" && attribs.content) {
pair = ["author", attribs.content];
} else if (attribs.name === "theme-color" && attribs.content) {
pair = ["theme_color", attribs.content];
} else if (attribs.name === "keywords" && attribs.content) {
const keywords = attribs.content
.replace(/^[,\s]{1,}|[,\s]{1,}$/g, "") // gets rid of trailing space or sommas
.split(/,{1,}\s{0,}/); // splits on 1+ commas followed by 0+ spaces
pair = ["keywords", keywords];
} else if (attribs.property && keys.includes(attribs.property)) {
const content = attribs.content || attribs.value;
pair = [attribs.property, content];
} else if (attribs.name && keys.includes(attribs.name)) {
const content = attribs.content || attribs.value;
pair = [attribs.name, content];
}
}
if (pair) {
metadata.push(pair);
}
},
onclosetag: function (tag: string) {
distanceFromRoot--;
parserContext.tagName = "";
if (distanceFromRoot <= 2 && tag === "title") {
metadata.push(["title", parserContext.title]);
parserContext.title = "";
}
// We want to parse as little as possible so finish once we see </head>
// if we have not seen a title tag within the head, we scan the entire
// document instead
if (tag === "head" && parserContext.title) {
parser.reset();
}
},
});
parser.write(text);
parser.end();
});
};
}
function parse(url: string) {
return function (metadata) {
// eslint-disable-next-line
const parsed: any = {};
const ogVideoTags = [];
const articleTags = [];
let lastParent;
for (const meta of metadata) {
const metaKey = meta[0];
let metaValue = meta[1];
const item = schema.get(metaKey);
// decoding html entities
if (typeof metaValue === "string") {
metaValue = he_decode(he_decode(metaValue.toString()));
} else if (Array.isArray(metaValue)) {
metaValue = metaValue.map((val) => he_decode(he_decode(val)));
}
if (!item) {
parsed[metaKey] = metaValue;
continue;
}
// special case for video tags which we want to map to each video object
if (metaKey === "og:video:tag") {
ogVideoTags.push(metaValue);
continue;
}
if (metaKey === "article:tag") {
articleTags.push(metaValue);
continue;
}
if (item.type === "number") {
metaValue = parseInt(metaValue, 10);
} else if (item.type === "url" && metaValue) {
metaValue = new URL(metaValue, url).href;
}
if (parsed[item.entry] === undefined) {
parsed[item.entry] = {};
}
let target = parsed[item.entry];
if (item.parent) {
if (item.category) {
if (!target[item.parent]) {
target[item.parent] = {};
}
if (!target[item.parent][item.category]) {
target[item.parent][item.category] = {};
}
target = target[item.parent][item.category];
} else {
if (Array.isArray(target[item.parent]) === false) {
target[item.parent] = [];
}
if (!target[item.parent][target[item.parent].length - 1]) {
target[item.parent].push({});
} else if (
(!lastParent || item.parent === lastParent) &&
target[item.parent][target[item.parent].length - 1] &&
target[item.parent][target[item.parent].length - 1][item.name]
) {
target[item.parent].push({});
}
lastParent = item.parent;
target = target[item.parent][target[item.parent].length - 1];
}
}
// some fields map to the same name so once we have one stick with it
target[item.name] || (target[item.name] = metaValue);
}
if (ogVideoTags.length && parsed.open_graph.videos) {
parsed.open_graph.videos = parsed.open_graph.videos.map((obj) => ({
...obj,
tags: ogVideoTags,
}));
}
if (articleTags.length && parsed.open_graph.articles) {
parsed.open_graph.articles = parsed.open_graph.articles.map((obj) => ({
...obj,
tags: articleTags,
}));
}
return parsed;
};
}
export { unfurl };