Skip to content

Commit 3d4ef5c

Browse files
authored
Merge pull request #5 from lucacasonato/fix-deno-lambda-48
Fix deno lambda 48
2 parents 5f55ecd + aa8ee6d commit 3d4ef5c

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ If you're unfamiliar with now runtimes, please read the [runtime docs](https://z
1313
{
1414
"functions": {
1515
"api/**/*.ts": {
16-
"runtime": "now-deno@0.1.0"
16+
"runtime": "now-deno@0.2.0"
1717
}
1818
}
1919
}
2020
```
2121

2222
```ts
2323
// api/hello.ts
24-
import { Context, Event } from 'https://deno.land/x/lambda/mod.ts';
24+
import { APIGatewayEventRequestContext, APIGatewayEvent } from 'https://deno.land/x/lambda/mod.ts';
2525

26-
export async function handler(event: Event, context: Context) {
26+
export async function handler(event: APIGatewayEvent, context: APIGatewayEventRequestContext) {
2727
return {
2828
statusCode: 200,
2929
body: `Welcome to deno ${Deno.version.deno} 🦕`,

example/api/version.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import { Context, Event, } from "https://deno.land/x/lambda/mod.ts";
1+
import {
2+
APIGatewayEventRequestContext,
3+
APIGatewayEvent,
4+
} from 'https://deno.land/x/lambda/mod.ts';
25

3-
export async function handler(event: Event, context: Context) {
6+
export async function handler(
7+
event: APIGatewayEvent,
8+
context: APIGatewayEventRequestContext
9+
) {
410
return {
511
statusCode: 200,
612
body: `Welcome to deno ${Deno.version.deno} 🦕`,
713
headers: {
8-
"Content-Type": "text/html; charset=utf-8"
9-
}
14+
'Content-Type': 'text/html; charset=utf-8',
15+
},
1016
};
1117
}

example/now.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"functions": {
33
"api/**/*.ts": {
4-
"runtime": "now-deno@0.1.0",
5-
"memory": 128
4+
"runtime": "now-deno@0.2.0",
5+
"memory": 128
66
}
77
}
88
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "now-deno",
33
"description": "Community based builder for using deno on the now/zeit platform",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"main": "./dist/index",
77
"homepage": "https://github.com/lucacasonato/now-deno",

src/index.ts

+38-5
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,34 @@ async function buildDenoLambda(
5050
const extname = path.extname(entrypointPath);
5151
const binName = path.basename(entrypointPath).replace(extname, '');
5252
const binPath = path.join(workPath, binName) + '.bundle.js';
53+
const denoDir = path.join(workPath, 'layer', '.deno_dir');
5354

5455
const { debug } = config;
5556
console.log('running `deno bundle`...');
5657
try {
5758
await execa(
58-
path.join(workPath, 'layer', 'amz-deno'),
59+
path.join(workPath, 'layer', 'bin', 'deno'),
5960
['bundle', entrypointPath, binPath].concat(debug ? ['-L debug'] : []),
6061
{
6162
env: {
62-
DENO_DIR: path.join(workPath, 'layer', '.deno_dir'),
63+
DENO_DIR: denoDir,
6364
},
6465
cwd: entrypointDirname,
6566
stdio: 'inherit',
6667
}
6768
);
6869
} catch (err) {
69-
console.error('failed to `deno bundle`');
70+
console.error('failed to `deno bundle`:' + err);
7071
throw err;
7172
}
7273

74+
const denoDirFiles = await getDenoDirFiles(denoDir);
75+
7376
const lambda = await createLambda({
7477
files: {
7578
...extraFiles,
7679
...layerFiles,
80+
...denoDirFiles,
7781
[binName + '.bundle.js']: new FileFsRef({
7882
mode: 0o755,
7983
fsPath: binPath,
@@ -83,6 +87,7 @@ async function buildDenoLambda(
8387
runtime: 'provided',
8488
environment: {
8589
HANDLER_EXT: 'bundle.js',
90+
PATH: process.env.PATH + ':./bin',
8691
},
8792
});
8893

@@ -95,6 +100,34 @@ async function buildDenoLambda(
95100
};
96101
}
97102

103+
async function walk(dir: string): Promise<string[]> {
104+
const f = await fs.readdir(dir);
105+
const files = await Promise.all(
106+
f.map(async file => {
107+
const filePath = path.join(dir, file);
108+
const stats = await fs.stat(filePath);
109+
if (stats.isDirectory()) return walk(filePath);
110+
else if (stats.isFile()) return filePath;
111+
throw 'File not dir or file: ' + filePath;
112+
})
113+
);
114+
115+
return files.flat();
116+
}
117+
118+
async function getDenoDirFiles(denoDirPath: string): Promise<Files> {
119+
const files: Files = {};
120+
121+
const dir = await walk(denoDirPath);
122+
123+
dir.forEach(file => {
124+
const f = path.join('.deno_dir', file.replace(denoDirPath + '/', ''));
125+
files[f] = new FileFsRef({ fsPath: file, mode: 0o755 });
126+
});
127+
128+
return files;
129+
}
130+
98131
async function getDenoLambdaLayer({ workPath }: BuildOptions): Promise<Files> {
99132
const zipPath = path.join(workPath, 'deno-lambda-layer.zip');
100133
try {
@@ -130,9 +163,9 @@ async function getDenoLambdaLayer({ workPath }: BuildOptions): Promise<Files> {
130163
mode: 0o755,
131164
fsPath: path.join(layerDir, 'bootstrap'),
132165
}),
133-
'amz-deno': new FileFsRef({
166+
'bin/deno': new FileFsRef({
134167
mode: 0o755,
135-
fsPath: path.join(layerDir, 'amz-deno'),
168+
fsPath: path.join(layerDir, 'bin/deno'),
136169
}),
137170
};
138171
}

0 commit comments

Comments
 (0)