Skip to content

Commit de29521

Browse files
committed
support cors
1 parent 62c9580 commit de29521

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

main.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ const flags = require("flags");
44
flags.defineString("db", "", "DB File path");
55
flags.defineBoolean("readonly", false, "Open the database for readonly");
66
flags.defineNumber("port", 2048, "TCP Port to listen on");
7+
flags.defineMultiString("cors", [], "CORS URLs to allow requests from");
78
flags.parse();
89

910
console.log("db", "=", flags.get("db"));
1011
console.log("readonly", "=", flags.get("readonly"));
1112
console.log("port", "=", flags.get("port"));
13+
console.log("cors", "=", flags.get("cors").join(", ") || "false");
1214

1315
const Database = require("better-sqlite3");
1416

@@ -19,14 +21,30 @@ const app = express();
1921
app.use(require("compression")());
2022
app.use(bodyParser.urlencoded({ extended: false, limit: "1mb" }));
2123
app.use(bodyParser.json({ limit: "1mb" }));
22-
app.use(function(req, res, next) {
24+
app.use(function (req, res, next) {
2325
req.connection.setTimeout(2 * 60 * 1000); // 2 minutes
2426
res.connection.setTimeout(2 * 60 * 1000); // 2 minutes
2527
next();
2628
});
2729

30+
if (flags.get("cors").length > 0) {
31+
const cors = require("cors");
32+
const corsWhitelist = new Set(flags.get("cors"));
33+
const corsOptions = {
34+
origin: function (origin, callback) {
35+
//https://www.w3.org/TR/cors/#access-control-allow-origin-response-header
36+
if (!origin || corsWhitelist.has(origin) || corsWhitelist.has("*")) {
37+
return callback(null, true);
38+
}
39+
40+
callback(new Error("Not allowed by CORS"));
41+
},
42+
};
43+
app.use(cors(corsOptions));
44+
}
45+
2846
function getSqlExecutor(httpRequestFieldName) {
29-
return function(req, res) {
47+
return function (req, res) {
3048
const sql = req[httpRequestFieldName].sql;
3149
if (!sql) {
3250
return res.send([]);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"better-sqlite3": "^6.0.1",
1111
"body-parser": "^1.19.0",
1212
"compression": "^1.7.4",
13+
"cors": "^2.8.5",
1314
"express": "^4.17.1",
1415
"flags": "^0.1.3"
1516
},

yarn.lock

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ core-util-is@~1.0.0:
165165
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
166166
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
167167

168+
cors@^2.8.5:
169+
version "2.8.5"
170+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
171+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
172+
dependencies:
173+
object-assign "^4"
174+
vary "^1"
175+
168176
debug@2.6.9:
169177
version "2.6.9"
170178
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
@@ -536,7 +544,7 @@ number-is-nan@^1.0.0:
536544
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
537545
integrity sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=
538546

539-
object-assign@^4.1.0:
547+
object-assign@^4, object-assign@^4.1.0:
540548
version "4.1.1"
541549
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
542550
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
@@ -866,7 +874,7 @@ utils-merge@1.0.1:
866874
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
867875
integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=
868876

869-
vary@~1.1.2:
877+
vary@^1, vary@~1.1.2:
870878
version "1.1.2"
871879
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
872880
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=

0 commit comments

Comments
 (0)