/
AvroParser.ts
470 lines (418 loc) · 13.7 KB
/
AvroParser.ts
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AvroReadable } from "./AvroReadable";
import { KeyValuePair } from "./utils/utils.common";
import { AbortSignalLike } from "@azure/abort-controller";
/**
* Options to configure the AvroParser read methods.
* See {@link AvroParser.readFixedBytes}, {@link AvroParser.readMap} and etc.
*
* @export
* @interface AvroParserReadOptions
*/
interface AvroParserReadOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof AvroParserReadOptions
*/
abortSignal?: AbortSignalLike;
}
export class AvroParser {
/**
* Reads a fixed number of bytes from the stream.
*
* @static
* @param {AvroReadable} [stream]
* @param {number} [length]
* @param {AvroParserReadOptions} [options={}]
* @returns {Promise<Uint8Array>}
* @memberof AvroParser
*/
public static async readFixedBytes(
stream: AvroReadable,
length: number,
options: AvroParserReadOptions = {}
): Promise<Uint8Array> {
const bytes = await stream.read(length, { abortSignal: options.abortSignal });
if (bytes.length != length) {
throw new Error("Hit stream end.");
}
return bytes;
}
/**
* Reads a single byte from the stream.
*
* @static
* @param {AvroReadable} [stream]
* @param {AvroParserReadOptions} [options={}]
* @returns {Promise<number>}
* @memberof AvroParser
*/
private static async readByte(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
const buf = await AvroParser.readFixedBytes(stream, 1, options);
return buf[0];
}
// int and long are stored in variable-length zig-zag coding.
// variable-length: https://lucene.apache.org/core/3_5_0/fileformats.html#VInt
// zig-zag: https://developers.google.com/protocol-buffers/docs/encoding?csw=1#types
private static async readZigZagLong(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
let zigZagEncoded = 0;
let significanceInBit = 0;
let byte, haveMoreByte, significanceInFloat;
do {
byte = await AvroParser.readByte(stream, options);
haveMoreByte = byte & 0x80;
zigZagEncoded |= (byte & 0x7f) << significanceInBit;
significanceInBit += 7;
} while (haveMoreByte && significanceInBit < 28); // bitwise operation only works for 32-bit integers
if (haveMoreByte) {
// Switch to float arithmetic
zigZagEncoded = zigZagEncoded;
significanceInFloat = 268435456; // 2 ** 28.
do {
byte = await AvroParser.readByte(stream, options);
zigZagEncoded += (byte & 0x7f) * significanceInFloat;
significanceInFloat *= 128; // 2 ** 7
} while (byte & 0x80);
const res = (zigZagEncoded % 2 ? -(zigZagEncoded + 1) : zigZagEncoded) / 2;
if (res < Number.MIN_SAFE_INTEGER || res > Number.MAX_SAFE_INTEGER) {
throw new Error("Integer overflow.");
}
return res;
}
return (zigZagEncoded >> 1) ^ -(zigZagEncoded & 1);
}
public static async readLong(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
return AvroParser.readZigZagLong(stream, options);
}
public static async readInt(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
return AvroParser.readZigZagLong(stream, options);
}
public static async readNull(): Promise<null> {
return null;
}
public static async readBoolean(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<Boolean> {
const b = await AvroParser.readByte(stream, options);
if (b == 1) {
return true;
} else if (b == 0) {
return false;
} else {
throw new Error("Byte was not a boolean.");
}
}
public static async readFloat(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
const u8arr = await AvroParser.readFixedBytes(stream, 4, options);
const view = new DataView(u8arr.buffer, u8arr.byteOffset, u8arr.byteLength);
return view.getFloat32(0, true); // littleEndian = true
}
public static async readDouble(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<number> {
const u8arr = await AvroParser.readFixedBytes(stream, 8, options);
const view = new DataView(u8arr.buffer, u8arr.byteOffset, u8arr.byteLength);
return view.getFloat64(0, true); // littleEndian = true
}
public static async readBytes(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<Uint8Array> {
const size = await AvroParser.readLong(stream, options);
if (size < 0) {
throw new Error("Bytes size was negative.");
}
return await stream.read(size, { abortSignal: options.abortSignal });
}
public static async readString(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<string> {
const u8arr = await AvroParser.readBytes(stream, options);
// polyfill TextDecoder to be backward compatible with older
// nodejs that doesn't expose TextDecoder as a global variable
if (typeof TextDecoder === "undefined" && typeof require !== "undefined") {
(global as any).TextDecoder = require("util").TextDecoder;
}
// FUTURE: need TextDecoder polyfill for IE
let utf8decoder = new TextDecoder();
return utf8decoder.decode(u8arr);
}
private static async readMapPair<T>(
stream: AvroReadable,
readItemMethod: (s: AvroReadable, options?: AvroParserReadOptions) => Promise<T>,
options: AvroParserReadOptions = {}
): Promise<KeyValuePair<T>> {
const key = await AvroParser.readString(stream, options);
// FUTURE: this won't work with readFixed (currently not supported) which needs a length as the parameter.
const value = await readItemMethod(stream, options);
return { key, value };
}
public static async readMap<T>(
stream: AvroReadable,
readItemMethod: (s: AvroReadable, options?: AvroParserReadOptions) => Promise<T>,
options: AvroParserReadOptions = {}
): Promise<Record<string, T>> {
const readPairMethod = async (
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<KeyValuePair<T>> => {
return await AvroParser.readMapPair(stream, readItemMethod, options);
};
const pairs: KeyValuePair<T>[] = await AvroParser.readArray(stream, readPairMethod, options);
let dict: Record<string, T> = {};
for (const pair of pairs) {
dict[pair.key] = pair.value;
}
return dict;
}
private static async readArray<T>(
stream: AvroReadable,
readItemMethod: (s: AvroReadable, options?: AvroParserReadOptions) => Promise<T>,
options: AvroParserReadOptions = {}
): Promise<T[]> {
let items: T[] = [];
for (
let count = await AvroParser.readLong(stream, options);
count != 0;
count = await AvroParser.readLong(stream, options)
) {
if (count < 0) {
// Ignore block sizes
await AvroParser.readLong(stream, options);
count = -count;
}
while (count--) {
const item: T = await readItemMethod(stream, options);
items.push(item);
}
}
return items;
}
}
interface RecordField {
name: string;
type: string | ObjectSchema | (string | ObjectSchema)[]; // Unions may not immediately contain other unions.
}
enum AvroComplex {
RECORD = "record",
ENUM = "enum",
ARRAY = "array",
MAP = "map",
UNION = "union",
FIXED = "fixed"
}
interface ObjectSchema {
type: Exclude<AvroComplex, AvroComplex.UNION>;
name?: string;
aliases?: string;
fields?: RecordField[];
symbols?: string[];
values?: string;
size?: number;
}
export abstract class AvroType {
/**
* Reads an object from the stream.
*
* @param stream
*/
public abstract read(
stream: AvroReadable,
options?: AvroParserReadOptions
): Promise<Object | null>;
/**
* Determines the AvroType from the Avro Schema.
*/
public static fromSchema(schema: string | Object): AvroType {
if (typeof schema == "string") {
return AvroType.fromStringSchema(schema);
} else if (Array.isArray(schema)) {
return AvroType.fromArraySchema(schema);
} else {
return AvroType.fromObjectSchema(schema as ObjectSchema);
}
}
private static fromStringSchema(schema: string): AvroType {
switch (schema) {
case AvroPrimitive.NULL:
case AvroPrimitive.BOOLEAN:
case AvroPrimitive.INT:
case AvroPrimitive.LONG:
case AvroPrimitive.FLOAT:
case AvroPrimitive.DOUBLE:
case AvroPrimitive.BYTES:
case AvroPrimitive.STRING:
return new AvroPrimitiveType(schema as AvroPrimitive);
default:
throw new Error(`Unexpected Avro type ${schema}`);
}
}
private static fromArraySchema(schema: any[]): AvroType {
return new AvroUnionType(schema.map(AvroType.fromSchema));
}
private static fromObjectSchema(schema: ObjectSchema): AvroType {
const type = schema.type;
// Primitives can be defined as strings or objects
try {
return AvroType.fromStringSchema(type);
} catch (err) {}
switch (type) {
case AvroComplex.RECORD:
if (schema.aliases) {
throw new Error(`aliases currently is not supported, schema: ${schema}`);
}
if (!schema.name) {
throw new Error(`Required attribute 'name' doesn't exist on schema: ${schema}`);
}
let fields: Record<string, AvroType> = {};
if (!schema.fields) {
throw new Error(`Required attribute 'fields' doesn't exist on schema: ${schema}`);
}
for (const field of schema.fields) {
fields[field.name] = AvroType.fromSchema(field.type);
}
return new AvroRecordType(fields, schema.name);
case AvroComplex.ENUM:
if (schema.aliases) {
throw new Error(`aliases currently is not supported, schema: ${schema}`);
}
if (!schema.symbols) {
throw new Error(`Required attribute 'symbols' doesn't exist on schema: ${schema}`);
}
return new AvroEnumType(schema.symbols);
case AvroComplex.MAP:
if (!schema.values) {
throw new Error(`Required attribute 'values' doesn't exist on schema: ${schema}`);
}
return new AvroMapType(AvroType.fromSchema(schema.values));
case AvroComplex.ARRAY: // Unused today
case AvroComplex.FIXED: // Unused today
default:
throw new Error(`Unexpected Avro type ${type} in ${schema}`);
}
}
}
enum AvroPrimitive {
NULL = "null",
BOOLEAN = "boolean",
INT = "int",
LONG = "long",
FLOAT = "float",
DOUBLE = "double",
BYTES = "bytes",
STRING = "string"
}
class AvroPrimitiveType extends AvroType {
private _primitive: AvroPrimitive;
constructor(primitive: AvroPrimitive) {
super();
this._primitive = primitive;
}
public async read(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<Object | null> {
switch (this._primitive) {
case AvroPrimitive.NULL:
return await AvroParser.readNull();
case AvroPrimitive.BOOLEAN:
return await AvroParser.readBoolean(stream, options);
case AvroPrimitive.INT:
return await AvroParser.readInt(stream, options);
case AvroPrimitive.LONG:
return await AvroParser.readLong(stream, options);
case AvroPrimitive.FLOAT:
return await AvroParser.readFloat(stream, options);
case AvroPrimitive.DOUBLE:
return await AvroParser.readDouble(stream, options);
case AvroPrimitive.BYTES:
return await AvroParser.readBytes(stream, options);
case AvroPrimitive.STRING:
return await AvroParser.readString(stream, options);
default:
throw new Error("Unknown Avro Primitive");
}
}
}
class AvroEnumType extends AvroType {
private readonly _symbols: string[];
constructor(symbols: string[]) {
super();
this._symbols = symbols;
}
public async read(stream: AvroReadable, options: AvroParserReadOptions = {}): Promise<Object> {
const value = await AvroParser.readInt(stream, options);
return this._symbols[value];
}
}
class AvroUnionType extends AvroType {
private readonly _types: AvroType[];
constructor(types: AvroType[]) {
super();
this._types = types;
}
public async read(
stream: AvroReadable,
options: AvroParserReadOptions = {}
): Promise<Object | null> {
const typeIndex = await AvroParser.readInt(stream, options);
return await this._types[typeIndex].read(stream, options);
}
}
class AvroMapType extends AvroType {
private readonly _itemType: AvroType;
constructor(itemType: AvroType) {
super();
this._itemType = itemType;
}
public async read(stream: AvroReadable, options: AvroParserReadOptions = {}): Promise<Object> {
const readItemMethod = async (
s: AvroReadable,
options?: AvroParserReadOptions
): Promise<Object | null> => {
return await this._itemType.read(s, options);
};
return await AvroParser.readMap(stream, readItemMethod, options);
}
}
class AvroRecordType extends AvroType {
private readonly _name: string;
private readonly _fields: Record<string, AvroType>;
constructor(fields: Record<string, AvroType>, name: string) {
super();
this._fields = fields;
this._name = name;
}
public async read(stream: AvroReadable, options: AvroParserReadOptions = {}): Promise<Object> {
let record: Record<string, Object | null> = {};
record["$schema"] = this._name;
for (const key in this._fields) {
if (this._fields.hasOwnProperty(key)) {
record[key] = await this._fields[key].read(stream, options);
}
}
return record;
}
}