Skip to content

Commit

Permalink
Wasm: save data in case of canceled requests
Browse files Browse the repository at this point in the history
There's currently no way for a service worker to know if a request has been canceled. This includes understanding if the user skipped forward in a video, for example.
To avoid returning the entire file every time, we're limiting Range requests from audio/video tags to 2MB chunks only.

See: w3c/ServiceWorker#1544
  • Loading branch information
ItalyPaleAle committed Oct 10, 2020
1 parent 186cd0b commit 0406a67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
15 changes: 0 additions & 15 deletions ui/src/sw/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,6 @@ self.addEventListener('message', async (event) => {
}
})

/*
;(async function main() {
// Load the WebAssembly
const result = await WebAssembly.instantiateStreaming(fetch('http://localhost:3129/ui/app.wasm'), go.importObject)
go.run(result.instance)
// Get the index
const index = Prvt.getIndex('http://localhost:3129', new Uint8Array(DecodeArrayBuffer(RemovePaddingChars(masterKey))))
console.log(index)
console.log(await index.listFolder('/'))
console.log(await index.stat())
console.log(await index.getFileByPath('/bensound-energy.mp3'))
console.log(await index.getFileById('f7a82545-fe70-4681-a5b2-ab593ebfab37'))
})()*/

/*
Test:
Expand Down
16 changes: 14 additions & 2 deletions wasm/decrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ func DecryptRequest() js.Func {
method = decryptRequestPromise(masterKey, fileId)
} else {
// Range requests

// If this is a request for a video or audio, then we will limit the returned range to to 2 MB max
// This is because we have no way yet to detect if requests are canceled (waiting for https://github.com/w3c/ServiceWorker/issues/1544) and this way we at least waste less data when requests are canceled
// We determine if the request is for a video or audio by looking at the destination (see https://developer.mozilla.org/en-US/docs/Web/API/Request/destination)
destination := req.Get("destination").String()
if destination == "audio" || destination == "track" || destination == "video" {
// Limit to 2 MB
// Also the case of Length = 0, which means that the request was for the entire file
if rng.Length > (2<<20) || rng.Length == 0 {
rng.Length = 2 << 20
}
}

// Make the request
method = decryptRangeRequestPromise(masterKey, fileId, rng)
}
promiseConstructor := js.Global().Get("Promise")
Expand Down Expand Up @@ -391,7 +405,6 @@ func responseUnderlyingSource(body io.ReadCloser, decryptFunc decryptFuncSignatu
for {
buf := make([]byte, 65536)
n, err := pr.Read(buf)
fmt.Println("Read", n, err)
if err != nil && err != io.EOF {
// Stream had an error
controller.Call("error", jsError(err.Error()))
Expand All @@ -402,7 +415,6 @@ func responseUnderlyingSource(body io.ReadCloser, decryptFunc decryptFuncSignatu
}
if err == io.EOF {
// Stream is done
fmt.Println("Stream closed")
controller.Call("close")
return
}
Expand Down

0 comments on commit 0406a67

Please sign in to comment.