New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@colyseus/schema

Package Overview
Dependencies
Maintainers
1
Versions
340
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@colyseus/schema - npm Package Compare versions

Comparing version 2.0.28 to 2.0.29

6

lib/index.js

@@ -20,7 +20,5 @@ "use strict";

(0, typeRegistry_1.registerType)("collection", { constructor: CollectionSchema_1.CollectionSchema, });
const utils_1 = require("./utils");
(0, utils_1.checkTypeScriptConfig)();
// Utils
var utils_2 = require("./utils");
Object.defineProperty(exports, "dumpChanges", { enumerable: true, get: function () { return utils_2.dumpChanges; } });
var utils_1 = require("./utils");
Object.defineProperty(exports, "dumpChanges", { enumerable: true, get: function () { return utils_1.dumpChanges; } });
const encode = require("./encoding/encode");

@@ -27,0 +25,0 @@ exports.encode = encode;

import { Schema } from "./Schema";
export declare function dumpChanges(schema: Schema): {};
export declare function checkTypeScriptConfig(): void;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkTypeScriptConfig = exports.dumpChanges = void 0;
const Reflection_1 = require("./Reflection");
exports.dumpChanges = void 0;
function dumpChanges(schema) {

@@ -24,26 +23,2 @@ const changeTrees = [schema['$changes']];

exports.dumpChanges = dumpChanges;
function checkTypeScriptConfig() {
const r = new Reflection_1.ReflectionField();
const descriptor = Object.getOwnPropertyDescriptor(r, "name");
if (descriptor.get === undefined || descriptor.set === undefined) {
console.error(`
‼️ Please check your tsconfig.json ‼️
@colyseus/schema requires the following settings:
-------------------------------------------------
"compilerOptions": {
// ...
"useDefineForClassFields": false,
"experimentalDecorators": true,
// ...
}
-------------------------------------------------
More info → https://github.com/colyseus/colyseus/issues/510#issuecomment-1507828422
`);
process.exit(1);
}
}
exports.checkTypeScriptConfig = checkTypeScriptConfig;
//# sourceMappingURL=utils.js.map

@@ -8,2 +8,5 @@ "use strict";

};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });

@@ -17,7 +20,28 @@ const util = require("node:util");

const MapSchema_1 = require("./types/MapSchema");
const Encoder_1 = require("./Encoder");
const Decoder_1 = require("./Decoder");
const EncodeOperation_1 = require("./changes/EncodeOperation");
const consts_1 = require("./changes/consts");
const DecodeOperation_1 = require("./changes/DecodeOperation");
const Encoder_1 = require("./encoder/Encoder");
const Decoder_1 = require("./decoder/Decoder");
const EncodeOperation_1 = require("./encoder/EncodeOperation");
const symbols_1 = require("./types/symbols");
const DecodeOperation_1 = require("./decoder/DecodeOperation");
const Reflection_1 = require("./Reflection");
const StateView_1 = require("./encoder/StateView");
function logSingleCall(label, callback) {
const time = Date.now();
const res = callback();
console.log(`${label}:`, Date.now() - time);
return res;
}
function logTime(label, callback) {
const time = Date.now();
for (let i = 0; i < 500000; i++) {
callback();
}
console.log(`${label}:`, Date.now() - time);
}
// // @ts-ignore
// globalThis.perform = function perform() {
// for (let i = 0; i < 500000; i++) {
// encoder.encodeAll();
// }
// }
// const timeout = setInterval(() => {}, 1000);

@@ -91,23 +115,12 @@ // function decorate({ get, set }, context: ClassAccessorDecoratorContext): ClassAccessorDecoratorResult<any, any> {

// decoder.decode(encoded);
// process.exit();
function log(message) {
console.log(util.inspect(message, false, 10, true));
}
Schema_1.Schema[consts_1.$encoder] = EncodeOperation_1.encodeSchemaOperation;
Schema_1.Schema[consts_1.$decoder] = DecodeOperation_1.decodeSchemaOperation;
MapSchema_1.MapSchema[consts_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
MapSchema_1.MapSchema[consts_1.$decoder] = DecodeOperation_1.decodeKeyValueOperation;
ArraySchema_1.ArraySchema[consts_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
ArraySchema_1.ArraySchema[consts_1.$encoder] = DecodeOperation_1.decodeKeyValueOperation;
class Vec3 extends Schema_1.Schema {
}
__decorate([
(0, annotations_1.type)("number")
], Vec3.prototype, "x", void 0);
__decorate([
(0, annotations_1.type)("number")
], Vec3.prototype, "y", void 0);
__decorate([
(0, annotations_1.type)("number")
], Vec3.prototype, "z", void 0);
Schema_1.Schema[symbols_1.$encoder] = EncodeOperation_1.encodeSchemaOperation;
Schema_1.Schema[symbols_1.$decoder] = DecodeOperation_1.decodeSchemaOperation;
MapSchema_1.MapSchema[symbols_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
MapSchema_1.MapSchema[symbols_1.$decoder] = DecodeOperation_1.decodeKeyValueOperation;
ArraySchema_1.ArraySchema[symbols_1.$encoder] = EncodeOperation_1.encodeKeyValueOperation;
ArraySchema_1.ArraySchema[symbols_1.$decoder] = DecodeOperation_1.decodeKeyValueOperation;
// // No need to extend Schema!
// class Vec3 {

@@ -118,11 +131,7 @@ // x: number;

// constructor() {
// // Need to initialize property descriptors
// Schema.initialize(this);
// }
// assign(data: { x: number, y: number, z: number }) {
// this.x = data.x;
// this.y = data.y;
// this.z = data.z;
// return this;
// }
// }
// // Define fields to encode/decode
// Metadata.setFields(Vec3, {

@@ -133,11 +142,10 @@ // x: "number",

// });
// Vec3.prototype[$getByIndex] = function (index: number) {
// return this[this.constructor[Symbol.metadata][index]];
// //
// Vec3[$track] = function (
// changeTree: ChangeTree,
// index: number,
// operation: OPERATION = OPERATION.ADD
// ) {
// changeTree.change(index, operation);
// };
// Vec3.prototype[$deleteByIndex] = function (index: number) {
// this[this.constructor[Symbol.metadata][index]] = undefined;
// };
// Vec3[$track] = function (changeTree, index: number, operation: OPERATION = OPERATION.ADD) {
// changeTree.change(index, operation, encodeSchemaOperation);
// };
// Vec3[$encoder] = encodeSchemaOperation;

@@ -148,9 +156,23 @@ // Vec3[$decoder] = decodeSchemaOperation;

// -------------------------------------------------------------------------------
class Vec3 extends Schema_1.Schema {
}
__decorate([
(0, annotations_1.type)("number"),
__metadata("design:type", Number)
], Vec3.prototype, "x", void 0);
__decorate([
(0, annotations_1.type)("number"),
__metadata("design:type", Number)
], Vec3.prototype, "y", void 0);
__decorate([
(0, annotations_1.type)("number"),
__metadata("design:type", Number)
], Vec3.prototype, "z", void 0);
// Vec3[$track] = function (changeTree, index) {
// changeTree.change(0, OPERATION.ADD);
// };
// Vec3[$encoder] = function (encoder, bytes, changeTree, index, operation) {
// encode.number(bytes, changeTree.ref.x);
// encode.number(bytes, changeTree.ref.y);
// encode.number(bytes, changeTree.ref.z);
// Vec3[$encoder] = function (encoder, bytes, changeTree, index, operation, it) {
// encode.number(bytes, changeTree.ref.x, it);
// encode.number(bytes, changeTree.ref.y, it);
// encode.number(bytes, changeTree.ref.z, it);
// };

@@ -177,6 +199,15 @@ // Vec3[$decoder] = function (

__decorate([
(0, annotations_1.type)(Vec3)
(0, annotations_1.type)(Vec3),
__metadata("design:type", Object)
], Entity.prototype, "position", void 0);
// TODO: @entity shouldn't be required here.
// (TypeContext.register() is required for inheritance support)
class Card extends Schema_1.Schema {
}
__decorate([
(0, annotations_1.type)("string"),
__metadata("design:type", String)
], Card.prototype, "suit", void 0);
__decorate([
(0, annotations_1.type)("number"),
__metadata("design:type", Number)
], Card.prototype, "num", void 0);
class Player extends Entity {

@@ -186,41 +217,117 @@ constructor() {

this.rotation = new Vec3().assign({ x: 0, y: 0, z: 0 });
this.secret = "private info only for this player";
// @type([Card])
// cards = new ArraySchema<Card>(
// new Card().assign({ suit: "Hearts", num: 1 }),
// new Card().assign({ suit: "Spaces", num: 2 }),
// new Card().assign({ suit: "Diamonds", num: 3 }),
// );
}
}
__decorate([
(0, annotations_1.type)(Vec3)
(0, annotations_1.type)(Vec3),
__metadata("design:type", Object)
], Player.prototype, "rotation", void 0);
__decorate([
(0, annotations_1.type)("string"),
__metadata("design:type", String)
], Player.prototype, "secret", void 0);
class Team extends Schema_1.Schema {
constructor() {
super(...arguments);
this.entities = new MapSchema_1.MapSchema();
}
}
__decorate([
(0, annotations_1.type)({ map: Entity }),
__metadata("design:type", Object)
], Team.prototype, "entities", void 0);
class State extends Schema_1.Schema {
constructor() {
super(...arguments);
// @type({ map: Base }) players = new MapSchema<Entity>();
this.num = 0;
this.str = "Hello world!";
// @type(Entity) entity = new Player().assign({
this.teams = new ArraySchema_1.ArraySchema();
// @type({ map: Entity }) entities = new MapSchema<Entity>();
// @type(Entity) entity1 = new Player().assign({
// position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
// });
this.entities = new MapSchema_1.MapSchema();
// @type(Entity) entity2 = new Player().assign({
// position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
// });
}
}
__decorate([
(0, annotations_1.type)("number")
(0, annotations_1.type)("number"),
__metadata("design:type", Number)
], State.prototype, "num", void 0);
__decorate([
(0, annotations_1.type)("string")
(0, annotations_1.type)("string"),
__metadata("design:type", Object)
], State.prototype, "str", void 0);
__decorate([
(0, annotations_1.type)({ map: Entity })
], State.prototype, "entities", void 0);
annotations_1.owned,
(0, annotations_1.type)([Team]),
__metadata("design:type", Object)
], State.prototype, "teams", void 0);
const state = new State();
state.entities.set("one", new Player().assign({
position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
}));
state.entities.set("two", new Player().assign({
position: new Vec3().assign({ x: 7, y: 8, z: 9 }),
rotation: new Vec3().assign({ x: 2, y: 3, z: 4 }),
}));
// for (let i=0;i<1000;i++) {
// state.entities.set("one" + i, new Player().assign({
// position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
// }));
// }
// state.entities.set("one", new Player().assign({
// position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
// }));
// state.entities.set("two", new Player().assign({
// position: new Vec3().assign({ x: 10, y: 10, z: 3 }),
// rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
// }));
function addTeam() {
const team = new Team();
team.entities.set("one", new Player().assign({
position: new Vec3().assign({ x: 1, y: 2, z: 3 }),
rotation: new Vec3().assign({ x: 4, y: 5, z: 6 }),
}));
team.entities.set("two", new Player().assign({
position: new Vec3().assign({ x: 7, y: 8, z: 9 }),
rotation: new Vec3().assign({ x: 2, y: 3, z: 4 }),
}));
state.teams.push(team);
}
addTeam();
addTeam();
const it = { offset: 0 };
const encoder = new Encoder_1.Encoder(state);
const encoded = encoder.encodeAll();
console.log(`(${encoded.length})`, encoded);
// logTime("encode time", () => encoder.encodeAll());
// const encoded = encoder.encode(it);
console.log("> will encode all...", state.toJSON());
const encoded = encoder.encode(it);
console.log("HEAP TOTAL:", process.memoryUsage().heapTotal / 1024 / 1024, "MB");
console.log("HEAP USED:", process.memoryUsage().heapUsed / 1024 / 1024, "MB");
// console.log("encoded.buffer =>", `(${encoded.byteLength} bytes)`);
const sharedOffset = it.offset;
// const team1View = new StateView<State>();
// team1View.owns(state.teams[0]);
const view = new StateView_1.StateView();
view.owns(state.teams[0]);
// view1['owned'].add(state[$changes]);
// view1['owned'].add(state.teams[$changes]);
// view1.owns(state.teams[0]);
// view1.owns(state.entities);
// view1.owns(state.entities.get("one"));
const view2 = new StateView_1.StateView();
console.log(">>> VIEW 2");
view2.owns(state.teams[1]);
// view2.owns(state.entities.get("two"));
console.log("> will encode view 1...");
const viewEncoded1 = encoder.encodeView(view, sharedOffset, it, encoder.sharedBuffer);
console.log("done. view1 encoded =>", `(${viewEncoded1.byteLength} bytes)`);
console.log("> will encode view 2...");
const viewEncoded2 = encoder.encodeView(view2, sharedOffset, it, encoder.sharedBuffer);
console.log("done. view2 encoded =>", `(${viewEncoded2.byteLength} bytes)`);
// setTimeout(() => {

@@ -231,22 +338,29 @@ // for (let i = 0; i < 500000; i++) {

// }, 1000)
// @ts-ignore
globalThis.perform = function perform() {
for (let i = 0; i < 500000; i++) {
encoder.encodeAll();
}
};
function logTime(label, callback) {
const time = Date.now();
for (let i = 0; i < 500000; i++) {
callback();
}
console.log(`${label}:`, Date.now() - time);
}
logTime("encode time", () => encoder.encodeAll());
// logTime("encode time", () => encoder.encodeAll());
// console.log(`encode: (${encoded.length})`, encoded);
// const encodedReflection = Reflection.encode(state, encoder.context);
// const decoded = Reflection.decode(encodedReflection);
const decoder = new Decoder_1.Decoder(new State());
decoder.decode(encoded);
log(decoder.root.toJSON());
console.log("----------------------------------- ENCODE reflection...");
const encodedReflection = Reflection_1.Reflection.encode(state, encoder.context);
console.log("----------------------------------- DECODE reflection...");
const decodedState = Reflection_1.Reflection.decode(encodedReflection);
// const decodedState = new State();
const decoder = new Decoder_1.Decoder(decodedState);
console.log("> will decode...");
// decoder.decode(encoded);
const changes = decoder.decode(viewEncoded1);
// decoder.decode(viewEncoded2);
console.log("CHANGES =>", changes);
// log(new DataView(encoded.buffer, 0, it.offset));
// log(new DataView(viewEncoded1.buffer, 0, it.offset));
log(decodedState.toJSON());
// console.log("encoder.$root.changes =>", encoder.$root.changes.length);
// console.log("encoder.$root.filteredChanges =>", encoder.$root.filteredChanges.length);
// state.teams[0].entities.get("one").position.x = 100;
// // encoder.
// const it2 = { offset: 0 };
// // const encoded = encoder.encode(it);
// const encoded2 = encoder.encode(it2);
// console.log(`> shared encode... (${encoded2.byteLength} bytes)`);
// const viewEncoded3 = encoder.encodeView(view1, sharedOffset, it, encoder.sharedBuffer);
// console.log(`> view1... (${viewEncoded3.byteLength} bytes)`);
// log(decoder.root.toJSON());
// logTime("decode time", () => decoder.decode(encoded));

@@ -253,0 +367,0 @@ // console.log("changes =>", changes);

{
"name": "@colyseus/schema",
"version": "2.0.28",
"version": "2.0.29",
"description": "Binary state serializer with delta encoding for games",

@@ -5,0 +5,0 @@ "bin": {

@@ -24,5 +24,2 @@ export { Schema } from "./Schema";

import { checkTypeScriptConfig } from "./utils";
checkTypeScriptConfig();
// Utils

@@ -29,0 +26,0 @@ export { dumpChanges } from "./utils";

import { Schema } from "./Schema";
import { ReflectionField } from "./Reflection";
import { ChangeTree } from "./changes/ChangeTree";

@@ -30,25 +29,1 @@

}
export function checkTypeScriptConfig() {
const r = new ReflectionField();
const descriptor = Object.getOwnPropertyDescriptor(r, "name");
if (descriptor.get === undefined || descriptor.set === undefined) {
console.error(`
‼️ Please check your tsconfig.json ‼️
@colyseus/schema requires the following settings:
-------------------------------------------------
"compilerOptions": {
// ...
"useDefineForClassFields": false,
"experimentalDecorators": true,
// ...
}
-------------------------------------------------
More info → https://github.com/colyseus/colyseus/issues/510#issuecomment-1507828422
`);
process.exit(1);
}
}

Sorry, the diff of this file is too big to display

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 too big to display

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc