-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: code cleanup, dep removal, & miscellaneous (#89)
* refactor: remove unused helia andu se verified fetch * chore: remove empty line * fix: load config iframe using a hash fragment fixes #88 * fix: load default config initially * chore: fix linting errors * chore: remove unused deps * chore: re-add needed @helia/ipns dependency * fix: set verifiedFetch when sw is activated * chore: remove lib/heliaFetch and cleanup sw.ts (#95) * fix: config loading on subdomains waits for updated config (#94) * fix: config loading on subdomains waits for updated config * fix: remove event.waitUtil inside channel.onmessagefrom the activate event is fired when the service worker is first activated, and then the channel.onmessagefrom event listener is set up. by the time the channel receives any messages, the event passed to activate will be done, and waitUntil has no effect (and throws an error) * fix: service worker handles being disposed of * fix: preload request from redirect page is handled by sw * chore: better trace logging for config updates --------- Co-authored-by: Daniel N <[email protected]> Co-authored-by: Russell Dempsey <[email protected]>
- Loading branch information
1 parent
62169a1
commit 0a6d2f3
Showing
11 changed files
with
224 additions
and
382 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,20 @@ | ||
import { trustlessGateway } from '@helia/block-brokers' | ||
import { createHeliaHTTP } from '@helia/http' | ||
import { delegatedHTTPRouting } from '@helia/routers' | ||
import { IDBBlockstore } from 'blockstore-idb' | ||
import { IDBDatastore } from 'datastore-idb' | ||
import { dnsJsonOverHttps } from '@helia/ipns/dns-resolvers' | ||
import { createVerifiedFetch, type VerifiedFetch } from '@helia/verified-fetch' | ||
import { getConfig } from './lib/config-db.ts' | ||
import { trace } from './lib/logger.ts' | ||
import type { Helia } from '@helia/interface' | ||
import { contentTypeParser } from './lib/content-type-parser.ts' | ||
import { log } from './lib/logger.ts' | ||
|
||
export async function getHelia (): Promise<Helia> { | ||
export async function getVerifiedFetch (): Promise<VerifiedFetch> { | ||
const config = await getConfig() | ||
trace(`config-debug: got config for sw location ${self.location.origin}`, config) | ||
const blockstore = new IDBBlockstore('./helia-sw/blockstore') | ||
const datastore = new IDBDatastore('./helia-sw/datastore') | ||
await blockstore.open() | ||
await datastore.open() | ||
log(`config-debug: got config for sw location ${self.location.origin}`, config) | ||
|
||
const helia = await createHeliaHTTP({ | ||
blockstore, | ||
datastore, | ||
blockBrokers: [ | ||
trustlessGateway({ | ||
gateways: [...config.gateways, 'https://trustless-gateway.link'] | ||
}) | ||
], | ||
routers: [...config.routers, 'https://delegated-ipfs.dev'].map(rUrl => delegatedHTTPRouting(rUrl)) | ||
const verifiedFetch = await createVerifiedFetch({ | ||
gateways: config.gateways ?? ['https://trustless-gateway.link'], | ||
routers: config.routers ?? ['https://delegated-ipfs.dev'], | ||
dnsResolvers: ['https://delegated-ipfs.dev/dns-query'].map(dnsJsonOverHttps) | ||
}, { | ||
contentTypeParser | ||
}) | ||
|
||
return helia | ||
return verifiedFetch | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { type ContentTypeParser } from '@helia/verified-fetch' | ||
import { fileTypeFromBuffer } from '@sgtpooki/file-type' | ||
|
||
// default from verified-fetch is application/octect-stream, which forces a download. This is not what we want for MANY file types. | ||
export const defaultMimeType = 'text/html' | ||
|
||
export const contentTypeParser: ContentTypeParser = async (bytes, fileName) => { | ||
const detectedType = (await fileTypeFromBuffer(bytes))?.mime | ||
if (detectedType != null) { | ||
return detectedType | ||
} | ||
if (fileName == null) { | ||
// no other way to determine file-type. | ||
return defaultMimeType | ||
} | ||
|
||
// no need to include file-types listed at https://github.com/SgtPooki/file-type#supported-file-types | ||
switch (fileName.split('.').pop()) { | ||
case 'css': | ||
return 'text/css' | ||
case 'html': | ||
return 'text/html' | ||
case 'js': | ||
return 'application/javascript' | ||
case 'json': | ||
return 'application/json' | ||
case 'txt': | ||
return 'text/plain' | ||
case 'woff2': | ||
return 'font/woff2' | ||
// see bottom of https://github.com/SgtPooki/file-type#supported-file-types | ||
case 'svg': | ||
return 'image/svg+xml' | ||
case 'csv': | ||
return 'text/csv' | ||
case 'doc': | ||
return 'application/msword' | ||
case 'xls': | ||
return 'application/vnd.ms-excel' | ||
case 'ppt': | ||
return 'application/vnd.ms-powerpoint' | ||
case 'msi': | ||
return 'application/x-msdownload' | ||
default: | ||
return defaultMimeType | ||
} | ||
} |
Oops, something went wrong.
@lidel removal of this will also help with total size as mentioned in #93