@omen.foundation/node-microservice-runtime 0.1.65 → 0.1.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,103 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import path from 'node:path';
4
- import { fileURLToPath } from 'node:url';
5
- import { runCommand } from './lib/cli-utils.mjs';
6
-
7
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
8
-
9
- function parseArgs(argv) {
10
- const args = {
11
- entry: 'dist/main.js',
12
- output: 'beam_openApi.json',
13
- envFile: undefined,
14
- cid: process.env.CID,
15
- pid: process.env.PID,
16
- host: process.env.HOST,
17
- namePrefix: process.env.NAME_PREFIX,
18
- service: undefined,
19
- skipBuild: false,
20
- };
21
-
22
- const queue = [...argv];
23
- while (queue.length > 0) {
24
- const current = queue.shift();
25
- switch (current) {
26
- case '--entry':
27
- args.entry = queue.shift();
28
- break;
29
- case '--output':
30
- args.output = queue.shift();
31
- break;
32
- case '--env-file':
33
- args.envFile = queue.shift();
34
- break;
35
- case '--cid':
36
- args.cid = queue.shift();
37
- break;
38
- case '--pid':
39
- args.pid = queue.shift();
40
- break;
41
- case '--host':
42
- args.host = queue.shift();
43
- break;
44
- case '--routing-key':
45
- case '--name-prefix':
46
- args.namePrefix = queue.shift();
47
- break;
48
- case '--service':
49
- args.service = queue.shift();
50
- break;
51
- case '--skip-build':
52
- args.skipBuild = true;
53
- break;
54
- default:
55
- throw new Error(`Unknown argument: ${current}`);
56
- }
57
- }
58
-
59
- if (!args.envFile && process.env.npm_config_env_file) {
60
- args.envFile = process.env.npm_config_env_file;
61
- }
62
-
63
- return args;
64
- }
65
-
66
- async function main() {
67
- const args = parseArgs(process.argv.slice(2));
68
-
69
- if (!args.skipBuild) {
70
- await runCommand('npm', ['run', 'build']);
71
- }
72
-
73
- const generateScript = path.resolve(__dirname, 'generate-openapi.mjs');
74
- const commandArgs = ['--entry', args.entry, '--output', args.output];
75
-
76
- if (args.envFile) {
77
- commandArgs.push('--env-file', args.envFile);
78
- }
79
- if (args.cid) {
80
- commandArgs.push('--cid', args.cid);
81
- }
82
- if (args.pid) {
83
- commandArgs.push('--pid', args.pid);
84
- }
85
- if (args.host) {
86
- commandArgs.push('--host', args.host);
87
- }
88
- if (args.namePrefix) {
89
- commandArgs.push('--routing-key', args.namePrefix);
90
- }
91
- if (args.service) {
92
- commandArgs.push('--service', args.service);
93
- }
94
-
95
- await runCommand(process.execPath, [generateScript, ...commandArgs], { shell: false });
96
-
97
- console.log('Validation completed successfully.');
98
- }
99
-
100
- main().catch((error) => {
101
- console.error(error instanceof Error ? error.message : error);
102
- process.exit(1);
103
- });
@@ -1,25 +0,0 @@
1
- import WebSocket from 'ws';
2
-
3
- const url = process.argv[2] ?? 'wss://api.beamable.com/socket';
4
-
5
- const ws = new WebSocket(url);
6
-
7
- ws.on('open', () => {
8
- console.log('ws open');
9
- const payload = JSON.stringify({ id: -1, method: 'get', path: 'gateway/nonce', body: {} });
10
- console.log('sending', payload);
11
- ws.send(payload);
12
- });
13
-
14
- ws.on('message', (data) => {
15
- console.log('message', data.toString());
16
- ws.close();
17
- });
18
-
19
- ws.on('error', (err) => {
20
- console.error('error', err);
21
- });
22
-
23
- ws.on('close', (code, reason) => {
24
- console.log('closed', code, reason.toString());
25
- });
package/src/auth.ts DELETED
@@ -1,117 +0,0 @@
1
- import { createHash } from 'node:crypto';
2
- import { URL } from 'node:url';
3
- import type { EnvironmentConfig } from './types.js';
4
- import type { GatewayRequester } from './requester.js';
5
- import { AuthenticationError } from './errors.js';
6
-
7
- interface NonceResponse {
8
- nonce: string;
9
- }
10
-
11
- interface AuthResponse {
12
- result: string;
13
- }
14
-
15
- interface TokenResponse {
16
- access_token?: string;
17
- error?: string;
18
- }
19
-
20
- export class AuthManager {
21
- private readonly env: EnvironmentConfig;
22
- private readonly requester: GatewayRequester;
23
-
24
- constructor(env: EnvironmentConfig, requester: GatewayRequester) {
25
- this.env = env;
26
- this.requester = requester;
27
- }
28
-
29
- async authenticate(): Promise<void> {
30
- if (this.env.secret) {
31
- await this.authenticateWithRealmSecret();
32
- return;
33
- }
34
- if (this.env.refreshToken) {
35
- await this.authenticateWithRefreshToken();
36
- return;
37
- }
38
- throw new AuthenticationError('Neither SECRET nor REFRESH_TOKEN is configured.');
39
- }
40
-
41
- private async authenticateWithRealmSecret(): Promise<void> {
42
- const nonce = await this.requester.request<NonceResponse>('get', 'gateway/nonce');
43
- if (!nonce?.nonce) {
44
- throw new AuthenticationError('Gateway did not provide a nonce for authentication.');
45
- }
46
- const signature = this.calculateRealmSignature(this.env.secret ?? '', nonce.nonce);
47
- const body = {
48
- cid: this.env.cid,
49
- pid: this.env.pid,
50
- signature,
51
- };
52
- const response = await this.requester.request<AuthResponse>('post', 'gateway/auth', body);
53
- if (!response || response.result !== 'ok') {
54
- throw new AuthenticationError(`Realm secret authentication failed with result=${response?.result ?? 'unknown'}.`);
55
- }
56
- }
57
-
58
- private async authenticateWithRefreshToken(): Promise<void> {
59
- const accessToken = await this.exchangeRefreshToken();
60
- const body = {
61
- cid: this.env.cid,
62
- pid: this.env.pid,
63
- token: accessToken,
64
- };
65
- const response = await this.requester.request<AuthResponse>('post', 'gateway/auth', body);
66
- if (!response || response.result !== 'ok') {
67
- throw new AuthenticationError(`Refresh-token authentication failed with result=${response?.result ?? 'unknown'}.`);
68
- }
69
- }
70
-
71
- private calculateRealmSignature(secret: string, nonce: string): string {
72
- const hash = createHash('md5');
73
- hash.update(secret + nonce, 'utf8');
74
- return hash.digest('base64');
75
- }
76
-
77
- private async exchangeRefreshToken(): Promise<string> {
78
- if (!this.env.refreshToken) {
79
- throw new AuthenticationError('REFRESH_TOKEN missing.');
80
- }
81
- const baseUrl = this.hostToHttpUrl();
82
- const tokenUrl = new URL('/basic/auth/token', baseUrl).toString();
83
- const response = await fetch(tokenUrl, {
84
- method: 'POST',
85
- headers: {
86
- 'Content-Type': 'application/json',
87
- Accept: 'application/json',
88
- 'beam-scope': `${this.env.cid}.${this.env.pid}`,
89
- },
90
- body: JSON.stringify({
91
- grant_type: 'refresh_token',
92
- refresh_token: this.env.refreshToken,
93
- }),
94
- });
95
-
96
- if (!response.ok) {
97
- throw new AuthenticationError(`Failed to retrieve access token. status=${response.status}`);
98
- }
99
-
100
- const payload = (await response.json()) as TokenResponse;
101
- if (!payload.access_token) {
102
- throw new AuthenticationError(`Refresh-token exchange failed: ${payload.error ?? 'unknown error'}`);
103
- }
104
- return payload.access_token;
105
- }
106
-
107
- private hostToHttpUrl(): string {
108
- const host = this.env.host.replace(/\/socket$/, '');
109
- if (host.startsWith('wss://')) {
110
- return `https://${host.substring('wss://'.length)}`;
111
- }
112
- if (host.startsWith('ws://')) {
113
- return `http://${host.substring('ws://'.length)}`;
114
- }
115
- return host;
116
- }
117
- }