-
Notifications
You must be signed in to change notification settings - Fork 14
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
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8db6c85
feat: `bring typescript`
eladb 99970fb
wip
eladb 0288856
update readme
eladb 2645311
Merge branch 'main' into eladb/typescript
eladb 24e65b5
lifting works!
eladb 24e6c02
update README
eladb ee163a3
Update README.md
eladb 21ed331
Merge remote-tracking branch 'origin/main' into eladb/typescript
eladb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ on: | |
paths: | ||
- ngrok/** | ||
jobs: | ||
build: | ||
build-ngrok: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ on: | |
paths: | ||
- ngrok/** | ||
jobs: | ||
build: | ||
build-ngrok: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
|
@@ -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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }), | ||
}; | ||
}; | ||
``` | ||
|
||
```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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
})() | ||
`; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 atypescript.QueueHandler
automatically, or something like that.