Skip to content

Commit

Permalink
feat/refactor: Event-ID Generated for Notifications Only
Browse files Browse the repository at this point in the history
+ Event-ID is generated in PREP notification middleware itself.
+ No longer setting `Event-ID` response header upon resource mutation, thus reverting the middlewares for unsafe methods to their original state, before PREP was introduced.
  • Loading branch information
CxRes committed Nov 10, 2024
1 parent c33bc32 commit 6c31436
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 16 deletions.
3 changes: 0 additions & 3 deletions lib/handlers/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ 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
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(200)
next()
} catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion lib/handlers/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function handler (req, res, next) {

const { method, path } = req
const { statusCode } = res
const eventID = res.getHeader('event-id')
const eventID = res.setEventID()
const fullUrl = new URL(path, `${req.protocol}://${req.hostname}/`)

// Date is a hack since node does not seem to provide access to send date.
Expand Down
4 changes: 0 additions & 4 deletions lib/handlers/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ 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 @@ -91,9 +90,6 @@ async function patchHandler (req, res, next) {
await applyPatch(patchObject, graph, url)
return writeGraph(graph, resource, ldp.resourceMapper.resolveFilePath(req.hostname), ldp.serverUri)
})

// Add event-id for notifications
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: 0 additions & 5 deletions lib/handlers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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,8 +72,6 @@ async function handler (req, res, next) {
// Handled by backpressure of streams!
busboy.on('finish', function () {
debug('Done storing files')
// Add event-id for notifications
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(200)
next()
})
Expand All @@ -94,8 +91,6 @@ async function handler (req, res, next) {
debug('File stored in ' + resourcePath)
header.addLinks(res, links)
res.set('Location', resourcePath)
// Add event-id for notifications
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(201)
next()
},
Expand Down
3 changes: 0 additions & 3 deletions lib/handlers/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ 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,8 +77,6 @@ async function putStream (req, res, next, stream = req) {
// Fails with Append on existing resource
if (!req.originalUrl.endsWith('.acl')) await checkPermission(req, resourceExists)
await ldp.put(req, stream, getContentType(req.headers))
// Add event-id for notifications
prep && res.setHeader('Event-ID', res.setEventID())
res.sendStatus(resourceExists ? 204 : 201)
return next()
} catch (err) {
Expand Down

0 comments on commit 6c31436

Please sign in to comment.