Skip to content

Commit

Permalink
[schema-registry-avro] serialize into JS's standard Uint8Array instea…
Browse files Browse the repository at this point in the history
…d of Buffer (#17549)

* [schema-registry-avro] serialize into JS's standard Uint8Array instead of Buffer

* return a strict Uint8Array
  • Loading branch information
deyaaeldeen authored Sep 13, 2021
1 parent 7d82d33 commit 92de67e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { SchemaRegistry } from '@azure/schema-registry';
export class SchemaRegistryAvroSerializer {
constructor(client: SchemaRegistry, groupName: string, options?: SchemaRegistryAvroSerializerOptions);
deserialize(input: Buffer | Blob | Uint8Array): Promise<unknown>;
serialize(value: unknown, schema: string): Promise<Buffer>;
serialize(value: unknown, schema: string): Promise<Uint8Array>;
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,19 @@ export class SchemaRegistryAvroSerializer {
* @param schema - The Avro schema to use.
* @returns A new buffer with the serialized value
*/
async serialize(value: unknown, schema: string): Promise<Buffer> {
async serialize(value: unknown, schema: string): Promise<Uint8Array> {
const entry = await this.getSchemaByContent(schema);
const payload = entry.type.toBuffer(value);
const buffer = Buffer.alloc(PAYLOAD_OFFSET + payload.length);

buffer.writeUInt32BE(FORMAT_INDICATOR, 0);
buffer.write(entry.id, SCHEMA_ID_OFFSET, SCHEMA_ID_LENGTH, "utf-8");
payload.copy(buffer, PAYLOAD_OFFSET);
return buffer;
return new Uint8Array(
buffer.buffer,
buffer.byteOffset,
buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT
);
}

// REVIEW: signature. See serialize and s/serialize into/deserialize from/.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ describe("SchemaRegistryAvroSerializer", function() {
const registry = createTestRegistry();
const schemaId = await registerTestSchema(registry);
const serializer = await createTestSerializer(false, registry);
const buffer = await serializer.serialize(testValue, testSchema);
const arr = await serializer.serialize(testValue, testSchema);
assert.isUndefined((arr as Buffer).readBigInt64BE);
const buffer = Buffer.from(arr);
assert.strictEqual(0x0, buffer.readUInt32BE(0));
assert.strictEqual(schemaId, buffer.toString("utf-8", 4, 36));
const payload = buffer.slice(36);
Expand Down

0 comments on commit 92de67e

Please sign in to comment.