Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function init(): Promise {
const SERVER_CONFIG = await config().get('calculator-service')
const ADD_SERVER_CONFIG = await config().get('add-service')
// Create thrift client
const thriftClient: AddService.Client = createHttpClient(
AddService.Client,
{
hostName: ADD_SERVER_CONFIG.host,
port: ADD_SERVER_CONFIG.port,
register: [
ThriftClientZipkinFilter({
localServiceName: 'calculator-service',
remoteServiceName: 'add-service',
tracerConfig: {
endpoint: 'http://localhost:9411/api/v1/spans',
httpInterval: 1000,
httpTimeout: 5000,
sampleRate: 1.0,
},
}),
],
const app = express()
app.use(
ZipkinTracingExpress({
localServiceName: 'calculator-client',
tracerConfig: {
endpoint: 'http://localhost:9411/api/v1/spans',
httpInterval: 1000,
httpTimeout: 5000,
sampleRate: 1.0,
},
}),
)
// Create thrift client
const thriftClient: Calculator.Client = createHttpClient(
Calculator.Client,
{
hostName: SERVER_CONFIG.host,
port: SERVER_CONFIG.port,
register: [
ThriftClientZipkinFilter({
localServiceName: 'calculator-client',
remoteServiceName: 'calculator-service',
tracerConfig: {
endpoint: 'http://localhost:9411/api/v1/spans',
httpInterval: 1000,
httpTimeout: 5000,
sampleRate: 1.0,
},
}),
],
export function createServer(sampleRate: number = 0): express.Application {
// Create thrift client
const addServiceClient: AddService.Client = createHttpClient(
AddService.Client,
{
hostName: ADD_SERVER_CONFIG.hostName,
port: ADD_SERVER_CONFIG.port,
register:
sampleRate > 0
? [
ThriftClientZipkinFilter({
localServiceName: 'calculator-service',
remoteServiceName: 'add-service',
tracerConfig: {
endpoint: process.env.ZIPKIN_ENDPOINT,
zipkinVersion:
process.env.ZIPKIN_VERSION === 'v2'
? 'v2'
: 'v1',
export async function createServer(
sampleRate: number = 0,
protocolType: ProtocolType = 'binary',
): Promise {
// Create thrift client
const addServiceClient: AddService.Client = createHttpClient(
AddService.Client,
{
hostName: ADD_SERVER_CONFIG.hostName,
port: ADD_SERVER_CONFIG.port,
register:
sampleRate > 0
? [
ThriftClientZipkinFilter({
localServiceName: 'calculator-service',
remoteServiceName: 'add-service',
tracerConfig: {
endpoint: process.env.ZIPKIN_ENDPOINT,
zipkinVersion:
process.env.ZIPKIN_VERSION === 'v2'
? 'v2'
: 'v1',
app.use([
ZipkinTracingExpress({
localServiceName: 'calculator-client',
tracerConfig: {
endpoint: process.env.ZIPKIN_ENDPOINT,
zipkinVersion:
process.env.ZIPKIN_VERSION === 'v2' ? 'v2' : 'v1',
sampleRate,
httpInterval: 0,
},
}),
])
}
// Create thrift client
const thriftClient: Calculator.Client = createHttpClient(
Calculator.Client,
{
hostName: HAPI_CALC_SERVER_CONFIG.hostName,
port: HAPI_CALC_SERVER_CONFIG.port,
protocol: protocolType,
register:
sampleRate > 0
? [
ThriftClientZipkinFilter({
localServiceName: 'calculator-client',
remoteServiceName: 'calculator-service',
tracerConfig: {
endpoint: process.env.ZIPKIN_ENDPOINT,
zipkinVersion:
process.env.ZIPKIN_VERSION === 'v2'
? 'v2'
it('should reject for a request to a missing service', async () => {
const badClient: Calculator.Client = createTcpClient<
Calculator.Client
>(Calculator.Client, {
hostName: 'fakehost',
port: 8888,
})
return badClient.add(5, 7).then(
(response: number) => {
console.log('res: ', response)
throw new Error('Should reject with host not found')
},
(err: any) => {
console.log('err: ', err)
expect(err).to.exist()
},
)
it('should reject when middleware rejects', async () => {
const client = createHttpClient(Calculator.Client, {
hostName: HAPI_CALC_SERVER_CONFIG.hostName,
port: HAPI_CALC_SERVER_CONFIG.port,
register: [
{
handler(
request: IThriftRequest,
next: NextFunction,
): Promise {
return next().then((res: IRequestResponse) => {
if (readThriftMethod(res.body) === 'nope') {
return Promise.resolve(res)
} else {
return Promise.reject(
new Error(
`Unrecognized method name: ${readThriftMethod(
res.body,
before(async () => {
calcServer = await createCalcServer()
addServer = await createAddServer()
client = createHttpClient(Calculator.Client, HAPI_CALC_SERVER_CONFIG)
return Promise.all([calcServer.start(), addServer.start()]).then(() => {
console.log('Thrift server started')
})
})
return new Promise((resolve, reject) => {
const app: express.Application = createCalcServer()
client = createHttpClient(
Calculator.Client,
EXPRESS_CALC_SERVER_CONFIG,
)
calcServer = app.listen(EXPRESS_CALC_SERVER_CONFIG.port, () => {
console.log(
`Express server listening on port: ${EXPRESS_CALC_SERVER_CONFIG.port}`,
)
addServer.start().then(() => resolve())
})
})
})
it('should reject when middleware does not add auth token', async () => {
const connection: HttpConnection = new HttpConnection({
serviceName: 'Calculator',
hostName: HAPI_CALC_SERVER_CONFIG.hostName,
port: HAPI_CALC_SERVER_CONFIG.port,
path: HAPI_CALC_SERVER_CONFIG.path,
})
const client = new Calculator.Client(connection)
return client.addWithContext(5, 7).then(
(response: number) => {
throw new Error(
`Mehtods should fail when middleware rejects`,
)
},
(err: any) => {
expect(err.message).to.equal('Unauthorized')
},