-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minor upgrade to the hyperschema project
- Loading branch information
1 parent
ab150e0
commit ae8043a
Showing
9 changed files
with
187 additions
and
135 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
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
89 changes: 0 additions & 89 deletions
89
packages/typescript/hyperschema-core/src/hrpc/transport/index.ts
This file was deleted.
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
82 changes: 82 additions & 0 deletions
82
packages/typescript/hyperschema-core/src/hrpc/transport/socketio.ts
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import * as socketIO from 'socket.io'; | ||
|
||
import { HyperRPCService, MetaObject } from '..'; | ||
import { AbstractTransportEngine, processError } from './logic'; | ||
|
||
function setupServiceEvents(socket: socketIO.Socket, service: HyperRPCService) { | ||
const cleanupFns: (() => void)[] = []; | ||
|
||
// setup service events | ||
Object.entries(service.events).forEach(([name, event]) => { | ||
if (event.emitterSetup) { | ||
const eventPath = service.path === '' ? name : `${service.path}/${name}`; | ||
const cleanup = event.emitterSetup((x) => { | ||
socket.emit(eventPath, event.eventType.parse(x)); | ||
}, socket.data); | ||
if (cleanup) { | ||
cleanupFns.push(cleanup); | ||
} | ||
} | ||
}); | ||
|
||
// setup child service events | ||
Object.values(service.subservices).forEach((childService) => { | ||
setupServiceEvents(socket, childService); | ||
}); | ||
|
||
socket.on('disconnect', () => { | ||
cleanupFns.forEach((fn) => fn()); | ||
}); | ||
} | ||
|
||
export class SocketIOTransport implements AbstractTransportEngine { | ||
io: socketIO.Server; | ||
constructor(io: socketIO.Server) { | ||
this.io = io; | ||
} | ||
|
||
mount(service: HyperRPCService): void { | ||
this.io.use((socket, next) => { | ||
const meta: MetaObject = { | ||
connId: socket.id, | ||
authToken: socket.handshake.auth.token, | ||
}; | ||
|
||
// merge the context into socket.data | ||
service.hyperRPC | ||
.contextFn({ $meta: meta }) | ||
.then((res: any) => { | ||
Object.keys(res).forEach((k) => { | ||
socket.data.$meta = meta; | ||
socket.data[k] = res[k]; | ||
}); | ||
next(); | ||
}) | ||
.catch(next); | ||
}); | ||
this.io.on('connection', async (socket) => { | ||
socket.onAny( | ||
(fullPath: string, args: unknown, callback: (resp: any) => void) => { | ||
try { | ||
const path = fullPath.split('/'); | ||
const functionName = path.pop()!; | ||
const endpointSvc = path.reduce( | ||
(acc, x) => acc.subservices[x], | ||
service, | ||
); | ||
|
||
const fn = endpointSvc.functions[functionName]; | ||
fn.call(socket.data, args) | ||
.then((ok) => callback({ ok })) | ||
.catch((err) => callback({ err: processError(err) })); | ||
} catch (err) { | ||
callback({ err: processError(err) }); | ||
} | ||
}, | ||
); | ||
|
||
setupServiceEvents(socket, service); | ||
socket.emit('ready'); | ||
}); | ||
} | ||
} |
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,6 +1,44 @@ | ||
import { HyperRPCService } from './hrpc'; | ||
import { AbstractTransportEngine } from './hrpc/transport/logic'; | ||
import { HyperschemaWriter } from './writers'; | ||
|
||
export * from './reflector'; | ||
export * from './hrpc'; | ||
export * from './hrpc/transport'; | ||
export * from './hrpc/transport/socketio'; | ||
export * from './errors'; | ||
export * from './generator'; | ||
export * from './writers'; | ||
export * from './types'; | ||
|
||
export interface HyperschemaServerOptions { | ||
system: any; | ||
root: HyperRPCService<any>; | ||
transports?: AbstractTransportEngine[]; | ||
writers?: HyperschemaWriter[]; | ||
} | ||
|
||
export interface HyperschemaStartOption { | ||
generate?: boolean; | ||
} | ||
|
||
export class HyperschemaServer { | ||
system: any; | ||
root: HyperRPCService<any>; | ||
transports: AbstractTransportEngine[]; | ||
writers: HyperschemaWriter[]; | ||
|
||
constructor(options: HyperschemaServerOptions) { | ||
this.system = options.system; | ||
this.root = options.root; | ||
this.transports = options.transports || []; | ||
|
||
this.transports.forEach((t) => t.mount(this.root)); | ||
this.writers = options.writers || []; | ||
} | ||
|
||
async start(options: HyperschemaStartOption = {}) { | ||
if (options.generate) { | ||
await Promise.all(this.writers.map((w) => w.write(this.system))); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { writeTypeScriptClient } from '../generator'; | ||
import { writeHyperschema } from '../reflector'; | ||
|
||
// Writers write the Hyperschema defintions to a file (or any other output) | ||
export interface HyperschemaWriter { | ||
write(hs: any): Promise<void>; | ||
} | ||
|
||
export class JSONWriter implements HyperschemaWriter { | ||
constructor(public path: string) {} | ||
|
||
async write(hs: any) { | ||
writeHyperschema(this.path, hs); | ||
} | ||
} | ||
|
||
export class TypeScriptWriter implements HyperschemaWriter { | ||
constructor(public path: string) {} | ||
|
||
async write(hs: any) { | ||
writeTypeScriptClient(this.path, hs); | ||
} | ||
} |