Comparing version 0.2.1 to 0.3.0
@@ -1,3 +0,3 @@ | ||
import { AsyncServerValue, PrimitiveValue, ServerValue } from './types'; | ||
export { AsyncServerValue, ServerValue, PrimitiveValue }; | ||
import { AsyncServerValue, PrimitiveValue, ServerValue, CommonServerValue, SemiPrimitiveValue, ErrorValue } from './types'; | ||
export { AsyncServerValue, ServerValue, PrimitiveValue, CommonServerValue, SemiPrimitiveValue, ErrorValue, }; | ||
export declare function serialize(source: ServerValue): string; | ||
@@ -4,0 +4,0 @@ export declare function serializeAsync(source: AsyncServerValue): Promise<string>; |
@@ -1,3 +0,7 @@ | ||
export type PrimitiveValue = boolean | string | number | undefined | null | bigint | Date | RegExp; | ||
export type ServerValue = PrimitiveValue | Array<ServerValue> | readonly ServerValue[] | { | ||
export type PrimitiveValue = boolean | string | number | undefined | null | bigint; | ||
export type ErrorValue = Error | AggregateError | EvalError | RangeError | ReferenceError | TypeError | SyntaxError | URIError; | ||
export type TypedArrayValue = Int8Array | Int16Array | Int32Array | Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Float32Array | Float64Array | BigInt64Array | BigUint64Array; | ||
export type SemiPrimitiveValue = RegExp | Date | TypedArrayValue; | ||
export type CommonServerValue = PrimitiveValue | SemiPrimitiveValue | ErrorValue; | ||
export type ServerValue = CommonServerValue | Array<ServerValue> | readonly ServerValue[] | Iterable<ServerValue> | { | ||
[key: string | number]: ServerValue; | ||
@@ -7,3 +11,3 @@ } | { | ||
} | Set<ServerValue> | Map<ServerValue, ServerValue>; | ||
export type AsyncServerValue = PrimitiveValue | Array<AsyncServerValue> | readonly AsyncServerValue[] | { | ||
export type AsyncServerValue = CommonServerValue | Array<AsyncServerValue> | readonly AsyncServerValue[] | Iterable<AsyncServerValue> | { | ||
[key: string | number]: AsyncServerValue; | ||
@@ -10,0 +14,0 @@ } | { |
{ | ||
"name": "seroval", | ||
"type": "module", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"files": [ | ||
@@ -67,3 +67,3 @@ "dist", | ||
}, | ||
"gitHead": "7888af9f6e1d875159dc676d59b59ba4b2e03d7e" | ||
"gitHead": "3be59fefb6ffb73b1bad693d956d1fd9d8d6d720" | ||
} |
@@ -61,7 +61,7 @@ # seroval | ||
```js | ||
((h,j,k,m)=>(h={number:[0.5906513825027921,-0,NaN,Infinity,-Infinity],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:BigInt("9007199254740991"),array:j=[,,,,k=new Map([["hello","world"],["mutual",m=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-02-06T06:16:00.384Z"),map:k,set:m},j[3]=j,h.self=h,k.set("self",k),m.add(m).add(j),h))() | ||
((h,j,k,m)=>(m={number:[0.4178420745429774,-0,NaN,1/0,-1/0],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:9007199254740991n,array:h=[,,,,j=new Map([["hello","world"],["mutual",k=new Set(["hello","world"])]])],regexp:/[a-z0-9]+/i,date:new Date("2023-03-13T00:52:11.335Z"),map:j,set:k},h[3]=h,j.set("self",j),k.add(k).add(h),m.self=m,m))() | ||
// Formatted for readability | ||
((h, j, k, m) => (h = { | ||
number: [0.5906513825027921, -0, NaN, Infinity, -Infinity], | ||
((h, j, k, m) => (m = { | ||
number: [0.4178420745429774, -0, NaN, 1/0, -1/0], | ||
string: ["hello world", "\x3Cscript>Hello World\x3C/script>"], | ||
@@ -71,12 +71,12 @@ boolean: [!0, !1], | ||
undefined: void 0, | ||
bigint: BigInt("9007199254740991"), | ||
array: j = [, , , , k = new Map([ | ||
bigint: 9007199254740991n, | ||
array: h = [, , , , j = new Map([ | ||
["hello", "world"], | ||
["mutual", m = new Set(["hello", "world"])] | ||
["mutual", k = new Set(["hello", "world"])] | ||
])], | ||
regexp: /[a-z0-9]+/i, | ||
date: new Date("2023-02-06T06:16:00.384Z"), | ||
map: k, | ||
set: m | ||
}, j[3] = j, h.self = h, k.set("self", k), m.add(m).add(j), h))() | ||
date: new Date("2023-03-13T00:52:11.335Z"), | ||
map: j, | ||
set: k | ||
}, h[3] = h, j.set("self", j), k.add(k).add(h), m.self = m, m))() | ||
``` | ||
@@ -107,3 +107,3 @@ | ||
```js | ||
((h,j,k,m,o,q)=>(q={a:h=new Map([["name","a"],["children",[j=new Map([["name","c"],["right",k=new Map([["name","b"],["children",o=[,m=new Map([["name","d"]])]]])]]),m]]]),b:k,c:j,d:m},o[0]=j,j.set("left",h),m.set("left",h).set("right",k),q))() | ||
((h,j,k,m,o,q)=>(q={a:h=new Map([["name","a"],["children",[j=new Map([["name","c"],["right",o=new Map([["name","b"],["children",k=[,m=new Map([["name","d"]])]]])]]),m]]]),b:o,c:j,d:m},j.set("left",h),k[0]=j,m.set("left",h).set("right",o),q))() | ||
@@ -116,5 +116,5 @@ // Formatted | ||
["name", "c"], | ||
["right", k = new Map([ | ||
["right", o = new Map([ | ||
["name", "b"], | ||
["children", o = [, m = new Map([ | ||
["children", k = [, m = new Map([ | ||
["name", "d"] | ||
@@ -125,6 +125,6 @@ ])]] | ||
]), | ||
b: k, | ||
b: o, | ||
c: j, | ||
d: m | ||
}, o[0] = j, j.set("left", h), m.set("left", h).set("right", k), q))() | ||
}, j.set("left", h), k[0] = j, m.set("left", h).set("right", o), q))() | ||
``` | ||
@@ -180,3 +180,24 @@ | ||
- `Set` | ||
- `TypedArray` | ||
- `Int8Array` | ||
- `Int16Array` | ||
- `Int32Array` | ||
- `Uint8Array` | ||
- `Uint16Array` | ||
- `Uint32Array` | ||
- `Uint8ClampedArray` | ||
- `Float32Array` | ||
- `Float64Array` | ||
- `BigInt64Array` | ||
- `BigUint64Array` | ||
- `Error` | ||
- `AggregateError` | ||
- `EvalError` | ||
- `RangeError` | ||
- `ReferenceError` | ||
- `SyntaxError` | ||
- `TypeError` | ||
- `URIError` | ||
- `Promise` (with `serializeAsync`) | ||
- [`Iterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) | ||
- Cyclic references (both self and mutual) | ||
@@ -183,0 +204,0 @@ |
/* eslint-disable no-await-in-loop */ | ||
import { join } from './array'; | ||
import { lookupRefsAsync, traverseAsync } from './async'; | ||
import { isPrimitive } from './checks'; | ||
import serializePrimitive from './serialize-primitive'; | ||
import { | ||
SerializationContext, | ||
createRef, | ||
createSerializationContext, | ||
generateTreeAsync, | ||
generateTreeSync, | ||
getRefParam, | ||
createRef, | ||
resolvePatches, | ||
createSerializationContext, | ||
} from './context'; | ||
import isPrimitive from './is-primitive'; | ||
import serializePrimitive from './serialize-primitive'; | ||
import { lookupRefs, traverseSync } from './sync'; | ||
SerializationContext, | ||
serializeTree, | ||
} from './tree'; | ||
import { | ||
@@ -19,5 +19,15 @@ AsyncServerValue, | ||
ServerValue, | ||
CommonServerValue, | ||
SemiPrimitiveValue, | ||
ErrorValue, | ||
} from './types'; | ||
export { AsyncServerValue, ServerValue, PrimitiveValue }; | ||
export { | ||
AsyncServerValue, | ||
ServerValue, | ||
PrimitiveValue, | ||
CommonServerValue, | ||
SemiPrimitiveValue, | ||
ErrorValue, | ||
}; | ||
@@ -35,3 +45,3 @@ function finalize<T extends ServerValue | AsyncServerValue>( | ||
const params = ctx.vars.length > 1 | ||
? `(${join(ctx.vars, ',')})` | ||
? `(${ctx.vars.join(',')})` | ||
: ctx.vars[0]; | ||
@@ -55,15 +65,4 @@ // Source is probably already assigned | ||
const ctx = createSerializationContext(); | ||
// Lookup possible shared references | ||
lookupRefs(ctx, source); | ||
// Add shared references to refs | ||
for (const [key, value] of ctx.refCount.entries()) { | ||
if (value > 1) { | ||
createRef(ctx, key); | ||
} | ||
} | ||
// Get top-level serialization | ||
const result = traverseSync(ctx, source); | ||
if (typeof result !== 'string') { | ||
throw new Error('Unreachable error'); | ||
} | ||
const tree = generateTreeSync(ctx, source); | ||
const result = serializeTree(ctx, tree); | ||
return finalize(ctx, source, result); | ||
@@ -77,15 +76,4 @@ } | ||
const ctx = createSerializationContext(); | ||
// Lookup possible shared references | ||
await lookupRefsAsync(ctx, source); | ||
// Add shared references to refs | ||
for (const [key, value] of ctx.refCount.entries()) { | ||
if (value > 1) { | ||
createRef(ctx, key); | ||
} | ||
} | ||
// Get top-level serialization | ||
const result = await traverseAsync(ctx, source); | ||
if (typeof result !== 'string') { | ||
throw new Error('Unreachable error'); | ||
} | ||
const tree = await generateTreeAsync(ctx, source); | ||
const result = serializeTree(ctx, tree); | ||
return finalize(ctx, source, result); | ||
@@ -92,0 +80,0 @@ } |
@@ -18,2 +18,8 @@ import quote from './quote'; | ||
} | ||
if (typeof value === 'bigint') { | ||
return `${value}n`; | ||
} | ||
if (typeof value === 'string') { | ||
return quote(value); | ||
} | ||
// negative 0 isn't the same as 0 | ||
@@ -23,12 +29,9 @@ if (Object.is(value, -0)) { | ||
} | ||
if (typeof value === 'bigint') { | ||
return `BigInt("${value}")`; | ||
if (Object.is(value, Infinity)) { | ||
return '1/0'; | ||
} | ||
if (typeof value === 'string') { | ||
return quote(value); | ||
if (Object.is(value, -Infinity)) { | ||
return '-1/0'; | ||
} | ||
if (value.constructor === Date) { | ||
return `new Date("${value.toISOString()}")`; | ||
} | ||
return String(value); | ||
} |
@@ -8,10 +8,42 @@ // Values that are non-recursive | ||
| null | ||
| bigint | ||
| bigint; | ||
export type ErrorValue = | ||
| Error | ||
| AggregateError | ||
| EvalError | ||
| RangeError | ||
| ReferenceError | ||
| TypeError | ||
| SyntaxError | ||
| URIError; | ||
export type TypedArrayValue = | ||
| Int8Array | ||
| Int16Array | ||
| Int32Array | ||
| Uint8Array | ||
| Uint16Array | ||
| Uint32Array | ||
| Uint8ClampedArray | ||
| Float32Array | ||
| Float64Array | ||
| BigInt64Array | ||
| BigUint64Array; | ||
export type SemiPrimitiveValue = | ||
| RegExp | ||
| Date | ||
| RegExp; | ||
| TypedArrayValue; | ||
export type CommonServerValue = | ||
| PrimitiveValue | ||
| SemiPrimitiveValue | ||
| ErrorValue; | ||
export type ServerValue = | ||
| PrimitiveValue | ||
| CommonServerValue | ||
| Array<ServerValue> | ||
| readonly ServerValue[] | ||
| Iterable<ServerValue> | ||
| { [key: string | number]: ServerValue } | ||
@@ -23,5 +55,6 @@ | { readonly [key: string | number]: ServerValue } | ||
export type AsyncServerValue = | ||
| PrimitiveValue | ||
| CommonServerValue | ||
| Array<AsyncServerValue> | ||
| readonly AsyncServerValue[] | ||
| Iterable<AsyncServerValue> | ||
| { [key: string | number]: AsyncServerValue } | ||
@@ -28,0 +61,0 @@ | { readonly [key: string | number]: AsyncServerValue } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
240251
3084
206
24