fixed module standart comparability, now ws sends initial data in message format

This commit is contained in:
Dmitriy Shishkov 2021-09-04 21:28:16 +03:00
parent 8be147c3b8
commit f0df9d9233
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
7 changed files with 83 additions and 5 deletions

View File

@ -2,11 +2,15 @@
"name": "@roomruler/back", "name": "@roomruler/back",
"version": "0.0.0", "version": "0.0.0",
"scripts": { "scripts": {
"build": "tsc --build", "build": "rollup --config rollup.config.js",
"start": "node dist/index.js" "start": "node dist/index.js"
}, },
"devDependencies": { "devDependencies": {
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@types/ws": "^7.4.7", "@types/ws": "^7.4.7",
"rollup": "^2.56.3",
"tslib": "^2.3.1",
"typescript": "^4.4.2" "typescript": "^4.4.2"
}, },
"dependencies": { "dependencies": {

View File

@ -0,0 +1,11 @@
const typescript = require("@rollup/plugin-typescript");
const { nodeResolve } = require("@rollup/plugin-node-resolve");
export default {
input: "src/index.ts",
output: {
dir: "dist",
format: "cjs",
},
plugins: [typescript(), nodeResolve({ resolveOnly: [/^@roomruler\/.*$/] })],
};

View File

@ -1,7 +1,12 @@
import "reflect-metadata"; import "reflect-metadata";
import { Server, OPEN } from "ws"; import { Server, OPEN } from "ws";
import { connect, getRoomList, updateFree } from "./db"; import { connect, getRoomList, updateFree } from "./db";
import { isMessage, isUpdateMessage } from "@roomruler/messages"; import {
composeMessage,
isMessage,
isUpdateMessage,
ListMessage,
} from "@roomruler/messages";
const main = async () => { const main = async () => {
const connection = await connect(); const connection = await connect();
@ -15,7 +20,11 @@ const main = async () => {
wss.on("connection", async (wsc, req) => { wss.on("connection", async (wsc, req) => {
console.log("New user connected from " + req.socket.remoteAddress); console.log("New user connected from " + req.socket.remoteAddress);
wsc.send(JSON.stringify(await getRoomList(connection))); wsc.send(
JSON.stringify(
composeMessage<ListMessage>("list", await getRoomList(connection))
)
);
wsc.on("message", async (data) => { wsc.on("message", async (data) => {
console.log("Got message from " + req.socket.remoteAddress); console.log("Got message from " + req.socket.remoteAddress);

View File

@ -5,5 +5,6 @@
"module": "commonjs", "module": "commonjs",
"outDir": "dist" "outDir": "dist"
}, },
"include": ["src"],
"references": [{ "path": "../../packages/messages/tsconfig.json" }] "references": [{ "path": "../../packages/messages/tsconfig.json" }]
} }

View File

@ -3,6 +3,7 @@
"main": "dist/src/index.js", "main": "dist/src/index.js",
"version": "0.0.0", "version": "0.0.0",
"types": "dist/src/index.d.ts", "types": "dist/src/index.d.ts",
"type": "module",
"scripts": { "scripts": {
"build": "tsc --build" "build": "tsc --build"
}, },

View File

@ -11,6 +11,21 @@ export type UpdateMessage = Message & {
}; };
}; };
export type Room = {
id: number;
title: string;
free: boolean;
x: number;
y: number;
width: number;
height: number;
};
export type ListMessage = Message & {
type: "list";
args: Room[];
};
const isObjLike = (obj: unknown): obj is object => const isObjLike = (obj: unknown): obj is object =>
Boolean(obj) && typeof obj === "object"; Boolean(obj) && typeof obj === "object";
@ -31,3 +46,40 @@ export const isUpdateMessage = (message: Message): message is UpdateMessage =>
typeof message.args.id === "number" && typeof message.args.id === "number" &&
hasProperty(message.args, "value") && hasProperty(message.args, "value") &&
typeof message.args.value === "boolean"; typeof message.args.value === "boolean";
export const isRoom = (obj: unknown): obj is Room => {
if (!(typeof obj === "object") || !obj) return false;
const ns = ["id", "x", "y", "width", "height"] as const;
for (const key of ns) {
if (!hasProperty(obj, key) || typeof obj[key] !== "number") return false;
}
if (
!hasProperty(obj, "title") ||
typeof obj.title !== "string" ||
!hasProperty(obj, "free") ||
typeof obj.free !== "boolean"
)
return false;
return true;
};
export const isArrayOf = <T extends unknown>(
obj: T,
itemCheck: (arg: T) => boolean
) => {
if (Array.isArray(obj)) {
for (const el of obj) if (!itemCheck(el)) return false;
return true;
} else return false;
};
export const composeMessage = <T extends Message>(
type: T["type"],
args: T["args"]
): Message => ({
type,
args,
});

View File

@ -1,10 +1,10 @@
{ {
"extends": "../../tsconfig.json", "extends": "../../tsconfig.json",
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "es6",
"target": "es6", "target": "es6",
"outDir": "dist", "outDir": "dist",
"composite": true "composite": true
}, },
"include": ["src"] "include": ["src"],
} }