Skip to content

Commit

Permalink
Upgrading dependencies, fixing bookmark API
Browse files Browse the repository at this point in the history
Various security and dependency upgrades, as well as fixing bookmarks
API logic
  • Loading branch information
rwampler committed Mar 25, 2023
1 parent f5e9831 commit 48ca84f
Show file tree
Hide file tree
Showing 10 changed files with 690 additions and 713 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# starpeace-server-multiverse-nodejs

[![GitHub release](https://img.shields.io/github/release/starpeace-project/starpeace-server-multiverse-nodejs.svg)](https://github.com/starpeace-project/starpeace-server-multiverse-nodejs/releases/)
[![Build Status](https://travis-ci.org/starpeace-project/starpeace-server-multiverse-nodejs.svg)](https://travis-ci.org/starpeace-project/starpeace-server-multiverse-nodejs)
[![GitHub license](https://img.shields.io/github/license/starpeace-project/starpeace-server-multiverse-nodejs.svg)](https://github.com/starpeace-project/starpeace-server-multiverse-nodejs/blob/master/LICENSE)
[![GitHub issues](https://img.shields.io/github/issues/starpeace-project/starpeace-server-multiverse-nodejs.svg)](https://github.com/starpeace-project/starpeace-server-multiverse-nodejs/issues)
[![GitHub stars](https://img.shields.io/github/stars/starpeace-project/starpeace-server-multiverse-nodejs.svg)](https://github.com/starpeace-project/starpeace-server-multiverse-nodejs/stargazers)
Expand Down
1,291 changes: 631 additions & 660 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@starpeace/starpeace-server-multiverse-nodejs",
"version": "0.1.4",
"version": "0.1.5",
"description": "STARPEACE Multiverse game-play and simulation server created with NodeJS",
"author": "starpeace-project",
"license": "MIT",
Expand All @@ -27,7 +27,7 @@
"test": "npm run build && mocha"
},
"devDependencies": {
"@babel/core": "^7.20.12",
"@babel/core": "^7.21.3",
"@babel/preset-env": "^7.20.2",
"@tsconfig/node16": "^1.0.3",
"@types/bad-words": "^3.0.1",
Expand All @@ -39,49 +39,49 @@
"@types/lodash": "^4.14.191",
"@types/luxon": "^3.2.0",
"@types/morgan": "^1.9.4",
"@types/node": "^18.11.18",
"@types/passport": "^1.0.11",
"@types/node": "^18.15.9",
"@types/passport": "^1.0.12",
"@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.35",
"@types/seedrandom": "^3.0.4",
"@types/seedrandom": "^3.0.5",
"@types/sharp": "^0.31.1",
"@types/sqlite3": "^3.1.8",
"@types/uuid": "^9.0.0",
"grunt": "^1.5.3",
"@types/uuid": "^9.0.1",
"grunt": "^1.6.1",
"grunt-contrib-clean": "^2.0.1",
"grunt-contrib-copy": "^1.0.0",
"matchdep": "*",
"mocha": "^10.2.0",
"typescript": "^4.9.4"
"typescript": "^5.0.2"
},
"dependencies": {
"@starpeace/starpeace-assets": "github:starpeace-project/starpeace-assets.git#v0.2.24",
"@starpeace/starpeace-assets-types": "github:starpeace-project/starpeace-assets-types.git#v0.3.1",
"@starpeace/starpeace-assets-types": "github:starpeace-project/starpeace-assets-types.git#v0.3.2",
"@types/inquirer": "^9.0.3",
"@types/mocha": "^10.0.1",
"bad-words": "^3.0.4",
"bcrypt": "^5.1.0",
"bmp-js": "^0.1.0",
"body-parser": "^1.20.1",
"body-parser": "^1.20.2",
"compression": "^1.7.4",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"express": "^4.18.2",
"express-winston": "^4.2.0",
"fs-extra": "^11.1.0",
"inquirer": "^9.1.4",
"fs-extra": "^11.1.1",
"inquirer": "^9.1.5",
"jsonwebtoken": "^9.0.0",
"lodash": "^4.17.21",
"luxon": "^3.2.1",
"luxon": "^3.3.0",
"memorystore": "^1.6.7",
"p-queue": "^6.6.2",
"p-queue": "npm:@esm2cjs/p-queue@^7.3.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pkg": "^5.8.0",
"socket.io": "^4.5.4",
"sqlite3": "^5.1.4",
"pkg": "^5.8.1",
"socket.io": "^4.6.1",
"sqlite3": "^5.1.6",
"ts-node": "^10.9.1",
"uuid": "^9.0.0",
"winston": "^3.8.2",
Expand Down
1 change: 1 addition & 0 deletions src/core/api/api-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export default class ApiFactory {
app.delete('/corporations/:corporationId/mail/:mailId', authenticate, verifyPlanet, verifyTycoon, corporationApi.deleteMail());

app.post('/companies', authenticate, verifyPlanet, verifyTycoon, companyApi.createCompany());
app.get('/companies/:companyId', authenticate, verifyPlanet, verifyVisa, companyApi.getCompany());
app.get('/companies/:companyId/buildings', authenticate, verifyPlanet, verifyVisa, buildingApi.getCompanyBuildings());
app.get('/companies/:companyId/inventions', authenticate, verifyPlanet, verifyTycoon, companyApi.getInventions());
app.put('/companies/:companyId/inventions/:inventionId', authenticate, verifyPlanet, verifyTycoon, companyApi.researchInvention());
Expand Down
16 changes: 16 additions & 0 deletions src/core/api/company-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ export default class CompanyApi {
this.caches = caches;
}

getCompany (): (req: express.Request, res: express.Response) => any {
return async (req: express.Request, res: express.Response) => {
if (!req.planet || !req.params.companyId) return res.status(400);

try {
const company: Company | null = this.caches.company.withPlanet(req.planet).forId(req.params.companyId);
if (!company) return res.status(404);
return res.json(company.toJsonApi());
}
catch (err) {
this.logger.error(err);
return res.status(500).json({});
}
};
}

createCompany (): (req: express.Request, res: express.Response) => any {
return async (req: express.Request, res: express.Response) => {
if (!req.planet || !req.visa || !req.visa.corporationId) return res.status(400).json({});
Expand Down
10 changes: 5 additions & 5 deletions src/core/api/corporation-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class CorporationApi {
}
catch (err) {
this.logger.error(err);
return res.status(500);
return res.status(500).json({});
}
};
}
Expand All @@ -66,7 +66,7 @@ export default class CorporationApi {
}
catch (err) {
this.logger.error(err);
return res.status(500);
return res.status(500).json({});
}
};
}
Expand Down Expand Up @@ -96,7 +96,7 @@ export default class CorporationApi {
}
catch (err) {
this.logger.error(err);
return res.status(500);
return res.status(500).json({});
}
};
}
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class CorporationApi {
}
catch (err) {
this.logger.error(err);
return res.status(500);
return res.status(500).json({});
}
};
}
Expand Down Expand Up @@ -231,7 +231,7 @@ export default class CorporationApi {
}
catch (err) {
this.logger.error(err);
return res.status(500);
return res.status(500).json({});
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/api/galaxy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class GalaxyApi {
create (): (req: express.Request, res: express.Response, next: any) => any {
return async (req: express.Request, res: express.Response, next: any): Promise<express.Response> => {
if (!req.body.username?.length || !req.body.password?.length) return res.status(400);
return passport.authenticate('register', { session: false }, async (error, user, info) => {
return passport.authenticate('register', { session: false }, async (error: any, user: Tycoon | undefined, info: any) => {
if (error) {
if (error === 'INVALID_NAME' || error === 'USERNAME_CONFLICT') {
return res.status(400).json({ code: error });
Expand All @@ -128,7 +128,7 @@ export default class GalaxyApi {
}
}
else {
return passport.authenticate('login', { session: false }, async (err, user) => {
return passport.authenticate('login', { session: false }, async (err: any, user: Tycoon | undefined) => {
if (err) {
this.logger.error(err);
return res.status(500).json({});
Expand Down
30 changes: 15 additions & 15 deletions src/core/events/model-event-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,36 @@ export default class ModelEventClient {
await this.requestSocket.send(JSON.stringify({ type: 'TYCOON:LIST' }));
const [result] = await this.requestSocket.receive();
return _.map(JSON.parse(result.toString()).tycoons, Tycoon.fromJson);
});
}, { throwOnTimeout: true });
}
async createTycoon (tycoon: Tycoon): Promise<Tycoon> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'TYCOON:CREATE', tycoon: tycoon.toJson() }));
const [result] = await this.requestSocket.receive();
return Tycoon.fromJson(JSON.parse(result.toString()).tycoon);
});
}, { throwOnTimeout: true });
}
async tycoon (tycoonId: string): Promise<Tycoon> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'TYCOON:GET', tycoonId: tycoonId }));
const [result] = await this.requestSocket.receive();
return Tycoon.fromJson(JSON.parse(result.toString()).tycoon);
});
}, { throwOnTimeout: true });
}

async issueToken (tycoon: Tycoon): Promise<string> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'TOKEN:ISSUE', tycoonId: tycoon.id }));
const [result] = await this.requestSocket.receive();
return JSON.parse(result.toString()).token;
});
}, { throwOnTimeout: true });
}
async loginToken (token: string): Promise<Tycoon> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'TOKEN:LOGIN', tokenId: token }));
const [result] = await this.requestSocket.receive();
return Tycoon.fromJson(JSON.parse(result.toString()).tycoon);
});
}, { throwOnTimeout: true });
}

async allTycoonVisas (): Promise<TycoonVisa[]> {
Expand All @@ -106,13 +106,13 @@ export default class ModelEventClient {
await this.requestSocket.send(JSON.stringify({ type: 'VISA:SAVE', visa: visa.toJson() }));
const [result] = await this.requestSocket.receive();
return TycoonVisa.fromJson(JSON.parse(result.toString()).visa);
});
}, { throwOnTimeout: true });
}
async destroyVisa (visaId: string): Promise<void> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'VISA:DESTROY', visaId: visaId }));
await this.requestSocket.receive();
});
}, { throwOnTimeout: true });
}

async allCorporations (planetId: string): Promise<Corporation[]> {
Expand All @@ -127,7 +127,7 @@ export default class ModelEventClient {
await this.requestSocket.send(JSON.stringify({ type: 'CORPORATION:CREATE', planetId: planetId, corporation: corporation.toJson() }));
const [result] = await this.requestSocket.receive();
return Corporation.fromJson(JSON.parse(result.toString()).corporation);
});
}, { throwOnTimeout: true });
}

async allCompanies (planetId: string): Promise<Company[]> {
Expand All @@ -142,7 +142,7 @@ export default class ModelEventClient {
await this.requestSocket.send(JSON.stringify({ type: 'COMPANY:CREATE', planetId: planetId, company: company.toJson() }));
const [result] = await this.requestSocket.receive();
return Company.fromJson(JSON.parse(result.toString()).company);
});
}, { throwOnTimeout: true });
}

async bookmarksForCorporation (planetId: string, corporationId: string): Promise<Bookmark[]> {
Expand Down Expand Up @@ -178,29 +178,29 @@ export default class ModelEventClient {
else {
return Building.fromJson(jsonResult.building);
}
});
}, { throwOnTimeout: true });
}

async getCompanyInventionSummary (planetId: string, companyId: string): Promise<InventionSummary> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'RESEARCH:SUMMARY', planetId: planetId, companyId: companyId }));
const [result] = await this.requestSocket.receive();
return InventionSummary.fromJson(JSON.parse(result.toString()).summary);
});
}, { throwOnTimeout: true });
}
async startResearch (planetId: string, companyId: string, inventionId: string): Promise<InventionSummary> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'RESEARCH:START', planetId: planetId, companyId: companyId, inventionId: inventionId }));
const [result] = await this.requestSocket.receive();
return InventionSummary.fromJson(JSON.parse(result.toString()).summary);
});
}, { throwOnTimeout: true });
}
async cancelResearch (planetId: string, companyId: string, inventionId: string): Promise<InventionSummary> {
return await this.requestQueue.add(async () => {
await this.requestSocket.send(JSON.stringify({ type: 'RESEARCH:CANCEL', planetId: planetId, companyId: companyId, inventionId: inventionId }));
const [result] = await this.requestSocket.receive();
return InventionSummary.fromJson(JSON.parse(result.toString()).summary);
});
}, { throwOnTimeout: true });
}

async mailForCorporation (planetId: string, corporationId: string): Promise<Mail[]> {
Expand All @@ -219,7 +219,7 @@ export default class ModelEventClient {
throw Error(json.error ?? 'Unable to send mail');
}
return Mail.fromJson(json.mail);
});
}, { throwOnTimeout: true });
}
async markReadMail (planetId: string, mailId: string): Promise<string> {
return await this.requestQueue.add(async () => {
Expand All @@ -241,7 +241,7 @@ export default class ModelEventClient {
await this.requestSocket.send(JSON.stringify({ type: 'PLANET:GET', planetId: planetId }));
const [result] = await this.requestSocket.receive();
return Planet.fromJson(JSON.parse(result.toString()).planet);
});
}, { throwOnTimeout: true });
}

async allTowns (planetId: string): Promise<Town[]> {
Expand Down
4 changes: 2 additions & 2 deletions src/core/events/model-event-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ export default class ModelEventServer {
}
else if (request.type === 'CORPORATION:CREATE') {
const corporation: Corporation = <Corporation> this.caches.corporation.withPlanetId(request.planetId).update(Corporation.fromJson(request.corporation));
await this.replySocket.send(JSON.stringify({ corporation: corporation.toJson() }))
await this.publisherSocket.send(['CORPORATION:UPDATE', JSON.stringify({ planetId: corporation.planetId, corporation: corporation.toJson() })])
await this.replySocket.send(JSON.stringify({ corporation: corporation.toJson() }));
await this.publisherSocket.send(['CORPORATION:UPDATE', JSON.stringify({ planetId: corporation.planetId, corporation: corporation.toJson() })]);
}

else if (request.type === 'COMPANY:LIST') {
Expand Down
12 changes: 1 addition & 11 deletions src/corporation/bookmark-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash';
import * as sqlite3 from 'sqlite3';

import Bookmark from '../corporation/bookmark';
Expand Down Expand Up @@ -28,15 +27,6 @@ export default class BookmarkStore {
});
}

all (): Promise<Bookmark[]> {
return new Promise((resolve: Function, reject: Function) => {
this.db.all("SELECT content FROM bookmarks", [], (err: Error, rows: Array<any>) => {
if (err) return reject(err);
resolve(_.map(_.filter(rows, (row: any) => row?.content != null), (row: any) => Bookmark.fromJson(JSON.parse(row.content))));
});
});
}

get (id: string): Promise<Bookmark | null> {
return new Promise((resolve: Function, reject: Function) => {
return this.db.get("SELECT content FROM bookmarks WHERE id = ?", [id], (err: Error, row: any) => {
Expand All @@ -50,7 +40,7 @@ export default class BookmarkStore {
return new Promise((resolve: Function, reject: Function) => {
this.db.all("SELECT content FROM bookmarks WHERE corporationId = ?", [corporationId], (err: Error, rows: Array<any>) => {
if (err) return reject(err);
resolve(_.map(_.filter(rows, (row: any) => row?.id), (row: any) => Bookmark.fromJson(row)));
resolve(rows.filter((row: any) => row?.content).map((row: any) => Bookmark.fromJson(JSON.parse(row.content))));
});
});
}
Expand Down

0 comments on commit 48ca84f

Please sign in to comment.