ldn-inbox-server 1.0.2 → 1.0.3
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/bin/inbox-server.js +1 -1
- package/index.js +21 -0
- package/package.json +1 -1
package/bin/inbox-server.js
CHANGED
package/index.js
CHANGED
|
@@ -4,10 +4,22 @@ const fs = require('fs');
|
|
|
4
4
|
const md5 = require('md5');
|
|
5
5
|
const { v4: uuidv4 } = require('uuid');
|
|
6
6
|
const fetch = require('node-fetch');
|
|
7
|
+
const log4js = require('log4js');
|
|
8
|
+
const logger = log4js.getLogger();
|
|
7
9
|
|
|
8
10
|
let INBOX_PATH = './inbox';
|
|
9
11
|
let JSON_SCHEMA = '';
|
|
10
12
|
|
|
13
|
+
log4js.configure({
|
|
14
|
+
appenders: {
|
|
15
|
+
stderr: { type: 'stderr' }
|
|
16
|
+
},
|
|
17
|
+
categories: {
|
|
18
|
+
default: { appenders: ['stderr'], level: process.env.LOG4JS ?? 'INFO' }
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
|
|
11
23
|
function inbox_server(options) {
|
|
12
24
|
INBOX = options['inbox'];
|
|
13
25
|
JSON_SCHEMA = JSON.parse(fs.readFileSync(options['schema'], { encoding: 'utf-8'}));
|
|
@@ -35,6 +47,7 @@ async function handle_inbox(path,handler,options) {
|
|
|
35
47
|
|
|
36
48
|
function doInbox(req,res) {
|
|
37
49
|
if (req.method !== 'POST') {
|
|
50
|
+
logger.error(`tried method ${req.method} on inbox : forbidden`);
|
|
38
51
|
res.writeHead(403);
|
|
39
52
|
res.end('Forbidden');
|
|
40
53
|
return;
|
|
@@ -50,6 +63,7 @@ function doInbox(req,res) {
|
|
|
50
63
|
// We are ok
|
|
51
64
|
}
|
|
52
65
|
else {
|
|
66
|
+
logger.error(`tried content-type ${headers['content-type']} : unknown`);
|
|
53
67
|
res.writeHead(400);
|
|
54
68
|
res.end(`Need a Content-Type 'application/ld+json'`);
|
|
55
69
|
return;
|
|
@@ -60,13 +74,16 @@ function doInbox(req,res) {
|
|
|
60
74
|
postData += data;
|
|
61
75
|
});
|
|
62
76
|
req.on('end',() => {
|
|
77
|
+
logger.debug(postData);
|
|
63
78
|
if (checkBody(postData)) {
|
|
64
79
|
const id = storeBody(postData);
|
|
80
|
+
logger.info(`accepted ${req.url}${id}`);
|
|
65
81
|
res.setHeader('Location',`${req.url}${id}`);
|
|
66
82
|
res.writeHead(201);
|
|
67
83
|
res.end(`Accepted ${req.url}${id}`);
|
|
68
84
|
}
|
|
69
85
|
else {
|
|
86
|
+
logger.error(`not-accepted post`);
|
|
70
87
|
res.writeHead(400);
|
|
71
88
|
res.end(`Looks like a weird POST to me...`);
|
|
72
89
|
}
|
|
@@ -79,8 +96,12 @@ function storeBody(data) {
|
|
|
79
96
|
const newpath = `${INBOX_PATH}/${id}.jsonld`;
|
|
80
97
|
|
|
81
98
|
if (! fs.existsSync(newpath)) {
|
|
99
|
+
logger.info(`storing ${newpath}`);
|
|
82
100
|
fs.writeFileSync(newpath,data);
|
|
83
101
|
}
|
|
102
|
+
else {
|
|
103
|
+
logger.info(`skiiping ${newpath} : already exists`);
|
|
104
|
+
}
|
|
84
105
|
|
|
85
106
|
return `${id}.jsonld`;
|
|
86
107
|
}
|