-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use devalue for island props serialization
- Loading branch information
Showing
14 changed files
with
22 additions
and
327 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
packages/astro/e2e/fixtures/error-cyclic/src/components/PreactCounter.tsx
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
packages/astro/e2e/fixtures/error-cyclic/src/pages/index.astro
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ set.add('test2'); | |
array={[0, "foo"]} | ||
map={map} | ||
set={set} | ||
infinity={Infinity} | ||
/> | ||
</main> | ||
</body> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,10 @@ | ||
import type { ValueOf } from '../../type-utils.js'; | ||
import type { AstroComponentMetadata } from '../../types/public/internal.js'; | ||
import { uneval } from 'devalue'; | ||
|
||
const PROP_TYPE = { | ||
Value: 0, | ||
JSON: 1, // Actually means Array | ||
RegExp: 2, | ||
Date: 3, | ||
Map: 4, | ||
Set: 5, | ||
BigInt: 6, | ||
URL: 7, | ||
Uint8Array: 8, | ||
Uint16Array: 9, | ||
Uint32Array: 10, | ||
}; | ||
|
||
function serializeArray( | ||
value: any[], | ||
metadata: AstroComponentMetadata | Record<string, any> = {}, | ||
parents = new WeakSet<any>(), | ||
): any[] { | ||
if (parents.has(value)) { | ||
throw new Error(`Cyclic reference detected while serializing props for <${metadata.displayName} client:${metadata.hydrate}>! | ||
Cyclic references cannot be safely serialized for client-side usage. Please remove the cyclic reference.`); | ||
} | ||
parents.add(value); | ||
const serialized = value.map((v) => { | ||
return convertToSerializedForm(v, metadata, parents); | ||
}); | ||
parents.delete(value); | ||
return serialized; | ||
} | ||
|
||
function serializeObject( | ||
value: Record<any, any>, | ||
metadata: AstroComponentMetadata | Record<string, any> = {}, | ||
parents = new WeakSet<any>(), | ||
): Record<any, any> { | ||
if (parents.has(value)) { | ||
throw new Error(`Cyclic reference detected while serializing props for <${metadata.displayName} client:${metadata.hydrate}>! | ||
Cyclic references cannot be safely serialized for client-side usage. Please remove the cyclic reference.`); | ||
export function serializeProps(props: Record<string, unknown>) { | ||
// Remove symbolic keys as devalue can't handle them and they don't really make sense to be | ||
// serialized as symbolic keys aren't really equal between the client and server realms | ||
if (Object.getOwnPropertySymbols(props).length) { | ||
props = Object.fromEntries(Object.entries(props).filter(([key]) => typeof key !== 'symbol')); | ||
} | ||
parents.add(value); | ||
const serialized = Object.fromEntries( | ||
Object.entries(value).map(([k, v]) => { | ||
return [k, convertToSerializedForm(v, metadata, parents)]; | ||
}), | ||
); | ||
parents.delete(value); | ||
return serialized; | ||
} | ||
|
||
function convertToSerializedForm( | ||
value: any, | ||
metadata: AstroComponentMetadata | Record<string, any> = {}, | ||
parents = new WeakSet<any>(), | ||
): [ValueOf<typeof PROP_TYPE>, any] | [ValueOf<typeof PROP_TYPE>] { | ||
const tag = Object.prototype.toString.call(value); | ||
switch (tag) { | ||
case '[object Date]': { | ||
return [PROP_TYPE.Date, (value as Date).toISOString()]; | ||
} | ||
case '[object RegExp]': { | ||
return [PROP_TYPE.RegExp, (value as RegExp).source]; | ||
} | ||
case '[object Map]': { | ||
return [PROP_TYPE.Map, serializeArray(Array.from(value as Map<any, any>), metadata, parents)]; | ||
} | ||
case '[object Set]': { | ||
return [PROP_TYPE.Set, serializeArray(Array.from(value as Set<any>), metadata, parents)]; | ||
} | ||
case '[object BigInt]': { | ||
return [PROP_TYPE.BigInt, (value as bigint).toString()]; | ||
} | ||
case '[object URL]': { | ||
return [PROP_TYPE.URL, (value as URL).toString()]; | ||
} | ||
case '[object Array]': { | ||
return [PROP_TYPE.JSON, serializeArray(value, metadata, parents)]; | ||
} | ||
case '[object Uint8Array]': { | ||
return [PROP_TYPE.Uint8Array, Array.from(value as Uint8Array)]; | ||
} | ||
case '[object Uint16Array]': { | ||
return [PROP_TYPE.Uint16Array, Array.from(value as Uint16Array)]; | ||
} | ||
case '[object Uint32Array]': { | ||
return [PROP_TYPE.Uint32Array, Array.from(value as Uint32Array)]; | ||
} | ||
default: { | ||
if (value !== null && typeof value === 'object') { | ||
return [PROP_TYPE.Value, serializeObject(value, metadata, parents)]; | ||
} else if (value === undefined) { | ||
return [PROP_TYPE.Value]; | ||
} else { | ||
return [PROP_TYPE.Value, value]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function serializeProps(props: any, metadata: AstroComponentMetadata) { | ||
const serialized = JSON.stringify(serializeObject(props, metadata)); | ||
return serialized; | ||
return uneval(props); | ||
} |
Oops, something went wrong.