This repository has been archived on 2023-12-16. You can view files and clone it, but cannot push or open issues or pull requests.
intergalactic/src/fetcher.js

143 lines
4.7 KiB
JavaScript

// Inspired by https://github.com/ipfs/ipfs-service-worker which does not work with recent versions
// of js-ipfs.
/* global self, Response */
'use strict'
const IPFS = require('ipfs')
let node
// https://redfin.engineering/how-to-fix-the-refresh-button-when-using-service-workers-a8e27af6df68
self.skipWaiting()
self.addEventListener('install', (event) => {
console.log('install step')
event.waitUntil(self.skipWaiting())
})
self.addEventListener('activate', (event) => {
console.log('activate step')
event.waitUntil(self.clients.claim())
})
function initialize_ipfs() {
return new Promise((resolve, reject) => {
node = new IPFS()
node.on('ready', () => {
console.log('js-ipfs node is ready')
resolve()
})
node.on('error', (err) => {
console.log('js-ipfs node errored', err)
reject(err)
})
})
}
let ipfs_initialized
function response(status_code, status_text, body, headers={}) {
// Return a promise for a response with the given HTTP status code, status text, and HTML body.
return new Promise((resolve, reject) => {
resolve(
new Response(
body,
{
status: status_code,
statusText: status_text,
headers: headers
}
)
)
})
}
function not_found_response() {
return response(
404, 'Not Found',
Buffer.from('<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>404 Not Found</h1></center><hr><center>Intergalactic</center></body></html>'),
{'Content-Type': 'text/html; charset=utf-8'}
)
}
function forbidden_response() {
return response(
403, 'Forbidden',
Buffer.from('<html><head><title>404 Not Found</title></head><body bgcolor="white"><center><h1>403 Forbidden</h1></center><hr><center>Intergalactic</center></body></html>'),
{'Content-Type': 'text/html; charset=utf-8'}
)
}
self.addEventListener('fetch', (event) => {
if (!event.request.url.startsWith(self.location.origin)) {
return console.log('fetch not in scope:', event.request.url)
}
console.log('handling fetch event:', event.request.url)
const request_path = (new URL(event.request.url)).pathname
if (request_path == '/ipfs/bundle.js' || request_path == '/ipfs/fetcher.js') {
return console.log('skipping bundles')
}
// If this is a same-origin or CORS request, and it's not to a URL within the same base IPFS
// hash, then block it as forbidden. This in effect gives each base IPFS hash its own origin.
if (event.request.mode == 'same-origin' || event.request.mode == 'cors') {
let matches = /^(\/ipfs\/\w*)/.exec((new URL(event.request.referrer)).pathname)
let referrer_base_path = matches[1]
if (referrer_base_path && !request_path.startsWith(referrer_base_path)) {
console.log(
'denying ' + event.request.mode + ' request from referrer with different base hash:',
event.request.referrer
)
event.respondWith(forbidden_response())
return
}
}
if (!ipfs_initialized) {
ipfs_initialized = initialize_ipfs()
}
event.respondWith(
ipfs_initialized.then(() => {
return node.files.get(request_path)
}).then((files) => {
// If there's just one result, return it.
if (files.length == 1 && files[0].content) {
console.log('found file content at', files[0].path)
return response(200, 'OK', files[0].content)
}
// If there are multiple results (and so this appears to be a directory), but this URL
// doesn't have a trailing slash, redirect. This ensures relative paths work correctly
// for any resources pulled in on the page.
if (files.length > 1 && event.request.url.slice(-1) != '/') {
console.log('redirect to', event.request.url + '/')
return response(302, 'Found', '', {'Location': event.request.url + '/'})
}
// If there are multiple results, look for an index.html page to return.
for (let file of files) {
if (file.path.endsWith('/index.html')) {
console.log('found file content at', file.path)
return response(200, 'OK', file.content)
}
}
console.log('file not found at', event.request.url)
return not_found_response()
}).catch((error) => {
console.log(error)
return not_found_response()
})
)
})