Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tape('XVIZFormatWriter#full matrix', t => {
const dataSources = [
TestXVIZSnapshot,
TestXVIZSnapshotString,
TestXVIZSnapshotBuffer,
TestXVIZSnapshotGLB
];
for (const source of dataSources) {
const xvizObj = new XVIZData(source);
for (const format of [
XVIZ_FORMAT.BINARY_GLB,
XVIZ_FORMAT.JSON_BUFFER,
XVIZ_FORMAT.JSON_STRING
]) {
const sink = new MemorySourceSink();
t.comment(`-- TestCase ${xvizObj.format} to ${format}`);
// Convert the data to the requested format
// data is state_update and this will default to a message sequence of 0
const formatWriter = new XVIZFormatWriter(sink, {format});
formatWriter.writeMessage(0, xvizObj);
// We don't really care about the key as each writer will have
// different identifier, (eg: 1-frame.json vs 1.frame.glb)
for (const [key, val] of sink.entries()) {
// Cover Arrays, Strings, ArrayBuffers
_getFormatOptions(msg) {
// default should be pass-thru of original data
if (!this.format) {
// If no format is specified, we send the 'natural' format
// but it must be a string or arraybuffer, not an OBJECT
// Test to determine if msg is either string or arraybuffer
if (
msg.format === XVIZ_FORMAT.OBJECT ||
(!msg.hasMessage() && typeof msg.buffer !== 'string' && !msg.buffer.byteLength)
) {
return XVIZ_FORMAT.BINARY_GLB;
}
// return the format set to the current data format
return msg.format;
}
return this.format;
}
this.socket = socket;
this.sink = new WebsocketSink(socket, options);
// TODO: options register:
// - compress
// - formatter
this.options = options;
// This is the actual format we use to send data and can change
// based on the message.
this.format = options.format;
if (this.format === XVIZ_FORMAT.OBJECT) {
// We can not output OBJECT on a websocket
this.format = XVIZ_FORMAT.BINARY_GLB;
}
this.writer = null;
// If format is undefined we want to send the 'natural' format of
// the data (as long as it's not an OBJECT).
// Keep track of current 'writer' format
this.writerFormat = null;
this._syncFormatWithWriter(this.format);
}
tape('XVIZFormatWriter#message writing', t => {
const xvizObj = new XVIZData(TestXVIZSnapshot);
const sink = new MemorySourceSink();
const formatWriter = new XVIZFormatWriter(sink, {format: XVIZ_FORMAT.BINARY_GLB});
formatWriter.writeMessage(0, xvizObj);
formatWriter.writeMessage(1, xvizObj);
formatWriter.writeMessage(2, xvizObj);
const expectedKeys = ['2-frame.glb', '3-frame.glb', '4-frame.glb'];
for (const key of expectedKeys) {
t.ok(sink.has(key), `entry ${key} was present`);
}
t.not(sink.has('1-frame.glb'), 'entry 1-frame.glb was not present');
t.end();
});
format: XVIZ_FORMAT.JSON_STRING
},
{
data: ` ${TestXVIZSnapshotString} `,
description: 'XVIZ String with whitespace head and tail',
format: XVIZ_FORMAT.JSON_STRING
},
{
data: TestXVIZSnapshotBuffer,
description: 'XVIZ String Buffer',
format: XVIZ_FORMAT.JSON_BUFFER
},
{
data: TestXVIZSnapshotGLB,
description: 'XVIZ Binary Buffer',
format: XVIZ_FORMAT.BINARY_GLB
},
{
data: Buffer.from(TestXVIZSnapshotBuffer),
description: 'XVIZ String NodeBuffer',
format: XVIZ_FORMAT.JSON_BUFFER,
nodeOnly: true
},
{
data: Buffer.from(TestXVIZSnapshotGLB),
description: 'XVIZ Binary NodeBuffer',
format: 'BINARY_GLB',
nodeOnly: true
}
];
tape('XVIZData#constructor', t => {
tape('XVIZData#type', t => {
const testCases = [
{
description: 'Binary Metadata',
data: MinimalBinaryMetadata,
format: XVIZ_FORMAT.BINARY_GLB,
type: 'metadata'
},
{
description: 'Binary StateUpdate',
data: MinimalBinaryStateUpdate,
format: XVIZ_FORMAT.BINARY_GLB,
type: 'state_update'
}
];
for (const test of testCases) {
const xvizObj = new XVIZData(test.data);
t.equal(
xvizObj.format,
test.format,
`${test.description} matches expected format ${test.format}`