feat: Add Flag to disable PREP · solid-server/node-solid-server@b0e7eb0 · GitHub
Skip to content

Commit b0e7eb0

Browse files
committed
feat: Add Flag to disable PREP
PREP can be disabled when the server is started with the `--no-prep` flag.
1 parent bda034a commit b0e7eb0

10 files changed

Lines changed: 45 additions & 21 deletions

File tree

README.md

Lines changed: 6 additions & 5 deletions

bin/lib/options.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ module.exports = [
143143
flag: true,
144144
default: false
145145
},
146+
{
147+
name: 'no-prep',
148+
help: 'Disable Per Resource Events',
149+
flag: true,
150+
default: false
151+
},
146152
// {
147153
// full: 'default-app',
148154
// help: 'URI to use as a default app for resources (default: https://linkeddata.github.io/warp/#/list/)'

lib/create-app.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ function createApp (argv = {}) {
6767
const app = express()
6868

6969
// Add PREP support
70-
app.use(acceptEvents, events, eventID, prep)
70+
if (argv.prep) {
71+
app.use(eventID)
72+
app.use(acceptEvents, events, prep)
73+
}
7174

7275
initAppLocals(app, argv, ldp)
7376
initHeaders(app)
@@ -123,7 +126,7 @@ function createApp (argv = {}) {
123126
}
124127

125128
// Attach the LDP middleware
126-
app.use('/', LdpMiddleware(corsSettings))
129+
app.use('/', LdpMiddleware(corsSettings, argv.prep))
127130

128131
// https://stackoverflow.com/questions/51741383/nodejs-express-return-405-for-un-supported-method
129132
app.use(function (req, res, next) {
@@ -176,6 +179,7 @@ function initAppLocals (app, argv, ldp) {
176179
app.locals.enforceToc = argv.enforceToc
177180
app.locals.tocUri = argv.tocUri
178181
app.locals.disablePasswordChecks = argv.disablePasswordChecks
182+
app.locals.prep = argv.prep
179183

180184
if (argv.email && argv.email.host) {
181185
app.locals.emailService = new EmailService(argv.templates.email, argv.email)
@@ -295,7 +299,7 @@ function initWebId (argv, app, ldp) {
295299
initAuthentication(app, argv)
296300

297301
if (argv.multiuser) {
298-
app.use(vhost('*', LdpMiddleware(corsSettings)))
302+
app.use(vhost('*', LdpMiddleware(corsSettings, argv.prep)))
299303
}
300304
}
301305

lib/handlers/delete.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ async function handler (req, res, next) {
66
debug('DELETE -- Request on' + req.originalUrl)
77

88
const ldp = req.app.locals.ldp
9+
const prep = req.app.locals.prep
910
try {
1011
await ldp.delete(req)
1112
debug('DELETE -- Ok.')
1213
// Add event-id for notifications
13-
res.setHeader('Event-ID', res.setEventID())
14+
prep && res.setHeader('Event-ID', res.setEventID())
1415
res.sendStatus(200)
1516
next()
1617
} catch (err) {

lib/handlers/get.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const prepConfig = 'accept=("message/rfc822" "application/ld+json" "text/turtle"
2323

2424
async function handler (req, res, next) {
2525
const ldp = req.app.locals.ldp
26+
const prep = req.app.locals.prep
2627
const includeBody = req.method === 'GET'
2728
const negotiator = new Negotiator(req)
2829
const baseUri = ldp.resourceMapper.resolveUrl(req.hostname, req.path)
@@ -138,7 +139,7 @@ async function handler (req, res, next) {
138139
res.statusCode = 206
139140
}
140141

141-
if (isRdf(contentType) && !res.sendEvents({
142+
if (prep & isRdf(contentType) && !res.sendEvents({
142143
config: { prep: prepConfig },
143144
body: stream,
144145
isBodyStream: true,
@@ -160,7 +161,7 @@ async function handler (req, res, next) {
160161
const headers = {
161162
'Content-Type': possibleRDFType
162163
}
163-
if (isRdf(contentType) && !res.sendEvents({
164+
if (prep && isRdf(contentType) && !res.sendEvents({
164165
config: { prep: prepConfig },
165166
body: data,
166167
headers

lib/handlers/patch.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function contentForNew (contentType) {
3939
// Handles a PATCH request
4040
async function patchHandler (req, res, next) {
4141
debug(`PATCH -- ${req.originalUrl}`)
42+
const prep = req.app.locals.prep
4243
try {
4344
// Obtain details of the target resource
4445
const ldp = req.app.locals.ldp
@@ -92,7 +93,7 @@ async function patchHandler (req, res, next) {
9293
})
9394

9495
// Add event-id for notifications
95-
res.setHeader('Event-ID', res.setEventID())
96+
prep && res.setHeader('Event-ID', res.setEventID())
9697
// Send the status and result to the client
9798
res.status(resourceExists ? 200 : 201)
9899
res.send(result)

lib/handlers/post.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const getContentType = require('../utils').getContentType
1111

1212
async function handler (req, res, next) {
1313
const ldp = req.app.locals.ldp
14+
const prep = req.app.locals.prep
1415
const contentType = getContentType(req.headers)
1516
debug('content-type is ', contentType)
1617
// Handle SPARQL(-update?) query
@@ -73,7 +74,7 @@ async function handler (req, res, next) {
7374
busboy.on('finish', function () {
7475
debug('Done storing files')
7576
// Add event-id for notifications
76-
res.setHeader('Event-ID', res.setEventID())
77+
prep && res.setHeader('Event-ID', res.setEventID())
7778
res.sendStatus(200)
7879
next()
7980
})
@@ -94,7 +95,7 @@ async function handler (req, res, next) {
9495
header.addLinks(res, links)
9596
res.set('Location', resourcePath)
9697
// Add event-id for notifications
97-
res.setHeader('Event-ID', res.setEventID())
98+
prep && res.setHeader('Event-ID', res.setEventID())
9899
res.sendStatus(201)
99100
next()
100101
},

lib/handlers/put.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ async function checkPermission (request, resourceExists) {
5959
// TODO could be renamed as putResource (it now covers container and non-container)
6060
async function putStream (req, res, next, stream = req) {
6161
const ldp = req.app.locals.ldp
62+
const prep = req.app.locals.prep
6263
// try {
6364
// Obtain details of the target resource
6465
let resourceExists = true
@@ -78,7 +79,7 @@ async function putStream (req, res, next, stream = req) {
7879
if (!req.originalUrl.endsWith('.acl')) await checkPermission(req, resourceExists)
7980
await ldp.put(req, stream, getContentType(req.headers))
8081
// Add event-id for notifications
81-
res.setHeader('Event-ID', res.setEventID())
82+
prep && res.setHeader('Event-ID', res.setEventID())
8283
res.sendStatus(resourceExists ? 204 : 201)
8384
return next()
8485
} catch (err) {

lib/ldp-middleware.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const index = require('./handlers/index')
1212
const copy = require('./handlers/copy')
1313
const notify = require('./handlers/notify')
1414

15-
function LdpMiddleware (corsSettings) {
15+
function LdpMiddleware (corsSettings, prep) {
1616
const router = express.Router('/')
1717

1818
// Add Link headers
@@ -24,10 +24,17 @@ function LdpMiddleware (corsSettings) {
2424

2525
router.copy('/*', allow('Write'), copy)
2626
router.get('/*', index, allow('Read'), header.addPermissions, get)
27-
router.post('/*', allow('Append'), post, notify)
28-
router.patch('/*', allow('Append'), patch, notify)
29-
router.put('/*', allow('Append'), put, notify)
30-
router.delete('/*', allow('Write'), del, notify)
27+
router.post('/*', allow('Append'), post)
28+
router.patch('/*', allow('Append'), patch)
29+
router.put('/*', allow('Append'), put)
30+
router.delete('/*', allow('Write'), del)
31+
32+
if (prep) {
33+
router.post('/*', notify)
34+
router.patch('/*', notify)
35+
router.put('/*', notify)
36+
router.delete('/*', notify)
37+
}
3138

3239
return router
3340
}

test/integration/prep-test.js

Lines changed: 2 additions & 1 deletion

0 commit comments

Comments
 (0)