1
0
Fork 0

AvroFactory: Extract avro-js type definitions into src/types

The declarations that were intended for the library were moved, but
additional things that were outside of the library were kept in place.
master
Mike Gerwitz 2019-12-09 12:06:52 -05:00 committed by Austin Schaffer
parent 9d6cb23e16
commit 4383d15c5a
4 changed files with 99 additions and 68 deletions

View File

@ -27,11 +27,10 @@ import { EventEmitter } from "events";
import { DocumentId } from '../document/Document';
import { context } from '../error/ContextError';
import { AmqpError } from '../error/AmqpError';
import { AvroSchema, AvroEncoderCtr } from './avro/AvroFactory';
import { AvroEncoderCtr } from './avro/AvroFactory';
import { AmqpConnection } from './amqp/AmqpConnection';
const avro = require( 'avro-js' );
import { AvroSchema, parse } from "avro-js";
export class DeltaPublisher implements AmqpPublisher
{
@ -62,7 +61,7 @@ export class DeltaPublisher implements AmqpPublisher
private readonly _encoder_ctr: AvroEncoderCtr,
private readonly _conn: AmqpConnection,
) {
this._schema = avro.parse( this.SCHEMA_PATH );
this._schema = parse( this.SCHEMA_PATH );
}

View File

@ -20,71 +20,13 @@
*/
import { Duplex } from 'stream';
const avro = require( 'avro-js' );
export interface AvroSchema
{
/**
* Write data to a buffer
*
* @param data - the data to write
*
* @return the buffer if successful
*/
toBuffer( data: Record<string, any> ): Buffer | null;
/**
* Check if data is valid against schema
*
* @param data - the data to validate
* @param opts - options specified as key/values
*
* @return the buffer if it is valid
*/
isValid(
data: Record<string, any>,
opts?: Record<string, any>
): Buffer | null;
/**
* Write to a buffer
*
* @param data - the data to write
* @param buffer - the buffer that will be written to
*/
encode( data: Record<string, any>, buffer: Buffer ): void;
/**
* Output to a json string
*
* @param data - the data to format
*
* @return the formatted data
*/
toString( data: Record<string, any> ): string;
/**
* Deserialize from a buffer
*
* @param buffer - the buffer to read from
*
* @return the resulting data
*/
fromBuffer( buffer: Buffer ): any;
}
import * as avro from "avro-js";
/** The avro encoder constructor type */
export type AvroEncoderCtr = ( type: AvroSchema ) => Duplex;
export type AvroEncoderCtr = ( type: avro.AvroSchema ) => Duplex;
/** The avro encoder constructor */
export function createAvroEncoder( schema: AvroSchema ): Duplex
export function createAvroEncoder( schema: avro.AvroSchema ): Duplex
{
return new avro.streams.BlockEncoder( schema );
}
}

89
src/types/avro-js.d.ts vendored 100644
View File

@ -0,0 +1,89 @@
/**
* avro-js type definitions
*
* Copyright (C) 2010-2019 R-T Specialty, LLC.
*
* This file is part of the Liza Data Collection Framework.
*
* liza is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Duplex } from 'stream';
declare module "avro-js";
declare function parse( schema: string ): AvroSchema;
export declare interface AvroSchema
{
/**
* Write data to a buffer
*
* @param data - the data to write
*
* @return the buffer if successful
*/
toBuffer( data: Record<string, any> ): Buffer | null;
/**
* Check if data is valid against schema
*
* @param data - the data to validate
* @param opts - options specified as key/values
*
* @return the buffer if it is valid
*/
isValid(
data: Record<string, any>,
opts?: Record<string, any>
): Buffer | null;
/**
* Write to a buffer
*
* @param data - the data to write
* @param buffer - the buffer that will be written to
*/
encode( data: Record<string, any>, buffer: Buffer ): void;
/**
* Output to a json string
*
* @param data - the data to format
*
* @return the formatted data
*/
toString( data: Record<string, any> ): string;
/**
* Deserialize from a buffer
*
* @param buffer - the buffer to read from
*
* @return the resulting data
*/
fromBuffer( buffer: Buffer ): any;
}
declare class BlockEncoder extends Duplex
{
constructor( schema: AvroSchema );
}
export declare const streams: {
BlockEncoder: typeof BlockEncoder,
};

View File

@ -31,9 +31,10 @@ import { Channel } from 'amqplib';
import {
createAvroEncoder,
AvroEncoderCtr,
AvroSchema,
} from '../../src/system/avro/AvroFactory';
import { AvroSchema } from "avro-js";
import { expect, use as chai_use } from 'chai';
chai_use( require( 'chai-as-promised' ) );
@ -481,4 +482,4 @@ function createMockDelta(): Delta<any>
timestamp: <UnixTimestamp>123123123,
data: <DeltaResult<any>>{},
}
}
}