rtp.js
Version:
RTP stack for Node.js and browser written in TypeScript
102 lines (101 loc) • 4.95 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Serializable_1 = require("../../packets/Serializable");
const helpers_1 = require("../../utils/helpers");
class Foo extends Serializable_1.Serializable {
constructor(view) {
super(view);
}
getByteLength() {
return this.view.byteLength;
}
serialize(buffer, byteOffset) {
const bufferData = this.getSerializationBuffer(buffer, byteOffset);
// Create new DataView with new buffer.
const view = new DataView(bufferData.buffer, bufferData.byteOffset, bufferData.byteLength);
const uint8Array = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
// Copy the entire view into the new buffer.
uint8Array.set(new Uint8Array(this.view.buffer, this.view.byteOffset, this.view.byteLength), 0);
// Update DataView.
this.view = view;
this.setSerializationNeeded(false);
}
clone(buffer, byteOffset, serializationBuffer, serializationByteOffset) {
const view = this.cloneInternal(buffer, byteOffset, serializationBuffer, serializationByteOffset);
return new Foo(view);
}
}
describe('parse Foo 1', () => {
let array;
let view;
let clonedView;
let foo;
beforeEach(() => {
// Buffer of 20 bytes.
array = new Uint8Array([
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
]);
// Create a view with just 10 bytes.
view = new DataView(array.buffer, array.byteOffset, 10);
clonedView = (0, helpers_1.clone)(view);
foo = new Foo(view);
});
test('serializable processing succeeds', () => {
expect(foo.getByteLength()).toBe(10);
expect(foo.needsSerialization()).toBe(false);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), clonedView)).toBe(true);
});
test('serialize() succeeds', () => {
const buffer = new ArrayBuffer(10);
const byteOffset = 0;
foo.serialize(buffer, byteOffset);
expect(foo.getByteLength()).toBe(10);
expect(foo.needsSerialization()).toBe(false);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), clonedView)).toBe(true);
});
test('serialize() succeeds if current buffer is given with a byteOffset that avoids collision', () => {
expect(() => foo.serialize(view.buffer, /* byteOffset */ 10)).not.toThrow();
expect(foo.getByteLength()).toBe(10);
expect(foo.needsSerialization()).toBe(false);
// This is true because obviously both are the same DataView instance,
// however it's been overwritten.
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), clonedView)).toBe(true);
});
test('serialize() fails if current buffer is given with same byteOffset', () => {
expect(() => foo.serialize(view.buffer, /* byteOffset */ 0)).toThrow(Error);
expect(foo.getByteLength()).toBe(10);
expect(foo.needsSerialization()).toBe(false);
// This is true because obviously both are the same DataView instance,
// however it's been overwritten.
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(foo.getView(), clonedView)).toBe(true);
});
test('clone() succeeds', () => {
const clonedFoo = foo.clone();
expect(clonedFoo.getByteLength()).toBe(10);
expect(clonedFoo.needsSerialization()).toBe(false);
expect((0, helpers_1.areDataViewsEqual)(clonedFoo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(clonedFoo.getView(), clonedView)).toBe(true);
});
test('clone() succeeds if current buffer is given', () => {
const clonedFoo = foo.clone(foo.getView().buffer, foo.getView().byteOffset);
expect(clonedFoo.getByteLength()).toBe(10);
expect(clonedFoo.needsSerialization()).toBe(false);
expect((0, helpers_1.areDataViewsEqual)(clonedFoo.getView(), view)).toBe(true);
expect((0, helpers_1.areDataViewsEqual)(clonedFoo.getView(), clonedView)).toBe(true);
});
test('clone() fails if current buffer is given for serialization and it collides', () => {
// Force serialization when cloning.
// @ts-expect-error --- Protected class method.
foo.setSerializationNeeded(true);
const buffer = new ArrayBuffer(10);
const byteOffset = 0;
expect(() => foo.clone(buffer, byteOffset,
/* serializationBuffer */ view.buffer,
/* byteOffset */ 9)).toThrow(RangeError);
});
});