@muze-nl/simplystore 0.6.4 → 0.6.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.
@@ -1,156 +0,0 @@
1
- import JSONTag from '@muze-nl/jsontag';
2
- import {source,isProxy,getIndex, getBuffer} from './symbols.mjs'
3
-
4
- // faststringify function for a fast parseable arraybuffer output
5
- //
6
- const encoder = new TextEncoder()
7
- const decoder = new TextDecoder()
8
-
9
- export default function stringify(value, meta, skipLength=false, index=false) {
10
- let resultArray = []
11
- if (!meta) {
12
- meta = {}
13
- }
14
- if (!meta.index) {
15
- meta.index = {}
16
- }
17
- if (!meta.index.id) {
18
- meta.index.id = new Map()
19
- }
20
- let references = new WeakMap()
21
-
22
- function stringifyValue(value) {
23
- let prop
24
- let typeString = JSONTag.getTypeString(value)
25
- let type = JSONTag.getType(value)
26
- switch (type) {
27
- case 'string':
28
- case 'decimal':
29
- case 'money':
30
- case 'link':
31
- case 'text':
32
- case 'blob':
33
- case 'color':
34
- case 'email':
35
- case 'hash':
36
- case 'duration':
37
- case 'phone':
38
- case 'url':
39
- case 'uuid':
40
- case 'date':
41
- case 'time':
42
- case 'datetime':
43
- if (JSONTag.isNull(value)) {
44
- value = 'null'
45
- } else {
46
- value = JSON.stringify(''+value)
47
- }
48
- prop = typeString + value
49
- break
50
- case 'int':
51
- case 'uint':
52
- case 'int8':
53
- case 'uint8':
54
- case 'int16':
55
- case 'uint16':
56
- case 'int32':
57
- case 'uint32':
58
- case 'int64':
59
- case 'uint64':
60
- case 'float':
61
- case 'float32':
62
- case 'float64':
63
- case 'timestamp':
64
- case 'number':
65
- case 'boolean':
66
- if (JSONTag.isNull(value)) {
67
- value = 'null'
68
- } else {
69
- value = JSON.stringify(value)
70
- }
71
- prop = typeString + value
72
- break
73
- case 'array':
74
- let entries = value.map(e => stringifyValue(e)).join(',')
75
- prop = typeString + '[' + entries + ']'
76
- break
77
- case 'object':
78
- if (!value) {
79
- prop = 'null'
80
- } else if (value[isProxy]) {
81
- prop = decoder.decode(value[getBuffer](current))
82
- } else {
83
- if (!references.has(value)) {
84
- references.set(value, resultArray.length)
85
- resultArray.push(value)
86
- }
87
- prop = '~'+references.get(value)
88
- }
89
- break
90
- default:
91
- throw new Error(JSONTag.getType(value)+' type not yet implemented')
92
- break
93
- }
94
- return prop
95
- }
96
-
97
- const encoder = new TextEncoder()
98
- const decoder = new TextDecoder()
99
-
100
- // is only ever called on object values
101
- // and should always return a stringified object, not a reference (~n)
102
- const innerStringify = (current) => {
103
- let object = resultArray[current]
104
- let result
105
-
106
- // if value is a valueProxy, just copy the input slice
107
- if (object && !JSONTag.isNull(object) && object[isProxy]) {
108
- return decoder.decode(object[getBuffer](current))
109
- }
110
- if (typeof object === 'undefined' || object === null) {
111
- return 'null'
112
- }
113
-
114
- let props = []
115
- for (let key of Object.getOwnPropertyNames(object)) {
116
- let value = object[key]
117
- let prop = stringifyValue(value)
118
- let enumerable = object.propertyIsEnumerable(key) ? '' : '#'
119
- props.push(enumerable+'"'+key+'":'+prop)
120
- }
121
- result = JSONTag.getTypeString(object)+'{'+props.join(',')+'}'
122
- return result
123
- }
124
-
125
- const encode = (s) => {
126
- if (skipLength) {
127
- return s
128
- }
129
- let length = new Blob([s]).size
130
- return '('+length+')'+s
131
- }
132
-
133
- resultArray.push(value)
134
- let current = 0
135
- while(current<resultArray.length) {
136
- resultArray[current] = innerStringify(current)
137
- current++
138
- }
139
-
140
- return resultArray.map(encode).join("\n")
141
- }
142
-
143
- export function stringToSAB(strData) {
144
- const buffer = encoder.encode(strData)
145
- const sab = new SharedArrayBuffer(buffer.length)
146
- let uint8sab = new Uint8Array(sab)
147
- uint8sab.set(buffer,0)
148
- return uint8sab
149
- }
150
-
151
- export function resultSetStringify(resultSet) {
152
- return resultSet.map((e,i) => {
153
- let buffer = e[getBuffer](i)
154
- return '('+buffer.length+')'+decoder.decode(buffer)
155
- }).join("\n")
156
- }
package/src/symbols.mjs DELETED
@@ -1,9 +0,0 @@
1
- export const source = Symbol('source')
2
- export const isProxy = Symbol('isProxy')
3
- export const getBuffer = Symbol('getBuffer')
4
- export const getIndex = Symbol('getIndex')
5
- export const isChanged = Symbol('isChanged')
6
- export const isParsed = Symbol('isParsed')
7
- export const getString = Symbol('getString')
8
- export const position = Symbol('position')
9
- export const parent = Symbol('parent')
@@ -1,12 +0,0 @@
1
- import * as commandWorker from './worker-command.mjs'
2
- import worker_threads from 'node:worker_threads'
3
- import JSONTag from '@muze-nl/jsontag'
4
-
5
- async function initialize() {
6
- let meta = {}
7
- let dataspace = JSONTag.parse(worker_threads.workerData, null, meta)
8
- await commandWorker.initialize(dataspace,meta)
9
- return commandWorker.runCommand
10
- }
11
-
12
- export default initialize()
@@ -1,51 +0,0 @@
1
- import JSONTag from '@muze-nl/jsontag'
2
- import commands from './commands.mjs'
3
-
4
- /**
5
- * Command Worker for threads.js library
6
- * returns JSONTag strings, since otherwise JSON.stringify is used
7
- * and type+attribute data gets lost
8
- */
9
-
10
- let dataspace, meta
11
-
12
- export function setDataspace(d) {
13
- dataspace = d
14
- }
15
-
16
- /**
17
- * @TODO: check for valid command id
18
- * @TODO: write valid commands to command log, emit 'ok', check in server.mjs for that
19
- * @TODO: setTimeout or do above checks in server.mjs before queueing
20
- */
21
-
22
- export async function initialize(jsontag, m) {
23
- if (!jsontag) { throw new Error('missing jsontag parameter')}
24
- dataspace = jsontag
25
- meta = m
26
- return true
27
- }
28
-
29
- export function runCommand({request, commandStr}) {
30
- if (!commandStr) { throw new Error('missing command parameter')}
31
- if (!request) { throw new Error('missing request parameter')}
32
- let response = {
33
- jsontag: true
34
- }
35
- let command = JSONTag.parse(commandStr) // raw body through express.raw()
36
- if (command && command.name && commands[command.name]) {
37
- try {
38
- commands[command.name](dataspace, command, request)
39
- response.body = JSONTag.stringify(dataspace) //@TODO: this is inefficient, patch would be better
40
- } catch(err) {
41
- console.log(err)
42
- response.code = 422;
43
- response.body = '<object class="Error">{"message":'+JSON.stringify(''+err)+',"code":422}'
44
- }
45
- } else {
46
- response.code = 404
47
- response.body = '<object class="Error">{"message":"Command '+command.name+' not found","code":404}'
48
- }
49
- return response
50
- }
51
-