@@ -11,6 +11,13 @@ const ALLOWED_RDF_MIME_TYPES = [
11
11
'text/turtle'
12
12
]
13
13
14
+ function getParent ( path ) {
15
+ if ( path === '' || path === '/' ) return
16
+ const parent = libPath . dirname ( path )
17
+ if ( parent === '/' ) return
18
+ return `${ parent } /`
19
+ }
20
+
14
21
function getActivity ( method ) {
15
22
if ( method === 'DELETE' ) {
16
23
return 'Delete'
@@ -34,11 +41,7 @@ function handler (req, res, next) {
34
41
const { method } = req
35
42
const { statusCode } = res
36
43
const eventID = res . getHeader ( 'event-id' )
37
-
38
- const parent = `${ libPath . dirname ( req . path ) } /`
39
- const parentID = res . setEventID ( parent )
40
44
const fullUrl = new URL ( req . path , `${ req . protocol } ://${ req . hostname } /` )
41
- const parentUrl = new URL ( parent , fullUrl )
42
45
43
46
// Date is a hack since node does not seem to provide access to send date.
44
47
// Date needs to be shared with parent notification
@@ -48,53 +51,64 @@ function handler (req, res, next) {
48
51
// If the resource itself newly created,
49
52
// it could not have been subscribed for notifications already
50
53
if ( ! ( ( method === 'PUT' || method === 'PATCH' ) && statusCode === 201 ) ) {
51
- trigger ( {
52
- generateNotification (
53
- negotiatedFields
54
- ) {
55
- const mediaType = negotiatedFields [ 'content-type' ]
54
+ try {
55
+ trigger ( {
56
+ generateNotification (
57
+ negotiatedFields
58
+ ) {
59
+ const mediaType = negotiatedFields [ 'content-type' ]
56
60
57
- if ( ALLOWED_RDF_MIME_TYPES . includes ( mediaType ?. [ 0 ] ) ) {
58
- return `${ headerTemplate ( negotiatedFields ) } \r\n${ solidRDFTemplate ( {
59
- activity : getActivity ( method ) ,
60
- eventID,
61
- object : String ( fullUrl ) ,
62
- date : eventDate ,
63
- // We use eTag as a proxy for state for now
64
- state : res . getHeader ( 'ETag' ) ,
65
- mediaType
66
- } ) } `
67
- } else {
68
- return defaultNotification ( {
69
- ...( res . method === 'POST' ) && { location : res . getHeader ( 'Content-Location' ) }
70
- } )
61
+ if ( ALLOWED_RDF_MIME_TYPES . includes ( mediaType ?. [ 0 ] ) ) {
62
+ return `${ headerTemplate ( negotiatedFields ) } \r\n${ solidRDFTemplate ( {
63
+ activity : getActivity ( method ) ,
64
+ eventID,
65
+ object : String ( fullUrl ) ,
66
+ date : eventDate ,
67
+ // We use eTag as a proxy for state for now
68
+ state : res . getHeader ( 'ETag' ) ,
69
+ mediaType
70
+ } ) } `
71
+ } else {
72
+ return defaultNotification ( {
73
+ ...( res . method === 'POST' ) && { location : res . getHeader ( 'Content-Location' ) }
74
+ } )
75
+ }
71
76
}
72
- }
73
- } )
77
+ } )
78
+ } catch ( error ) {
79
+ // Failed notification message
80
+ }
74
81
}
75
82
76
83
// Write a notification to parent container
77
84
// POST in Solid creates a child resource
78
- if ( method !== 'POST' ) {
79
- trigger ( {
80
- path : parent ,
81
- generateNotification (
82
- negotiatedFields
83
- ) {
84
- const mediaType = negotiatedFields [ 'content-type' ]
85
- if ( ALLOWED_RDF_MIME_TYPES . includes ( mediaType ?. [ 0 ] ) ) {
86
- return `${ headerTemplate ( negotiatedFields ) } \r\n${ solidRDFTemplate ( {
87
- activity : getParentActivity ( method , statusCode ) ,
88
- eventID : parentID ,
89
- date : eventDate ,
90
- object : String ( parentUrl ) ,
91
- target : statusCode === 201 ? String ( fullUrl ) : undefined ,
92
- eTag : undefined ,
93
- mediaType
94
- } ) } `
85
+ const parent = getParent ( req . path )
86
+ if ( parent && method !== 'POST' ) {
87
+ try {
88
+ const parentID = res . setEventID ( parent )
89
+ const parentUrl = new URL ( parent , fullUrl )
90
+ trigger ( {
91
+ path : parent ,
92
+ generateNotification (
93
+ negotiatedFields
94
+ ) {
95
+ const mediaType = negotiatedFields [ 'content-type' ]
96
+ if ( ALLOWED_RDF_MIME_TYPES . includes ( mediaType ?. [ 0 ] ) ) {
97
+ return `${ headerTemplate ( negotiatedFields ) } \r\n${ solidRDFTemplate ( {
98
+ activity : getParentActivity ( method , statusCode ) ,
99
+ eventID : parentID ,
100
+ date : eventDate ,
101
+ object : String ( parentUrl ) ,
102
+ target : statusCode === 201 ? String ( fullUrl ) : undefined ,
103
+ eTag : undefined ,
104
+ mediaType
105
+ } ) } `
106
+ }
95
107
}
96
- }
97
- } )
108
+ } )
109
+ } catch ( error ) {
110
+ // Failed notification message
111
+ }
98
112
}
99
113
100
114
next ( )
0 commit comments