@muze-nl/simplystore 0.5.3 → 0.5.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/simplystore",
3
- "version": "0.5.3",
3
+ "version": "0.5.4",
4
4
  "main": "src/server.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  import JSONTag from '@muze-nl/jsontag'
2
- import {source} from './symbols.mjs'
2
+ import {source, isChanged} from './symbols.mjs'
3
3
  import fastParse from './fastParse.mjs'
4
4
  import {stringToSAB,resultSetStringify} from './fastStringify.mjs'
5
5
  import writeFileAtomic from 'write-file-atomic'
@@ -25,7 +25,7 @@ export const metaIdProxy = {
25
25
  })
26
26
  },
27
27
  set: (id,ref) => {
28
- //FICME: is this correct?
28
+ //FIXME: is this correct?
29
29
  meta.index.id.set(id, resultSet.length-1)
30
30
  },
31
31
  get: (id) => {
@@ -46,7 +46,10 @@ export const metaIdProxy = {
46
46
  export const FastJSONTag = {
47
47
  getType: (obj) => JSONTag.getType(obj[source]),
48
48
  getAttribute: (obj, attr) => JSONTag.getAttribute(obj[source],attr),
49
- setAttribute: (obj, attr, value) => JSONTag.setAttribute(obj[source], attr, value),
49
+ setAttribute: (obj, attr, value) => {
50
+ obj[isChanged] = true
51
+ return JSONTag.setAttribute(obj[source], attr, value)
52
+ },
50
53
  getAttributes: (obj) => JSONTag.getAttributes(obj[source]),
51
54
  getAttributeString: (obj) => JSONTag.getAttributesString(obj[source]),
52
55
  getTypeString: (obj) => JSONTag.getTypeString(obj[source])
@@ -59,6 +62,7 @@ export async function initialize(task) {
59
62
  metaProxy.index.id = metaIdProxy
60
63
  datafile = task.datafile
61
64
  commands = await import(task.commandsFile).then(mod => {
65
+ console.log('commands loaded:',Object.keys(mod.default))
62
66
  return mod.default
63
67
  })
64
68
  }
@@ -71,29 +75,28 @@ export default async function runCommand(commandStr, request) {
71
75
  jsontag: true
72
76
  }
73
77
  if (commands[task.name]) {
74
- try {
75
- commands[task.name](dataspace, task, request, metaProxy)
76
- FastJSONTag.setAttribute(dataspace, 'command', task.id)
78
+ let time = Date.now()
79
+ commands[task.name](dataspace, task, request, metaProxy)
80
+ FastJSONTag.setAttribute(dataspace, 'command', task.id)
77
81
 
78
- const strData = resultSetStringify(resultSet)
79
- const uint8sab = stringToSAB(strData)
80
- response.data = uint8sab
81
- response.meta = {
82
- index: {
83
- id: meta.index.id
84
- }
82
+ const strData = resultSetStringify(resultSet)
83
+ const uint8sab = stringToSAB(strData)
84
+ response.data = uint8sab
85
+ response.meta = {
86
+ index: {
87
+ id: meta.index.id
85
88
  }
86
-
87
- await writeFileAtomic(datafile, uint8sab)
88
- } catch(err) {
89
- console.error('error',err)
90
- response.code = 422;
91
- response.body = '<object class="Error">{"message":'+JSON.stringify(''+err)+',"code":422}'
92
89
  }
90
+
91
+ await writeFileAtomic(datafile, uint8sab)
92
+ let end = Date.now()
93
+ console.log('task time',end-time)
93
94
  } else {
94
- console.error('Command not found', task.name, commands)
95
- response.code = 404
96
- response.body = '<object class="Error">{"message":"Command '+task.name+' not found","code":404}'
95
+ console.error('Command not found', task.name)
96
+ throw {
97
+ code: 404,
98
+ message: "Command "+task.name+" not found"
99
+ }
97
100
  }
98
101
  return response
99
102
  }
@@ -3,11 +3,7 @@ import runCommand, { initialize } from '../src/command-worker-module.mjs'
3
3
 
4
4
  parentPort.on('message', async data => {
5
5
  let result
6
- try {
7
- await initialize(data)
8
- result = await runCommand(data.command)
9
- } catch(err) {
10
- result = { error: err.message }
11
- }
6
+ await initialize(data)
7
+ result = await runCommand(data.command)
12
8
  parentPort.postMessage(result)
13
9
  })
package/src/fastParse.mjs CHANGED
@@ -838,10 +838,12 @@ export default function parse(input, meta, immutable=true)
838
838
  set(target, prop, value) {
839
839
  if (!immutable) {
840
840
  firstParse()
841
- if (JSONTag.getType(value)==='object' && !value[isProxy]) {
842
- value = getNewValueProxy(value)
841
+ if (prop!==isChanged) {
842
+ if (JSONTag.getType(value)==='object' && !value[isProxy]) {
843
+ value = getNewValueProxy(value)
844
+ }
845
+ target[prop] = value
843
846
  }
844
- target[prop] = value
845
847
  targetIsChanged = true
846
848
  return true
847
849
  }
package/src/server.mjs CHANGED
@@ -153,7 +153,7 @@ 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.status)
156
+ status.set(command.id, command)
157
157
  }
158
158
  }
159
159
  }
@@ -208,31 +208,38 @@ async function main(options) {
208
208
  start(
209
209
  // resolve()
210
210
  (data) => {
211
- if (!data || data.error) {
212
- console.error('ERROR: SimplyStore cannot run command ', command.id, err)
213
- if (!data.error) {
214
- status.set(command.id, 'failed')
215
- throw new Error('Unexpected command failure')
211
+ let s
212
+ if (!data || (data.code>=300 && data.code<=499)) {
213
+ console.error('ERROR: SimplyStore cannot run command ', command.id, data)
214
+ if (!data?.code) {
215
+ s = {code: 500, status: "failed"}
216
216
  } else {
217
- status.set(command.id, data.error)
218
- throw data.error
217
+ s = {code: data.code, status: "failed", message: data.message, details: data.details}
218
+ }
219
+ status.set(command.id, s)
220
+ } else {
221
+ s = {code: 200, status: "done"}
222
+ status.set(command.id, s)
223
+ if (data.data) { // data has changed, commands may do other things instead of changing data
224
+ jsontagBuffer = data.data
225
+ meta = data.meta
226
+ // restart query workers with new data
227
+ let oldPool = queryWorkerPool
228
+ queryWorkerPool = new WorkerPool(maxWorkers, queryWorker, queryWorkerInitTask())
229
+ setTimeout(() => {
230
+ oldPool.close()
231
+ }, 2000)
219
232
  }
220
233
  }
221
- jsontagBuffer = data.data
222
- meta = data.meta
223
- status.set(command.id, 'done')
224
- appendFile(commandStatus, JSONTag.stringify({command:command.id, status: 'done'}))
225
- // restart query workers with new data
226
- let oldPool = queryWorkerPool
227
- queryWorkerPool = new WorkerPool(maxWorkers, queryWorker, queryWorkerInitTask())
228
- setTimeout(() => {
229
- oldPool.close()
230
- }, 2000)
234
+ let l = Object.assign({command:command.id}, s)
235
+ console.log('appending status ',l,s)
236
+ appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))
231
237
  },
232
238
  //reject()
233
239
  (error) => {
234
- status.set(command.id, error)
235
- appendFile(commandStatus, JSONTag.stringify({command:command.id, status: error}))
240
+ let s = {status: "failed", code: error.code, message: error.message, details: error.details}
241
+ status.set(command.id, s)
242
+ appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))
236
243
  }
237
244
  )
238
245
  } else {
@@ -270,8 +277,9 @@ async function main(options) {
270
277
 
271
278
  runNextCommand()
272
279
  } catch(err) {
273
- appendFile(commandStatus, JSONTag.stringify({command:commandId, status: 'ERROR: '+err.message}))
274
- status.set(commandId, 'ERROR: '+err.message)
280
+ let s = {code:err.code||500, status:'failed', message:err.message, details:err.details}
281
+ status.set(commandId, s)
282
+ appendFile(commandStatus, JSONTag.stringify(Object.assign({command:commandId}, s)))
275
283
  console.error('ERROR: SimplyStore cannot run command ', commandId, err)
276
284
  }
277
285
  })
@@ -279,32 +287,35 @@ async function main(options) {
279
287
  function checkCommand(req, res) {
280
288
  let commandStr = req.body.toString() // raw body through express.raw()
281
289
  let command = JSONTag.parse(commandStr)
290
+ let commandOK = {
291
+ command: command?.id,
292
+ code: 202,
293
+ status: 'accepted'
294
+ }
282
295
  if (!command || !command.id) {
283
296
  error = {
284
297
  code: 422,
285
- message: "Command has no id"
298
+ message: "Command has no id",
299
+ details: command
286
300
  }
287
301
  sendResponse({code: 422, body: JSON.stringify(error)}, res)
288
302
  return false
289
303
  } else if (status.has(command.id)) {
290
- result = "OK"
291
- sendResponse({body: JSON.stringify(result)}, res)
304
+ sendResponse({body: JSON.stringify(s)}, res)
292
305
  return false
293
306
  } else if (!command.name) {
294
307
  error = {
295
308
  code: 422,
296
- message: "Command has no name"
309
+ message: "Command has no name",
310
+ details: command
297
311
  }
298
312
  sendResponse({code:422, body: JSON.stringify(error)}, res)
299
313
  return false
300
314
  }
301
315
  appendFile(commandLog, JSONTag.stringify(command))
302
- appendFile(commandStatus, JSONTag.stringify({
303
- command: command.id,
304
- status: 'accepted'
305
- }))
306
- status.set(command.id, 'accepted')
307
- sendResponse({code: 202, body: '"Accepted"'}, res)
316
+ appendFile(commandStatus, JSONTag.stringify(commandOK))
317
+ status.set(command.id, commandOK)
318
+ sendResponse({code: 202, body: JSON.stringify(commandOK)}, res)
308
319
  return command.id
309
320
  }
310
321
 
@@ -319,7 +330,7 @@ async function main(options) {
319
330
  sendResponse({
320
331
  code: 404,
321
332
  jsontag: false,
322
- body: JSON.stringify({code: 404, message: "Command not found"})
333
+ body: JSON.stringify({code: 404, message: "Command not found", details: req.params.id})
323
334
  }, res)
324
335
  }
325
336
  })