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
gr2m authored Nov 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent fd052f4 commit f67f3c8
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
@@ -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
@@ -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()
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
9,286 changes: 7,613 additions & 1,673 deletions package-lock.json

Large diffs are not rendered by default.

52 changes: 33 additions & 19 deletions package.json
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"
}
}
147 changes: 16 additions & 131 deletions test/index.test.ts
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)
})
})
})
76 changes: 57 additions & 19 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,62 @@
{
"extends": "@octokit/tsconfig",

"compilerOptions": {
"module": "Node16",
"verbatimModuleSyntax": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"pretty": true,
"strict": true,
"sourceMap": true,
"outDir": "./",
"skipLibCheck": true,
"noImplicitAny": true,
"esModuleInterop": true,
"declaration": true,
"resolveJsonModule": true,
"allowJs": true,
"lib": ["es2023", "dom"],
"moduleResolution": "node16"
/* Basic Options */
"target": "es2017", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

},
"include": ["./*"],
"files": ["index.ts"]

0 comments on commit f67f3c8

Please sign in to comment.