-
Notifications
You must be signed in to change notification settings - Fork 305
/
Copy pathutils.js
285 lines (259 loc) · 7.52 KB
/
utils.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* eslint-disable node/no-deprecated-api */
module.exports.pathBasename = pathBasename
module.exports.hasSuffix = hasSuffix
module.exports.serialize = serialize
module.exports.translate = translate
module.exports.stringToStream = stringToStream
module.exports.debrack = debrack
module.exports.stripLineEndings = stripLineEndings
module.exports.fullUrlForReq = fullUrlForReq
module.exports.routeResolvedFile = routeResolvedFile
module.exports.getQuota = getQuota
module.exports.overQuota = overQuota
module.exports.getContentType = getContentType
module.exports.parse = parse
module.exports.HTMLDataIsland = HTMLDataIsland
const fs = require('fs')
const path = require('path')
const util = require('util')
const $rdf = require('rdflib')
const from = require('from2')
const url = require('url')
const debug = require('./debug').fs
const getSize = require('get-folder-size')
const ns = require('solid-namespace')($rdf)
/**
* Returns a fully qualified URL from an Express.js Request object.
* (It's insane that Express does not provide this natively.)
*
* Usage:
*
* ```
* console.log(util.fullUrlForReq(req))
* // -> https://example.com/path/to/resource?q1=v1
* ```
*
* @param req {IncomingRequest}
*
* @return {string}
*/
function fullUrlForReq (req) {
const fullUrl = url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: url.resolve(req.baseUrl, req.path),
query: req.query
})
return fullUrl
}
/**
* Removes the `<` and `>` brackets around a string and returns it.
* Used by the `allow` handler in `verifyDelegator()` logic.
* @method debrack
*
* @param s {string}
*
* @return {string}
*/
function debrack (s) {
if (!s || s.length < 2) {
return s
}
if (s[0] !== '<') {
return s
}
if (s[s.length - 1] !== '>') {
return s
}
return s.substring(1, s.length - 1)
}
async function parse (data, baseUri, contentType) {
const graph = $rdf.graph()
return new Promise((resolve, reject) => {
try {
return $rdf.parse(data, graph, baseUri, contentType, (err, str) => {
if (err) {
return reject(err)
}
resolve(str)
})
} catch (err) {
return reject(err)
}
})
}
function pathBasename (fullpath) {
let bname = ''
if (fullpath) {
bname = (fullpath.lastIndexOf('/') === fullpath.length - 1)
? ''
: path.basename(fullpath)
}
return bname
}
function hasSuffix (path, suffixes) {
for (const i in suffixes) {
if (path.indexOf(suffixes[i], path.length - suffixes[i].length) !== -1) {
return true
}
}
return false
}
function serialize (graph, baseUri, contentType) {
return new Promise((resolve, reject) => {
try {
// target, kb, base, contentType, callback
$rdf.serialize(null, graph, baseUri, contentType, function (err, result) {
if (err) {
return reject(err)
}
if (result === undefined) {
return reject(new Error('Error serializing the graph to ' +
contentType))
}
resolve(result)
})
} catch (err) {
reject(err)
}
})
}
function HTMLDataIsland (data) {
let from = ''
let dataScript = ''
const scripts = data.split('</script')
if (scripts && scripts.length) {
for (let script of scripts) {
script = '<script' + script.split('<script')[1] + '</script>'
const RDFType = ['text/turtle', 'text/n3', 'application/ld+json', 'application/rdf+xml']
const contentType = RDFType.find(type => script.includes(`type="${type}"`))
if (contentType) {
dataScript = script // .replace(/^<script(.*?)>/gms, '').replace(/<\/script>$/gms, '')
from = contentType
break
}
}
}
return [dataScript, from]
}
function translate (stream, baseUri, from, to) {
return new Promise((resolve, reject) => {
let data = ''
stream
.on('data', function (chunk) {
data += chunk
})
.on('end', function () {
// check for HTML data island
let dataScript = ''
if (from === 'text/html') {
[dataScript, from] = HTMLDataIsland(data)
data = dataScript.replace(/^<script(.*?)>/gms, '').replace(/<\/script>$/gms, '')
if (!from) {
reject(new Error(404, 'data island do not exist'))
}
}
if (from === 'text/html') return resolve(data)
// parse 'from', serialize 'to'
const graph = $rdf.graph()
$rdf.parse(data, graph, baseUri, from, function (err) {
if (err) return reject(err)
resolve(serialize(graph, baseUri, to))
})
})
})
}
function stringToStream (string) {
return from(function (size, next) {
// if there's no more content
// left in the string, close the stream.
if (!string || string.length <= 0) {
return next(null, null)
}
// Pull in a new chunk of text,
// removing it from the string.
const chunk = string.slice(0, size)
string = string.slice(size)
// Emit "chunk" from the stream.
next(null, chunk)
})
}
/**
* Removes line endings from a given string. Used for WebID TLS Certificate
* generation.
*
* @param obj {string}
*
* @return {string}
*/
function stripLineEndings (obj) {
if (!obj) { return obj }
return obj.replace(/(\r\n|\n|\r)/gms, '')
}
/**
* Adds a route that serves a static file from another Node module
*/
function routeResolvedFile (router, path, file, appendFileName = true) {
const fullPath = appendFileName ? path + file.match(/[^/]+$/) : path
const fullFile = require.resolve(file)
router.get(fullPath, (req, res) => res.sendFile(fullFile))
}
/**
* Returns the number of bytes that the user owning the requested POD
* may store or Infinity if no limit
*/
async function getQuota (root, serverUri) {
const filename = path.join(root, 'settings/serverSide.ttl')
let prefs
try {
prefs = await _asyncReadfile(filename)
} catch (error) {
debug('Setting no quota. While reading serverSide.ttl, got ' + error)
return Infinity
}
const graph = $rdf.graph()
const storageUri = serverUri + '/'
try {
$rdf.parse(prefs, graph, storageUri, 'text/turtle')
} catch (error) {
throw new Error('Failed to parse serverSide.ttl, got ' + error)
}
return Number(graph.anyValue($rdf.sym(storageUri), ns.solid('storageQuota'))) || Infinity
}
/**
* Returns true of the user has already exceeded their quota, i.e. it
* will check if new requests should be rejected, which means they
* could PUT a large file and get away with it.
*/
async function overQuota (root, serverUri) {
const quota = await getQuota(root, serverUri)
if (quota === Infinity) {
return false
}
// TODO: cache this value?
const size = await actualSize(root)
return (size > quota)
}
/**
* Returns the number of bytes that is occupied by the actual files in
* the file system. IMPORTANT NOTE: Since it traverses the directory
* to find the actual file sizes, this does a costly operation, but
* neglible for the small quotas we currently allow. If the quotas
* grow bigger, this will significantly reduce write performance, and
* so it needs to be rewritten.
*/
function actualSize (root) {
return util.promisify(getSize)(root)
}
function _asyncReadfile (filename) {
return util.promisify(fs.readFile)(filename, 'utf-8')
}
/**
* Get the content type from a headers object
* @param headers An Express or Fetch API headers object
* @return {string} A content type string
*/
function getContentType (headers) {
const value = headers.get ? headers.get('content-type') : headers['content-type']
return value ? value.replace(/;.*/, '') : ''
}