web_sw_boot.mx raw

   1  // Regression: the service worker's half of a Moxie wasm bundle boots.
   2  //
   3  // A service worker is a third execution context - no document, no Worker - and
   4  // `moxie build -target wasm` stages its host as $sw/wasm-host-sw.mjs beside the
   5  // runtime. This fixture is the module that host instantiates: it registers the
   6  // install hook and logs, which is all a boot proof needs.
   7  //
   8  // The proof is not this file's stdout (a service worker's console is its own):
   9  // tests/wasm/emit_check.sh builds it to $sw/sw.wasm, registers the emitted
  10  // $sw/wasm-host-sw.mjs from a page through the emitted sw-register.js, and
  11  // fails unless the worker reaches `activated` AND the server saw the worker
  12  // fetch its module. A worker whose host or module is broken never gets there.
  13  //
  14  // Two markers, for two harnesses. browser-only: phase 0 builds it natively
  15  // (the wasm-only packages are panic stubs) and skips the run. skip: phase 5's
  16  // generic target list loads every fixture through the page/Worker host, and a
  17  // service-worker module has no meaning there - this one is built and booted by
  18  // tests/wasm/emit_check.sh, in the context it is written for.
  19  //:wasmtest browser-only
  20  //:wasmtest skip
  21  package main
  22  
  23  import (
  24  	"web/sw"
  25  )
  26  
  27  func onInstall(event sw.Event) {
  28  	// Nothing to precache in a boot proof; the hook exists so the host's
  29  	// sw_on_install path is exercised and the module is not trivially empty.
  30  	// WaitUntil hands the handler a continuation, and the worker stays alive
  31  	// until it is called - which is the one thing a service worker must get
  32  	// right, since the browser kills it the moment the event settles.
  33  	sw.WaitUntil(event, func(done func()) { done() })
  34  }
  35  
  36  func main() {
  37  	// Termination: a batch program must still be stoppable, so the entry
  38  	// handles the channel the runtime closes on SIGINT/SIGTERM.
  39  	select {
  40  	case <-shutdown:
  41  		return
  42  	default:
  43  	}
  44  
  45  	sw.OnInstall(onInstall)
  46  	sw.Log("sw fixture: hooks registered")
  47  }
  48