From 4383d15c5a375b7e189888aa22f198557997db47 Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Mon, 9 Dec 2019 12:06:52 -0500 Subject: [PATCH] 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. --- src/system/DeltaPublisher.ts | 7 ++- src/system/avro/AvroFactory.ts | 66 ++--------------------- src/types/avro-js.d.ts | 89 +++++++++++++++++++++++++++++++ test/system/DeltaPublisherTest.ts | 5 +- 4 files changed, 99 insertions(+), 68 deletions(-) create mode 100644 src/types/avro-js.d.ts diff --git a/src/system/DeltaPublisher.ts b/src/system/DeltaPublisher.ts index 94c925c..5def3a9 100644 --- a/src/system/DeltaPublisher.ts +++ b/src/system/DeltaPublisher.ts @@ -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 ); } diff --git a/src/system/avro/AvroFactory.ts b/src/system/avro/AvroFactory.ts index 0a07a32..32ba1ec 100644 --- a/src/system/avro/AvroFactory.ts +++ b/src/system/avro/AvroFactory.ts @@ -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 ): 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, - opts?: Record - ): Buffer | null; - - - /** - * Write to a buffer - * - * @param data - the data to write - * @param buffer - the buffer that will be written to - */ - encode( data: Record, buffer: Buffer ): void; - - - /** - * Output to a json string - * - * @param data - the data to format - * - * @return the formatted data - */ - toString( data: Record ): 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 ); -} \ No newline at end of file +} diff --git a/src/types/avro-js.d.ts b/src/types/avro-js.d.ts new file mode 100644 index 0000000..a2eff67 --- /dev/null +++ b/src/types/avro-js.d.ts @@ -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 . + */ +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 ): 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, + opts?: Record + ): Buffer | null; + + + /** + * Write to a buffer + * + * @param data - the data to write + * @param buffer - the buffer that will be written to + */ + encode( data: Record, buffer: Buffer ): void; + + + /** + * Output to a json string + * + * @param data - the data to format + * + * @return the formatted data + */ + toString( data: Record ): 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, +}; diff --git a/test/system/DeltaPublisherTest.ts b/test/system/DeltaPublisherTest.ts index fcb788c..b5dfed1 100644 --- a/test/system/DeltaPublisherTest.ts +++ b/test/system/DeltaPublisherTest.ts @@ -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 timestamp: 123123123, data: >{}, } -} \ No newline at end of file +}