-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: master
Are you sure you want to change the base?
Changes from 9 commits
a697a23
f0652e1
81d6a27
7195321
f90b3f1
6317e79
ca8be3a
2a10b39
d40587e
144e9ec
c368800
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,21 +9,31 @@ export type DownloadOptions = | |
headers: Record<string, unknown> | ||
}> | ||
|
||
type Callback = (err?: any) => void | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid |
||
|
||
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 | ||
|
@@ -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 = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better change it to |
||
) => { | ||
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 | ||
|
@@ -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')) { | ||
|
@@ -74,7 +75,11 @@ async (path: string, { signal, ...opts }: SendFileOptions = {}) => { | |
|
||
res._init.status = status | ||
|
||
const file = await Deno.readFile(path, { signal }) | ||
let file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--allow-all
is insecure to use. if you meant to add a permission add it via--allow-{perm}