-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/crash product suppliers sync (#245)
Co-authored-by: Clément Désiles <[email protected]>
- Loading branch information
1 parent
49212a6
commit a7a76a6
Showing
26 changed files
with
526 additions
and
403 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
import { CollectorApiServer } from "./lib/collector-api"; | ||
import { LiveSyncApiServer } from "./lib/live-sync-api"; | ||
import { SyncApiServer } from "./lib/sync-api"; | ||
import {CollectorApiServer} from "./lib/collector-api"; | ||
import {LiveSyncApiServer} from "./lib/live-sync-api"; | ||
import {SyncApiServer} from "./lib/sync-api"; | ||
import {WsServer} from "./lib/ws-server"; | ||
|
||
const syncApi = new SyncApiServer(process.env.SYNC_API_PORT ?? "3232"); | ||
const collectorApi = new CollectorApiServer( | ||
process.env.COLLECTOR_API_PORT ?? "3333" | ||
); | ||
const liveSyncApi = new LiveSyncApiServer(process.env.LIVE_SYNC_API_PORT ?? '3434'); | ||
const probe = new WsServer(+process.env.PROBE_PORT || 8080) | ||
|
||
syncApi.listen(); | ||
collectorApi.listen(); | ||
liveSyncApi.listen(); | ||
new SyncApiServer(probe).listen(+process.env.SYNC_API_PORT || 3232); | ||
new CollectorApiServer(probe).listen(+process.env.COLLECTOR_API_PORT || 3333); | ||
new LiveSyncApiServer(probe).listen(+process.env.LIVE_SYNC_API_PORT || 3434); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,29 @@ | ||
import express, { Request, Response, Express, NextFunction } from "express"; | ||
import { WsServer } from "./ws-server"; | ||
// @ts-expect-error Express is imported as commonjs module. Raises an error because there is no tsconfig. | ||
import express, {Request, Response, Express, NextFunction} from "express"; | ||
import {WsServer} from "./ws-server"; | ||
|
||
export class Server { | ||
api: Express; | ||
port: number; | ||
|
||
wsServer: WsServer; | ||
|
||
public constructor(port: number) { | ||
public constructor(probe: WsServer) { | ||
this.api = express(); | ||
|
||
const wsServer = WsServer.getInstance(); | ||
|
||
this.api.use((req: Request, res: Response, next: NextFunction) => { | ||
wsServer.sendDataToWS(this.constructor.name, req); | ||
next(); | ||
this.api.get("/healthcheck", (_req, res) => { | ||
res.status(200).send({mock: this.constructor.name}); | ||
}); | ||
|
||
this.api.use((req: Request, res: Response, next: NextFunction) => { | ||
//TODO : make prettier | ||
// send data to probe after parsing params | ||
req.on('close', () => { | ||
probe.sendDataToWS(this.constructor.name, req); | ||
}) | ||
req.on('data', buf => console.log(buf.toString('utf8'))); | ||
next(); | ||
}); | ||
this.port = port; | ||
} | ||
|
||
public async listen() { | ||
console.log(`${this.constructor.name} listening on port \x1b[96m${this.port}\x1b[0m`); | ||
return this.api.listen(this.port); | ||
public async listen(port: number) { | ||
console.log(`${this.constructor.name} listening on port \x1b[96m${port}\x1b[0m`); | ||
return this.api.listen(port); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,43 @@ | ||
import WebSocket, { WebSocketServer } from 'ws'; | ||
import { Request } from "express"; | ||
import {WebSocketServer} from 'ws'; | ||
import {Request} from "express"; | ||
|
||
/** | ||
* Websocket server used by [mock-probe.ts] to wait for and inspect requests during testing. | ||
*/ | ||
export class WsServer { | ||
private static instance: WsServer; | ||
private static server: WebSocketServer; | ||
private static client: WebSocket|null; | ||
|
||
private constructor() { } | ||
|
||
public static getInstance(): WsServer { | ||
if (!WsServer.instance) { | ||
WsServer.instance = new WsServer(); | ||
|
||
WsServer.server = new WebSocketServer({ port: 8080 }); | ||
|
||
WsServer.server.on('connection', (ws: WebSocket) => { | ||
if (WsServer.server.clients.size > 1) { | ||
throw new Error('Too many connection to websocket server !'); | ||
} | ||
WsServer.client = ws; | ||
}); | ||
WsServer.server.on('error', err => { throw err } ) | ||
|
||
console.log(`WS server started on port \x1b[96m8080\x1b[0m`); | ||
} | ||
|
||
return WsServer.instance; | ||
} | ||
|
||
public sendDataToWS(apiName: string, request: Request) { | ||
if (!WsServer.client) return; | ||
|
||
const data = { | ||
apiName, | ||
method: request.method, | ||
headers: request.headers, | ||
url: request.url, | ||
query: request.query, | ||
body: request.body ?? {}, | ||
}; | ||
|
||
WsServer.client.send(JSON.stringify(data)); | ||
private server: WebSocketServer; | ||
|
||
constructor(port: number) { | ||
this.server = new WebSocketServer({port}); | ||
|
||
this.server.on('error', err => { | ||
console.error(err); | ||
}) | ||
|
||
console.log(`Probe listening on port \x1b[96m${port}\x1b[0m`); | ||
} | ||
|
||
/** | ||
* send data to all connected clients | ||
* @param apiName | ||
* @param request | ||
*/ | ||
public sendDataToWS(apiName: string, request: Request) { | ||
const data = { | ||
apiName, | ||
method: request.method, | ||
headers: request.headers, | ||
url: request.url, | ||
query: request.query, | ||
params: request.params, | ||
body: request.body ?? {}, | ||
}; | ||
|
||
// there may be no client if we're not runing an automated test suite | ||
if (this.server.clients.size > 0) { | ||
this.server.clients.forEach((client) => { | ||
client.send(JSON.stringify(data)); | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
{ | ||
"name": "ps_eventbus_e2e_tests", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"test:e2e": "jest --config=jest.config.json --runInBand", | ||
"test:e2e:watch": "jest --config=jest.config.json --watchAll --runInBand", | ||
"test:e2e:detectOpenHandles": "jest --config=jest.config.json --runInBand --detectOpenHandles" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@types/jest": "^29.5.10", | ||
"@types/node": "^20.10.0", | ||
"@types/supertest": "^2.0.16", | ||
"jest-expect-message": "^1.1.3", | ||
"jest-extended": "^4.0.2", | ||
"jest-mock-extended": "^3.0.5", | ||
"jest": "^29.7.0", | ||
"supertest": "6.3.3", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.3.2", | ||
"@types/ws": "^8.5.10", | ||
"ws": "^8.15.1" | ||
}, | ||
"engines": { | ||
"yarn": "please use pnpm", | ||
"npm": "please use pnpm", | ||
"node": ">=20", | ||
"pnpm": ">=8" | ||
} | ||
"name": "ps_eventbus_e2e_tests", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"test:e2e": "jest --config=jest.config.json", | ||
"test:e2e:watch": "jest --config=jest.config.json --watchAll", | ||
"test:e2e:detectOpenHandles": "jest --config=jest.config.json --detectOpenHandles" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.7.0", | ||
"@types/jest": "^29.5.10", | ||
"@types/node": "^20.10.0", | ||
"@types/supertest": "^2.0.16", | ||
"jest-expect-message": "^1.1.3", | ||
"jest-extended": "^4.0.2", | ||
"jest-mock-extended": "^3.0.5", | ||
"jest": "^29.7.0", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.3.2", | ||
"@types/ws": "^8.5.10", | ||
"ws": "^8.16.0", | ||
"rxjs": "^7.8.1", | ||
"ramda": "^0.29.1", | ||
"@types/ramda": "^0.29.1", | ||
"axios": "^1.6.7" | ||
}, | ||
"engines": { | ||
"yarn": "please use pnpm", | ||
"npm": "please use pnpm", | ||
"node": ">=18", | ||
"pnpm": ">=8" | ||
} | ||
} |
Oops, something went wrong.