Make PacketList a valid subtype of Array and update Packet.tag types (#1289)

Changes:
- Implementation:
  - Remove `PacketList.prototype.concat` and `push`
    (we solely rely on `Array.push` instead)
  - Fix https://github.com/openpgpjs/openpgpjs/issues/907 by
    correctly handling result of `filterByTag`
  - Implement `write()` method for `Trust` and `Marker` packets,
    to make them compatible with the `BasePacket` interface
- Types:
  - Simplify and updated `PacketList` type definitions
  - Fix types for `Packet.tag`, which is `static` since
    https://github.com/openpgpjs/openpgpjs/pull/1268
  - Prevent passing SubkeyPackets where KeyPackets are expected,
    and vice versa
This commit is contained in:
larabr
2021-04-29 17:18:39 +02:00
committed by GitHub
parent 2d07c43030
commit aeddac438e
17 changed files with 189 additions and 154 deletions

View File

@@ -6,9 +6,13 @@
* - if it fails to run, edit this file to match the actual library API, then edit the definitions file (openpgp.d.ts) accordingly.
*/
import { generateKey, readKey, readKeys, Key, readMessage, createMessage, Message, createCleartextMessage, encrypt, decrypt, sign, verify, config } from '../..';
import { expect } from 'chai';
import {
generateKey, readKey, readKeys, Key,
readMessage, createMessage, Message, createCleartextMessage,
encrypt, decrypt, sign, verify, config, enums,
LiteralDataPacket, PacketList, CompressedDataPacket, PublicKeyPacket, PublicSubkeyPacket, SecretKeyPacket, SecretSubkeyPacket
} from '../..';
(async () => {
@@ -78,6 +82,34 @@ import { expect } from 'chai';
const verifiedBinaryData: Uint8Array = verifiedBinary.data;
expect(verifiedBinaryData).to.deep.equal(binary);
// Generic packetlist
const packets = new PacketList();
expect(packets.push()).to.equal(0);
expect(packets.push(new LiteralDataPacket())).to.equal(1);
packets.map(packet => packet.write);
// @ts-expect-error for unsafe downcasting
packets.map((packet: LiteralDataPacket) => packet.getText());
// @ts-expect-error for non-packet element
try { new PacketList().push(1); } catch (e) {}
// Packetlist of specific type
const literalPackets = new PacketList<LiteralDataPacket>();
literalPackets.push(new LiteralDataPacket());
literalPackets[0].write();
literalPackets.map((packet: LiteralDataPacket) => packet);
packets.push(...literalPackets);
// @ts-expect-error for incompatible packetlist type
literalPackets.push(...packets);
// @ts-expect-error for incompatible packet type
new PacketList<LiteralDataPacket>().push(new CompressedDataPacket());
// @ts-expect-error for incompatible packet type
new PacketList<PublicKeyPacket>().push(new PublicSubkeyPacket());
// @ts-expect-error for incompatible packet type
new PacketList<SecretKeyPacket>().push(new SecretSubkeyPacket());
expect(LiteralDataPacket.tag).to.equal(enums.packet.literalData);
// // Detached - sign cleartext message (armored)
// import { Message, sign } from 'openpgp';
// const message = await createMessage({ text: util.removeTrailingSpaces(text) });