-
Notifications
You must be signed in to change notification settings - Fork 2
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
add module service adapter, unify route handling to handleRequestWithEdgeSpec
#23
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3306ccd
F
mxsdev efadae4
unneeded import
mxsdev 772110a
EdgeSpec -> EdgeSpecRouteBundle
mxsdev ef308a9
rename create server from route map
mxsdev 2859825
refactor hadnleRequestWithEdgeSpec
mxsdev 003944b
Merge remote-tracking branch 'upstream/main' into mxsdev/module-servi…
mxsdev b2031d9
fixes
mxsdev f47e53d
better EdgeSpecOptions
mxsdev 2c547a9
rename file
mxsdev 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,88 @@ | ||
import { | ||
type EdgeSpecAdapter, | ||
type EdgeSpecRouteBundle, | ||
handleRequestWithEdgeSpec, | ||
} from "src/types/edge-spec" | ||
import { EdgeSpecRequest, EdgeSpecRouteFn } from "src/types/web-handler" | ||
|
||
export interface ModuleServiceOptions { | ||
routeParam?: string | ||
handleRouteParamNotFound?: EdgeSpecRouteFn | ||
allowMatchingOnAnyCatchAllRouteParam?: boolean | ||
} | ||
|
||
export type ModuleService = (options?: ModuleServiceOptions) => EdgeSpecRouteFn | ||
|
||
export const createModuleService: EdgeSpecAdapter<[], ModuleService> = ( | ||
moduleServiceEdgeSpec | ||
) => { | ||
return (options) => async (request) => { | ||
// cascade options down the edge spec chain | ||
const edgeSpec: EdgeSpecRouteBundle = { | ||
...request.edgeSpec, | ||
...moduleServiceEdgeSpec, | ||
...(options?.handleRouteParamNotFound && { | ||
handleModuleServiceRouteNotFound: options?.handleRouteParamNotFound, | ||
}), | ||
} | ||
|
||
const pathnameOverrideResult = getPathnameOverride(request, options ?? {}) | ||
|
||
if ("failed" in pathnameOverrideResult) { | ||
return await pathnameOverrideResult.failed(request) | ||
} | ||
|
||
const response = await handleRequestWithEdgeSpec( | ||
edgeSpec, | ||
pathnameOverrideResult.pathnameOverride | ||
)(request) | ||
|
||
return response | ||
} | ||
} | ||
|
||
function getPathnameOverride( | ||
request: EdgeSpecRequest, | ||
options: ModuleServiceOptions | ||
): | ||
| { | ||
pathnameOverride: string | undefined | ||
} | ||
| { | ||
failed: EdgeSpecRouteFn | ||
} { | ||
const { | ||
routeParam, | ||
handleRouteParamNotFound = request.edgeSpec | ||
.handleModuleServiceRouteNotFound ?? | ||
(() => { | ||
throw new Error("Module service route not found!") | ||
}), | ||
allowMatchingOnAnyCatchAllRouteParam = true, | ||
} = options | ||
|
||
let paths: string[] | undefined | ||
|
||
if (routeParam) { | ||
const candidate = request.pathParams?.[routeParam] | ||
|
||
if (candidate && Array.isArray(candidate)) { | ||
paths = candidate | ||
} | ||
} | ||
|
||
if (!paths && allowMatchingOnAnyCatchAllRouteParam) { | ||
for (const routes of Object.values(request.pathParams ?? {})) { | ||
if (Array.isArray(routes)) { | ||
paths = routes | ||
break | ||
} | ||
} | ||
} | ||
|
||
if (!paths) { | ||
return { failed: handleRouteParamNotFound } | ||
} | ||
|
||
return { pathnameOverride: "/" + paths.join("/") } | ||
} |
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 |
---|---|---|
@@ -1,17 +1,13 @@ | ||
import { EdgeSpecAdapter } from "src/types/edge-spec" | ||
import { EdgeSpecAdapter, handleRequestWithEdgeSpec } from "src/types/edge-spec" | ||
import { EdgeSpecFetchEvent } from "src/types/web-handler" | ||
|
||
export const addFetchListener: EdgeSpecAdapter = (edgeSpec) => { | ||
addEventListener("fetch", async (event) => { | ||
// TODO: find a better way to cast this | ||
const fetchEvent = event as unknown as EdgeSpecFetchEvent | ||
|
||
const { matchedRoute, routeParams } = edgeSpec.routeMatcher( | ||
new URL(fetchEvent.request.url).pathname | ||
fetchEvent.respondWith( | ||
await handleRequestWithEdgeSpec(edgeSpec)(fetchEvent.request) | ||
) | ||
const handler = edgeSpec.routeMapWithHandlers[matchedRoute] | ||
fetchEvent.request.pathParams = routeParams | ||
|
||
fetchEvent.respondWith(await handler(fetchEvent.request)) | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./types/edge-spec.js" | ||
export * from "./codegen/generate-module-code.js" | ||
export * from "./create-with-edge-spec.js" |
mxsdev marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
---|---|---|
@@ -1,18 +1,67 @@ | ||
import type { EdgeSpecRouteFn, EdgeSpecRouteParams } from "./web-handler" | ||
import { | ||
createEdgeSpecRequest, | ||
type EdgeSpecRouteFn, | ||
type EdgeSpecRouteParams, | ||
} from "./web-handler.js" | ||
|
||
export type EdgeSpecRouteMatcher = (pathname: string) => { | ||
matchedRoute: string | ||
routeParams: EdgeSpecRouteParams | ||
} | ||
import type { ReadonlyDeep } from "type-fest" | ||
|
||
export type EdgeSpecRouteMatcher = (pathname: string) => | ||
| { | ||
matchedRoute: string | ||
routeParams: EdgeSpecRouteParams | ||
} | ||
| undefined | ||
| null | ||
|
||
export interface EdgeSpec { | ||
export type EdgeSpecRouteMap = Record<string, EdgeSpecRouteFn> | ||
|
||
export interface EdgeSpecOptions { | ||
routeMatcher: EdgeSpecRouteMatcher | ||
routeMapWithHandlers: { | ||
[route: string]: EdgeSpecRouteFn | ||
} | ||
routeMapWithHandlers: EdgeSpecRouteMap | ||
|
||
handleModuleServiceRouteNotFound?: EdgeSpecRouteFn | ||
handle404?: EdgeSpecRouteFn | ||
} | ||
|
||
export type EdgeSpecAdapter<ReturnValue = void> = ( | ||
edgeSpec: EdgeSpec, | ||
port?: number | ||
) => ReturnValue | ||
// make this deeply immutable to force usage through helper functions | ||
export type EdgeSpecRouteBundle = ReadonlyDeep<EdgeSpecOptions> | ||
|
||
mxsdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
export type EdgeSpecAdapter< | ||
Options extends Array<unknown> = [], | ||
ReturnValue = void, | ||
> = (edgeSpec: EdgeSpecRouteBundle, ...options: Options) => ReturnValue | ||
|
||
export function handleRequestWithEdgeSpec( | ||
edgeSpec: EdgeSpecRouteBundle, | ||
pathnameOverride?: string | ||
): (request: Request) => Promise<Response> { | ||
return async (request: Request) => { | ||
const { | ||
routeMatcher, | ||
routeMapWithHandlers, | ||
handle404 = () => | ||
new Response("Not found", { | ||
status: 404, | ||
}), | ||
} = edgeSpec | ||
|
||
const pathname = pathnameOverride ?? new URL(request.url).pathname | ||
const { matchedRoute, routeParams } = routeMatcher(pathname) ?? {} | ||
|
||
const routeFn = matchedRoute && routeMapWithHandlers[matchedRoute] | ||
|
||
const edgeSpecRequest = createEdgeSpecRequest(request, { | ||
edgeSpec, | ||
pathParams: routeParams, | ||
}) | ||
|
||
if (!routeFn) { | ||
return await handle404(edgeSpecRequest) | ||
} | ||
|
||
const response: Response = await routeFn(edgeSpecRequest) | ||
|
||
return response | ||
} | ||
} |
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 kinda like
but don't want to bikeshed too much
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.
@seveibar
will deal with this in follow-up discussions/PRs/RFCs