Skip to content

Commit c42f974

Browse files
committed
Polish code related to ResourceMapper
1 parent db9b74a commit c42f974

File tree

10 files changed

+24
-36
lines changed

10 files changed

+24
-36
lines changed

lib/capability-discovery.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function capabilityDiscovery () {
4646
function serviceCapabilityDocument (serviceConfig) {
4747
return (req, res) => {
4848
// Add the server root url
49-
serviceConfig.root = req.app.locals.ldp.resourceMapper.getBaseUrl(req.hostname, req.path)
49+
serviceConfig.root = req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path)
5050
// Add the 'apps' urls section
5151
serviceConfig.apps = req.app.locals.appUrls
5252
res.json(serviceConfig)

lib/handlers/allow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function allow (mode) {
1111
}
1212

1313
// Set up URL to filesystem mapping
14-
const rootUrl = ldp.resourceMapper.getBaseUrl(req.hostname)
14+
const rootUrl = ldp.resourceMapper.resolveUrl(req.hostname)
1515

1616
// Determine the actual path of the request
1717
// (This is used as an ugly hack to check the ACL status of other resources.)

lib/handlers/copy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function handler (req, res, next) {
2020
}
2121
const fromExternal = !!url.parse(copyFrom).hostname
2222
const ldp = req.app.locals.ldp
23-
const serverRoot = ldp.resourceMapper.getBaseUrl(req.hostname)
23+
const serverRoot = ldp.resourceMapper.resolveUrl(req.hostname)
2424
const copyFromUrl = fromExternal ? copyFrom : serverRoot + copyFrom
2525
const copyTo = res.locals.path || req.path
2626
const { path: copyToPath } = await ldp.resourceMapper.mapUrlToFile({ url: req })

lib/handlers/get.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function handler (req, res, next) {
2020
const ldp = req.app.locals.ldp
2121
const includeBody = req.method === 'GET'
2222
const negotiator = new Negotiator(req)
23-
const baseUri = ldp.resourceMapper.getBaseUrl(req.hostname, req.path)
23+
const baseUri = ldp.resourceMapper.resolveUrl(req.hostname, req.path)
2424
const path = res.locals.path || req.path
2525
const requestedType = negotiator.mediaType()
2626
let possibleRDFType = negotiator.mediaType(RDFs)
@@ -31,7 +31,7 @@ async function handler (req, res, next) {
3131

3232
// Set live updates
3333
if (ldp.live) {
34-
res.header('Updates-Via', ldp.resourceMapper.getBaseUrl(req.hostname).replace(/^http/, 'ws'))
34+
res.header('Updates-Via', ldp.resourceMapper.resolveUrl(req.hostname).replace(/^http/, 'ws'))
3535
}
3636

3737
debug(req.originalUrl + ' on ' + req.hostname)
@@ -156,21 +156,21 @@ async function globHandler (req, res, next) {
156156

157157
debugGlob('found matches ' + matches)
158158
Promise.all(matches.map(match => new Promise(async (resolve, reject) => {
159-
const baseUri = await ldp.resourceMapper.mapFileToUrl({ path: match, hostname: req.hostname })
159+
const urlData = await ldp.resourceMapper.mapFileToUrl({ path: match, hostname: req.hostname })
160160
fs.readFile(match, {encoding: 'utf8'}, function (err, fileData) {
161161
if (err) {
162162
debugGlob('error ' + err)
163163
return resolve()
164164
}
165-
// Files should have the .ttl extension or be extensionless (also Turtle)
166-
if (baseUri.contentType !== 'text/turtle') {
165+
// Files should be Turtle
166+
if (urlData.contentType !== 'text/turtle') {
167167
return resolve()
168168
}
169169
// The agent should have Read access to the file
170170
hasReadPermissions(match, req, res, function (allowed) {
171171
if (allowed) {
172172
try {
173-
$rdf.parse(fileData, globGraph, baseUri.url, 'text/turtle')
173+
$rdf.parse(fileData, globGraph, urlData.url, 'text/turtle')
174174
} catch (parseErr) {
175175
debugGlob(`error parsing ${match}: ${parseErr}`)
176176
}

lib/handlers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ async function handler (req, res, next) {
3030
await ldp.exists(req.hostname, path.join(req.path, indexFile))
3131
res.locals.path = path.join(req.path, indexFile)
3232
debug('Found an index for current path')
33-
return next()
3433
} catch (e) {
35-
next()
34+
// Ignore errors
3635
}
36+
next()
3737
}

lib/handlers/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function linkAuthProvider (req, res) {
2222
}
2323

2424
function linkServiceEndpoint (req, res) {
25-
let serviceEndpoint = url.resolve(req.app.locals.ldp.resourceMapper.getBaseUrl(req.hostname, req.path), '.well-known/solid')
25+
let serviceEndpoint = url.resolve(req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path), '.well-known/solid')
2626
addLink(res, serviceEndpoint, 'service')
2727
}
2828

lib/header.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function addPermissions (req, res, next) {
103103
if (!acl) return next()
104104

105105
// Turn permissions for the public and the user into a header
106-
const resource = req.app.locals.ldp.resourceMapper.getBaseUrl(req.hostname, req.path)
106+
const resource = req.app.locals.ldp.resourceMapper.resolveUrl(req.hostname, req.path)
107107
Promise.all([
108108
getPermissionsFor(acl, null, resource),
109109
getPermissionsFor(acl, session.userId, resource)

lib/ldp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class LDP {
7474
debug.settings('Config path: ' + this.configPath)
7575
debug.settings('Suffix Acl: ' + this.suffixAcl)
7676
debug.settings('Suffix Meta: ' + this.suffixMeta)
77-
debug.settings('Filesystem Root: ' + (this.resourceMapper ? this.resourceMapper._rootPath : 'none'))
77+
debug.settings('Filesystem Root: ' + (this.resourceMapper ? this.resourceMapper.rootPath : 'none'))
7878
debug.settings('Allow WebID authentication: ' + !!this.webid)
7979
debug.settings('Live-updates: ' + !!this.live)
8080
debug.settings('Multi-user: ' + !!this.multiuser)
@@ -181,7 +181,7 @@ class LDP {
181181
}
182182
}
183183
const { url: putUrl, contentType } = await this.resourceMapper.mapFileToUrl(
184-
{ path: this.resourceMapper._rootPath + resourcePath, hostname: host })
184+
{ path: this.resourceMapper.rootPath + resourcePath, hostname: host })
185185
await ldp.put(putUrl, stream, contentType)
186186
return originalPath
187187
}
@@ -249,7 +249,7 @@ class LDP {
249249
})
250250
file.on('finish', function () {
251251
debug.handlers('PUT -- Wrote data to: ' + filePath)
252-
resolve(null)
252+
resolve()
253253
})
254254
})
255255
}
@@ -326,7 +326,7 @@ class LDP {
326326

327327
async get (options) {
328328
const { path: filename, contentType } = await this.resourceMapper.mapUrlToFile({ url: options })
329-
const baseUri = this.resourceMapper.getBaseUrl(options.hostname)
329+
const baseUri = this.resourceMapper.resolveUrl(options.hostname)
330330

331331
let stats
332332
try {
@@ -385,7 +385,7 @@ class LDP {
385385
if (utils.hasSuffix(filename, this.turtleExtensions)) {
386386
contentType = 'text/turtle'
387387
}
388-
return resolve({ stream, contentType, 'container': false, contentRange, chunksize })
388+
return resolve({ stream, contentType, container: false, contentRange, chunksize })
389389
})
390390
})
391391
}

lib/resource-mapper.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ResourceMapper {
2929
}
3030
}
3131

32-
get rootPath() {
32+
get rootPath () {
3333
return this._rootPath
3434
}
3535

@@ -68,7 +68,7 @@ class ResourceMapper {
6868
async mapFileToUrl ({ path, hostname }) {
6969
// Determine the URL by chopping off everything after the dollar sign
7070
const pathname = this._removeDollarExtension(path.substring(this._rootPath.length))
71-
const url = `${this.getBaseUrl(hostname)}${encodeURI(pathname)}`
71+
const url = `${this.resolveUrl(hostname)}${encodeURI(pathname)}`
7272
return { url, contentType: this._getContentTypeByExtension(path) }
7373
}
7474

@@ -77,15 +77,11 @@ class ResourceMapper {
7777
return !this._includeHost ? this._rootPath : `${this._rootPath}/${hostname}`
7878
}
7979

80-
// Gets the base URL for the given hostname
80+
// Resolve a URL for the given hostname
8181
// Optionally, a pathname may be passed that will be appended to the baseUrl.
82-
getBaseUrl (hostname, pathname) {
83-
let baseUrl = !this._includeHost ? this._rootUrl
84-
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}`
85-
if (pathname) {
86-
baseUrl += pathname
87-
}
88-
return baseUrl
82+
resolveUrl (hostname, pathname = '') {
83+
return !this._includeHost ? `${this._rootUrl}${pathname}`
84+
: `${this._protocol}//${hostname}${this._port}${this._rootUrl}${pathname}`
8985
}
9086

9187
// Determine the full file path corresponding to a URL

lib/utils.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ function serialize (graph, baseUri, contentType) {
110110

111111
function translate (stream, baseUri, from, to) {
112112
return new Promise((resolve, reject) => {
113-
// Handle Turtle Accept header
114-
if (to === 'text/turtle' ||
115-
to === 'text/n3' ||
116-
to === 'application/turtle' ||
117-
to === 'application/n3') {
118-
to = 'text/turtle'
119-
}
120-
121113
let data = ''
122114
stream
123115
.on('data', function (chunk) {

0 commit comments

Comments
 (0)