From 75cf8087b29d25e2f688c5e2a62e2468771a2961 Mon Sep 17 00:00:00 2001
From: Ulf Seltmann <seltmann@ub.uni-leipzig.de>
Date: Wed, 30 Aug 2017 15:32:36 +0200
Subject: [PATCH] added cross-origin support

---
 package-lock.json | 20 +++++++++++++++++++-
 package.json      |  4 +++-
 src/server.ts     | 33 ++++++++++++++++++++++++++-------
 3 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 4c040f2..b25abc6 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "dacap",
-  "version": "1.0.0",
+  "version": "1.0.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -87,6 +87,15 @@
         "@types/express": "4.0.37"
       }
     },
+    "@types/cors": {
+      "version": "2.8.1",
+      "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.1.tgz",
+      "integrity": "sha1-VAc8rzt6dB5n+4JIP4Omyt+6dQE=",
+      "dev": true,
+      "requires": {
+        "@types/express": "4.0.37"
+      }
+    },
     "@types/debug": {
       "version": "0.0.30",
       "resolved": "https://registry.npmjs.org/@types/debug/-/debug-0.0.30.tgz",
@@ -1417,6 +1426,15 @@
       "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
     },
+    "cors": {
+      "version": "2.8.4",
+      "resolved": "https://registry.npmjs.org/cors/-/cors-2.8.4.tgz",
+      "integrity": "sha1-K9OB8usgECAQXNUOpZ2mMJBpRoY=",
+      "requires": {
+        "object-assign": "4.1.1",
+        "vary": "1.1.1"
+      }
+    },
     "create-ecdh": {
       "version": "4.0.0",
       "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.0.tgz",
diff --git a/package.json b/package.json
index 3a11d4e..2067caf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "dacap",
-  "version": "1.0.1",
+  "version": "1.0.2",
   "description": "proxies,compresses and caches api-requests",
   "main": "bin/dacap",
   "bin": {
@@ -33,6 +33,7 @@
     "body-parser": "^1.17.2",
     "compression": "^1.7.0",
     "core-js": "^2.5.0",
+    "cors": "^2.8.4",
     "debug": "^3.0.1",
     "express": "^4.15.4",
     "node-cache": "^4.1.5",
@@ -47,6 +48,7 @@
   "devDependencies": {
     "@types/body-parser": "^1.16.5",
     "@types/compression": "0.0.33",
+    "@types/cors": "^2.8.1",
     "@types/debug": "0.0.30",
     "@types/express": "^4.0.37",
     "@types/mocha": "^2.2.42",
diff --git a/src/server.ts b/src/server.ts
index 20ffe39..0cc0d8c 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -4,12 +4,13 @@ import * as debugFactory from 'debug';
 import * as path from 'path';
 import * as cache from './cache';
 import * as bodyparser from 'body-parser';
-
+import * as cors from 'cors';
 const debug = debugFactory('dacap:server');
 
 export class Server {
 	private register: cache.Register;
 	private expressApp: express.Application;
+	private corsOptions: cors.CorsOptions;
 
 	constructor(private config: {
 		storagePath: string,
@@ -22,21 +23,39 @@ export class Server {
 		autosaveInterval: number,
 		registerName: string
 	}) {
-		this._init();
+
+		this.expressApp = express();
+
+		this.initRegister();
+		this.initMiddlewares();
+		this.initRoutes();
 	}
 
-	private _init() {
+	private initRegister() {
 		this.register = new cache.Register(this.config.storagePath, this.config.registerName);
 		this.register.restore();
 		setTimeout(this.register.save, this.config.autosaveInterval * 1000);
 
-		this.expressApp = express();
-		this.expressApp.get(/favicon.ico/, (req, res, next) => {
-			res.send();
-		});
+	}
+
+	private initMiddlewares() {
+		this.corsOptions = {
+			origin: (origin, callback) => {
+				callback(null, true);
+			},
+			optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
+		}
+		// globally allow all origins
+		this.expressApp.use(cors(this.corsOptions))
 
 		this.expressApp.use(compression());
 		this.expressApp.use(bodyparser.json());
+	}
+
+	private initRoutes() {
+		this.expressApp.get(/favicon.ico/, (req, res, next) => {
+			res.send();
+		});
 
 		this.expressApp.get('/admin/api/config', (req, res, next) => {
 			return res.json(this.config);
-- 
GitLab