lib0 1.0.0-rc.23 → 1.0.0-rc.25
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/package.json +5 -5
- package/src/environment.common.js +2 -1
- package/src/schema.js +20 -2
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "lib0",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "1.0.0-rc.25",
|
|
4
|
+
"description": "isomorphic utility functions",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"funding": {
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
"url": "https://github.com/sponsors/dmonad"
|
|
10
10
|
},
|
|
11
11
|
"bin": {
|
|
12
|
-
"0gentesthtml": "
|
|
13
|
-
"0serve": "
|
|
14
|
-
"0ecdsa-generate-keypair": "
|
|
12
|
+
"0gentesthtml": "src/bin/gentesthtml.js",
|
|
13
|
+
"0serve": "src/bin/0serve.js",
|
|
14
|
+
"0ecdsa-generate-keypair": "src/bin/0ecdsa-generate-keypair.js"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
17
|
"dist/**/*.d.ts",
|
|
@@ -31,7 +31,7 @@ export const globalScope = /* @__PURE__ */(() =>/** @type {any} */ (typeof globa
|
|
|
31
31
|
* which has `btoa`/`atob`/`fetch` but no DOM). Excludes Node and Deno.
|
|
32
32
|
* @type {boolean}
|
|
33
33
|
*/
|
|
34
|
-
/* c8 ignore
|
|
34
|
+
/* c8 ignore start */
|
|
35
35
|
export const isBrowser = /* @__PURE__ */(() =>
|
|
36
36
|
!isNode && !isDeno && (
|
|
37
37
|
(typeof window !== 'undefined' && typeof document !== 'undefined') ||
|
|
@@ -39,3 +39,4 @@ export const isBrowser = /* @__PURE__ */(() =>
|
|
|
39
39
|
(typeof globalScope.WorkerGlobalScope !== 'undefined' && globalScope.self instanceof globalScope.WorkerGlobalScope)
|
|
40
40
|
)
|
|
41
41
|
)()
|
|
42
|
+
/* c8 ignore stop */
|
package/src/schema.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import * as arr from './array.js'
|
|
8
|
+
import * as buffer from './buffer.js'
|
|
8
9
|
import * as error from './error.js'
|
|
9
10
|
import * as equalityTraits from './trait/equality.js'
|
|
10
11
|
import * as fun from './function.js'
|
|
@@ -1259,6 +1260,8 @@ const _failed = Symbol('schema:coercion failed')
|
|
|
1259
1260
|
|
|
1260
1261
|
const _bigintRegex = /^[+-]?\d+$/
|
|
1261
1262
|
|
|
1263
|
+
const _base64Regex = /^[A-Za-z0-9+/]+={0,2}$/
|
|
1264
|
+
|
|
1262
1265
|
/**
|
|
1263
1266
|
* Render a value for an error message. Objects are only rendered by kind - printing them adds
|
|
1264
1267
|
* noise (and `String` throws on null-prototype objects).
|
|
@@ -1388,6 +1391,20 @@ const _createCoercer = ($s, cache) => {
|
|
|
1388
1391
|
? o
|
|
1389
1392
|
: (o === 'true' ? true : (o === 'false' ? false : _fail(ctx, path, o, expected)))
|
|
1390
1393
|
}
|
|
1394
|
+
if (_isMeta($$uint8Array, $s)) {
|
|
1395
|
+
// the format is checked here (rather than left to the decoder) because the `buffer.fromBase64`
|
|
1396
|
+
// backends disagree on invalid input: `Buffer` silently ignores unexpected characters, `atob` &
|
|
1397
|
+
// `Uint8Array.fromBase64` throw
|
|
1398
|
+
return (o, path, ctx) => {
|
|
1399
|
+
if ($uint8Array.check(o)) return o
|
|
1400
|
+
if (typeof o !== 'string' || !_base64Regex.test(o)) return _fail(ctx, path, o, expected)
|
|
1401
|
+
// base64 encodes 3 bytes as 4 characters. The '=' padding is optional, but when it is there
|
|
1402
|
+
// the length has to be a multiple of 4 - and without it the last chunk must not be a single
|
|
1403
|
+
// character, which carries no full byte.
|
|
1404
|
+
const rest = o.length % 4
|
|
1405
|
+
return (o.endsWith('=') ? rest === 0 : rest !== 1) ? buffer.fromBase64(o) : _fail(ctx, path, o, expected)
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1391
1408
|
if (_isMeta($$literal, $s) || _isMeta($$null, $s) || _isMeta($$undefined, $s)) {
|
|
1392
1409
|
const literals = /** @type {Array<Primitive>} */ (shape)
|
|
1393
1410
|
return (o, path, ctx) => {
|
|
@@ -1530,8 +1547,9 @@ const _buildCoercer = ($s, cache) => {
|
|
|
1530
1547
|
* Unlike `check` (which only validates) a coercion converts values that don't match yet - `'42'`
|
|
1531
1548
|
* becomes `42` for `$number`, `'true'` becomes `true` for `$boolean`. Values that already match
|
|
1532
1549
|
* are returned untouched (including object identity). Containers (`$object`, `$array`, `$record`,
|
|
1533
|
-
* `$tuple`, `$union`, `$optional`) are coerced recursively.
|
|
1534
|
-
*
|
|
1550
|
+
* `$tuple`, `$union`, `$optional`) are coerced recursively. `$uint8Array` accepts a base64 string
|
|
1551
|
+
* (padding optional) - the inverse of `buffer.toBase64`. Schemas without a meaningful conversion
|
|
1552
|
+
* (`$instanceOf`, `$custom`, ..) simply have to match.
|
|
1535
1553
|
*
|
|
1536
1554
|
* Failures are returned instead of thrown - this is the only api in lib0 that reports errors as a
|
|
1537
1555
|
* value.
|