From 0d1ed72823fa8d547b205d289f1302bfc9cae3b1 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 09:19:01 -0400 Subject: [PATCH 01/10] Add comments to LDP class --- lib/ldp.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ldp.js b/lib/ldp.js index cade52701..f78a15334 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -32,6 +32,23 @@ const RDF_MIME_TYPES = [ 'application/x-turtle' ] +/** + * @constructor + * @param [options={}] {Object} + * @param [options.root] + * @param [options.suffixAcl] + * @param [options.suffixMeta] + * @param [options.errorPages] + * @param [options.fileBrowser] + * @param [options.suppressDataBrowser] + * @param [options.dataBrowserPath] + * @param [options.webid] + * @param [options.auth] + * @param [options.idp] + * @param [options.proxy] + * @param [options.live] + * @param [options.store] + */ class LDP { constructor (argv = {}) { extend(this, argv) From f3c8c50dabcc375f3cd533abb9dd8773eb7ecc98 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 09:27:23 -0400 Subject: [PATCH 02/10] Extract printing of debug info from LDP constructor --- lib/create-app.js | 1 + lib/ldp.js | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/create-app.js b/lib/create-app.js index 62d0cedf1..8a6d790f1 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -45,6 +45,7 @@ function createApp (argv = {}) { argv.templates = config.initTemplateDirs(configPath) const ldp = new LDP(argv) + ldp.printDebugInfo() const app = express() diff --git a/lib/ldp.js b/lib/ldp.js index f78a15334..a8b9d14b8 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -35,6 +35,9 @@ const RDF_MIME_TYPES = [ /** * @constructor * @param [options={}] {Object} + * @param [options.serverUri] + * @param [options.dbPath] + * @param [options.configPath] * @param [options.root] * @param [options.suffixAcl] * @param [options.suffixMeta] @@ -44,7 +47,7 @@ const RDF_MIME_TYPES = [ * @param [options.dataBrowserPath] * @param [options.webid] * @param [options.auth] - * @param [options.idp] + * @param [options.multiuser] * @param [options.proxy] * @param [options.live] * @param [options.store] @@ -89,7 +92,9 @@ class LDP { if (this.corsProxy && this.corsProxy[ 0 ] !== '/') { this.corsProxy = '/' + this.corsProxy } + } + printDebugInfo () { debug.settings('Server URI: ' + this.serverUri) debug.settings('Auth method: ' + this.auth) debug.settings('Db path: ' + this.dbPath) @@ -102,8 +107,6 @@ class LDP { debug.settings('Multi-user: ' + !!this.multiuser) debug.settings('Suppress default data browser app: ' + this.suppressDataBrowser) debug.settings('Default data browser app file path: ' + this.dataBrowserPath) - - return this } stat (file, callback) { From 9d6ceddda94d4765b38d6b095314232e996a27bb Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 12:11:17 -0400 Subject: [PATCH 03/10] Remove 'extend' dependency --- bin/lib/start.js | 3 +-- lib/ldp.js | 7 +++---- package.json | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bin/lib/start.js b/bin/lib/start.js index 8967fe238..b39cf81db 100644 --- a/bin/lib/start.js +++ b/bin/lib/start.js @@ -2,7 +2,6 @@ const options = require('./options') const fs = require('fs') -const extend = require('extend') const { cyan, red, bold } = require('colorette') module.exports = function (program, server) { @@ -23,7 +22,7 @@ module.exports = function (program, server) { start.option('-v, --verbose', 'Print the logs to console') start.action((opts) => { - let argv = extend({}, opts, { version: program.version() }) + let argv = Object.assign({}, opts, { version: program.version() }) let configFile = argv['configFile'] || './config.json' fs.readFile(configFile, (err, file) => { diff --git a/lib/ldp.js b/lib/ldp.js index a8b9d14b8..4471b9215 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -10,7 +10,6 @@ const utils = require('./utils') const error = require('./http-error') const stringToStream = require('./utils').stringToStream const serialize = require('./utils').serialize -const extend = require('extend') const rimraf = require('rimraf') const ldpContainer = require('./ldp-container') const parse = require('./utils').parse @@ -53,8 +52,8 @@ const RDF_MIME_TYPES = [ * @param [options.store] */ class LDP { - constructor (argv = {}) { - extend(this, argv) + constructor (options = {}) { + Object.assign(this, options) // Setting root if (!this.root) { @@ -76,7 +75,7 @@ class LDP { // Error pages folder this.errorPages = null if (!this.noErrorPages) { - this.errorPages = argv.errorPages + this.errorPages = options.errorPages if (!this.errorPages) { // TODO: For now disable error pages if errorPages parameter is not explicitly passed this.noErrorPages = true diff --git a/package.json b/package.json index df29a64b8..b8eda2e49 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,6 @@ "express": "^4.16.3", "express-handlebars": "^3.0.0", "express-session": "^1.15.6", - "extend": "^3.0.0", "from2": "^2.1.0", "fs-extra": "^2.1.0", "glob": "^7.1.1", From eef07d24e55517a941ba9add03c4541f1845d01d Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 12:16:31 -0400 Subject: [PATCH 04/10] Remove unused 'skin' param --- lib/ldp.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 4471b9215..4883a221e 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -84,10 +84,6 @@ class LDP { } } - if (this.skin !== false) { - this.skin = true - } - if (this.corsProxy && this.corsProxy[ 0 ] !== '/') { this.corsProxy = '/' + this.corsProxy } From eada0d802728ae4095ddbfdfdcade073808a0ded Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 12:27:29 -0400 Subject: [PATCH 05/10] Extract initErrorPages() out of LDP constructor --- lib/ldp.js | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 4883a221e..d01967bb6 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -55,38 +55,18 @@ class LDP { constructor (options = {}) { Object.assign(this, options) - // Setting root - if (!this.root) { - this.root = process.cwd() - } - if (!this.root.endsWith('/')) { - this.root += '/' - } - - // Suffixes - if (!this.suffixAcl) { - this.suffixAcl = '.acl' - } - if (!this.suffixMeta) { - this.suffixMeta = '.meta' - } + this.suffixAcl = options.suffixAcl || '.acl' + this.suffixMeta = options.suffixMeta || '.meta' this.turtleExtensions = [ '.ttl', this.suffixAcl, this.suffixMeta ] - // Error pages folder - this.errorPages = null - if (!this.noErrorPages) { - this.errorPages = options.errorPages - if (!this.errorPages) { - // TODO: For now disable error pages if errorPages parameter is not explicitly passed - this.noErrorPages = true - } else if (!this.errorPages.endsWith('/')) { - this.errorPages += '/' - } - } + this.root = options.root || process.cwd() + if (!this.root.endsWith('/')) { this.root += '/' } - if (this.corsProxy && this.corsProxy[ 0 ] !== '/') { + if (this.corsProxy && !this.corsProxy.startsWith('/')) { this.corsProxy = '/' + this.corsProxy } + + this.initErrorPages(options) } printDebugInfo () { @@ -104,6 +84,19 @@ class LDP { debug.settings('Default data browser app file path: ' + this.dataBrowserPath) } + initErrorPages (options) { + this.errorPages = null + if (!this.noErrorPages) { + this.errorPages = options.errorPages + if (!this.errorPages) { + // TODO: For now disable error pages if errorPages parameter is not explicitly passed + this.noErrorPages = true + } else if (!this.errorPages.endsWith('/')) { + this.errorPages += '/' + } + } + } + stat (file, callback) { fs.stat(file, function (err, stats) { if (err) { From 7f9170438d8a40d75ea00b9aa9e7945fba74d0b0 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 13:36:33 -0400 Subject: [PATCH 06/10] Move printDebugInfo() to server-config.js --- lib/create-app.js | 3 ++- lib/ldp.js | 15 --------------- lib/server-config.js | 19 ++++++++++++++++++- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/create-app.js b/lib/create-app.js index 8a6d790f1..a22b954c6 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -44,8 +44,9 @@ function createApp (argv = {}) { const configPath = config.initConfigDir(argv) argv.templates = config.initTemplateDirs(configPath) + config.printDebugInfo(argv) + const ldp = new LDP(argv) - ldp.printDebugInfo() const app = express() diff --git a/lib/ldp.js b/lib/ldp.js index d01967bb6..8565c2d2e 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -69,21 +69,6 @@ class LDP { this.initErrorPages(options) } - printDebugInfo () { - debug.settings('Server URI: ' + this.serverUri) - debug.settings('Auth method: ' + this.auth) - debug.settings('Db path: ' + this.dbPath) - debug.settings('Config path: ' + this.configPath) - debug.settings('Suffix Acl: ' + this.suffixAcl) - debug.settings('Suffix Meta: ' + this.suffixMeta) - debug.settings('Filesystem Root: ' + this.root) - debug.settings('Allow WebID authentication: ' + !!this.webid) - debug.settings('Live-updates: ' + !!this.live) - debug.settings('Multi-user: ' + !!this.multiuser) - debug.settings('Suppress default data browser app: ' + this.suppressDataBrowser) - debug.settings('Default data browser app file path: ' + this.dataBrowserPath) - } - initErrorPages (options) { this.errorPages = null if (!this.noErrorPages) { diff --git a/lib/server-config.js b/lib/server-config.js index a7dbba6fd..ccb49e8fe 100644 --- a/lib/server-config.js +++ b/lib/server-config.js @@ -5,6 +5,22 @@ const fs = require('fs-extra') const path = require('path') +const debug = require('./debug') + +function printDebugInfo (options) { + debug.settings('Server URI: ' + options.serverUri) + debug.settings('Auth method: ' + options.auth) + debug.settings('Db path: ' + options.dbPath) + debug.settings('Config path: ' + options.configPath) + debug.settings('Suffix Acl: ' + options.suffixAcl) + debug.settings('Suffix Meta: ' + options.suffixMeta) + debug.settings('Filesystem Root: ' + options.root) + debug.settings('Allow WebID authentication: ' + !!options.webid) + debug.settings('Live-updates: ' + !!options.live) + debug.settings('Multi-user: ' + !!options.multiuser) + debug.settings('Suppress default data browser app: ' + options.suppressDataBrowser) + debug.settings('Default data browser app file path: ' + options.dataBrowserPath) +} /** * Ensures that a directory has been copied / initialized. Used to ensure that @@ -132,5 +148,6 @@ module.exports = { ensureWelcomePage, initConfigDir, initDefaultViews, - initTemplateDirs + initTemplateDirs, + printDebugInfo } From 6feab82a453062906ada01f0ef635c70bce3c999 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 12:33:56 -0400 Subject: [PATCH 07/10] Explicitly assign LDP options in constructor (instead of Object.assign) --- config/defaults.js | 4 +++- lib/ldp.js | 25 +++++++++++++++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/config/defaults.js b/config/defaults.js index a2032929c..fdfdf6184 100644 --- a/config/defaults.js +++ b/config/defaults.js @@ -11,7 +11,9 @@ module.exports = { 'port': 8443, 'serverUri': 'https://localhost:8443', 'webid': true, - 'dataBrowserPath': 'default' + 'dataBrowserPath': 'default', + 'suffixAcl': '.acl', + 'suffixMeta': '.meta' // For use in Enterprises to configure a HTTP proxy for all outbound HTTP requests from the SOLID server (we use // https://www.npmjs.com/package/global-tunnel-ng). diff --git a/lib/ldp.js b/lib/ldp.js index 8565c2d2e..d85ca1520 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -15,6 +15,7 @@ const ldpContainer = require('./ldp-container') const parse = require('./utils').parse const fetch = require('node-fetch') const { promisify } = require('util') +const defaults = require('../config/defaults') const DEFAULT_CONTENT_TYPE = 'text/turtle' @@ -34,6 +35,7 @@ const RDF_MIME_TYPES = [ /** * @constructor * @param [options={}] {Object} + * @param [options.auth] * @param [options.serverUri] * @param [options.dbPath] * @param [options.configPath] @@ -45,18 +47,29 @@ const RDF_MIME_TYPES = [ * @param [options.suppressDataBrowser] * @param [options.dataBrowserPath] * @param [options.webid] - * @param [options.auth] * @param [options.multiuser] - * @param [options.proxy] * @param [options.live] - * @param [options.store] + * @param [options.corsProxy] */ class LDP { constructor (options = {}) { - Object.assign(this, options) + options = Object.assign({}, defaults, options) + this.auth = options.auth + this.serverUri = options.serverUri + this.dbPath = options.dbPath + this.configPath = options.configPath + this.suffixAcl = options.suffixAcl + this.suffixMeta = options.suffixMeta + this.multiuser = options.multiuser + this.webid = options.webid + this.strictOrigin = options.strictOrigin + this.live = options.live + this.errorPages = options.errorPages + this.noErrorPages = options.noErrorPages + this.errorHandler = options.errorHandler + this.dataBrowserPath = options.dataBrowserPath + this.suppressDataBrowser = options.suppressDataBrowser - this.suffixAcl = options.suffixAcl || '.acl' - this.suffixMeta = options.suffixMeta || '.meta' this.turtleExtensions = [ '.ttl', this.suffixAcl, this.suffixMeta ] this.root = options.root || process.cwd() From bbec55c4e7284ca6c5e5de6618535f912c42eb83 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Mon, 11 Jun 2018 13:54:06 -0400 Subject: [PATCH 08/10] Remove 'auth' as ldp param - not needed --- lib/create-server.js | 2 +- lib/ldp.js | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/create-server.js b/lib/create-server.js index 31216c834..59fd2fe3f 100644 --- a/lib/create-server.js +++ b/lib/create-server.js @@ -72,7 +72,7 @@ function createServer (argv, app) { cert: cert }, argv) - if (ldp.webid && ldp.auth === 'tls') { + if (argv.webid && argv.auth === 'tls') { credentials.requestCert = true } diff --git a/lib/ldp.js b/lib/ldp.js index d85ca1520..78e3a42ed 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -35,7 +35,6 @@ const RDF_MIME_TYPES = [ /** * @constructor * @param [options={}] {Object} - * @param [options.auth] * @param [options.serverUri] * @param [options.dbPath] * @param [options.configPath] @@ -54,7 +53,6 @@ const RDF_MIME_TYPES = [ class LDP { constructor (options = {}) { options = Object.assign({}, defaults, options) - this.auth = options.auth this.serverUri = options.serverUri this.dbPath = options.dbPath this.configPath = options.configPath From 6064fac0b765540443804e5682e6971280557b09 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Thu, 14 Jun 2018 11:51:14 -0400 Subject: [PATCH 09/10] Move ldp.live to host.live --- lib/create-app.js | 3 ++- lib/create-server.js | 5 ++--- lib/handlers/get.js | 4 ++-- lib/ldp.js | 1 - lib/models/solid-host.js | 2 ++ lib/server-config.js | 9 +++++++++ 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/create-app.js b/lib/create-app.js index a22b954c6..32ba95939 100644 --- a/lib/create-app.js +++ b/lib/create-app.js @@ -39,7 +39,8 @@ function createApp (argv = {}) { // Override default configs (defaults) with passed-in params (argv) argv = Object.assign({}, defaults, argv) - argv.host = SolidHost.from({ port: argv.port, serverUri: argv.serverUri }) + const hostConfig = config.hostConfigFor(argv) + argv.host = SolidHost.from(hostConfig) const configPath = config.initConfigDir(argv) argv.templates = config.initTemplateDirs(configPath) diff --git a/lib/create-server.js b/lib/create-server.js index 59fd2fe3f..c3071a52d 100644 --- a/lib/create-server.js +++ b/lib/create-server.js @@ -13,7 +13,6 @@ function createServer (argv, app) { argv = argv || {} app = app || express() const ldpApp = createApp(argv) - const ldp = ldpApp.locals.ldp || {} let mount = argv.mount || '/' // Removing ending '/' if (mount.length > 1 && @@ -98,9 +97,9 @@ function createServer (argv, app) { } // Setup Express app - if (ldp.live) { + if (argv.live) { const solidWs = SolidWs(server, ldpApp) - ldpApp.locals.ldp.live = solidWs.publish.bind(solidWs) + ldpApp.locals.host.live = solidWs.publish.bind(solidWs) } return server diff --git a/lib/handlers/get.js b/lib/handlers/get.js index c92731792..0ea455963 100644 --- a/lib/handlers/get.js +++ b/lib/handlers/get.js @@ -19,7 +19,7 @@ const error = require('../http-error') const RDFs = require('../ldp').RDF_MIME_TYPES function handler (req, res, next) { - const ldp = req.app.locals.ldp + const { ldp, host: hostConfig } = req.app.locals const includeBody = req.method === 'GET' const negotiator = new Negotiator(req) const baseUri = utils.getFullUri(req) @@ -32,7 +32,7 @@ function handler (req, res, next) { res.header('MS-Author-Via', 'SPARQL') // Set live updates - if (ldp.live) { + if (hostConfig.live) { res.header('Updates-Via', utils.getBaseUri(req).replace(/^http/, 'ws')) } diff --git a/lib/ldp.js b/lib/ldp.js index 78e3a42ed..02047b8af 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -61,7 +61,6 @@ class LDP { this.multiuser = options.multiuser this.webid = options.webid this.strictOrigin = options.strictOrigin - this.live = options.live this.errorPages = options.errorPages this.noErrorPages = options.noErrorPages this.errorHandler = options.errorHandler diff --git a/lib/models/solid-host.js b/lib/models/solid-host.js index 663546f62..012a5cc75 100644 --- a/lib/models/solid-host.js +++ b/lib/models/solid-host.js @@ -24,6 +24,8 @@ class SolidHost { this.parsedUri = url.parse(this.serverUri) this.host = this.parsedUri.host this.hostname = this.parsedUri.hostname + + this.live = options.live } /** diff --git a/lib/server-config.js b/lib/server-config.js index ccb49e8fe..14220ee63 100644 --- a/lib/server-config.js +++ b/lib/server-config.js @@ -22,6 +22,14 @@ function printDebugInfo (options) { debug.settings('Default data browser app file path: ' + options.dataBrowserPath) } +function hostConfigFor (argv) { + return { + port: argv.port, + serverUri: argv.serverUri, + live: argv.live + } +} + /** * Ensures that a directory has been copied / initialized. Used to ensure that * account templates, email templates and default apps have been copied from @@ -146,6 +154,7 @@ function initTemplateDirs (configPath) { module.exports = { ensureDirCopyExists, ensureWelcomePage, + hostConfigFor, initConfigDir, initDefaultViews, initTemplateDirs, From b80b708a50560b857da09144cb86dae7cabc81d1 Mon Sep 17 00:00:00 2001 From: Dmitri Zagidulin Date: Thu, 14 Jun 2018 11:59:36 -0400 Subject: [PATCH 10/10] Remove corsProxy mention from ldp - not needed --- lib/ldp.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/ldp.js b/lib/ldp.js index 02047b8af..5fb0f006b 100644 --- a/lib/ldp.js +++ b/lib/ldp.js @@ -47,8 +47,6 @@ const RDF_MIME_TYPES = [ * @param [options.dataBrowserPath] * @param [options.webid] * @param [options.multiuser] - * @param [options.live] - * @param [options.corsProxy] */ class LDP { constructor (options = {}) { @@ -72,10 +70,6 @@ class LDP { this.root = options.root || process.cwd() if (!this.root.endsWith('/')) { this.root += '/' } - if (this.corsProxy && !this.corsProxy.startsWith('/')) { - this.corsProxy = '/' + this.corsProxy - } - this.initErrorPages(options) }