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

Add sample nodejs host #495

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 34 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch sample ts host",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\samples\\ts\\host\\out\\index.js",
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"console": "integratedTerminal"
},
{
"type": "node",
"request": "launch",
"name": "Launch sample ts client",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\samples\\ts\\client\\out\\index.js",
"outFiles": [
"${workspaceFolder}/**/*.js"
],
"console": "integratedTerminal"
}
]
}
1 change: 1 addition & 0 deletions samples/ts/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
out/
1 change: 1 addition & 0 deletions samples/ts/client/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registy=https://registry.npmjs.org/
6 changes: 6 additions & 0 deletions samples/ts/client/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.exclude": {
"out/": true,
"node_modules/": true,
}
}
113 changes: 113 additions & 0 deletions samples/ts/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {
ManagementApiVersions,
ProductHeaderValue,
TunnelManagementHttpClient,
TunnelRequestOptions,
} from "@microsoft/dev-tunnels-management";
import {
Tunnel,
TunnelAccessScopes,
} from "@microsoft/dev-tunnels-contracts";
import {
TunnelRelayTunnelClient,
} from "@microsoft/dev-tunnels-connections";

const tunnelId = process.env.TUNNEL_ID;
if (!tunnelId) {
console.error("TUNNEL_ID environment variable is required.");
process.exit(1);
}

const clusterId = process.env.CLUSTER_ID || 'usw2';

const accessToken = process.env.ACCESS_TOKEN;
if (!accessToken) {
console.error("ACCESS_TOKEN environment variable is required.");
process.exit(1);
}

async function connectClient(tunnelManagementClient: TunnelManagementHttpClient) {
const tunnelReference: Tunnel = {
tunnelId,
clusterId,
};

const tunnelRequestOptions: TunnelRequestOptions = {
tokenScopes: [TunnelAccessScopes.Connect],
accessToken,
};

const tunnel = await tunnelManagementClient.getTunnel(tunnelReference, tunnelRequestOptions);
let client = new TunnelRelayTunnelClient();
client.trace = (level, eventId, msg, err) => {
console.log(`client: ${msg}`);
};

await client.connect(tunnel!);
console.log('Tunnel client connected');
return { tunnel, client };
}

function readAnyKey() {
return new Promise<void>((resolve) => {
// Debugger console may not support raw mode. Check if it's available.
if (typeof process.stdin.setRawMode === "function") {
process.stdin.setRawMode(true);
}

process.stdin.resume();
process.stdin.once("data", () => {
if (typeof process.stdin.setRawMode === "function") {
process.stdin.setRawMode(false);
}

resolve();
});
});
}

const aadToken = process.env.AAD_TOKEN;
if (!aadToken) {
console.error("AAD_TOKEN environment variable is required. You can get your AAD token by running 'devtunnels login --verbose' and copying the token from the output.");
process.exit(1);
}

function createTunnelManagementClient() {
const userAgent: ProductHeaderValue = {
name: "devtunnels-sample-client",
version: "1.0.0",
};

const tunnelManagementClient = new TunnelManagementHttpClient(
userAgent,
ManagementApiVersions.Version20230927preview,
() => Promise.resolve(`Bearer ${aadToken}`)
);

tunnelManagementClient.trace = (msg) => {
console.log(`tunnel: ${msg}`);
}

return tunnelManagementClient;
}

async function main() {
try {
const tunnelManagementClient = createTunnelManagementClient();

console.log("\nConnecting client...");
const { tunnel, client } = await connectClient(tunnelManagementClient);
console.log("\nPress any key to exit...\n");
await readAnyKey();

console.log("\nStopping tunnel client...");
client.dispose();

} catch (err) {
console.error(err);
}

process.exit();
}

main();
Loading
Loading