Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring typescript (WIP) #63

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .github/workflows/ngrok-pull.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
paths:
- ngrok/**
jobs:
build:
build-ngrok:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
18 changes: 17 additions & 1 deletion .github/workflows/ngrok-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
paths:
- ngrok/**
jobs:
build:
build-ngrok:
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -29,9 +29,25 @@ jobs:
- name: Pack
run: wing pack
working-directory: ngrok
- name: Get package version
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: ngrok
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: ngrok
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: ngrok-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: ngrok v${{ env.WINGLIB_VERSION }}
tag_name: ngrok-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/typescript-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: typescript-pull
on:
pull_request:
paths:
- typescript/**
jobs:
build-typescript:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: typescript
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: typescript
- name: Test
run: wing test
working-directory: typescript
- name: Pack
run: wing pack
working-directory: typescript
53 changes: 53 additions & 0 deletions .github/workflows/typescript-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: typescript-release
on:
push:
branches:
- main
paths:
- typescript/**
jobs:
build-typescript:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: typescript
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
- name: Install dependencies
run: npm install --include=dev
working-directory: typescript
- name: Test
run: wing test
working-directory: typescript
- name: Pack
run: wing pack
working-directory: typescript
- name: Get package version
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >>
"$GITHUB_ENV"
working-directory: typescript
- name: Publish
run: npm publish --access=public --registry https://registry.npmjs.org --tag
latest *.tgz
working-directory: typescript
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Tag commit
uses: tvdias/[email protected]
with:
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
tag: typescript-v${{ env.WINGLIB_VERSION }}
- name: GitHub release
uses: softprops/action-gh-release@v1
with:
name: typescript v${{ env.WINGLIB_VERSION }}
tag_name: typescript-v${{ env.WINGLIB_VERSION }}
files: "*.tgz"
token: ${{ secrets.PROJEN_GITHUB_TOKEN }}
18 changes: 18 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ pull_request_rules:
- -check-failure=build-github
- -check-pending=build-github
- -check-stale=build-github
- -check-failure=build-ngrok
- -check-pending=build-ngrok
- -check-stale=build-ngrok
- -check-failure=build-postgres
- -check-pending=build-postgres
- -check-stale=build-postgres
- -check-failure=build-redis
- -check-pending=build-redis
- -check-stale=build-redis
- -check-failure=build-ts-function
- -check-pending=build-ts-function
- -check-stale=build-ts-function
- -check-failure=build-typescript
- -check-pending=build-typescript
- -check-stale=build-typescript
- -check-failure=build-websockets
- -check-pending=build-websockets
- -check-stale=build-websockets
Expand Down Expand Up @@ -84,12 +93,21 @@ pull_request_rules:
- -check-failure=build-github
- -check-pending=build-github
- -check-stale=build-github
- -check-failure=build-ngrok
- -check-pending=build-ngrok
- -check-stale=build-ngrok
- -check-failure=build-postgres
- -check-pending=build-postgres
- -check-stale=build-postgres
- -check-failure=build-redis
- -check-pending=build-redis
- -check-stale=build-redis
- -check-failure=build-ts-function
- -check-pending=build-ts-function
- -check-stale=build-ts-function
- -check-failure=build-typescript
- -check-pending=build-typescript
- -check-stale=build-typescript
- -check-failure=build-websockets
- -check-pending=build-websockets
- -check-stale=build-websockets
Expand Down
2 changes: 2 additions & 0 deletions typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions typescript/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Wing

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
57 changes: 57 additions & 0 deletions typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# TypeScript support for Winglang

> TODO

## Prerequisites

* [winglang](https://winglang.io).

## Installation

`sh
npm i @winglibs/typescript
`

## Usage

The `FunctionHandler`, `QueueHandler` and `ApiEndpointHandler` types can be used to define inflight
handlers from a TypeScript codebase.

For example:

```ts
// src/index.ts
exports.handler = async (event, context) => {
console.log("Welcome to TypeScript", event);
return {
status: 200,
body: JSON.stringify({ message: "Hello, TypeScript" }),
};
};
Comment on lines +23 to +30
Copy link
Contributor

@Chriscbr Chriscbr Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add types to this example?

As a user I'd want some kind of information or disclaimer about what the signature of the function should be and whether it's enforced by the Wing library or not, since it might mean the same function I write in TypeScript for a typescript.FunctionHandler can't be reused in a typescript.QueueHandler automatically, or something like that.

```

```js
// main.w
bring typescript;

new cloud.Function(new typescript.FunctionHandler("./src"));
eladb marked this conversation as resolved.
Show resolved Hide resolved
```

## TODO

- [ ] README
- [ ] Tests
- [ ] Hot reloading
- [ ] Interact with cloud resources from the typescript side

Workarounds:

* https://github.com/winglang/wing/issues/5476
* `queue.setConsumerFunction`
* `cloud.Api()` with `TypeScriptFunction`
* `__dirname` to reference `src`
* `patchToInflight`

## License

This library is licensed under the [MIT License](./LICENSE).
13 changes: 13 additions & 0 deletions typescript/example/main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
bring cloud;
bring "../handlers.w" as typescript;

let src = "./src";

new cloud.Function(new typescript.FunctionHandler(src));

let queue = new cloud.Queue();
queue.setConsumer(new typescript.QueueHandler(src));

let api = new cloud.Api();
api.get("/foo", new typescript.ApiEndpointHandler(src) as "foo");
api.post("/bar", new typescript.ApiEndpointHandler(src) as "bar");
7 changes: 7 additions & 0 deletions typescript/example/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.handler = async (event, context) => {
console.log("Welcome to TypeScript", event);
return {
status: 200,
body: JSON.stringify({ message: "Hello, TypeScript" }),
};
};
56 changes: 56 additions & 0 deletions typescript/handlers.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
bring cloud;
bring fs;

pub class FunctionHandler impl cloud.IFunctionHandler {
new(src: str) {
Util.makeHandler(this, src);
}

pub inflight handle(event: str): str? {
// dummy
}
}

pub class QueueHandler impl cloud.IQueueSetConsumerHandler {
new(src: str) {
Util.makeHandler(this, src);
}
pub inflight handle(message: str): void {
// dummy
}
}

pub class ApiEndpointHandler impl cloud.IApiEndpointHandler {
new(src: str) {
Util.makeHandler(this, src);
}
pub inflight handle(request: cloud.ApiRequest): cloud.ApiResponse {
return { status: 500 };
}
}


struct Bundle {
entrypointPath: str;
directory: str;
hash: str;
outfilePath: str;
sourcemapPath: str;
}


class Util {
pub static makeHandler(handler: std.IResource, src: str) {
nodeof(handler).hidden = true;
let tmpdir = fs.mkdtemp();
let bundle = Util.createBundle(src, [], tmpdir);
let code = fs.readFile(bundle.entrypointPath);
Util.patchToInflight(handler, code);
}

extern "./util.js"
static patchToInflight(h: std.IResource, code: str): void;

extern "./util.js"
static createBundle(entrypoint: str, external: Array<str>?, outputDir: str?): Bundle;
}
15 changes: 15 additions & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@winglibs/typescript",
"description": "TypeScript support for Winglang",
"version": "0.0.1",
"author": {
"name": "Elad Ben-Israel",
"email": "[email protected]"
},
"repository": {
"type": "git",
"url": "https://github.com/winglang/winglibs.git",
"directory": "typescript"
},
"license": "MIT"
}
20 changes: 20 additions & 0 deletions typescript/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exports.createBundle = require('@winglang/sdk/lib/shared/bundling').createBundle;

exports.patchToInflight = (handler, code) => {
handler._toInflight = () => `
(function() {
if (!global.entrypoint) {
const x = {};
(function(exports) {
${code}
})(x);

global.entrypoint = {
handle: x.handler
};
}

return global.entrypoint;
})()
`;
};
Loading