Skip to content

Commit

Permalink
Add sample ts client
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaBiryukov committed Nov 6, 2024
1 parent 47096cb commit e4f416b
Show file tree
Hide file tree
Showing 11 changed files with 824 additions and 19 deletions.
15 changes: 14 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
"${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

0 comments on commit e4f416b

Please sign in to comment.