-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[feat] expose handler to allow use in custom server #2051
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
Merged
benmccann
merged 1 commit into
sveltejs:master
from
matths:feature/adapter-node-inject-existing-server-in-polka
Aug 27, 2021
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,22 @@ | ||
// TODO hardcoding the relative location makes this brittle | ||
// @ts-ignore | ||
import { init, render } from '../output/server/app.js'; | ||
// @ts-ignore | ||
import { path, host, port } from './env.js'; | ||
import { createServer } from './server'; | ||
|
||
init(); | ||
import { assetsMiddleware, kitMiddleware, prerenderedMiddleware } from './middlewares.js'; | ||
import compression from 'compression'; | ||
import polka from 'polka'; | ||
|
||
const instance = createServer({ render }); | ||
const server = polka().use( | ||
// https://github.com/lukeed/polka/issues/173 | ||
// @ts-ignore - nothing we can do about so just ignore it | ||
compression({ threshold: 0 }), | ||
assetsMiddleware, | ||
kitMiddleware, | ||
prerenderedMiddleware | ||
); | ||
|
||
const listenOpts = { path, host, port }; | ||
instance.listen(listenOpts, () => { | ||
|
||
server.listen(listenOpts, () => { | ||
console.log(`Listening on ${path ? path : host + ':' + port}`); | ||
}); | ||
|
||
export { instance }; | ||
export { server }; |
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,40 @@ | ||
import { getRawBody } from '@sveltejs/kit/node'; | ||
|
||
/** | ||
* @return {import('polka').Middleware} | ||
*/ | ||
// TODO: type render function from @sveltejs/kit/adapter | ||
// @ts-ignore | ||
export function create_kit_middleware({ render }) { | ||
return async (req, res) => { | ||
const parsed = new URL(req.url || '', 'http://localhost'); | ||
|
||
let body; | ||
|
||
try { | ||
body = await getRawBody(req); | ||
} catch (err) { | ||
res.statusCode = err.status || 400; | ||
return res.end(err.reason || 'Invalid request body'); | ||
} | ||
|
||
const rendered = await render({ | ||
method: req.method, | ||
headers: req.headers, // TODO: what about repeated headers, i.e. string[] | ||
path: parsed.pathname, | ||
query: parsed.searchParams, | ||
rawBody: body | ||
}); | ||
|
||
if (rendered) { | ||
res.writeHead(rendered.status, rendered.headers); | ||
if (rendered.body) { | ||
res.write(rendered.body); | ||
} | ||
res.end(); | ||
} else { | ||
res.statusCode = 404; | ||
res.end('Not found'); | ||
} | ||
}; | ||
} |
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,48 @@ | ||
// TODO hardcoding the relative location makes this brittle | ||
// Also, we need most of the logic in another file for testing because | ||
// ../output/server/app.js doesn't exist when we run the tests | ||
// @ts-ignore | ||
import { init, render } from '../output/server/app.js'; | ||
import { create_kit_middleware } from './kit-middleware.js'; | ||
|
||
import fs from 'fs'; | ||
import { dirname, join } from 'path'; | ||
import sirv from 'sirv'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
// App is a dynamic file built from the application layer. | ||
|
||
const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
/** @type {import('polka').Middleware} */ | ||
const noop_handler = (_req, _res, next) => next(); | ||
const paths = { | ||
assets: join(__dirname, '/assets'), | ||
prerendered: join(__dirname, '/prerendered') | ||
}; | ||
|
||
export const prerenderedMiddleware = fs.existsSync(paths.prerendered) | ||
? sirv(paths.prerendered, { | ||
etag: true, | ||
maxAge: 0, | ||
gzip: true, | ||
brotli: true | ||
}) | ||
: noop_handler; | ||
|
||
export const assetsMiddleware = fs.existsSync(paths.assets) | ||
? sirv(paths.assets, { | ||
setHeaders: (res, pathname) => { | ||
// @ts-expect-error - dynamically replaced with define | ||
if (pathname.startsWith(/* eslint-disable-line no-undef */ APP_DIR)) { | ||
res.setHeader('cache-control', 'public, max-age=31536000, immutable'); | ||
} | ||
}, | ||
gzip: true, | ||
brotli: true | ||
}) | ||
: noop_handler; | ||
|
||
export const kitMiddleware = (function () { | ||
init(); | ||
return create_kit_middleware({ render }); | ||
})(); |
This file was deleted.
Oops, something went wrong.
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
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.
Other questions the docs should answer: Where do you put this file? Is there some option you need to specify to make it use this vs the default?
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.
Not sure what you mean. Using it this way is kind of advanced stuff for experienced node users. So I am not sure how to verbose to document. I'm not a native speaker, so feel free to come up with better documentations.
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 mean if you want to create your own server using the code below does it matter what the file path or name is?