-
Notifications
You must be signed in to change notification settings - Fork 2
/
Parcel.ts
43 lines (38 loc) · 1.41 KB
/
Parcel.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
// This module wouldn't duplicate Cargo.ts if TypeScript supported static+abstract members
import { SignatureOptions } from '../..';
import * as serialization from '../ramf/serialization';
import { InvalidMessageError } from './InvalidMessageError';
import { CargoMessageSet } from './payloads/CargoMessageSet';
import { ServiceMessage } from './payloads/ServiceMessage';
import { RAMFMessage } from './RAMFMessage';
const concreteMessageTypeOctet = 0x50;
const concreteMessageVersionOctet = 0;
export class Parcel extends RAMFMessage<ServiceMessage> {
public static async deserialize(parcelSerialized: ArrayBuffer): Promise<Parcel> {
if (CargoMessageSet.MAX_MESSAGE_LENGTH < parcelSerialized.byteLength) {
throw new InvalidMessageError(
`Parcels must not span more than ${CargoMessageSet.MAX_MESSAGE_LENGTH} octets ` +
`(got ${parcelSerialized.byteLength} octets)`,
);
}
return serialization.deserialize(
parcelSerialized,
concreteMessageTypeOctet,
concreteMessageVersionOctet,
Parcel,
);
}
protected readonly deserializePayload = ServiceMessage.deserialize;
public async serialize(
senderPrivateKey: CryptoKey,
signatureOptions?: SignatureOptions,
): Promise<ArrayBuffer> {
return serialization.serialize(
this,
concreteMessageTypeOctet,
concreteMessageVersionOctet,
senderPrivateKey,
signatureOptions,
);
}
}