@cryptovoxels/messages
Advanced tools
Comparing version 2.0.0-4 to 2.0.0-5
@@ -24,2 +24,3 @@ import { Quaternion } from './utils'; | ||
} | ||
export declare const encoderCreator: <Type>() => (msg: Type) => Uint8Array; | ||
export declare const decode: (data: any) => any; | ||
@@ -26,0 +27,0 @@ export declare type PingMessage = { |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GenericEncoder = exports.DestroyAvatarEncoder = exports.WorldStateEncoder = exports.JoinEncoder = exports.UpdateAvatarEncoder = exports.WompMessageEncoder = exports.VoiceStateMessageEncoder = exports.AvatarVoiceState = exports.TypingMessageEncoder = exports.NewCostumeEncoder = exports.MemeCubeEncoder = exports.EmoteEncoder = exports.AvatarChangedEncoder = exports.CreateAvatarEncoder = exports.ChatEncoder = exports.TrafficEncoder = exports.LoginCompleteEncoder = exports.LoginEncoder = exports.AnonEncoder = exports.PongEncoder = exports.PingEncoder = exports.decode = exports.MessageType = exports.Emotes = void 0; | ||
exports.GenericEncoder = exports.DestroyAvatarEncoder = exports.WorldStateEncoder = exports.JoinEncoder = exports.UpdateAvatarEncoder = exports.WompMessageEncoder = exports.VoiceStateMessageEncoder = exports.AvatarVoiceState = exports.TypingMessageEncoder = exports.NewCostumeEncoder = exports.MemeCubeEncoder = exports.EmoteEncoder = exports.AvatarChangedEncoder = exports.CreateAvatarEncoder = exports.ChatEncoder = exports.TrafficEncoder = exports.LoginCompleteEncoder = exports.LoginEncoder = exports.AnonEncoder = exports.PongEncoder = exports.PingEncoder = exports.decode = exports.encoderCreator = exports.MessageType = exports.Emotes = void 0; | ||
const msgpack_1 = require("@msgpack/msgpack"); | ||
const uuidParse = __importStar(require("uuid-parse")); | ||
const utils_1 = require("./utils"); | ||
const uuid_1 = require("uuid"); | ||
var constant_1 = require("./constant"); | ||
@@ -62,9 +43,10 @@ Object.defineProperty(exports, "Emotes", { enumerable: true, get: function () { return constant_1.Emotes; } }); | ||
const encoderCreator = () => (msg) => msgPacker.encode(msg); | ||
exports.encoderCreator = encoderCreator; | ||
const decode = (data) => msgUnpacker.decode(data); | ||
exports.decode = decode; | ||
exports.PingEncoder = encoderCreator(); | ||
exports.PongEncoder = encoderCreator(); | ||
exports.AnonEncoder = encoderCreator(); | ||
exports.LoginEncoder = encoderCreator(); | ||
exports.LoginCompleteEncoder = encoderCreator(); | ||
exports.PingEncoder = exports.encoderCreator(); | ||
exports.PongEncoder = exports.encoderCreator(); | ||
exports.AnonEncoder = exports.encoderCreator(); | ||
exports.LoginEncoder = exports.encoderCreator(); | ||
exports.LoginCompleteEncoder = exports.encoderCreator(); | ||
// A codec to compress theLoginComplete message | ||
@@ -92,5 +74,5 @@ extensionCodec.register({ | ||
}); | ||
exports.TrafficEncoder = encoderCreator(); | ||
exports.ChatEncoder = encoderCreator(); | ||
exports.CreateAvatarEncoder = encoderCreator(); | ||
exports.TrafficEncoder = exports.encoderCreator(); | ||
exports.ChatEncoder = exports.encoderCreator(); | ||
exports.CreateAvatarEncoder = exports.encoderCreator(); | ||
// A codec to compress the CreateAvatar message | ||
@@ -103,3 +85,3 @@ extensionCodec.register({ | ||
} | ||
return msgpack_1.encode([uuidParse.parse(input.uuid || ''), input.description.name, walletToBytes(input.description.wallet)]); | ||
return msgpack_1.encode([encodeUUID(input.uuid), input.description.name, walletToBytes(input.description.wallet)]); | ||
}, | ||
@@ -110,3 +92,3 @@ decode: (data) => { | ||
type: MessageType.createAvatar, | ||
uuid: uuidParse.unparse(res[0]), | ||
uuid: decodeUUID(res[0]), | ||
description: { | ||
@@ -119,7 +101,7 @@ name: res[1], | ||
}); | ||
exports.AvatarChangedEncoder = encoderCreator(); | ||
exports.EmoteEncoder = encoderCreator(); | ||
exports.MemeCubeEncoder = encoderCreator(); | ||
exports.NewCostumeEncoder = encoderCreator(); | ||
exports.TypingMessageEncoder = encoderCreator(); | ||
exports.AvatarChangedEncoder = exports.encoderCreator(); | ||
exports.EmoteEncoder = exports.encoderCreator(); | ||
exports.MemeCubeEncoder = exports.encoderCreator(); | ||
exports.NewCostumeEncoder = exports.encoderCreator(); | ||
exports.TypingMessageEncoder = exports.encoderCreator(); | ||
// 64 | ||
@@ -133,5 +115,5 @@ var AvatarVoiceState; | ||
})(AvatarVoiceState = exports.AvatarVoiceState || (exports.AvatarVoiceState = {})); | ||
exports.VoiceStateMessageEncoder = encoderCreator(); | ||
exports.WompMessageEncoder = encoderCreator(); | ||
exports.UpdateAvatarEncoder = encoderCreator(); | ||
exports.VoiceStateMessageEncoder = exports.encoderCreator(); | ||
exports.WompMessageEncoder = exports.encoderCreator(); | ||
exports.UpdateAvatarEncoder = exports.encoderCreator(); | ||
// A codec that only encodes the updateAvatar data to an array we 'compress' this very common (124b/183b) | ||
@@ -145,3 +127,3 @@ // in reality the message is smaller due to the avatar rotation and neck rotations Z axis is zero:d out since we dont tilt avatars | ||
} | ||
return msgpack_1.encode([uuidParse.parse(input.uuid), Float32Array.from(input.position), utils_1.compressQuaternion(input.bodyOrientation), utils_1.compressQuaternion(input.headOrientation)]); | ||
return msgpack_1.encode([encodeUUID(input.uuid), Float32Array.from(input.position), utils_1.compressQuaternion(input.bodyOrientation), utils_1.compressQuaternion(input.headOrientation)]); | ||
}, | ||
@@ -153,3 +135,3 @@ decode: (data) => { | ||
type: MessageType.updateAvatar, | ||
uuid: uuidParse.unparse(res[0]), | ||
uuid: decodeUUID(res[0]), | ||
position: uint8ToFloat32(res[1]), | ||
@@ -162,3 +144,3 @@ bodyOrientation: utils_1.decompressQuaternion(res[2]), | ||
}); | ||
exports.JoinEncoder = encoderCreator(); | ||
exports.JoinEncoder = exports.encoderCreator(); | ||
extensionCodec.register({ | ||
@@ -181,3 +163,3 @@ type: MessageType.join, | ||
}); | ||
exports.WorldStateEncoder = encoderCreator(); | ||
exports.WorldStateEncoder = exports.encoderCreator(); | ||
extensionCodec.register({ | ||
@@ -200,3 +182,3 @@ type: MessageType.worldState, | ||
}); | ||
exports.DestroyAvatarEncoder = encoderCreator(); | ||
exports.DestroyAvatarEncoder = exports.encoderCreator(); | ||
/** Utility functions from here on **/ | ||
@@ -231,3 +213,17 @@ function uint8ToFloat32(data) { | ||
// all encoders can in practice encode all types of messages, but we typehint the message argument with encoderCreator | ||
exports.GenericEncoder = encoderCreator(); | ||
exports.GenericEncoder = exports.encoderCreator(); | ||
function encodeUUID(uuid) { | ||
try { | ||
return uuid_1.parse(uuid); | ||
} | ||
catch { } | ||
return []; | ||
} | ||
function decodeUUID(buf) { | ||
try { | ||
return uuid_1.stringify(buf); | ||
} | ||
catch { } | ||
return '00000000-0000-0000-0000-000000000000'; | ||
} | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@cryptovoxels/messages", | ||
"version": "2.0.0-4", | ||
"version": "2.0.0-5", | ||
"description": "common protocol for multiplayer messages", | ||
@@ -23,5 +23,3 @@ "main": "dist/index.js", | ||
"@msgpack/msgpack": "^2.7.0", | ||
"uuid-parse": "^1.1.0", | ||
"bufferutil": "^4.0.3", | ||
"utf-8-validate": "^5.0.5" | ||
"uuid": "^8.3.2" | ||
}, | ||
@@ -31,3 +29,3 @@ "devDependencies": { | ||
"@types/tape": "^4.13.0", | ||
"@types/uuid-parse": "^1.0.0", | ||
"@types/uuid": "^8.3.3", | ||
"fast-check": "^2.17.0", | ||
@@ -34,0 +32,0 @@ "npm-run-all": "^4.1.5", |
import { Decoder, Encoder, decode as decodeAlias, encode, ExtensionCodec } from '@msgpack/msgpack' | ||
import * as uuidParse from 'uuid-parse' | ||
import { compressQuaternion, decompressQuaternion, Quaternion, rotationDecode, rotationEncode } from './utils' | ||
import { float32Array } from 'fast-check' | ||
import { parse, stringify } from 'uuid' | ||
export { Emotes } from './constant' | ||
@@ -44,3 +43,3 @@ | ||
const encoderCreator = | ||
export const encoderCreator = | ||
<Type>() => | ||
@@ -142,3 +141,3 @@ (msg: Type): Uint8Array => | ||
} | ||
return encode([uuidParse.parse(input.uuid || ''), input.description.name, walletToBytes(input.description.wallet)]) | ||
return encode([encodeUUID(input.uuid), input.description.name, walletToBytes(input.description.wallet)]) | ||
}, | ||
@@ -149,3 +148,3 @@ decode: (data): CreateAvatarMessage => { | ||
type: MessageType.createAvatar, | ||
uuid: uuidParse.unparse(res[0]), | ||
uuid: decodeUUID(res[0]), | ||
description: { | ||
@@ -245,3 +244,3 @@ name: res[1], | ||
} | ||
return encode([uuidParse.parse(input.uuid), Float32Array.from(input.position), compressQuaternion(input.bodyOrientation), compressQuaternion(input.headOrientation)]) | ||
return encode([encodeUUID(input.uuid), Float32Array.from(input.position), compressQuaternion(input.bodyOrientation), compressQuaternion(input.headOrientation)]) | ||
}, | ||
@@ -255,3 +254,3 @@ decode: (data): UpdateAvatarMessage => { | ||
type: MessageType.updateAvatar, | ||
uuid: uuidParse.unparse(res[0]), | ||
uuid: decodeUUID(res[0]), | ||
position: uint8ToFloat32(res[1]), | ||
@@ -376,1 +375,15 @@ bodyOrientation: decompressQuaternion(res[2]), | ||
export const GenericEncoder = encoderCreator<Message>() | ||
function encodeUUID(uuid: string): ArrayLike<number> { | ||
try { | ||
return parse(uuid) | ||
} catch {} | ||
return [] | ||
} | ||
function decodeUUID(buf: ArrayLike<number>): string { | ||
try { | ||
return stringify(buf) | ||
} catch {} | ||
return '00000000-0000-0000-0000-000000000000' | ||
} |
@@ -40,3 +40,3 @@ import test, { Test } from 'tape' | ||
createTestCase(msg.TypingMessageEncoder, 50, { type: msg.MessageType.typing, uuid: uuid }), | ||
createTestCase(msg.CreateAvatarEncoder, 71, { type: msg.MessageType.createAvatar, uuid: uuid, description: { name: 'a', wallet: wallet } }), | ||
createTestCase(msg.CreateAvatarEncoder, 60, { type: msg.MessageType.createAvatar, uuid: uuid, description: { name: 'a', wallet: wallet } }), | ||
createTestCase(msg.DestroyAvatarEncoder, 50, { type: msg.MessageType.destroyAvatar, uuid: uuid }), | ||
@@ -60,3 +60,4 @@ ] | ||
encoder: msg.UpdateAvatarEncoder, | ||
size: 59, | ||
// @todo original size was 76 | ||
size: 48, | ||
msg: { type: msg.MessageType.updateAvatar, uuid: uuid, position: [1.23, 4.56, 7.89], bodyOrientation: [0.24, 0.665, -0.665, 0.24], headOrientation: [0.24, 0.665, -0.665, 0.24], animation: 2 }, | ||
@@ -86,3 +87,3 @@ }, | ||
const encoded = WorldStateEncoder({ type: msg.MessageType.worldState, avatars: avatars, server_ts: 120 }) | ||
t.equal(encoded.length, 174, 'encoded message size is correct') | ||
t.equal(encoded.length, 150, 'encoded message size is correct') | ||
const decoded: WorldStateMessage = msg.decode(encoded) | ||
@@ -122,3 +123,3 @@ t.equal(decoded.avatars.length, avatars.length, 'we have three avatar update messages') | ||
const encoded = JoinEncoder(joinMsg) | ||
t.equal(encoded.length, 352, 'encoded message size is correct') | ||
t.equal(encoded.length, 304, 'encoded message size is correct') | ||
const decoded: JoinMessage = msg.decode(encoded) | ||
@@ -159,3 +160,3 @@ t.equal(decoded.type, msg.MessageType.join) | ||
createTestCase(msg.LoginCompleteEncoder, 17, { type: msg.MessageType.loginComplete, user: { name: 'anonymous', wallet: anonWallet }, ts: 100, server_ts: 120 }), | ||
createTestCase(msg.CreateAvatarEncoder, 44, { type: msg.MessageType.createAvatar, uuid: uuid, description: { name: 'anonymous', wallet: anonWallet } }), | ||
createTestCase(msg.CreateAvatarEncoder, 33, { type: msg.MessageType.createAvatar, uuid: uuid, description: { name: 'anonymous', wallet: anonWallet } }), | ||
] | ||
@@ -162,0 +163,0 @@ |
Sorry, the diff of this file is not supported yet
70877
4
1547
+ Addeduuid@^8.3.2
+ Addeduuid@8.3.2(transitive)
- Removedbufferutil@^4.0.3
- Removedutf-8-validate@^5.0.5
- Removeduuid-parse@^1.1.0
- Removeduuid-parse@1.1.0(transitive)