ldn-inbox-server 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +30 -1
  2. package/package.json +2 -1
package/index.js CHANGED
@@ -2,6 +2,8 @@ const { start_server } = require('mellon-server');
2
2
  const Validator = require('jsonschema').Validator;
3
3
  const fs = require('fs');
4
4
  const md5 = require('md5');
5
+ const { v4: uuidv4 } = require('uuid');
6
+ const fetch = require('node-fetch');
5
7
 
6
8
  let INBOX_PATH = './inbox';
7
9
  let JSON_SCHEMA = '';
@@ -105,4 +107,31 @@ function checkBody(data) {
105
107
  }
106
108
  }
107
109
 
108
- module.exports = { inbox_server , handle_inbox };
110
+ async function sendNotification(url,json, options) {
111
+ if (!json['@context']) {
112
+ json['@context'] = "https://www.w3.org/ns/activitystreams";
113
+ }
114
+
115
+ if (!json['id']) {
116
+ json['id'] = 'urn:uuid:' + uuidv4();
117
+ }
118
+
119
+ let fetcher = options['fetch'] ?? fetch;
120
+
121
+ const response = await fetcher(url, {
122
+ method: 'POST',
123
+ headers: {
124
+ 'Accept': 'application/json',
125
+ 'Content-Type': 'application/json'
126
+ },
127
+ body: JSON.stringify(json)
128
+ });
129
+
130
+ if (! response.ok) {
131
+ throw Error(`failed to POST to ${url}`);
132
+ }
133
+
134
+ return true;
135
+ }
136
+
137
+ module.exports = { inbox_server , handle_inbox , sendNotification };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ldn-inbox-server",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "A demonstration Event Notifications Inbox server",
5
5
  "main": "index.js",
6
6
  "author": "Patrick Hochstenbach <Patrick.Hochstenbach@UGent.be>",
@@ -21,6 +21,7 @@
21
21
  "jsonschema": "^1.4.1",
22
22
  "md5": "^2.3.0",
23
23
  "mellon-server": "^1.0.3",
24
+ "node-fetch": "1.7.3",
24
25
  "uuid": "^9.0.1"
25
26
  }
26
27
  }