-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
9 changed files
with
7,796 additions
and
2,036 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,5 @@ server/public/main.min.* | |
coverage | ||
.vscode | ||
index.js | ||
index.js.map | ||
*.d.ts | ||
*.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.