UNPKG

@tamgl/colyseus-schema

Version:

Binary state serializer with delta encoding for games

55 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EncodeSchemaError = void 0; exports.assertType = assertType; exports.assertInstanceType = assertInstanceType; class EncodeSchemaError extends Error { } exports.EncodeSchemaError = EncodeSchemaError; function assertType(value, type, klass, field) { let typeofTarget; let allowNull = false; switch (type) { case "number": case "int8": case "uint8": case "int16": case "uint16": case "int32": case "uint32": case "int64": case "uint64": case "float32": case "float64": typeofTarget = "number"; if (isNaN(value)) { console.log(`trying to encode "NaN" in ${klass.constructor.name}#${field}`); } break; case "bigint64": case "biguint64": typeofTarget = "bigint"; break; case "string": typeofTarget = "string"; allowNull = true; break; case "boolean": // boolean is always encoded as true/false based on truthiness return; default: // skip assertion for custom types // TODO: allow custom types to define their own assertions return; } if (typeof (value) !== typeofTarget && (!allowNull || (allowNull && value !== null))) { let foundValue = `'${JSON.stringify(value)}'${(value && value.constructor && ` (${value.constructor.name})`) || ''}`; throw new EncodeSchemaError(`a '${typeofTarget}' was expected, but ${foundValue} was provided in ${klass.constructor.name}#${field}`); } } function assertInstanceType(value, type, instance, field) { if (!(value instanceof type)) { throw new EncodeSchemaError(`a '${type.name}' was expected, but '${value && value.constructor.name}' was provided in ${instance.constructor.name}#${field}`); } } //# sourceMappingURL=assert.js.map