Skip to content

Commit

Permalink
fix: Revert "refactor: modernize code and remove dependencies (#260)" (
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m authored Nov 17, 2023
1 parent fd052f4 commit f67f3c8
Show file tree
Hide file tree
Showing 9 changed files with 7,796 additions and 2,036 deletions.
117 changes: 0 additions & 117 deletions .github/workflows/ci.yml

This file was deleted.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ server/public/main.min.*
coverage
.vscode
index.js
index.js.map
*.d.ts
*.tsbuildinfo
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

language: node_js

node_js:
- "10"

notifications:
disabled: true

install:
- npm install -g codecov
- npm install

branches:
except:
- /^v\d+\.\d+\.\d+$/

script:
- npm test
- codecov
13 changes: 7 additions & 6 deletions bin/smee.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ program
.option('-P, --path <path>', 'URL path to post proxied requests to`', '/')
.parse(process.argv)

const opts = program.opts()

const {
target = `http://127.0.0.1:${opts.port}${opts.path}`
} = opts
let target
if (program.target) {
target = program.target
} else {
target = `http://127.0.0.1:${program.port}${program.path}`
}

async function setup () {
let source = opts.url
let source = program.url

if (!source) {
source = await Client.createChannel()
Expand Down
119 changes: 49 additions & 70 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,88 @@
import validator from "validator";
import EventSource from "eventsource";
import url from "url";
import querystring from "querystring";
import validator from 'validator'
import EventSource from 'eventsource'
import superagent from 'superagent'
import url from 'url'
import querystring from 'querystring'

type Severity = "info" | "error";
type Severity = 'info' | 'error'

interface Options {
source: string;
target: string;
logger?: Pick<Console, Severity>;
fetch?: any;
source: string
target: string
logger?: Pick<Console, Severity>
}

class Client {
source: string;
target: string;
fetch: typeof global.fetch;
logger: Pick<Console, Severity>;
events!: EventSource;

constructor({
source,
target,
logger = console,
fetch = global.fetch,
}: Options) {
this.source = source;
this.target = target;
this.logger = logger!;
this.fetch = fetch;
constructor ({ source, target, logger = console }: Options) {
this.source = source
this.target = target
this.logger = logger!

if (!validator.isURL(this.source)) {
throw new Error("The provided URL is invalid.");
throw new Error('The provided URL is invalid.')
}
}

static async createChannel({ fetch = global.fetch } = {}) {
const response = await fetch("https://smee.io/new", {
method: "HEAD",
redirect: "manual",
});
const address = response.headers.get("location");
if (!address) {
throw new Error("Failed to create channel");
}
return address;
static async createChannel () {
return superagent.head('https://smee.io/new').redirects(0).catch((err) => {
return err.response.headers.location
})
}

async onmessage(msg: any) {
const data = JSON.parse(msg.data);
onmessage (msg: any) {
const data = JSON.parse(msg.data)

const target = url.parse(this.target, true);
const mergedQuery = { ...target.query, ...data.query };
target.search = querystring.stringify(mergedQuery);
const target = url.parse(this.target, true)
const mergedQuery = Object.assign(target.query, data.query)
target.search = querystring.stringify(mergedQuery)

delete data.query;
delete data.query

const body = JSON.stringify(data.body);
delete data.body;
const req = superagent.post(url.format(target)).send(data.body)

const headers: { [key: string]: any } = {};
delete data.body

Object.keys(data).forEach((key) => {
headers[key] = data[key];
});
Object.keys(data).forEach(key => {
req.set(key, data[key])
})

headers["content-length"] = Buffer.byteLength(body);

try {
const response = await this.fetch(url.format(target), {
method: "POST",
mode: data["sec-fetch-mode"],
cache: "default",
body,
headers,
});
this.logger.info(`POST ${response.url} - ${response.status}`);
} catch (err) {
this.logger.error(err);
}
req.end((err, res) => {
if (err) {
this.logger.error(err)
} else {
this.logger.info(`${req.method} ${req.url} - ${res.status}`)
}
})
}

onopen() {
this.logger.info("Connected", this.events.url);
onopen () {
this.logger.info('Connected', this.events.url)
}

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

start() {
start () {
const events = new EventSource(this.source);

// Reconnect immediately
(events as any).reconnectInterval = 0; // This isn't a valid property of EventSource
(events as any).reconnectInterval = 0 // This isn't a valid property of EventSource

events.addEventListener("message", this.onmessage.bind(this));
events.addEventListener("open", this.onopen.bind(this));
events.addEventListener("error", this.onerror.bind(this));
events.addEventListener('message', this.onmessage.bind(this))
events.addEventListener('open', this.onopen.bind(this))
events.addEventListener('error', this.onerror.bind(this))

this.logger.info(`Forwarding ${this.source} to ${this.target}`);
this.events = events;
this.logger.info(`Forwarding ${this.source} to ${this.target}`)
this.events = events

return events;
return events
}
}

export = Client;
export = Client
Loading

0 comments on commit f67f3c8

Please sign in to comment.