libyay 1.1.1 → 2.0.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.
Files changed (3) hide show
  1. package/README.md +163 -13
  2. package/package.json +6 -3
  3. package/yay.js +3093 -1403
package/README.md CHANGED
@@ -25,36 +25,172 @@ const result = parseYay('key: "value"');
25
25
  // => { key: "value" }
26
26
 
27
27
  // Parse with filename for error messages
28
- const data = parseYay(source, 'config.yay');
28
+ const data = parseYay(source, { filename: 'config.yay' });
29
29
  ```
30
30
 
31
31
  ## API
32
32
 
33
- ### `parseYay(source, filename?)`
33
+ ### `parseYay(source, options?)`
34
34
 
35
35
  Parses a YAY document string and returns the corresponding JavaScript value.
36
36
 
37
37
  **Parameters:**
38
38
 
39
39
  - `source` - A string containing the YAY document (UTF-8)
40
- - `filename` - Optional filename for error messages
40
+ - `options.filename` - Optional filename for error messages
41
+ - `options.decodeReference(uri)` - Turn a reference's URI text into the
42
+ caller's value. The URI is checked against YAY's reference rule before the
43
+ hook sees it. A document with a reference and no hook is refused, naming
44
+ the reference and its location; a document without references needs no
45
+ hook. There is no default reference value.
46
+ - `options.decodeSymbol(name)` - Turn a symbol's name into the caller's
47
+ value. The default is `symbol`, the symbol record below. A caller that
48
+ needs JavaScript registry symbols may use `name => Symbol.for(name)`.
49
+ - `options.bytes` - How to make a byte value. The default is a plain
50
+ `Uint8Array`. `"immutable"` makes the immutable byteArray that the locked
51
+ `@endo/pass-style` accepts, a hardened immutable `ArrayBuffer` (that
52
+ version rejects a `Uint8Array` view over one), and throws where the
53
+ environment has neither `ArrayBuffer.prototype.transferToImmutable` nor
54
+ `sliceToImmutable`, native or shimmed by `@endo/immutable-arraybuffer`
55
+ (`@endo/init` loads it). A function is the caller's own hook from the plain
56
+ `Uint8Array` to any value.
41
57
 
42
58
  **Returns:** A JavaScript value representing the parsed document.
43
59
 
44
60
  **Throws:** `Error` with line:column location if parsing fails.
45
61
 
62
+ ### `tagged(tag, payload)`, `isTagged(value)`
63
+
64
+ A tagged value is a tag, any string, and one payload, any YAY value. The
65
+ parser makes them with `tagged`, which returns a frozen record with the exact
66
+ shape of Endo's `makeTagged`: `Symbol.for("passStyle")` is `"tagged"`,
67
+ `Symbol.toStringTag` is the tag, and `payload` is its only enumerable
68
+ property. The record is shallowly frozen, ready for a hardened environment to
69
+ harden the payload and accept the result as pass data. `isTagged` recognizes
70
+ this shape, including a value made by Endo's `makeTagged`.
71
+
72
+ `tagged` refuses a tag that is not well-formed Unicode, and a payload that
73
+ carries one anywhere: a string, a record key, or the tag or name a nested
74
+ tagged value or symbol record holds in its non-enumerable
75
+ `Symbol.toStringTag` ("Illegal surrogate"; see `isWellFormedString`).
76
+
77
+ ```js
78
+ import { parseYay, tagged } from 'libyay';
79
+
80
+ parseYay('a: 10 (s)');
81
+ // => { a: tagged("s", 10n) }
82
+ parseYay('(rgb)\n- 255\n- 136\n- 0');
83
+ // => tagged("rgb", [255n, 136n, 0n])
84
+ ```
85
+
86
+ ### `symbol(name)`, `isSymbol(value)`
87
+
88
+ A symbol is a name distinguished from a string. `symbol` returns a frozen
89
+ record on `Object.prototype` with exactly two non-enumerable properties:
90
+ `Symbol.for("passStyle")` is `"symbol"`, and `Symbol.toStringTag` is the
91
+ name. `isSymbol` recognizes exactly that shape. Symbols with equal names
92
+ compare equal under Node's deep equality, while remaining distinct from
93
+ strings and tagged values. A name with a lone surrogate is refused
94
+ ("Illegal surrogate").
95
+
96
+ ```js
97
+ import { parseYay, symbol } from 'libyay';
98
+
99
+ parseYay('(method-name)');
100
+ // => symbol("method-name")
101
+ parseYay('(@@iterator)');
102
+ // => symbol("@@iterator"), with no well-known-symbol rule
103
+ ```
104
+
105
+ ### `reference(uri)`, `isReference(value)`, `Reference`
106
+
107
+ A reference is a URI or IRI in angle brackets, `<https://example.com/>`,
108
+ held as written and never dereferenced. `reference` is a ready-made decode
109
+ hook a caller may choose: it returns a frozen `Reference` instance with a
110
+ `reference` property, the name `yay -t js` writes. Its matching encode hook
111
+ is `(value) => value.reference`. The parser does not use it unless the caller
112
+ supplies it. Two references are equal (`assert.deepStrictEqual`) when their
113
+ texts are, and a reference never equals a string or a plain `{reference}`
114
+ object. A tagged reference, `<ocapn://a.tcp> (Counter)`, is a tagged value
115
+ over whatever the decode hook made of its URI.
116
+
117
+ ```js
118
+ import { parseYay, reference } from 'libyay';
119
+
120
+ parseYay('icon: <https://example.com/cat.png>', {
121
+ decodeReference: reference,
122
+ });
123
+ // => { icon: reference("https://example.com/cat.png") }
124
+ ```
125
+
126
+ ### `ysonToCapData(yson, options?)`, `capDataToYson(capData, options?)`
127
+
128
+ YSON writes a reference as smallcaps writes a remotable, a string beginning
129
+ with `$`, with the URI in place of the slot: `"$https://example.com/"`.
130
+ `ysonToCapData` takes YSON text and returns CapData, `{body, slots}`: each
131
+ distinct URI is checked against YAY's reference rule and passed to
132
+ `options.decodeReference`, whose value goes to `slots` in order of first
133
+ appearance; the body names it `$0`, `$1`, ... as `@endo/marshal`'s smallcaps
134
+ does. A YSON document with a reference and no decode hook is refused,
135
+ naming the reference and its JSON path.
136
+
137
+ `capDataToYson` puts each slot back, through `options.encodeReference` when
138
+ given, or as URI text when the slot is already a string. It validates the
139
+ URI after the hook returns, drops any alleged interface
140
+ (`$0.Alleged: Counter`), and refuses a promise (`&0`), which YAY does not
141
+ write.
142
+
143
+ Both directions refuse a string, key, tag or symbol name that is not
144
+ well-formed Unicode, at any depth, with the readers' message ("Illegal
145
+ surrogate"), and both keep negative zero, which they write as `-0`.
146
+
147
+ ```js
148
+ import { makeMarshal } from '@endo/marshal';
149
+ import { ysonToCapData } from 'libyay';
150
+
151
+ const { fromCapData } = makeMarshal(undefined, (uri, iface) =>
152
+ lookup(uri), { serializeBodyFormat: 'smallcaps' });
153
+ fromCapData(
154
+ ysonToCapData('["$ocapn://a.tcp", "$ocapn://a.tcp"]', {
155
+ decodeReference: (uri) => uri,
156
+ }),
157
+ );
158
+ // one slot, "ocapn://a.tcp"; both items are the remotable lookup returned
159
+ ```
160
+
161
+ ### `isWellFormedString(value)`
162
+
163
+ Whether `value` is a string of Unicode scalar values, with no unpaired
164
+ UTF-16 surrogate: the check YAY makes at every string boundary (the parser,
165
+ `symbol`, `tagged`, `reference` and the CapData bridge). It has the shape
166
+ of Endo's `isWellFormedString` in `@endo/pass-style`: `false` for any
167
+ non-string, even one that converts to a well-formed string, then
168
+ `String.prototype.isWellFormed`, or without it a scan by code point.
169
+
170
+ ## Equality, and the comparable subset
171
+
172
+ Negative zero is an expressible value, distinct from zero, and libyay
173
+ keeps it: the parser reads `-0.0` as `-0`, and the CapData bridge carries
174
+ it. It is outside YAY's comparable subset, where equality and ordering
175
+ are defined for keys and sorting (GRAMMAR.md); libyay has no API that
176
+ compares or sorts values, so it has nothing to refuse.
177
+
46
178
  ## Type Mapping
47
179
 
48
180
  | YAY Type | JavaScript Type |
49
181
  |----------|-----------------|
50
182
  | `null` | `null` |
183
+ | `undefined` | `undefined`; an object member holding it is an own property |
51
184
  | big integer | `bigint` |
52
185
  | float64 | `number` |
53
186
  | boolean | `boolean` |
54
187
  | string | `string` |
55
188
  | array | `Array` |
56
189
  | object | `Object` |
57
- | bytes | `Uint8Array` |
190
+ | bytes | `Uint8Array`, or `ArrayBuffer` with `bytes: 'immutable'` |
191
+ | tagged | Endo-shaped record from `tagged(tag, payload)` |
192
+ | reference | the decode hook's value; `Reference` from `reference(uri)` |
193
+ | symbol | frozen symbol record from `symbol(name)`, or the decode hook's value |
58
194
 
59
195
  # YAY Format
60
196
 
@@ -265,12 +401,12 @@ Content on the same line as the backtick starts the string without a leading new
265
401
  [string-block-root-same-line.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-block-root-same-line.yay)
266
402
  ```yay
267
403
  ` I think you ought to know I'm feeling very depressed.
268
- This will all end in tears.
404
+ Life? Don't talk to me about life.
269
405
  ```
270
406
 
271
407
  [string-block-root-same-line.js](https://github.com/kriskowal/yay/blob/main/test/js/string-block-root-same-line.js)
272
408
  ```js
273
- "I think you ought to know I'm feeling very depressed.\nThis will all end in tears.\n"
409
+ "I think you ought to know I'm feeling very depressed.\nLife? Don't talk to me about life.\n"
274
410
  ```
275
411
 
276
412
  Backtick alone on its line produces a leading newline because content starts on the following line.
@@ -403,7 +539,7 @@ next: 1
403
539
  })
404
540
  ```
405
541
 
406
- In canonical YAY, a block string is followed by one blank line, unless it is
542
+ In the formatter's form, a block string is followed by one blank line, unless it is
407
543
  the last thing in the document.
408
544
  The blank line is not part of the value, since trailing empty lines never are,
409
545
  so a document reads the same with or without it.
@@ -438,12 +574,12 @@ A double-quoted string.
438
574
 
439
575
  [string-inline-doublequote-basic.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-inline-doublequote-basic.yay)
440
576
  ```yay
441
- "This will all end in tears."
577
+ "Life? Don't talk to me about life."
442
578
  ```
443
579
 
444
580
  [string-inline-doublequote-basic.js](https://github.com/kriskowal/yay/blob/main/test/js/string-inline-doublequote-basic.js)
445
581
  ```js
446
- "This will all end in tears."
582
+ "Life? Don't talk to me about life."
447
583
  ```
448
584
 
449
585
  A single-quoted string (literal, no escapes).
@@ -754,8 +890,8 @@ Hex and comments on the same lines for inline documentation of byte sequences.
754
890
 
755
891
  [bytearray-block-hex-and-comment.yay](https://github.com/kriskowal/yay/blob/main/test/yay/bytearray-block-hex-and-comment.yay)
756
892
  ```yay
757
- > b0b5 # first chunk
758
- c0ff # second chunk
893
+ > b0b5 # first chunk
894
+ c0ff # second chunk
759
895
  ```
760
896
 
761
897
  [bytearray-block-hex-and-comment.js](https://github.com/kriskowal/yay/blob/main/test/js/bytearray-block-hex-and-comment.js)
@@ -868,13 +1004,27 @@ YAY has strict whitespace rules that the parser enforces:
868
1004
  ## Running Tests
869
1005
 
870
1006
  ```bash
871
- cd js
872
- node --test yay.test.js
1007
+ cd js/libyay
1008
+ npm test
873
1009
  ```
874
1010
 
875
1011
  The test runner uses fixture files from `../test/`.
876
1012
  Files with `.yay` extension contain YAY input.
877
1013
  Files with `.js` extension contain expected JavaScript output.
1014
+ The tagged-value cases are in the shared corpus, named `tagged-*`, and
1015
+ `tagged.test.js` checks that YAY refuses the ones in `../../test/meh/`.
1016
+ `smallcaps.test.js` runs YAY documents through a built binyay
1017
+ (`yay -f yay -t yson`; `YAY_BIN`, or `rust/target/release` or `debug`) and
1018
+ decodes the YSON with Endo's smallcaps encoding (`@endo/marshal`), then
1019
+ encodes it back. The Endo packages are required dev dependencies, so install
1020
+ them with `npm install`; the test skips, and says why, only when binyay is
1021
+ missing. `yay.test.js` reads the shared corpus in `../../test/`, `test/meh`
1022
+ among it: YAY reads each MEH document as MEH does, or refuses it.
1023
+ `references.test.js` checks the references model and the CapData bridge;
1024
+ `capdata.test.js` takes the bridge through `@endo/marshal`; and
1025
+ `codec.test.js`, under `@endo/init`, checks that a configured codec's decoded
1026
+ references and immutable bytes make every value kind pass data, and that the
1027
+ encode hook gives the same text back.
878
1028
 
879
1029
  ## References
880
1030
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libyay",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "YAY (Yet Another YAML) is a human readable, writable, and diffable data document format",
5
5
  "type": "module",
6
6
  "main": "yay.js",
@@ -12,12 +12,15 @@
12
12
  "README.md"
13
13
  ],
14
14
  "scripts": {
15
- "test": "node --test yay.test.js",
16
- "coverage": "c8 --all --include yay.js --reporter=text --reporter=html node --test yay.test.js"
15
+ "test": "node --test yay.test.js tagged.test.js symbol.test.js surrogate.test.js parity.test.js smallcaps.test.js references.test.js capdata.test.js codec.test.js",
16
+ "bench": "node bench.mjs",
17
+ "coverage": "c8 --all --include yay.js --reporter=text --reporter=html node --test yay.test.js tagged.test.js symbol.test.js surrogate.test.js parity.test.js smallcaps.test.js references.test.js capdata.test.js codec.test.js"
17
18
  },
18
19
  "author": "Kris Kowal",
19
20
  "license": "Apache-2.0",
20
21
  "devDependencies": {
22
+ "@endo/init": "^1.1.13",
23
+ "@endo/marshal": "^1.10.0",
21
24
  "c8": "^10.1.3"
22
25
  }
23
26
  }