Skip to content

Commit cfbd7f6

Browse files
author
James Bray
committed
Fix multi-value cookies
1 parent a177e02 commit cfbd7f6

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const config = {
2626

2727
export default config;
2828
```
29-
Copy `serverless.yml` from the root of this repo to the root of your project
29+
Copy `serverless.yml` from the root of this repo to the root of your project, make sure to change the service name in `serverless.yml`.
3030

3131
After building your app run `sls deploy` to deploy code to AWS using the build tool [serverless](https://www.serverless.com/).
3232

package-lock.json

+14-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yarbsemaj/adapter-lambda",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"license": "MIT",
55
"description": "An adapter for [SvelteKit](https://kit.svelte.dev/) for AWS Lambda via Lambda Proxy and API Gateway. [Serverless](https://www.serverless.com/) deployment.",
66
"repository": {
@@ -32,6 +32,7 @@
3232
"esbuild": "0.10.1",
3333
"fs-extra": "^10.0.0",
3434
"serverless": "^3.0.0",
35-
"serverless-s3-deploy": "^0.10.1"
35+
"serverless-s3-deploy": "^0.10.1",
36+
"set-cookie-parser": "^2.5.0"
3637
}
3738
}

serverless.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins:
88

99
provider:
1010
name: aws
11-
runtime: nodejs14.x
11+
runtime: nodejs16.x
1212
lambdaHashingVersion: 20201221
1313
region: us-east-1 #Lambda@Edge must be deployed in us-east-1
1414
stage: ${opt:stage, 'dev'}

src/serverless.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Server } from '../index.js';
22
import { manifest } from '../manifest.js';
3+
import { splitCookiesString } from 'set-cookie-parser';
34

45
export async function handler(event) {
56
const app = new Server(manifest);
@@ -8,10 +9,10 @@ export async function handler(event) {
89
const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8';
910
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;
1011

11-
if(cookies){
12+
if (cookies) {
1213
headers['cookie'] = cookies.join('; ')
1314
}
14-
15+
1516
let rawURL = `https://${requestContext.domainName}${rawPath}${rawQueryString ? `?${rawQueryString}` : ''}`
1617

1718
//Render the app
@@ -25,18 +26,23 @@ export async function handler(event) {
2526
if (rendered) {
2627
const resp = {
2728
headers: {},
28-
multiValueHeaders: {},
29+
cookies: [],
2930
body: await rendered.text(),
3031
statusCode: rendered.status
3132
}
33+
3234
for (let k of rendered.headers.keys()) {
3335
let header = rendered.headers.get(k)
3436

35-
//For multivalue headers, join them
36-
if (header instanceof Array) {
37-
header = header.join(',')
37+
if (k == 'set-cookie') {
38+
resp.cookies = resp.cookies.concat(splitCookiesString(header));
39+
} else {
40+
//For multivalue headers, join them
41+
if (header instanceof Array) {
42+
header = header.join(',')
43+
}
44+
resp.headers[k] = header
3845
}
39-
resp.headers[k] = header
4046
}
4147
return resp
4248
}

0 commit comments

Comments
 (0)