Skip to content

Commit

Permalink
Merge pull request #53 from lifeomic/typescript
Browse files Browse the repository at this point in the history
Convert to typescript
  • Loading branch information
DavidTanner authored Jun 28, 2022
2 parents 0f0d104 + 725a806 commit cc5868f
Show file tree
Hide file tree
Showing 40 changed files with 929 additions and 719 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"extends": [
"@lifeomic/standards/javascript"
"@lifeomic/standards"
],
"env": {
"node": true,
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/pr-branch-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v3
with:
node-version: 14
- name: Test
run: |
yarn install
yarn test
node-version: 16
cache: yarn
- run: yarn install
- run: yarn test --testTimeout=120000 --maxWorkers=50%
17 changes: 7 additions & 10 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@ jobs:
NPM_TOKEN: ${{ secrets.LIFEOMIC_NPM_TOKEN }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/setup-node@v3
with:
node-version: 14
- name: Test
run: |
echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
yarn install --frozen-lockfile
yarn test
- name: Publish
run: |
yarn semantic-release
node-version: 16
cache: yarn
- run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > .npmrc
- run: yarn install --frozen-lockfile
- run: yarn test --testTimeout=120000 --maxWorkers=50%
- run: yarn semantic-release
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
node_modules/

yarn-error.log

test/build
coverage
41 changes: 41 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Config } from '@jest/types';
import inspector from 'inspector';

// If we are debugging then extend the timeout to max value, otherwise use the default.
const testTimeout = inspector.url() ? 1e8 : undefined;

const config: Config.InitialOptions = {
rootDir: __dirname,
preset: '@lifeomic/jest-config',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': [
'@swc/jest',
{
jsc: { target: 'es2021' },
},
],
},
collectCoverageFrom: ['src/**/*.ts'],
collectCoverage: false,
coverageThreshold: {
global: {
statements: 100,
branches: 100,
functions: 100,
lines: 100,
},
},
coveragePathIgnorePatterns: ['<rootDir>/test/', '/node_modules/'],
clearMocks: true,
resetMocks: true,
restoreMocks: true,
testMatch: [
'<rootDir>/test/**/*.test.ts',
],
verbose: true,
maxWorkers: 4,
testTimeout,
};

export default config;
35 changes: 19 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,38 @@
"alpha": "src/cli.js"
},
"scripts": {
"prepublishOnly": "tsc -p tsconfig.build.json",
"lint": "eslint .",
"postlint": "tsc",
"pretest": "yarn lint",
"test": "jest"
"test": "jest",
"clean": "yarn tsc --build --clean; rm -rf tsconfig.build.tsbuildinfo tsconfig.tsbuildinfo"
},
"devDependencies": {
"@lifeomic/eslint-config-standards": "^2.1.2",
"@lifeomic/jest-config": "^1.1.2",
"@lifeomic/typescript-config": "^1.0.3",
"@swc/core": "^1.2.207",
"@swc/jest": "^0.2.21",
"@types/glob": "^7.2.0",
"@types/jest": "^28.1.3",
"@types/koa": "^2.13.4",
"@types/koa-bodyparser": "^4.3.7",
"@types/node": "^16",
"conventional-changelog-conventionalcommits": "^4.6.3",
"coveralls": "^3.0.1",
"eslint": "^8.18.0",
"jest": "^28.1.1",
"koa": "^2.3.0",
"koa-bodyparser": "^4.2.0",
"semantic-release": "^19.0.2"
},
"ava": {
"files": [
"test/**/*.test.js"
]
},
"eslintConfig": {
"extends": "plugin:@lifeomic/node/recommended"
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"semantic-release": "^19.0.2",
"ts-jest": "^28.0.5",
"ts-node": "^10.8.1",
"typescript": "^4.7.4"
},
"dependencies": {
"@lifeomic/alpha": "^2.1.0",
"glob": "^7.1.2",
"pkginfo": "^0.4.1",
"yargs": "^17.3.1"
"glob": "^8.0.3",
"yargs": "^17.5.1"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 0 additions & 10 deletions src/ValidationError.js

This file was deleted.

9 changes: 9 additions & 0 deletions src/ValidationError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class ValidationError extends Error {
constructor (
public stdout: string,
public stderr: string,
) {
super('An error was detected during response validation');
this.name = 'ValidationError';
}
}
20 changes: 10 additions & 10 deletions src/alphaProxy.js → src/alphaProxy.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
const { createServer } = require('http');
import { createServer } from 'http';
import { Config } from './types';
import { callAlpha } from './utils';

const alphaProxy = (baseConfig, callback) => {
export const alphaProxy = (baseConfig: Config) => {
return createServer((req, response) => {
const { method, url } = req;
const requestConfig = {
const requestConfig: Config = {
...baseConfig,
method,
url: baseConfig.url + url,
method: method as Config['method'],
url: `${baseConfig.url as string}${url as string}`,
headers: {
...baseConfig.headers,
...req.headers,
...req.headers as Record<string, string>,
},
};

let data = '';

req.on('data', (chunk) => {
req.on('data', (chunk: string) => {
data += chunk;
});

req.on('end', () => {
if (data) {
requestConfig.data = data;
}
callback(requestConfig).then((result) => {
callAlpha(requestConfig).then((result) => {
response.statusCode = result.status;
response.write(result.data);
response.end();
Expand All @@ -34,5 +36,3 @@ const alphaProxy = (baseConfig, callback) => {
});
}).listen(baseConfig.proxyPort, 'localhost');
};

module.exports = alphaProxy;
59 changes: 0 additions & 59 deletions src/cli.js

This file was deleted.

65 changes: 65 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env node

import glob from 'glob';
import path from 'path';
import yargs from 'yargs';
import { ValidationError } from './ValidationError';
import { alphaProxy } from './alphaProxy';
import { Config } from './types';
import { callAlpha } from './utils';

const config: Config = {
transformResponse: [(data) => data],
validateStatus: () => true,
responsePostProcessors: [],
};

const run = async () => {
const plugins = await Promise.all(glob.sync(path.join(__dirname, 'plugins/*.[jt]s'))
.map(async (filename) => {
const plugin = await import(filename);
return plugin.default(yargs);
}));

const args = await yargs.parse();

const skipRequest = plugins.some((execute) => execute(config, args));

const {
proxied,
proxyPort,
} = config;

if (proxied) {
alphaProxy(config).on('listening', () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
console.log(`Proxy is listening on port ${proxyPort!}; Press any key to quit;`);
});

// These are only relevant in a terminal, not in tests
/* istanbul ignore next */
if (process.stdin.isTTY) {
/* istanbul ignore next */
process.stdin.setRawMode(true);
}
process.stdin.on('data', () => {
process.exit(0);
});
} else if (!skipRequest) {
callAlpha(config).then((result) => {
process.stdout.write(result.data);
}).catch((error) => {
if (error instanceof ValidationError) {
process.stdout.write(error.stdout);
process.stderr.write(error.stderr);
} else {
console.error(error.toString());
}
process.exitCode = 1;
});
}
};

run().catch((error) => {
console.error(error);
});
38 changes: 0 additions & 38 deletions src/plugins/headers.js

This file was deleted.

Loading

0 comments on commit cc5868f

Please sign in to comment.