Skip to content

Commit a09dc1f

Browse files
Implement ldp.fetchGraph and pass it in to ACLChecker
1 parent 2a0ca11 commit a09dc1f

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

lib/acl-checker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const PermissionSet = require('solid-permissions').PermissionSet
44
const rdf = require('rdflib')
55
const debug = require('./debug').ACL
66
const HTTPError = require('./http-error')
7-
const { promisify } = require('util')
87

98
const DEFAULT_ACL_SUFFIX = '.acl'
109

@@ -15,6 +14,7 @@ class ACLChecker {
1514
this.host = options.host
1615
this.origin = options.origin
1716
this.fetch = options.fetch
17+
this.fetchGraph = options.fetchGraph
1818
this.strictOrigin = options.strictOrigin
1919
this.suffix = options.suffix || DEFAULT_ACL_SUFFIX
2020
}
@@ -33,7 +33,7 @@ class ACLChecker {
3333
.then(acl => this.getPermissionSet(acl))
3434
}
3535

36-
const options = { fetchGraph: promisify(this.fetch) }
36+
const options = { fetchGraph: this.fetchGraph }
3737

3838
// Check the resource's permissions
3939
return this._permissionSet
@@ -90,7 +90,7 @@ class ACLChecker {
9090

9191
// Tests whether the permissions allow a given operation
9292
checkAccess (permissionSet, user, mode) {
93-
const options = { fetchGraph: promisify(this.fetch) }
93+
const options = { fetchGraph: this.fetchGraph }
9494
return permissionSet.checkAccess(this.resource, user, mode, options)
9595
.then(hasAccess => {
9696
if (hasAccess) {

lib/handlers/allow.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ function allow (mode) {
3939
origin: req.get('origin'),
4040
host: req.protocol + '://' + req.get('host'),
4141
fetch: fetchFromLdp(mapper, ldp),
42+
fetchGraph: (uri, contentType) => {
43+
// first try loading from local fs
44+
return ldp.getGraph(uri, contentType)
45+
// failing that, fetch remote graph
46+
.catch(() => ldp.fetchGraph(uri, contentType))
47+
},
4248
suffix: ldp.suffixAcl,
4349
strictOrigin: ldp.strictOrigin
4450
})

lib/ldp.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const extend = require('extend')
1414
const rimraf = require('rimraf')
1515
const ldpContainer = require('./ldp-container')
1616
const parse = require('./utils').parse
17+
const fetch = require('node-fetch')
18+
const { promisify } = require('util')
1719

1820
const DEFAULT_CONTENT_TYPE = 'text/turtle'
1921

@@ -296,7 +298,36 @@ class LDP {
296298
}
297299

298300
/**
299-
* Fetches the graph at a given uri, parses it and and returns it.
301+
* Remotely loads the graph at a given uri, parses it and and returns it.
302+
* Usage:
303+
*
304+
* ```
305+
* ldp.fetchGraph('https://example.com/contacts/card1.ttl')
306+
* .then(graph => {
307+
* // const matches = graph.match(...)
308+
* })
309+
* ```
310+
*
311+
* @param uri {string} Fully qualified uri of the request.
312+
* @param [contentType] {string}
313+
*
314+
* @return {Promise<Graph>}
315+
*/
316+
async fetchGraph (uri, contentType = DEFAULT_CONTENT_TYPE) {
317+
const response = await fetch(uri)
318+
if (!response.ok) {
319+
const error = new Error(
320+
`Error fetching ${uri}: ${response.status} ${response.statusText}`
321+
)
322+
error.statusCode = response.status || 400
323+
throw error
324+
}
325+
const body = await response.text()
326+
return promisify(parse)(body, uri, contentType)
327+
}
328+
329+
/**
330+
* Loads from fs the graph at a given uri, parses it and and returns it.
300331
* Usage:
301332
*
302333
* ```

0 commit comments

Comments
 (0)