All files binaryFormatter.js

99.06% Statements 213/215
91.2% Branches 83/91
100% Functions 19/19
99.06% Lines 213/215

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638    1x 1x 1x 1x                   1x   1x 1x 1x 1x       1x   1x     1x                               1x       1x                                                             1x       1x                                                                     1x     1x                                                                             1x         1x                             1x                               1x                                                                               1x                 70607x 4x   70603x 1x   70602x 40562x   30040x 4587x   25453x 25453x 70579x   25451x                   56490x 56490x 20363x 20363x 56466x   20363x   36127x 36127x 3673x   32454x 32454x 32452x   2x 2x 1x               1x       6x 12x     4x     2x         10x   10x 6x 6x 4x     6x                               10x       10x 6x     4x 4x     4x 4x                 7x               7x 7x       7x 7x   7x   7x 1x   6x   7x 7x 7x 7x 1x   6x         6x 6x 6x 5x     5x     6x 2x     6x 18x 12x   6x 6x 6x 6x 5x     5x     6x 2x       6x 6x 4x 4x 4x 4x     6x 6x   6x       4x           4x 4x     4x 4x     4x 4x 9x 3x   6x 6x 3x 3x 3x 3x 3x   3x   3x       3x 3x 3x 6x 6x 6x 6x 6x 2x     3x 1x   2x   3x       3x 3x 3x           16x 7x 6x   9x 4x 3x 1x   2x 1x   1x   5x 2x         3x         9x               9x 9x 9x 6x   3x 3x 3x 3x 3x   9x 9x 12x 12x 12x 12x 8x   4x 4x 4x 4x 4x   12x   9x         3x 3x 3x 3x   3x       16x 9x 9x   7x   3x 1x 2x 1x   1x   3x   4x 2x 2x         2x       14x 14x 14x       3x 3x         13x 11x       14x 14x 14x 14x 14x     1x                      
'use strict';
 
const avro = require( 'avro-js' );
const semver = require( 'semver' );
const { dataDir, readJSON } = require( './fileUtils.js' );
const { validatesAsQuote, validatesAsReference, validatesAsString } = require( './schema.js' );
const {
	convertZListToItemArray,
	findTypeConverterToCodeIdentity,
	findFunctionIdentity,
	findIdentity,
	findTypeConverterFromCodeIdentity,
	isObject,
	isZReference,
	isZType
} = require( './utils.js' );
 
const Z1_SCHEMA_ = readJSON( dataDir( 'avro', 'Z1.avsc' ) );
const Z6_SCHEMA_ = readJSON( dataDir( 'avro', 'Z6.avsc' ) );
const Z9_SCHEMA_ = readJSON( dataDir( 'avro', 'Z9.avsc' ) );
const Z99_SCHEMA_ = readJSON( dataDir( 'avro', 'Z99.avsc' ) );
 
// A ZObject is the union of arbitrary Z1s, Z6s, and Z9s.
// Note that Z1_SCHEMA_ must go last because it references the others by name.
const Z1_UNION_ = [ Z6_SCHEMA_, Z9_SCHEMA_, Z99_SCHEMA_, Z1_SCHEMA_ ];
// semver 0.0.1
const avroSchema = avro.parse( Z1_UNION_ );
 
// Requests in v0.0.2 also indicate whether to use reentrance.
const V002_REQUEST_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ZOBJECT',
	fields: [
		{
			name: 'reentrant',
			type: 'boolean'
		},
		{
			name: 'zobject',
			type: Z1_UNION_
		}
	]
};
// semver 0.0.2
const zobjectSchemaV002 = avro.parse( V002_REQUEST_SCHEMA_ );
 
// Requests in v0.0.3 rely on a minimal set of information instead of an entire
// Z7, allowing for further compression and facilitating later extension.
const V003_REQUEST_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ZOBJECT',
	fields: [
		{
			name: 'reentrant',
			type: 'boolean'
		},
		{
			name: 'codingLanguage',
			type: 'string'
		},
		{
			name: 'codeString',
			type: 'string'
		},
		{
			name: 'functionName',
			type: 'string'
		},
		{
			name: 'functionArguments',
			type: {
				type: 'map',
				values: Z1_UNION_
			}
		}
	]
};
// semver 0.0.3
const requestSchemaV003 = avro.parse( V003_REQUEST_SCHEMA_ );
 
// Requests in v0.0.4 rely on a minimal set of information instead of an entire
// Z7, allowing for further compression and facilitating later extension.
const V004_REQUEST_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ZOBJECT',
	fields: [
		{
			name: 'reentrant',
			type: 'boolean'
		},
		{
			name: 'remainingTime',
			type: 'double'
		},
		{
			name: 'codingLanguage',
			type: 'string'
		},
		{
			name: 'codeString',
			type: 'string'
		},
		{
			name: 'functionName',
			type: 'string'
		},
		{
			name: 'functionArguments',
			type: {
				type: 'map',
				values: Z1_UNION_
			}
		}
	]
};
// semver 0.0.4
const requestSchemaV004 = avro.parse( V004_REQUEST_SCHEMA_ );
 
// v0.0.5: like v0.0.4 but with a request ID!
const V005_REQUEST_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ZOBJECT',
	fields: [
		{
			name: 'reentrant',
			type: 'boolean'
		},
		{
			name: 'remainingTime',
			type: 'double'
		},
		{
			name: 'codingLanguage',
			type: 'string'
		},
		{
			name: 'codeString',
			type: 'string'
		},
		{
			name: 'functionName',
			type: 'string'
		},
		{
			name: 'requestId',
			type: 'string'
		},
		{
			name: 'functionArguments',
			type: {
				type: 'map',
				values: Z1_UNION_
			}
		}
	]
};
// semver 0.0.5
const requestSchemaV005 = avro.parse( V005_REQUEST_SCHEMA_ );
 
// v0.1.0: first new version after release.
// Like v0.0.5 but with type converters
// Also encapsulates functionName, codeString in a CODE record.
const CODE_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'CODE',
	fields: [
		{
			name: 'codeString',
			type: 'string'
		},
		{
			name: 'functionName',
			type: 'string'
		}
	]
};
const ARGUMENT_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ARGUMENT',
	fields: [
		{
			name: 'deserializer',
			type: [ 'null', 'ztypes.CODE' ],
			default: null
		},
		{
			name: 'object',
			type: Z1_UNION_
		}
	]
};
const V010_REQUEST_SCHEMA_ = {
	type: 'record',
	namespace: 'ztypes',
	name: 'ZOBJECT',
	fields: [
		{
			name: 'reentrant',
			type: 'boolean'
		},
		{
			name: 'remainingTime',
			type: 'double'
		},
		{
			name: 'requestId',
			type: 'string'
		},
		{
			name: 'codingLanguage',
			type: 'string'
		},
		{
			name: 'function',
			type: CODE_SCHEMA_
		},
		{
			name: 'serializer',
			type: [ 'null', 'ztypes.CODE' ],
			default: null
		},
		{
			name: 'functionArguments',
			type: {
				type: 'map',
				values: ARGUMENT_SCHEMA_
			}
		}
	]
};
// semver 0.1.0
const requestSchemaV010 = avro.parse( V010_REQUEST_SCHEMA_ );
 
/**
 * Transform a ZObject into the form expected by the binary schema.
 *
 * @param {Object} ZObject a normal-form ZObject
 * @return {Object} an object in the intermediate binary format
 */
function convertNormalForBinaryFormat( ZObject ) {
	if ( !isObject( ZObject ) ) {
		throw new Error( `ZObject was not an Object: ${ JSON.stringify( ZObject ) }.` );
	}
	if ( validatesAsQuote( ZObject ).isValid() ) {
		return { 'ztypes.Z99': { Z99K1: JSON.stringify( ZObject.Z99K1 ) } };
	}
	if ( validatesAsReference( ZObject ).isValid() ) {
		return { 'ztypes.Z9': { Z9K1: ZObject.Z9K1 } };
	}
	if ( validatesAsString( ZObject ).isValid() ) {
		return { 'ztypes.Z6': { Z6K1: ZObject.Z6K1 } };
	}
	const valueMap = {};
	for ( const key of Object.keys( ZObject ) ) {
		valueMap[ key ] = convertNormalForBinaryFormat( ZObject[ key ] );
	}
	return { 'ztypes.Z1': { valueMap: valueMap } };
}
 
/**
 * Recover a ZObject from the form imposed by the binary schema.
 *
 * @param {Object} binaryFormatZObject a ZObject in the intermediate binary format
 * @return {Object} a normal-form ZObject
 */
function recoverNormalFromBinaryFormat( binaryFormatZObject ) {
	const Z1 = binaryFormatZObject[ 'ztypes.Z1' ];
	if ( Z1 !== undefined ) {
		const result = {};
		for ( const key of Object.keys( Z1.valueMap ) ) {
			result[ key ] = recoverNormalFromBinaryFormat( Z1.valueMap[ key ] );
		}
		return result;
	}
	const Z6 = binaryFormatZObject[ 'ztypes.Z6' ];
	if ( Z6 !== undefined ) {
		return { Z1K1: 'Z6', Z6K1: Z6.Z6K1 };
	}
	const Z9 = binaryFormatZObject[ 'ztypes.Z9' ];
	if ( Z9 !== undefined ) {
		return { Z1K1: 'Z9', Z9K1: Z9.Z9K1 };
	}
	const Z99 = binaryFormatZObject[ 'ztypes.Z99' ];
	if ( Z99 !== undefined ) {
		return {
			Z1K1: {
				Z1K1: 'Z9',
				Z9K1: 'Z99'
			},
			Z99K1: JSON.parse( Z99.Z99K1 )
		};
	}
	throw new Error( 'Invalid binary form; must define one of ztypes.(Z1|Z6|Z9|Z99}' );
}
 
function programmingLanguagesMatch( desiredLanguage, retrievedLanguage ) {
	for ( const language of [ 'python', 'javascript' ] ) {
		if (
			desiredLanguage.startsWith( language ) &&
                retrievedLanguage.startsWith( language ) ) {
			return true;
		}
	}
	return false;
}
 
function findTypeConverterForProgrammingLanguage( desiredLanguage, typeConverterList, getCode ) {
	const typeConverterArray = (
		( typeConverterList === undefined ) ?
			[] : convertZListToItemArray( typeConverterList ) );
	for ( const typeConverter of typeConverterArray ) {
		const retrievedLanguage = getCode( typeConverter ).Z16K1.Z61K1.Z6K1;
		if ( programmingLanguagesMatch( desiredLanguage, retrievedLanguage ) ) {
			return typeConverter;
		}
	}
	return null;
}
 
/**
 * Given a programming language and a list of type converters, find the one whose
 * programming language is compatible with the desired language.
 *
 * @param {string} desiredLanguage the desired programming lanugage
 * @param {Object} typeConverterList a Z881/List of either Z46/Type converters to code or
 *   Z64/Type converters from code
 * @param {Function} getCode callback which retrieves the Code object from a given Z46 or Z64
 * @param {Function} getIdentity get Z9/Reference Identity for a Z46 or Z64
 * @return {Object|null} a matching type converter, or null if none is found
 */
function getCodeObjectForTypeConverterList(
	desiredLanguage, typeConverterList, getCode, getIdentity ) {
	const typeConverter = findTypeConverterForProgrammingLanguage(
		desiredLanguage,
		typeConverterList,
		getCode );
	if ( typeConverter === null ) {
		return null;
	}
	// The Type Converter's Identity must be a Z9/Reference or we can't proceed.
	const typeConverterIdentity = getIdentity( typeConverter );
	Iif ( typeConverterIdentity === null ) {
		return null;
	}
	const typeConverterCode = getCode( typeConverter );
	return {
		'ztypes.CODE': {
			codeString: typeConverterCode.Z16K2.Z6K1,
			functionName: typeConverterIdentity.Z9K1
		}
	};
}
 
function convertRequestToBinaryV010Plus( functionCallRequest, enableTypeConverters = false ) {
	const response = {
		reentrant: functionCallRequest.reentrant,
		remainingTime: functionCallRequest.remainingTime,
		requestId: functionCallRequest.requestId,
		function: {},
		functionArguments: {}
	};
 
	const functionCall = functionCallRequest.zobject;
	const theFunction = functionCall.Z7K1;
 
	// We assume that the orchestrator will have already selected a single implementation,
	// so the first implementation will be the only one.
	const firstImplementation = theFunction.Z8K4.K1;
	const functionCode = firstImplementation.Z14K3;
 
	const programmingLanguageObject = functionCode.Z16K1;
	let programmingLanguage;
	if ( isZReference( programmingLanguageObject ) ) {
		programmingLanguage = programmingLanguageObject.Z9K1;
	} else {
		programmingLanguage = programmingLanguageObject.Z61K1.Z6K1;
	}
	response.codingLanguage = programmingLanguage;
	response.function.codeString = functionCode.Z16K2.Z6K1;
	const functionIdentity = findFunctionIdentity( theFunction );
	if ( functionIdentity === null ) {
		throw new Error( 'Identity of Z8 was not a Z9: ' + JSON.stringify( theFunction ) );
	}
	response.function.functionName = functionIdentity.Z9K1;
 
	// Add a type converter if the information is present in the function's return type.
	// If return type is not given as a Z4/Type, or if it lacks the Z4K8 key,
	// we cannot populate the type converter.
	const returnType = theFunction.Z8K2;
	let fromTypeConverterCode = null;
	if ( enableTypeConverters ) {
		fromTypeConverterCode = getCodeObjectForTypeConverterList(
			programmingLanguage,
			returnType.Z4K8,
			( Z64 ) => Z64.Z64K3,
			findTypeConverterFromCodeIdentity );
	}
	if ( fromTypeConverterCode !== null ) {
		response.serializer = fromTypeConverterCode;
	}
 
	for ( const key of Object.keys( functionCall ) ) {
		if ( key === 'Z1K1' || key === 'Z7K1' ) {
			continue;
		}
		const value = functionCall[ key ];
		const argument = {};
		let toTypeConverterCode = null;
		if ( enableTypeConverters ) {
			toTypeConverterCode = getCodeObjectForTypeConverterList(
				programmingLanguage,
				value.Z1K1.Z4K7,
				( Z46 ) => Z46.Z46K3,
				findTypeConverterToCodeIdentity );
		}
		if ( toTypeConverterCode !== null ) {
			argument.deserializer = toTypeConverterCode;
		}
 
		// No reason to type convert the whole dang type.
		let valueToTypeConvert = value;
		if ( isZType( value.Z1K1 ) ) {
			const typeIdentity = findIdentity( value.Z1K1.Z4K1 );
			Eif ( typeIdentity !== null ) {
				valueToTypeConvert = { ...value };
				valueToTypeConvert.Z1K1 = typeIdentity;
			}
		}
		argument.object = convertNormalForBinaryFormat( valueToTypeConvert );
		response.functionArguments[ key ] = argument;
	}
	return response;
}
 
function convertRequestToBinaryV003Plus( ZObject ) {
	const response = {
		reentrant: ZObject.reentrant,
		functionArguments: {}
	};
 
	// This is the only difference between semver v0.0.3 and v0.0.4.
	Eif ( ZObject.remainingTime !== null ) {
		response.remainingTime = ZObject.remainingTime;
	}
	// This is the only difference between semver v0.0.4 and v0.0.5.
	Eif ( ZObject.requestId !== null ) {
		response.requestId = ZObject.requestId;
	}
 
	const actualObject = ZObject.zobject;
	for ( const key of Object.keys( actualObject ) ) {
		if ( key === 'Z1K1' ) {
			continue;
		}
		const value = actualObject[ key ];
		if ( key === 'Z7K1' ) {
			response.functionName = value.Z8K5.Z9K1;
			const firstImplementation = value.Z8K4.K1;
			response.codingLanguage = firstImplementation.Z14K3.Z16K1.Z61K1.Z6K1;
			response.codeString = firstImplementation.Z14K3.Z16K2.Z6K1;
			continue;
		}
		response.functionArguments[ key ] = convertNormalForBinaryFormat( value );
	}
	return response;
}
 
function convertFormattedRequestToBinaryV010Plus( request ) {
	const result = { ...request };
	result.functionArguments = {};
	for ( const key of Object.keys( request.functionArguments ) ) {
		const argument = {};
		result.functionArguments[ key ] = argument;
		const original = request.functionArguments[ key ];
		argument.object = convertNormalForBinaryFormat( original.object );
		if ( original.deserializer ) {
			argument.deserializer = { 'ztypes.CODE': original.deserializer };
		}
	}
	if ( result.serializer ) {
		result.serializer = { 'ztypes.CODE': result.serializer };
	} else {
		delete result.serializer;
	}
	return result;
}
 
function convertFormattedRequestToBinary( request, version ) {
	Eif ( semver.gte( version, '0.1.0' ) ) {
		const response = convertFormattedRequestToBinaryV010Plus( request );
		return requestSchemaV010.toBuffer( response );
	}
	throw new Error( 'Formatted request -> binary operation only supported for semvers >= 0.1.0' );
}
 
function convertZObjectToBinary( ZObject, version = '0.1.0', enableTypeConverters = false ) {
	if ( semver.gte( version, '0.1.0' ) ) {
		const response = convertRequestToBinaryV010Plus( ZObject, enableTypeConverters );
		return requestSchemaV010.toBuffer( response );
	}
	if ( semver.gte( version, '0.0.3' ) ) {
		const response = convertRequestToBinaryV003Plus( ZObject );
		if ( semver.gte( version, '0.0.5' ) ) {
			return requestSchemaV005.toBuffer( response );
		}
		if ( semver.gte( version, '0.0.4' ) ) {
			return requestSchemaV004.toBuffer( response );
		}
		return requestSchemaV003.toBuffer( response );
	}
	if ( semver.gte( version, '0.0.2' ) ) {
		return zobjectSchemaV002.toBuffer( {
			reentrant: ZObject.reentrant,
			zobject: convertNormalForBinaryFormat( ZObject.zobject )
		} );
	}
	return avroSchema.toBuffer( convertNormalForBinaryFormat( ZObject ) );
}
 
function recoverRequestFromAvroFormat010Plus( recovered ) {
	// Copy all members so that the result loses the ZOBJECT type annotation.
	const result = {
		reentrant: recovered.reentrant,
		remainingTime: recovered.remainingTime,
		requestId: recovered.requestId,
		codingLanguage: recovered.codingLanguage,
		function: {},
		functionArguments: {}
	};
	result.function.codeString = recovered.function.codeString;
	result.function.functionName = recovered.function.functionName;
	if ( recovered.serializer === null ) {
		result.serializer = null;
	} else {
		const rawFromTypeConverter = recovered.serializer[ 'ztypes.CODE' ];
		const fromTypeConverter = {};
		fromTypeConverter.codeString = rawFromTypeConverter.codeString;
		fromTypeConverter.functionName = rawFromTypeConverter.functionName;
		result.serializer = fromTypeConverter;
	}
	result.functionArguments = {};
	for ( const key of Object.keys( recovered.functionArguments ) ) {
		const argument = {};
		const original = recovered.functionArguments[ key ];
		argument.object = recoverNormalFromBinaryFormat( original.object );
		if ( original.deserializer === null ) {
			argument.deserializer = null;
		} else {
			const rawToTypeConverter = original.deserializer[ 'ztypes.CODE' ];
			const toTypeConverter = {};
			toTypeConverter.codeString = rawToTypeConverter.codeString;
			toTypeConverter.functionName = rawToTypeConverter.functionName;
			argument.deserializer = toTypeConverter;
		}
		result.functionArguments[ key ] = argument;
	}
	return result;
}
 
function recoverRequestFromAvroFormat3Plus( recovered ) {
	// Copy all members so that the result loses the ZOBJECT type annotation.
	const result = { ...recovered };
	for ( const key of Object.keys( recovered.functionArguments ) ) {
		const argument = recovered.functionArguments[ key ];
		result.functionArguments[ key ] = recoverNormalFromBinaryFormat( argument );
	}
	return result;
}
 
function getZObjectFromBinary( buffer, version = '0.1.0' ) {
	if ( semver.gte( version, '0.1.0' ) ) {
		const recovered = requestSchemaV010.fromBuffer( buffer );
		return recoverRequestFromAvroFormat010Plus( recovered );
	}
	if ( semver.gte( version, '0.0.3' ) ) {
		let recovered;
		if ( semver.gte( version, '0.0.5' ) ) {
			recovered = requestSchemaV005.fromBuffer( buffer );
		} else if ( semver.gte( version, '0.0.4' ) ) {
			recovered = requestSchemaV004.fromBuffer( buffer );
		} else {
			recovered = requestSchemaV003.fromBuffer( buffer );
		}
		return recoverRequestFromAvroFormat3Plus( recovered );
	}
	if ( semver.gte( version, '0.0.2' ) ) {
		const recovered = zobjectSchemaV002.fromBuffer( buffer );
		return {
			reentrant: recovered.reentrant,
			zobject: recoverNormalFromBinaryFormat( recovered.zobject )
		};
	}
	return recoverNormalFromBinaryFormat( avroSchema.fromBuffer( buffer ) );
}
 
function addVersionToBuffer( formatted, version ) {
	const semverBuffer = Buffer.from( version );
	const lengthBuffer = Buffer.from( String.fromCharCode( semverBuffer.length ) );
	return Buffer.concat( [ lengthBuffer, semverBuffer, formatted ] );
}
 
function convertFormattedRequestToVersionedBinary( request, version ) {
	const response = convertFormattedRequestToBinary( request, version );
	return addVersionToBuffer( response, version );
}
 
function convertWrappedZObjectToVersionedBinary(
	wrappedZObject, version, enableTypeConverters = false ) {
	const response = convertZObjectToBinary( wrappedZObject, version, enableTypeConverters );
	return addVersionToBuffer( response, version );
}
 
function getWrappedZObjectFromVersionedBinary( buffer ) {
	const length = buffer.slice( 0, 1 );
	const lengthPivot = 1 + length.toString().charCodeAt( 0 );
	const version = buffer.slice( 1, lengthPivot ).toString();
	const avroFormatted = buffer.slice( lengthPivot, buffer.length );
	return getZObjectFromBinary( avroFormatted, version );
}
 
module.exports = {
	avroSchema,
	convertFormattedRequestToVersionedBinary,
	convertWrappedZObjectToVersionedBinary,
	convertZObjectToBinary,
	getWrappedZObjectFromVersionedBinary,
	getZObjectFromBinary,
	convertNormalForBinaryFormat,
	recoverNormalFromBinaryFormat,
	zobjectSchemaV002
};