ldn-inbox-server 1.2.1 → 1.3.0
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.
- package/{.env → .env-example} +5 -1
- package/README.md +24 -10
- package/bin/ldn-inbox-server.js +18 -6
- package/config/inbox_config.json +10 -0
- package/config/outbox_config.json +10 -0
- package/handler/demo_inbox_handler.js +6 -0
- package/handler/demo_notification_handler.js +12 -8
- package/handler/eventlog_notification_handler.js +114 -0
- package/handler/multi_notification_handler.js +57 -0
- package/lib/handler.js +1 -0
- package/lib/index.js +135 -1
- package/lib/util.js +12 -1
- package/package.json +7 -4
- /package/{lib/sendNotificationHandler.js → handler/send_notification_handler.js} +0 -0
- /package/public/{index.html → index.html-example} +0 -0
package/{.env → .env-example}
RENAMED
|
@@ -2,10 +2,14 @@ LOG4JS=INFO
|
|
|
2
2
|
LDN_SERVER_HOST=localhost
|
|
3
3
|
LDN_SERVER_PORT=8000
|
|
4
4
|
LDN_SERVER_INBOX_GLOB="^.*\.jsonld$"
|
|
5
|
+
LDN_SERVER_BASEURL=http://localhost:8000
|
|
5
6
|
LDN_SERVER_INBOX_BATH_SIZE=5
|
|
6
7
|
LDN_SERVER_INBOX_URL=inbox/
|
|
7
8
|
LDN_SERVER_INBOX_PATH=./inbox
|
|
8
9
|
LDN_SERVER_ERROR_PATH=./error
|
|
9
10
|
LDN_SERVER_OUTBOX_PATH=./outbox
|
|
10
11
|
LDN_SERVER_PUBLIC_PATH=./public
|
|
11
|
-
LDN_SERVER_JSON_SCHEMA=./config/notification_schema.json
|
|
12
|
+
LDN_SERVER_JSON_SCHEMA=./config/notification_schema.json
|
|
13
|
+
LDN_SERVER_INBOX_CONFIG=./config/inbox_config.json
|
|
14
|
+
LDN_SERVER_OUTBOX_CONFIG=./config/outbox_config.json
|
|
15
|
+
LDN_SERVER_HAS_PUBLIC_INBOX=0
|
package/README.md
CHANGED
|
@@ -39,20 +39,23 @@ npx ldn-inbox-server handle @inbox -hn ./handler/demo_notification_handler.js
|
|
|
39
39
|
Send the notifications in the outbox:
|
|
40
40
|
|
|
41
41
|
```
|
|
42
|
-
npx ldn-inbox-server handle @outbox
|
|
42
|
+
npx ldn-inbox-server handle @outbox -hn ./handler/send_notification_handler.js
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
## Environment
|
|
46
46
|
|
|
47
47
|
- `LOG4JS` : log4js logging level
|
|
48
|
-
- `LDN_SERVER_HOST` :
|
|
49
|
-
- `LDN_SERVER_PORT` :
|
|
50
|
-
- `LDN_SERVER_INBOX_URL` :
|
|
51
|
-
- `LDN_SERVER_INBOX_PATH` :
|
|
52
|
-
- `LDN_SERVER_ERROR_PATH` :
|
|
53
|
-
- `LDN_SERVER_OUTBOX_PATH` :
|
|
54
|
-
- `LDN_SERVER_PUBLIC_PATH` :
|
|
55
|
-
- `LDN_SERVER_JSON_SCHEMA` :
|
|
48
|
+
- `LDN_SERVER_HOST` : LDN inbox host
|
|
49
|
+
- `LDN_SERVER_PORT` : LDN inbox port
|
|
50
|
+
- `LDN_SERVER_INBOX_URL` : LDN inbox url (path)
|
|
51
|
+
- `LDN_SERVER_INBOX_PATH` : LDN inbox path
|
|
52
|
+
- `LDN_SERVER_ERROR_PATH` : LDN error path
|
|
53
|
+
- `LDN_SERVER_OUTBOX_PATH` : LDN outbox path
|
|
54
|
+
- `LDN_SERVER_PUBLIC_PATH` : public (HTML) path
|
|
55
|
+
- `LDN_SERVER_JSON_SCHEMA` : notification JSON validation schema
|
|
56
|
+
- `LDN_SERVER_BASEURL` : baseurl of the LDN inbox server
|
|
57
|
+
- `LDN_SERVER_INBOX_GLOB` : glob of files to process in inbox directory
|
|
58
|
+
- `LDN_SERVER_HAS_PUBLIC_INBOX` : if true, then public read access is allowed on inbox
|
|
56
59
|
|
|
57
60
|
## Extend
|
|
58
61
|
|
|
@@ -71,7 +74,14 @@ main();
|
|
|
71
74
|
|
|
72
75
|
async function main() {
|
|
73
76
|
await handle_inbox('./inbox', {
|
|
74
|
-
'
|
|
77
|
+
'inbox': './inbox',
|
|
78
|
+
'outbox': './outbox',
|
|
79
|
+
'public': './public',
|
|
80
|
+
'error': './error',
|
|
81
|
+
'batch_size': 5,
|
|
82
|
+
'glob': '^.*\\.jsonld$',
|
|
83
|
+
'config': './config/inbox_config.json',
|
|
84
|
+
'notification_handler': 'handler/demo_notification_handler.js'
|
|
75
85
|
});
|
|
76
86
|
}
|
|
77
87
|
```
|
|
@@ -99,6 +109,10 @@ A handler can be started on any directory. E.g. a workflow might be:
|
|
|
99
109
|
- processed LDN messages will end up in the "outbox" box
|
|
100
110
|
- invalid processing will be saved into the "error" box
|
|
101
111
|
|
|
112
|
+
## Multi handler
|
|
113
|
+
|
|
114
|
+
A `handler/multi_notification_handler.js` is available to start multiple handler for each notification messages. The handlers to start are specified in a configuraton file that can be passed via the `config` parameter of an `handle_inbox`. In the commmand line tool `bin/ldn-inbox-server` the default location of such config file is `config/inbox_config.json` when processing an `@inbox`, and `config/outbox_config.json` when processing an `@outbox`.
|
|
115
|
+
|
|
102
116
|
## See also
|
|
103
117
|
|
|
104
118
|
- [mellon-server](https://www.npmjs.com/package/mellon-server)
|
package/bin/ldn-inbox-server.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const fs = require('fs');
|
|
4
4
|
const { program } = require('commander');
|
|
5
5
|
const { inbox_server } = require('../lib/index');
|
|
6
|
-
const { handle_inbox
|
|
6
|
+
const { handle_inbox } = require('../lib/handler');
|
|
7
7
|
require('dotenv').config();
|
|
8
8
|
|
|
9
9
|
const HOST = process.env.LDN_SERVER_HOST ?? 'localhost';
|
|
@@ -11,26 +11,32 @@ const PORT = process.env.LDN_SERVER_PORT ?? 8000;
|
|
|
11
11
|
const INBOX_GLOB = process.env.LDN_SERVER_INBOX_GLOB ?? "^.*\\.jsonld$";
|
|
12
12
|
const INBOX_BATCH_SIZE = process.env.LDN_SERVER_INBOX_BATH_SIZE ?? 5;
|
|
13
13
|
const INBOX_URL = process.env.LDN_SERVER_INBOX_URL ?? 'inbox/';
|
|
14
|
+
const INBOX_BASE_URL = process.env.LDN_SERVER_BASEURL ?? 'http://localhost:8000';
|
|
14
15
|
const PUBLIC_PATH = process.env.LDN_SERVER_PUBLIC_PATH ?? './public';
|
|
15
16
|
const INBOX_PATH = process.env.LDN_SERVER_INBOX_PATH ?? './inbox';
|
|
16
17
|
const ERROR_PATH = process.env.LDN_SERVER_ERROR_PATH ?? './error';
|
|
17
18
|
const OUTBOX_PATH = process.env.LDN_SERVER_OUTBOX_PATH ?? './outbox';
|
|
18
19
|
const JSON_SCHEMA_PATH = process.env.LDN_SERVER_JSON_SCHEMA ?? './config/notification_schema.json';
|
|
20
|
+
const INBOX_CONFIG = process.env.LDN_SERVER_INBOX_CONFIG;
|
|
21
|
+
const OUTBOX_CONFIG = process.env.LDN_SERVER_OUTBOX_CONFIG;
|
|
22
|
+
const HAS_PUBLIC = process.env.LDN_SERVER_HAS_PUBLIC_INBOX ?? 0;
|
|
19
23
|
|
|
20
24
|
program
|
|
21
25
|
.name('lnd-inbox-server')
|
|
22
|
-
.version('1.2.
|
|
26
|
+
.version('1.2.2')
|
|
23
27
|
.description('A demonstration Event Notifications Inbox server');
|
|
24
28
|
|
|
25
29
|
program
|
|
26
30
|
.command('start-server')
|
|
27
31
|
.option('--host <host>','host',HOST)
|
|
28
32
|
.option('--port <port>','port',PORT)
|
|
29
|
-
.option('--url <
|
|
33
|
+
.option('--url <path>','path',INBOX_URL)
|
|
34
|
+
.option('--base <url>','base url',INBOX_BASE_URL)
|
|
30
35
|
.option('--inbox <inbox>','inbox',INBOX_PATH)
|
|
31
36
|
.option('--public <public>','public',PUBLIC_PATH)
|
|
32
37
|
.option('--schema <schema>','json schema',JSON_SCHEMA_PATH)
|
|
33
38
|
.option('--registry <registry>','registry',null)
|
|
39
|
+
.option('--inbox-public','public readable inbox',HAS_PUBLIC)
|
|
34
40
|
.action( (options) => {
|
|
35
41
|
inbox_server(options);
|
|
36
42
|
});
|
|
@@ -39,10 +45,12 @@ program
|
|
|
39
45
|
.command('handler')
|
|
40
46
|
.option('--inbox <inbox>','inbox',INBOX_PATH)
|
|
41
47
|
.option('--outbox <outbox>','outbox',OUTBOX_PATH)
|
|
48
|
+
.option('--public <public>','public',PUBLIC_PATH)
|
|
42
49
|
.option('--error <errbox>','errbox',ERROR_PATH)
|
|
43
50
|
.option('--loop <seconds>', 'run in a loop',0)
|
|
44
51
|
.option('--batch_size <num>','batch size to process',INBOX_BATCH_SIZE)
|
|
45
52
|
.option('--glob <glob>','files to process in inbox',INBOX_GLOB)
|
|
53
|
+
.option('--config <path>','config file for handlers')
|
|
46
54
|
.option('-hi,--inbox_handler <handler>','inbox handler')
|
|
47
55
|
.option('-hn,--notification_handler <handler>','notification handler')
|
|
48
56
|
.argument('<box>','box to process')
|
|
@@ -50,11 +58,15 @@ program
|
|
|
50
58
|
switch (box) {
|
|
51
59
|
case '@inbox':
|
|
52
60
|
box = INBOX_PATH;
|
|
61
|
+
if (!options['config'] && fs.existsSync(INBOX_CONFIG)) {
|
|
62
|
+
options['config'] = INBOX_CONFIG
|
|
63
|
+
}
|
|
53
64
|
break;
|
|
54
65
|
case '@outbox':
|
|
55
66
|
box = OUTBOX_PATH;
|
|
56
|
-
options['
|
|
57
|
-
|
|
67
|
+
if (!options['config'] && fs.existsSync(OUTBOX_CONFIG)) {
|
|
68
|
+
options['config'] = OUTBOX_CONFIG
|
|
69
|
+
}
|
|
58
70
|
break;
|
|
59
71
|
}
|
|
60
72
|
if (options['loop']) {
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Demonstration of an alternatve inbox handler
|
|
5
|
+
* (don't use it the one in lib/handler.js is much more mature)
|
|
6
|
+
* To use it set it via the `--hi` option of `bin/ldn-inbox-server.js`, or
|
|
7
|
+
* the `inbox_handler` option of the `handle_inbox` function.
|
|
8
|
+
*/
|
|
3
9
|
async function handle({path,options}) {
|
|
4
10
|
console.log(`handleInbox(${path},..)`);
|
|
5
11
|
fs.readdir(path, (err,files) => {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const fs = require('fs');
|
|
2
|
+
const md5 = require('md5');
|
|
2
3
|
const logger = require('../lib/util.js').getLogger();
|
|
3
4
|
|
|
4
5
|
async function handle({path,options}) {
|
|
@@ -7,17 +8,13 @@ async function handle({path,options}) {
|
|
|
7
8
|
try {
|
|
8
9
|
const json = JSON.parse(fs.readFileSync(path, { encoding: 'utf-8'}));
|
|
9
10
|
|
|
10
|
-
const outboxFile = options['outbox'] + '/' + path.split('/').pop();
|
|
11
|
-
|
|
12
11
|
const id = json['id'];
|
|
13
12
|
const object = json['object'];
|
|
14
13
|
const actor_id = json['actor']['id'];
|
|
15
14
|
const actor_type = json['actor']['type'];
|
|
16
15
|
const actor_inbox = json['actor']['inbox'];
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
fs.writeFileSync(outboxFile, JSON.stringify({
|
|
17
|
+
const data = JSON.stringify({
|
|
21
18
|
type: 'Accept',
|
|
22
19
|
actor: {
|
|
23
20
|
id: 'http://my.server' ,
|
|
@@ -32,13 +29,20 @@ async function handle({path,options}) {
|
|
|
32
29
|
type: actor_type ,
|
|
33
30
|
inbox: actor_inbox
|
|
34
31
|
}
|
|
35
|
-
},null,
|
|
36
|
-
|
|
32
|
+
},null,4);
|
|
33
|
+
|
|
34
|
+
const outboxFile = options['outbox'] + '/' + md5(data) + '.jsonld';
|
|
35
|
+
|
|
36
|
+
logger.info(`storing Accept to ${outboxFile}`);
|
|
37
|
+
|
|
38
|
+
fs.writeFileSync(outboxFile,data);
|
|
39
|
+
|
|
40
|
+
return { path, options, success: true };
|
|
37
41
|
}
|
|
38
42
|
catch(e) {
|
|
39
43
|
logger.error(`failed to process ${path}`);
|
|
40
44
|
logger.debug(e);
|
|
41
|
-
return { path,options, success: false };
|
|
45
|
+
return { path, options, success: false };
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
48
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const md5 = require('md5');
|
|
3
|
+
const fsPath = require('path');
|
|
4
|
+
const lockfile = require('proper-lockfile');
|
|
5
|
+
const logger = require('../lib/util.js').getLogger();
|
|
6
|
+
|
|
7
|
+
const EVENT_DIR = 'events';
|
|
8
|
+
const EVENT_LOG = 'events.jsonld';
|
|
9
|
+
|
|
10
|
+
async function handle({path,options}) {
|
|
11
|
+
logger.info(`parsing notification ${path}`);
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const json = fs.readFileSync(path, { encoding: 'utf-8'});
|
|
15
|
+
|
|
16
|
+
const fileName = path.split('/').pop();
|
|
17
|
+
const logDir = fsPath.join(options['public'],EVENT_DIR,'log');
|
|
18
|
+
|
|
19
|
+
if (! fs.existsSync(logDir)) {
|
|
20
|
+
logger.info(`creating ${logDir}`);
|
|
21
|
+
fs.mkdirSync(logDir, { recursive : true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const outboxFile = fsPath.join(logDir,fileName);
|
|
25
|
+
|
|
26
|
+
fs.writeFileSync(outboxFile, json);
|
|
27
|
+
|
|
28
|
+
// Updating metadata file
|
|
29
|
+
const metaFile = outboxFile + '.meta';
|
|
30
|
+
|
|
31
|
+
fs.writeFileSync(metaFile, JSON.stringify({
|
|
32
|
+
'Content-Type': 'application/ld+json',
|
|
33
|
+
'Last-Modified': nowISO()
|
|
34
|
+
},null,4));
|
|
35
|
+
|
|
36
|
+
await updateEventLog({path,options});
|
|
37
|
+
|
|
38
|
+
return { path, options, success: true };
|
|
39
|
+
}
|
|
40
|
+
catch(e) {
|
|
41
|
+
logger.error(`failed to process ${path}`);
|
|
42
|
+
logger.error(e);
|
|
43
|
+
return { path, options, success: false };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function updateEventLog({path,options}) {
|
|
48
|
+
logger.info(`updating eventlog for ${path}`);
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const notification = fs.readFileSync(path, { encoding: 'utf-8'});
|
|
52
|
+
const notification_checksum = md5(notification);
|
|
53
|
+
|
|
54
|
+
const baseUrl = process.env.LDN_SERVER_BASEURL ?? "";
|
|
55
|
+
const fileName = path.split('/').pop();
|
|
56
|
+
const entry = `${baseUrl}/${EVENT_DIR}/log/${fileName}`;
|
|
57
|
+
|
|
58
|
+
const eventLog = fsPath.join(options['public'],EVENT_DIR,EVENT_LOG);
|
|
59
|
+
|
|
60
|
+
let json;
|
|
61
|
+
|
|
62
|
+
if (fs.existsSync(eventLog)) {
|
|
63
|
+
json = JSON.parse(fs.readFileSync(eventLog, { encoding: 'utf-8'}));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
json = {
|
|
67
|
+
"@context": "https://labs.eventnotifications.net/contexts/eventlog.jsonld",
|
|
68
|
+
"type": "EventLog",
|
|
69
|
+
"member": []
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (json['member'].findIndex( (e) => e === entry) >= 0) {
|
|
74
|
+
logger.info(`${entry} already in ${eventLog}`);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
logger.info(`updating ${eventLog}`);
|
|
78
|
+
|
|
79
|
+
json['member'].push({
|
|
80
|
+
"id": entry ,
|
|
81
|
+
"checksum": {
|
|
82
|
+
"type": "Checksum",
|
|
83
|
+
"algorithm": "spdx:checksumAlgorithm_md5",
|
|
84
|
+
"checksumValue": notification_checksum
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (fs.existsSync(eventLog)) {
|
|
89
|
+
try {
|
|
90
|
+
const lock = await lockfile(eventLog, { retries: 10 });
|
|
91
|
+
fs.writeFileSync(eventLog,JSON.stringify(json,null,4));
|
|
92
|
+
lock(); // release
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
logger.error(`failed to update ${eventLog}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
fs.writeFileSync(eventLog,JSON.stringify(json,null,4));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
catch (e) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function nowISO() {
|
|
111
|
+
return (new Date()).toUTCString();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = { handle };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const { dynamic_handler , parseAsJSON } = require('../lib/util');
|
|
2
|
+
const logger = require('../lib/util.js').getLogger();
|
|
3
|
+
|
|
4
|
+
async function handle({path,options}) {
|
|
5
|
+
let success = false;
|
|
6
|
+
|
|
7
|
+
const config = parseAsJSON(options['config']);
|
|
8
|
+
|
|
9
|
+
if (! config) {
|
|
10
|
+
logger.error('no configuration found for multi_notification_handler');
|
|
11
|
+
return { path, options, success: false };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const handlers = config['notification_handler']?.['multi']?.['handlers'];
|
|
15
|
+
|
|
16
|
+
if (! handlers) {
|
|
17
|
+
logger.error('no notification_handler.multi.handlers key in configuration file');
|
|
18
|
+
return { path, options, success: false };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
logger.info(`starting multi handler`);
|
|
23
|
+
|
|
24
|
+
for (let i = 0 ; i < handlers.length ; i++) {
|
|
25
|
+
logger.info(`starting ${handlers[i]}`);
|
|
26
|
+
|
|
27
|
+
const handler = dynamic_handler(handlers[i],null);
|
|
28
|
+
|
|
29
|
+
if (! handler) {
|
|
30
|
+
throw new Error(`failed to load ${handlers[i]}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const result = await handler({path,options});
|
|
34
|
+
|
|
35
|
+
if (result['success']) {
|
|
36
|
+
logger.info(`finished ${handlers[i]}`);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
throw new Error(`Eek! ${handlers[i]} failed`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
success = true;
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
logger.error(`failed to process ${path}`);
|
|
47
|
+
logger.error(e);
|
|
48
|
+
|
|
49
|
+
success = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
logger.info(`finished multi handler`);
|
|
53
|
+
|
|
54
|
+
return { path, options, success: success };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
module.exports = { handle };
|
package/lib/handler.js
CHANGED
|
@@ -17,6 +17,7 @@ async function defaultInboxHandler({path,options}) {
|
|
|
17
17
|
|
|
18
18
|
const worker = options['notification_handler'] ?? fsPath.resolve(__dirname,'..','lib','notification.js');
|
|
19
19
|
|
|
20
|
+
// Run the notifications using a node.js worker pool
|
|
20
21
|
const pool = new piscina({
|
|
21
22
|
filename: worker,
|
|
22
23
|
maxQueue: queue_size
|
package/lib/index.js
CHANGED
|
@@ -14,11 +14,15 @@ const logger = getLogger();
|
|
|
14
14
|
|
|
15
15
|
let INBOX_URL = 'inbox/';
|
|
16
16
|
let INBOX_PATH = './inbox';
|
|
17
|
+
let INBOX_BASE_URL = 'http://localhost:8000';
|
|
18
|
+
let INBOX_PUBLIC_READABLE = 0;
|
|
17
19
|
let JSON_SCHEMA = '';
|
|
18
20
|
|
|
19
21
|
function inbox_server(options) {
|
|
20
22
|
INBOX_URL = options['url'];
|
|
21
23
|
INBOX_PATH = options['inbox'];
|
|
24
|
+
INBOX_BASE_URL = options['base'];
|
|
25
|
+
INBOX_PUBLIC_READABLE = options['inboxPublic'];
|
|
22
26
|
JSON_SCHEMA = JSON.parse(fs.readFileSync(options['schema'], { encoding: 'utf-8'}));
|
|
23
27
|
let registry = [{ path : `${INBOX_URL}.*` , do: doInbox }];
|
|
24
28
|
|
|
@@ -38,16 +42,117 @@ function inbox_server(options) {
|
|
|
38
42
|
start_server({
|
|
39
43
|
host: options['host'],
|
|
40
44
|
port: options['port'],
|
|
45
|
+
base: options['base'],
|
|
41
46
|
public: options['public'],
|
|
42
47
|
registry: registry
|
|
43
48
|
});
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
function doInbox(req,res) {
|
|
47
|
-
if (req.method
|
|
52
|
+
if (req.method === 'GET' && INBOX_PUBLIC_READABLE == 1) {
|
|
53
|
+
doInboxGET(req,res);
|
|
54
|
+
}
|
|
55
|
+
else if (req.method == 'HEAD' && INBOX_PUBLIC_READABLE == 1) {
|
|
56
|
+
doInboxHEAD(req,res);
|
|
57
|
+
}
|
|
58
|
+
else if (req.method === 'POST') {
|
|
59
|
+
doInboxPOST(req,res);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
48
62
|
logger.error(`tried method ${req.method} on inbox : forbidden`);
|
|
49
63
|
res.writeHead(403);
|
|
50
64
|
res.end('Forbidden');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function doInboxGET(req,res) {
|
|
69
|
+
const pathItem = req.url.substring(INBOX_URL.length);
|
|
70
|
+
|
|
71
|
+
logger.debug(`doInboxGET (for ${pathItem})`);
|
|
72
|
+
|
|
73
|
+
if (pathItem === '/') {
|
|
74
|
+
doInboxGET_Index(req,res);
|
|
75
|
+
}
|
|
76
|
+
else if (pathItem.match(/^\/[a-z0-9]+\.jsonld$/)) {
|
|
77
|
+
doInboxGET_Read(req,res);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
res.writeHead(403);
|
|
81
|
+
res.end('Forbidden');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function doInboxHEAD(req,res) {
|
|
86
|
+
const pathItem = req.url.substring(INBOX_URL.length);
|
|
87
|
+
|
|
88
|
+
logger.debug(`doInboxHEAD (for ${pathItem})`);
|
|
89
|
+
|
|
90
|
+
if (pathItem === '/') {
|
|
91
|
+
res.setHeader('Content-Type','application/ld+json');
|
|
92
|
+
res.writeHead(200);
|
|
93
|
+
res.end();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (pathItem.match(/^\/[a-z0-9]+\.jsonld$/)) {
|
|
98
|
+
const id = pathItem.substring(1);
|
|
99
|
+
const result = getBody(id);
|
|
100
|
+
|
|
101
|
+
if (result) {
|
|
102
|
+
res.setHeader('Content-Type','application/ld+json');
|
|
103
|
+
res.writeHead(200);
|
|
104
|
+
res.end();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
res.writeHead(403);
|
|
109
|
+
res.end('Forbidden');
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
res.writeHead(403);
|
|
114
|
+
res.end('Forbidden');
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function doInboxGET_Index(req,res) {
|
|
120
|
+
const notifications = listInbox().map( (e) => {
|
|
121
|
+
return INBOX_BASE_URL + '/' + INBOX_URL + e;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const result = {
|
|
125
|
+
"@context": "http://www.w3.org/ns/ldp",
|
|
126
|
+
"@id": INBOX_BASE_URL + '/' + INBOX_URL,
|
|
127
|
+
"contains": notifications
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
res.writeHead(200);
|
|
131
|
+
res.end(JSON.stringify(result,null,2));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function doInboxGET_Read(req,res) {
|
|
135
|
+
const pathItem = req.url.substring(INBOX_URL.length);
|
|
136
|
+
|
|
137
|
+
const result = getBody(pathItem.substring(1));
|
|
138
|
+
|
|
139
|
+
if (result) {
|
|
140
|
+
res.setHeader('Content-Type','application/ld+json');
|
|
141
|
+
res.writeHead(200);
|
|
142
|
+
res.end(result);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
res.writeHead(403);
|
|
146
|
+
res.end('Forbidden');
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function doInboxPOST(req,res) {
|
|
151
|
+
const pathItem = req.url.substring(INBOX_URL.length);
|
|
152
|
+
|
|
153
|
+
if (pathItem !== '/') {
|
|
154
|
+
req.writeHead(403);
|
|
155
|
+
res.end('Forbidden');
|
|
51
156
|
return;
|
|
52
157
|
}
|
|
53
158
|
|
|
@@ -88,6 +193,35 @@ function doInbox(req,res) {
|
|
|
88
193
|
});
|
|
89
194
|
}
|
|
90
195
|
|
|
196
|
+
function listInbox() {
|
|
197
|
+
const glob = new RegExp("^.*\\.jsonld$");
|
|
198
|
+
|
|
199
|
+
logger.debug(`listInbox()`);
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const entries = fs.readdirSync(INBOX_PATH).filter( (file) => {
|
|
203
|
+
return file.match(glob);
|
|
204
|
+
});
|
|
205
|
+
return entries;
|
|
206
|
+
}
|
|
207
|
+
catch(e) {
|
|
208
|
+
logger.error(e);
|
|
209
|
+
return [];
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function getBody(id) {
|
|
214
|
+
logger.debug(`getBody(${id})`);
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
return fs.readFileSync(INBOX_PATH + '/' + id, {encoding : 'utf-8'});
|
|
218
|
+
}
|
|
219
|
+
catch(e) {
|
|
220
|
+
logger.error(e);
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
91
225
|
function storeBody(data) {
|
|
92
226
|
try {
|
|
93
227
|
const id = md5(data);
|
package/lib/util.js
CHANGED
|
@@ -125,11 +125,22 @@ function dynamic_handler(handler,fallback) {
|
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
function parseAsJSON(path) {
|
|
129
|
+
try {
|
|
130
|
+
return JSON.parse(fs.readFileSync(path, { encoding: 'utf-8'}));
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
logger.error(`failed to parse ${path}`);
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
128
138
|
module.exports = {
|
|
129
139
|
getLogger ,
|
|
130
140
|
backOff_fetch ,
|
|
131
141
|
fetchOriginal ,
|
|
132
142
|
moveTo ,
|
|
133
143
|
sendNotification ,
|
|
134
|
-
dynamic_handler
|
|
144
|
+
dynamic_handler ,
|
|
145
|
+
parseAsJSON
|
|
135
146
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ldn-inbox-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A demonstration Event Notifications Inbox server",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"author": "Patrick Hochstenbach <Patrick.Hochstenbach@UGent.be>",
|
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
"server": "npx ldn-inbox-server start-server",
|
|
10
10
|
"demo-post": "curl -X POST -H 'Content-Type: application/ld+json' --data-binary '@examples/offer.jsonld' http://localhost:8000/inbox/",
|
|
11
11
|
"handle-inbox": "npx ldn-inbox-server handler @inbox -hn ./handler/demo_notification_handler.js",
|
|
12
|
-
"handle-outbox": "npx ldn-inbox-server handler @outbox",
|
|
13
|
-
"
|
|
12
|
+
"handle-outbox": "npx ldn-inbox-server handler @outbox -hn ./handler/send_notification_handler.js",
|
|
13
|
+
"handle-eventlog": "npx ldn-inbox-server handler @inbox -hn handler/eventlog_notification_handler.js",
|
|
14
|
+
"handle-inbox-multi": "npx ldn-inbox-server handler @inbox -hn ./handler/multi_notification_handler.js",
|
|
15
|
+
"handle-outbox-multi": "npx ldn-inbox-server handler @outbox -hn ./handler/multi_notification_handler.js",
|
|
16
|
+
"clean": "rm error/* inbox/* outbox/* public/events/* public/events/log/*"
|
|
14
17
|
},
|
|
15
18
|
"bin": "./bin/ldn-inbox-server.js",
|
|
16
19
|
"keywords": [
|
|
@@ -24,7 +27,7 @@
|
|
|
24
27
|
"exponential-backoff": "^3.1.1",
|
|
25
28
|
"jsonschema": "^1.4.1",
|
|
26
29
|
"md5": "^2.3.0",
|
|
27
|
-
"mellon-server": "^1.0.
|
|
30
|
+
"mellon-server": "^1.0.8",
|
|
28
31
|
"node-fetch": "1.7.3",
|
|
29
32
|
"piscina": "^4.4.0",
|
|
30
33
|
"proper-lockfile": "^4.1.2",
|
|
File without changes
|
|
File without changes
|