Skip to content

Commit

Permalink
Use enums for format in literal packet
Browse files Browse the repository at this point in the history
  • Loading branch information
larabr committed Nov 17, 2021
1 parent 9630a77 commit 065732a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/message.js
Expand Up @@ -862,9 +862,9 @@ export async function createMessage({ text, binary, filename, date = new Date(),
}
const literalDataPacket = new LiteralDataPacket(date);
if (text !== undefined) {
literalDataPacket.setText(input, format);
literalDataPacket.setText(input, enums.write(enums.literal, format));
} else {
literalDataPacket.setBytes(input, format);
literalDataPacket.setBytes(input, enums.write(enums.literal, format));
}
if (filename !== undefined) {
literalDataPacket.setFilename(filename);
Expand Down
12 changes: 6 additions & 6 deletions src/packet/literal_data.js
Expand Up @@ -35,7 +35,7 @@ class LiteralDataPacket {
* @param {Date} date - The creation date of the literal package
*/
constructor(date = new Date()) {
this.format = 'utf8'; // default format for literal data packets
this.format = enums.literal.utf8; // default format for literal data packets
this.date = util.normalizeDate(date);
this.text = null; // textual data representation
this.data = null; // literal data representation
Expand All @@ -46,9 +46,9 @@ class LiteralDataPacket {
* Set the packet data to a javascript native string, end of line
* will be normalized to \r\n and by default text is converted to UTF8
* @param {String | ReadableStream<String>} text - Any native javascript string
* @param {utf8|binary|text|mime} [format] - The format of the string of bytes
* @param {enums.literal} [format] - The format of the string of bytes
*/
setText(text, format = 'utf8') {
setText(text, format = enums.literal.utf8) {
this.format = format;
this.text = text;
this.data = null;
Expand All @@ -70,7 +70,7 @@ class LiteralDataPacket {
/**
* Set the packet data to value represented by the provided string of bytes.
* @param {Uint8Array | ReadableStream<Uint8Array>} bytes - The string of bytes
* @param {utf8|binary|text|mime} format - The format of the string of bytes
* @param {enums.literal} format - The format of the string of bytes
*/
setBytes(bytes, format) {
this.format = format;
Expand Down Expand Up @@ -123,7 +123,7 @@ class LiteralDataPacket {
async read(bytes) {
await stream.parse(bytes, async reader => {
// - A one-octet field that describes how the data is formatted.
const format = enums.read(enums.literal, await reader.readByte());
const format = await reader.readByte(); // enums.literal

const filename_len = await reader.readByte();
this.filename = util.decodeUTF8(await reader.readBytes(filename_len));
Expand All @@ -145,7 +145,7 @@ class LiteralDataPacket {
const filename = util.encodeUTF8(this.filename);
const filename_length = new Uint8Array([filename.length]);

const format = new Uint8Array([enums.write(enums.literal, this.format)]);
const format = new Uint8Array([this.format]);
const date = util.writeDate(this.date);

return util.concatUint8Array([format, filename_length, filename, date]);
Expand Down
4 changes: 2 additions & 2 deletions test/general/openpgp.js
Expand Up @@ -3481,7 +3481,7 @@ aOU=
}).then(async function (message) {
const literals = message.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1);
expect(literals[0].format).to.equal('binary');
expect(literals[0].format).to.equal(openpgp.enums.literal.binary);
expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
Expand Down Expand Up @@ -3510,7 +3510,7 @@ aOU=
}).then(async function (message) {
const literals = message.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1);
expect(literals[0].format).to.equal('mime');
expect(literals[0].format).to.equal(openpgp.enums.literal.mime);
expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
Expand Down

0 comments on commit 065732a

Please sign in to comment.