Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed existing tests #19

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
29 changes: 21 additions & 8 deletions extensions/res/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,31 @@ export type DownloadOptions =
headers: Record<string, unknown>
}>

type Callback = (err?: any) => void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid any wherever possible, use unknown instead


export const download = <
Req extends Request = Request,
Res extends DummyResponse = DummyResponse,
>(req: Req, res: Res) =>
async (
path: string,
filename?: string,
options: DownloadOptions = {},
filename?: string | Callback,
options?: DownloadOptions | Callback,
cb?: Callback,
): Promise<Res> => {
const name: string | null = filename as string
let opts: DownloadOptions = options

let name: string | null = filename as string
let done = cb
let opts: DownloadOptions = (options || null) as DownloadOptions
if (typeof filename === 'function') {
done = filename
name = null
} else if (typeof options === 'function') {
done = options
}
opts = opts || {}
// set Content-Disposition when file is sent
const headers: Record<string, string> = {
'Content-Disposition': contentDisposition(name || path),
'Content-Disposition': contentDisposition(basename(name || path)),
}

// merge user-provided headers
Expand All @@ -34,13 +44,16 @@ async (
}
}
}

// merge user-provided options
opts = { ...opts, headers }

// send file

return await sendFile<Req, Res>(req, res)(path, opts)
return await sendFile<Req, Res>(req, res)(
path,
opts,
done || (() => undefined),
)
}

export const attachment =
Expand Down
15 changes: 10 additions & 5 deletions extensions/res/send/sendFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ export const sendFile = <
Req extends Request = Request,
Res extends DummyResponse = DummyResponse,
>(req: Req, res: Res) =>
async (path: string, { signal, ...opts }: SendFileOptions = {}) => {
async (
path: string,
{ signal, ...opts }: SendFileOptions = {},
cb?: (err?: any) => void,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better change it to unknown, we don't know what kind of error could be thrown

) => {
const { root, headers = {}, encoding = 'utf-8', ...options } = opts

if (!_path.isAbsolute(path) && !root) {
throw new TypeError('path must be absolute')
}

const filePath = root ? _path.join(root, path) : path

const stats = await Deno.stat(filePath)

headers['Content-Encoding'] = encoding
Expand All @@ -48,7 +50,6 @@ async (path: string, { signal, ...opts }: SendFileOptions = {}) => {

headers['Content-Security-Policy'] = 'default-src \'none\''
headers['X-Content-Type-Options'] = 'nosniff'

let status = 200

if (req.headers.get('range')) {
Expand All @@ -74,7 +75,11 @@ async (path: string, { signal, ...opts }: SendFileOptions = {}) => {

res._init.status = status

const file = await Deno.readFile(path, { signal })
let file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be instead wrapped in try catch

await Deno.readFile(filePath, { signal }).then((f) => file = f).catch((e) => {
cb!(e)
file = null
})

await send(req, res)(file)

Expand Down
11 changes: 11 additions & 0 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ export class Router<

return this
}

all(...args: UseMethodParams): this {
const handlers = args.slice(1).flat()
pushMiddleware(this.middleware)({
path: args[0] as Handler,
handler: handlers[0] as Handler,
handlers: handlers.slice(1) as Handler[],
type: 'route'
})
return this
}
/**
* Return the app's absolute pathname
* based on the parent(s) that have
Expand Down
Loading