-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat(transport): Make SvelteKit adapter deployable on Netlify Edge Functions #2155
Draft
blaine-arcjet
wants to merge
3
commits into
main
Choose a base branch
from
phated/svelte-transport
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 all commits
Commits
Show all changes
3 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 |
---|---|---|
|
@@ -136,4 +136,6 @@ edge-light.js | |
edge-light.d.ts | ||
index.js | ||
index.d.ts | ||
svelte.js | ||
svelte.d.ts | ||
test/*.js |
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
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,79 @@ | ||
// This entire workaround is to make SvelteKit run on Netlify Edge Functions | ||
// which execute in an old version of Deno that doesn't support some `node:*` | ||
// imports, such as `node:assert`. This makes the undici import fail when we try | ||
// to use `@connectrpc/connect-node`. | ||
// | ||
// By making these dynamic imports, we can await them inside of the `unary` or | ||
// `stream` function calls to figure out which client we are using. | ||
|
||
export function createTransport(baseUrl: string) { | ||
const web = import("@connectrpc/connect-web").then( | ||
({ createConnectTransport }) => { | ||
// The Connect Node client doesn't work on edge runtimes: https://github.com/bufbuild/connect-es/pull/589 | ||
// so set the transport using connect-web. The interceptor is required for it work in the edge runtime. | ||
return createConnectTransport({ | ||
baseUrl, | ||
interceptors: [ | ||
/** | ||
* Ensures redirects are followed to properly support the Next.js/Vercel Edge | ||
* Runtime. | ||
* @see | ||
* https://github.com/connectrpc/connect-es/issues/749#issuecomment-1693507516 | ||
*/ | ||
(next) => (req) => { | ||
req.init.redirect = "follow"; | ||
return next(req); | ||
}, | ||
], | ||
fetch, | ||
}); | ||
}, | ||
); | ||
const node = import("@connectrpc/connect-node").then( | ||
({ Http2SessionManager, createConnectTransport }) => { | ||
// We create our own session manager so we can attempt to pre-connect | ||
const sessionManager = new Http2SessionManager(baseUrl, { | ||
// AWS Global Accelerator doesn't support PING so we use a very high idle | ||
// timeout. Ref: | ||
// https://docs.aws.amazon.com/global-accelerator/latest/dg/introduction-how-it-works.html#about-idle-timeout | ||
idleConnectionTimeoutMs: 340 * 1000, | ||
}); | ||
|
||
// We ignore the promise result because this is an optimistic pre-connect | ||
sessionManager.connect(); | ||
|
||
return createConnectTransport({ | ||
baseUrl, | ||
httpVersion: "2", | ||
sessionManager, | ||
}); | ||
}, | ||
); | ||
|
||
return { | ||
async unary( | ||
...args: [any, any, any, any, any, any, any] | ||
): Promise<unknown> { | ||
let client; | ||
try { | ||
client = await node; | ||
} catch { | ||
client = await web; | ||
} | ||
|
||
return client.unary(...args); | ||
}, | ||
async stream( | ||
...args: [any, any, any, any, any, any, any] | ||
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. Same as above |
||
): Promise<unknown> { | ||
let client; | ||
try { | ||
client = await node; | ||
} catch { | ||
client = await web; | ||
} | ||
|
||
return client.stream(...args); | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
{ | ||
"extends": "@arcjet/tsconfig/base", | ||
"include": ["bun.ts", "edge-light.ts", "index.ts"] | ||
"include": ["bun.ts", "edge-light.ts", "index.ts", "svelte.ts"] | ||
} |
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.
Can we get some concrete types for these?
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 believe these types come from connectrpc directly and we don't want to add another dependency to get the types. There may be a way to use utility types to pull them out but I'm not sure.