Skip to content

Commit

Permalink
Don't require API key, more logging, better exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
fredsted committed Feb 19, 2024
1 parent ce4ae8c commit 7f6927b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
3 changes: 0 additions & 3 deletions commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ export default async (argv) => {
if (!argv.token && !process.env.WH_TOKEN) {
throw new Error('Please specify a token (--token)')
}
if (!argv['api-key'] && !process.env.WH_API_KEY) {
throw new Error('Please specify an API Key (--api-key)')
}
if (!argv.command && !process.env.WH_COMMAND) {
throw new Error('Please specify a command (--command)')
}
Expand Down
3 changes: 0 additions & 3 deletions commands/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ export default async (argv) => {
if (!argv.token && !process.env.WH_TOKEN) {
throw new Error('Please specify a token (--token)')
}
if (!argv['api-key'] && !process.env.WH_API_KEY) {
throw new Error('Please specify an API Key (--api-key)')
}
if (!argv.target && !process.env.WH_TARGET) {
throw new Error('Please specify a target (--target)')
}
Expand Down
8 changes: 6 additions & 2 deletions commands/lib/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ import client from "socket.io-client";
import logger from "./log.js";

export default (tokenId, apiKey, onRequest) => {
const headers = apiKey ? {'Api-Key': apiKey} : {};
const echo = new Echo.default({
host: process.env.WH_WS_HOST ?? 'wss://ws.webhook.site',
broadcaster: 'socket.io',
client,
auth: {headers: {'Api-Key': apiKey}}
auth: {headers}
})

let channel = echo.private(`token.${tokenId}`);

channel.socket.on('connect', (error) => {
logger.trace('WS: Connected', { error })
logger.info('Connected to Websocket. Listening for requests.', { error })
if (!apiKey) {
logger.warn('Warning: If token is associated with a Webhook.site account, specify an API key.', { error })
}
})

channel.socket.on('error', (error) => {
Expand Down
16 changes: 14 additions & 2 deletions commands/lib/log.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import pino from "pino";

const logger = pino({
const log = pino({
level: process.env.WH_LOG_LEVEL ?? 'info',
})

export default logger
process.on('uncaughtException',
(err) => {
log.fatal(err, 'Unhandled Exception');
setTimeout(() => { process.abort() }, 1000).unref()
process.exit(1);
});

process.on('unhandledRejection',
(err) => {
log.error(err, 'Unhandled Promise Rejection');
});

export default log
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@webhooksite/cli",
"description": "Client for Webhook.site",
"repository": "webhooksite/cli",
"version": "0.1.7",
"version": "0.1.8",
"type": "module",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
Expand Down

0 comments on commit 7f6927b

Please sign in to comment.