-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
108 lines (95 loc) · 2.95 KB
/
app.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
const express = require("express");
const cors = require("cors");
const app = express();
const ogpParser = require("ogp-parser");
const { LRUCache } = require("lru-cache");
const { URL } = require("url");
const whitelist = [
"http://localhost:5001",
"https://oc.app",
"https://test.oc.app",
"https://webtest.oc.app",
];
// Create an LRU cache with a max size of 10000 items or 1 GB
const cache = new LRUCache({
max: 5000,
maxSize: 500 * 1024 * 1024,
sizeCalculation: (value, key) => JSON.stringify(value).length + key.length,
ttl: 3600 * 1000, // 1 hour
});
const corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error(`Origin ${origin} is not permitted`));
}
},
};
app.use(cors(corsOptions));
app.get("/preview", async (req, res) => {
const url = req.query.url;
if (!url) {
return res.status(400).json({ error: "URL parameter is required" });
}
try {
const callerOrigin = req.headers.origin || req.headers.referer;
if (callerOrigin) {
try {
const callerUrl = new URL(callerOrigin);
const targetUrl = new URL(url);
// if the origins match then this is an OC url for which we cannot return meaningful meta data
if (callerUrl.origin === targetUrl.origin) {
const msg = `We cannot return meaningful metadata for internal links (yet): ${url}`;
console.warn(msg);
return res.status(404).json({
error: msg,
});
}
} catch (error) {
console.warn("Failed to parse URL for origin check:", error);
}
}
const cachedData = cache.get(url);
if (cachedData) {
console.log("Returning OpenGraph metadata from cache for ", url);
res.set("Cache-Control", "public, max-age=3600");
return res.json(cachedData);
}
const metadata = await ogpParser(url);
if (!metadata) {
console.log("OpenGraph metadata not found", url);
return res.status(404).json({ error: "OpenGraph metadata not found" });
}
const data = {
title: metadata.title,
description: metadata.ogp["og:description"],
image: metadata.ogp["og:image"],
imageAlt: metadata.ogp["og:image:alt"],
};
cache.set(url, data);
res.set("Cache-Control", "public, max-age=3600"); // Cache for 1 hour
return res.json(data);
} catch (error) {
console.error("Error getting OpenGraph metadata", url, error);
return res
.status(500)
.json({ error: `Error getting OpenGraph metadata for ${url}` });
}
});
// Let's see if we can spot what's happening with the memory
setInterval(() => {
try {
const used = process.memoryUsage();
console.log(
`Heap: ${(used.heapUsed / 1024 / 1024).toFixed(2)} MB / ${(
used.heapTotal /
1024 /
1024
).toFixed(2)} MB`
);
} catch (err) {
console.error(err);
}
}, 60000); // Every minute
module.exports = app;