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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[node] Add isomorphic functions #9947
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
82a720e
add nodejs-web runtime
Kikobeats 9ae470e
[node] serverless named exports
Kikobeats c4d7096
use server address instead of localhost
Kikobeats d40ac95
refactor: move into helpers-web
Kikobeats 706699e
add named export for Edge
Kikobeats 43df4a6
test: only for node18 or above
Kikobeats 8503358
Update packages/node/src/edge-functions/edge-handler-template.js
Kikobeats 1a2c62e
rename handler into userModule
Kikobeats b923bc6
fix: store error into a variable
Kikobeats db77f8b
Create young-rats-smoke.md
Kikobeats 0ae95e4
keep try/catch
Kikobeats 45d2da1
update test assertion
Kikobeats 0163cc8
Update packages/node/src/serverless-functions/serverless-handler.mts
EndangeredMassa b7e06c4
update handler not found message
EndangeredMassa 97ce530
make error messages look more like errors
EndangeredMassa 9ad8318
run prettier
EndangeredMassa File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,6 @@ | ||
--- | ||
"vercel": minor | ||
"@vercel/node": minor | ||
--- | ||
|
||
[node] Add isomorphic functions |
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
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
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,76 @@ | ||
import type { ServerResponse, IncomingMessage } from 'http'; | ||
import type { NodeHandler } from '@edge-runtime/node-utils'; | ||
import { buildToNodeHandler } from '@edge-runtime/node-utils'; | ||
|
||
class FetchEvent { | ||
public request: Request; | ||
public awaiting: Set<Promise<void>>; | ||
public response: Response | null; | ||
|
||
constructor(request: Request) { | ||
this.request = request; | ||
this.response = null; | ||
this.awaiting = new Set(); | ||
} | ||
|
||
respondWith(response: Response) { | ||
this.response = response; | ||
} | ||
|
||
waitUntil() { | ||
throw new Error('waitUntil is not implemented yet for Node.js'); | ||
} | ||
} | ||
|
||
const webHandlerToNodeHandler = buildToNodeHandler( | ||
{ | ||
Headers, | ||
ReadableStream, | ||
Request: class extends Request { | ||
constructor(input: RequestInfo | URL, init?: RequestInit | undefined) { | ||
super(input, addDuplexToInit(init)); | ||
} | ||
}, | ||
Uint8Array: Uint8Array, | ||
FetchEvent: FetchEvent, | ||
}, | ||
{ defaultOrigin: 'https://vercel.com' } | ||
); | ||
|
||
/** | ||
* When users export at least one HTTP handler, we will generate | ||
* a generic handler routing to the right method. If there is no | ||
* handler function exported returns null. | ||
*/ | ||
export function getWebExportsHandler(listener: any, methods: string[]) { | ||
const handlerByMethod: { [key: string]: NodeHandler } = {}; | ||
|
||
for (const key of methods) { | ||
handlerByMethod[key] = | ||
typeof listener[key] !== 'undefined' | ||
? webHandlerToNodeHandler(listener[key]) | ||
: defaultHttpHandler; | ||
} | ||
|
||
return (req: IncomingMessage, res: ServerResponse) => { | ||
const method = req.method ?? 'GET'; | ||
handlerByMethod[method](req, res); | ||
}; | ||
} | ||
|
||
/** | ||
* Add `duplex: 'half'` by default to all requests | ||
* https://github.com/vercel/edge-runtime/blob/bf167c418247a79d3941bfce4a5d43c37f512502/packages/primitives/src/primitives/fetch.js#L22-L26 | ||
* https://developer.chrome.com/articles/fetch-streaming-requests/#streaming-request-bodies | ||
*/ | ||
function addDuplexToInit(init: RequestInit | undefined) { | ||
if (typeof init === 'undefined' || typeof init === 'object') { | ||
return { duplex: 'half', ...init }; | ||
} | ||
return init; | ||
} | ||
|
||
function defaultHttpHandler(_: IncomingMessage, res: ServerResponse) { | ||
res.statusCode = 405; | ||
res.end(); | ||
} |
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 |
---|---|---|
|
@@ -59,7 +59,12 @@ export function entrypointToOutputPath( | |
} | ||
|
||
export function logError(error: Error) { | ||
console.error(error.message); | ||
let message = error.message; | ||
if (!message.startsWith('Error:')) { | ||
message = `Error: ${message}`; | ||
} | ||
console.error(message); | ||
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 is an improvement, but I realized we show errors differently between startup/runtime and serverless-node/edge. We should standardize those differences, but not in this PR. |
||
|
||
if (error.stack) { | ||
// only show the stack trace if debug is enabled | ||
// because it points to internals, not user code | ||
|
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,10 @@ | ||
/* global Response */ | ||
|
||
const baseUrl = ({ headers }) => | ||
`${headers.get('x-forwarded-proto')}://${headers.get('x-forwarded-host')}`; | ||
|
||
export function GET(request) { | ||
const { searchParams } = new URL(request.url, baseUrl(request)); | ||
const name = searchParams.get('name'); | ||
return new Response(`Greetings, ${name}`); | ||
} |
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.
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 don't understand how this TS error was fixed
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.
Upgrading
@edge-runtime/node-utils
fixed the issue I think because of this PR: vercel/edge-runtime#309