Skip to content

Commit

Permalink
feat: Add Flag to disable PREP
Browse files Browse the repository at this point in the history
PREP can be disabled when the server is started with the `--no-prep` flag.
  • Loading branch information
CxRes committed Oct 10, 2024
1 parent bda034a commit b0e7eb0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 21 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Your users will have a dedicated folder under `./data` at `./data/<username>.<yo
> To use Gmail you may need to configure ["Allow Less Secure Apps"](https://www.google.com/settings/security/lesssecureapps) in your Gmail account unless you are using 2FA in which case you would have to create an [Application Specific](https://security.google.com/settings/security/apppasswords) password. You also may need to unlock your account with ["Allow access to your Google account"](https://accounts.google.com/DisplayUnlockCaptcha) to use SMTP.
also add to `config.json`
```
```
"useEmail": true,
"emailHost": "smtp.gmail.com",
"emailPort": "465",
Expand Down Expand Up @@ -206,6 +206,7 @@ $ solid start --help
--multiuser Enable multi-user mode
--idp [value] Obsolete; use --multiuser
--no-live Disable live support through WebSockets
--no-prep Disable Per Resource Events
--proxy [value] Obsolete; use --corsProxy
--cors-proxy [value] Serve the CORS proxy on this path
--suppress-data-browser Suppress provision of a data browser
Expand Down Expand Up @@ -271,7 +272,7 @@ docker run -p 8443:8443 --name solid node-solid-server


This will enable you to login to solid on https://localhost:8443 and then create a new account
but not yet use that account. After a new account is made you will need to create an entry for
but not yet use that account. After a new account is made you will need to create an entry for
it in your local (/etc/)hosts file in line with the account and subdomain, i.e. --

```pre
Expand All @@ -280,16 +281,16 @@ it in your local (/etc/)hosts file in line with the account and subdomain, i.e.

You can modify the config within the docker container as follows:

- Copy the `config.json` to the current directory with:
- Copy the `config.json` to the current directory with:
```bash
docker cp solid:/usr/src/app/config.json .
```
- Edit the `config.json` file
- Copy the file back with
- Copy the file back with
```bash
docker cp config.json solid:/usr/src/app/
```
- Restart the server with
- Restart the server with
```bash
docker restart solid
```
Expand Down
6 changes: 6 additions & 0 deletions bin/lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ module.exports = [
flag: true,
default: false
},
{
name: 'no-prep',
help: 'Disable Per Resource Events',
flag: true,
default: false
},
// {
// full: 'default-app',
// help: 'URI to use as a default app for resources (default: https://linkeddata.github.io/warp/#/list/)'
Expand Down
10 changes: 7 additions & 3 deletions lib/create-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ function createApp (argv = {}) {
const app = express()

// Add PREP support
app.use(acceptEvents, events, eventID, prep)
if (argv.prep) {
app.use(eventID)
app.use(acceptEvents, events, prep)
}

initAppLocals(app, argv, ldp)
initHeaders(app)
Expand Down Expand Up @@ -123,7 +126,7 @@ function createApp (argv = {}) {
}

// Attach the LDP middleware
app.use('/', LdpMiddleware(corsSettings))
app.use('/', LdpMiddleware(corsSettings, argv.prep))

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

if (argv.email && argv.email.host) {
app.locals.emailService = new EmailService(argv.templates.email, argv.email)
Expand Down Expand Up @@ -295,7 +299,7 @@ function initWebId (argv, app, ldp) {
initAuthentication(app, argv)

if (argv.multiuser) {
app.use(vhost('*', LdpMiddleware(corsSettings)))
app.use(vhost('*', LdpMiddleware(corsSettings, argv.prep)))
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ async function handler (req, res, next) {
debug('DELETE -- Request on' + req.originalUrl)

const ldp = req.app.locals.ldp
const prep = req.app.locals.prep
try {
await ldp.delete(req)
debug('DELETE -- Ok.')
// Add event-id for notifications
res.setHeader('Event-ID', res.setEventID())
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(200)
next()
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions lib/handlers/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const prepConfig = 'accept=("message/rfc822" "application/ld+json" "text/turtle"

async function handler (req, res, next) {
const ldp = req.app.locals.ldp
const prep = req.app.locals.prep
const includeBody = req.method === 'GET'
const negotiator = new Negotiator(req)
const baseUri = ldp.resourceMapper.resolveUrl(req.hostname, req.path)
Expand Down Expand Up @@ -138,7 +139,7 @@ async function handler (req, res, next) {
res.statusCode = 206
}

if (isRdf(contentType) && !res.sendEvents({
if (prep & isRdf(contentType) && !res.sendEvents({
config: { prep: prepConfig },
body: stream,
isBodyStream: true,
Expand All @@ -160,7 +161,7 @@ async function handler (req, res, next) {
const headers = {
'Content-Type': possibleRDFType
}
if (isRdf(contentType) && !res.sendEvents({
if (prep && isRdf(contentType) && !res.sendEvents({
config: { prep: prepConfig },
body: data,
headers
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function contentForNew (contentType) {
// Handles a PATCH request
async function patchHandler (req, res, next) {
debug(`PATCH -- ${req.originalUrl}`)
const prep = req.app.locals.prep
try {
// Obtain details of the target resource
const ldp = req.app.locals.ldp
Expand Down Expand Up @@ -92,7 +93,7 @@ async function patchHandler (req, res, next) {
})

// Add event-id for notifications
res.setHeader('Event-ID', res.setEventID())
prep && res.setHeader('Event-ID', res.setEventID())
// Send the status and result to the client
res.status(resourceExists ? 200 : 201)
res.send(result)
Expand Down
5 changes: 3 additions & 2 deletions lib/handlers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const getContentType = require('../utils').getContentType

async function handler (req, res, next) {
const ldp = req.app.locals.ldp
const prep = req.app.locals.prep
const contentType = getContentType(req.headers)
debug('content-type is ', contentType)
// Handle SPARQL(-update?) query
Expand Down Expand Up @@ -73,7 +74,7 @@ async function handler (req, res, next) {
busboy.on('finish', function () {
debug('Done storing files')
// Add event-id for notifications
res.setHeader('Event-ID', res.setEventID())
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(200)
next()
})
Expand All @@ -94,7 +95,7 @@ async function handler (req, res, next) {
header.addLinks(res, links)
res.set('Location', resourcePath)
// Add event-id for notifications
res.setHeader('Event-ID', res.setEventID())
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(201)
next()
},
Expand Down
3 changes: 2 additions & 1 deletion lib/handlers/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async function checkPermission (request, resourceExists) {
// TODO could be renamed as putResource (it now covers container and non-container)
async function putStream (req, res, next, stream = req) {
const ldp = req.app.locals.ldp
const prep = req.app.locals.prep
// try {
// Obtain details of the target resource
let resourceExists = true
Expand All @@ -78,7 +79,7 @@ async function putStream (req, res, next, stream = req) {
if (!req.originalUrl.endsWith('.acl')) await checkPermission(req, resourceExists)
await ldp.put(req, stream, getContentType(req.headers))
// Add event-id for notifications
res.setHeader('Event-ID', res.setEventID())
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(resourceExists ? 204 : 201)
return next()
} catch (err) {
Expand Down
17 changes: 12 additions & 5 deletions lib/ldp-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const index = require('./handlers/index')
const copy = require('./handlers/copy')
const notify = require('./handlers/notify')

function LdpMiddleware (corsSettings) {
function LdpMiddleware (corsSettings, prep) {
const router = express.Router('/')

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

router.copy('/*', allow('Write'), copy)
router.get('/*', index, allow('Read'), header.addPermissions, get)
router.post('/*', allow('Append'), post, notify)
router.patch('/*', allow('Append'), patch, notify)
router.put('/*', allow('Append'), put, notify)
router.delete('/*', allow('Write'), del, notify)
router.post('/*', allow('Append'), post)
router.patch('/*', allow('Append'), patch)
router.put('/*', allow('Append'), put)
router.delete('/*', allow('Write'), del)

if (prep) {
router.post('/*', notify)
router.patch('/*', notify)
router.put('/*', notify)
router.delete('/*', notify)
}

return router
}
3 changes: 2 additions & 1 deletion test/integration/prep-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe('Per Resource Events Protocol', function () {
dataBrowserPath: 'default',
root: path.join(__dirname, '../resources'),
auth: 'oidc',
webid: false
webid: false,
prep: true
})
server.listen(8443, done)
})
Expand Down

0 comments on commit b0e7eb0

Please sign in to comment.