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

NodeJS sdk #589

Merged
merged 24 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ae8e7ac
sdk work
camotts Nov 16, 2023
8bbd627
remove old initial js impl and replace with ts
camotts Nov 21, 2023
d4a883b
update relative zrok path
camotts Nov 21, 2023
6c90553
Last minute updates
camotts Mar 12, 2024
3a9f26e
checkpoint
rentallect Mar 25, 2024
68ac00b
checkpoint
rentallect Mar 25, 2024
240fadf
Merge remote-tracking branch 'origin/main' into node-sdk
rentallect Mar 25, 2024
c4842a5
adjust sdk examples to use backend instead of
rentallect Mar 25, 2024
831c6cb
adjust sdk example: tcpTunnel must be private
rentallect Mar 25, 2024
7f38602
adjust generate_rest.sh
rentallect Mar 25, 2024
07c23ee
update generated api/model files
rentallect Mar 25, 2024
caffedc
adjust generate_rest.sh
rentallect Mar 25, 2024
335481f
CI
rentallect Mar 25, 2024
fb8c49d
add README to example
rentallect Mar 26, 2024
cc8a8d2
CI
rentallect Mar 26, 2024
12b553e
replace files that were deleted accidentally
rentallect Mar 26, 2024
fe8cbfe
switch to 'actions/setup-node@v4'
rentallect Mar 26, 2024
0b2379d
prepare for 'npm publish' events
rentallect Mar 26, 2024
c8d48af
transition 'pastefrom' example to use 'ziti.listen', 'ziti.write', 'z…
rentallect Apr 9, 2024
73aeec8
leverage ziti_services_refresh(), and awaitDialPermissionPresent() du…
rentallect Apr 12, 2024
9f071f5
Merge remote-tracking branch 'origin/main' into node-sdk
rentallect May 1, 2024
5ee69d9
CI for the zrok NodeJS SDK
rentallect May 1, 2024
8e0d333
CI for the zrok NodeJS SDK
rentallect May 1, 2024
54941a6
fix public frontends array
rentallect May 1, 2024
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
97 changes: 97 additions & 0 deletions .github/workflows/node-sdk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Build/Release Node SDK

on:
release:
types: [ published ]
push:
branches: [ main ]
paths-ignore:
- 'package.json'
- 'CHANGELOG.md'
pull_request:
branches: [ main ]

jobs:

build:
name: Build for Node-${{ matrix.node_ver }} ${{ matrix.config.target }}/${{ matrix.config.arch }}
runs-on: ${{ matrix.config.os }}

env:
BUILD_NUMBER: ${{ github.run_number }}
AWS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}

strategy:
matrix:
config:
- { os: ubuntu-20.04, target: "linux", arch: "x64" }
node_ver: [ 20 ]
fail-fast: false

steps:

- name: Node Version
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_ver }}

- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: 'recursive'

- name: Get current zrok repo tag
id: tag
run: echo ::set-output name=TAG::$(git describe --tags --abbrev=0)

- name: Update zrok NodeJS-SDK's package.json version based on current zrok repo git tag
if: github.ref == 'refs/heads/main'
run: |
cd ${{ runner.workspace }}/${{ github.event.repository.name }}/sdk/nodejs/sdk
npm version ${{ steps.tag.outputs.TAG }} --no-git-tag-version --allow-same-version
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git commit -am "Update package.json version to ${{ steps.tag.outputs.TAG }}" || echo "No changes to commit"

- name: Push changes to zrok NodeJS-SDK's package.json
if: github.ref == 'refs/heads/main'
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ github.ref }}

- name: Pull newly bumped zrok NodeJS-SDK semver
if: github.ref == 'refs/heads/main'
run: git pull

- name: Setup .npmrc
if: github.ref == 'refs/heads/main'
# Setup .npmrc file to prepare for possible publish to npm
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'

- name: Build the zrok NodeJS-SDK
run: |
cd ${{ runner.workspace }}/${{ github.event.repository.name }}/sdk/nodejs/sdk
npm install
npm run build
env:
BUILD_DATE: ${{ steps.date.outputs.date }}

- name: Publish production release
if: github.ref == 'refs/heads/main'
run: |
cd ${{ runner.workspace }}/${{ github.event.repository.name }}/sdk/nodejs/sdk
npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: NPM Publish
if: github.ref == 'refs/heads/main'
uses: JS-DevTools/npm-publish@v1
with:
token: ${{ secrets.NPM_TOKEN }}
access: public
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ etc/dev.yml
etc/dev-frontend.yml

# Dependencies
/node_modules/
node_modules/

# Artifacts
/dist/
dist/

# Generated files
.docusaurus
Expand Down
15 changes: 14 additions & 1 deletion bin/generate_rest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ command -v openapi >/dev/null 2>&1 || {
}

command -v swagger-codegen 2>&1 || {
echo >&2 "command 'swagger-codegen. see: https://github.com/swagger-api/swagger-codegen for installation"
echo >&2 "command 'swagger-codegen' not installed. see: https://github.com/swagger-api/swagger-codegen for installation"
exit 1
}

command -v openapi-generator-cli 2>&1 || {
echo >&2 "command 'openapi-generator-cli' not installed. see: https://www.npmjs.com/package/@openapitools/openapi-generator-cli for installation"
exit 1
}

command -v realpath 2>&1 || {
echo >&2 "command 'realpath' not installed. see: https://www.npmjs.com/package/realpath for installation"
exit 1
}

Expand All @@ -35,6 +45,9 @@ swagger generate client -P rest_model_zrok.Principal -f "$zrokSpec" -c rest_clie
echo "...generating js client"
openapi -s specs/zrok.yml -o ui/src/api -l js

echo "...generating ts client"
openapi-generator-cli generate -i specs/zrok.yml -o sdk/nodejs/sdk/src/zrok/api -g typescript-node

echo "...generating python client"
swagger-codegen generate -i specs/zrok.yml -o sdk/python/sdk/zrok -c $pythonConfig -l python

Expand Down
2 changes: 1 addition & 1 deletion sdk/golang/examples/http-server/cmd/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func main() {
}

shr, err := sdk.CreateShare(root, &sdk.ShareRequest{
BackendMode: sdk.TcpTunnelBackendMode,
BackendMode: sdk.ProxyBackendMode,
ShareMode: sdk.PublicShareMode,
Frontends: []string{"public"},
Target: "http-server",
Expand Down
73 changes: 73 additions & 0 deletions sdk/nodejs/examples/http-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# "http-server" SDK Example

This `http-server` example is a minimal `zrok` application that surfaces a basic http server over a private zrok share.

## Implementation

```js
let root = zrok.Load()
```

The `root` is a structure that contains all of the user's environment detail and allows the SDK application to access the `zrok` service instance and the underlying OpenZiti network.

```js
let shr = await zrok.CreateShare(root, new zrok.ShareRequest(zrok.PROXY_BACKEND_MODE, zrok.PRIVATE_SHARE_MODE, "http-server", ["private"]));
console.log(`access your private HTTP Server on another machine using: 'zrok access private ${shr.Token}'`)
```

The `zrok.CreateShare` call uses the loaded `environment` root along with the details of the share request (`zrok.ShareRequest`) to create the share that will be used to access the `http-server`.

We are using the `zrok.PROXY_BACKEND_MODE` to handle tcp traffic. This time we are using `zrok.PRIVATE_SHARE_MODE` to take advantage of a private share that is running. With that we set which frontends to listen on, so we use whatever is configured, `private` here.

Further down we emit where to access the service.

Then we create a NodeJS Express web server that is configured to listen for ONLY incoming Ziti connections. No TCP connections from the open internet are possible.

```js
let app = zrok.express( shr.Token );

...

app.listen(undefined, () => {
console.log(`private HTTP Server is now listening for incoming requests`)
})

...

app.get('/', function(_: Request, res: any){
...
}
```

Then we create a signal handler that catches CTRL-C. When that signal is caught, we tear down teh zrok share, and exit the process.

```js
process.on('SIGINT', async () => {
console.log("Now deleting your private zrok share...")
await zrok.DeleteShare(root, shr)
process.exit(15);
});
```

## How to execute this example

You should first set up and enable your local `zrok` environment. Then execute the following cmds:

```cmd
npm install
node dist/index.js http-server
```

When the example app begins execution, you should see output on the console that resembles the following:

```
Now creating your private zrok share...
access your private HTTP Server on another machine using: 'zrok access private <SOME_TOKEN>'
private HTTP Server is now listening for incoming requests
```

Later, when you are done using the http-server, you can press CTRL_C, and the app will shut down, like this:

```
Now deleting your private zrok share...
```
18 changes: 18 additions & 0 deletions sdk/nodejs/examples/http-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dependencies": {
"commander": "^11.1.0",
"express": "^4.18.2",
"path": "^0.12.7",
"readline-sync": "^1.4.10",
"@openziti/zrok": "^0.1.0"
michaelquigley marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@types/node": "^20.9.0",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.4.3"
},
"scripts": {
"build": "npx tsc"
}
}
55 changes: 55 additions & 0 deletions sdk/nodejs/examples/http-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { Command } = require("commander");
const zrok = require("zrok")


const program = new Command();

program
.command('http-server')
.version("1.0.0")
.description("command to host an HTTP Server")
.action(async () => {

// Load the zrok env
let root = zrok.Load()

// Authenticate with the Ziti network
await zrok.init( root ).catch(( err: Error ) => { console.error(err); return process.exit(1) });

// Set this to a larger value (e.g. 10) to trace lower-level Ziti SDK activity
zrok.setLogLevel(0)

// Create the zrok private share that will represent this web server
console.log("Now creating your private zrok share...")
let shr = await zrok.CreateShare(root, new zrok.ShareRequest(zrok.PROXY_BACKEND_MODE, zrok.PRIVATE_SHARE_MODE, "http-server", []));
console.log(`access your private HTTP Server on another machine using: 'zrok access private ${shr.Token}'`)

// Create a NodeJS Express web server that listens NOT on a TCP port, but for incoming Ziti connections to the private zrok share
let app = zrok.express( shr.Token );

// Set up a simple route
let reqCtr = 0;
app.get('/', function(_: Request, res: any){
reqCtr++;
console.log(`received a GET request... reqCtr[${reqCtr}]`);
res.write(`Hello zrok! reqCtr[${reqCtr}]`)
res.end()
});

// Start listening for incoming requests
app.listen(undefined, () => {
console.log(`private HTTP Server is now listening for incoming requests`)
})

// Delete the private share upon CTRL-C
process.on('SIGINT', async () => {
console.log("Now deleting your private zrok share...")
await zrok.DeleteShare(root, shr)
process.exit(15);
});

});


program.parse(process.argv)
const options = program.opts();
12 changes: 12 additions & 0 deletions sdk/nodejs/examples/http-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"strict": true,
"target": "es6",
"module": "commonjs",
"sourceMap": true,
"esModuleInterop": true,
"moduleResolution": "node"
}
}
18 changes: 18 additions & 0 deletions sdk/nodejs/examples/pastebin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"dependencies": {
"commander": "^11.1.0",
"express": "^4.18.2",
"path": "^0.12.7",
"readline-sync": "^1.4.10",
"@openziti/zrok": "^0.1.0"
},
"devDependencies": {
"@types/node": "^20.9.0",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.4.3"
},
"scripts": {
"build": "npx tsc"
}
}
Loading
Loading