Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools oft miss.
colyseus-events
Advanced tools
Generate json-patch events from colyseus state.
import { wireEvents } from 'colyseus-events';
const room: Room<GameState> = await client.joinOrCreate("game");
const events = wireEvents(room.state, new EventEmitter());
// `events` will emit json-patch events whenever the room state changes
Due to breaking API changes in Colyseus, this version only supports Colyseus 0.14 and above (@colyseus/schema >= 1.0.2)
The schema types new to Colyseus 0.14 (CollectionSchema
and SetSchema
) are not yet supported. please open an issue if you would like to see them supported.
npm install colyseus-events --save
Import wireEvents
and call it once when connecting to a room on the client side:
import { wireEvents } from 'colyseus-events';
const room: Room<GameState> = await client.joinOrCreate("game");
const events = wireEvents(room.state, new EventEmitter());
// `events` will emit json-patch events whenever the room state changes
then you can wire listeners to events
using the JSON-pointer of target field as event name.
To change the behavior for parts or all of your state, use customWireEvents
to produce your own version of wireEvents
:
import { customWireEvents, coreVisitors} from 'colyseus-events';
const special = {
visit: (traverse: Traverse, state: Container, events: Events, jsonPath: string): boolean => { /* see Visitor implementation below*/},
};
const wireEvents = customWireEvents([ special, ...coreVisitors]);
const room: Room<GameState> = await client.joinOrCreate("game");
const events = wireEvents(room.state, new EventEmitter());
// `events` will emit json-patch events whenever the room state changes
customWireEvents
accepts a single argument, a collection of Visitor
objects, and returns afunctyion compatible with the default wireEvents
. In fact, the default wireEvents
function is itself the result customWireEvents
when using coreVisitors
as the argument. it is defined in wire-events.ts by the following line:
export const wireEvents = customWireEvents(coreVisitors);
The order of the visitors is crucial: they are executed as a fallback chain: the first visitor to return true
will stop the chain and prevent later visitors from wiring the same state. So be sure to order them by specificity: the more specific handlers should first check for their use case before the generic visitors, and coreVisitors
should be the last visitors.
A visitor must implement a single method, visit
. This method should:
false
if not..onChange
and possibly .onAdd
and .onRemove
, or perhaspse the .listen
mechanism.MapSchema
:{
visit: (traverse: Traverse, state: Container, events: Events, namespace: string) => {
// Check if it is going to handle the state object, and return `false` if not.
if (!(state instanceof MapSchema)) {
return false;
}
// Hook on new elements
state.onAdd = (value: Colyseus, field) => {
const fieldNamespace = `${namespace}/${field}`; // path to the new element
events.emit(namespace, Add(fieldNamespace, value)); // emit the add event
traverse(value, events, fieldNamespace); // call the traverse function on the new value
};
...
// Call the traverse function for each child member of the state.
for (const [field, value] of state.entries()) {
const fieldNamespace = `${namespace}/${field}`;
traverse(value as Colyseus, events, fieldNamespace);
}
// finally return true. this will break the visitors fallback chain and complete the wiring for this object.
return true;
}
}
For example, given the room state:
export class Inner extends Schema {
@type('uint8') public x = 0;
@type('uint8') public y = 0;
}
export class GameState extends Schema {
@type('uint8') public foo = 0;
@type(Inner) public bar = new Inner();
@type(['uint8']) public numbersArray = new ArraySchema<number>();
@type({ map: 'uint8' }) public mapNumbers = new MapSchema<number>();
}
when changing a value in Schema or collection (ArraySchema or MapSchema), an event will be emitted. The name of the event will be the JSON-pointer describing the location of the property. The event value will be a "replace" JSON Patch corresponding with the change. For example:
room.state.foo = 1
an event named '/foo'
will be emitted with value { op: 'replace', path: '/foo', value: 1 }
room.numbersArray[0] = 1
(assuming numbersArray had a previous value at index 0) an event named '/numbersArray/1'
will be emitted with value { op: 'replace', path: '/numbersArray/1', value: 1 }
room.mapNumbers.set('F00', 1)
(assuming mapNumbers had a previous value at key F00
) an event named '/mapNumbers/F00'
will be emitted with value { op: 'replace', path: '/mapNumbers/F00', value: 1 }
room.state.bar.x = 1
an event named '/bar/x'
will be emitted with value { op: 'replace', path: '/bar/x', value: 1 }
room.state.bar = new Inner()
an event named '/bar'
will be emitted with value { op: 'replace', path: '/bar', value: {{the actual object in state.bar }} }
...and so on.
when adding or removing elements in a collection (ArraySchema or MapSchema), an event will be also be emitted. The name of the event will be the JSON-pointer describing the location of the container. The event value will be a "add" or "remove" JSON Patch corresponding with the change. the path
in the event value will point to the location of the element that was added or removed.
For example:
room.numbersArray.push(1)
an event named '/numbersArray'
will be triggered with value { op: 'add', path: '/numbersArray/0', value: 1 }
room.numbersArray.pop()
an event named '/numbersArray'
will be triggered with value { op: 'remove', path: '/numbersArray/0' }
room.mapNumbers.set('F00', 1)
an event named '/mapNumbers'
will be triggered with value { op: 'add', path: '/mapNumbers/F00', value: 1 }
room.mapNumbers.delete('F00')
an event named '/mapNumbers'
will be triggered with value { op: 'remove', path: '/mapNumbers/F00' }
...and so on.
You are welcomed to explore the tests in the github repo for more examples.
to install a development environment, you need to have node.js git installd.
Then, git clone
this repo locally and run:
$ npm install
$ npm test
and that's it, you've just installed the development environment!
This project is written with VSCode in mind. specifically configured for these extensions: dbaeumer.vscode-eslint, esbenp.prettier-vscode.
npm run test
execute all tests.
npm run clean
Removes any built code and any built executables.
npm run build
Cleans, then builds the library.
Your built code will be in the ./dist/
directory.
FAQs
generate notification events from colyseus state
The npm package colyseus-events receives a total of 1 weekly downloads. As such, colyseus-events popularity was classified as not popular.
We found that colyseus-events demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools oft miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.