-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·130 lines (113 loc) · 2.95 KB
/
server.js
File metadata and controls
executable file
·130 lines (113 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env node
const http = require('http');
const { parseArgs } = require('util');
// Help message
const showHelp = () => {
console.log(`
OAuth2 Callback Server for Bruno
Usage:
npx @usebruno/oauth2-callback-server [options]
Options:
-p, --port <number> Port number (default: 8090, or next available)
-h, --help Show this help message
Examples:
npx @usebruno/oauth2-callback-server
npx @usebruno/oauth2-callback-server --port 8090
npx @usebruno/oauth2-callback-server -p 8090
`);
};
// Parse command-line arguments
let values;
try {
({ values } = parseArgs({
options: {
port: {
type: 'string',
short: 'p'
},
help: {
type: 'boolean',
short: 'h',
default: false
}
},
strict: true
}));
} catch (err) {
console.error(`Error: ${err.message}\n`);
showHelp();
process.exit(1);
}
// Show help
if (values.help) {
showHelp();
process.exit(0);
}
// Get user-specified port or null
let requestedPort = null;
if (values.port) {
requestedPort = parseInt(values.port, 10);
// Validate port
if (isNaN(requestedPort) || requestedPort < 1 || requestedPort > 65535) {
console.error('Error: Invalid port number. Port must be between 1 and 65535.');
process.exit(1);
}
}
const htmlContent = `<!doctype html>
<html>
<head>
<title>Bruno OAuth2 Redirect</title>
<script>
(function() {
var url = 'bruno://app/oauth2/callback';
url += window.location.search || '';
url += window.location.hash || '';
window.open(url);
})();
</script>
</head>
<body>
Redirecting to Bruno...
</body>
</html>`;
// Start server
async function startServer() {
try {
// Dynamically import get-port (ESM module)
const getPort = (await import('get-port')).default;
let PORT;
if (requestedPort) {
// User specified a port - try to use it exactly
PORT = await getPort({ port: requestedPort });
if (PORT !== requestedPort) {
console.error(`Error: Port ${requestedPort} is already in use. Please try a different port.`);
process.exit(1);
}
} else {
// No port specified - find an available port starting from 8090
PORT = await getPort({ port: 8090 });
}
const server = http.createServer((req, res) => {
if (req.url.startsWith('/callback')) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlContent);
} else {
res.writeHead(404, { 'Content-Type': 'text/plain' });
res.end('Not Found');
}
});
// Attach error handler
server.on('error', (err) => {
console.error('Server error:', err);
process.exit(1);
});
// Start listening
server.listen(PORT, '127.0.0.1', () => {
console.log(`OAuth2 callback server running at http://127.0.0.1:${PORT}/callback`);
});
} catch (err) {
console.error(`Error: ${err.message}`);
process.exit(1);
}
}
startServer();