-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathipfs-service-worker.js
68 lines (65 loc) · 1.95 KB
/
ipfs-service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
self.oninstall = (event) => {
event.waitUntil(self.skipWaiting())
}
self.onactivate = (event) => {
event.waitUntil(self.clients.claim())
}
importScripts('./lib/ServiceWorkerWare.js')
var root = (() => {
var tokens = (self.location + '').split('/')
tokens[ tokens.length - 1 ] = ''
return tokens.join('/')
})()
var worker = new ServiceWorkerWare()
worker.get(root, (req, res) => {
let content = '<html><body><br />' +
'Welcome to IPFS <br /> Enter a hash into the address bar ' +
'<br /> ie: ' + root + 'ipfs/QmcTopr4M1aYDAu7WbbBfNVWx1sRBnd6cPBSHjBbqXAovm' +
'</body></html>'
return Promise.resolve(new Response(content, {
headers: {
'Content-Type': 'text/html'
}
}))
})
worker.get(root + 'ipfs/', (req, res) => {
let url = req.clone().url
let hash = req.parameters.hash
let resource = req.parameters.resource
let content = '<html><body><br />' +
'Please enter a valid hash</body></html>'
console.log('IPFS GET: ' + url)
return Promise.resolve(new Response(content, {
headers: {
'Content-Type': 'text/html'
}
}))
})
worker.get(root + 'ipfs/:hash', (req, res) => {
let url = req.clone().url
let hash = req.parameters.hash
let content = '<html><body><br />' +
'Request for: <br />' + url +
'<br/>will work one day</body></html>'
console.log('IPFS GET: ' + url)
return Promise.resolve(new Response(content, {
headers: {
'Content-Type': 'text/html'
}
}))
})
worker.get(root + 'ipfs/:hash/*', (req, res) => {
let url = req.clone().url
let hash = req.parameters.hash
let resource = url.substring(url.indexOf(hash) + hash.lenth, url.length)
let content = '<html><body><br />' +
'Request for: <br />' + url +
'<br/>will work one day</body></html>'
console.log('IPFS GET: ' + url)
return Promise.resolve(new Response(content, {
headers: {
'Content-Type': 'text/html'
}
}))
})
worker.init()