feat(server): basic implementation of server runtime and SDK (#13)
* feat(server): basic implementation of server runtime and SDK * refactor: better typing
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "@proj-airi/server-sdk",
|
||||
"type": "module",
|
||||
"version": "0.1.1",
|
||||
"private": false,
|
||||
"description": "Client-side SDK implementation for connecting to Airi server components and runtimes",
|
||||
"author": {
|
||||
"name": "Neko Ayaka",
|
||||
"email": "neko@ayaka.moe",
|
||||
"url": "https://github.com/nekomeowww"
|
||||
},
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/moeru-ai/airi.git",
|
||||
"directory": "packages/server-sdk"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.mjs",
|
||||
"require": "./dist/index.cjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
"types": "./dist/index.d.ts",
|
||||
"files": [
|
||||
"README.md",
|
||||
"dist",
|
||||
"package.json"
|
||||
],
|
||||
"scripts": {
|
||||
"dev": "pnpm run stub",
|
||||
"stub": "unbuild --stub",
|
||||
"build": "unbuild",
|
||||
"package:publish": "pnpm build && pnpm publish --access public --no-git-checks"
|
||||
},
|
||||
"dependencies": {
|
||||
"@proj-airi/server-shared": "workspace:^",
|
||||
"crossws": "^0.3.1",
|
||||
"defu": "^6.1.4"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import type { WebSocketEvent, WebSocketEvents } from '@proj-airi/server-shared/types'
|
||||
import type { Blob } from 'node:buffer'
|
||||
import WebSocket from 'crossws/websocket'
|
||||
import { defu } from 'defu'
|
||||
|
||||
export interface ClientOptions {
|
||||
url?: string
|
||||
name: string
|
||||
possibleEvents?: Array<(keyof WebSocketEvents)>
|
||||
}
|
||||
|
||||
export class Client {
|
||||
private websocket: WebSocket
|
||||
|
||||
constructor(options: ClientOptions) {
|
||||
const opts = defu<Required<ClientOptions>, Required<Omit<ClientOptions, 'name'>>[]>(options, { url: 'ws://localhost:6121/ws', possibleEvents: [] })
|
||||
|
||||
this.websocket = new WebSocket(opts.url)
|
||||
this.send({
|
||||
type: 'module:announce',
|
||||
data: {
|
||||
name: opts.name,
|
||||
possibleEvents: opts.possibleEvents,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
send(data: WebSocketEvent): void {
|
||||
this.websocket.send(JSON.stringify(data))
|
||||
}
|
||||
|
||||
sendRaw(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
|
||||
this.websocket.send(data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './client'
|
||||
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"lib": [
|
||||
"ESNext"
|
||||
],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"isolatedModules": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user