Skip to content

Commit

Permalink
Allow preserving express request keys within adapter node
Browse files Browse the repository at this point in the history
  • Loading branch information
chrskerr committed Dec 14, 2022
1 parent 7ba7ded commit 85a3998
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-nails-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-node': minor
---

Allow preserving specific keys from the original request object for use during SSR
13 changes: 12 additions & 1 deletion packages/adapter-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ export default {
// default options are shown
out: 'build',
precompress: false,
envPrefix: ''
envPrefix: '',
preservedRequestKeys: [],
})
}
};
Expand Down Expand Up @@ -145,6 +146,16 @@ 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
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface AdapterOptions {
out?: string;
precompress?: boolean;
envPrefix?: string;
preservedRequestKeys?: string[];
}

export default function plugin(options?: AdapterOptions): Adapter;
5 changes: 3 additions & 2 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 = '' } = opts;
const { out = 'build', precompress, envPrefix = '', preservedRequestKeys = [] } = opts;

return {
name: '@sveltejs/adapter-node',
Expand Down Expand Up @@ -72,7 +72,8 @@ export default function (opts = {}) {
HANDLER: './handler.js',
MANIFEST: './server/manifest.js',
SERVER: `./server/index.js`,
ENV_PREFIX: JSON.stringify(envPrefix)
ENV_PREFIX: JSON.stringify(envPrefix),
PRESERVED_REQUEST_KEYS: JSON.stringify(preservedRequestKeys)
}
});
}
Expand Down
7 changes: 7 additions & 0 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ 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 Down Expand Up @@ -54,6 +56,11 @@ const ssr = async (req, res) => {
request: req,
bodySizeLimit: body_size_limit
});

for (const key of preservedRequestKeys) {
// @ts-expect-error
request[key] = req[key];
}
} catch (err) {
res.statusCode = err.status || 400;
res.end('Invalid request body');
Expand Down

0 comments on commit 85a3998

Please sign in to comment.