Skip to content

Commit 3947c2d

Browse files
author
Andrei
committed
Merge pull request #58 from nicola/no_uriBase
Removing uriBase/uri
2 parents 30dc1b5 + d3bf311 commit 3947c2d

12 files changed

Lines changed: 131 additions & 105 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ npm install
3030
```javascript
3131
var ldnode = require('ldnode')
3232

33-
var ldp = ldnode.createServer({
34-
uri: "http://example.com/test/",
35-
base: __dirname + '/test/'
36-
})
33+
var ldp = ldnode.createServer()
3734
ldp.listen(1234, function() {
3835
// Started Linked Data Platform
3936
})
@@ -46,7 +43,7 @@ You can integrate it with your existing express app
4643
```javascript
4744
var ldnode = require('ldnode')
4845
var app = require('express')()
49-
app.use('/test', ldnode(opts))
46+
app.use('/test', ldnode())
5047
...
5148
```
5249

@@ -76,18 +73,19 @@ Usage: ldnode [options]
7673
Options:
7774
-v, --verbose Print the logs to console
7875
--version Print current ldnode version
79-
-u, --uri Default address of the server (e.g. http[s]://host:port/path)
76+
-m, --mount Where to mount Linked Data Platform (default: '/')
8077
-b, --base Base location to serve resources
8178
-p, --port Port to use
8279
-c, --cache Set cache time (in seconds), 0 for no cache
8380
-K, --key Path to the ssl key
8481
-C, --cert Path to the ssl cert
8582
--webid Enable WebID+TLS authentication
8683
-s, --secret HTTP Session secret key (e.g. "your secret phrase")
87-
--no-live Disable live support through WebSockets
84+
--no-live Disable live support through WebSockets
8885
-sA, --suffix-acl Suffix for acl files (default: '.acl')
8986
-sC, --suffix-changes Suffix for acl files (default: '.changes')
9087
-sE, --suffix-sse Suffix for SSE files (default: '.events')
88+
9189
```
9290

9391
## Tests

‎acl.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ function allow(mode, req, res) {
3535
for (var i = 0; i < depth.length; i++) {
3636
var pathAcl = S(filepath).endsWith(options.suffixAcl) ?
3737
filepath : filepath + options.suffixAcl;
38-
var pathUri = file.filenameToBaseUri(filepath, options.uri, options.base);
38+
var uri = file.uriAbs(req);
39+
var pathUri = file.filenameToBaseUri(filepath, uri, options.base);
3940
relativePath = path.relative(options.base, filepath);
4041

4142
debug("Checking " + accessType + "<" + mode + "> to " +

‎bin/ldnode.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ var argv = require('nomnom')
1919
});
2020
}
2121
})
22-
.option('uri', {
23-
abbr: 'u',
24-
help: 'Default address of the server (e.g. http[s]://host:port/path)'
22+
.option('mount', {
23+
abbr: 'm',
24+
help: 'Where to mount Linked Data Platform (default: \'/\')'
2525
})
2626
.option('base', {
2727
abbr: 'b',

‎container.js‎

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,49 @@ var addUriTriple = function(kb, s, o, p) {
1818

1919
var turtleExtension = ".ttl";
2020

21-
function createRootContainer(options) {
22-
if (!metadata.hasContainerMetadata(options.base)) {
23-
var rootContainer = $rdf.graph();
24-
addUriTriple(rootContainer, options.pathStart,
25-
rdfVocab.type, ldpVocab.Container);
26-
addUriTriple(rootContainer, options.pathStart,
27-
rdfVocab.type, ldpVocab.BasicContainer);
28-
rootContainer.add(rootContainer.sym(options.pathStart),
29-
rootContainer.sym('http://purl.org/dc/terms/title'),
30-
'"Root Container"');
31-
var serializedContainer = $rdf.serialize(undefined, rootContainer,
32-
options.pathStart, 'text/turtle');
33-
metadata.writeContainerMetadata(options.base,
34-
serializedContainer, function(err) {
35-
if (err) {
36-
//TODO handle error
37-
debug("Could not write root container");
38-
} else {
39-
debug("Wrote root container to " + options.base);
40-
}
41-
});
21+
function createRootContainer(fileBase, baseUrl) {
22+
if (metadata.hasContainerMetadata(fileBase)) {
23+
return;
4224
}
25+
26+
var rootContainer = $rdf.graph();
27+
addUriTriple(
28+
rootContainer,
29+
baseUrl,
30+
rdfVocab.type,
31+
ldpVocab.Container);
32+
addUriTriple(
33+
rootContainer,
34+
baseUrl,
35+
rdfVocab.type,
36+
ldpVocab.BasicContainer);
37+
38+
rootContainer.add(
39+
rootContainer.sym(baseUrl),
40+
rootContainer.sym('http://purl.org/dc/terms/title'),
41+
'"Root Container"');
42+
43+
var serializedContainer = $rdf.serialize(
44+
undefined,
45+
rootContainer,
46+
baseUrl,
47+
'text/turtle');
48+
49+
metadata.writeContainerMetadata(
50+
fileBase,
51+
serializedContainer,
52+
function(err) {
53+
if (err) {
54+
//TODO handle error
55+
debug("Could not write root container");
56+
} else {
57+
debug("Wrote root container to " + fileBase);
58+
}
59+
});
60+
4361
}
4462

45-
function createResourceUri(options, containerURI, slug, isBasicContainer) {
63+
function createResourceUri(usedURIs, containerURI, slug, isBasicContainer) {
4664
var newPath;
4765
if (slug) {
4866
if (S(slug).endsWith(turtleExtension)) {
@@ -61,65 +79,81 @@ function createResourceUri(options, containerURI, slug, isBasicContainer) {
6179
newPath = path.join(containerURI, uuid.v1() + turtleExtension);
6280
}
6381
}
64-
if (!(fs.existsSync(newPath) || containerURI in options.usedURIs)) {
65-
options.usedURIs[newPath] = true;
82+
if (!(fs.existsSync(newPath) || containerURI in usedURIs)) {
83+
usedURIs[newPath] = true;
6684
} else {
6785
return null;
6886
}
6987
return newPath;
7088
}
7189

72-
function releaseResourceUri(options, uri) {
73-
delete options.usedURIs[uri];
90+
function releaseResourceUri(usedURIs, uri) {
91+
delete usedURIs[uri];
7492
}
7593

76-
function createNewResource(options, resourcePath, resourceGraph, callback) {
77-
var resourceURI = path.relative(options.base, resourcePath);
94+
function createNewResource(usedURIs, fileBase, uri, resourcePath, resourceGraph, callback) {
95+
var resourceURI = path.relative(fileBase, resourcePath);
7896
//TODO write files with relative URIS.
79-
var rawResource = $rdf.serialize(undefined,
80-
resourceGraph, options.uri + resourceURI, 'text/turtle');
97+
var rawResource = $rdf.serialize(
98+
undefined,
99+
resourceGraph,
100+
uri + resourceURI,
101+
'text/turtle');
102+
81103
debug("Writing new resource to " + resourcePath);
82104
debug("Resource:\n" + rawResource);
83-
fs.writeFile(resourcePath, rawResource, writeResourceCallback);
105+
106+
fs.writeFile(
107+
resourcePath,
108+
rawResource,
109+
writeResourceCallback);
84110

85111
function writeResourceCallback(err) {
86112
if (err) {
87113
debug("Error writing resource: " + err);
88-
module.exports.releaseResourceUri(options, resourcePath);
89-
callback(err);
90-
} else {
114+
releaseResourceUri(usedURIs, resourcePath);
91115
return callback(err);
92116
}
117+
118+
return callback(err);
93119
}
94120
}
95121

96-
function createNewContainer(options, containerPath, containerGraph, callback) {
122+
function createNewContainer(usedURIs, uri, containerPath, containerGraph, callback) {
97123
fs.mkdir(containerPath, mkdirCallback);
98124

99125
function mkdirCallback(err) {
100126
if (err) {
101127
debug("Error creating directory for new container: " + err);
102-
module.exports.releaseResourceUri(options, containerPath);
128+
releaseResourceUri(usedURIs, containerPath);
103129
return callback(err);
104-
} else {
105-
var rawContainer = $rdf.serialize(undefined, containerGraph,
106-
options.uri, 'text/turtle');
107-
debug("rawContainer " + rawContainer);
108-
metadata.writeContainerMetadata(containerPath, rawContainer,
109-
writeContainerCallback);
110130
}
131+
132+
var rawContainer = $rdf.serialize(
133+
undefined,
134+
containerGraph,
135+
uri,
136+
'text/turtle');
137+
138+
debug("rawContainer " + rawContainer);
139+
140+
metadata.writeContainerMetadata(
141+
containerPath,
142+
rawContainer,
143+
writeContainerCallback);
111144
}
112145

113146
function writeContainerCallback(err) {
114147
if (err) {
115148
debug("Error writing container: " + err);
116-
module.exports.releaseResourceUri(options, containerPath);
117-
return callback(err);
118-
} else {
119-
debug("Wrote container to " + containerPath);
120-
module.exports.releaseResourceUri(options, containerPath);
149+
releaseResourceUri(usedURIs, containerPath);
121150
return callback(err);
122151
}
152+
153+
debug("Wrote container to " + containerPath);
154+
releaseResourceUri(usedURIs, containerPath);
155+
return callback(err);
156+
123157
}
124158
}
125159

‎examples/simple-ldp-server.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ app.get('/', function (req, res) {
1111
})
1212

1313
// Mounting ldnode on /ldp
14-
var ldp = ldnode({
15-
uri: 'http://localhost:3000/ldp'
16-
})
14+
var ldp = ldnode()
1715
app.use('/ldp', ldp)
1816

1917
// Starting server

‎fileStore.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ function uriToRelativeFilename(uri, base) {
2727

2828
function filenameToBaseUri(filename, uri, base) {
2929
var uriPath = S(filename).strip(base).toString();
30-
return uri + uriPath;
30+
return uri + '/' + uriPath;
31+
}
32+
33+
function uriBase(req) {
34+
return req.protocol + '://' + req.get('host') + (req.baseUrl || '');
3135
}
3236

3337
exports.uriToFilename = uriToFilename;
3438
exports.uriToRelativeFilename = uriToRelativeFilename;
3539
exports.filenameToBaseUri = filenameToBaseUri;
40+
exports.uriAbs = uriBase;

‎handlers/get.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var metaExtension = '.meta';
2222

2323
function get(req, res, includeBody) {
2424
var options = req.app.locals.ldp;
25+
var uri = file.uriAbs(req);
2526

2627
// Add request to subscription service
2728
if (('' + req.path).slice(-options.suffixChanges.length) ===
@@ -118,7 +119,7 @@ function get(req, res, includeBody) {
118119
try {
119120
var fileData = fs.readFileSync(match,
120121
{encoding: "utf8"});
121-
var baseUri = file.filenameToBaseUri(match, options.uri, options.base);
122+
var baseUri = file.filenameToBaseUri(match, uri, options.base);
122123
//TODO integrate ACL
123124
if (S(match).endsWith(".ttl") && aclAllow(match)) {
124125
$rdf.parse(fileData, globGraph, baseUri,
@@ -153,7 +154,7 @@ function get(req, res, includeBody) {
153154

154155
function parseLinkedData(turtleData) {
155156
var accept = header.parseAcceptHeader(req);
156-
var baseUri = file.filenameToBaseUri(filename, options.uri, options.base);
157+
var baseUri = file.filenameToBaseUri(filename, uri, options.base);
157158

158159
// Handle Turtle Accept header
159160
if (accept === undefined || accept === null) {
@@ -190,7 +191,7 @@ function get(req, res, includeBody) {
190191

191192
function parseContainer(containerData) {
192193
//Handle other file types
193-
var baseUri = file.filenameToBaseUri(filename, options.uri, options.base);
194+
var baseUri = file.filenameToBaseUri(filename, uri, options.base);
194195
var resourceGraph = $rdf.graph();
195196
try {
196197
$rdf.parse(containerData, resourceGraph, baseUri, 'text/turtle');

‎handlers/post.js‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,30 @@ function handler(req, res) {
6868
resourceMetadata.isBasicContainer);
6969

7070
if (resourcePath === null) {
71-
container.releaseResourceUri(options, resourcePath);
71+
container.releaseResourceUri(options.usedURIs, resourcePath);
7272
debug("POST -- URI already exists or in use");
7373
return res.sendStatus(400);
7474
}
75-
var resourceGraph = $rdf.graph();
7675

76+
var resourceGraph = $rdf.graph();
7777
// Get the request text
7878
// TODO make sure correct text is selected
7979
var requestText = req.convertedText || req.text;
80+
var uri = file.uriAbs(req);
81+
var resourceBaseUri = file.filenameToBaseUri(
82+
resourcePath,
83+
uri,
84+
options.base);
8085

81-
var resourceBaseUri;
8286
try {
83-
resourceBaseUri = file.filenameToBaseUri(
84-
resourcePath,
85-
options.uri,
86-
options.base);
87-
8887
$rdf.parse(
8988
requestText,
9089
resourceGraph,
9190
resourceBaseUri,
92-
'text/turtle');
91+
contentType);
9392
} catch (parseErr) {
9493
debug("POST -- Error parsing resource: " + parseErr);
95-
container.releaseResourceUri(options, resourcePath);
94+
container.releaseResourceUri(options.usedURIs, resourcePath);
9695
return res.sendStatus(400);
9796
}
9897

@@ -102,13 +101,16 @@ function handler(req, res) {
102101
resourcePath += '/';
103102
resourceBaseUri += '/';
104103
container.createNewContainer(
105-
options,
104+
options.usedURIs,
105+
uri,
106106
resourcePath,
107107
resourceGraph,
108108
containerCallback);
109109
} else {
110110
container.createNewResource(
111-
options,
111+
options.usedURIs,
112+
options.base,
113+
uri,
112114
resourcePath,
113115
resourceGraph,
114116
resourceCallback);

0 commit comments

Comments
 (0)