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

Support clientContext and identity attributes on Context #34

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions runtime/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,40 @@ function investigate {
echo "
import { $HANDLER_NAME } from '$LAMBDA_TASK_ROOT/$HANDLER_FILE';
const INVOCATION = '${API_ROOT}invocation/';
function tryParse(headers, headerName) {
const json = headers.get(headerName);
if (json) {
try {
return JSON.parse(json)
} catch (e) {
console.error(
'Unable to parse header \'' + headerName + '\' value as JSON: ' + json)
}
}
}

while (true) {
const next = await fetch(INVOCATION + 'next');
const headers = next.headers;
const reqId = headers.get('Lambda-Runtime-Aws-Request-Id');
const reqId = headers.get('lambda-runtime-aws-request-id');
const context = {
functionName: '$AWS_LAMBDA_FUNCTION_NAME',
functionVersion: '$AWS_LAMBDA_FUNCTION_VERSION',
invokedFunctionArn: headers.get('lambda-runtime-invoked-function-arn'),
memoryLimitInMB: $AWS_LAMBDA_FUNCTION_MEMORY_SIZE,
memoryLimitInMB: '$AWS_LAMBDA_FUNCTION_MEMORY_SIZE',
awsRequestId: headers.get('lambda-runtime-aws-request-id'),
logGroupName: '$AWS_LAMBDA_LOG_GROUP_NAME',
logStreamName: '$AWS_LAMBDA_LOG_STREAM_NAME',
identity: undefined,
clientContext: undefined,
identity: tryParse(headers, 'lambda-runtime-cognito-identity'),
clientContext: tryParse(headers, 'lambda-runtime-client-context'),
getRemainingTimeInMillis: function() {
return Number(headers.get('lambda-runtime-deadline-ms')) - Date.now();
}
},
// FIXME: we add these for type compatibility with Definitely Typed.
callbackWaitsForEmptyEventLoop: undefined,
done: undefined,
fail: undefined,
succeed: undefined
}
let res;
try {
Expand Down Expand Up @@ -170,6 +187,12 @@ 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"

# FIXME remove DENO_FLAGS=DENO_FLAGS environment variable setting.
# FIXME remove DENO_FLAGS=DENO_FLAGS environment variable setting (used in testing only).
DENO_DIR=/tmp/deno_dir DENO_FLAGS=$DENO_FLAGS NO_COLOR=true $SCRIPT_DIR/amz-deno run $DENO_FLAGS /tmp/runtime.js \
|| investigate;

# For debugging purposes of the bootstrap script itself it's useful to change the extension to /tmp/runtime.ts
# and run this code instead to get more verbose and type errors during testing.
# Note: on production you will see the verbose error message anyway in cloudwatch logs.
# DENO_DIR=/tmp/deno_dir DENO_FLAGS=$DENO_FLAGS NO_COLOR=true $SCRIPT_DIR/amz-deno run $DENO_FLAGS /tmp/runtime.ts &> /tmp/fail.out \
# || error "error: $(cat /tmp/fail.out)"
2 changes: 1 addition & 1 deletion tests/decorate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function decorate(target) { }
function decorate(target) {}

@decorate
class Foo {}
Expand Down
7 changes: 6 additions & 1 deletion tests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export async function withContext(
event: APIGatewayProxyEvent,
context: Context
) {
return { name: context.functionName, awsRequestId: context.awsRequestId };
return {
name: context.functionName,
awsRequestId: context.awsRequestId,
clientContext: context.clientContext,
identity: context.identity
};
}

// TODO add a test for this behavior.
Expand Down
8 changes: 4 additions & 4 deletions tests/importmap.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"imports": {
"std/": "https://deno.land/std/"
}
}
"imports": {
"std/": "https://deno.land/std/"
}
}
8 changes: 5 additions & 3 deletions tests/pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ export function handler(event, context) {

export async function assertLock(event, context) {
// assert --lock was passed
// Note: This is a file with external imports in order for -lock to be used.
// FIXME remove this env hack and pull out the actual cli args from Deno itself.
// (we don't necessary need to set DENO_FLAGS for deno in bootstrap.)
if (Deno.env("DENO_FLAGS").includes("--lock=lock.json")) {
// (we shouldn't need to set DENO_FLAGS for deno in bootstrap.)
const flags: string = Deno.env("DENO_FLAGS")!;
if (/--lock=lock\.json/.test(flags)) {
return {};
}
throw new Error("--lock not passed to deno run");
throw new Error("--lock=lock.json not passed to deno run " + flags);
}
5 changes: 5 additions & 0 deletions tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export async function serveEvents(testJson) {
"lambda-runtime-aws-request-id": reqId.toString(),
"lambda-runtime-deadline-ms": (Date.now() + 300000).toString()
});
if (testJson.headers) {
for (let [k, v] of Object.entries(testJson.headers)) {
headers.append(k, JSON.stringify(v));
}
}
await req.respond({
body: enc.encode(JSON.stringify(e.value)),
headers
Expand Down
34 changes: 34 additions & 0 deletions tests/test_config2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"env": { "_HANDLER": "handlers.withContext" },
"events": [{ "hello": "deno" }],
"expected": [
{
"status": "ok",
"content": "{\"name\":\"test\",\"awsRequestId\":\"1\",\"clientContext\":{\"client\":{\"installationId\":\"installationId\",\"appTitle\":\"appTitle\",\"appVersionName\":\"versionName\",\"appVersionCode\":\"versionCode\",\"appPackageName\":\"packageName\"},\"env\":{\"platformVersion\":\"platformVersion\",\"platform\":\"platform\",\"make\":\"make\",\"model\":\"model\",\"locale\":\"locale\"}},\"identity\":{\"cognitoIdentityId\":\"us-east-1:605249a8-8fc1-40ed-bf89-23bc74ecc232\",\"cognitoIdentityPoolId\":\"us-east-1:1699ebc0-7900-4099-b910-2df94f52a030\"}}"
}
],
"files": ["handlers.ts"],
"layer": "deno-lambda-layer.zip",
"headers": {
"Lambda-Runtime-Client-Context": {
"client": {
"installationId": "installationId",
"appTitle": "appTitle",
"appVersionName": "versionName",
"appVersionCode": "versionCode",
"appPackageName": "packageName"
},
"env": {
"platformVersion": "platformVersion",
"platform": "platform",
"make": "make",
"model": "model",
"locale": "locale"
}
},
"Lambda-Runtime-Cognito-Identity": {
"cognitoIdentityId": "us-east-1:605249a8-8fc1-40ed-bf89-23bc74ecc232",
"cognitoIdentityPoolId": "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030"
}
}
}
5 changes: 4 additions & 1 deletion tests/test_importmap.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"env": { "_HANDLER": "importmap.handler", "DENO_IMPORTMAP": "importmap.json" },
"env": {
"_HANDLER": "importmap.handler",
"DENO_IMPORTMAP": "importmap.json"
},
"events": [{ "hello": "deno" }],
"expected": [
{
Expand Down
2 changes: 1 addition & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
}