bundle-port-bridge.mjs raw

   1  // The application half of the bundle-port fixture: it owns the page protocol.
   2  //
   3  // A bridge may import the host's marshalling helpers, which is the point of the
   4  // split - the host answers the module's imports, the bridge decides what they
   5  // mean. readStr/cbs read the module's memory and queue a module callback; the
   6  // port at the other end belongs to the page that called startBundle.
   7  import { readStr, cbs } from './$runtime/host.mjs';
   8  
   9  export default function appBridge({ port }) {
  10    if (!port) throw new Error('the bundle-port bridge needs the page port');
  11    return {
  12      bundle_port_on_message(cbID) {
  13        port.onmessage = (e) => cbs(cbID, String(e.data));
  14      },
  15      bundle_port_post(ptr, len) {
  16        port.postMessage(readStr(ptr, len));
  17      },
  18    };
  19  }
  20