Fun with Service Workers

A presentation at Codemotion Milan in November 2018 in Milan, Metropolitan City of Milan, Italy by Trent Willis

Slide 1

Slide 1

FUN WITH SERVICE WORKERS @TRENTMWILLIS

Slide 2

Slide 2

ATWOOD’S LAW "ANY APPLICATION THAT CAN BE WRITTEN IN JAVASCRIPT, WILL EVENTUALLY BE WRITTEN IN JAVASCRIPT.”

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

Slide 6

Slide 6

FUN WITH SERVICE WORKERS

Slide 7

Slide 7

@TRENTMWILLIS

Slide 8

Slide 8

LOVE THE WEB LOVE JAVASCRIPT @TRENTMWILLIS

Slide 9

Slide 9

JAVASCRIPT CONTINUES TO EVOLVE @TRENTMWILLIS

Slide 10

Slide 10

WEB HYPERTEXT APPLICATION TECHNOLOGY WORKING GROUP @TRENTMWILLIS

Slide 11

Slide 11

WHATWG @TRENTMWILLIS

Slide 12

Slide 12

STREAMS SPEC “IF INSTALLED INSIDE THE FETCH HOOK OF A SERVICE WORKER, THIS WOULD ALLOW DEVELOPERS TO TRANSPARENTLY POLYFILL NEW IMAGE FORMATS.” @TRENTMWILLIS

Slide 13

Slide 13

POLYFILLING FILE FORMATS @TRENTMWILLIS

Slide 14

Slide 14

New Image Format @TRENTMWILLIS

Slide 15

Slide 15

New Image Format <img src=“./my-image.nif”> @TRENTMWILLIS

Slide 16

Slide 16

New Image Format <img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF @TRENTMWILLIS

Slide 17

Slide 17

New Image Format Service Worker <img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF @TRENTMWILLIS

Slide 18

Slide 18

New Image Format .nif Service Worker .gif <img src=“./my-image.nif”> *Can Display PNG/JPEG/GIF, Not NIF @TRENTMWILLIS

Slide 19

Slide 19

ALL CODE WILL BE AVAILABLE ONLINE @TRENTMWILLIS

Slide 20

Slide 20

navigator.serviceWorker.register(‘./service-worker.js’); @TRENTMWILLIS

Slide 21

Slide 21

self.addEventListener('fetch', (event) => {; }); @TRENTMWILLIS

Slide 22

Slide 22

self.addEventListener('fetch', (event) => {; event.respondWith(); }); @TRENTMWILLIS

Slide 23

Slide 23

self.addEventListener('fetch', (event) => {; event.respondWith(validFileFromPolyfilledFile(event.request)); }); @TRENTMWILLIS

Slide 24

Slide 24

self.addEventListener('fetch', (event) => {; if (isPolyfilledFile(event.request)) { event.respondWith(validFileFromPolyfilledFile(event.request)); validFileFromPolyfilledFile }; }); @TRENTMWILLIS

Slide 25

Slide 25

const validFileFromPolyfilledFile = async (request) => { }; @TRENTMWILLIS

Slide 26

Slide 26

const validFileFromPolyfilledFile = async (request) => { const originalResponse = await fetch(request); }; @TRENTMWILLIS

Slide 27

Slide 27

const validFileFromPolyfilledFile = async (request) => { const originalResponse = await fetch(request); const originalData = originalResponse.blob(); // .text(), .json(), etc. }; @TRENTMWILLIS

Slide 28

Slide 28

const validFileFromPolyfilledFile = async (request) => { const originalResponse = await fetch(request); const originalData = originalResponse.blob(); // .text(), .json(), etc. const modifiedData = polyfillTransform(originalData); }; @TRENTMWILLIS

Slide 29

Slide 29

const validFileFromPolyfilledFile = async (request) => { const originalResponse = await fetch(request); const originalData = originalResponse.blob(); // .text(), .json(), etc. const modifiedData = polyfillTransform(originalData); const modifiedResponse = new Response(modifiedData); return modifiedResponse; }; @TRENTMWILLIS

Slide 30

Slide 30

cryptogram-naive.glitch.me @TRENTMWILLIS

Slide 31

Slide 31

html-modules-polyfill.glitch.me @TRENTMWILLIS

Slide 32

Slide 32

COMPILING CODE AT RUNTIME @TRENTMWILLIS

Slide 33

Slide 33

TYPESCRIPT AT RUNTIME @TRENTMWILLIS

Slide 34

Slide 34

<script src="my-module.ts"></script>

@TRENTMWILLIS

Slide 35

Slide 35

<script src="my-module.ts" type="module"></script>

@TRENTMWILLIS

Slide 36

Slide 36

CACHE API @TRENTMWILLIS

Slide 37

Slide 37

ONLY COMPILE CHANGED FILES @TRENTMWILLIS

Slide 38

Slide 38

const compileWithCache = async (response, compile) => { }; @TRENTMWILLIS

Slide 39

Slide 39

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); }; @TRENTMWILLIS

Slide 40

Slide 40

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); if (response.headers.get('status') === '304') { }; }; @TRENTMWILLIS

Slide 41

Slide 41

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); if (response.headers.get('status') === '304') { return cache.match(request.url); }; }; @TRENTMWILLIS

Slide 42

Slide 42

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); if (response.headers.get('status') === '304') { return cache.match(request.url); }; const compiledResponse = compile(response); }; @TRENTMWILLIS

Slide 43

Slide 43

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); if (response.headers.get('status') === '304') { return cache.match(request.url); }; const compiledResponse = compile(response); cache.put(request.url, compiledResponse.clone()); }; @TRENTMWILLIS

Slide 44

Slide 44

const compileWithCache = async (response, compile) => { const cache = await caches.open(‘compile-cache'); if (response.headers.get('status') === '304') { return cache.match(request.url); }; const compiledResponse = compile(response); cache.put(request.url, compiledResponse.clone()); return compiledResponse; }; @TRENTMWILLIS

Slide 45

Slide 45

typescript-in-browser.glitch.me @TRENTMWILLIS

Slide 46

Slide 46

OFF THE MAIN THREAD DATA PROCESSING @TRENTMWILLIS

Slide 47

Slide 47

API JS @TRENTMWILLIS Process Here

Slide 48

Slide 48

API Service Worker UI @TRENTMWILLIS Process Here

Slide 49

Slide 49

API Service Worker Process Here UI @TRENTMWILLIS

Slide 50

Slide 50

API Web Worker Service Worker UI @TRENTMWILLIS Process Here

Slide 51

Slide 51

const transform = async (request, clientId) => { const [response, port] = await Promise.all([ fetch(request), getPortToWebWorkerFromClient(clientId) getPortToWebWorkerFromClient ]); }; @TRENTMWILLIS

Slide 52

Slide 52

const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); }; @TRENTMWILLIS

Slide 53

Slide 53

const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); return new Promise(resolve => { }); }; @TRENTMWILLIS

Slide 54

Slide 54

const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); return new Promise(resolve => { client.postMessage({}); }); }; @TRENTMWILLIS

Slide 55

Slide 55

const webWorker = new Worker('web-worker.js'); navigator.serviceWorker.onmessage = () => { }; @TRENTMWILLIS

Slide 56

Slide 56

const webWorker = new Worker('web-worker.js'); navigator.serviceWorker.onmessage = () => { const channel = new MessageChannel(); }; @TRENTMWILLIS

Slide 57

Slide 57

const webWorker = new Worker('web-worker.js'); navigator.serviceWorker.onmessage = () => { const channel = new MessageChannel(); const serviceWorker = navigator.serviceWorker.controller; serviceWorker.postMessage({port: channel.port1}, [channel.port1]); }; @TRENTMWILLIS

Slide 58

Slide 58

const webWorker = new Worker('web-worker.js'); navigator.serviceWorker.onmessage = () => { const channel = new MessageChannel(); const serviceWorker = navigator.serviceWorker.controller; serviceWorker.postMessage({port: channel.port1}, [channel.port1]); webWorker.postMessage({port: channel.port2}, [channel.port2]); }; @TRENTMWILLIS

Slide 59

Slide 59

const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); return new Promise(resolve => { client.postMessage({}); }); }; @TRENTMWILLIS

Slide 60

Slide 60

const getPortToWebWorkerFromClient = async (clientId) => { const client = await self.clients.get(clientId); return new Promise(resolve => { self.onmessage = (msg) => { self.onmessage = null; resolve(msg.data.port); }; client.postMessage({}); }); }; @TRENTMWILLIS

Slide 61

Slide 61

const transform = async (request, clientId) => { const [response, port] = await Promise.all([ fetch(request), getPortToWebWorkerFromClient(clientId) ]); }; @TRENTMWILLIS

Slide 62

Slide 62

const transform = async (request, clientId) => { const [response, port] = await Promise.all([ fetch(request), getPortToWebWorkerFromClient(clientId) ]); return new Promise(async (resolve) => { }); }; @TRENTMWILLIS

Slide 63

Slide 63

const transform = async (request, clientId) => { const [response, port] = await Promise.all([ fetch(request), getPortToWebWorkerFromClient(clientId) ]); return new Promise(async (resolve) => { port.postMessage(await response.json()); }); }; @TRENTMWILLIS

Slide 64

Slide 64

const transform = async (request, clientId) => { const [response, port] = await Promise.all([ fetch(request), getPortToWebWorkerFromClient(clientId) ]); return new Promise(async (resolve) => { port.onmessage = (msg) => { resolve(new Response([${msg.data.toString()}])); }; port.postMessage(await response.json()); }); }; @TRENTMWILLIS

Slide 65

Slide 65

service-worker-worker.glitch.me @TRENTMWILLIS

Slide 66

Slide 66

STREAM DATA PROCESSING @TRENTMWILLIS

Slide 67

Slide 67

Original Data @TRENTMWILLIS Modified Data

Slide 68

Slide 68

BATCH BATCH DIGRA PROCESSING Encrypted Chunk Encrypted Chunk Decrypted Chunk @TRENTMWILLIS Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Decrypted Chunk

Slide 69

Slide 69

STREAM BATCH DIGRA PROCESSING Encrypted Chunk Encrypted Chunk Decrypted Chunk @TRENTMWILLIS Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Encrypted Chunk Decrypted Chunk Decrypted Chunk

Slide 70

Slide 70

Data Chunk Data Chunk Modified Chunk @TRENTMWILLIS Data Chunk Modified Chunk Data Chunk Modified Chunk Data Chunk Modified Chunk Data Chunk Modified Chunk Modified Chunk

Slide 71

Slide 71

Original Data Data Chunk Data Chunk Modified Chunk Data Chunk Modified Chunk @TRENTMWILLIS Modified Data Data Chunk Modified Chunk Data Chunk Modified Chunk Data Chunk Modified Chunk Modified Chunk

Slide 72

Slide 72

const validFileFromPolyfilledFile = async (request) => { const originalResponse = await fetch(request); const originalData = originalResponse.blob(); // .text(), .json(), etc. const modifiedData = polyfillTransform(originalData); const modifiedResponse = new Response(modifiedData); return modifiedResponse; }; @TRENTMWILLIS

Slide 73

Slide 73

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 74

Slide 74

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 75

Slide 75

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 76

Slide 76

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 77

Slide 77

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 78

Slide 78

const validFileFromPolyfilledFile = async (request) => {; const originalResponse = await fetch(request); const transformStream = new TransformStream(new Transformer()); Transformer const transformedBody = originalResponse.body.pipeThrough( transformStream ); const transformedResponse = new Response(transformedBody); return transformedResponse; }; @TRENTMWILLIS

Slide 79

Slide 79

class Transformer {; async start() {}; async transform() {}; async flush() {}; }; @TRENTMWILLIS

Slide 80

Slide 80

cryptogram-streaming.glitch.me @TRENTMWILLIS

Slide 81

Slide 81

POLYFILLING FILE FORMATS SERVICE WORKERS COMPILING CODE AT RUNTIME WEB ASSEMBLY OFF THE MAIN THREAD DATA PROCESSING STREAM DATA PROCESSING @TRENTMWILLIS STREAMS ESNEXT WEB COMPONENTS AND MORE!

Slide 82

Slide 82

EXPERIMENT & HAVE FUN WITH JAVASCRIPT! *ALSO, GLITCH IS GREAT FOR EXPERIMENTS! @TRENTMWILLIS

Slide 83

Slide 83

QUESTIONS? @TRENTMWILLIS