Skip to content

Commit e2b023e

Browse files
author
James Bray
committed
first commit
0 parents  commit e2b023e

File tree

9 files changed

+198
-0
lines changed

9 files changed

+198
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
/files

.npmignore

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) James Bray <yarbsemaj@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# adapter-lambda for SvelteKit
2+
3+
An adapter to build a [SvelteKit](https://kit.svelte.dev/) app into a lambda ready for deployment with lambda proxy.
4+
```
5+
npm install --save-dev @yarbsemaj/adapter-lambda
6+
```
7+
8+
## Usage
9+
10+
In your `svelte.config.cjs` configure the adapter as bellow;
11+
12+
```js
13+
import preprocess from 'svelte-preprocess';
14+
import serverless from '@yarbsemaj/adapter-lambda';
15+
16+
/** @type {import('@sveltejs/kit').Config} */
17+
const config = {
18+
preprocess: preprocess(),
19+
20+
kit: {
21+
target: '#svelte',
22+
adapter: serverless(),
23+
},
24+
25+
};
26+
```
27+
## A note on static assets
28+
Precompiled pages, client and static resources should be served independently of your dynamic content. One solution to this could be to upload the `build/assets/` directory to S3 and using its static site hosting functionality. Then, by using a CDN like CloudFront, requests could be routed to the correct origin.

index.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
const { copyFileSync, unlinkSync} = require('fs');
2+
const { join } = require('path');
3+
4+
const esbuild = require('esbuild');
5+
6+
/**
7+
* @param {{
8+
* out?: string;
9+
* }} options
10+
*/
11+
module.exports = function ({ out = 'build' } = {}) {
12+
/** @type {import('@sveltejs/kit').Adapter} */
13+
const adapter = {
14+
name: 'adapter-serverless',
15+
16+
async adapt(builder) {
17+
18+
const static_directory = join(out, 'assets');
19+
const server_directory = join(out, 'server');
20+
21+
builder.utils.log.minor('Copying assets');
22+
builder.utils.copy_client_files(static_directory);
23+
builder.utils.copy_static_files(static_directory);
24+
25+
26+
builder.utils.log.minor('Copying server');
27+
builder.utils.copy_server_files(out);
28+
copyFileSync(`${__dirname}/files/serverless.js`, `${server_directory}/_serverless.js`);
29+
copyFileSync(`${__dirname}/files/shims.js`, `${server_directory}/shims.js`);
30+
31+
32+
builder.utils.log.minor('Building lambda');
33+
esbuild.buildSync({
34+
entryPoints: [`${server_directory}/_serverless.js`],
35+
outfile: `${server_directory}/serverless.js`,
36+
inject: [join(`${server_directory}/shims.js`)],
37+
format: 'cjs',
38+
bundle: true,
39+
platform: 'node',
40+
});
41+
42+
builder.utils.log.minor('Prerendering static pages');
43+
await builder.utils.prerender({
44+
dest: `${static_directory}`,
45+
});
46+
47+
builder.utils.log.minor('Cleanup');
48+
unlinkSync(`${server_directory}/_serverless.js`);
49+
unlinkSync(`${out}/app.js`);
50+
},
51+
};
52+
53+
return adapter;
54+
};

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@yarbsemaj/adapter-serverless-lambda",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"description": "An adapter for [SvelteKit](https://kit.svelte.dev/) for AWS Lambda via Lambda Proxy and API Gateway. [Serverless](https://www.serverless.com/) deployment.",
6+
"repository": {
7+
"type": "git",
8+
"url": "git+https://github.com/yarbsemaj/adapter-serverless-lambda.git"
9+
},
10+
"keywords": [
11+
"sveltekit",
12+
"lambda",
13+
"api-gateway",
14+
"AWS",
15+
"adapter"
16+
],
17+
"author": "James Bray <yarbsemaj.com> (https://yarbsemaj.com)",
18+
"bugs": {
19+
"url": "https://github.com/yarbsemaj/adapter-serverless-lambda/issues"
20+
},
21+
"homepage": "https://github.com/yarbsemaj/adapter-serverless-lambda#readme",
22+
"main": "index.js",
23+
"files": [
24+
"files"
25+
],
26+
"scripts": {
27+
"build": "esbuild src/serverless.js --bundle --format=esm --platform=node --external:'../app.*' --outfile=files/serverless.js && cp src/shims.js files/shims.js ",
28+
"prepare": "npm run build"
29+
},
30+
"devDependencies": {},
31+
"dependencies": {
32+
"esbuild": "0.10.1"
33+
}
34+
}

src/serverless.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { URLSearchParams } from 'url';
2+
import * as app from '../app.js';
3+
4+
app.init();
5+
6+
export async function handler(event) {
7+
const { path, headers, rawQuery, body, isBase64Encoded } = event;
8+
9+
const encoding = isBase64Encoded ? 'base64' : headers['content-encoding'] || 'utf-8';
10+
const rawBody = typeof body === 'string' ? Buffer.from(body, encoding) : body;
11+
12+
const query = new URLSearchParams(rawQuery);
13+
14+
const rendered = await app.render({
15+
host: event.requestContext.domainName,
16+
method: event.httpMethod,
17+
rawBody,
18+
headers,
19+
query,
20+
path,
21+
})
22+
23+
if (rendered) {
24+
const resp = {
25+
headers: {},
26+
multiValueHeaders: {},
27+
body: rendered.body,
28+
statusCode: rendered.status
29+
}
30+
Object.keys(rendered.headers).forEach(k => {
31+
const v = rendered.headers[k]
32+
if (v instanceof Array) {
33+
resp.multiValueHeaders[k] = v
34+
} else {
35+
resp.headers[k] = v
36+
}
37+
})
38+
return resp
39+
}
40+
return {
41+
statusCode: 404,
42+
body: 'Not found.'
43+
}
44+
}

src/shims.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';

0 commit comments

Comments
 (0)