@muze-nl/simplystore 0.4.6 → 0.5.0

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.4.6",
3
+ "version": "0.5.0",
4
4
  "main": "src/server.mjs",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -17,17 +17,25 @@
17
17
  "homepage": "https://github.com/simplyedit/simplystore#readme",
18
18
  "dependencies": {
19
19
  "@muze-nl/jsontag": "^0.8.6",
20
- "array-where-select": "^0.4.4",
20
+ "array-where-select": "^0.4.7",
21
21
  "codemirror": "^6.0.1",
22
22
  "express": "^4.18.1",
23
23
  "json-pointer": "^0.6.2",
24
24
  "jsonpath-plus": "^7.2.0",
25
25
  "piscina": "^4.1.0",
26
- "vm2": "^3.9.13"
26
+ "vm2": "^3.9.13",
27
+ "write-file-atomic": "^5.0.1"
27
28
  },
28
29
  "devDependencies": {
29
30
  "eslint": "^8.48.0",
30
31
  "process": "^0.11.10",
31
32
  "tap": "^16.3.8"
32
- }
33
+ },
34
+ "files": [
35
+ "README.md",
36
+ "LICENSE",
37
+ "src/",
38
+ "www/",
39
+ "scripts/"
40
+ ]
33
41
  }
@@ -0,0 +1,25 @@
1
+ import JSONTag from '@muze-nl/jsontag'
2
+ import fastStringify from '../src/fastStringify.mjs'
3
+ import fs from 'node:fs'
4
+
5
+ if (process.argv.length<=3) {
6
+ console.log('usage: node ./convert.mjs {inputfile} {outputfile}')
7
+ process.exit()
8
+ }
9
+
10
+ // parse command line
11
+ let inputFile = process.argv[2]
12
+ let outputFile = process.argv[3]
13
+
14
+ // load file
15
+ let input = fs.readFileSync(inputFile, 'utf-8')
16
+
17
+ // parse jsontag
18
+ let data = JSONTag.parse(input)
19
+
20
+ // write resultset to output
21
+ let strData = fastStringify(data)
22
+
23
+ fs.writeFileSync(outputFile, strData)
24
+
25
+ console.log('Converted data written to ',outputFile)
@@ -0,0 +1,100 @@
1
+ import JSONTag from '@muze-nl/jsontag'
2
+ import {source} from './symbols.mjs'
3
+ import fastParse from './fastParse.mjs'
4
+ import {stringToSAB,resultSetStringify} from './fastStringify.mjs'
5
+ import writeFileAtomic from 'write-file-atomic'
6
+
7
+ let commands = {}
8
+ let resultSet = []
9
+ let dataspace
10
+ let datafile
11
+ let meta = {}
12
+ let metaProxy = {
13
+ index: {
14
+ }
15
+ }
16
+
17
+ export const metaIdProxy = {
18
+ forEach: (callback) => {
19
+ meta.index.id.forEach((ref,id) => {
20
+ callback({
21
+ deref: () => {
22
+ return resultSet[ref]
23
+ }
24
+ },id)
25
+ })
26
+ },
27
+ set: (id,ref) => {
28
+ //FICME: is this correct?
29
+ meta.index.id.set(id, resultSet.length-1)
30
+ },
31
+ get: (id) => {
32
+ let index = meta.index.id.get(id)
33
+ if (index || index===0) {
34
+ return {
35
+ deref: () => {
36
+ return resultSet[index]
37
+ }
38
+ }
39
+ }
40
+ },
41
+ has: (id) => {
42
+ return meta.index.id.has(id)
43
+ }
44
+ }
45
+
46
+ export const FastJSONTag = {
47
+ getType: (obj) => JSONTag.getType(obj[source]),
48
+ getAttribute: (obj, attr) => JSONTag.getAttribute(obj[source],attr),
49
+ setAttribute: (obj, attr, value) => JSONTag.setAttribute(obj[source], attr, value),
50
+ getAttributes: (obj) => JSONTag.getAttributes(obj[source]),
51
+ getAttributeString: (obj) => JSONTag.getAttributesString(obj[source]),
52
+ getTypeString: (obj) => JSONTag.getTypeString(obj[source])
53
+ }
54
+
55
+ export async function initialize(task) {
56
+ resultSet = fastParse(task.data, task.meta, false) // false means mutable
57
+ dataspace = resultSet[0]
58
+ meta = task.meta
59
+ metaProxy.index.id = metaIdProxy
60
+ datafile = task.datafile
61
+ commands = await import(task.commandsFile).then(mod => {
62
+ return mod.default
63
+ })
64
+ }
65
+
66
+ export default async function runCommand(commandStr, request) {
67
+ let task = JSONTag.parse(commandStr, null, metaProxy)
68
+ if (!task.id) { throw new Error('missing command id')}
69
+ if (!task.name) { throw new Error('missing command name parameter')}
70
+ let response = {
71
+ jsontag: true
72
+ }
73
+ if (commands[task.name]) {
74
+ try {
75
+ commands[task.name](dataspace, task, request, metaProxy)
76
+ FastJSONTag.setAttribute(dataspace, 'command', task.id)
77
+
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
+ }
85
+ }
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
+ }
93
+ } 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}'
97
+ }
98
+ return response
99
+ }
100
+
@@ -0,0 +1,13 @@
1
+ import { parentPort } from 'node:worker_threads'
2
+ import runCommand, { initialize } from '../src/command-worker-module.mjs'
3
+
4
+ parentPort.on('message', async data => {
5
+ let result
6
+ try {
7
+ await initialize(data)
8
+ result = await runCommand(data.command)
9
+ } catch(err) {
10
+ result = { error: err.message }
11
+ }
12
+ parentPort.postMessage(result)
13
+ })