-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
114 lines (99 loc) · 2.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Copyright (c) 2017
* Qblinks Incorporated ("Qblinks").
* All rights reserved.
*
* The information contained herein is confidential and proprietary to
* Qblinks. Use of this information by anyone other than authorized employees
* of Qblinks is granted only under a written non-disclosure agreement,
* expressly prescribing the scope and manner of such use.
*/
'use strict';
const fs = require('fs');
const merge = require('merge');
const xim_driver = {};
xim_driver.action = require('./action.js');
xim_driver.authenticate = require('./authenticate.js');
xim_driver.discovery = require('./discovery.js');
xim_driver.stat = require('./stat.js');
xim_driver.unlink = require('./unlink.js');
xim_driver.version = (event, callback) => {
const output = merge({}, event);
const commit = fs.readFileSync('./commit_info.json') || '{"id":null,"message":null,"author":null}';
const info = JSON.parse(commit) || { id: null, message: null, author: null };
output.version = info;
output.result = {
err_no: 0,
err_msg: 'ok',
};
callback(output);
};
exports.handler = (event, context, callback) => {
const method = event.method;
const input = merge({}, event);
delete input.method;
let result_body = '{}';
let result_statusCode = 200;
const supportMethodMap = {
authenticate: true,
discovery: true,
action: true,
stat: true,
unlink: true,
version: true,
};
if (supportMethodMap[method]) {
const type = event.xim_type;
const channel = event.xim_channel;
const set = event.xim_channel_set;
if (type === undefined ||
channel === undefined ||
set === undefined
) {
result_statusCode = 422;
result_body = JSON.stringify({
status: 'Failed',
message: 'Unprocessable Entity',
});
const response = {
statusCode: result_statusCode,
body: result_body,
};
callback(null, response);
} else if (!xim_driver[method]) {
result_statusCode = 405;
result_body = JSON.stringify({
status: 'Falied',
message: `Invalid XIM method: ${method}`,
});
const result_response = {
statusCode: result_statusCode,
body: result_body,
};
callback(null, result_response);
} else {
xim_driver[method](input, (options) => {
const result_options = options;
result_statusCode = 200;
delete result_options.quantum_token;
result_body = JSON.stringify(result_options);
const result_response = {
statusCode: result_statusCode,
body: result_body,
};
callback(null, result_response);
});
}
} else {
result_statusCode = 405;
result_body = JSON.stringify({
status: 'Failed',
message: 'Method Not Allowed',
});
const result_response = {
statusCode: result_statusCode,
body: result_body,
};
callback(null, result_response);
}
};