Skip to content

Commit 127dde1

Browse files
Pass in fs into LdpFileStore
1 parent 00688f7 commit 127dde1

3 files changed

Lines changed: 33 additions & 28 deletions

File tree

lib/api/ldp/api.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ class LdpHttpHandler {
99
/**
1010
* Usage:
1111
* ```
12-
* const store = new LdpFileStore({host, fetch, ...})
12+
* const mapperOptions = {
13+
* rootUrl: host.serverUri,
14+
* rootPath: host.root,
15+
* includeHost: host.multiuser
16+
* }
17+
* const mapper = new LegacyResourceMapper(mapperOptions)
18+
*
19+
* const storeOptions = {
20+
* host, mapper, suffixMeta, suffixAcl, dataBrowserPath, suppressDataBrowser
21+
* }
22+
* const store = new LdpFileStore(storeOptions)
23+
*
1324
* const acl = new AclChecker({host, fetch, store, ...})
1425
*
15-
* const ldp = new LdpHttpHandler({store, acl})
26+
* const ldp = new LdpHttpHandler({store, acl, host, fetch})
1627
* ```
1728
*
1829
* @param options.store {LdpFileStore|LdpMemoryStore|LdpQuadStore} storage backend

lib/api/ldp/ldp-operation.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ class LdpOperation {
5656

5757
writeCommonHeaders ({res, result}) {
5858
// set headers in common to all LDP responses
59-
res.set('Content-Type', result.contentType)
60-
res.set('Content-Length', result.contentLength)
59+
if (result) { // delete operation don't return a result, for example
60+
res.set('Content-Type', result.contentType)
61+
res.set('Content-Length', result.contentLength)
62+
}
6163
}
6264
}
6365

@@ -145,8 +147,8 @@ class LdpGlobOperation extends LdpGetOperation {}
145147
*
146148
* If target is a resource:
147149
* - writes the resource, always overwriting existing contents if any existed
148-
* (since we don't support conditional `If-None-Match` requests)
149-
* - has `mkdir -p` semantics (creates missing container hierarchy)
150+
* (since we currently don't support conditional `If-None-Match` requests)
151+
* - has `mkdir -p` semantics (creates intermediate container hierarchy)
150152
*/
151153
class LdpPutOperation extends LdpOperation {}
152154

lib/storage/ldp/ldp-file-store.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
'use strict'
22

3-
const fs = require('fs-extra')
43
const { LdpFileResource, LdpFileContainer } = require('./ldp-file-resource')
54

65
class LdpFileStore {
76
/**
87
* @param options {object}
98
*
10-
* @param options.root {string} Root fs path for data storage
11-
* @param options.multiuser {boolean}
9+
* @param options.fs {object} Expects the `fs-extra` API
10+
* @param options.mapper {LegacyResourceMapper}
11+
*
1212
* @param options.suffixAcl {string}
1313
* @param options.suffixMeta {string}
14-
* @param options.mapper {LegacyResourceMapper}
1514
* @param options.dataBrowserPath {string}
16-
* @param options.suppressDataBrowser
15+
* @param options.suppressDataBrowser {boolean}
1716
*/
1817
constructor (options) {
19-
this.root = options.root
20-
this.multiuser = options.multiuser
21-
this.suffixAcl = options.suffixAcl
22-
this.suffixMeta = options.suffixMeta
23-
18+
this.fs = options.fs
2419
this.mapper = options.mapper
2520

21+
this.suffixAcl = options.suffixAcl
22+
this.suffixMeta = options.suffixMeta
2623
this.dataBrowserPath = options.dataBrowserPath
2724
this.suppressDataBrowser = options.suppressDataBrowser
2825
}
@@ -34,7 +31,7 @@ class LdpFileStore {
3431
* @throws {Error} When encountering a filesystem error that's not "File does
3532
* not exist", such as `EACCES` etc.
3633
*
37-
* @see https://nodejs.org/api/fs.html#fs_class_fs_stats
34+
* @see https://nodejs.org/api/this.fs.html#fs_class_fs_stats
3835
*
3936
* @returns {Promise<LdpFileResource|LdpFileContainer>}
4037
*/
@@ -46,7 +43,7 @@ class LdpFileStore {
4643

4744
// Try and load file metadata
4845
try {
49-
fsStats = await fs.stat(path)
46+
fsStats = await this.fs.stat(path)
5047
} catch (error) {
5148
if (error.code === 'ENOENT') {
5249
exists = false
@@ -55,12 +52,7 @@ class LdpFileStore {
5552
}
5653
}
5754

58-
let isContainer
59-
if (exists) {
60-
isContainer = fsStats.isDirectory()
61-
} else {
62-
isContainer = target.url.endsWith('/')
63-
}
55+
const isContainer = fsStats ? fsStats.isDirectory() : target.url.endsWith('/')
6456

6557
const encoding = target.charset() // todo: add default charset 'utf8'?
6658

@@ -77,7 +69,7 @@ class LdpFileStore {
7769
async exists (target) {
7870
const resource = await this.resource(target)
7971

80-
return fs.pathExists(resource.filePath)
72+
return this.fs.pathExists(resource.filePath)
8173
}
8274

8375
/**
@@ -107,7 +99,7 @@ class LdpFileStore {
10799
* @returns {Promise}
108100
*/
109101
async loadContainerContents (container) {
110-
container.contents = await fs.readdir(container.path, container.encoding)
102+
container.contents = await this.fs.readdir(container.path, container.encoding)
111103
}
112104

113105
/**
@@ -116,7 +108,7 @@ class LdpFileStore {
116108
* @returns {Promise}
117109
*/
118110
async deleteResource (resource) {
119-
return fs.remove(resource.path)
111+
return this.fs.remove(resource.path)
120112
}
121113

122114
/**
@@ -130,7 +122,7 @@ class LdpFileStore {
130122
* @returns {Promise}
131123
*/
132124
async deleteContainer (container) {
133-
return fs.remove(container.path)
125+
return this.fs.remove(container.path)
134126
}
135127
}
136128

0 commit comments

Comments
 (0)