index.js
function makeModuleKeys(moduleIdentifier) {
  // Allocate a public/private key pair.
  function privateKey(fun) {
    const previous = hidden;

    hidden = privateKey;
    try {
      return fun();
    } finally {
      hidden = previous;
    }
  }
  function publicKey() {
    return hidden === privateKey;
  }
  publicKeys.add(publicKey);

  // Allow a private key to be used in lieu of the pair.
  defineProperty(
    privateKey, 'publicKey', { value: publicKey, enumerable: true });

  // We attach a module identifier to the public key to enable
  // whitelisting based on strings in a configuration without having
  // to load modules before storing their public key in a set.
  defineProperties(
    publicKey,
    {
      moduleIdentifier: { value: `${ moduleIdentifier }`, enumerable: true },
      call: { value: fCall, enumerable: true },
      apply: { value: fApply, enumerable: true },
    });

  /**
   * Wraps a value in a box so that only an approved
   * opener may unbox it.
   *
   * @param {*} value the value that will be given to
   *    an approved unboxer.
   * @param {!function(function():boolean):boolean} mayOpen
   *    receives the public key of the opener.
   *    Should return `true` to allow.
   *    This will be called in the context of the opener's
   *    private key, so the public key should also return true
   *    called with no arguments.
   * @return {!Box} a box that is opaque to any receivers that cannot
   *    unbox it.
   */
  function box(value, mayOpen) {
    if (typeof mayOpen !== 'function') {
      throw new TypeError(`Expected function not ${ mayOpen }`);
    }
    // Allocate an opaque token
    const newBox = new Box();
    boxes.set(
      newBox,
      freeze({ boxerPriv: privateKey, boxerPub: publicKey, value, mayOpen }));
    return newBox;
  }

  /**
   * Tries to open a box.
   *
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @param {*} fallback a value to substitute if unboxing failed.
   *    Defaults to undefined.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */
  function unbox(box, ifFrom, fallback) { // eslint-disable-line no-shadow
    if (ifFrom == null) { // eslint-disable-line
      ifFrom = () => true;
    }
    if (typeof ifFrom !== 'function') {
      throw new TypeError(`Expected function not ${ ifFrom }`);
    }
    const boxData = boxes.get(box);
    if (!boxData) {
      return fallback;
    }
    const { boxerPriv, boxerPub, value, mayOpen } = boxData;
    // Require mutual consent
    // TODO: Is this the object identity equivalent of an
    // out-of-order verify/decrypt fault?
    // http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html
    return (privateKey(() => mayOpen(publicKey)) === true &&
            boxerPriv(() => ifFrom(boxerPub)) === true) ?
      value :
      fallback;
  }

  const neverBoxed = {};
  /**
   * Like unbox but raises an exception if unboxing fails.
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */
  function unboxStrict(box, ifFrom) { // eslint-disable-line no-shadow
    const result = unbox(box, ifFrom, neverBoxed);
    if (result === neverBoxed) {
      throw new Error('Could not unbox');
    }
    return result;
  }

  return defineProperties(
    create(null),
    {
      // These close over private keys, so do not leak them.
      box: { value: box, enumerable: true },
      unbox: { value: unbox, enumerable: true },
      unboxStrict: { value: unboxStrict, enumerable: true },
      privateKey: { value: privateKey, enumerable: true },
      isPublicKey: { value: isPublicKey, enumerable: true },

      // Modules may allow access to this, perhaps via module object.
      // See cjs/index.js.
      publicKey: { value: publicKey, enumerable: true },
      [publicKeySymbol]: { value: publicKey, enumerable: true },
    });
}
function makeModuleKeys(moduleIdentifier) {
  // Allocate a public/private key pair.
  function privateKey(fun) {
    const previous = hidden;

    hidden = privateKey;
    try {
      return fun();
    } finally {
      hidden = previous;
    }
  }
  function publicKey() {
    return hidden === privateKey;
  }
  publicKeys.add(publicKey);

  // Allow a private key to be used in lieu of the pair.
  defineProperty(
    privateKey, 'publicKey', { value: publicKey, enumerable: true });

  // We attach a module identifier to the public key to enable
  // whitelisting based on strings in a configuration without having
  // to load modules before storing their public key in a set.
  defineProperties(
    publicKey,
    {
      moduleIdentifier: { value: `${ moduleIdentifier }`, enumerable: true },
      call: { value: fCall, enumerable: true },
      apply: { value: fApply, enumerable: true },
    });

  /**
   * Wraps a value in a box so that only an approved
   * opener may unbox it.
   *
   * @param {*} value the value that will be given to
   *    an approved unboxer.
   * @param {!function(function():boolean):boolean} mayOpen
   *    receives the public key of the opener.
   *    Should return `true` to allow.
   *    This will be called in the context of the opener's
   *    private key, so the public key should also return true
   *    called with no arguments.
   * @return {!Box} a box that is opaque to any receivers that cannot
   *    unbox it.
   */
  function box(value, mayOpen) {
    if (typeof mayOpen !== 'function') {
      throw new TypeError(`Expected function not ${ mayOpen }`);
    }
    // Allocate an opaque token
    const newBox = new Box();
    boxes.set(
      newBox,
      freeze({ boxerPriv: privateKey, boxerPub: publicKey, value, mayOpen }));
    return newBox;
  }

  /**
   * Tries to open a box.
   *
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @param {*} fallback a value to substitute if unboxing failed.
   *    Defaults to undefined.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */
  function unbox(box, ifFrom, fallback) { // eslint-disable-line no-shadow
    if (ifFrom == null) { // eslint-disable-line
      ifFrom = () => true;
    }
    if (typeof ifFrom !== 'function') {
      throw new TypeError(`Expected function not ${ ifFrom }`);
    }
    const boxData = boxes.get(box);
    if (!boxData) {
      return fallback;
    }
    const { boxerPriv, boxerPub, value, mayOpen } = boxData;
    // Require mutual consent
    // TODO: Is this the object identity equivalent of an
    // out-of-order verify/decrypt fault?
    // http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html
    return (privateKey(() => mayOpen(publicKey)) === true &&
            boxerPriv(() => ifFrom(boxerPub)) === true) ?
      value :
      fallback;
  }

  const neverBoxed = {};
  /**
   * Like unbox but raises an exception if unboxing fails.
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */
  function unboxStrict(box, ifFrom) { // eslint-disable-line no-shadow
    const result = unbox(box, ifFrom, neverBoxed);
    if (result === neverBoxed) {
      throw new Error('Could not unbox');
    }
    return result;
  }

  return defineProperties(
    create(null),
    {
      // These close over private keys, so do not leak them.
      box: { value: box, enumerable: true },
      unbox: { value: unbox, enumerable: true },
      unboxStrict: { value: unboxStrict, enumerable: true },
      privateKey: { value: privateKey, enumerable: true },
      isPublicKey: { value: isPublicKey, enumerable: true },

      // Modules may allow access to this, perhaps via module object.
      // See cjs/index.js.
      publicKey: { value: publicKey, enumerable: true },
      [publicKeySymbol]: { value: publicKey, enumerable: true },
    });
}
function makeModuleKeys ( moduleIdentifier ) { // Allocate a public/private key pair. function privateKey ( fun ) { const previous = hidden ; hidden = privateKey ; try { return fun ( ) ; } finally { hidden = previous ; } } function publicKey ( ) { return hidden === privateKey ; } publicKeys . add ( publicKey ) ; // Allow a private key to be used in lieu of the pair. defineProperty ( privateKey , 'publicKey' , { value : publicKey , enumerable : true } ) ; // We attach a module identifier to the public key to enable // whitelisting based on strings in a configuration without having // to load modules before storing their public key in a set. defineProperties ( publicKey , { moduleIdentifier : { value : ` ${ moduleIdentifier } ` , enumerable : true } , call : { value : fCall , enumerable : true } , apply : { value : fApply , enumerable : true } , } ) ; /**
   * Wraps a value in a box so that only an approved
   * opener may unbox it.
   *
   * @param {*} value the value that will be given to
   *    an approved unboxer.
   * @param {!function(function():boolean):boolean} mayOpen
   *    receives the public key of the opener.
   *    Should return `true` to allow.
   *    This will be called in the context of the opener's
   *    private key, so the public key should also return true
   *    called with no arguments.
   * @return {!Box} a box that is opaque to any receivers that cannot
   *    unbox it.
   */ function box ( value , mayOpen ) { if ( typeof mayOpen !== 'function' ) { throw new TypeError ( ` ${ mayOpen } ` ) ; } // Allocate an opaque token const newBox = new Box ( ) ; boxes . set ( newBox , freeze ( { boxerPriv : privateKey , boxerPub : publicKey , value , mayOpen } ) ) ; return newBox ; } /**
   * Tries to open a box.
   *
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @param {*} fallback a value to substitute if unboxing failed.
   *    Defaults to undefined.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */ function unbox ( box , ifFrom , fallback ) { // eslint-disable-line no-shadow if ( ifFrom == null ) { // eslint-disable-line ifFrom = ( ) => true ; } if ( typeof ifFrom !== 'function' ) { throw new TypeError ( ` ${ ifFrom } ` ) ; } const boxData = boxes . get ( box ) ; if ( ! boxData ) { return fallback ; } const { boxerPriv , boxerPub , value , mayOpen } = boxData ; // Require mutual consent // TODO: Is this the object identity equivalent of an // out-of-order verify/decrypt fault? // http://world.std.com/~dtd/sign_encrypt/sign_encrypt7.html return ( privateKey ( ( ) => mayOpen ( publicKey ) ) === true && boxerPriv ( ( ) => ifFrom ( boxerPub ) ) === true ) ? value : fallback ; } const neverBoxed = { } ; /**
   * Like unbox but raises an exception if unboxing fails.
   * @param {*} box the box to unbox.
   * @param {?function(function():boolean):boolean} ifFrom
   *    if the box may be opened by this unboxer's owner,
   *    then ifFrom receives the publicKey of the box creator.
   *    It should return true to allow unboxing to proceed.
   * @return {*} the value if unboxing is allowed or fallback otherwise.
   */ function unboxStrict ( box , ifFrom ) { // eslint-disable-line no-shadow const result = unbox ( box , ifFrom , neverBoxed ) ; if ( result === neverBoxed ) { throw new Error ( 'Could not unbox' ) ; } return result ; } return defineProperties ( create ( null ) , { // These close over private keys, so do not leak them. box : { value : box , enumerable : true } , unbox : { value : unbox , enumerable : true } , unboxStrict : { value : unboxStrict , enumerable : true } , privateKey : { value : privateKey , enumerable : true } , isPublicKey : { value : isPublicKey , enumerable : true } , // Modules may allow access to this, perhaps via module object. // See cjs/index.js. publicKey : { value : publicKey , enumerable : true } , [ publicKeySymbol ] : { value : publicKey , enumerable : true } , } ) ; }
Creates a bundle that should be available as a local variable to module code.
Creates a bundle that should be available as a local variable to module code .