Skip to content

Commit 20e0af1

Browse files
committed
Promisify ldp#exists
1 parent 2ad05e8 commit 20e0af1

File tree

5 files changed

+70
-75
lines changed

5 files changed

+70
-75
lines changed

lib/handlers/allow.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const debug = require('../debug.js').ACL
66
const LegacyResourceMapper = require('../legacy-resource-mapper')
77

88
function allow (mode) {
9-
return function allowHandler (req, res, next) {
9+
return async function allowHandler (req, res, next) {
1010
const ldp = req.app.locals.ldp || {}
1111
if (!ldp.webid) {
1212
return next()
@@ -27,37 +27,41 @@ function allow (mode) {
2727
: req.path
2828

2929
// Check whether the resource exists
30-
ldp.exists(req.hostname, reqPath, (err, ret) => {
31-
// Ensure directories always end in a slash
32-
const stat = err ? null : ret.stream
33-
if (!reqPath.endsWith('/') && stat && stat.isDirectory()) {
34-
reqPath += '/'
35-
}
30+
let stat
31+
try {
32+
const ret = await ldp.exists(req.hostname, reqPath)
33+
stat = ret.stream
34+
} catch (err) {
35+
stat = null
36+
}
3637

37-
// Obtain and store the ACL of the requested resource
38-
req.acl = new ACL(rootUrl + reqPath, {
39-
origin: req.get('origin'),
40-
host: req.protocol + '://' + req.get('host'),
41-
fetch: fetchFromLdp(mapper, ldp),
42-
fetchGraph: (uri, options) => {
43-
// first try loading from local fs
44-
return ldp.getGraph(uri, options.contentType)
45-
// failing that, fetch remote graph
46-
.catch(() => ldp.fetchGraph(uri, options))
47-
},
48-
suffix: ldp.suffixAcl,
49-
strictOrigin: ldp.strictOrigin,
50-
originsAllowed: ldp.originsAllowed
51-
})
38+
// Ensure directories always end in a slash
39+
if (!reqPath.endsWith('/') && stat && stat.isDirectory()) {
40+
reqPath += '/'
41+
}
5242

53-
// Ensure the user has the required permission
54-
const userId = req.session.userId
55-
req.acl.can(userId, mode)
56-
.then(() => next(), err => {
57-
debug(`${mode} access denied to ${userId || '(none)'}`)
58-
next(err)
59-
})
43+
// Obtain and store the ACL of the requested resource
44+
req.acl = new ACL(rootUrl + reqPath, {
45+
origin: req.get('origin'),
46+
host: req.protocol + '://' + req.get('host'),
47+
fetch: fetchFromLdp(mapper, ldp),
48+
fetchGraph: (uri, options) => {
49+
// first try loading from local fs
50+
return ldp.getGraph(uri, options.contentType)
51+
// failing that, fetch remote graph
52+
.catch(() => ldp.fetchGraph(uri, options))
53+
},
54+
suffix: ldp.suffixAcl,
55+
strictOrigin: ldp.strictOrigin
6056
})
57+
58+
// Ensure the user has the required permission
59+
const userId = req.session.userId
60+
req.acl.can(userId, mode)
61+
.then(() => next(), err => {
62+
debug(`${mode} access denied to ${userId || '(none)'}`)
63+
next(err)
64+
})
6165
}
6266
}
6367

lib/handlers/index.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ const debug = require('debug')('solid:index')
55
const utils = require('../utils')
66
const Negotiator = require('negotiator')
77

8-
function handler (req, res, next) {
8+
async function handler (req, res, next) {
99
const indexFile = 'index.html'
1010
const ldp = req.app.locals.ldp
1111
const negotiator = new Negotiator(req)
1212
const requestedType = negotiator.mediaType()
1313
const filename = utils.reqToPath(req)
1414

15-
ldp.stat(filename).then(stats => {
15+
try {
16+
const stats = await ldp.stat(filename)
1617
if (!stats.isDirectory()) {
1718
return next()
1819
}
@@ -27,13 +28,11 @@ function handler (req, res, next) {
2728
debug('Looking for index in ' + req.path)
2829

2930
// Check if file exists in first place
30-
ldp.exists(req.hostname, path.join(req.path, indexFile), function (err) {
31-
if (err) {
32-
return next()
33-
}
34-
res.locals.path = path.join(req.path, indexFile)
35-
debug('Found an index for current path')
36-
return next()
37-
})
38-
}, () => next())
31+
await ldp.exists(req.hostname, path.join(req.path, indexFile))
32+
res.locals.path = path.join(req.path, indexFile)
33+
debug('Found an index for current path')
34+
return next()
35+
} catch (e) {
36+
next()
37+
}
3938
}

lib/handlers/post.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const patch = require('./patch')
88
const error = require('../http-error')
99
const { extensions } = require('mime-types')
1010

11-
function handler (req, res, next) {
11+
async function handler (req, res, next) {
1212
const ldp = req.app.locals.ldp
1313
const contentType = req.get('content-type')
1414
debug('content-type is ', contentType)
@@ -26,29 +26,28 @@ function handler (req, res, next) {
2626
}
2727

2828
// Check if container exists
29-
ldp.exists(req.hostname, containerPath, function (err, ret) {
30-
if (err) {
31-
return next(error(err, 'Container not valid'))
32-
}
33-
34-
let stats
29+
let stats
30+
try {
31+
const ret = await ldp.exists(req.hostname, containerPath)
3532
if (ret) {
3633
stats = ret.stream
3734
}
35+
} catch (err) {
36+
return next(error(err, 'Container not valid'))
37+
}
3838

39-
// Check if container is a directory
40-
if (stats && !stats.isDirectory()) {
41-
debug('Path is not a container, 405!')
42-
return next(error(405, 'Requested resource is not a container'))
43-
}
39+
// Check if container is a directory
40+
if (stats && !stats.isDirectory()) {
41+
debug('Path is not a container, 405!')
42+
return next(error(405, 'Requested resource is not a container'))
43+
}
4444

45-
// Dispatch to the right handler
46-
if (req.is('multipart/form-data')) {
47-
multi()
48-
} else {
49-
one()
50-
}
51-
})
45+
// Dispatch to the right handler
46+
if (req.is('multipart/form-data')) {
47+
multi()
48+
} else {
49+
one()
50+
}
5251

5352
function multi () {
5453
debug('receving multiple files')

lib/ldp.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,10 @@ class LDP {
262262
})
263263
}
264264

265-
exists (hostname, path, callback) {
265+
async exists (hostname, path) {
266266
const options = { hostname, path, includeBody: false }
267-
if (callback) {
268-
return this.get(options).then((data) => callback(null, data), callback)
269-
} else {
270-
return this.get(options).then(() => true)
271-
}
267+
await this.get(options)
268+
return true
272269
}
273270

274271
/**

lib/models/account-manager.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,12 @@ class AccountManager {
107107
*
108108
* @return {Promise<boolean>}
109109
*/
110-
accountUriExists (accountUri, accountResource = '/') {
111-
return new Promise((resolve, reject) => {
112-
this.store.exists(accountUri, accountResource, (err, result) => {
113-
if (err && err.status === 404) {
114-
return resolve(false)
115-
}
116-
117-
resolve(!!result)
118-
})
119-
})
110+
async accountUriExists (accountUri, accountResource = '/') {
111+
try {
112+
return await this.store.exists(accountUri, accountResource)
113+
} catch (err) {
114+
return false
115+
}
120116
}
121117

122118
/**

0 commit comments

Comments
 (0)