-
Notifications
You must be signed in to change notification settings - Fork 821
Adds check to throw error if non-serializable state is used in a move #896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
702c990
Adds check to throw error if non-serialiable state is used
17b786f
Ignore line that cant be tested
ab5c28a
More lines ignored for coverage
d65fd56
More efficiently ignoring coverage
9ac99e0
Merge branch 'master' into nonSerialiazableError
vdfdev 4b366fd
Address review
c428a89
Removes unnecessary diff
0810fb2
Fix integration test
2ed62b0
Fix lint warnings
b5b9be5
Update src/plugins/plugin-serializable.ts
vdfdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
| import { Client } from '../client/client'; | ||
|
|
||
| describe('plugin-serializable', () => { | ||
| let client; | ||
|
|
||
| beforeAll(() => { | ||
| const game = { | ||
| moves: { | ||
| serializable: () => { | ||
| return { hello: 'world' }; | ||
| }, | ||
|
|
||
| nonSerializable: () => { | ||
| class Foo { | ||
| a: number; | ||
| constructor(a: number) { | ||
| this.a = a; | ||
| } | ||
| } | ||
| return { hello: new Foo(1) }; | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| client = Client({ game }); | ||
| }); | ||
|
|
||
| test('does not throw for serializable move', () => { | ||
| expect(() => { | ||
| client.moves.serializable(); | ||
| }).not.toThrow(); | ||
| }); | ||
|
|
||
| test('throws for non-serializable move', () => { | ||
| expect(() => { | ||
| client.moves.nonSerializable(); | ||
| }).toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or 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,55 @@ | ||
| import type { Plugin, AnyFn, Ctx } from '../types'; | ||
| import isPlainObject from 'lodash.isplainobject'; | ||
|
|
||
| /** | ||
| * Check if a value can be serialized (e.g. using `JSON.stringify`). | ||
| * Adapted from: https://stackoverflow.com/a/30712764/3829557 | ||
| */ | ||
| function isSerializable(value: any) { | ||
| // Primitives are OK. | ||
| if ( | ||
| value === undefined || | ||
| value === null || | ||
| typeof value === 'boolean' || | ||
| typeof value === 'number' || | ||
| typeof value === 'string' | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| // A non-primitive value that is neither a POJO or an array cannot be serialized. | ||
| if (!isPlainObject(value) && !Array.isArray(value)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Recurse entries if the value is an object or array. | ||
| for (const key in value) { | ||
| if (!isSerializable(value[key])) return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Plugin that checks whether state is serializable, in order to avoid | ||
| * network serialization bugs. | ||
| */ | ||
| const SerializablePlugin: Plugin = { | ||
| name: 'plugin-serializable', | ||
|
|
||
| fnWrap: (move: AnyFn) => (G: unknown, ctx: Ctx, ...args: any[]) => { | ||
| const result = move(G, ctx, ...args); | ||
| /* istanbul ignore if */ | ||
| if (process.env.NODE_ENV === 'production') { | ||
| // Do not perform this check in production. | ||
| return result; | ||
| } | ||
| if (!isSerializable(result)) { | ||
| throw new Error(`Move state is not JSON-serialiazable. See | ||
| https://boardgame.io/documentation/#/?id=state for more information.`); | ||
| } | ||
| return result; | ||
| }, | ||
| }; | ||
|
|
||
| export default SerializablePlugin; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.