-
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.
feat: add dir-index-html plugin and test
- Loading branch information
Showing
4 changed files
with
908 additions
and
4 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
packages/verified-fetch/src/plugins/plugin-handle-dir-index-html.ts
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,46 @@ | ||
import { code as dagPbCode } from '@ipld/dag-pb' | ||
import { dirIndexHtml } from '../utils/dir-index-html.js' | ||
import { okResponse } from '../utils/responses.js' | ||
import { BasePlugin } from './plugin-base.js' | ||
import type { PluginContext, VerifiedFetchPluginFactory } from './types.js' | ||
|
||
export class DirIndexHtmlPlugin extends BasePlugin { | ||
readonly codes = [dagPbCode] | ||
canHandle (context: PluginContext): boolean { | ||
const { cid, accept, pathDetails, directoryEntries } = context | ||
this.log('checking if we can handle %c with accept %s', cid, accept) | ||
if (pathDetails == null) { | ||
return false | ||
} | ||
if (pathDetails.terminalElement?.type !== 'directory') { | ||
return false | ||
} | ||
|
||
if (directoryEntries?.length === 0) { | ||
return false | ||
} | ||
|
||
return cid.code === dagPbCode | ||
} | ||
|
||
async handle (context: PluginContext): Promise<Response> { | ||
const { resource, pathDetails, directoryEntries } = context | ||
// const { getBlockstore } = this.pluginOptions | ||
|
||
if (pathDetails?.terminalElement == null) { | ||
throw new TypeError('Path details are required') | ||
} | ||
if (directoryEntries == null || directoryEntries?.length === 0) { | ||
throw new TypeError('Directory entries are required') | ||
} | ||
const terminalElement = pathDetails.terminalElement | ||
|
||
const gatewayURL = resource | ||
const htmlResponse = dirIndexHtml(terminalElement, directoryEntries, { gatewayURL, log: this.log }) | ||
const response = okResponse(resource, htmlResponse) | ||
response.headers.set('content-type', 'text/html') | ||
return response | ||
} | ||
} | ||
|
||
export const dirIndexHtmlPluginFactory: VerifiedFetchPluginFactory = (opts) => new DirIndexHtmlPlugin(opts) |
Oops, something went wrong.