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

feat: v3 #307

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 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
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ jobs:
node-version:
- 18
- 20
- 21
- 22
os:
- ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Run `smee --help` for usage.
### Node Client

```js
const SmeeClient = require('smee-client')
import SmeeClient from 'smee-client'

const smee = new SmeeClient({
source: 'https://smee.io/abc123',
Expand All @@ -40,3 +40,7 @@ const events = smee.start()
// Stop forwarding events
events.close()
```

#### Proxy Servers

By default, the `SmeeClient` API client makes use of the standard proxy server environment variables.
24 changes: 16 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import validator from "validator";
import EventSource from "eventsource";
import {
fetch as undiciFetch,
EventSource,
EnvHttpProxyAgent,
type ErrorEvent,
type MessageEvent,
} from "undici";
import url from "url";
import querystring from "querystring";

Expand All @@ -23,7 +29,7 @@ class Client {
source,
target,
logger = console,
fetch = global.fetch,
fetch = undiciFetch,
}: Options) {
this.source = source;
this.target = target;
Expand All @@ -35,10 +41,11 @@ class Client {
}
}

static async createChannel({ fetch = global.fetch } = {}) {
static async createChannel({ fetch = undiciFetch } = {}) {
const response = await fetch("https://smee.io/new", {
method: "HEAD",
redirect: "manual",
dispatcher: new EnvHttpProxyAgent(),
wolfy1339 marked this conversation as resolved.
Show resolved Hide resolved
});
const address = response.headers.get("location");
if (!address) {
Expand All @@ -47,7 +54,7 @@ class Client {
return address;
}

async onmessage(msg: any) {
async onmessage(msg: MessageEvent<string>) {
const data = JSON.parse(msg.data);

const target = url.parse(this.target, true);
Expand Down Expand Up @@ -76,7 +83,6 @@ class Client {
const response = await this.fetch(url.format(target), {
method: "POST",
mode: data["sec-fetch-mode"],
cache: "default",
body,
headers,
});
Expand All @@ -90,12 +96,14 @@ class Client {
this.logger.info("Connected", this.events.url);
}

onerror(err: any) {
onerror(err: ErrorEvent) {
this.logger.error(err);
}

start() {
const events = new EventSource(this.source);
const events = new EventSource(this.source, {
dispatcher: new EnvHttpProxyAgent(),
});

// Reconnect immediately
(events as any).reconnectInterval = 0; // This isn't a valid property of EventSource
Expand All @@ -111,4 +119,4 @@ class Client {
}
}

export = Client;
export default Client;
89 changes: 32 additions & 57 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0-development",
"description": "Client to proxy webhooks to localhost",
"main": "index.js",
"type": "module",
"bin": {
"smee": "./bin/smee.js"
},
Expand All @@ -24,13 +25,12 @@
"license": "ISC",
"dependencies": {
"commander": "^12.0.0",
"eventsource": "^2.0.2",
"undici": "6.19.8",
"validator": "^13.11.0"
},
"devDependencies": {
"@octokit/tsconfig": "^3.0.0",
"@types/eventsource": "^1.1.15",
"@types/node": "^20.0.0",
"@types/node": "^18.19.14",
"@types/validator": "^13.11.6",
"@vitest/coverage-v8": "^2.0.0",
"fastify": "^4.24.3",
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Client from "../index";
import Client from "../index.ts";
import { describe, test, expect } from "vitest";
import { fastify as Fastify } from "fastify";

Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"extends": "@octokit/tsconfig",

"compilerOptions": {
"verbatimModuleSyntax": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"pretty": true,
Expand All @@ -12,8 +11,7 @@
"noImplicitAny": true,
"esModuleInterop": true,
"declaration": true,
"allowJs": true,
"lib": ["es2023", "dom"]
"allowJs": true
},
"include": ["./*"],
"files": ["index.ts"]
Expand Down