Skip to content

Commit

Permalink
feat: bring ngrok
Browse files Browse the repository at this point in the history
  • Loading branch information
eladb committed Jan 16, 2024
1 parent 66aa015 commit 3ea0aac
Show file tree
Hide file tree
Showing 13 changed files with 533 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ngrok-pull.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: ngrok-pull
on:
pull_request:
paths:
- ngrok/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: ngrok
- 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: ngrok
- name: Test
run: wing test
working-directory: ngrok
- name: Pack
run: wing pack
working-directory: ngrok
37 changes: 37 additions & 0 deletions .github/workflows/ngrok-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: ngrok-release
on:
push:
branches:
- main
paths:
- ngrok/**
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
sparse-checkout: ngrok
- 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: ngrok
- name: Test
run: wing test
working-directory: ngrok
- name: Pack
run: wing pack
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 }}
2 changes: 2 additions & 0 deletions ngrok/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions ngrok/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.
54 changes: 54 additions & 0 deletions ngrok/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Wing ngrok support

This library can be used to create an [ngrok](https://ngrok.com) tunnel for local development that
forwards HTTP requests to a localhost endpoint.

When compiled to the cloud, this resource is a no-op.

## Prerequisites

* [winglang](https://winglang.io).
* An [ngrok account](https://ngrok.com).
* `NGROK_AUTHTOKEN` should include the auth token for your ngrok user.

## Installation

```sh
npm i @winglibs/ngrok
```

## Usage

Let's forward all requests that are sent to `eladb.ngrok.dev` to our `cloud.Api`.

```js
bring ngrok;

let api = new cloud.Api();

api.get("/", inflight () => {
return {
status: 200,
body: "hello ngrok!"
};
});

let t = new ngrok.Tunnel(api.url, domain: "eladb.ngrok.dev");

new cloud.Function(inflight () => {
log("tunnel connected to {t.url}");
});
```

## Roadmap

- [ ] Do not require the domain
- [ ] `onConnect()`

## Maintainers

- [@eladb](https://github.com/eladb)

## License

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

let api = new cloud.Api();

api.get("/", inflight () => {
return {
status: 200,
body: "boo1m!"
};
});

let web = new cloud.Website(path: "./public");

let t = new ngrok.Tunnel(api.url, domain: "eladb.ngrok.dev");

new cloud.Function(inflight () => {
log(t.url);
});
16 changes: 16 additions & 0 deletions ngrok/ngrok.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const ngrok = require('@ngrok/ngrok');

async function main(url, domain) {
const x = await ngrok.forward({ addr: url, domain, authtoken_from_env: true, proto: "http" });
return x.url();
}

const url = process.argv[2];
const domain = process.argv[3];

main(url, domain)
.then((url) => process.stdout.write(url))
.catch((e) => process.stderr.write(e.message));

process.on("SIGINT", () => ngrok.kill().then(() => process.exit(0)));
process.stdin.resume();
33 changes: 33 additions & 0 deletions ngrok/ngrok.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
bring cloud;
bring "./ngrok.w" as ngrok;

let api = new cloud.Api();

api.get("/", inflight () => {
return {
body: "hello?",
status: 200
};
});

api.get("/world", inflight () => {
return {
body: "damn it!!",
status: 200
};
});

api.get("/uri", inflight () => {
return {
status: 200,
body: "hey uri"
};
});

let w = new cloud.Website(path: "./public");

let external = new ngrok.Tunnel(api.url, domain: "eladbgithub.ngrok.dev");

new cloud.Function(inflight () => {
log("ready {external.domain}");
});
52 changes: 52 additions & 0 deletions ngrok/ngrok.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
bring cloud;
bring util;
bring fs;
bring sim;

interface ChildProcess {
inflight kill(): void;
inflight url(): str;
}

pub struct NgrokProps {
domain: str?;
}

pub class Tunnel {
pub url: str;
state: sim.State;

new(url: str, props: NgrokProps?) {
this.state = new sim.State();
this.url = this.state.token("url");

if !nodeof(this).app.isTestEnvironment {
if !util.tryEnv("NGROK_AUTHTOKEN")? {
throw "NGROK_AUTHTOKEN is not defined";
}

let s = new cloud.Service(inflight () => {
try {
let child = Tunnel.spawn("node", Array<str?>["./ngrok.js", url, props?.domain]);
log("ngrok: {child.url()} => {url}");
this.state.set("url", child.url());
return () => {
child.kill();
};
} catch e {
log("error: {e}");

// this is needed, the exception will cause any dependents to never be initialized
this.state.set("url", "<error>");
}
});

// no need to show the ugly details
nodeof(s).hidden = true;
nodeof(this.state).hidden = true;
}
}

extern "./util.js"
static inflight spawn(cmd: str, args: Array<str?>, opts: Json?): ChildProcess;
}
Loading

0 comments on commit 3ea0aac

Please sign in to comment.