-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve cyclic reference detection, now ignores non-cyclic shared ref…
…erences (#4684) * fix: improve cyclic reference detection, now ignores references that are not parent/child * fix: only track cyclic parents Co-authored-by: Nate Moore <[email protected]>
- Loading branch information
1 parent
cc242d3
commit 919df13
Showing
3 changed files
with
98 additions
and
14 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Fixes regression introduced in [#4646](https://github.com/withastro/astro/pull/4646) with better cyclic reference detection |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { expect } from 'chai'; | ||
import { serializeProps } from '../dist/runtime/server/serialize.js'; | ||
|
||
describe('serialize', () => { | ||
it('serializes a plain value', () => { | ||
const input = { a: 1 }; | ||
const output = `{"a":[0,1]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes an array', () => { | ||
const input = { a: [0] }; | ||
const output = `{"a":[1,"[[0,0]]"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a regular expression', () => { | ||
const input = { a: /b/ }; | ||
const output = `{"a":[2,"b"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a Date', () => { | ||
const input = { a: new Date(0) }; | ||
const output = `{"a":[3,"1970-01-01T00:00:00.000Z"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a Map', () => { | ||
const input = { a: new Map([[0, 1]]) }; | ||
const output = `{"a":[4,"[[1,\\"[[0,0],[0,1]]\\"]]"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a Set', () => { | ||
const input = { a: new Set([0, 1, 2, 3]) }; | ||
const output = `{"a":[5,"[[0,0],[0,1],[0,2],[0,3]]"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a BigInt', () => { | ||
const input = { a: BigInt('1') }; | ||
const output = `{"a":[6,"1"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('serializes a URL', () => { | ||
const input = { a: new URL('https://example.com/') }; | ||
const output = `{"a":[7,"https://example.com/"]}`; | ||
expect(serializeProps(input)).to.equal(output); | ||
}) | ||
it('cannot serialize a cyclic reference', () => { | ||
const a = {} | ||
a.b = a; | ||
const input = { a }; | ||
expect(() => serializeProps(input)).to.throw(/cyclic/); | ||
}) | ||
it('cannot serialize a cyclic array', () => { | ||
const input = { foo: ['bar'] } | ||
input.foo.push(input) | ||
expect(() => serializeProps(input)).to.throw(/cyclic/); | ||
}) | ||
it('cannot serialize a deep cyclic reference', () => { | ||
const a = { b: {}}; | ||
a.b.c = a; | ||
const input = { a }; | ||
expect(() => serializeProps(input)).to.throw(/cyclic/); | ||
}) | ||
it('can serialize shared references that are not cyclic', () => { | ||
const b = {} | ||
const input = { a: { b, b }, b }; | ||
expect(() => serializeProps(input)).not.to.throw(); | ||
}) | ||
}) |