Refactor src/packet/*.js to use import & export

This commit is contained in:
Tankred Hase 2016-02-05 14:44:22 +07:00
parent 9ebe31a4ec
commit 70ac662073
20 changed files with 148 additions and 183 deletions

View File

@ -1,74 +1,70 @@
'use strict';
/**
* @requires enums
* @module packet
*/
var enums = require('../enums.js');
import enums from '../enums.js';
// This is pretty ugly, but browserify needs to have the requires explicitly written.
/** @see module:packet/compressed */
export { default as Compressed } from './compressed.js';
/** @see module:packet/sym_encrypted_integrity_protected */
export { default as SymEncryptedIntegrityProtected } from './sym_encrypted_integrity_protected.js';
/** @see module:packet/public_key_encrypted_session_key */
export { default as PublicKeyEncryptedSessionKey } from './public_key_encrypted_session_key.js';
/** @see module:packet/sym_encrypted_session_key */
export { default as SymEncryptedSessionKey } from './sym_encrypted_session_key.js';
/** @see module:packet/literal */
export { default as Literal } from './literal.js';
/** @see module:packet/public_key */
export { default as PublicKey } from './public_key.js';
/** @see module:packet/symmetrically_encrypted */
export { default as SymmetricallyEncrypted } from './symmetrically_encrypted.js';
/** @see module:packet/marker */
export { default as Marker } from './marker.js';
/** @see module:packet/public_subkey */
export { default as PublicSubkey } from './public_subkey.js';
/** @see module:packet/user_attribute */
export { default as UserAttribute } from './user_attribute.js';
/** @see module:packet/one_pass_signature */
export { default as OnePassSignature } from './one_pass_signature.js';
/** @see module:packet/secret_key */
export { default as SecretKey } from './secret_key.js';
/** @see module:packet/userid */
export { default as Userid } from './userid.js';
/** @see module:packet/secret_subkey */
export { default as SecretSubkey } from './secret_subkey.js';
/** @see module:packet/signature */
export { default as Signature } from './signature.js';
/** @see module:packet/trust */
export { default as Trust } from './trust.js';
module.exports = {
/** @see module:packet/compressed */
Compressed: require('./compressed.js'),
/** @see module:packet/sym_encrypted_integrity_protected */
SymEncryptedIntegrityProtected: require('./sym_encrypted_integrity_protected.js'),
/** @see module:packet/public_key_encrypted_session_key */
PublicKeyEncryptedSessionKey: require('./public_key_encrypted_session_key.js'),
/** @see module:packet/sym_encrypted_session_key */
SymEncryptedSessionKey: require('./sym_encrypted_session_key.js'),
/** @see module:packet/literal */
Literal: require('./literal.js'),
/** @see module:packet/public_key */
PublicKey: require('./public_key.js'),
/** @see module:packet/symmetrically_encrypted */
SymmetricallyEncrypted: require('./symmetrically_encrypted.js'),
/** @see module:packet/marker */
Marker: require('./marker.js'),
/** @see module:packet/public_subkey */
PublicSubkey: require('./public_subkey.js'),
/** @see module:packet/user_attribute */
UserAttribute: require('./user_attribute.js'),
/** @see module:packet/one_pass_signature */
OnePassSignature: require('./one_pass_signature.js'),
/** @see module:packet/secret_key */
SecretKey: require('./secret_key.js'),
/** @see module:packet/userid */
Userid: require('./userid.js'),
/** @see module:packet/secret_subkey */
SecretSubkey: require('./secret_subkey.js'),
/** @see module:packet/signature */
Signature: require('./signature.js'),
/** @see module:packet/trust */
Trust: require('./trust.js'),
/**
* Allocate a new packet
* @param {String} tag property name from {@link module:enums.packet}
* @returns {Object} new packet object with type based on tag
*/
newPacketFromTag: function (tag) {
return new this[packetClassFromTagName(tag)]();
},
/**
* Allocate a new packet from structured packet clone
* See {@link http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#safe-passing-of-structured-data}
* @param {Object} packetClone packet clone
* @returns {Object} new packet object with data from packet clone
*/
fromStructuredClone: function(packetClone) {
var tagName = enums.read(enums.packet, packetClone.tag);
var packet = this.newPacketFromTag(tagName);
for (var attr in packetClone) {
if (packetClone.hasOwnProperty(attr)) {
packet[attr] = packetClone[attr];
}
/**
* Allocate a new packet
* @param {String} tag property name from {@link module:enums.packet}
* @returns {Object} new packet object with type based on tag
*/
export function newPacketFromTag(tag) {
return new this[packetClassFromTagName(tag)]();
}
/**
* Allocate a new packet from structured packet clone
* See {@link http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#safe-passing-of-structured-data}
* @param {Object} packetClone packet clone
* @returns {Object} new packet object with data from packet clone
*/
export function fromStructuredClone(packetClone) {
var tagName = enums.read(enums.packet, packetClone.tag);
var packet = newPacketFromTag(tagName);
for (var attr in packetClone) {
if (packetClone.hasOwnProperty(attr)) {
packet[attr] = packetClone[attr];
}
if (packet.postCloneTypeFix) {
packet.postCloneTypeFix();
}
return packet;
}
};
if (packet.postCloneTypeFix) {
packet.postCloneTypeFix();
}
return packet;
}
/**
* Convert tag name to class name

View File

@ -31,18 +31,16 @@
'use strict';
module.exports = Compressed;
var enums = require('../enums.js'),
util = require('../util.js'),
Zlib = require('../compression/zlib.min.js'),
RawInflate = require('../compression/rawinflate.min.js'),
RawDeflate = require('../compression/rawdeflate.min.js');
import enums from '../enums.js';
import util from '../util.js';
import Zlib from '../compression/zlib.min.js';
import RawInflate from '../compression/rawinflate.min.js';
import RawDeflate from '../compression/rawdeflate.min.js';
/**
* @constructor
*/
function Compressed() {
export default function Compressed() {
/**
* Packet type
* @type {module:enums.packet}

View File

@ -1,15 +1,18 @@
'use strict';
module.exports = {
import * as packets from './all_packets.js';
import List from './packetlist.js';
const mod = {
/**
* @name module:packet.List
* @see module:packet/packetlist
*/
List: require('./packetlist.js')
List: List
};
var packets = require('./all_packets.js');
for (var i in packets) {
module.exports[i] = packets[i];
for (let i in packets) {
mod[i] = packets[i];
}
export default mod;

View File

@ -27,15 +27,13 @@
'use strict';
module.exports = Literal;
var util = require('../util.js'),
enums = require('../enums.js');
import util from '../util.js';
import enums from '../enums.js';
/**
* @constructor
*/
function Literal() {
export default function Literal() {
this.tag = enums.packet.literal;
this.format = 'utf8'; // default format for literal data packets
this.date = new Date();

View File

@ -31,14 +31,12 @@
'use strict';
module.exports = Marker;
var enums = require('../enums.js');
import enums from '../enums.js';
/**
* @constructor
*/
function Marker() {
export default function Marker() {
this.tag = enums.packet.marker;
}

View File

@ -31,16 +31,14 @@
'use strict';
module.exports = OnePassSignature;
var util = require('../util.js'),
enums = require('../enums.js'),
type_keyid = require('../type/keyid.js');
import util from '../util.js';
import enums from '../enums.js';
import type_keyid from '../type/keyid.js';
/**
* @constructor
*/
function OnePassSignature() {
export default function OnePassSignature() {
this.tag = enums.packet.onePassSignature; // The packet type
this.version = null; // A one-octet version number. The current version is 3.
this.type = null; // A one-octet signature type. Signature types are described in {@link http://tools.ietf.org/html/rfc4880#section-5.2.1|RFC4880 Section 5.2.1}.

View File

@ -23,9 +23,9 @@
'use strict';
var util = require('../util.js');
import util from '../util.js';
module.exports = {
export default {
readSimpleLength: function(bytes) {
var len = 0,
offset,

View File

@ -11,17 +11,15 @@
'use strict';
module.exports = Packetlist;
var util = require('../util'),
packetParser = require('./packet.js'),
packets = require('./all_packets.js'),
enums = require('../enums.js');
import util from '../util';
import packetParser from './packet.js';
import * as packets from './all_packets.js';
import enums from '../enums.js';
/**
* @constructor
*/
function Packetlist() {
export default function Packetlist() {
/** The number of packets contained within the list.
* @readonly
* @type {Integer} */
@ -193,7 +191,7 @@ Packetlist.prototype.concat = function (packetlist) {
* @param {Object} packetClone packetlist clone
* @returns {Object} new packetlist object with data from packetlist clone
*/
module.exports.fromStructuredClone = function(packetlistClone) {
Packetlist.fromStructuredClone = function(packetlistClone) {
var packetlist = new Packetlist();
for (var i = 0; i < packetlistClone.length; i++) {
packetlist.push(packets.fromStructuredClone(packetlistClone[i]));

View File

@ -32,18 +32,16 @@
'use strict';
module.exports = PublicKey;
var util = require('../util.js'),
type_mpi = require('../type/mpi.js'),
type_keyid = require('../type/keyid.js'),
enums = require('../enums.js'),
crypto = require('../crypto');
import util from '../util.js';
import type_mpi from '../type/mpi.js';
import type_keyid from '../type/keyid.js';
import enums from '../enums.js';
import crypto from '../crypto';
/**
* @constructor
*/
function PublicKey() {
export default function PublicKey() {
this.tag = enums.packet.publicKey;
this.version = 4;
/** Key creation date.

View File

@ -39,18 +39,16 @@
'use strict';
module.exports = PublicKeyEncryptedSessionKey;
var type_keyid = require('../type/keyid.js'),
util = require('../util.js'),
type_mpi = require('../type/mpi.js'),
enums = require('../enums.js'),
crypto = require('../crypto');
import type_keyid from '../type/keyid.js';
import util from '../util.js';
import type_mpi from '../type/mpi.js';
import enums from '../enums.js';
import crypto from '../crypto';
/**
* @constructor
*/
function PublicKeyEncryptedSessionKey() {
export default function PublicKeyEncryptedSessionKey() {
this.tag = enums.packet.publicKeyEncryptedSessionKey;
this.version = 3;

View File

@ -23,16 +23,14 @@
'use strict';
module.exports = PublicSubkey;
var publicKey = require('./public_key.js'),
enums = require('../enums.js');
import publicKey from './public_key.js';
import enums from '../enums.js';
/**
* @constructor
* @extends module:packet/public_key
*/
function PublicSubkey() {
export default function PublicSubkey() {
publicKey.call(this);
this.tag = enums.packet.publicSubkey;
}

View File

@ -33,20 +33,18 @@
'use strict';
module.exports = SecretKey;
var publicKey = require('./public_key.js'),
enums = require('../enums.js'),
util = require('../util.js'),
crypto = require('../crypto'),
type_mpi = require('../type/mpi.js'),
type_s2k = require('../type/s2k.js');
import publicKey from './public_key.js';
import enums from '../enums.js';
import util from '../util.js';
import crypto from '../crypto';
import type_mpi from '../type/mpi.js';
import type_s2k from '../type/s2k.js';
/**
* @constructor
* @extends module:packet/public_key
*/
function SecretKey() {
export default function SecretKey() {
publicKey.call(this);
this.tag = enums.packet.secretKey;
// encrypted secret-key data

View File

@ -23,16 +23,14 @@
'use strict';
module.exports = SecretSubkey;
var secretKey = require('./secret_key.js'),
enums = require('../enums.js');
import secretKey from './secret_key.js';
import enums from '../enums.js';
/**
* @constructor
* @extends module:packet/secret_key
*/
function SecretSubkey() {
export default function SecretSubkey() {
secretKey.call(this);
this.tag = enums.packet.secretSubkey;
}

View File

@ -31,19 +31,17 @@
* @module packet/signature
*/
module.exports = Signature;
var util = require('../util.js'),
packet = require('./packet.js'),
enums = require('../enums.js'),
crypto = require('../crypto'),
type_mpi = require('../type/mpi.js'),
type_keyid = require('../type/keyid.js');
import util from '../util.js';
import packet from './packet.js';
import enums from '../enums.js';
import crypto from '../crypto';
import type_mpi from '../type/mpi.js';
import type_keyid from '../type/keyid.js';
/**
* @constructor
*/
function Signature() {
export default function Signature() {
this.tag = enums.packet.signature;
this.version = 4;
this.signatureType = null;

View File

@ -34,19 +34,17 @@
'use strict';
module.exports = SymEncryptedIntegrityProtected;
var util = require('../util.js'),
crypto = require('../crypto'),
enums = require('../enums.js'),
asmCrypto = require('asmcrypto-lite'),
nodeCrypto = util.getNodeCrypto(),
Buffer = util.getNodeBuffer();
import util from '../util.js';
import crypto from '../crypto';
import enums from '../enums.js';
import asmCrypto from 'asmcrypto-lite';
const nodeCrypto = util.getNodeCrypto();
const Buffer = util.getNodeBuffer();
/**
* @constructor
*/
function SymEncryptedIntegrityProtected() {
export default function SymEncryptedIntegrityProtected() {
this.tag = enums.packet.symEncryptedIntegrityProtected;
/** The encrypted payload. */
this.encrypted = null; // string

View File

@ -38,17 +38,15 @@
'use strict';
var util = require('../util.js'),
type_s2k = require('../type/s2k.js'),
enums = require('../enums.js'),
crypto = require('../crypto');
module.exports = SymEncryptedSessionKey;
import util from '../util.js';
import type_s2k from '../type/s2k.js';
import enums from '../enums.js';
import crypto from '../crypto';
/**
* @constructor
*/
function SymEncryptedSessionKey() {
export default function SymEncryptedSessionKey() {
this.tag = enums.packet.symEncryptedSessionKey;
this.version = 4;
this.sessionKey = null;

View File

@ -30,16 +30,14 @@
'use strict';
module.exports = SymmetricallyEncrypted;
var crypto = require('../crypto'),
enums = require('../enums.js'),
config = require('../config');
import crypto from '../crypto';
import enums from '../enums.js';
import config from '../config';
/**
* @constructor
*/
function SymmetricallyEncrypted() {
export default function SymmetricallyEncrypted() {
this.tag = enums.packet.symmetricallyEncrypted;
this.encrypted = null;
/** Decrypted packets contained within.

View File

@ -5,14 +5,12 @@
'use strict';
module.exports = Trust;
var enums = require('../enums.js');
import enums from '../enums.js';
/**
* @constructor
*/
function Trust() {
export default function Trust() {
this.tag = enums.packet.trust;
}

View File

@ -38,16 +38,14 @@
'use strict';
var util = require('../util.js'),
packet = require('./packet.js'),
enums = require('../enums.js');
module.exports = UserAttribute;
import util from '../util.js';
import packet from './packet.js';
import enums from '../enums.js';
/**
* @constructor
*/
function UserAttribute() {
export default function UserAttribute() {
this.tag = enums.packet.userAttribute;
this.attributes = [];
}

View File

@ -30,15 +30,13 @@
'use strict';
module.exports = Userid;
var util = require('../util.js'),
enums = require('../enums.js');
import util from '../util.js';
import enums from '../enums.js';
/**
* @constructor
*/
function Userid() {
export default function Userid() {
this.tag = enums.packet.userid;
/** A string containing the user id. Usually in the form
* John Doe <john@example.com>