Finish refactoring src/**/*.js to use import & export

This commit is contained in:
Tankred Hase 2016-02-05 15:19:49 +07:00
parent 70ac662073
commit 8728db2b08
18 changed files with 54 additions and 86 deletions

View File

@ -39,7 +39,7 @@ import armor from './encoding/armor.js';
* if message not yet signed * if message not yet signed
*/ */
function CleartextMessage(text, packetlist) { export function CleartextMessage(text, packetlist) {
if (!(this instanceof CleartextMessage)) { if (!(this instanceof CleartextMessage)) {
return new CleartextMessage(text, packetlist); return new CleartextMessage(text, packetlist);
} }
@ -149,7 +149,7 @@ CleartextMessage.prototype.armor = function() {
* @return {module:cleartext~CleartextMessage} new cleartext message object * @return {module:cleartext~CleartextMessage} new cleartext message object
* @static * @static
*/ */
function readArmored(armoredText) { export function readArmored(armoredText) {
var input = armor.decode(armoredText); var input = armor.decode(armoredText);
if (input.type !== enums.armor.signed) { if (input.type !== enums.armor.signed) {
throw new Error('No cleartext signed message.'); throw new Error('No cleartext signed message.');
@ -205,6 +205,3 @@ function verifyHeaders(headers, packetlist) {
throw new Error('Hash algorithm mismatch in armor header and signature'); throw new Error('Hash algorithm mismatch in armor header and signature');
} }
} }
exports.CleartextMessage = CleartextMessage;
exports.readArmored = readArmored;

View File

@ -33,7 +33,7 @@
import enums from '../enums.js'; import enums from '../enums.js';
module.exports = { export default {
prefer_hash_algorithm: enums.hash.sha256, prefer_hash_algorithm: enums.hash.sha256,
encryption_cipher: enums.symmetric.aes256, encryption_cipher: enums.symmetric.aes256,
compression: enums.compression.zip, compression: enums.compression.zip,

View File

@ -2,5 +2,7 @@
* @see module:config/config * @see module:config/config
* @module config * @module config
*/ */
import config from './config.js';
module.exports = config; 'use strict';
export { default } from './config.js';

View File

@ -5,12 +5,10 @@
'use strict'; 'use strict';
module.exports = LocalStorage;
/** /**
* @constructor * @constructor
*/ */
function LocalStorage() {} export default function LocalStorage() {}
/** /**
* Reads the config out of the HTML5 local storage * Reads the config out of the HTML5 local storage

View File

@ -14,7 +14,7 @@ import random from './random';
import pkcs1 from './pkcs1'; import pkcs1 from './pkcs1';
import crypto from './crypto.js'; import crypto from './crypto.js';
module.exports = { const mod = {
/** @see module:crypto/cipher */ /** @see module:crypto/cipher */
cipher: cipher, cipher: cipher,
/** @see module:crypto/hash */ /** @see module:crypto/hash */
@ -32,5 +32,7 @@ module.exports = {
}; };
for (var i in crypto) { for (var i in crypto) {
module.exports[i] = crypto[i]; mod[i] = crypto[i];
} }
export default mod;

View File

@ -402,7 +402,7 @@ function armor(messagetype, body, partindex, parttotal) {
return result.join(''); return result.join('');
} }
module.exports = { export default {
encode: armor, encode: armor,
decode: dearmor decode: dearmor
}; };

View File

@ -108,7 +108,7 @@ function r2s(t) {
return new Uint8Array(r); return new Uint8Array(r);
} }
module.exports = { export default {
encode: s2r, encode: s2r,
decode: r2s decode: r2s
}; };

View File

@ -4,7 +4,7 @@
* @module enums * @module enums
*/ */
module.exports = { export default {
/** A string to key specifier type /** A string to key specifier type
* @enum {Integer} * @enum {Integer}

View File

@ -38,7 +38,7 @@ import util from './util';
* @param {module:packet/packetlist} packetlist The packets that form this key * @param {module:packet/packetlist} packetlist The packets that form this key
*/ */
function Key(packetlist) { export function Key(packetlist) {
if (!(this instanceof Key)) { if (!(this instanceof Key)) {
return new Key(packetlist); return new Key(packetlist);
} }
@ -887,7 +887,7 @@ SubKey.prototype.update = function(subKey, primaryKey) {
* @return {{keys: Array<module:key~Key>, err: (Array<Error>|null)}} result object with key and error arrays * @return {{keys: Array<module:key~Key>, err: (Array<Error>|null)}} result object with key and error arrays
* @static * @static
*/ */
function readArmored(armoredText) { export function readArmored(armoredText) {
var result = {}; var result = {};
result.keys = []; result.keys = [];
try { try {
@ -931,7 +931,7 @@ function readArmored(armoredText) {
* @return {module:key~Key} * @return {module:key~Key}
* @static * @static
*/ */
function generate(options) { export function generate(options) {
var packetlist, secretKeyPacket, userIdPacket, dataToSign, signaturePacket, secretSubkeyPacket, subkeySignaturePacket; var packetlist, secretKeyPacket, userIdPacket, dataToSign, signaturePacket, secretSubkeyPacket, subkeySignaturePacket;
options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign; options.keyType = options.keyType || enums.publicKey.rsa_encrypt_sign;
@ -1042,7 +1042,7 @@ function generate(options) {
* @param {Array<module:key~Key>} keys Set of keys * @param {Array<module:key~Key>} keys Set of keys
* @return {enums.symmetric} Preferred symmetric algorithm * @return {enums.symmetric} Preferred symmetric algorithm
*/ */
function getPreferredSymAlgo(keys) { export function getPreferredSymAlgo(keys) {
var prioMap = {}; var prioMap = {};
keys.forEach(function(key) { keys.forEach(function(key) {
var primaryUser = key.getPrimaryUser(); var primaryUser = key.getPrimaryUser();
@ -1069,8 +1069,3 @@ function getPreferredSymAlgo(keys) {
} }
return prefAlgo.algo; return prefAlgo.algo;
} }
exports.Key = Key;
exports.readArmored = readArmored;
exports.generate = generate;
exports.getPreferredSymAlgo = getPreferredSymAlgo;

View File

@ -25,7 +25,7 @@
'use strict'; 'use strict';
import keyModule from '../key.js'; import * as keyModule from '../key.js';
import LocalStore from './localstore.js'; import LocalStore from './localstore.js';
/** /**

View File

@ -25,7 +25,7 @@
'use strict'; 'use strict';
import config from '../config'; import config from '../config';
import keyModule from '../key.js'; import * as keyModule from '../key.js';
import util from '../util.js'; import util from '../util.js';
export default function LocalStore(prefix) { export default function LocalStore(prefix) {

View File

@ -32,7 +32,7 @@ import enums from './enums.js';
import armor from './encoding/armor.js'; import armor from './encoding/armor.js';
import config from './config'; import config from './config';
import crypto from './crypto'; import crypto from './crypto';
import keyModule from './key.js'; import * as keyModule from './key.js';
/** /**
* @class * @class
@ -42,7 +42,7 @@ import keyModule from './key.js';
* See {@link http://tools.ietf.org/html/rfc4880#section-11.3} * See {@link http://tools.ietf.org/html/rfc4880#section-11.3}
*/ */
function Message(packetlist) { export function Message(packetlist) {
if (!(this instanceof Message)) { if (!(this instanceof Message)) {
return new Message(packetlist); return new Message(packetlist);
} }
@ -242,7 +242,7 @@ Message.prototype.encrypt = function(keys, passwords) {
* @param {(Array<String>|String)} password(s) for message encryption * @param {(Array<String>|String)} password(s) for message encryption
* @return {Array<module:message~Message>} new message with encrypted content * @return {Array<module:message~Message>} new message with encrypted content
*/ */
function encryptSessionKey(sessionKey, symAlgo, keys, passwords) { export function encryptSessionKey(sessionKey, symAlgo, keys, passwords) {
/** Convert to arrays if necessary */ /** Convert to arrays if necessary */
if(keys && !Array.prototype.isPrototypeOf(keys)) { if(keys && !Array.prototype.isPrototypeOf(keys)) {
@ -396,7 +396,7 @@ Message.prototype.armor = function() {
* @return {module:message~Message} new message object * @return {module:message~Message} new message object
* @static * @static
*/ */
function readArmored(armoredText) { export function readArmored(armoredText) {
//TODO how do we want to handle bad text? Exception throwing //TODO how do we want to handle bad text? Exception throwing
//TODO don't accept non-message armored texts //TODO don't accept non-message armored texts
var input = armor.decode(armoredText).data; var input = armor.decode(armoredText).data;
@ -409,7 +409,7 @@ function readArmored(armoredText) {
* @return {module:message~Message} new message object * @return {module:message~Message} new message object
* @static * @static
*/ */
function read(input) { export function read(input) {
var packetlist = new packet.List(); var packetlist = new packet.List();
packetlist.read(input); packetlist.read(input);
return new Message(packetlist); return new Message(packetlist);
@ -420,7 +420,7 @@ function read(input) {
* @param {String} content An 8 bit ascii string containing e.g. a MIME subtree with text nodes or attachments * @param {String} content An 8 bit ascii string containing e.g. a MIME subtree with text nodes or attachments
* @param {String} detachedSignature The detached ascii armored PGP signature * @param {String} detachedSignature The detached ascii armored PGP signature
*/ */
function readSignedContent(content, detachedSignature) { export function readSignedContent(content, detachedSignature) {
var literalDataPacket = new packet.Literal(); var literalDataPacket = new packet.Literal();
literalDataPacket.setBytes(util.str2Uint8Array(content), enums.read(enums.literal, enums.literal.binary)); literalDataPacket.setBytes(util.str2Uint8Array(content), enums.read(enums.literal, enums.literal.binary));
var packetlist = new packet.List(); var packetlist = new packet.List();
@ -437,7 +437,7 @@ function readSignedContent(content, detachedSignature) {
* @return {module:message~Message} new message object * @return {module:message~Message} new message object
* @static * @static
*/ */
function fromText(text, filename) { export function fromText(text, filename) {
var literalDataPacket = new packet.Literal(); var literalDataPacket = new packet.Literal();
// text will be converted to UTF8 // text will be converted to UTF8
literalDataPacket.setText(text); literalDataPacket.setText(text);
@ -456,7 +456,7 @@ function fromText(text, filename) {
* @return {module:message~Message} new message object * @return {module:message~Message} new message object
* @static * @static
*/ */
function fromBinary(bytes, filename) { export function fromBinary(bytes, filename) {
if(!Uint8Array.prototype.isPrototypeOf(bytes)) { if(!Uint8Array.prototype.isPrototypeOf(bytes)) {
throw new Error('Data must be in the form of a Uint8Array'); throw new Error('Data must be in the form of a Uint8Array');
} }
@ -473,11 +473,3 @@ function fromBinary(bytes, filename) {
literalDataPacketlist.push(literalDataPacket); literalDataPacketlist.push(literalDataPacket);
return new Message(literalDataPacketlist); return new Message(literalDataPacketlist);
} }
exports.Message = Message;
exports.read = read;
exports.readArmored = readArmored;
exports.readSignedContent = readSignedContent;
exports.fromText = fromText;
exports.fromBinary = fromBinary;
exports.encryptSessionKey = encryptSessionKey;

View File

@ -33,19 +33,19 @@
'use strict'; 'use strict';
import * as message from './message.js';
import * as cleartext from './cleartext.js';
import * as key from './key.js';
import armor from './encoding/armor.js'; import armor from './encoding/armor.js';
import enums from './enums.js'; import enums from './enums.js';
import message from './message.js';
import cleartext from './cleartext.js';
import config from './config/config.js'; import config from './config/config.js';
import key from './key.js';
import util from './util'; import util from './util';
import AsyncProxy from './worker/async_proxy.js'; import AsyncProxy from './worker/async_proxy.js';
import es6Promise from 'es6-promise'; import es6Promise from 'es6-promise';
es6Promise.polyfill(); // load ES6 Promises polyfill es6Promise.polyfill(); // load ES6 Promises polyfill
var asyncProxy = null; // instance of the asyncproxy let asyncProxy = null; // instance of the asyncproxy
/** /**
* Set the path for the web worker script and create an instance of the async proxy * Set the path for the web worker script and create an instance of the async proxy
@ -54,7 +54,7 @@ var asyncProxy = null; // instance of the asyncproxy
* web worker initialized with 'openpgp.worker.js' * web worker initialized with 'openpgp.worker.js'
* @return {Boolean} true if worker created successfully * @return {Boolean} true if worker created successfully
*/ */
function initWorker(path, options) { export function initWorker(path, options) {
if (options && options.worker || typeof window !== 'undefined' && window.Worker) { if (options && options.worker || typeof window !== 'undefined' && window.Worker) {
options = options || {}; options = options || {};
options.config = config; options.config = config;
@ -69,7 +69,7 @@ function initWorker(path, options) {
* Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker() * Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker()
* @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized * @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized
*/ */
function getWorker() { export function getWorker() {
return asyncProxy; return asyncProxy;
} }
@ -83,7 +83,7 @@ function getWorker() {
* @return {Promise<String> or Promise<Packetlist>} encrypted ASCII armored message, or Packetlist if params.packets is true * @return {Promise<String> or Promise<Packetlist>} encrypted ASCII armored message, or Packetlist if params.packets is true
* @static * @static
*/ */
function encryptMessage(keys, data, passwords, params) { export function encryptMessage(keys, data, passwords, params) {
if (asyncProxy) { if (asyncProxy) {
return asyncProxy.encryptMessage(keys, data, passwords, params); return asyncProxy.encryptMessage(keys, data, passwords, params);
@ -129,7 +129,7 @@ function encryptMessage(keys, data, passwords, params) {
* @return {Promise<Packetlist>} Binary string of key packets * @return {Promise<Packetlist>} Binary string of key packets
* @static * @static
*/ */
function encryptSessionKey(sessionKey, algo, keys, passwords) { export function encryptSessionKey(sessionKey, algo, keys, passwords) {
if (asyncProxy) { if (asyncProxy) {
return asyncProxy.encryptSessionKey(sessionKey, algo, keys, passwords); return asyncProxy.encryptSessionKey(sessionKey, algo, keys, passwords);
@ -151,7 +151,7 @@ function encryptSessionKey(sessionKey, algo, keys, passwords) {
* @return {Promise<String>} encrypted ASCII armored message * @return {Promise<String>} encrypted ASCII armored message
* @static * @static
*/ */
function signAndEncryptMessage(publicKeys, privateKey, text) { export function signAndEncryptMessage(publicKeys, privateKey, text) {
if (!publicKeys.length) { if (!publicKeys.length) {
publicKeys = [publicKeys]; publicKeys = [publicKeys];
} }
@ -181,7 +181,7 @@ function signAndEncryptMessage(publicKeys, privateKey, text) {
* or null if no literal data found * or null if no literal data found
* @static * @static
*/ */
function decryptMessage(privateKey, msg, params) { export function decryptMessage(privateKey, msg, params) {
if (asyncProxy) { if (asyncProxy) {
return asyncProxy.decryptMessage(privateKey, msg, params); return asyncProxy.decryptMessage(privateKey, msg, params);
} }
@ -216,7 +216,7 @@ function decryptMessage(privateKey, msg, params) {
* or null if no key packets found * or null if no key packets found
* @static * @static
*/ */
function decryptSessionKey(privateKey, msg) { export function decryptSessionKey(privateKey, msg) {
if (asyncProxy) { if (asyncProxy) {
return asyncProxy.decryptSessionKey(privateKey, msg); return asyncProxy.decryptSessionKey(privateKey, msg);
} }
@ -238,7 +238,7 @@ function decryptSessionKey(privateKey, msg) {
* with verified signatures or null if no literal data found * with verified signatures or null if no literal data found
* @static * @static
*/ */
function decryptAndVerifyMessage(privateKey, publicKeys, msg) { export function decryptAndVerifyMessage(privateKey, publicKeys, msg) {
if (!publicKeys.length) { if (!publicKeys.length) {
publicKeys = [publicKeys]; publicKeys = [publicKeys];
} }
@ -267,7 +267,7 @@ function decryptAndVerifyMessage(privateKey, publicKeys, msg) {
* @return {Promise<String>} ASCII armored message * @return {Promise<String>} ASCII armored message
* @static * @static
*/ */
function signClearMessage(privateKeys, text) { export function signClearMessage(privateKeys, text) {
if (!privateKeys.length) { if (!privateKeys.length) {
privateKeys = [privateKeys]; privateKeys = [privateKeys];
} }
@ -292,7 +292,7 @@ function signClearMessage(privateKeys, text) {
* cleartext with status of verified signatures * cleartext with status of verified signatures
* @static * @static
*/ */
function verifyClearSignedMessage(publicKeys, msg) { export function verifyClearSignedMessage(publicKeys, msg) {
if (!publicKeys.length) { if (!publicKeys.length) {
publicKeys = [publicKeys]; publicKeys = [publicKeys];
} }
@ -325,7 +325,7 @@ function verifyClearSignedMessage(publicKeys, msg) {
* @return {Promise<Object>} {key: module:key~Key, privateKeyArmored: String, publicKeyArmored: String} * @return {Promise<Object>} {key: module:key~Key, privateKeyArmored: String, publicKeyArmored: String}
* @static * @static
*/ */
function generateKeyPair(options) { export function generateKeyPair(options) {
// use web worker if web crypto apis are not supported // use web worker if web crypto apis are not supported
if (!util.getWebCrypto() && asyncProxy) { if (!util.getWebCrypto() && asyncProxy) {
return asyncProxy.generateKeyPair(options); return asyncProxy.generateKeyPair(options);
@ -389,15 +389,3 @@ function onError(message, error) {
// rethrow new high level error for api users // rethrow new high level error for api users
throw new Error(message); throw new Error(message);
} }
exports.initWorker = initWorker;
exports.getWorker = getWorker;
exports.encryptMessage = encryptMessage;
exports.encryptSessionKey = encryptSessionKey;
exports.signAndEncryptMessage = signAndEncryptMessage;
exports.decryptMessage = decryptMessage;
exports.decryptSessionKey = decryptSessionKey;
exports.decryptAndVerifyMessage = decryptAndVerifyMessage;
exports.signClearMessage = signClearMessage;
exports.verifyClearSignedMessage = verifyClearSignedMessage;
exports.generateKeyPair = generateKeyPair;

View File

@ -30,12 +30,10 @@
import util from '../util.js'; import util from '../util.js';
module.exports = Keyid;
/** /**
* @constructor * @constructor
*/ */
function Keyid() { export default function Keyid() {
this.bytes = ''; this.bytes = '';
} }
@ -63,17 +61,17 @@ Keyid.prototype.isNull = function() {
return this.bytes === ''; return this.bytes === '';
}; };
module.exports.mapToHex = function (keyId) { Keyid.mapToHex = function (keyId) {
return keyId.toHex(); return keyId.toHex();
}; };
module.exports.fromClone = function (clone) { Keyid.fromClone = function (clone) {
var keyid = new Keyid(); var keyid = new Keyid();
keyid.bytes = clone.bytes; keyid.bytes = clone.bytes;
return keyid; return keyid;
}; };
module.exports.fromId = function (hex) { Keyid.fromId = function (hex) {
var keyid = new Keyid(); var keyid = new Keyid();
keyid.read(util.hex2bin(hex)); keyid.read(util.hex2bin(hex));
return keyid; return keyid;

View File

@ -39,12 +39,10 @@
import BigInteger from '../crypto/public_key/jsbn.js'; import BigInteger from '../crypto/public_key/jsbn.js';
import util from '../util.js'; import util from '../util.js';
module.exports = MPI;
/** /**
* @constructor * @constructor
*/ */
function MPI() { export default function MPI() {
/** An implementation dependent integer */ /** An implementation dependent integer */
this.data = null; this.data = null;
} }
@ -109,7 +107,7 @@ MPI.prototype.fromBigInteger = function (bn) {
this.data = bn.clone(); this.data = bn.clone();
}; };
module.exports.fromClone = function (clone) { MPI.fromClone = function (clone) {
clone.data.copyTo = BigInteger.prototype.copyTo; clone.data.copyTo = BigInteger.prototype.copyTo;
var bn = new BigInteger(); var bn = new BigInteger();
clone.data.copyTo(bn); clone.data.copyTo(bn);

View File

@ -35,12 +35,10 @@ import enums from '../enums.js';
import util from '../util.js'; import util from '../util.js';
import crypto from '../crypto'; import crypto from '../crypto';
module.exports = S2K;
/** /**
* @constructor * @constructor
*/ */
function S2K() { export default function S2K() {
/** @type {module:enums.hash} */ /** @type {module:enums.hash} */
this.algorithm = 'sha256'; this.algorithm = 'sha256';
/** @type {module:enums.s2k} */ /** @type {module:enums.s2k} */
@ -201,7 +199,7 @@ S2K.prototype.produce_key = function (passphrase, numBytes) {
return util.concatUint8Array(arr).subarray(0, numBytes); return util.concatUint8Array(arr).subarray(0, numBytes);
}; };
module.exports.fromClone = function (clone) { S2K.fromClone = function (clone) {
var s2k = new S2K(); var s2k = new S2K();
s2k.algorithm = clone.algorithm; s2k.algorithm = clone.algorithm;
s2k.type = clone.type; s2k.type = clone.type;

View File

@ -25,7 +25,7 @@
import config from './config'; import config from './config';
module.exports = { export default {
readNumber: function (bytes) { readNumber: function (bytes) {
var n = 0; var n = 0;

View File

@ -28,7 +28,7 @@
import crypto from '../crypto'; import crypto from '../crypto';
import packet from '../packet'; import packet from '../packet';
import key from '../key.js'; import * as key from '../key.js';
import type_keyid from '../type/keyid.js'; import type_keyid from '../type/keyid.js';
var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker var INITIAL_RANDOM_SEED = 50000, // random bytes seeded to worker