@muze-nl/od-jsontag 0.1.4 → 0.1.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.
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.6",
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,6 @@ export default function parse(input, meta, immutable=true)
25
25
  if (!meta.baseURL) {
26
26
  meta.baseURL = 'http://localhost/'
27
27
  }
28
-
29
28
  let at, ch, value, result;
30
29
  let escapee = {
31
30
  '"': '"',
@@ -688,6 +687,9 @@ export default function parse(input, meta, immutable=true)
688
687
  } else if (prop===isChanged) {
689
688
  return true
690
689
  } else {
690
+ if (meta.access && !meta.access(target, prop)) {
691
+ return undefined
692
+ }
691
693
  if (Array.isArray(target[prop])) {
692
694
  return new Proxy(target[prop], handlers.newArrayHandler)
693
695
  }
@@ -695,6 +697,9 @@ export default function parse(input, meta, immutable=true)
695
697
  }
696
698
  },
697
699
  set(target, prop, value) {
700
+ if (meta.access && !meta.access(target, prop)) {
701
+ return undefined
702
+ }
698
703
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
699
704
  value = getNewValueProxy(value)
700
705
  }
@@ -727,6 +732,9 @@ export default function parse(input, meta, immutable=true)
727
732
  return true
728
733
  break
729
734
  default:
735
+ if (meta.access && !meta.access(target, prop, 'get')) {
736
+ return undefined
737
+ }
730
738
  if (Array.isArray(target[prop])) {
731
739
  return new Proxy(target[prop], handlers.newArrayHandler)
732
740
  }
@@ -735,6 +743,9 @@ export default function parse(input, meta, immutable=true)
735
743
  }
736
744
  },
737
745
  set(target, prop, value) {
746
+ if (meta.access && !meta.access(target, prop, 'set')) {
747
+ return undefined
748
+ }
738
749
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
739
750
  value = getNewValueProxy(value)
740
751
  }
@@ -764,6 +775,9 @@ export default function parse(input, meta, immutable=true)
764
775
  } else if (prop===isChanged) {
765
776
  return target[parent][isChanged]
766
777
  } else {
778
+ if (meta.access && !meta.access(target, prop, 'get')) {
779
+ return undefined
780
+ }
767
781
  if (Array.isArray(target[prop])) {
768
782
  target[prop][parent] = target[parent]
769
783
  return new Proxy(target[prop], handlers.arrayHandler)
@@ -775,6 +789,9 @@ export default function parse(input, meta, immutable=true)
775
789
  if (immutable) {
776
790
  throw new Error('dataspace is immutable')
777
791
  }
792
+ if (meta.access && !meta.access(target, prop, 'set')) {
793
+ return undefined
794
+ }
778
795
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
779
796
  value = getNewValueProxy(value)
780
797
  }
@@ -786,6 +803,9 @@ export default function parse(input, meta, immutable=true)
786
803
  if (immutable) {
787
804
  throw new Error('dataspace is immutable')
788
805
  }
806
+ if (meta.access && !meta.access(target, prop, 'deleteProperty')) {
807
+ return undefined
808
+ }
789
809
  //FIXME: if target[prop] was the last reference to an object
790
810
  //that object should be deleted so that its line will become empty
791
811
  //when stringifying resultArray again
@@ -799,6 +819,9 @@ export default function parse(input, meta, immutable=true)
799
819
  firstParse(target)
800
820
  switch(prop) {
801
821
  case source:
822
+ if (meta.access && !meta.access(target, prop, 'get')) {
823
+ return undefined
824
+ }
802
825
  return target
803
826
  break
804
827
  case isProxy:
@@ -823,6 +846,9 @@ export default function parse(input, meta, immutable=true)
823
846
  return target[isChanged]
824
847
  break
825
848
  default:
849
+ if (meta.access && !meta.access(target, prop, 'get')) {
850
+ return undefined
851
+ }
826
852
  if (Array.isArray(target[prop])) {
827
853
  target[prop][parent] = target
828
854
  return new Proxy(target[prop], handlers.arrayHandler)
@@ -837,6 +863,9 @@ export default function parse(input, meta, immutable=true)
837
863
  }
838
864
  firstParse(target)
839
865
  if (prop!==isChanged) {
866
+ if (prop!=resultSet && meta.access && !meta.access(target, prop, 'set')) {
867
+ return undefined
868
+ }
840
869
  if (JSONTag.getType(value)==='object' && !value[isProxy]) {
841
870
  value = getNewValueProxy(value)
842
871
  }
@@ -849,6 +878,9 @@ export default function parse(input, meta, immutable=true)
849
878
  if (immutable) {
850
879
  throw new Error('dataspace is immutable')
851
880
  }
881
+ if (meta.access && !meta.access(target, prop, 'deleteProperty')) {
882
+ return undefined
883
+ }
852
884
  firstParse(target)
853
885
  delete target[prop]
854
886
  target[isChanged] = true
@@ -866,10 +898,16 @@ export default function parse(input, meta, immutable=true)
866
898
  if (immutable) {
867
899
  throw new Error('dataspace is immutable')
868
900
  }
901
+ if (meta.access && !meta.access(target, prop, 'defineProperty')) {
902
+ return undefined
903
+ }
869
904
  firstParse(target)
870
905
  Object.defineProperty(target, prop, descriptor)
871
906
  },
872
907
  has(target, prop) {
908
+ if (meta.access && !meta.access(target, prop, 'has')) {
909
+ return false
910
+ }
873
911
  firstParse()
874
912
  return prop in target
875
913
  },
@@ -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
-