FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Implement Alignment Support for Custom Extensions in Encoder/Decoder by eddig · Pull Request #245 · msgpack/msgpack-javascript · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (5) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 10 additions & 2 deletions src/Decoder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,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);
});
});
});

Back | FazBrowse Home | New Git URL