Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from hayd/0.25.0
Browse files Browse the repository at this point in the history
Bump version to 0.25.0
  • Loading branch information
hayd authored Nov 27, 2019
2 parents 73c899f + b508149 commit 7b80576
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
gzip -9 amz-deno
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/') && github.repository == 'hayd/deno_lambda'
if: startsWith(github.ref, 'refs/tags/') && github.repository == 'hayd/deno-lambda'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down
2 changes: 0 additions & 2 deletions runtime/artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ set -euo pipefail

# populate the cache from deno.land/x/lambda so we use the current files
mkdir -p .deno_dir/deps/https/deno.land/x/lambda/
cp bootstrap.ts .deno_dir/deps/https/deno.land/x/lambda/bootstrap.ts
cp mod.ts .deno_dir/deps/https/deno.land/x/lambda/mod.ts
cp runtime.ts .deno_dir/deps/https/deno.land/x/lambda/runtime.ts
# this shouldn't require a fetch to deno.land
DENO_DIR=.deno_dir ./amz-deno fetch hello.ts

Expand Down
25 changes: 2 additions & 23 deletions runtime/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ DENO_DIR=${DENO_DIR-'.deno_dir'}
HANDLER_EXT=${HANDLER_EXT-'ts'}
HANDLER_FILE=${HANDLER_FILE}.${HANDLER_EXT}

# If this ENV is 'true' then we let deno call the invocation loop.
DENO_LAMBDA=${DENO_LAMBDA-false}

# If we fail prior to the handler loop we post an init error.
function error {
echo "error:" $1
Expand Down Expand Up @@ -46,18 +43,9 @@ function investigate {
DENO_DIR=/tmp/deno_dir NO_COLOR=true $SCRIPT_DIR/amz-deno fetch $LAMBDA_TASK_ROOT/$HANDLER_FILE \
|| error "unable to compile $HANDLER_FILE"

if [[ $DENO_LAMBDA = 'false' ]];
then echo "
import { $HANDLER_NAME } from '$LAMBDA_TASK_ROOT/$HANDLER_FILE';
" > /tmp/runtime-test.js
echo "import { $HANDLER_NAME } from '$LAMBDA_TASK_ROOT/$HANDLER_FILE';" > /tmp/runtime-test.js
DENO_DIR=/tmp/deno_dir NO_COLOR=true $SCRIPT_DIR/amz-deno fetch /tmp/runtime-test.js 2> /dev/null \
|| error "$HANDLER_FILE must export a function named '$HANDLER_NAME'";
fi;

if [[ $DENO_LAMBDA = 'true' ]] ;
# if using bundle we could see if an ENV variable is set. Or something to confirm we're using the lib.
then error "unhandled bundle error";
fi;

# perhaps a bug in bootstrap? Should we always exit 1?
}
Expand Down Expand Up @@ -142,14 +130,5 @@ cp -R $LAMBDA_TASK_ROOT/$DENO_DIR/gen /tmp/deno_dir/gen &> /dev/null \
&& cp -R $LAMBDA_TASK_ROOT/$DENO_DIR/LAMBDA_TASK_ROOT/ /tmp/deno_dir/gen/file$LAMBDA_TASK_ROOT &> /dev/null \
|| echo "warn: unable to import '$DENO_DIR/' as DENO_DIR"

# If DENO_LAMBDA is misused, not true or false, we exit.
if ! ( [[ $DENO_LAMBDA = 'true' ]] || [[ $DENO_LAMBDA = 'false' ]] ) ;
then error "if set DENO_LAMBDA must be either true or false";
fi;

if [[ $DENO_LAMBDA = 'false' ]];
then DENO_DIR=/tmp/deno_dir NO_COLOR=true $SCRIPT_DIR/amz-deno run --allow-net --allow-read --allow-env /tmp/runtime.js \
|| investigate;
else DENO_DIR=/tmp/deno_dir NO_COLOR=true $SCRIPT_DIR/amz-deno run --allow-net --allow-read --allow-env $LAMBDA_TASK_ROOT/$HANDLER_FILE \
DENO_DIR=/tmp/deno_dir NO_COLOR=true $SCRIPT_DIR/amz-deno run --allow-net --allow-read --allow-env /tmp/runtime.js \
|| investigate;
fi;
113 changes: 0 additions & 113 deletions runtime/bootstrap.ts

This file was deleted.

17 changes: 0 additions & 17 deletions runtime/runtime.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Note: This must be built in the .. context

FROM hayd/deno:amazonlinux1-0.24.0
FROM hayd/deno:amazonlinux1-0.25.0
# This is the runtime used by AWS Lambda
# plus a compatible deno executable /bin/deno.
# https://github.com/hayd/deno_docker
Expand Down
5 changes: 1 addition & 4 deletions tests/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
import { handler } from "./hello.ts";
import { lambda } from "https://deno.land/x/lambda/runtime.ts";

lambda.handler = handler;
export { handler } from "./hello.ts";
6 changes: 3 additions & 3 deletions tests/deps.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { assertEquals } from "https://deno.land/std@v0.24.0/testing/asserts.ts";
export { serve } from "https://deno.land/std@v0.24.0/http/server.ts";
export { runIfMain, test } from "https://deno.land/std@v0.24.0/testing/mod.ts";
export { assertEquals } from "https://deno.land/std@v0.25.0/testing/asserts.ts";
export { serve } from "https://deno.land/std@v0.25.0/http/server.ts";
export { runIfMain, test } from "https://deno.land/std@v0.25.0/testing/mod.ts";
8 changes: 8 additions & 0 deletions tests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ export async function log(event: Event, context: Context) {
console.log(message);
return { message };
}

export async function noArgs() {
return {};
}

export async function wrongArgs(a: number, b: number, c: number) {
return { result: a * b * c };
}
120 changes: 68 additions & 52 deletions tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,82 @@ import { serve } from "./deps.ts";
const encode = new TextEncoder().encode;
const dec = new TextDecoder();

const testJson = JSON.parse(dec.decode(Deno.readFileSync(Deno.args[1])));

// start the server prior to running bootstrap.
const PORT = 1993;
const s = serve(`0.0.0.0:${PORT}`);

const bootstrap = Deno.readDirSync("/var/task/")
.map(x => x.name)
.includes("bootstrap")
? "/var/task/bootstrap"
: "/opt/bootstrap";
function bootstrap(testJson) {
const bootstrapScript = Deno.readDirSync("/var/task/")
.map(x => x.name)
.includes("bootstrap")
? "/var/task/bootstrap"
: "/opt/bootstrap";

return Deno.run({
args: [bootstrapScript],
stdout: "piped",
stderr: "piped", // comment this out to debug
env: testJson.env,
cwd: "/var/task"
});
}

const p = Deno.run({
args: [bootstrap],
stdout: "piped",
stderr: "piped", // comment this out to debug
env: testJson.env,
cwd: "/var/task"
});
// is this in cli/std? r: ReadCloser
async function read(r) {
const buf = new Uint8Array(10000);
const n = await r.read(buf);
return dec.decode(buf.slice(n));
}

const statusOK = encode('{"status":"OK"}\n');

const events = testJson["events"][Symbol.iterator]();
// iterate through the events until done.
let reqId = 0;
for await (const req of s) {
if (req.method == "POST") {
if (req.url.endsWith("/response")) {
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "ok", content: body }));
} else if (req.url.endsWith("/init/error")) {
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "error", content: body }));
export async function serveEvents(testJson) {
// start the server prior to running bootstrap.
const s = serve(`0.0.0.0:${PORT}`);
const p = bootstrap(testJson);

const events = testJson["events"][Symbol.iterator]();
const responses = [];
// iterate through the events until done.
let reqId = 0;
for await (const req of s) {
if (req.method == "POST") {
if (req.url.endsWith("/response")) {
const body = dec.decode(await req.body());
responses.push(JSON.stringify({ status: "ok", content: body }));
} else if (req.url.endsWith("/init/error")) {
const body = dec.decode(await req.body());
responses.push(JSON.stringify({ status: "error", content: body }));
await req.respond({ body: statusOK });
break;
} else if (req.url.endsWith(`/${reqId}/error`)) {
const body = dec.decode(await req.body());
responses.push(JSON.stringify({ status: "error", content: body }));
} else {
throw new Error("Unreachable!");
}
await req.respond({ body: statusOK });
p.kill(9);
s.close();
Deno.exit();
} else if (req.url.endsWith(`/${reqId}/error`)) {
const body = dec.decode(await req.body());
console.log(JSON.stringify({ status: "error", content: body }));
} else {
throw new Error("Unreachable!");
}
await req.respond({ body: statusOK });
} else {
// assert endsWith /next
const e = events.next();
if (e.done) {
p.kill(9);
s.close();
Deno.exit();
} else {
reqId++;
const headers = new Headers({
"lambda-runtime-invoked-function-arn":
"arn:aws:lambda:us-east-1:776893852117:function:test",
"lambda-runtime-aws-request-id": reqId.toString(),
"lambda-runtime-deadline-ms": (Date.now() + 300000).toString()
});
await req.respond({ body: encode(JSON.stringify(e.value)), headers });
// assert endsWith /next
const e = events.next();
if (e.done) {
break;
} else {
reqId++;
const headers = new Headers({
"lambda-runtime-invoked-function-arn":
"arn:aws:lambda:us-east-1:776893852117:function:test",
"lambda-runtime-aws-request-id": reqId.toString(),
"lambda-runtime-deadline-ms": (Date.now() + 300000).toString()
});
await req.respond({ body: encode(JSON.stringify(e.value)), headers });
}
}
}
const out = await read(p.stdout);
p.kill(9);
s.close();
await p.status();
return {
responses,
out
};
}
18 changes: 5 additions & 13 deletions tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assertEquals, runIfMain, test } from "./deps.ts";
import { serveEvents } from "./server.ts";

const dec = new TextDecoder();

Expand Down Expand Up @@ -56,21 +57,12 @@ for (const t of testFiles) {
await emptyDir("/var/task");
await addFiles(testJson["files"], "/var/task");

const p = Deno.run({
args: ["deno", "-A", "/src/tests/server.ts", testPath],
stdout: "piped",
// stderr: "piped",
cwd: "/var/task"
});
const out = dec.decode(await p.output());

const results = out
.trim()
.split("\n")
.map(x => JSON.parse(x));
const out = await serveEvents(testJson);
const responses = out.responses.map(x => JSON.parse(x));
const expected = testJson["expected"];

assertEquals(expected, results);
assertEquals(expected, responses);
// console.log(out.out) // FIXME this is always empty
}
});
}
Expand Down
Loading

0 comments on commit 7b80576

Please sign in to comment.