Skip to content

Commit

Permalink
All
Browse files Browse the repository at this point in the history
  • Loading branch information
horita-yuya committed Nov 21, 2024
1 parent 857beec commit 3ef08ba
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/Publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
env:
CGO_ENABLED: 0

- name: Build type-evaluate.js
- name: Build compute-type-value.js
working-directory: ./compute-type-value
run: yarn install && yarn build

Expand All @@ -30,7 +30,7 @@ jobs:
tar -xJf node-v20.18.0-linux-x64.tar.xz --strip-components=1
cp bin/node tlrt/runtime/node
cp bootstrap-go/build/bootstrap tlrt/runtime/bootstrap
cp compute-type-value/dist/type-evaluate.js tlrt/runtime/type-evaluate.js
cp compute-type-value/dist/compute-type-value.js tlrt/runtime/compute-type-value.js
cp terraform/aws_lambda_layer.tf tlrt/aws_lambda_layer.tf
cp terraform/tlrt_sample_index.ts tlrt/tlrt_sample_index.ts
cp terraform/tlrt_sample_lambda.tf tlrt/tlrt_sample_lambda.tf
Expand Down
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,46 @@

tlrt is a custom runtime for AWS Lambda that allows you to write lambda function in type level programming.

tlrt is composed of three components:
1. entrypoint
This is a entrypoint called by AWS Lambda. It reads the input from the event and pass it to the handler.
Define your handler function as a Handler type and implement it.
The following code is an example of a handler function that adds two numbers.

2. compute-type-value
This reads your handler and compute the value of the handler.
```typescript
type Num<N extends number, COUNT extends number[]> = COUNT["length"] extends N
? COUNT
: Num<N, [1, ...COUNT]>;

3. lambda-layer (terraform)
type Sum<N1 extends number, N2 extends number> = [
...Num<N1, []>,
...Num<N2, []>,
]["length"];

export type Handler<EVENT extends { n1: number; n2: number }> = Sum<
EVENT["n1"],
EVENT["n2"]
>;
```

Upload this code to AWS Lambda and call the lambda function with the following event.

```json
{
"n1": 1,
"n2": 2
}
```

Then, you will get the following response.

![sample_add](./resources/sample_add.png)

## TLRT Components:

### 1. entrypoint
This is a entrypoint called by AWS Lambda. It reads the input from the event and pass it to the function code you deployed.

### 2. compute-type-value
Combines the event passed to the lambda function and the function code you deployed.
And computed the value of it.

### 3. lambda-layer (terraform)
To use the above components, you need to create a lambda layer. This terraform module creates a lambda layer for you.
2 changes: 1 addition & 1 deletion bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ do

REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)

RESPONSE=$(/opt/node /opt/type-evaluate.js $_HANDLER "$EVENT_DATA" $LAMBDA_TASK_ROOT)
RESPONSE=$(/opt/node /opt/compute-type-value.js $_HANDLER "$EVENT_DATA" $LAMBDA_TASK_ROOT)

curl "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response" -d "$RESPONSE"
done
2 changes: 1 addition & 1 deletion bootstrap-go/cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func StartLoop() {
requestId := header.Get("Lambda-Runtime-Aws-Request-Id")
event := string(body)

nodeRes, err := exec.Command("/opt/node", "/opt/type-evaluate.js", handler, event, lambdaTaskRoot).Output()
nodeRes, err := exec.Command("/opt/node", "/opt/compute-type-value.js", handler, event, lambdaTaskRoot).Output()

if err != nil {
panic(err)
Expand Down
2 changes: 1 addition & 1 deletion compute-type-value/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
```shell
node dist/type-evaluate.js sampleIndex.Handler '{"n1":1,"n2":2}' "$(pwd)"
node dist/compute-type-value.js sampleIndex.Handler '{"n1":1,"n2":2}' "$(pwd)"
```


Expand Down
2 changes: 1 addition & 1 deletion compute-type-value/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "esbuild ./index.ts --bundle --minify --platform=node --target=es2020 --outfile=dist/type-evaluate.js",
"build": "esbuild ./index.ts --bundle --minify --platform=node --target=es2020 --outfile=dist/compute-type-value.js",
"test": "vitest"
},
"dependencies": {
Expand Down
7 changes: 6 additions & 1 deletion compute-type-value/sampleSum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ type Num<N extends number, COUNT extends number[]> = COUNT["length"] extends N
? COUNT
: Num<N, [1, ...COUNT]>;

export type Sum<N1 extends number, N2 extends number> = [
type Sum<N1 extends number, N2 extends number> = [
...Num<N1, []>,
...Num<N2, []>,
]["length"];

export type Handler<EVENT extends { n1: number; n2: number }> = Sum<
EVENT["n1"],
EVENT["n2"]
>;
Binary file added resources/sample_add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions samples/mahjong-api/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Mahjong API

## How to Deploy

AWS Lambda function named "tlrt-sample" must be existed.
```shell
yarn build
yarn zip
Expand All @@ -6,4 +11,6 @@ yarn deploy

```shell
aws lambda invoke --function-name tlrt-sample --payload '{"t1": "o2", "t2": "o3", "t3": "o4", "t4": "I2", "t5": "I2", "t6": "I2", "t7": "C4", "t8": "C5", "t9": "C6", "t10": "I6", "t11": "I6", "t12": "I6", "t13": "o4", "t14": "o4", "winning": "o3", "style": "ron", "position": "child"}' --cli-binary-format raw-in-base64-out /tmp/res.json && cat /tmp/res.json

{"result":"1300"}
```
4 changes: 2 additions & 2 deletions tlrt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Your-Terraform-Project
├── runtime
│ ├── bootstrap
│ ├── node
│ └── type-evaluate.js
│ └── compute-type-value.js
├── tlrt_sample_index.ts (Optional)
└── tlrt_sample_lambda.tf (Optional)
```
Expand All @@ -45,4 +45,4 @@ Output:
2. runtime
1. bootstrap
2. node
3. type-evaluate.js
3. compute-type-value.js

0 comments on commit 3ef08ba

Please sign in to comment.