@muze-nl/simplystore 0.6.12 → 0.6.14

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.6.12",
3
+ "version": "0.6.14",
4
4
  "main": "src/server.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -51,6 +51,13 @@ export const metaIdProxy = {
51
51
  }
52
52
  }
53
53
 
54
+ const metaReadProxy = {
55
+ foreach: metaProxy.forEach,
56
+ get: metaProxy.get,
57
+ has: metaProxy.has,
58
+ set: meta.set
59
+ }
60
+
54
61
  export async function initialize(task) {
55
62
  for(let jsontag of task.data) {
56
63
  dataspace = parse(jsontag, task.meta, false) // false means mutable
@@ -60,45 +67,49 @@ export async function initialize(task) {
60
67
  metaProxy.index.id = metaIdProxy
61
68
  datafile = task.datafile
62
69
  commands = await import(task.commandsFile).then(mod => {
63
- console.log('commands loaded:',Object.keys(mod.default))
64
70
  return mod.default
65
71
  })
66
72
  }
67
73
 
68
74
  export default async function runCommand(commandStr, request) {
69
- let task = JSONTag.parse(commandStr, null, metaProxy)
70
- if (!task.id) { throw new Error('missing command id')}
71
- if (!task.name) { throw new Error('missing command name parameter')}
72
75
  let response = {
73
76
  jsontag: true
74
77
  }
75
- if (commands[task.name]) {
76
- let time = Date.now()
77
- commands[task.name](dataspace, task, request, metaProxy)
78
- //TODO: if command/task makes no changes, skip updating data.jsontag and writing it, skip response.data
79
- JSONTag.setAttribute(dataspace, 'command', task.id)
80
-
81
- const uint8sab = serialize(dataspace, {meta, changes: true}) // serialize only changes
82
- response.data = uint8sab
83
- response.meta = {
84
- index: {
85
- id: meta.index.id
78
+ try {
79
+ let task = JSONTag.parse(commandStr, null, metaReadProxy)
80
+ if (!task.id) { throw new Error('missing command id')}
81
+ if (!task.name) { throw new Error('missing command name parameter')}
82
+ if (commands[task.name]) {
83
+ let time = Date.now()
84
+ commands[task.name](dataspace, task, request, metaProxy)
85
+ //TODO: if command/task makes no changes, skip updating data.jsontag and writing it, skip response.data
86
+ JSONTag.setAttribute(dataspace, 'command', task.id)
87
+
88
+ const uint8sab = serialize(dataspace, {meta, changes: true}) // serialize only changes
89
+ response.data = uint8sab
90
+ response.meta = {
91
+ index: {
92
+ id: meta.index.id
93
+ }
86
94
  }
87
- }
88
- //TODO: write data every x commands or x minutes, in seperate thread
95
+ //TODO: write data every x commands or x minutes, in seperate thread
89
96
 
90
- let newfilename = datafile + (meta.parts ? '.'+meta.parts : '')
91
- await writeFileAtomic(newfilename, uint8sab)
92
- meta.parts++
93
- response.meta.parts = meta.parts
94
- let end = Date.now()
95
- console.log('task time',end-time)
96
- } else {
97
- console.error('Command not found', task.name)
98
- throw {
99
- code: 404,
100
- message: "Command "+task.name+" not found"
97
+ let newfilename = datafile + (meta.parts ? '.'+meta.parts : '')
98
+ await writeFileAtomic(newfilename, uint8sab)
99
+ meta.parts++
100
+ response.meta.parts = meta.parts
101
+ let end = Date.now()
102
+ console.log('task time',end-time)
103
+ } else {
104
+ console.error('Command not found', task.name)
105
+ throw {
106
+ code: 404,
107
+ message: "Command "+task.name+" not found"
108
+ }
101
109
  }
110
+ } catch(err) {
111
+ console.error('task error', err)
112
+ throw err
102
113
  }
103
114
  return response
104
115
  }
@@ -2,11 +2,11 @@ import pointer from 'json-pointer'
2
2
  import {VM} from 'vm2'
3
3
  import { memoryUsage } from 'node:process'
4
4
  import JSONTag from '@muze-nl/jsontag'
5
+ import * as odJSONTag from '@muze-nl/od-jsontag/src/jsontag.mjs'
5
6
  import {source, isProxy, resultSet} from '@muze-nl/od-jsontag/src/symbols.mjs'
6
7
  import parse from '@muze-nl/od-jsontag/src/parse.mjs'
7
8
  import {_,from,not,anyOf,allOf,asc,desc,sum,count,avg,max,min} from 'jaqt'
8
9
 
9
- let resultArr = []
10
10
  let dataspace
11
11
  let meta = {}
12
12
  let metaProxy = {
@@ -14,11 +14,49 @@ let metaProxy = {
14
14
  }
15
15
  }
16
16
 
17
+ function protect(target) {
18
+ if (target[source]) {
19
+ throw new Error('Data is immutable')
20
+ }
21
+ }
22
+
23
+ const myJSONTag = {
24
+ getAttribute: odJSONTag.getAttribute,
25
+ getAttributes: odJSONTag.getAttributes,
26
+ getType: odJSONTag.getType,
27
+ getTypeString: odJSONTag.getTypeString,
28
+ setAttribute: (target, name, value) => {
29
+ protect(target)
30
+ return odJSONTag.setAttribute(target, name, value)
31
+ },
32
+ setType: (target, type) => {
33
+ protect(target)
34
+ return odJSONTag.setType(target, type)
35
+ },
36
+ setAttributes: (target, attributes) => {
37
+ protect(target)
38
+ return odJSONTag.setAttributes(target, attributes)
39
+ },
40
+ addAttribute: (target, name, value) => {
41
+ protect(target)
42
+ return odJSONTag.addAttribute(target, name, value)
43
+ },
44
+ removeAttribute: (target, name) => {
45
+ protect(target)
46
+ return odJSONTag.removeAttribute(target, name)
47
+ },
48
+ getAttributesString: odJSONTag.getAttributesString,
49
+ isNull: odJSONTag.isNull,
50
+ clone: JSONTag.clone,
51
+ Link: JSONTag.Link,
52
+ Null: JSONTag.Null
53
+ }
54
+
17
55
  const metaIdProxy = {
18
56
  get: (id) => {
19
57
  let index = meta.index.id.get(id)
20
58
  if (index || index===0) {
21
- return resultArr[index]
59
+ return meta.resultArr[index]
22
60
  }
23
61
  },
24
62
  has: (id) => {
@@ -39,7 +77,6 @@ const tasks = {
39
77
  for (let sab of task.req.body) { //body contains an array of sharedArrayBuffers with initial data and changes
40
78
  dataspace = parse(sab, meta)
41
79
  }
42
- resultArr = meta.resultArray
43
80
  metaProxy.index.id = metaIdProxy
44
81
  //@TODO: add meta.index.references? and baseURL
45
82
  return true
@@ -49,7 +86,6 @@ const tasks = {
49
86
  meta.index = task.req.meta.index
50
87
  }
51
88
  dataspace = parse(task.req.body, meta) //update only has a single changeset
52
- resultArr = meta.resultArray
53
89
  return true
54
90
  },
55
91
  query: async (task) => {
@@ -95,7 +131,7 @@ export function runQuery(pointer, request, query) {
95
131
  max,
96
132
  min,
97
133
  // console: connectConsole(res),
98
- JSONTag,
134
+ JSONTag: myJSONTag,
99
135
  request
100
136
  },
101
137
  wasm: false
package/src/server.mjs CHANGED
@@ -245,6 +245,7 @@ async function main(options) {
245
245
  },
246
246
  //reject()
247
247
  (error) => {
248
+ console.error(error)
248
249
  let s = {status: "failed", code: error.code, message: error.message, details: error.details}
249
250
  status.set(command.id, s)
250
251
  appendFile(commandStatus, JSONTag.stringify(Object.assign({command:command.id}, s)))