2
2
3
3
// Note the 'go.1.23.4' below, that matches the version you just found:
4
4
importScripts ( 'https://cdn.jsdelivr.net/gh/golang/[email protected] /misc/wasm/wasm_exec.js' )
5
- // If you compiled with TinyGo then, similarly, use:
6
- // importScripts('https://cdn.jsdelivr.net/gh/tinygo-org/[email protected] /targets/wasm_exec.js')
7
5
8
- importScripts ( 'https://cdn.jsdelivr.net/gh/nlepage/[email protected] /sw.js' )
6
+ // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
7
+ // The base of this code comes from:
8
+ //
9
+ // https://github.com/nlepage/go-wasm-http-server
10
+ //
11
+ // Apache License
12
+ // Version 2.0, January 2004
13
+ // http://www.apache.org/licenses/
14
+ //
15
+ // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
16
+ function registerWasmHTTPListener ( wasm , { base, passthroughFunc, cacheName, args = [ ] } = { } ) {
17
+ let path = new URL ( registration . scope ) . pathname
18
+ if ( base && base !== '' ) path = `${ trimEnd ( path , '/' ) } /${ trimStart ( base , '/' ) } `
9
19
20
+ const handlerPromise = new Promise ( setHandler => {
21
+ self . wasmhttp = {
22
+ path,
23
+ setHandler,
24
+ }
25
+ } )
26
+
27
+ const go = new Go ( )
28
+ go . argv = [ wasm , ...args ]
29
+ const source = cacheName
30
+ ? caches . open ( cacheName ) . then ( ( cache ) => cache . match ( wasm ) ) . then ( ( response ) => response ?? fetch ( wasm ) )
31
+ : caches . match ( wasm ) . then ( response => ( response ) ?? fetch ( wasm ) )
32
+ WebAssembly . instantiateStreaming ( source , go . importObject ) . then ( ( { instance } ) => go . run ( instance ) )
33
+
34
+ addEventListener ( 'fetch' , e => {
35
+ const { pathname } = new URL ( e . request . url ) ;
36
+
37
+ if ( passthroughFunc && passthroughFunc ( e . request ) ) {
38
+ e . respondWith ( fetch ( e . request ) )
39
+ return ;
40
+ }
41
+
42
+ if ( ! pathname . startsWith ( path ) ) {
43
+ // e.respondWith(fetch(e.request))
44
+ return ;
45
+ }
46
+
47
+ e . respondWith ( handlerPromise . then ( handler => handler ( e . request ) ) )
48
+ } )
49
+ }
50
+
51
+ function trimStart ( s , c ) {
52
+ let r = s
53
+ while ( r . startsWith ( c ) ) r = r . slice ( c . length )
54
+ return r
55
+ }
56
+
57
+ function trimEnd ( s , c ) {
58
+ let r = s
59
+ while ( r . endsWith ( c ) ) r = r . slice ( 0 , - c . length )
60
+ return r
61
+ }
62
+ // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
10
63
11
64
const CURRENT_VERSION = "{{.Version}}" ;
12
65
const WASM = 'main.wasm'
@@ -37,4 +90,43 @@ addEventListener('message', (event) => {
37
90
console . log ( `The service worker sent me a message: ${ event . data } ` ) ;
38
91
} )
39
92
40
- registerWasmHTTPListener ( WASM )
93
+ const approvedFiles = [
94
+ "index.html" ,
95
+ "sw.js"
96
+ ]
97
+
98
+ function isSameOrigin ( urlString ) {
99
+ const urlOrigin = ( new URL ( urlString ) ) . origin ;
100
+ return urlOrigin === self . location . origin ;
101
+ }
102
+
103
+ function urlIsRootServiceWorkerInstall ( urlString ) {
104
+ //
105
+ let cleanString = urlString ;
106
+ if ( ! cleanString . endsWith ( "/" ) ) {
107
+ cleanString += "/"
108
+ }
109
+
110
+ return self . location . href . slice ( 0 , - 5 ) === cleanString
111
+ }
112
+
113
+ registerWasmHTTPListener (
114
+ WASM ,
115
+ {
116
+ passthroughFunc : ( request ) => {
117
+ let url = new URL ( request . url ) ;
118
+
119
+ if ( urlIsRootServiceWorkerInstall ( request . url ) ) {
120
+ return true ;
121
+ }
122
+
123
+ for ( let i = 0 ; i < approvedFiles . length ; i ++ ) {
124
+ if ( url . pathname . endsWith ( approvedFiles [ i ] ) ) {
125
+ return true ;
126
+ }
127
+ }
128
+
129
+ return ! isSameOrigin ( request . url ) ;
130
+ }
131
+ }
132
+ )
0 commit comments