libyay 1.0.0 → 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 +190 -13
  2. package/package.json +6 -3
  3. package/yay.js +3110 -1398
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # libyay
2
2
 
3
- A parser for the [YAY](https://github.com/kriskowal/yay) data format, implemented in JavaScript (ES2024+).
3
+ [![npm](https://img.shields.io/npm/v/libyay.svg)](https://www.npmjs.com/package/libyay)
4
+
5
+ A parser for the [YAY](https://kriskowal.com/yay) data format, implemented in JavaScript (ES2024+).
4
6
 
5
7
  ## Requirements
6
8
 
@@ -23,36 +25,172 @@ const result = parseYay('key: "value"');
23
25
  // => { key: "value" }
24
26
 
25
27
  // Parse with filename for error messages
26
- const data = parseYay(source, 'config.yay');
28
+ const data = parseYay(source, { filename: 'config.yay' });
27
29
  ```
28
30
 
29
31
  ## API
30
32
 
31
- ### `parseYay(source, filename?)`
33
+ ### `parseYay(source, options?)`
32
34
 
33
35
  Parses a YAY document string and returns the corresponding JavaScript value.
34
36
 
35
37
  **Parameters:**
36
38
 
37
39
  - `source` - A string containing the YAY document (UTF-8)
38
- - `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.
39
57
 
40
58
  **Returns:** A JavaScript value representing the parsed document.
41
59
 
42
60
  **Throws:** `Error` with line:column location if parsing fails.
43
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
+
44
178
  ## Type Mapping
45
179
 
46
180
  | YAY Type | JavaScript Type |
47
181
  |----------|-----------------|
48
182
  | `null` | `null` |
183
+ | `undefined` | `undefined`; an object member holding it is an own property |
49
184
  | big integer | `bigint` |
50
185
  | float64 | `number` |
51
186
  | boolean | `boolean` |
52
187
  | string | `string` |
53
188
  | array | `Array` |
54
189
  | object | `Object` |
55
- | 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 |
56
194
 
57
195
  # YAY Format
58
196
 
@@ -263,12 +401,12 @@ Content on the same line as the backtick starts the string without a leading new
263
401
  [string-block-root-same-line.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-block-root-same-line.yay)
264
402
  ```yay
265
403
  ` I think you ought to know I'm feeling very depressed.
266
- This will all end in tears.
404
+ Life? Don't talk to me about life.
267
405
  ```
268
406
 
269
407
  [string-block-root-same-line.js](https://github.com/kriskowal/yay/blob/main/test/js/string-block-root-same-line.js)
270
408
  ```js
271
- "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"
272
410
  ```
273
411
 
274
412
  Backtick alone on its line produces a leading newline because content starts on the following line.
@@ -401,6 +539,31 @@ next: 1
401
539
  })
402
540
  ```
403
541
 
542
+ In the formatter's form, a block string is followed by one blank line, unless it is
543
+ the last thing in the document.
544
+ The blank line is not part of the value, since trailing empty lines never are,
545
+ so a document reads the same with or without it.
546
+ A string whose last line is empty, like `"a\n\n"`, cannot be a block string
547
+ and is written with double quotes.
548
+
549
+ [string-block-separated-property.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-block-separated-property.yay)
550
+ ```yay
551
+ message: `
552
+ We shall fight on the beaches.
553
+
554
+ We shall never surrender.
555
+
556
+ next: 1
557
+ ```
558
+
559
+ [string-block-separated-property.js](https://github.com/kriskowal/yay/blob/main/test/js/string-block-separated-property.js)
560
+ ```js
561
+ ({
562
+ "message": "We shall fight on the beaches.\n\nWe shall never surrender.\n",
563
+ "next": 1n,
564
+ })
565
+ ```
566
+
404
567
  ## Inline Strings
405
568
 
406
569
  Strings may be quoted with double or single quotes.
@@ -411,12 +574,12 @@ A double-quoted string.
411
574
 
412
575
  [string-inline-doublequote-basic.yay](https://github.com/kriskowal/yay/blob/main/test/yay/string-inline-doublequote-basic.yay)
413
576
  ```yay
414
- "This will all end in tears."
577
+ "Life? Don't talk to me about life."
415
578
  ```
416
579
 
417
580
  [string-inline-doublequote-basic.js](https://github.com/kriskowal/yay/blob/main/test/js/string-inline-doublequote-basic.js)
418
581
  ```js
419
- "This will all end in tears."
582
+ "Life? Don't talk to me about life."
420
583
  ```
421
584
 
422
585
  A single-quoted string (literal, no escapes).
@@ -727,8 +890,8 @@ Hex and comments on the same lines for inline documentation of byte sequences.
727
890
 
728
891
  [bytearray-block-hex-and-comment.yay](https://github.com/kriskowal/yay/blob/main/test/yay/bytearray-block-hex-and-comment.yay)
729
892
  ```yay
730
- > b0b5 # first chunk
731
- c0ff # second chunk
893
+ > b0b5 # first chunk
894
+ c0ff # second chunk
732
895
  ```
733
896
 
734
897
  [bytearray-block-hex-and-comment.js](https://github.com/kriskowal/yay/blob/main/test/js/bytearray-block-hex-and-comment.js)
@@ -841,13 +1004,27 @@ YAY has strict whitespace rules that the parser enforces:
841
1004
  ## Running Tests
842
1005
 
843
1006
  ```bash
844
- cd js
845
- node --test yay.test.js
1007
+ cd js/libyay
1008
+ npm test
846
1009
  ```
847
1010
 
848
1011
  The test runner uses fixture files from `../test/`.
849
1012
  Files with `.yay` extension contain YAY input.
850
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.
851
1028
 
852
1029
  ## References
853
1030
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libyay",
3
- "version": "1.0.0",
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
  }