Skip to content

Commit bf9a59e

Browse files
committed
Fixed all tests, rebased on v5.0.0
1 parent bfdb2ce commit bf9a59e

File tree

11 files changed

+62
-55
lines changed

11 files changed

+62
-55
lines changed

bin/lib/cli-utils.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const AccountManager = require('../../lib/models/account-manager')
66
const SolidHost = require('../../lib/models/solid-host')
77

88
module.exports.getAccountManager = getAccountManager
9+
module.exports.loadAccounts = loadAccounts
910
module.exports.loadConfig = loadConfig
1011
module.exports.loadUsernames = loadUsernames
1112

@@ -47,11 +48,23 @@ function loadConfig (program, options) {
4748
return argv
4849
}
4950

50-
function loadUsernames (config) {
51-
const files = fs.readdirSync(config.root)
52-
const hostname = new URL(config.serverUri).hostname
51+
/**
52+
*
53+
* @param root
54+
* @param [serverUri] If not set, hostname must be set
55+
* @param [hostname] If not set, serverUri must be set
56+
* @returns {*}
57+
*/
58+
function loadAccounts ({ root, serverUri, hostname }) {
59+
const files = fs.readdirSync(root)
60+
hostname = hostname || new URL(serverUri).hostname
5361
const isUserDirectory = new RegExp(`.${hostname}$`)
5462
return files
5563
.filter(file => isUserDirectory.test(file))
64+
}
65+
66+
function loadUsernames ({ root, serverUri }) {
67+
const hostname = new URL(serverUri).hostname
68+
return loadAccounts({ root, hostname })
5669
.map(userDirectory => userDirectory.substr(0, userDirectory.length - hostname.length - 1))
5770
}

bin/lib/updateIndex.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ const cheerio = require('cheerio')
44
const LDP = require('../../lib/ldp')
55
const { URL } = require('url')
66
const debug = require('../../lib/debug')
7+
const { readFile } = require('../../lib/common/fs-utils')
78

89
const { compileTemplate, writeTemplate } = require('../../lib/common/template-utils')
9-
const { getAccountManager, loadConfig, loadUsernames } = require('./cli-utils')
10+
const { loadConfig, loadAccounts } = require('./cli-utils')
1011
const { getName, getWebId } = require('../../lib/common/user-utils')
1112
const { initConfigDir, initTemplateDirs } = require('../../lib/server-config')
1213

@@ -21,21 +22,20 @@ module.exports = function (program) {
2122
const indexTemplatePath = path.join(templates.account, 'index.html')
2223
const indexTemplate = await compileTemplate(indexTemplatePath)
2324
const ldp = new LDP(config)
24-
const accountManager = getAccountManager(config, { ldp })
25-
const usernames = loadUsernames(config)
26-
const usersProcessed = usernames.map(async username => {
27-
const accountDirectory = accountManager.accountDirFor(username)
28-
const indexFilePath = path.join(accountDirectory, 'index.html')
25+
const accounts = loadAccounts(config)
26+
const usersProcessed = accounts.map(async account => {
27+
const accountDirectory = path.join(config.root, account)
28+
const indexFilePath = path.join(accountDirectory, '/index.html')
2929
if (!isUpdateAllowed(indexFilePath)) {
3030
return
3131
}
32-
const accountUrl = getAccountUrl(username, config)
32+
const accountUrl = getAccountUrl(account, config)
3333
try {
34-
const webId = await getWebId(accountDirectory, accountUrl, { ldp })
35-
const name = await getName(webId, { ldp })
34+
const webId = await getWebId(accountDirectory, accountUrl, ldp.suffixMeta, (filePath) => readFile(filePath))
35+
const name = await getName(webId, ldp.fetchGraph)
3636
writeTemplate(indexFilePath, indexTemplate, { name, webId })
3737
} catch (err) {
38-
debug.errors(`Failed to create new index for ${username}: ${JSON.stringify(err, null, 2)}`)
38+
debug.errors(`Failed to create new index for ${account}: ${JSON.stringify(err, null, 2)}`)
3939
}
4040
})
4141
await Promise.all(usersProcessed)

lib/common/template-utils.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,3 @@ function writeTemplate (filePath, template, substitutions) {
4949
writeFile(filePath, source)
5050
}
5151

52-
function writeTemplate (filePath, template, substitutions) {
53-
const source = template(substitutions)
54-
writeFile(filePath, source)
55-
}
56-
57-
function writeTemplate (filePath, template, substitutions) {
58-
const source = template(substitutions)
59-
writeFile(filePath, source)
60-
}
61-
62-
function writeTemplate (filePath, template, substitutions) {
63-
const source = template(substitutions)
64-
writeFile(filePath, source)
65-
}

lib/common/user-utils.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
const $rdf = require('rdflib')
22

3-
const LDP = require('../ldp')
4-
53
const SOLID = $rdf.Namespace('http://www.w3.org/ns/solid/terms#')
64
const VCARD = $rdf.Namespace('http://www.w3.org/2006/vcard/ns#')
75

86
module.exports.getName = getName
97
module.exports.getWebId = getWebId
108
module.exports.isValidUsername = isValidUsername
119

12-
async function getName (webId, options = {}) {
13-
const ldp = setupLDP(options)
14-
const graph = await ldp.fetchGraph(webId)
10+
async function getName (webId, fetchGraph) {
11+
const graph = await fetchGraph(webId)
1512
const nameNode = graph.any($rdf.sym(webId), VCARD('fn'))
1613
return nameNode.value
1714
}
1815

19-
async function getWebId (accountDirectory, accountUrl, options = {}) {
20-
const ldp = setupLDP(options)
21-
const metaFileUri = `${accountUrl}/${ldp.suffixMeta}`
22-
const metaData = await ldp.readContainerMeta(accountDirectory)
16+
async function getWebId (accountDirectory, accountUrl, suffixMeta, fetchData) {
17+
const metaFilePath = `${accountDirectory}/${suffixMeta}`
18+
const metaFileUri = `${accountUrl}${suffixMeta}`
19+
const metaData = await fetchData(metaFilePath)
2320
const metaGraph = $rdf.graph()
2421
$rdf.parse(metaData, metaGraph, metaFileUri, 'text/turtle')
2522
const webIdNode = metaGraph.any(undefined, SOLID('account'), $rdf.sym(accountUrl))
@@ -29,10 +26,3 @@ async function getWebId (accountDirectory, accountUrl, options = {}) {
2926
function isValidUsername (username) {
3027
return /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(username)
3128
}
32-
33-
function setupLDP (options) {
34-
if (!options.ldp && !options.config) {
35-
throw new Error('Require ldp or config set in options for getWebId')
36-
}
37-
return options.ldp || new LDP(options.config)
38-
}

lib/handlers/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function handler (req, res, next) {
2020
}
2121
// redirect to the right container if missing trailing /
2222
if (req.path.lastIndexOf('/') !== req.path.length - 1) {
23-
return res.redirect(301, path.join(req.path, '/'))
23+
return res.redirect(301, req.path + '/')
2424
}
2525

2626
if (requestedType && requestedType.indexOf('text/html') !== 0) {
@@ -30,7 +30,7 @@ async function handler (req, res, next) {
3030

3131
// Check if file exists in first place
3232
await ldp.exists(req.hostname, path.join(req.path, indexFile))
33-
res.locals.path = path.join(req.path, indexFile)
33+
res.locals.path = url.resolve(req.path, indexFile)
3434
debug('Found an index for current path')
3535
} catch (e) {
3636
// Ignore errors

lib/ldp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class LDP {
102102
const { path } = await this.resourceMapper.mapUrlToFile({ url })
103103
return await promisify(fs.readFile)(path, {'encoding': 'utf8'})
104104
} catch (err) {
105-
throw error(404, "Can't read file")
105+
throw error(err.status, err.message)
106106
}
107107
}
108108

@@ -284,7 +284,7 @@ class LDP {
284284
}
285285
const body = await response.text()
286286

287-
return promisify(parse)(body, uri, getContentType(response.headers))
287+
return parse(body, uri, getContentType(response.headers))
288288
}
289289

290290
/**

lib/resource-mapper.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const URL = require('url')
33
const { promisify } = require('util')
44
const { types, extensions } = require('mime-types')
55
const readdir = promisify(fs.readdir)
6+
const HTTPError = require('./http-error')
67

78
// A ResourceMapper maintains the mapping between HTTP URLs and server filenames,
89
// following the principles of the “sweet spot” discussed in
@@ -79,7 +80,7 @@ class ResourceMapper {
7980
if (isIndex) {
8081
match = ''
8182
} else {
82-
throw new Error(`File not found: ${fullPath}`)
83+
throw new HTTPError(404, `File not found: ${fullPath}`)
8384
}
8485
}
8586
path = `${folder}${match}`

lib/utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports.routeResolvedFile = routeResolvedFile
1010
module.exports.getQuota = getQuota
1111
module.exports.overQuota = overQuota
1212
module.exports.getContentType = getContentType
13+
module.exports.parse = parse
1314

1415
const fs = require('fs')
1516
const path = require('path')
@@ -69,6 +70,22 @@ function debrack (s) {
6970
return s.substring(1, s.length - 1)
7071
}
7172

73+
async function parse (data, baseUri, contentType) {
74+
const graph = $rdf.graph()
75+
return new Promise((resolve, reject) => {
76+
try {
77+
return $rdf.parse(data, graph, baseUri, contentType, (err, str) => {
78+
if (err) {
79+
return reject(err)
80+
}
81+
resolve(str)
82+
})
83+
} catch (err) {
84+
return reject(err)
85+
}
86+
})
87+
}
88+
7289
function pathBasename (fullpath) {
7390
let bname = ''
7491
if (fullpath) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a> <b> <c> .
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a> <b> <c> .

0 commit comments

Comments
 (0)