mirror of
https://github.com/ringtools/ringtools-server-ts.git
synced 2024-05-16 05:44:40 +02:00
Better project structure
This commit is contained in:
parent
ebd2512a99
commit
0f7a91cef2
@ -35,5 +35,6 @@
|
||||
],
|
||||
"plugins": [
|
||||
"@typescript-eslint"
|
||||
]
|
||||
],
|
||||
"ignorePatterns": "test/**/*.ts"
|
||||
}
|
4
.taprc
Normal file
4
.taprc
Normal file
@ -0,0 +1,4 @@
|
||||
test-env: [
|
||||
TS_NODE_FILES=true,
|
||||
TS_NODE_PROJECT=./test/tsconfig.json
|
||||
]
|
5
jest.config.js
Normal file
5
jest.config.js
Normal file
@ -0,0 +1,5 @@
|
||||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
};
|
20
package.json
20
package.json
@ -3,34 +3,52 @@
|
||||
"version": "1.0.0",
|
||||
"main": "src/index.ts",
|
||||
"license": "Apache-2.0",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/ajv-compiler": "^3.0.0",
|
||||
"@types/tap": "^15.0.5",
|
||||
"@types/ws": "^8.2.2",
|
||||
"ajv-formats": "^2.1.1",
|
||||
"axios": "^0.24.0",
|
||||
"dotenv": "^11.0.0",
|
||||
"fastify": "^3.25.3",
|
||||
"fastify-cli": "^2.14.0",
|
||||
"fastify-decorators": "^3.10.0",
|
||||
"fastify-env": "^2.1.1",
|
||||
"fastify-helmet": "^6.0.0",
|
||||
"fastify-plugin": "^3.0.0",
|
||||
"fastify-sensible": "^3.1.2",
|
||||
"fastify-socket.io": "^3.0.0",
|
||||
"pino-pretty": "^7.3.0",
|
||||
"reflect-metadata": "^0.1.13",
|
||||
"rxjs": "^7.5.2",
|
||||
"socket.io": "^4.4.1",
|
||||
"source-map-support": "^0.5.21",
|
||||
"tap": "^15.1.6",
|
||||
"typescript": "^4.5.4",
|
||||
"ws": "^8.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@shopify/eslint-plugin": "^41.0.1",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node": "^17.0.8",
|
||||
"@typescript-eslint/eslint-plugin": "^5.8.1",
|
||||
"@typescript-eslint/parser": "^5.8.1",
|
||||
"concurrently": "^7.0.0",
|
||||
"eslint": "^8.5.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"fastify-tsconfig": "^1.0.1",
|
||||
"jest": "^27.4.7",
|
||||
"prettier": "^2.5.1",
|
||||
"ts-jest": "^27.1.2",
|
||||
"ts-node": "^10.4.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "ts-node src/index.ts",
|
||||
"lint": "eslint . --ext .ts"
|
||||
"build:ts": "tsc",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"test": "npm run build:ts && tsc -p test/tsconfig.json && tap --no-coverage-map --no-check-coverage --ts test/**/*.test.ts"
|
||||
}
|
||||
}
|
||||
|
29
src/app.ts
Normal file
29
src/app.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import 'reflect-metadata';
|
||||
|
||||
import {resolve} from 'path/posix';
|
||||
|
||||
import dotenv from 'dotenv';
|
||||
import fastify from 'fastify';
|
||||
import socketioServer from 'fastify-socket.io';
|
||||
import {bootstrap} from 'fastify-decorators';
|
||||
import fastifySensible from 'fastify-sensible';
|
||||
import fastifyHelmet from 'fastify-helmet';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const build = (opts = {}) => {
|
||||
const app = fastify(opts);
|
||||
|
||||
app
|
||||
.register(fastifyHelmet, {contentSecurityPolicy: false})
|
||||
.register(socketioServer)
|
||||
.register(fastifySensible)
|
||||
.register(bootstrap, {
|
||||
directory: resolve(__dirname, 'controllers'),
|
||||
mask: /\.controller\./,
|
||||
});
|
||||
|
||||
return app;
|
||||
};
|
||||
|
||||
export {build};
|
79
src/controllers/channel.controller.ts
Normal file
79
src/controllers/channel.controller.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import {FastifyInstance, FastifyReply, FastifyRequest} from 'fastify';
|
||||
import {
|
||||
Controller,
|
||||
FastifyInstanceToken,
|
||||
GET,
|
||||
Hook,
|
||||
Inject,
|
||||
} from 'fastify-decorators';
|
||||
import {Socket} from 'socket.io';
|
||||
import {DefaultEventsMap} from 'socket.io/dist/typed-events';
|
||||
|
||||
import {LndService} from '../services/lnd-service';
|
||||
|
||||
/**
|
||||
* Handles channel information methods
|
||||
*/
|
||||
@Controller({route: '/channel'})
|
||||
export default class ChannelController {
|
||||
@Inject(FastifyInstanceToken)
|
||||
public instance!: FastifyInstance;
|
||||
|
||||
constructor(private readonly lndService: LndService) {
|
||||
lndService.channelUpdateSubject.subscribe((data) => {
|
||||
for (const channel of data) {
|
||||
lndService
|
||||
.getChannel(channel.chan_id)
|
||||
.then((chanInfo) => {
|
||||
this.instance.io
|
||||
.to([chanInfo.node1_pub, chanInfo.node2_pub, chanInfo.channel_id])
|
||||
.emit('channel', chanInfo);
|
||||
this.instance.io
|
||||
.of('/channel')
|
||||
.to(channel.chan_id)
|
||||
.emit('channel', chanInfo);
|
||||
this.instance.io
|
||||
.of('/node')
|
||||
.to([chanInfo.node1_pub, chanInfo.node2_pub])
|
||||
.emit('channel', chanInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@GET({url: '/:channelId'})
|
||||
async getChannel(
|
||||
request: FastifyRequest<{Params: {channelId: string}}>,
|
||||
reply: FastifyReply<any>,
|
||||
) {
|
||||
if (isNaN(Number(request.params.channelId))) return reply.notFound();
|
||||
|
||||
return this.lndService.getChannel(request.params.channelId);
|
||||
}
|
||||
|
||||
@Hook('onReady')
|
||||
registerWsHandlers() {
|
||||
const channelNamespace = this.instance.io.of('/channel');
|
||||
|
||||
channelNamespace.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
socket.join(data);
|
||||
this.lndService
|
||||
.getChannel(data)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('channel', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
socket.on('unsubscribe_all', (_data) => {
|
||||
for (const room of socket.rooms) {
|
||||
socket.leave(room);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
60
src/controllers/main.controller.ts
Normal file
60
src/controllers/main.controller.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import {FastifyInstance} from 'fastify';
|
||||
import {
|
||||
Controller,
|
||||
FastifyInstanceToken,
|
||||
Hook,
|
||||
Inject,
|
||||
} from 'fastify-decorators';
|
||||
import {Socket} from 'socket.io';
|
||||
import {DefaultEventsMap} from 'socket.io/dist/typed-events';
|
||||
|
||||
import {LndService} from '../services/lnd-service';
|
||||
|
||||
/**
|
||||
* Registers the root Socket.IO endpoints
|
||||
*/
|
||||
@Controller({
|
||||
route: '/',
|
||||
})
|
||||
export default class MainController {
|
||||
@Inject(FastifyInstanceToken)
|
||||
public instance!: FastifyInstance;
|
||||
|
||||
@Inject(LndService)
|
||||
private readonly lndService!: LndService;
|
||||
|
||||
@Hook('onReady')
|
||||
registerWsHandlers() {
|
||||
this.instance.io.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe_channel', (data) => {
|
||||
for (const channelId of data.data) {
|
||||
socket.join(channelId);
|
||||
}
|
||||
});
|
||||
socket.on('subscribe_pubkey', (data) => {
|
||||
for (const pubKey of data.data) {
|
||||
socket.join(pubKey);
|
||||
this.lndService
|
||||
.getNode(pubKey)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('pubkey', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
socket.on('unsubscribe_pubkey', (data) => {
|
||||
for (const pubKey of data.data) {
|
||||
socket.leave(pubKey);
|
||||
}
|
||||
});
|
||||
socket.on('unsubscribe_all', (_data) => {
|
||||
for (const room of socket.rooms) {
|
||||
socket.leave(room);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
67
src/controllers/node.controller.ts
Normal file
67
src/controllers/node.controller.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import {FastifyRequest, FastifyReply, FastifyInstance} from 'fastify';
|
||||
import {
|
||||
Controller,
|
||||
FastifyInstanceToken,
|
||||
GET,
|
||||
Hook,
|
||||
Inject,
|
||||
} from 'fastify-decorators';
|
||||
import {Socket} from 'socket.io';
|
||||
import {DefaultEventsMap} from 'socket.io/dist/typed-events';
|
||||
|
||||
import {LndService} from '../services/lnd-service';
|
||||
|
||||
/**
|
||||
* Handles node information methods
|
||||
*/
|
||||
@Controller({route: '/node'})
|
||||
export default class NodeController {
|
||||
@Inject(FastifyInstanceToken)
|
||||
public instance!: FastifyInstance;
|
||||
|
||||
constructor(private readonly lndService: LndService) {
|
||||
lndService.nodeUpdateSubject.subscribe((data) => {
|
||||
for (const node of data) {
|
||||
lndService
|
||||
.getNode(node.identity_key)
|
||||
.then((nodeInfo) => {
|
||||
this.instance.io.to(nodeInfo.node.pub_key).emit('pubkey', nodeInfo);
|
||||
this.instance.io
|
||||
.of('/node')
|
||||
.to(nodeInfo.node.pub_key)
|
||||
.emit('node', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@GET({url: '/:pubKey'})
|
||||
async getNode(request: FastifyRequest<any>, reply: FastifyReply<any>) {
|
||||
if (request.params.pubKey.length !== 66) return reply.notFound();
|
||||
|
||||
return this.lndService.getNode(request.params.pubKey);
|
||||
}
|
||||
|
||||
@Hook('onReady')
|
||||
registerWsHandlers() {
|
||||
const nodeNamespace = this.instance.io.of('/node');
|
||||
|
||||
nodeNamespace.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
for (const pubKey of data) {
|
||||
socket.join(pubKey);
|
||||
this.lndService
|
||||
.getNode(pubKey)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('node', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
194
src/index.ts
194
src/index.ts
@ -1,194 +0,0 @@
|
||||
import dotenv from 'dotenv';
|
||||
import fastify, {FastifyReply, FastifyRequest} from 'fastify';
|
||||
import fastifyEnv from 'fastify-env';
|
||||
import fastifySensible from 'fastify-sensible';
|
||||
import socketioServer from 'fastify-socket.io';
|
||||
import {Socket} from 'socket.io';
|
||||
import {DefaultEventsMap} from 'socket.io/dist/typed-events';
|
||||
|
||||
import {LndService} from './lightning/lnd-service';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const lndService = new LndService(
|
||||
process.env.LND_REST_API || 'localhost:8080',
|
||||
process.env.MACAROON || '',
|
||||
);
|
||||
|
||||
lndService.run();
|
||||
|
||||
const envSchema = {
|
||||
type: 'object',
|
||||
required: ['PORT'],
|
||||
properties: {
|
||||
PORT: {
|
||||
type: 'string',
|
||||
default: 3000,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const envOptions = {
|
||||
dotenv: true,
|
||||
schema: envSchema,
|
||||
};
|
||||
|
||||
const server = fastify({
|
||||
logger: true,
|
||||
});
|
||||
|
||||
server.register(socketioServer);
|
||||
// server.register(helmet, {enableCSPNonces: true});
|
||||
server.register(fastifyEnv, envOptions);
|
||||
server.register(fastifySensible);
|
||||
|
||||
server.get(
|
||||
'/node/:pubKey',
|
||||
{
|
||||
schema: {
|
||||
querystring: {
|
||||
pubKey: {type: 'string'},
|
||||
},
|
||||
},
|
||||
},
|
||||
async (request: FastifyRequest<any>, reply: FastifyReply<any>) => {
|
||||
if (request.params.pubKey.length !== 66) reply.notFound();
|
||||
|
||||
return lndService.getNode(request.params.pubKey);
|
||||
},
|
||||
);
|
||||
|
||||
server.get(
|
||||
'/channel/:channelId',
|
||||
{
|
||||
schema: {
|
||||
querystring: {
|
||||
channelId: {type: 'integer'},
|
||||
},
|
||||
},
|
||||
},
|
||||
async (request: FastifyRequest<any>, reply: FastifyReply<any>) => {
|
||||
return lndService.getChannel(request.params.channelId);
|
||||
},
|
||||
);
|
||||
|
||||
lndService.channelUpdateSubject.subscribe((data) => {
|
||||
for (const channel of data) {
|
||||
lndService
|
||||
.getChannel(channel.chan_id)
|
||||
.then((chanInfo) => {
|
||||
server.io
|
||||
.to([chanInfo.node1_pub, chanInfo.node2_pub, chanInfo.channel_id])
|
||||
.emit('channel', chanInfo);
|
||||
server.io.of('/channel').to(channel.chan_id).emit('channel', chanInfo);
|
||||
server.io
|
||||
.of('/node')
|
||||
.to([chanInfo.node1_pub, chanInfo.node2_pub])
|
||||
.emit('channel', chanInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
lndService.nodeUpdateSubject.subscribe((data) => {
|
||||
for (const node of data) {
|
||||
lndService
|
||||
.getNode(node.identity_key)
|
||||
.then((nodeInfo) => {
|
||||
server.io.to(nodeInfo.node.pub_key).emit('pubkey', nodeInfo);
|
||||
server.io.of('/node').to(nodeInfo.node.pub_key).emit('node', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
server.ready((err: Error) => {
|
||||
if (err) throw err;
|
||||
|
||||
const nodeNamespace = server.io.of('/node');
|
||||
const channelNamespace = server.io.of('/channel');
|
||||
|
||||
server.io.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe_channel', (data) => {
|
||||
for (const channelId of data.data) {
|
||||
socket.join(channelId);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('subscribe_pubkey', (data) => {
|
||||
for (const pubKey of data.data) {
|
||||
socket.join(pubKey);
|
||||
|
||||
lndService
|
||||
.getNode(pubKey)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('pubkey', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('unsubscribe_pubkey', (data) => {
|
||||
for (const pubKey of data.data) {
|
||||
socket.leave(pubKey);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('unsubscribe_all', (_data) => {
|
||||
for (const room of socket.rooms) {
|
||||
socket.leave(room);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
channelNamespace.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
socket.join(data);
|
||||
|
||||
lndService
|
||||
.getChannel(data)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('channel', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
});
|
||||
|
||||
socket.on('unsubscribe_all', (_data) => {
|
||||
for (const room of socket.rooms) {
|
||||
socket.leave(room);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
nodeNamespace.on(
|
||||
'connection',
|
||||
(socket: Socket<DefaultEventsMap, DefaultEventsMap>) => {
|
||||
socket.on('subscribe', (data) => {
|
||||
for (const pubKey of data) {
|
||||
socket.join(pubKey);
|
||||
|
||||
lndService
|
||||
.getNode(pubKey)
|
||||
.then((nodeInfo) => {
|
||||
socket.emit('node', nodeInfo);
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
server.listen(process.env.PORT || 3000, (err, address) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Server listening at ${address}`);
|
||||
});
|
15
src/server.ts
Normal file
15
src/server.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import {build} from './app';
|
||||
|
||||
const server = build({
|
||||
logger: {
|
||||
level: 'info',
|
||||
prettyPrint: true,
|
||||
},
|
||||
});
|
||||
|
||||
server.listen(3000, (err, address) => {
|
||||
if (err) {
|
||||
server.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
@ -3,6 +3,10 @@ import * as https from 'https';
|
||||
import axios from 'axios';
|
||||
import {Subject} from 'rxjs';
|
||||
import WebSocket from 'ws';
|
||||
import {Initializer, Service} from 'fastify-decorators';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const httpsAgent = new https.Agent({
|
||||
rejectUnauthorized: false,
|
||||
@ -21,13 +25,24 @@ interface GraphUpdateResult {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Responsible for connecting to LND in several ways
|
||||
*/
|
||||
@Service()
|
||||
export class LndService {
|
||||
ws: WebSocket;
|
||||
ws!: WebSocket;
|
||||
channelUpdateSubject: Subject<any> = new Subject();
|
||||
nodeUpdateSubject: Subject<any> = new Subject();
|
||||
closedChannelSubject: Subject<any> = new Subject();
|
||||
lndRestApiUrl!: string;
|
||||
macaroon!: string;
|
||||
|
||||
constructor(protected lndRestApiUrl: string, protected macaroon: string) {
|
||||
constructor() {}
|
||||
|
||||
@Initializer()
|
||||
init() {
|
||||
this.lndRestApiUrl = process.env.LND_REST_API || 'localhost:8080';
|
||||
this.macaroon = process.env.MACAROON || '';
|
||||
this.ws = new WebSocket(
|
||||
`wss://${this.lndRestApiUrl}/v1/graph/subscribe?method=GET`,
|
||||
{
|
13
test/app.test.ts
Normal file
13
test/app.test.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import tap from 'tap';
|
||||
|
||||
import {testBuild} from '../test/helper';
|
||||
|
||||
tap.test('requests the "/" route', async (t: any) => {
|
||||
const app = await testBuild(t);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
});
|
||||
t.equal(response.statusCode, 200, 'returns a status code of 200');
|
||||
});
|
18
test/controllers/channel.controller.test.ts
Normal file
18
test/controllers/channel.controller.test.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import tap from 'tap';
|
||||
|
||||
import {testBuild} from '../../test/helper';
|
||||
|
||||
tap.test('Non-numeric channels should return 404', async (t: any) => {
|
||||
t.plan(1);
|
||||
|
||||
const app = await testBuild(t);
|
||||
|
||||
const response = await app.inject({
|
||||
url: '/channel/abcdefg',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
t.equal(response.statusCode, 404, 'returns a status code of 404');
|
||||
|
||||
t.teardown(() => app.close());
|
||||
});
|
18
test/controllers/main.controller.test.ts
Normal file
18
test/controllers/main.controller.test.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import tap from 'tap';
|
||||
|
||||
import {testBuild} from '../../test/helper';
|
||||
|
||||
tap.test('Main controller should do nothing', async (t: any) => {
|
||||
t.plan(1);
|
||||
|
||||
const app = await testBuild(t);
|
||||
|
||||
const response = await app.inject({
|
||||
url: '/',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
t.equal(response.statusCode, 404, 'returns a status code of 404');
|
||||
|
||||
t.teardown(() => app.close());
|
||||
});
|
42
test/controllers/node.controller.test.ts
Normal file
42
test/controllers/node.controller.test.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import tap from 'tap';
|
||||
|
||||
import {testBuild} from '../../test/helper';
|
||||
|
||||
tap.test('Too short pubkeys should return 404', async (t: any) => {
|
||||
t.plan(1);
|
||||
const app = await testBuild(t);
|
||||
const response = await app.inject({
|
||||
url: '/node/abcdefg',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
t.equal(response.statusCode, 404, 'returns a status code of 404');
|
||||
|
||||
t.teardown(() => app.close());
|
||||
});
|
||||
|
||||
tap.test('Too long pubkeys should return 404', async (t: any) => {
|
||||
t.plan(1);
|
||||
const app = await testBuild(t);
|
||||
const response = await app.inject({
|
||||
url: '/node/0380b3dbdf090cacee19eb4dc7a82630bd3de8b12608dd7bee971fb3cd2a5ae2fcd',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
t.equal(response.statusCode, 404, 'returns a status code of 404');
|
||||
|
||||
t.teardown(() => app.close());
|
||||
});
|
||||
|
||||
tap.test('Node URI should return 404', async (t: any) => {
|
||||
t.plan(1);
|
||||
const app = await testBuild(t);
|
||||
const response = await app.inject({
|
||||
url: '/node/0380b3dbdf090cacee19eb4dc7a82630bd3de8b12608dd7bee971fb3cd2a5ae2fc@[2a04:52c0:103:c1e3::1]:9735',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
t.equal(response.statusCode, 404, 'returns a status code of 404');
|
||||
|
||||
t.teardown(() => app.close());
|
||||
});
|
38
test/helper.ts
Normal file
38
test/helper.ts
Normal file
@ -0,0 +1,38 @@
|
||||
// This file contains code that we reuse between our tests.
|
||||
import Fastify from 'fastify'
|
||||
import fp from 'fastify-plugin'
|
||||
import { build } from '../src/app'
|
||||
import * as tap from 'tap';
|
||||
|
||||
export type Test = typeof tap['Test']['prototype'];
|
||||
|
||||
// Fill in this config with all the configurations
|
||||
// needed for testing the application
|
||||
async function config () {
|
||||
return {}
|
||||
}
|
||||
|
||||
// Automatically build and tear down our instance
|
||||
async function testBuild (t: Test) {
|
||||
const app = Fastify()
|
||||
|
||||
// fastify-plugin ensures that all decorators
|
||||
// are exposed for testing purposes, this is
|
||||
// different from the production setup
|
||||
void app.register(fp(build), await config())
|
||||
|
||||
await app.ready();
|
||||
|
||||
// Tear down our app after we are done
|
||||
t.teardown(async() => {
|
||||
if (app)
|
||||
await app.close()
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
export {
|
||||
config,
|
||||
testBuild
|
||||
}
|
9
test/services/lnd-service.test.ts
Normal file
9
test/services/lnd-service.test.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import tap from 'tap';
|
||||
import { LndService } from '../../src/services/lnd-service';
|
||||
|
||||
tap.test('LndService should instantiate', async (t: any) => {
|
||||
t.plan(1)
|
||||
const lndService = new LndService();
|
||||
|
||||
t.type(lndService, LndService);
|
||||
});
|
9
test/tsconfig.json
Normal file
9
test/tsconfig.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["../src/**/*.ts", "**/*.ts"]
|
||||
}
|
||||
|
109
tsconfig.json
109
tsconfig.json
@ -1,101 +1,12 @@
|
||||
{
|
||||
"extends": "fastify-tsconfig",
|
||||
"compilerOptions": {
|
||||
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
||||
|
||||
/* Projects */
|
||||
// "incremental": true, /* Enable incremental compilation */
|
||||
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
||||
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
||||
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||
|
||||
/* Language and Environment */
|
||||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
||||
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
||||
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
||||
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||
|
||||
/* Modules */
|
||||
"module": "commonjs", /* Specify what module code is generated. */
|
||||
// "rootDir": "./", /* Specify the root folder within your source files. */
|
||||
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
||||
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||
// "resolveJsonModule": true, /* Enable importing .json files */
|
||||
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
||||
|
||||
/* JavaScript Support */
|
||||
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
||||
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
||||
|
||||
/* Emit */
|
||||
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
||||
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
||||
// "removeComments": true, /* Disable emitting comments. */
|
||||
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
||||
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
||||
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
||||
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
||||
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||
|
||||
/* Interop Constraints */
|
||||
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
||||
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||
|
||||
/* Type Checking */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
||||
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
||||
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
||||
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
||||
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
||||
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
||||
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
||||
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
||||
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
||||
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||
|
||||
/* Completeness */
|
||||
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||
}
|
||||
}
|
||||
"experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
||||
"emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
|
||||
"outDir": "dist",
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user