This repository has been archived by the owner on May 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlite.js
166 lines (132 loc) · 4.67 KB
/
lite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var rdf = require('rdf-ext')
var inherits = require('inherits')
var AbstractStore = require('rdf-store-abstract')
function httpSuccess (statusCode) {
return (statusCode >= 200 && statusCode < 300)
}
function LdpStore (options) {
options = options || {}
this.parsers = options.parsers ? new rdf.Parsers(options.parsers) : rdf.parsers
this.serializers = options.serializers ? new rdf.Serializers(options.serializers) : rdf.serializers
this.defaultParser = options.defaultParser || 'text/turtle'
this.defaultSerializer = options.defaultSerializer || 'text/turtle'
this.defaultPatchSerializer = options.defaultPatchSerializer || options.defaultSerializer || 'text/turtle'
this.request = options.request || rdf.defaultRequest
}
inherits(LdpStore, AbstractStore)
LdpStore.prototype.add = function (iri, graph, callback, options) {
var self = this
return new Promise(function (resolve, reject) {
callback = callback || function () {}
options = options || {}
var method = 'PUT'
var contentType = self.defaultSerializer
var headers = {}
headers['Content-Type'] = contentType
if (options.method) {
method = options.method
}
if (options.etag) {
headers['If-Match'] = options.etag
}
if (options.useEtag && graph.etag) {
headers['If-Match'] = graph.etag
}
self.serializers.serialize(contentType, graph).then(function (data) {
return self.request(method, iri, headers, data, null, options).then(function (res) {
if (!httpSuccess(res.statusCode)) {
callback('status code error: ' + res.statusCode)
return Promise.reject('status code error: ' + res.statusCode)
}
callback(null, graph)
resolve(graph)
})
}).catch(function (error) {
callback(error)
reject(error)
})
})
}
LdpStore.prototype.delete = function (iri, callback, options) {
var self = this
return new Promise(function (resolve, reject) {
callback = callback || function () {}
self.request('DELETE', iri, {}, null, null, options).then(function (res) {
if (!httpSuccess(res.statusCode)) {
callback('status code error: ' + res.statusCode)
return Promise.reject('status code error: ' + res.statusCode)
}
callback()
resolve()
}).catch(function (error) {
callback(error)
reject(error)
})
})
}
LdpStore.prototype.graph = function (iri, callback, options) {
var self = this
return new Promise(function (resolve, reject) {
callback = callback || function () {}
options = options || {}
self.request('GET', iri, {'Accept': self.parsers.list().join(', ')}, null, null, options).then(function (res) {
// also test for status code != 0 for local browser requests
if (!httpSuccess(res.statusCode) && res.statusCode !== 0) {
callback('status code error: ' + res.statusCode)
return Promise.reject('status code error: ' + res.statusCode)
}
var contentType
if (options.contentType) {
contentType = options.contentType
} else {
if ('content-type' in res.headers) {
contentType = res.headers['content-type'].split(';')[0]
}
if (!contentType || !(contentType in self.parsers)) {
contentType = self.defaultParser
}
}
return self.parsers.parse(contentType, res.content, null, iri).then(function (graph) {
// copy etag header to Graph object
if (options.useEtag && 'etag' in res.headers) {
graph.etag = res.headers.etag
}
callback(null, graph)
resolve(graph)
})
}).catch(function (error) {
callback(error)
reject(error)
})
})
}
LdpStore.prototype.merge = function (iri, graph, callback, options) {
var self = this
return new Promise(function (resolve, reject) {
var contentType = self.defaultPatchSerializer
var headers = {}
callback = callback || function () {}
options = options || {}
headers['Content-Type'] = contentType
if ('etag' in options) {
headers['If-Match'] = options.etag
}
if ('useEtag' in options && options.useEtag && 'etag' in graph) {
headers['If-Match'] = graph.etag
}
self.serializers.serialize(contentType, graph).then(function (data) {
return self.request('PATCH', iri, headers, data, null, options).then(function (res) {
if (!httpSuccess(res.statusCode)) {
callback('status code error: ' + res.statusCode)
return Promise.reject('status code error: ' + res.statusCode)
}
callback(null, graph)
resolve(graph)
})
}).catch(function (error) {
callback(error)
reject(error)
})
})
}
module.exports = LdpStore