-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
refactor: add pipeline concept #8020
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Environment } from '../render'; | ||
import type { EndpointCallResult } from '../endpoint/index.js'; | ||
import mime from 'mime'; | ||
import { attachCookiesToResponse } from '../cookies/index.js'; | ||
import { Pipeline } from '../pipeline.js'; | ||
|
||
/** | ||
* Thrown when an endpoint contains a response with the header "X-Astro-Response" === 'Not-Found' | ||
*/ | ||
export class EndpointNotFoundError extends Error { | ||
originalResponse: Response; | ||
constructor(originalResponse: Response) { | ||
super(); | ||
this.originalResponse = originalResponse; | ||
} | ||
} | ||
|
||
export class SSRRoutePipeline extends Pipeline { | ||
encoder = new TextEncoder(); | ||
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. Also maybe this could be private too. |
||
|
||
constructor(env: Environment) { | ||
super(env); | ||
this.setEndpointHandler(this.#ssrEndpointHandler); | ||
} | ||
|
||
// This function is responsible for handling the result coming from an endpoint. | ||
async #ssrEndpointHandler(request: Request, response: EndpointCallResult): Promise<Response> { | ||
Comment on lines
+23
to
+27
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. Small nit: Is 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. Technically, no. Not all the future pipelines will have to handle endpoints. For example: a testing pipeline will need to be used only when needed, an Astro container won't need it (I presume) |
||
if (response.type === 'response') { | ||
if (response.response.headers.get('X-Astro-Response') === 'Not-Found') { | ||
throw new EndpointNotFoundError(response.response); | ||
} | ||
return response.response; | ||
} else { | ||
const url = new URL(request.url); | ||
const headers = new Headers(); | ||
const mimeType = mime.getType(url.pathname); | ||
if (mimeType) { | ||
headers.set('Content-Type', `${mimeType};charset=utf-8`); | ||
} else { | ||
headers.set('Content-Type', 'text/plain;charset=utf-8'); | ||
} | ||
const bytes = | ||
response.encoding !== 'binary' ? this.encoder.encode(response.body) : response.body; | ||
headers.set('Content-Length', bytes.byteLength.toString()); | ||
|
||
const newResponse = new Response(bytes, { | ||
status: 200, | ||
headers, | ||
}); | ||
attachCookiesToResponse(newResponse, response.cookies); | ||
return newResponse; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { AstroCookies } from './cookies.js'; | ||
export { attachToResponse, getSetCookiesFromResponse } from './response.js'; | ||
export { attachCookiesToResponse, getSetCookiesFromResponse } from './response.js'; |
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.
wow, love all of this deletion
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.
Agreed, all this cleanup in the
App
is great!