-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 |
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -1,40 +1,54 @@ | ||
{ | ||
"name": "smee-client", | ||
"version": "1.2.3", | ||
"description": "Client to proxy webhooks to localhost", | ||
"description": "Client to proxy webhooks to local host", | ||
"main": "index.js", | ||
"bin": { | ||
"smee": "./bin/smee.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts", | ||
"bin" | ||
"bin", | ||
"lib" | ||
], | ||
"scripts": { | ||
"build": "tsc -p tsconfig.json", | ||
"lint": "prettier --check \"index.ts\" \"test/**/*.ts\" package.json tsconfig.json --end-of-line auto", | ||
"lint:fix": "prettier --write \"index.ts\" \"test/**/*.ts\" package.json tsconfig.json --end-of-line auto", | ||
"test": "vitest run", | ||
"test:coverage": "vitest run --coverage", | ||
"test:dev": "vitest --ui --coverage" | ||
"test": "jest --coverage && standard", | ||
"build": "tsc -p tsconfig.json" | ||
}, | ||
"repository": "github:probot/smee-client", | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"commander": "^11.1.0", | ||
"eventsource": "^2.0.2", | ||
"validator": "^13.11.0" | ||
"commander": "^9.0.0", | ||
"eventsource": "^2.0.0", | ||
"morgan": "^1.9.1", | ||
"superagent": "^8.0.0", | ||
"validator": "^13.7.0" | ||
}, | ||
"devDependencies": { | ||
"@octokit/tsconfig": "^2.0.0", | ||
"@types/eventsource": "^1.1.15", | ||
"@types/validator": "^13.11.6", | ||
"@vitest/coverage-v8": "^0.34.6", | ||
"fastify": "^4.24.3", | ||
"prettier": "^3.1.0", | ||
"typescript": "^5.0.0", | ||
"vitest": "^0.34.6" | ||
"@babel/core": "^7.4.0", | ||
"@types/eventsource": "^1.1.8", | ||
"@types/jest": "^29.0.0", | ||
"@types/nock": "^10.0.0", | ||
"@types/superagent": "^4.1.15", | ||
"@types/validator": "^13.0.0", | ||
"babel-core": "^7.0.0-bridge.0", | ||
"babel-jest": "^29.0.0", | ||
"connect-sse": "^1.2.0", | ||
"jest": "^29.0.0", | ||
"nock": "^13.0.0", | ||
"standard": "^17.0.0", | ||
"supertest": "^6.0.0", | ||
"ts-jest": "^29.0.0", | ||
"typescript": "^4.0.0" | ||
}, | ||
"standard": { | ||
"env": [ | ||
"jest" | ||
] | ||
}, | ||
"jest": { | ||
"preset": "ts-jest" | ||
} | ||
} |
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,131 +1,16 @@ | ||
import Client from "../index"; | ||
import { describe, test, expect } from "vitest"; | ||
import { fastify as Fastify } from "fastify"; | ||
|
||
describe("client", () => { | ||
describe("createChannel", () => { | ||
test("returns a new channel", async () => { | ||
const channel = await Client.createChannel(); | ||
expect(channel).toMatch(/https:\/\/smee\.io\/[0-9a-zA-Z]{10,}/); | ||
}); | ||
|
||
test("throws if could not create a new channel", async () => { | ||
expect( | ||
Client.createChannel({ | ||
// @ts-ignore | ||
fetch: async () => { | ||
return { | ||
headers: { | ||
get: () => null, | ||
}, | ||
}; | ||
}, | ||
}), | ||
).rejects.toThrow("Failed to create channel"); | ||
}); | ||
}); | ||
|
||
describe("constructor", () => { | ||
describe("source", () => { | ||
test("throws if source is not a valid URL", () => { | ||
expect( | ||
() => | ||
new Client({ | ||
source: "mailto:do-not-reply@example.com", | ||
target: "https://example.com", | ||
}), | ||
).toThrow("The provided URL is invalid."); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("onmessage", () => { | ||
test("returns a new channel", async () => { | ||
expect.assertions(2); | ||
|
||
let finishedPromise = { | ||
promise: undefined, | ||
reject: undefined, | ||
resolve: undefined, | ||
} as { | ||
promise?: Promise<any>; | ||
resolve?: (value?: any) => any; | ||
reject?: (reason?: any) => any; | ||
}; | ||
|
||
finishedPromise.promise = new Promise((resolve, reject) => { | ||
finishedPromise.resolve = resolve; | ||
finishedPromise.reject = reject; | ||
}); | ||
|
||
let callCount = 0; | ||
const fastify = Fastify(); | ||
|
||
fastify.route({ | ||
url: "/", | ||
method: "POST", | ||
handler: async (request, reply) => { | ||
callCount++; | ||
|
||
expect(JSON.stringify(request.body)).toBe( | ||
JSON.stringify({ hello: "world" }), | ||
); | ||
|
||
if (callCount === 2) { | ||
finishedPromise.resolve!(); | ||
} | ||
return reply | ||
.send(request.body) | ||
.header("content-type", "application/json"); | ||
}, | ||
}); | ||
|
||
const target = await fastify.listen(); | ||
|
||
const source = await Client.createChannel(); | ||
const client = new Client({ | ||
source, | ||
target, | ||
}); | ||
|
||
let readyPromise = { | ||
promise: undefined, | ||
reject: undefined, | ||
resolve: undefined, | ||
} as { | ||
promise?: Promise<any>; | ||
resolve?: (value?: any) => any; | ||
reject?: (reason?: any) => any; | ||
}; | ||
|
||
readyPromise.promise = new Promise((resolve, reject) => { | ||
readyPromise.resolve = resolve; | ||
readyPromise.reject = reject; | ||
}); | ||
|
||
client.onopen = readyPromise.resolve!; | ||
client.onerror = readyPromise.reject!; | ||
client.start(); | ||
|
||
await readyPromise.promise; | ||
|
||
await fetch(target + "/", { | ||
method: "POST", | ||
body: JSON.stringify({ hello: "world" }), | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}); | ||
|
||
await fetch(source, { | ||
method: "POST", | ||
body: JSON.stringify({ hello: "world" }), | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
}); | ||
|
||
await finishedPromise.promise; | ||
}); | ||
}); | ||
}); | ||
import Client = require('..') | ||
import nock = require('nock') | ||
|
||
describe('client', () => { | ||
describe('createChannel', () => { | ||
test('returns a new channel', async () => { | ||
const req = nock('https://smee.io').head('/new').reply(302, '', { | ||
Location: 'https://smee.io/abc123' | ||
}) | ||
|
||
const channel = await Client.createChannel() | ||
expect(channel).toEqual('https://smee.io/abc123') | ||
expect(req.isDone()).toBe(true) | ||
}) | ||
}) | ||
}) |
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