@muze-nl/simplystore 0.5.4 → 0.5.6

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/server.mjs +61 -45
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/simplystore",
3
- "version": "0.5.4",
3
+ "version": "0.5.6",
4
4
  "main": "src/server.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/server.mjs CHANGED
@@ -29,43 +29,43 @@ async function main(options) {
29
29
  const commandLog = options.commandLog || './command-log.jsontag'
30
30
  const commandStatus = options.commandStatus || './command-status.jsontag'
31
31
 
32
- server.use(express.static(wwwroot))
32
+ server.use(express.static(wwwroot))
33
33
 
34
- // allow access to raw body, used to parse a query send as post body
34
+ // allow access to raw body, used to parse a query send as post body
35
35
  server.use(express.raw({
36
36
  type: (req) => true // parse body on all requests
37
37
  }))
38
38
 
39
39
  function loadData() {
40
- return new Promise((resolve,reject) => {
41
- let worker = new Worker(loadWorker)
42
- worker.on('message', result => {
43
- resolve(result)
44
- worker.terminate()
45
- })
46
- worker.on('error', error => {
47
- reject(error)
48
- worker.terminate()
49
- })
50
- worker.postMessage(datafile)
51
- })
40
+ return new Promise((resolve,reject) => {
41
+ let worker = new Worker(loadWorker)
42
+ worker.on('message', result => {
43
+ resolve(result)
44
+ worker.terminate()
45
+ })
46
+ worker.on('error', error => {
47
+ reject(error)
48
+ worker.terminate()
49
+ })
50
+ worker.postMessage(datafile)
51
+ })
52
52
  }
53
53
  try {
54
54
  let data = await loadData()
55
- jsontagBuffer = data.data
55
+ jsontagBuffer = data.data
56
56
  meta = data.meta
57
- } catch(err) {
58
- console.error('ERROR: SimplyStore cannot load '+datafile, err)
59
- process.exit(1)
60
- }
57
+ } catch(err) {
58
+ console.error('ERROR: SimplyStore cannot load '+datafile, err)
59
+ process.exit(1)
60
+ }
61
61
 
62
62
  const queryWorkerInitTask = () => {
63
63
  return {
64
- name: 'init',
65
- req: {
66
- body: jsontagBuffer,
64
+ name: 'init',
65
+ req: {
66
+ body: jsontagBuffer,
67
67
  meta
68
- }
68
+ }
69
69
  }
70
70
  }
71
71
 
@@ -94,19 +94,19 @@ async function main(options) {
94
94
  let path = req.path.substr(6) // cut '/query'
95
95
  console.log('query',path)
96
96
  let request = {
97
- method: req.method,
98
- url: req.originalUrl,
99
- query: req.query,
100
- path: path
97
+ method: req.method,
98
+ url: req.originalUrl,
99
+ query: req.query,
100
+ path: path
101
101
  }
102
102
  if (accept(req,res,['application/jsontag'])) {
103
103
  request.jsontag = true
104
104
  }
105
105
  try {
106
- let result = await queryWorkerPool.run('query', request)
107
- sendResponse(result, res)
106
+ let result = await queryWorkerPool.run('query', request)
107
+ sendResponse(result, res)
108
108
  } catch(error) {
109
- sendError(error, res)
109
+ sendError(error, res)
110
110
  }
111
111
  let end = Date.now()
112
112
  console.log(path, (end-start), process.memoryUsage())
@@ -122,20 +122,20 @@ async function main(options) {
122
122
  }
123
123
  let path = req.path.substr(6) // cut '/query'
124
124
  let request = {
125
- method: req.method,
126
- url: req.originalUrl,
127
- query: req.query,
128
- path: path,
129
- body: req.body.toString()
125
+ method: req.method,
126
+ url: req.originalUrl,
127
+ query: req.query,
128
+ path: path,
129
+ body: req.body.toString()
130
130
  }
131
131
  if (accept(req,res,['application/jsontag'])) {
132
132
  request.jsontag = true
133
133
  }
134
134
  try {
135
- let result = await queryWorkerPool.run('query', request)
136
- sendResponse(result, res)
135
+ let result = await queryWorkerPool.run('query', request)
136
+ sendResponse(result, res)
137
137
  } catch(error) {
138
- sendError(error, res)
138
+ sendError(error, res)
139
139
  }
140
140
  let end = Date.now()
141
141
  console.log(path, (end-start), process.memoryUsage())
@@ -153,9 +153,13 @@ async function main(options) {
153
153
  let lines = file.split("\n").filter(Boolean) //filter clears empty lines
154
154
  for(let line of lines) {
155
155
  let command = JSONTag.parse(line)
156
- status.set(command.id, command)
156
+ status.set(command.command, command)
157
157
  }
158
+ } else {
159
+ console.error('Could not open command status',commandStatusFile)
158
160
  }
161
+ } else {
162
+ console.log('no command status', commandStatusFile)
159
163
  }
160
164
  return status
161
165
  }
@@ -257,8 +261,8 @@ async function main(options) {
257
261
  if (!commandId) {
258
262
  return
259
263
  }
260
- let commandStr = req.body.toString()
261
264
  try {
265
+ let commandStr = req.body.toString()
262
266
  let request = {
263
267
  method: req.method,
264
268
  url: req.originalUrl,
@@ -285,12 +289,24 @@ async function main(options) {
285
289
  })
286
290
 
287
291
  function checkCommand(req, res) {
292
+ let error
288
293
  let commandStr = req.body.toString() // raw body through express.raw()
289
- let command = JSONTag.parse(commandStr)
290
- let commandOK = {
291
- command: command?.id,
292
- code: 202,
293
- status: 'accepted'
294
+ try {
295
+ let command = JSONTag.parse(commandStr)
296
+
297
+ let commandOK = {
298
+ command: command?.id,
299
+ code: 202,
300
+ status: 'accepted'
301
+ }
302
+ } catch(err) {
303
+ error = {
304
+ code: 400,
305
+ message: "Bad request",
306
+ details: err
307
+ }
308
+ sendResponse({code: 400, body: JSON.stringify(error)}, res)
309
+ return false
294
310
  }
295
311
  if (!command || !command.id) {
296
312
  error = {