-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat: middleware for several adapters #13477
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
Draft
dummdidumm
wants to merge
49
commits into
main
Choose a base branch
from
middleware-take-2
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.
Draft
Changes from 9 commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
8269fd8
provide emulate hook to intercept request early on
dummdidumm 9d0beac
vercel WIP
dummdidumm fd8c296
fix
dummdidumm b5ea739
bundle ourselves instead
dummdidumm ac0ca4b
wrap rewrite and url automatically
dummdidumm 9a38b48
devtimefix
dummdidumm 2d406bd
add capability for adapters to provide additional entry points
dummdidumm 253318a
vercel middleware
dummdidumm 05e9b62
node
dummdidumm 8b8c4ac
lint
dummdidumm 344dd7b
expose new helpers from Kit
dummdidumm cb2d693
incorporate new helpers in Vercel adapter + fix a few bugs
dummdidumm db4ed04
do base path normalization within Vite middleware so adapters don't h…
dummdidumm b6e623f
tweak adapter API: switch allowed to disallowed, restrict imports to …
dummdidumm 1c9b512
tests
dummdidumm fe1b82b
writing adapters docs
dummdidumm 45b4706
fix
dummdidumm c20d423
fix lint / test
dummdidumm 62289cb
vercel-middleware -> edge-middleware
dummdidumm 9a3c6e2
netlify edge middleware
dummdidumm e994082
cloudflare adapter
dummdidumm 05c941e
fix
dummdidumm 1d9839a
fix
dummdidumm 5d5eb26
fix, docs
dummdidumm 70ad0f8
fix, docs tweaks
dummdidumm b159f21
fix
dummdidumm 4335dc5
changesets
dummdidumm b5f68cb
drive-by notes fix
dummdidumm 37bc902
reuse `supports` mechanism, simplify additionalEntryPoints to a recor…
dummdidumm 5585df2
fix
dummdidumm 0b5396e
do normalization inside adapters
dummdidumm 76f5a9b
don't bundle sveltekit
dummdidumm 679759f
put them into src so that they benefit from better intellisense
dummdidumm e39683e
fix docs
dummdidumm c1f7591
Merge branch 'main' into middleware-take-2
dummdidumm 0876716
tweaks
dummdidumm 8524d6f
netlify: make where it runs on configurable; node/preview: run it on …
dummdidumm 6e90adc
beforeRequest -> interceptRequest
dummdidumm 083b22d
fix test config
dummdidumm bec3bed
Merge branch 'main' into middleware-take-2
dummdidumm 4e948fa
Merge remote-tracking branch 'origin/main' into middleware-take-2
dummdidumm b367434
Update tsconfig.json
dummdidumm 1ee1d04
docs tweaks
dummdidumm a2c5222
fix folder path
dummdidumm 30a3680
tweak
dummdidumm a2bb531
Merge branch 'main' into middleware-take-2
Rich-Harris cd116e9
docs feedback
dummdidumm 663d59d
warn on stream read (don't error because you can't guard against it i…
dummdidumm f9efcbd
fix test
dummdidumm 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,106 @@ | ||
import { initServer } from 'SERVER_INIT'; | ||
import * as user_middleware from 'MIDDLEWARE'; | ||
|
||
initServer({ | ||
env: { | ||
env: /** @type {Record<string, string>} */ (process.env), | ||
public_prefix: 'PUBLIC_PREFIX', | ||
private_prefix: 'PRIVATE_PREFIX' | ||
} | ||
}); | ||
|
||
export const config = user_middleware.config; | ||
|
||
/** | ||
* @param {Request} request | ||
* @param {any} context | ||
*/ | ||
export default async function middleware(request, context) { | ||
const url = new URL(request.url); | ||
|
||
const is_route_resolution = has_resolution_suffix(url.pathname); | ||
const is_data_request = has_data_suffix(url.pathname); | ||
|
||
if (is_route_resolution) { | ||
url.pathname = strip_resolution_suffix(url.pathname); | ||
} else if (is_data_request) { | ||
url.pathname = strip_data_suffix(url.pathname); | ||
} | ||
|
||
if (is_route_resolution || is_data_request) { | ||
request = new Request(url, request); | ||
} | ||
|
||
const response = await user_middleware.default(request, context); | ||
|
||
if (response instanceof Response && response.headers.has('x-middleware-rewrite')) { | ||
const rewritten = new URL( | ||
/** @type {string} */ (response.headers.get('x-middleware-rewrite')), | ||
url | ||
); | ||
|
||
if (rewritten.hostname === url.hostname) { | ||
if (is_route_resolution) { | ||
rewritten.pathname = add_resolution_suffix(rewritten.pathname); | ||
} else if (is_data_request) { | ||
rewritten.pathname = add_data_suffix(rewritten.pathname); | ||
} | ||
|
||
response.headers.set('REWRITE_HEADER', rewritten.pathname); | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
// the following internal helpers are a copy-paste of kit/src/runtime/pathname.js - should we expose them publicly? | ||
dummdidumm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
const DATA_SUFFIX = '/__data.json'; | ||
const HTML_DATA_SUFFIX = '.html__data.json'; | ||
|
||
/** @param {string} pathname */ | ||
function has_data_suffix(pathname) { | ||
return pathname.endsWith(DATA_SUFFIX) || pathname.endsWith(HTML_DATA_SUFFIX); | ||
} | ||
|
||
/** @param {string} pathname */ | ||
function add_data_suffix(pathname) { | ||
if (pathname.endsWith('.html')) return pathname.replace(/\.html$/, HTML_DATA_SUFFIX); | ||
return pathname.replace(/\/$/, '') + DATA_SUFFIX; | ||
} | ||
|
||
/** @param {string} pathname */ | ||
function strip_data_suffix(pathname) { | ||
if (pathname.endsWith(HTML_DATA_SUFFIX)) { | ||
return pathname.slice(0, -HTML_DATA_SUFFIX.length) + '.html'; | ||
} | ||
|
||
return pathname.slice(0, -DATA_SUFFIX.length); | ||
} | ||
|
||
const ROUTE_SUFFIX = '/__route.js'; | ||
|
||
/** | ||
* @param {string} pathname | ||
* @returns {boolean} | ||
*/ | ||
function has_resolution_suffix(pathname) { | ||
return pathname.endsWith(ROUTE_SUFFIX); | ||
} | ||
|
||
/** | ||
* Convert a regular URL to a route to send to SvelteKit's server-side route resolution endpoint | ||
* @param {string} pathname | ||
* @returns {string} | ||
*/ | ||
function add_resolution_suffix(pathname) { | ||
return pathname.replace(/\/$/, '') + ROUTE_SUFFIX; | ||
} | ||
|
||
/** | ||
* @param {string} pathname | ||
* @returns {string} | ||
*/ | ||
function strip_resolution_suffix(pathname) { | ||
return pathname.slice(0, -ROUTE_SUFFIX.length); | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.