I could understand fromText and fromBinary if they were class methods that returned a newly allocated and constructed message initialized with the specified data (essentially a class factory). That would be in keeping with other APIs. 

However given the current implementation what should they do for the situations you describe below (an existing compressed or encrypted message)? fromText currently just creates another literalData object and adds it to the packet list which is definitely not the right thing. 

getLiteralData is currently pretty useless, it just returns literalData's internal state without any context to tell you what it is, it would make more sense to have it return the literalData object, which would make getText and getBinary redundant. 

So my recommendation would be fromText and fromBinary as described in the first paragraph. getText and getBinary to retrieve the final result, and isBinary to determine which API to call. If you still want getLiteralData then have it return the object rather than the internal data buffer. 

----- Original Message -----

| From: "Thomas Oberndörfer" <notifications@github.com>
| To: "openpgpjs/openpgpjs" <openpgpjs@noreply.github.com>
| Cc: "Robert Nelson" <robertn@the-nelsons.org>
| Sent: Saturday, December 7, 2013 3:43:15 PM
| Subject: Re: [openpgpjs] Merge signature changes from master (#137)

| In src/packet/literal.js:
| >    /**
| >     * Get the byte sequence representing the literal packet data
| >     * @returns {String} A sequence of bytes
| >     */
| >    this.getBytes = function() {
| > -    var bytes = this.data;
| > -    switch (this.format) {
| > -      case 'utf8':
| > -        bytes = bytes.replace(/\n/g, '\r\n');
| > -        bytes = util.encode_utf8(bytes);
| > -        break;
| > -      case 'text':
| > -        bytes = bytes.replace(/\n/g, '\r\n');
| > -        break;
| > -    }
| > -    return bytes;
| > +    return this.format == 'utf8' ? util.encode_utf8(this.data) :
| > this.data;
| >    }
| >

| I wanted to have the Message class representing this composition:

| OpenPGP Message :- Encrypted Message | Signed Message | Compressed Message |
| Literal Message.

| From: http://tools.ietf.org/html/rfc4880#section-11.3

| The idea is that the methods of the Message class "transforms" the message
| object into different representation:

| * Message.decrypt tranforms Encrypted Message into e.g. Compressed Message,
| it returns a new message with a packlist containing the content of the
| packet_symmetrically_encrypted
| * Message.unwrapCompressed returns a new Message with the content of the
| compressed packet, this will be most of time be a packetlist that contains
| literal data packet
| * Message.encrypt does: e.g. Literal Message -> Encrypted Message And so
| on...

| getText makes sense: if your Message does not have a literal data packet you
| just return null.

| But what should setText do if your Message is e.g. a Compressed Message or a
| Encrypted Message?
| Same for setBytes. That's why I think in these situations you would always
| create a new message with
| fromText or fromBinary methods.

| getBytes is also very generic. If you have a encrypted Encrypted Message what
| do you want to return?
| getLiteralData is explicit: it either returns literal data or null if your
| message doesn't have it.

| —
| Reply to this email directly or view it on GitHub .