@muze-nl/simplystore 0.5.5 → 0.5.7

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.5",
3
+ "version": "0.5.7",
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, isChanged} from './symbols.mjs'
2
+ import {source, isChanged, getIndex} 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,8 +25,16 @@ export const metaIdProxy = {
25
25
  })
26
26
  },
27
27
  set: (id,ref) => {
28
- //FIXME: is this correct?
29
- meta.index.id.set(id, resultSet.length-1)
28
+ if (!meta.index.id.has(id)) {
29
+ if (ref[getIndex]) {
30
+ meta.index.id.set(id, ref[getIndex])
31
+ } else {
32
+ throw new Error('cannot set index.id for non-proxy')
33
+ }
34
+ } else {
35
+ let line = meta.index.id.get(id)
36
+ resultSet[line] = ref
37
+ }
30
38
  },
31
39
  get: (id) => {
32
40
  let index = meta.index.id.get(id)
@@ -44,15 +52,16 @@ export const metaIdProxy = {
44
52
  }
45
53
 
46
54
  export const FastJSONTag = {
47
- getType: (obj) => JSONTag.getType(obj[source]),
48
- getAttribute: (obj, attr) => JSONTag.getAttribute(obj[source],attr),
55
+ getType: (obj) => JSONTag.getType(obj?.[source]),
56
+ getAttribute: (obj, attr) => JSONTag.getAttribute(obj?.[source],attr),
49
57
  setAttribute: (obj, attr, value) => {
58
+ if (!obj) return
50
59
  obj[isChanged] = true
51
60
  return JSONTag.setAttribute(obj[source], attr, value)
52
61
  },
53
- getAttributes: (obj) => JSONTag.getAttributes(obj[source]),
54
- getAttributeString: (obj) => JSONTag.getAttributesString(obj[source]),
55
- getTypeString: (obj) => JSONTag.getTypeString(obj[source])
62
+ getAttributes: (obj) => JSONTag.getAttributes(obj?.[source]),
63
+ getAttributeString: (obj) => JSONTag.getAttributesString(obj?.[source]),
64
+ getTypeString: (obj) => JSONTag.getTypeString(obj?.[source])
56
65
  }
57
66
 
58
67
  export async function initialize(task) {
@@ -77,7 +86,6 @@ export default async function runCommand(commandStr, request) {
77
86
  if (commands[task.name]) {
78
87
  let time = Date.now()
79
88
  commands[task.name](dataspace, task, request, metaProxy)
80
- //TODO: if command/task makes no changes, skip updating data.jsontag and writing it, skip response.data
81
89
  FastJSONTag.setAttribute(dataspace, 'command', task.id)
82
90
 
83
91
  const strData = resultSetStringify(resultSet)
@@ -88,7 +96,7 @@ export default async function runCommand(commandStr, request) {
88
96
  id: meta.index.id
89
97
  }
90
98
  }
91
- //TODO: write data every x commands or x minutes, in seperate thread
99
+
92
100
  await writeFileAtomic(datafile, uint8sab)
93
101
  let end = Date.now()
94
102
  console.log('task time',end-time)
package/src/fastParse.mjs CHANGED
@@ -726,7 +726,7 @@ export default function parse(input, meta, immutable=true)
726
726
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
727
727
  value = getNewValueProxy(value)
728
728
  }
729
- target[prop] = val
729
+ target[prop] = value
730
730
  return true
731
731
  }
732
732
  }
@@ -761,6 +761,9 @@ export default function parse(input, meta, immutable=true)
761
761
  get(target, prop) {
762
762
  if (target[prop] instanceof Function) {
763
763
  if (['copyWithin','fill','pop','push','reverse','shift','sort','splice','unshift'].indexOf(prop)!==-1) {
764
+ if (immutable) {
765
+ throw new Error('dataspace is immutable')
766
+ }
764
767
  targetIsChanged = true
765
768
  }
766
769
  return (...args) => {
@@ -782,6 +785,9 @@ export default function parse(input, meta, immutable=true)
782
785
  }
783
786
  },
784
787
  set(target, prop, value) {
788
+ if (immutable) {
789
+ throw new Error('dataspace is immutable')
790
+ }
785
791
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
786
792
  value = getNewValueProxy(value)
787
793
  }
@@ -790,6 +796,9 @@ export default function parse(input, meta, immutable=true)
790
796
  return true
791
797
  },
792
798
  deleteProperty(target, prop) {
799
+ if (immutable) {
800
+ throw new Error('dataspace is immutable')
801
+ }
793
802
  //FIXME: if target[prop] was the last reference to an object
794
803
  //that object should be deleted so that its line will become empty
795
804
  //when stringifying resultArray again
@@ -828,7 +837,7 @@ export default function parse(input, meta, immutable=true)
828
837
  return targetIsChanged
829
838
  break
830
839
  default:
831
- if (!immutable && Array.isArray(target[prop])) {
840
+ if (Array.isArray(target[prop]) && target[prop].propertyIsEnumerable()) {
832
841
  return new Proxy(target[prop], arrayHandler)
833
842
  }
834
843
  return target[prop] // FIXME: make arrays immutable as well
@@ -1021,5 +1030,4 @@ export default function parse(input, meta, immutable=true)
1021
1030
  }
1022
1031
 
1023
1032
  return resultArray
1024
- }
1025
-
1033
+ }
package/src/server.mjs CHANGED
@@ -261,8 +261,8 @@ async function main(options) {
261
261
  if (!commandId) {
262
262
  return
263
263
  }
264
- let commandStr = req.body.toString()
265
264
  try {
265
+ let commandStr = req.body.toString()
266
266
  let request = {
267
267
  method: req.method,
268
268
  url: req.originalUrl,
@@ -289,12 +289,24 @@ async function main(options) {
289
289
  })
290
290
 
291
291
  function checkCommand(req, res) {
292
+ let error, command, commandOK
292
293
  let commandStr = req.body.toString() // raw body through express.raw()
293
- let command = JSONTag.parse(commandStr)
294
- let commandOK = {
295
- command: command?.id,
296
- code: 202,
297
- status: 'accepted'
294
+ try {
295
+ command = JSONTag.parse(commandStr)
296
+
297
+ 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
298
310
  }
299
311
  if (!command || !command.id) {
300
312
  error = {