From 70ac6620737efac109550ee43fb104a21173e558 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 5 Feb 2016 14:44:22 +0700 Subject: [PATCH] Refactor src/packet/*.js to use import & export --- src/packet/all_packets.js | 122 +++++++++--------- src/packet/compressed.js | 14 +- src/packet/index.js | 15 ++- src/packet/literal.js | 8 +- src/packet/marker.js | 6 +- src/packet/one_pass_signature.js | 10 +- src/packet/packet.js | 4 +- src/packet/packetlist.js | 14 +- src/packet/public_key.js | 14 +- .../public_key_encrypted_session_key.js | 14 +- src/packet/public_subkey.js | 8 +- src/packet/secret_key.js | 16 +-- src/packet/secret_subkey.js | 8 +- src/packet/signature.js | 16 +-- .../sym_encrypted_integrity_protected.js | 16 +-- src/packet/sym_encrypted_session_key.js | 12 +- src/packet/symmetrically_encrypted.js | 10 +- src/packet/trust.js | 6 +- src/packet/user_attribute.js | 10 +- src/packet/userid.js | 8 +- 20 files changed, 148 insertions(+), 183 deletions(-) diff --git a/src/packet/all_packets.js b/src/packet/all_packets.js index 9df86d1e..5f5b5663 100644 --- a/src/packet/all_packets.js +++ b/src/packet/all_packets.js @@ -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 diff --git a/src/packet/compressed.js b/src/packet/compressed.js index f9bc86d5..e6db0134 100644 --- a/src/packet/compressed.js +++ b/src/packet/compressed.js @@ -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} diff --git a/src/packet/index.js b/src/packet/index.js index cd9e6578..552716ff 100644 --- a/src/packet/index.js +++ b/src/packet/index.js @@ -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; diff --git a/src/packet/literal.js b/src/packet/literal.js index 75f3ba71..e3d0a025 100644 --- a/src/packet/literal.js +++ b/src/packet/literal.js @@ -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(); diff --git a/src/packet/marker.js b/src/packet/marker.js index 089c1939..f6ebd985 100644 --- a/src/packet/marker.js +++ b/src/packet/marker.js @@ -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; } diff --git a/src/packet/one_pass_signature.js b/src/packet/one_pass_signature.js index 0d0a3fdd..4da4c63e 100644 --- a/src/packet/one_pass_signature.js +++ b/src/packet/one_pass_signature.js @@ -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}. diff --git a/src/packet/packet.js b/src/packet/packet.js index 8629449c..92030f90 100644 --- a/src/packet/packet.js +++ b/src/packet/packet.js @@ -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, diff --git a/src/packet/packetlist.js b/src/packet/packetlist.js index 9d810cbe..421c7309 100644 --- a/src/packet/packetlist.js +++ b/src/packet/packetlist.js @@ -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])); diff --git a/src/packet/public_key.js b/src/packet/public_key.js index e57c1d63..6542998b 100644 --- a/src/packet/public_key.js +++ b/src/packet/public_key.js @@ -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. diff --git a/src/packet/public_key_encrypted_session_key.js b/src/packet/public_key_encrypted_session_key.js index f3e174c0..fcdd71d0 100644 --- a/src/packet/public_key_encrypted_session_key.js +++ b/src/packet/public_key_encrypted_session_key.js @@ -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; diff --git a/src/packet/public_subkey.js b/src/packet/public_subkey.js index a7456c84..45b8523c 100644 --- a/src/packet/public_subkey.js +++ b/src/packet/public_subkey.js @@ -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; } diff --git a/src/packet/secret_key.js b/src/packet/secret_key.js index a800d927..b7d5bdf3 100644 --- a/src/packet/secret_key.js +++ b/src/packet/secret_key.js @@ -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 diff --git a/src/packet/secret_subkey.js b/src/packet/secret_subkey.js index e07db468..45cc2563 100644 --- a/src/packet/secret_subkey.js +++ b/src/packet/secret_subkey.js @@ -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; } diff --git a/src/packet/signature.js b/src/packet/signature.js index 2def8b7c..78e545c3 100644 --- a/src/packet/signature.js +++ b/src/packet/signature.js @@ -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; diff --git a/src/packet/sym_encrypted_integrity_protected.js b/src/packet/sym_encrypted_integrity_protected.js index 89da1e4d..2265a33e 100644 --- a/src/packet/sym_encrypted_integrity_protected.js +++ b/src/packet/sym_encrypted_integrity_protected.js @@ -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 diff --git a/src/packet/sym_encrypted_session_key.js b/src/packet/sym_encrypted_session_key.js index 8b33a3f2..045d24bb 100644 --- a/src/packet/sym_encrypted_session_key.js +++ b/src/packet/sym_encrypted_session_key.js @@ -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; diff --git a/src/packet/symmetrically_encrypted.js b/src/packet/symmetrically_encrypted.js index e43829cf..2b919be3 100644 --- a/src/packet/symmetrically_encrypted.js +++ b/src/packet/symmetrically_encrypted.js @@ -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. diff --git a/src/packet/trust.js b/src/packet/trust.js index 30ca330c..c4617da5 100644 --- a/src/packet/trust.js +++ b/src/packet/trust.js @@ -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; } diff --git a/src/packet/user_attribute.js b/src/packet/user_attribute.js index c1c516bd..00689898 100644 --- a/src/packet/user_attribute.js +++ b/src/packet/user_attribute.js @@ -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 = []; } diff --git a/src/packet/userid.js b/src/packet/userid.js index ba0d4cf6..5c5f4580 100644 --- a/src/packet/userid.js +++ b/src/packet/userid.js @@ -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