Skip to content

Commit

Permalink
Updated to include the entire original request in platform
Browse files Browse the repository at this point in the history
  • Loading branch information
chrskerr committed Dec 15, 2022
1 parent f0a843f commit 42ffef8
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .changeset/fast-nails-attack.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@sveltejs/adapter-node': minor
---

Allow preserving specific keys from the original request object for use during SSR
Include the original request within the Platform object.
13 changes: 1 addition & 12 deletions packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export default {
// default options are shown
out: 'build',
precompress: false,
envPrefix: '',
preservedRequestKeys: []
envPrefix: ''
})
}
};
Expand Down Expand Up @@ -146,16 +145,6 @@ MY_CUSTOM_ORIGIN=https://my.site \
node build
```

### preservedRequestKeys

Allows preserving specific keys (such as custom keys set by Express middleware) from the original request object which was supplied to SvelteKit.

This is will allow access to these keys within server hooks and handlers.

```js
preservedRequestKeys: ['sessionID'],
```

## Custom server

The adapter creates two files in your build directory — `index.js` and `handler.js`. Running `index.js` — e.g. `node build`, if you use the default build directory — will start a server on the configured port.
Expand Down
6 changes: 6 additions & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ declare global {
const ENV_PREFIX: string;
}

declare namespace App {
export interface Platform {
originalReq: Request;
}
}

interface AdapterOptions {
out?: string;
precompress?: boolean;
Expand Down
5 changes: 2 additions & 3 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const files = fileURLToPath(new URL('./files', import.meta.url).href);

/** @type {import('.').default} */
export default function (opts = {}) {
const { out = 'build', precompress, envPrefix = '', preservedRequestKeys = [] } = opts;
const { out = 'build', precompress, envPrefix = '' } = opts;

return {
name: '@sveltejs/adapter-node',
Expand Down Expand Up @@ -72,8 +72,7 @@ export default function (opts = {}) {
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: `./server/index.js`,
ENV_PREFIX: JSON.stringify(envPrefix),
PRESERVED_REQUEST_KEYS: JSON.stringify(preservedRequestKeys)
ENV_PREFIX: JSON.stringify(envPrefix)
}
});
}
Expand Down
10 changes: 1 addition & 9 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ const body_size_limit = parseInt(env('BODY_SIZE_LIMIT', '524288'));

const dir = path.dirname(fileURLToPath(import.meta.url));

const preservedRequestKeys = JSON.parse('PRESERVED_REQUEST_KEYS');

/**
* @param {string} path
* @param {boolean} client
Expand All @@ -49,19 +47,13 @@ function serve(path, client = false) {
/** @type {import('polka').Middleware} */
const ssr = async (req, res) => {
let request;
const platform = {}

try {
request = await getRequest({
base: origin || get_origin(req.headers),
request: req,
bodySizeLimit: body_size_limit
});

for (const key of preservedRequestKeys) {
// @ts-expect-error
platform[key] = req[key];
}
} catch (err) {
res.statusCode = err.status || 400;
res.end('Invalid request body');
Expand All @@ -79,7 +71,7 @@ const ssr = async (req, res) => {
setResponse(
res,
await server.respond(request, {
platform,
platform: { originalReq: req },
getClientAddress: () => {
if (address_header) {
const value = /** @type {string} */ (req.headers[address_header]) || '';
Expand Down

0 comments on commit 42ffef8

Please sign in to comment.