-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.mjs
More file actions
428 lines (398 loc) · 14.4 KB
/
Copy pathserver.mjs
File metadata and controls
428 lines (398 loc) · 14.4 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import express from 'express'
import fs from 'fs'
import JSONTag from '@muze-nl/jsontag'
import WorkerPool from './workerPool.mjs'
import { Worker } from 'worker_threads'
import { fileURLToPath } from 'url'
import {appendFile} from './util.mjs'
import path from 'path'
import httpStatusCodes from './statusCodes.mjs'
import writeFileAtomic from 'write-file-atomic'
const server = express()
const __dirname = path.dirname(path.dirname(fileURLToPath(import.meta.url)))
let jsontagBuffers = null
let meta = {}
async function main(options) {
if (!options) {
options = {}
}
const port = options.port || 3000
const datafile = options.datafile || './data.od-jsontag'
const schemaFile = options.schemaFile || null
const wwwroot = options.wwwroot || __dirname+'/www'
const maxWorkers = options.maxWorkers || 8
const queryWorker = options.queryWorker || __dirname+'/src/query-worker.mjs'
const loadWorker = options.loadWorker || __dirname+'/src/load-worker.mjs'
const commandWorker = options.commandWorker || __dirname+'/src/command-worker.mjs'
const commandsFile = options.commandsFile || __dirname+'/src/commands.mjs'
const commandLog = options.commandLog || './command-log.jsontag'
const commandStatus = options.commandStatus || './command-status.jsontag'
const access = options.access || null
server.use(express.static(wwwroot))
console.log("Debug information:")
console.log("maxworkers: ", maxWorkers)
// allow access to raw body, used to parse a query send as post body
server.use(express.raw({
type: (req) => true, // parse body on all requests
limit: '50MB'
}))
function loadData() {
return new Promise((resolve,reject) => {
let worker = new Worker(loadWorker)
worker.on('message', result => {
resolve(result)
worker.terminate()
})
worker.on('error', error => {
reject(error)
worker.terminate()
})
worker.postMessage({dataFile:datafile,schemaFile})
})
}
try {
let data = await loadData()
jsontagBuffers = [data.data]
meta = data.meta
} catch(err) {
console.error('ERROR: SimplyStore cannot load '+datafile, err)
process.exit(1)
}
const queryWorkerInitTask = () => {
return {
name: 'init',
req: {
body: jsontagBuffers,
meta,
access
}
}
}
let queryWorkerPool = new WorkerPool(maxWorkers, queryWorker, queryWorkerInitTask())
server.get('/query/*', async (req, res, next) =>
{
let start = Date.now()
if ( !accept(req,res,
['application/jsontag','application/json','text/html','text/javascript','image/*'],
function(req, res, accept) {
switch(accept) {
case 'text/html':
case 'image/*':
case 'text/javascript':
handleWebRequest(req,res,{root:wwwroot});
return false
break
}
return true
}
)) {
// done
return
}
let path = req.path.substr(6) // cut '/query'
console.log('query',path)
let request = {
method: req.method,
url: req.originalUrl,
query: req.query,
path: path
}
if (accept(req,res,['application/jsontag'])) {
request.jsontag = true
}
try {
let result = await queryWorkerPool.run('query', request)
sendResponse(result, res)
} catch(error) {
sendError(error, res)
}
let end = Date.now()
console.log(path, (end-start), process.memoryUsage())
})
server.post('/query/*', async (req,res) => {
let start = Date.now()
if ( !accept(req,res,
['application/jsontag','application/json'])
) {
sendError({code:406, message:'Not Acceptable',accept:['application/json','application/jsontag']},res)
return
}
let path = req.path.substr(6) // cut '/query'
let request = {
method: req.method,
url: req.originalUrl,
query: req.query,
path: path,
body: req.body.toString()
}
if (accept(req,res,['application/jsontag'])) {
request.jsontag = true
}
try {
let result = await queryWorkerPool.run('query', request)
sendResponse(result, res)
} catch(error) {
sendError(error, res)
}
let end = Date.now()
console.log(path, (end-start), process.memoryUsage())
// queryWorkerPool.memoryUsage()
})
let status = loadCommandStatus(commandStatus)
function loadCommandStatus(commandStatusFile) {
let status = new Map()
if (fs.existsSync(commandStatusFile)) {
let file = fs.readFileSync(commandStatusFile, 'utf-8')
if (file) {
let lines = file.split("\n").filter(Boolean) //filter clears empty lines
for(let line of lines) {
let command = JSONTag.parse(line)
status.set(command.command, command)
}
} else {
console.error('Could not open command status',commandStatusFile)
}
} else {
console.log('no command status', commandStatusFile)
}
return status
}
let commandQueue = []
function loadCommandLog(commandLog) {
if (!fs.existsSync(commandLog)) {
return
}
let log = fs.readFileSync(commandLog)
if (log) {
let lines = log.split("\n")
for(let line of lines) {
let command = JSONTag.parse(line)
let state = status.get(command.id)
switch(state) {
case 'accepted': // enqueue
commandQueue.push(command)
break;
case 'done': // do nothing
break;
default: // error, do nothing
break;
}
}
}
}
loadCommandLog()
let commandWorkerInstance
async function runNextCommand() {
let command = commandQueue.shift()
if (command) {
let start = (resolve, reject) => {
if (!commandWorkerInstance) {
commandWorkerInstance = new Worker(commandWorker)
}
commandWorkerInstance.on('message', result => {
resolve(result)
runNextCommand()
})
commandWorkerInstance.on('error', error => {
reject(error)
runNextCommand()
})
commandWorkerInstance.postMessage(command)
}
start(
// resolve()
(data) => {
let s
if (!data || (data.code>=300 && data.code<=499)) {
console.error('ERROR: SimplyStore cannot run command ', command.id, data)
if (!data?.code) {
s = {code: 500, status: "failed"}
} else {
s = {code: data.code, status: "failed", message: data.message, details: data.details}
}
status.set(command.id, s)
} else {
s = {code: 200, status: "done"}
status.set(command.id, s)
if (data.data) { // data has changed, commands may do other things instead of changing data
jsontagBuffers.push(data.data) // push changeset to jsontagBuffers so that new query workers get all changes from scratch
meta = data.meta
queryWorkerPool.update({
name: 'update',
req: {
body: jsontagBuffers[jsontagBuffers.length-1], // only add the last change, update tasks for earlier changes have already been sent
meta
}
})
}
}
let l = Object.assign({command:command.id}, s)
appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))
},
//reject()
(error) => {
console.error(error)
let s = {status: "failed", code: error.code, message: error.message, details: error.details}
status.set(command.id, s)
appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))
}
)
} else {
// this code can never be triggered from the post(/command/) route, since it always adds a command to the queue
// so you can only get here from commandWorkerInstance.on() route
// which means that the commandWorkerInstance has finished running the previous command
await commandWorkerInstance.terminate()
commandWorkerInstance.unref() // @FIXME is this needed?
commandWorkerInstance = null // @FIXME or this?
}
}
server.post('/command', async (req, res) => {
let commandId = checkCommand(req, res)
if (!commandId) {
return
}
try {
let commandStr = req.body.toString()
let request = {
method: req.method,
url: req.originalUrl,
query: req.query
}
commandQueue.push({
id:commandId,
command:commandStr,
request,
meta,
data:jsontagBuffers,
commandsFile,
datafile
})
runNextCommand()
} catch(err) {
let s = {code:err.code||500, status:'failed', message:err.message, details:err.details}
status.set(commandId, s)
appendFile(commandStatus, JSONTag.stringify(Object.assign({command:commandId}, s)))
console.error('ERROR: SimplyStore cannot run command ', commandId, err)
}
})
function checkCommand(req, res) {
let error, command, commandOK
let commandStr = req.body.toString() // raw body through express.raw()
try {
command = JSONTag.parse(commandStr)
commandOK = {
command: command?.id,
code: 202,
status: 'accepted'
}
} catch(err) {
error = {
code: 400,
message: "Bad request",
details: err
}
sendResponse({code: 400, body: JSON.stringify(error)}, res)
return false
}
if (!command || !command.id) {
error = {
code: 422,
message: "Command has no id",
details: command
}
sendResponse({code: 422, body: JSON.stringify(error)}, res)
return false
} else if (status.has(command.id)) {
sendResponse({body: JSON.stringify(s)}, res)
return false
} else if (!command.name) {
error = {
code: 422,
message: "Command has no name",
details: command
}
sendResponse({code:422, body: JSON.stringify(error)}, res)
return false
}
appendFile(commandLog, JSONTag.stringify(command))
appendFile(commandStatus, JSONTag.stringify(commandOK))
status.set(command.id, commandOK)
sendResponse({code: 202, body: JSON.stringify(commandOK)}, res)
return command.id
}
server.get('/command/:id', (req, res) => {
if (status.has(req.params.id)) {
let result = status.get(req.params.id)
sendResponse({
jsontag: false,
body: JSON.stringify(result)
},res)
} else {
sendResponse({
code: 404,
jsontag: false,
body: JSON.stringify({code: 404, message: "Command not found", details: req.params.id})
}, res)
}
})
server.listen(port, () => {
console.log('SimplyStore listening on port '+port)
let used = Math.round(process.memoryUsage().rss / 1024 / 1024);
console.log(`(${used} MB)`);
})
}
function sendResponse(response, res) {
if (response.code && httpStatusCodes[response.code]) {
res.status(response.code)
}
if (response.jsontag) {
res.setHeader('content-type','application/jsontag')
} else {
res.setHeader('content-type','application/json')
}
res.send(response.body)+"\n"
}
function sendError(error, res) {
console.error(error)
if (error.code && httpStatusCodes[error.code]) {
res.status(error.code)
} else {
res.status(500)
}
res.setHeader('content-type','application/json')
res.send(JSON.stringify(error))
}
server.run = main
export default server
function accept(req, res, mimetypes, handler) {
let accept = req.accepts(mimetypes)
if (!accept) {
res.status(406)
res.send("<h1>406 Unacceptable</h1>\n")
return false
}
if (typeof handler === 'function') {
return handler(req, res, accept)
}
return true
}
function handleWebRequest(req,res,options)
{
let path = req.path;
path = path.replace(/[^a-z0-9_\.\-\/]*/gi, '') // whitelist acceptable file paths
path = path.replace(/\.+/g, '.') // blacklist '..'
if (!path) {
path = '/'
}
if (path.substring(path.length-1)==='/') {
path += 'index.html'
}
const fileOptions = {
root: options.root
}
if (fs.existsSync(fileOptions.root+path)) {
res.sendFile(path, fileOptions)
} else {
res.sendFile('/index.html', fileOptions)
}
}