Skip to content

Commit

Permalink
Merge pull request #148 from sebringrose/hotfixes
Browse files Browse the repository at this point in the history
BodyInit instead of string for SSR and CryptoHash
  • Loading branch information
sejori authored Mar 26, 2023
2 parents 6d16f46 + e41c9e0 commit b728e26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion handlers/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Handler, HandlerOptions } from "../types.ts"
import { Crypto } from "../utils/Crypto.ts"
import { mergeHeaders } from "../utils/helpers.ts"

export type Render = (ctx: RequestContext) => string | Promise<string>
export type Render = (ctx: RequestContext) => BodyInit | Promise<BodyInit>
export interface ssrHandlerOptions extends HandlerOptions {
crypto?: Crypto
}
Expand Down
5 changes: 3 additions & 2 deletions utils/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ export class Crypto {
* @param contents: string
* @returns hashHex: string
*/
async hash(contents: string): Promise<string> {
const hashBuffer = await crypto.subtle.digest(this.algData.hash, encoder.encode(contents))
async hash(contents: BodyInit): Promise<string> {
const temp = new Response(contents) // how to array buffer all the things
const hashBuffer = await crypto.subtle.digest(this.algData.hash, await temp.arrayBuffer())
return encodeB64(hashBuffer)
}

Expand Down
6 changes: 6 additions & 0 deletions utils/Profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type ProfileResults = Record<
>

class Profiler {
/**
* Benchmark performance of all server routes one at a time
* @param server
* @param config
* @returns results: ProfileResults
*/
static async run(server: Server, config?: ProfileConfig) {
const url = (config && config.url) || `http://${server.hostname}:${server.port}`
const count = (config && config.count) || 100
Expand Down
21 changes: 18 additions & 3 deletions utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import { Middleware, Route } from "../types.ts"
import { staticHandler } from "../handlers/static.ts"

/**
* Merge source headers into base headers and return base
* @param base: Headers
* @param source: Headers
* @returns
*/
export const mergeHeaders = (base: Headers, source: Headers) => {
for (const pair of source) {
base.set(pair[0], pair[1])
Expand All @@ -10,19 +16,28 @@ export const mergeHeaders = (base: Headers, source: Headers) => {
return base
}

export const staticDir = async (dirUrl: URL, middleware?: Middleware | Middleware[], routes?: Route[], depth = 1): Promise<Route[]> => {
/**
* Recursively create an array of routes with the static handler
* for all files/sub-directories in the provided directory.
* @param dirUrl: URL
* @param middleware: Middleware for each route added
* @param routes: init Route array
* @param depth: used internally to correct paths, don't set
* @returns routes: Route[]
*/
export const staticDir = async (dirUrl: URL, middleware?: Middleware | Middleware[], routes?: Route[], _depth = 1): Promise<Route[]> => {
if (!(await Deno.stat(dirUrl)).isDirectory) throw new Error("URL does not point to directory.")
if (!routes) routes = []

for await (const file of Deno.readDir(dirUrl)) {
const fileUrl = new URL(`file://${dirUrl.pathname}/${file.name}`)
if (file.isDirectory) {
await staticDir(fileUrl, middleware, routes, depth+1)
await staticDir(fileUrl, middleware, routes, _depth+1)
} else {
const pieces = dirUrl.pathname.split("/")

let dirPath = ''
for (let i=1; i<depth; i++) dirPath = `${pieces[pieces.length-i]}/${dirPath}`
for (let i=1; i<_depth; i++) dirPath = `${pieces[pieces.length-i]}/${dirPath}`

const filePath = `${dirPath}${file.name}`
routes.push({
Expand Down

0 comments on commit b728e26

Please sign in to comment.