Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Alignment Support for Custom Extensions in Encoder/Decoder #245

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,16 @@ export class Decoder<ContextType = undefined> {
throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
}

const extType = this.view.getInt8(this.pos + headOffset);
const data = this.decodeBinary(size, headOffset + 1 /* extType */);
let padding = 0;
let extType = this.view.getInt8(this.pos + headOffset);

// 0xc1 => -63 (Int8) (noop byte)
while (extType === -63) {
padding++;
extType = this.view.getInt8(this.pos + headOffset + padding);
}

const data = this.decodeBinary(size, headOffset + padding + 1 /* extType */);
return this.extensionCodec.decode(data, extType, this.context);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,14 @@ export class Encoder<ContextType = undefined> {
} else {
throw new Error(`Too large extension object: ${size}`);
}
if (ext.align && Number.isInteger(ext.align)) {
const align = ext.align;
const dataPos = this.pos + 1; // + extType size
const padding = (align - (dataPos % align)) % align;
for (let i = 0; i < padding; i++) {
this.writeU8(0xc1); // noop byte
}
}
this.writeI8(ext.type);
this.writeU8a(ext.data);
}
Expand Down
1 change: 1 addition & 0 deletions src/ExtData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export class ExtData {
constructor(
readonly type: number,
readonly data: Uint8Array,
readonly align: number | undefined | null = null,
) {}
}
7 changes: 6 additions & 1 deletion src/ExtensionCodec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ export class ExtensionCodec<ContextType = undefined> implements ExtensionCodecTy
// custom extensions
private readonly encoders: Array<ExtensionEncoderType<ContextType> | undefined | null> = [];
private readonly decoders: Array<ExtensionDecoderType<ContextType> | undefined | null> = [];
private readonly aligns: Array<number | undefined | null> = [];

public constructor() {
this.register(timestampExtension);
}

public register({
type,
align,
encode,
decode,
}: {
type: number;
align?: number;
encode: ExtensionEncoderType<ContextType>;
decode: ExtensionDecoderType<ContextType>;
}): void {
if (type >= 0) {
// custom extensions
this.encoders[type] = encode;
this.decoders[type] = decode;
this.aligns[type] = align;
} else {
// built-in extensions
const index = 1 + type;
Expand Down Expand Up @@ -80,7 +84,8 @@ export class ExtensionCodec<ContextType = undefined> implements ExtensionCodecTy
const data = encodeExt(object, context);
if (data != null) {
const type = i;
return new ExtData(type, data);
const align = this.aligns[type];
return new ExtData(type, data, align);
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/ExtensionCodec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,32 @@ describe("ExtensionCodec", () => {
]);
});
});

context("custom extensions with alignment", () => {
const extensionCodec = new ExtensionCodec();

extensionCodec.register({
type: 0x01,
align: 4,
encode: (object: unknown): Uint8Array | null => {
if (object instanceof Float32Array) {
return new Uint8Array(object.buffer);
}
return null;
},
decode: (data: Uint8Array) => {
return new Float32Array(data.buffer, data.byteOffset, data.byteLength / Float32Array.BYTES_PER_ELEMENT);
},
});

it("encodes and decodes Float32Array type with zero-copy", () => {
const data = {
position: new Float32Array([1.1, 2.2, 3.3, 4.4, 5.5]),
};
const encoded = encode(data, { extensionCodec });
const decoded = decode(encoded, { extensionCodec });
assert.deepStrictEqual(decoded, data);
assert.strictEqual(decoded.position.buffer, encoded.buffer);
});
});
});