-
Notifications
You must be signed in to change notification settings - Fork 48
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
support absolute root path for serveStatic #78
Open
miyamonz
wants to merge
2
commits into
honojs:main
Choose a base branch
from
miyamonz:serve-static-abs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { getFilePathforAbsRoot } from './getFilePathforAbsRoot' | ||
|
||
describe('getFilePathforAbsRoot', () => { | ||
it('Should return file path correctly', async () => { | ||
expect(getFilePathforAbsRoot({ filename: 'foo', root: '/bar' })).toBe('/bar/foo/index.html') | ||
expect(getFilePathforAbsRoot({ filename: 'foo.txt', root: '/bar' })).toBe('/bar/foo.txt') | ||
}) | ||
}) |
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,23 @@ | ||
import { getFilePath } from 'hono/utils/filepath' | ||
|
||
/** | ||
* wrapper for getFilePath, with absolute root path | ||
*/ | ||
export function getFilePathforAbsRoot(options: { | ||
root: string | ||
filename: string | ||
defaultDocument?: string | ||
}) { | ||
if (!options.root.startsWith('/')) { | ||
throw new Error('root must be absolute path') | ||
} | ||
|
||
const path = getFilePath({ | ||
filename: options.filename, | ||
defaultDocument: options.defaultDocument, | ||
}) | ||
if (!path) return undefined | ||
|
||
const root = options.root + (options.root?.endsWith('/') ? '' : '/') | ||
return root + path | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think that
getFilePathForAbsRoot
would be a better name.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.
I agree.
I am considering a better name but have not yet been able to come up with one.
The current getFilePath function has the following features:
../a
=> undefined)./hoge/fuga
=>hoge/fuga
,/hoo
=>hoo
)/hoo
=>/hoo/index.html
)ref: https://github.com/honojs/hono/blob/fc73d5d105b65ae823184d33e67c8228aa1c2e02/src/utils/filepath.test.ts
I think that getFilePath has two roles:
features 1 and 2 are for KV, and the rest are for general use to serve something. Is that correct?
Relative to the parent or absolute path doesn't work because of the features 1 and 2.
I've tested that serveStatic in Bun and Deno also doesn't work.
I believe it would be better to prepare another function specifically for KV that utilizes getFilePath and implements features for KV as follows:
What are your thoughts on this? If you agree with this opinion, I will create a pull request for it.
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.
These are for both KV and other file system-based runtimes.
'1' is necessary to avoid issues such as directory traversal. In older versions of Bun, the URL path was not normalized, so there was a possibility that the URL path included
..
, which could lead to unexpected behavior. I think we can handle it in a different place, such asserve-static
for Bun, but currently, we are handling it ingetFilePath()
.'2' is to normalize the path to find the file. The file path emitted from
getFilePath()
will be used as shown in the link:https://github.com/honojs/hono/blob/fc73d5d105b65ae823184d33e67c8228aa1c2e02/src/adapter/bun/serve-static.ts#L38
In KV as well,
/foo
will fall back to/foo/index.html
. So, this is necessary for KV.