-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-default-api.js
172 lines (135 loc) · 3.9 KB
/
rest-default-api.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* This is the Core REST API my-own-cluster implementation
*
* It allows to execute basic operations like :
*
* - register a blob
* - plus a blob as a function
* - plug a blob as a file
* - call a function
*
* It is mainly used by the CLI program.
* It is normally bound to the cluster web endpoint during startup (in main.go)
*/
/// reference path="./core-api-guest.d.ts"
const moc = requireApi('core')
function getInputRequest() {
var bufferId = moc.getInputBufferId()
var bufferBytes = moc.readExchangeBuffer(bufferId)
var reqText = new TextDecoder("utf-8").decode(bufferBytes)
return JSON.parse(reqText)
}
function plugFunction() {
var req = getInputRequest()
moc.plugFunction(
req.method,
req.path,
req.name,
req.start_function,
req.data || "",
JSON.stringify(req.tags || {})
)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true,
}))
return 200
}
function plugFile() {
var req = getInputRequest()
moc.plugFile(
req.method,
req.path,
req.name,
JSON.stringify(req.tags || {})
)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true,
}))
return 200
}
function unplugPath() {
var req = getInputRequest()
moc.unplugPath(
req.method,
req.path
)
moc.writeExchangeBufferStatusCode(moc.getOutputBufferId(), 200)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true,
}))
}
function plugFilter() {
var req = getInputRequest()
var filterId = moc.plugFilter(
req.name,
req.start_function,
req.data
)
moc.writeExchangeBufferStatusCode(moc.getOutputBufferId(), 200)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true,
filter_id: filterId
}))
}
function unplugFilter() {
var headers = moc.readExchangeBufferHeaders(moc.getInputBufferId())
var filterId = headers["x-moc-path-param-filter-id"]
moc.unplugFilter(filterId)
moc.writeExchangeBufferStatusCode(moc.getOutputBufferId(), 200)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true
}))
}
function registerBlob() {
var req = getInputRequest()
var bytes = moc.base64Decode(req.bytes)
var techID;
if (req.name) {
techID = moc.registerBlobWithName(
req.name,
req.content_type,
bytes
);
} else {
techID = moc.registerBlob(
req.content_type,
bytes
);
}
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify({
status: true,
tech_id: techID
}))
return 200
}
function callFunction() {
var req = getInputRequest()
var inputExchangeBufferId = moc.createExchangeBuffer()
if (req.input)
moc.writeExchangeBuffer(inputExchangeBufferId, moc.base64Decode(req.input))
var outputExchangeBufferId = moc.createExchangeBuffer()
var result = moc.callFunction(
req.name,
req.start_function || "_start",
req.arguments || [],
req.mode || "direct",
inputExchangeBufferId,
outputExchangeBufferId,
req.posix_file_name || "",
req.posix_arguments || [""]
)
var response = {
status: true,
result: result,
output: moc.base64Encode(moc.readExchangeBuffer(outputExchangeBufferId))
}
moc.writeExchangeBufferStatusCode(moc.getOutputBufferId(), 200)
moc.writeExchangeBuffer(moc.getOutputBufferId(), JSON.stringify(response))
moc.freeBuffer(inputExchangeBufferId)
moc.freeBuffer(outputExchangeBufferId)
}
function exportDatabase() {
var exp = moc.exportDatabase()
moc.writeExchangeBufferStatusCode(moc.getOutputBufferId(), 200)
moc.writeExchangeBuffer(moc.getOutputBufferId(), exp)
}