From 6d87c6f1bfb822686330c2c7c47baebc6b09bb12 Mon Sep 17 00:00:00 2001 From: TheCactusBlue Date: Sat, 24 Feb 2024 01:13:36 +1000 Subject: [PATCH] bump version again --- apps/typescript/server/src/index.ts | 11 ++----- .../typescript/hyperschema-core/package.json | 2 +- .../src/hrpc/transport/logic.ts | 1 + .../src/hrpc/transport/socketio.ts | 29 +++++++++++++++++-- .../typescript/hyperschema-core/src/index.ts | 2 ++ 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/typescript/server/src/index.ts b/apps/typescript/server/src/index.ts index 88dc214..3c24ba9 100644 --- a/apps/typescript/server/src/index.ts +++ b/apps/typescript/server/src/index.ts @@ -19,11 +19,9 @@ const hss = new HyperschemaServer({ system: hs, root: hs.MainService, transports: [ - new SocketIOTransport( - new socketIO.Server(app.server, { - cors: { origin: '*' }, - }), - ), + new SocketIOTransport({ + port: 3100, + }), ], writers: [ new TypeScriptWriter(path.join(__dirname, '../../client/src/hs-client.ts')), @@ -34,9 +32,6 @@ const hss = new HyperschemaServer({ console.log('generating hyperschema...'); await hss.start({ generate: true }); console.log('generated hyperschema!'); - app.listen({ port: 3100 }, () => { - console.log('listening on port 3100'); - }); })().then(() => { console.log('server started'); }); diff --git a/packages/typescript/hyperschema-core/package.json b/packages/typescript/hyperschema-core/package.json index 3b8508d..3cfd8c6 100644 --- a/packages/typescript/hyperschema-core/package.json +++ b/packages/typescript/hyperschema-core/package.json @@ -4,7 +4,7 @@ "types": "dist/index.d.ts", "packageManager": "yarn@3.4.1", "license": "MIT", - "version": "0.0.10", + "version": "0.0.11", "devDependencies": { "@types/node": "^20.7.0", "@types/ws": "^8.5.10", diff --git a/packages/typescript/hyperschema-core/src/hrpc/transport/logic.ts b/packages/typescript/hyperschema-core/src/hrpc/transport/logic.ts index 1c0e5d9..1acd70c 100644 --- a/packages/typescript/hyperschema-core/src/hrpc/transport/logic.ts +++ b/packages/typescript/hyperschema-core/src/hrpc/transport/logic.ts @@ -18,4 +18,5 @@ export function processError(err: unknown) { export interface AbstractTransportEngine { mount(service: HyperRPCService): void; + run(): Promise; } diff --git a/packages/typescript/hyperschema-core/src/hrpc/transport/socketio.ts b/packages/typescript/hyperschema-core/src/hrpc/transport/socketio.ts index 8d8dfdb..c8ad12a 100644 --- a/packages/typescript/hyperschema-core/src/hrpc/transport/socketio.ts +++ b/packages/typescript/hyperschema-core/src/hrpc/transport/socketio.ts @@ -1,3 +1,4 @@ +import fastify from 'fastify'; import * as socketIO from 'socket.io'; import { HyperRPCService, MetaObject } from '..'; @@ -29,10 +30,34 @@ function setupServiceEvents(socket: socketIO.Socket, service: HyperRPCService) { }); } +interface SocketIOTransportOptions { + io?: socketIO.Server; + port?: number; + meta?: any; +} + export class SocketIOTransport implements AbstractTransportEngine { io: socketIO.Server; - constructor(io: socketIO.Server) { - this.io = io; + run: () => Promise; + + constructor(options: SocketIOTransportOptions) { + if (options.io) { + this.io = options.io; + this.run = async () => {}; + } else { + const app = fastify(); + app.get('/', async () => { + return options.meta ?? { hello: 'world' }; + }); + + this.io = new socketIO.Server(app.server, { + cors: { origin: '*' }, + }); + + this.run = async () => { + await app.listen({ port: options.port ?? 3510 }); + }; + } } mount(service: HyperRPCService): void { diff --git a/packages/typescript/hyperschema-core/src/index.ts b/packages/typescript/hyperschema-core/src/index.ts index a428fc4..f1f5df8 100644 --- a/packages/typescript/hyperschema-core/src/index.ts +++ b/packages/typescript/hyperschema-core/src/index.ts @@ -40,5 +40,7 @@ export class HyperschemaServer { if (options.generate) { await Promise.all(this.writers.map((w) => w.write(this.system))); } + + await Promise.all(this.transports.map((t) => t.run())); } }