Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Gerwitz <mike.gerwitz@ryansg.com>2019-12-09 12:06:52 -0500
committerAustin Schaffer <austin.schaffer@ryansg.com>2019-12-12 10:27:09 -0500
commit4383d15c5a375b7e189888aa22f198557997db47 (patch)
tree92013d00288b72a705582f2bf0d98ef11e590128
parent9d6cb23e161eb333b6a7cbc25fa6db03b1ac10dc (diff)
downloadliza-4383d15c5a375b7e189888aa22f198557997db47.tar.gz
liza-4383d15c5a375b7e189888aa22f198557997db47.tar.bz2
liza-4383d15c5a375b7e189888aa22f198557997db47.zip
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.
-rw-r--r--src/system/DeltaPublisher.ts7
-rw-r--r--src/system/avro/AvroFactory.ts66
-rw-r--r--src/types/avro-js.d.ts89
-rw-r--r--test/system/DeltaPublisherTest.ts5
4 files changed, 99 insertions, 68 deletions
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<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 );
-} \ 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 <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,
+};
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<any>
timestamp: <UnixTimestamp>123123123,
data: <DeltaResult<any>>{},
}
-} \ No newline at end of file
+}