diff --git a/.env.example b/.env.example index dc86968..2eda92f 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ NODE_ENV=development PORT=5000 +ID_LENGTH=10 diff --git a/package.json b/package.json index 1b4eb12..8412924 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "multiplayer chess", "version": "1.0.0", - "description": "", - "main": "index.js", + "private":true, + "type": "module", "scripts": { "start": "node server/dist/server.js", - "dev:server": "nodemon server/src/server.ts", + "dev:server": "nodemon --watch './**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm' server/src/server.ts", "dev:client": "npm run dev --prefix client", "dev": "concurrently \"npm run dev:server\" \"npm run dev:client\"", "build:server": "tsc -p .", @@ -13,12 +13,10 @@ "build": "concurrently \"npm run build:server\" \"npm run build:client\"", "test": "vitest run" }, - "keywords": [], - "author": "", - "license": "ISC", "dependencies": { "dotenv": "^16.3.1", "express": "^4.18.2", + "nanoid": "^4.0.2", "socket.io": "^4.7.1" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1efcac0..23df116 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: express: specifier: ^4.18.2 version: 4.18.2 + nanoid: + specifier: ^4.0.2 + version: 4.0.2 socket.io: specifier: ^4.7.1 version: 4.7.1 @@ -1099,6 +1102,12 @@ packages: hasBin: true dev: true + /nanoid@4.0.2: + resolution: {integrity: sha512-7ZtY5KTCNheRGfEFxnedV5zFiORN1+Y1N6zvPTnHQd8ENUvfaDBeuJDZb2bN/oXwXxu3qkTXDzy57W5vAmDTBw==} + engines: {node: ^14 || ^16 || >=18} + hasBin: true + dev: false + /negotiator@0.6.3: resolution: {integrity: sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==} engines: {node: '>= 0.6'} diff --git a/server/src/server.ts b/server/src/server.ts index 5ccf3bf..b5d3d1c 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -3,11 +3,17 @@ import path from "path"; import dotenv from "dotenv"; import http from "http"; import { Server } from "socket.io"; -import { randomUUID } from "crypto"; import Chess, { COLOR, Color, Move } from "./engine"; +import { nanoid as nanoidOriginal } from "nanoid"; dotenv.config(); +const ID_LENGTH = isNaN(parseInt(process.env.ID_LENGTH!)) + ? 10 + : parseInt(process.env.ID_LENGTH!); + +const nanoid = () => nanoidOriginal(ID_LENGTH); + const PORT = process.env.PORT || 5000; const app = express(); @@ -105,9 +111,9 @@ if (process.env.NODE_ENV === "development") }); app.get("/api/create-game", (_req, res) => { - let id = randomUUID(); + let id = nanoid(); - while (rooms.has(id)) id = randomUUID(); + while (rooms.has(id)) id = nanoid(); rooms.set(id, { b: null, diff --git a/tsconfig.json b/tsconfig.json index 333e812..014dba6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,8 +11,8 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ - // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + "target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "lib": ["ESNext"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ @@ -21,13 +21,13 @@ // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ - // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ /* Modules */ - "module": "commonjs", /* Specify what module code is generated. */ + "module": "ESNext", /* Specify what module code is generated. */ // "rootDir": "", /* Specify the root folder within your source files. */ - "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "Node", /* Specify how TypeScript looks up a file from a given module specifier. */ // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ @@ -39,7 +39,7 @@ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ /* JavaScript Support */ - // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + "allowJs": false, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ @@ -51,7 +51,7 @@ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ "outDir": "./server/dist", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ - // "noEmit": true, /* Disable emitting files from a compilation. */ + "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ @@ -69,9 +69,9 @@ // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ /* Interop Constraints */ - // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ - // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ - "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ + "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": false, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */