Skip to content

Commit e6895c6

Browse files
author
Immanuel Pelzer
committed
Adapt to mr
1 parent b34f001 commit e6895c6

13 files changed

+107
-127
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
DS_Store

.vscode/launch.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
3+
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
4+
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Run AVA test",
11+
"program": "${workspaceFolder}/node_modules/ava/profile.js",
12+
"args": [
13+
"${file}"
14+
],
15+
"skipFiles": [
16+
"<node_internals>/**/*.js"
17+
]
18+
}
19+
]
20+
}
File renamed without changes.

src/components/requester.js components/requester.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,37 @@ const upstreamCote = require('@cloud/cote')
66
const debug = require('debug')('cote-http-req')
77
const config = require('../config')
88

9-
let env = '-'
109

1110
class Requester {
1211
constructor(options, discoveryOptions = {}) {
1312
this.key = options.key || 'default'
14-
this.targetHostname = options.hostname || this.key
13+
this.uri = options.uri
14+
this.port = options.port || config.http.port
15+
if (this.uri) {
16+
if (!this.uri.includes('http://') && !this.uri.includes('https://')) {
17+
throw new Error('Invalid uri format. Needs to be prefixed by https:// or http://')
18+
}
19+
}
1520
if (!options.name) options.name = `Requester ${crypto.randomBytes(3).toString('hex')}`
16-
// Create a new upstream cote responder while preserving passed env
17-
18-
upstreamCote.Requester.constructor._environment = `${env}:`
1921
this.cote = new upstreamCote.Requester(options, { log: false, ...discoveryOptions })
2022
}
2123

22-
static setEnvironment(environment) {
23-
if (!environment) return
24-
env = environment
25-
}
26-
2724
async send(payload, callback) {
2825
debug('sending', payload)
2926
const key = escape(this.key)
3027
let httpRes
28+
29+
// If uri is not set use normal cote
30+
if (!this.uri) {
31+
debug('using cote')
32+
if (callback) return this.cote.send(payload, callback)
33+
return this.cote.send(payload)
34+
}
35+
3136
// Try to communicate over http
3237
try {
33-
httpRes = await got(`http://${this.targetHostname}:${config.http.port}/${env}/${key}/${payload.type}`, {
38+
debug('using http')
39+
httpRes = await got(`${this.uri}:${this.port}/${key}/${payload.type}`, {
3440
method: 'POST',
3541
headers: {
3642
'content-type': 'application/json'
@@ -39,13 +45,6 @@ class Requester {
3945
})
4046
} catch (error) {
4147
debug(error)
42-
// If there are 404s use cote instead
43-
if (error.statusCode == 404) {
44-
debug('using cote fallback')
45-
if (callback) return this.cote.send(payload, callback)
46-
return this.cote.send(payload)
47-
}
48-
4948
throw error
5049
}
5150

src/components/responder.js components/responder.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,31 @@ const upstreamCote = require('@cloud/cote')
55
const debug = require('debug')('cote-http-res')
66
const app = require('./app')
77

8-
const occupiedOn = {}
9-
108
let responders = {}
119

12-
let env = '-'
13-
1410
class Responder {
1511
constructor(options, discoveryOptions = {}) {
1612
this.key = options.key || 'default'
1713
// Is namespaced Responder which cant be handled yet, fallback to upstream cote
1814
if (!options.name) options.name = `Responder ${crypto.randomBytes(3).toString('hex')}`
1915
if (!responders[this.key]) {
20-
// Create a new upstream cote responder while preserving passed env
21-
upstreamCote.Responder.constructor._environment = `${env}:`
2216
const rsp = new upstreamCote.Responder(options, { log: false, ...discoveryOptions })
2317
// Return upstream cote if undhandled use case (namespaced and respondsTo for now)
2418
if (options.namespace || options.respondsTo) return rsp
2519
responders[this.key] = rsp
2620
}
2721
}
2822

29-
static setEnvironment(environment) {
30-
if (!environment) return
31-
env = environment
32-
}
33-
3423
on(name, fn) {
3524
debug('did setup responder', name)
3625
// Validate expected inputs
3726
if (!name || typeof name != 'string') throw new Error('Invalid first parameter, needs to be a string')
3827
if (!fn || typeof fn != 'function') throw new Error('Invalid second parameter, needs to be function')
3928
// Setup this also in cote
4029
responders[this.key].on(name, fn)
41-
// This library can't handle Responders targeted at a Sockend
42-
if (this.upstreamCoteUsage) return
43-
// Check if this name is occupied already
44-
if (occupiedOn[name]) throw new Error(`on(${name},fn) is already occupied`)
45-
occupiedOn[name] = true
4630
// Setup express endpoint
4731
name = escape(name)
48-
app.post(`/${env}/${this.key}/${name}`, (request, response) => {
32+
app.post(`/${this.key}/${name}`, (request, response) => {
4933
debug('recieved req', request.body)
5034
function callback(err, res) {
5135
if (!err) return send(res)
File renamed without changes.

index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* eslint-disable global-require */
2+
/* eslint-disable no-underscore-dangle */
3+
const cote = require('@cloud/cote')
4+
5+
const coteHttp = { ...cote }
6+
coteHttp.Requester = require('./components/requester')
7+
coteHttp.Responder = require('./components/responder')
8+
9+
module.exports = coteHttp
File renamed without changes.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "@immanuel/cote-http",
3-
"version": "0.0.7",
3+
"version": "0.0.11",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "ava test/*.test.js --verbose"
7+
"test": "ava test/*.test.js --verbose --serial"
88
},
99
"publishConfig": {
1010
"registry": "https://npm.unueng.com"

src/index.js

-50
This file was deleted.

test/fallbacks.test.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
/* eslint-disable global-require */
2-
import r from 'randomstring'
3-
42
process.env.COTE_HTTP_PORT = 3334
53

64
const test = require('ava')
5+
const coteHttp = require('../')
6+
const cote = require('@cloud/cote')
77

88
test('req res model requester does fallback to cote on 404', async(t) => {
9-
const environment = r.generate()
10-
11-
const coteHttp = require('../src/index')({ environment })
12-
const cote = require('@cloud/cote')({ environment })
139
const responder = new cote.Responder({ key: 'localhost', name: 'test responder' })
1410
const request = { type: 'test11' }
1511
const response = 'bla'
@@ -22,15 +18,11 @@ test('req res model requester does fallback to cote on 404', async(t) => {
2218
})
2319

2420
const res1 = await requester.send(request)
21+
t.falsy(res1.http)
2522
t.is(response, res1)
2623
})
2724

2825
test('req res model cote responders are compatible with cote-http', async(t) => {
29-
const environment = r.generate()
30-
31-
const coteHttp = require('../src/index')({ environment })
32-
const cote = require('@cloud/cote')({ environment })
33-
3426
const responder = new coteHttp.Responder({ key: 'localhost', name: 'test responder' })
3527

3628
const request = { type: 'test23' }
@@ -47,4 +39,13 @@ test('req res model cote responders are compatible with cote-http', async(t) =>
4739

4840
const res1 = await requester.send(request)
4941
t.is(res1, response)
42+
43+
const requester2 = new coteHttp.Requester({
44+
key: 'localhost',
45+
uri: 'http://localhost',
46+
name: 'test requester'
47+
})
48+
49+
const res2 = await requester2.send(request)
50+
t.is(response, res2)
5051
})

0 commit comments

Comments
 (0)