All files / pub-keystore/in-mem index.js

94.68% Statements 178/188
77.46% Branches 55/71
65.38% Functions 17/26
94.68% Lines 178/188

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 1891x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 1x 1x 1x 1x 563x 563x 556x 563x 7x 7x 563x 563x 140x 224x 140x 140x 140x 563x 136x 104x 104x 563x 563x 563x 563x 563x 563x 1x 1x 579x 579x 16x 16x 579x 140x 140x 563x 563x 563x 579x 1x 1x 945x 945x 8x 8x 937x 945x 656x 656x 281x 945x 1x 1x 1034x 1034x 9x 9x 1025x 1034x 744x 744x 281x 1034x 1x 1x 856x 856x 8x 8x 848x 856x 568x 568x 280x 856x 1x 1x 64x 64x     64x 64x 1x 1x 193x 192x 192x 192x 193x 193x 193x     193x 8x 8x 185x 185x 185x 193x 138x 1x 1x 1x 1x 1x 137x 137x 137x 137x 137x 137x 137x 137x 47x 47x 47x 47x 47x 1x 1x 1x 97x 97x     97x 8x 8x 89x 97x 45x 45x 45x 45x 89x 97x 1x 1x 301x 301x     301x 301x 1x 1x 16x 16x 4x 4x 12x 12x 12x 16x 1x 1x 104x 104x 104x     104x 104x 104x 1x 1x 36x 36x 36x 1x 1x 1x 563x 1x  
/*jslint node: true */
 
const { EventEmitter } = require('events');
const { randomBytes } = require('crypto');
 
class KeyState extends EventEmitter {
    constructor() {
        super();
        this.keys_by_uri = new Map()
        this.keys_by_issuer_id = new Map();
    }
}
 
class PubKeyStoreMemory extends EventEmitter {
    constructor(options, cb) {
        super();
        if (options.share_keys_with) {
            this._state = options.share_keys_with._state;
        } else {
            this._state = new KeyState();
        }
        this._changes = !options.no_changes;
        if (this._changes) {
            this._change_listener = (uri, rev, deleted) => {
                this.emit('change', uri, rev, deleted);
            };
            this._state.on('change', this._change_listener);
        }
        this._deploy_listener = store => {
            if (store !== this) {
                this.replicate();
            }
        };
        this._state.on('deploy', this._deploy_listener);
        this._no_updates = options.no_updates;
        this._open = true;
        cb(null, this);
    }
 
    close(cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        if (this._changes) {
            this._state.removeListener('change', this._change_listener);
        }
        this._state.removeListener('deploy', this._deploy_listener);
        this._open = false;
        cb();
    }
 
    get_pub_key_by_uri(uri, cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        const entry = this._state.keys_by_uri.get(uri);
        if (!entry) {
            return cb(null, null);
        }
        cb(null, entry.pub_key, entry.issuer_id, entry.rev);
    }
 
    get_pub_key_by_issuer_id(issuer_id, cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        const entry = this._state.keys_by_issuer_id.get(issuer_id);
        if (!entry) {
            return cb(null, null);
        }
        cb(null, entry.pub_key, entry.uri, entry.rev);
    }
 
    get_issuer_id(uri, cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        const entry = this._state.keys_by_uri.get(uri);
        if (!entry) {
            return cb(null, null);
        }
        cb(null, entry.issuer_id, entry.rev);
    }
 
    get_uris(cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        cb(null, Array.from(this._state.keys_by_uri.keys()));
    }
 
    add_pub_key(uri, pub_key, options, cb) {
        if (typeof options == 'function') {
            cb = options;
            options = {};
        }
        options = options || {};
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        if ((uri === null) || (uri === undefined)) {
            return cb(new Error('invalid_uri'));
        }
        const issuer_id = randomBytes(64).toString('hex');
        const rev = randomBytes(64).toString('hex');
        const entry = this._state.keys_by_uri.get(uri);
        if (entry) {
            if (this._no_updates && !options.allow_update) {
                const err = new Error('already exists');
                err.statusCode = 409;
                err.error = 'conflict';
                return cb(err);
            }
            this._state.keys_by_issuer_id.delete(entry.issuer_id);
            entry.issuer_id = issuer_id;
            entry.pub_key = pub_key;
            entry.rev = rev;
            this._state.keys_by_issuer_id.set(issuer_id, entry);
            this._state.emit('change', uri, rev, false);
            return cb(null, issuer_id, entry.rev);
        }
        const new_entry = { uri, issuer_id, pub_key, rev };
        this._state.keys_by_uri.set(uri, new_entry);
        this._state.keys_by_issuer_id.set(issuer_id, new_entry);
        this._state.emit('change', uri, rev, false);
        cb(null, issuer_id, rev);
    }            
 
    remove_pub_key(uri, cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        if ((uri === null) || (uri === undefined)) {
            return cb(new Error('invalid_uri'));
        }
        const entry = this._state.keys_by_uri.get(uri);
        if (entry) {
            this._state.keys_by_uri.delete(uri);
            this._state.keys_by_issuer_id.delete(entry.issuer_id);
            this._state.emit('change', uri, randomBytes(64).toString('hex'), true);
        }
        cb();
    }
 
    create(cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        cb();
    }
 
    destroy(cb) {
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        this._state.keys_by_uri.clear();
        this._state.keys_by_issuer_id.clear();
        this.close(cb);
    }
 
    replicate(opts, cb) {
        (typeof opts !== 'function') || (cb = opts);
        cb = cb || (() => {});
        if (!this._open) {
            return cb(new Error('not_open'));
        }
        this.emit('replicated', cb => cb());
        cb();
    }
 
    deploy(cb) {
        this._state.emit('deploy', this);
        !cb || cb();
    }
}
 
module.exports = function (config, cb) {
    new PubKeyStoreMemory(config, cb);
};