@muze-nl/od-jsontag 0.1.4 → 0.1.5

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # od-jsontag: On Demand JSONTag
2
2
 
3
- This library implements a parser and stringifier for a variant of jsontag which is optimized so that you only need to parse objects that you use and skip parsing any other objects.
3
+ This library implements a parser and stringifier for a variant of [JSONTag](https://github.com/muze-nl/jsontag/) which is optimized so that you only need to parse objects that you use and skip parsing any other objects.
4
4
  This is especially useful to share data between threads or other workers using sharedArrayBuffers, the only shared memory option in javascript currently.
5
5
 
6
6
  The parse function creates in memory Proxy objects that trigger parsing only when accessed. You can use the data as normal objects for most use-cases. The format supports non-enumerable
@@ -9,3 +9,25 @@ properties, which aren't part of the normal JSONTag format. The parse function e
9
9
  The stringify function creates a sharedArrayBuffer, which represents a file with one object per line. Each line is prefixed with a byte counter that indicates the length of the line. References to other objects are encoded as ~n, where n is the line number (starting at 0).
10
10
 
11
11
  The parse function doesn't build an id index, because that requires parsing all objects. Instead the stringify function builds or updates the id index. It isn't included in the string result.
12
+
13
+ In addition to the normal meta options, as defined in the [JSONTag library](https://github.com/muze-nl/jsontag/), od-jsontag adds the `meta.access` option. This must be a function like:
14
+
15
+ ```
16
+ meta.access = (object, property, method) => true | false
17
+ ```
18
+
19
+ If meta.access returns true, access to that property is allowed. Otherwise access is disallowed and the property value returned is `undefined`.
20
+
21
+ The `method` parameter is one of `set`,`get`,`has`,`deleteProperty`,`defineProperty`.
22
+
23
+ Add the access function in the meta parameter of the `parse` method:
24
+
25
+ ```
26
+ import parse from 'od-jsontag'
27
+ meta = {
28
+ access: (object, property, method) => {
29
+ return property=='name'
30
+ }
31
+ }
32
+ const root = parse(string, meta)
33
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@muze-nl/od-jsontag",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "On Demand JSONTag: parse/serialize large datastructures on demand, useful for sharing data between threads",
5
5
  "type": "module",
6
6
  "author": "Auke van Slooten <auke@muze.nl>",
package/src/parse.mjs CHANGED
@@ -25,7 +25,9 @@ export default function parse(input, meta, immutable=true)
25
25
  if (!meta.baseURL) {
26
26
  meta.baseURL = 'http://localhost/'
27
27
  }
28
-
28
+ if (!meta.access) {
29
+ meta.access = () => true
30
+ }
29
31
  let at, ch, value, result;
30
32
  let escapee = {
31
33
  '"': '"',
@@ -688,6 +690,9 @@ export default function parse(input, meta, immutable=true)
688
690
  } else if (prop===isChanged) {
689
691
  return true
690
692
  } else {
693
+ if (!meta.access(target, prop)) {
694
+ return undefined
695
+ }
691
696
  if (Array.isArray(target[prop])) {
692
697
  return new Proxy(target[prop], handlers.newArrayHandler)
693
698
  }
@@ -695,6 +700,9 @@ export default function parse(input, meta, immutable=true)
695
700
  }
696
701
  },
697
702
  set(target, prop, value) {
703
+ if (!meta.access(target, prop)) {
704
+ return undefined
705
+ }
698
706
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
699
707
  value = getNewValueProxy(value)
700
708
  }
@@ -727,6 +735,9 @@ export default function parse(input, meta, immutable=true)
727
735
  return true
728
736
  break
729
737
  default:
738
+ if (!meta.access(target, prop, 'get')) {
739
+ return undefined
740
+ }
730
741
  if (Array.isArray(target[prop])) {
731
742
  return new Proxy(target[prop], handlers.newArrayHandler)
732
743
  }
@@ -735,6 +746,9 @@ export default function parse(input, meta, immutable=true)
735
746
  }
736
747
  },
737
748
  set(target, prop, value) {
749
+ if (!meta.access(target, prop, 'set')) {
750
+ return undefined
751
+ }
738
752
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
739
753
  value = getNewValueProxy(value)
740
754
  }
@@ -764,6 +778,9 @@ export default function parse(input, meta, immutable=true)
764
778
  } else if (prop===isChanged) {
765
779
  return target[parent][isChanged]
766
780
  } else {
781
+ if (!meta.access(target, prop, 'get')) {
782
+ return undefined
783
+ }
767
784
  if (Array.isArray(target[prop])) {
768
785
  target[prop][parent] = target[parent]
769
786
  return new Proxy(target[prop], handlers.arrayHandler)
@@ -775,6 +792,9 @@ export default function parse(input, meta, immutable=true)
775
792
  if (immutable) {
776
793
  throw new Error('dataspace is immutable')
777
794
  }
795
+ if (!meta.access(target, prop, 'set')) {
796
+ return undefined
797
+ }
778
798
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
779
799
  value = getNewValueProxy(value)
780
800
  }
@@ -786,6 +806,9 @@ export default function parse(input, meta, immutable=true)
786
806
  if (immutable) {
787
807
  throw new Error('dataspace is immutable')
788
808
  }
809
+ if (!meta.access(target, prop, 'deleteProperty')) {
810
+ return undefined
811
+ }
789
812
  //FIXME: if target[prop] was the last reference to an object
790
813
  //that object should be deleted so that its line will become empty
791
814
  //when stringifying resultArray again
@@ -799,6 +822,9 @@ export default function parse(input, meta, immutable=true)
799
822
  firstParse(target)
800
823
  switch(prop) {
801
824
  case source:
825
+ if (!meta.access(target, prop, 'get')) {
826
+ return undefined
827
+ }
802
828
  return target
803
829
  break
804
830
  case isProxy:
@@ -823,6 +849,9 @@ export default function parse(input, meta, immutable=true)
823
849
  return target[isChanged]
824
850
  break
825
851
  default:
852
+ if (!meta.access(target, prop, 'get')) {
853
+ return undefined
854
+ }
826
855
  if (Array.isArray(target[prop])) {
827
856
  target[prop][parent] = target
828
857
  return new Proxy(target[prop], handlers.arrayHandler)
@@ -837,6 +866,9 @@ export default function parse(input, meta, immutable=true)
837
866
  }
838
867
  firstParse(target)
839
868
  if (prop!==isChanged) {
869
+ if (prop!=resultSet && !meta.access(target, prop, 'set')) {
870
+ return undefined
871
+ }
840
872
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
841
873
  value = getNewValueProxy(value)
842
874
  }
@@ -849,6 +881,9 @@ export default function parse(input, meta, immutable=true)
849
881
  if (immutable) {
850
882
  throw new Error('dataspace is immutable')
851
883
  }
884
+ if (!meta.access(target, prop, 'deleteProperty')) {
885
+ return undefined
886
+ }
852
887
  firstParse(target)
853
888
  delete target[prop]
854
889
  target[isChanged] = true
@@ -866,10 +901,16 @@ export default function parse(input, meta, immutable=true)
866
901
  if (immutable) {
867
902
  throw new Error('dataspace is immutable')
868
903
  }
904
+ if (!meta.access(target, prop, 'defineProperty')) {
905
+ return undefined
906
+ }
869
907
  firstParse(target)
870
908
  Object.defineProperty(target, prop, descriptor)
871
909
  },
872
910
  has(target, prop) {
911
+ if (!meta.access(target, prop, 'has')) {
912
+ return false
913
+ }
873
914
  firstParse()
874
915
  return prop in target
875
916
  },
@@ -1,65 +0,0 @@
1
- import JSONTag from '@muze-nl/jsontag'
2
- import {source} from './symbols.mjs'
3
-
4
- function getSource(obj) {
5
- if (obj && Object.hasOwn(obj, source)) {
6
- obj = obj[source]
7
- }
8
- return obj
9
- }
10
-
11
- export function getType(obj) {
12
- obj = getSource(obj)
13
- return JSONTag.getType(obj)
14
- }
15
-
16
- export function getAttribute(obj, attr) {
17
- obj = getSource(obj)
18
- return JSONTag.getAttribute(obj, attr)
19
- }
20
-
21
- export function getAttributes(obj) {
22
- obj = getSource(obj)
23
- return JSONTag.getAttributes(obj)
24
- }
25
-
26
- export function getAttributeString(obj) {
27
- obj = getSource(obj)
28
- return JSONTag.getAttributesString(obj)
29
- }
30
-
31
- export function getTypeString(obj) {
32
- obj = getSource(obj)
33
- return JSONTag.getTypeString(obj)
34
- }
35
-
36
- export function isNull(obj) {
37
- obj = getSource(obj)
38
- return JSONTag.isNull(obj)
39
- }
40
-
41
- export function setAttribute(obj, attr, value) {
42
- obj = getSource(obj)
43
- return JSONTag.setAttribute(obj, attr, value)
44
- }
45
-
46
- export function setAttributes(obj, attr) {
47
- obj = getSource(obj)
48
- return JSONTag.setAttribute(obj, attr)
49
- }
50
-
51
- export function setType(obj, type) {
52
- obj = getSource(obj)
53
- return JSONTag.setType(obj, type)
54
- }
55
-
56
- export function addAttribute(obj, attr, value) {
57
- obj = getSource(obj)
58
- return JSONTag.addAttribute(obj, attr, value)
59
- }
60
-
61
- export function removeAttribute(obj, attr) {
62
- obj = getSource(obj)
63
- return JSONTag.removeAttribute(obj, attr)
64
- }
65
-
package/src/jsontag.mjs~ DELETED
@@ -1,65 +0,0 @@
1
- import JSONTag from '@muze-nl/jsontag'
2
- import {source} from './symbols.mjs'
3
-
4
- function getSource(obj) {
5
- if (obj.hasOwnProperty(source)) {
6
- obj = obj[source]
7
- }
8
- return obj
9
- }
10
-
11
- export function getType(obj) {
12
- obj = getSource(obj)
13
- return JSONTag.getType(obj)
14
- }
15
-
16
- export function getAttribute(obj, attr) {
17
- obj = getSource(obj)
18
- return JSONTag.getAttribute(obj, attr)
19
- }
20
-
21
- export function getAttributes(obj) {
22
- obj = getSource(obj)
23
- return JSONTag.getAttributes(obj)
24
- }
25
-
26
- export function getAttributeString(obj) {
27
- obj = getSource(obj)
28
- return JSONTag.getAttributesString(obj)
29
- }
30
-
31
- export function getTypeString(obj) {
32
- obj = getSource(obj)
33
- return JSONTag.getTypeString(obj)
34
- }
35
-
36
- export function isNull(obj) {
37
- obj = getSource(obj)
38
- return JSONTag.isNull(obj)
39
- }
40
-
41
- export function setAttribute(obj, attr, value) {
42
- obj = getSource(obj)
43
- return JSONTag.setAttribute(obj, attr, value)
44
- }
45
-
46
- export function setAttributes(obj, attr) {
47
- obj = getSource(obj)
48
- return JSONTag.setAttribute(obj, attr)
49
- }
50
-
51
- export function setType(obj, type) {
52
- obj = getSource(obj)
53
- return JSONTag.setType(obj, type)
54
- }
55
-
56
- export function addAttribute(obj, attr, value) {
57
- obj = getSource(obj)
58
- return JSONTag.addAttribute(obj, attr, value)
59
- }
60
-
61
- export function removeAttribute(obj, attr) {
62
- obj = getSource(obj)
63
- return JSONTag.removeAttribute(obj, attr)
64
- }
65
-