Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
refactor: rewriting on the basis of gdyrrahitis/pcard
Browse files Browse the repository at this point in the history
See https://github.com/gdyrrahitis/pcard

In particular:
- use typescript
- use mocha, chai and nyc for integration and unit tests
- modularize
  • Loading branch information
severo committed Jan 21, 2020
1 parent 97d51d2 commit 4f38e1f
Show file tree
Hide file tree
Showing 23 changed files with 1,650 additions and 473 deletions.
785 changes: 496 additions & 289 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"deploy": "git push heroku master"
},
"dependencies": {
"@hapi/joi": "^17.0.2",
"@hapi/joi": "^17.1.0",
"express": "^4.17.1",
"nanoid": "^2.1.9",
"randomcolor": "^0.5.4",
"rate-limiter-flexible": "^1.3.0",
"rate-limiter-flexible": "^1.3.1",
"socket.io": "^2.3.0"
},
"engines": {
Expand All @@ -36,13 +36,17 @@
"devDependencies": {
"@types/chai": "^4.2.7",
"@types/express": "^4.17.2",
"@types/hapi__joi": "^16.0.6",
"@types/mocha": "^5.2.7",
"@types/nanoid": "^2.1.0",
"@types/node": "^13.1.8",
"@types/randomcolor": "^0.5.3",
"@types/socket.io": "^2.1.4",
"@types/socket.io-client": "^1.4.32",
"@types/uuid": "^3.4.6",
"chai": "^4.2.0",
"concurrently": "^5.0.2",
"heroku": "^7.35.1",
"heroku": "^7.36.2",
"husky": "^4.0.10",
"mocha": "^7.0.0",
"nodemon": "^2.0.2",
Expand Down
44 changes: 0 additions & 44 deletions src/domain/Room.ts

This file was deleted.

35 changes: 0 additions & 35 deletions src/domain/User.ts

This file was deleted.

45 changes: 0 additions & 45 deletions src/domain/UserProfile.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/domain/events/event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export class Event {
static readonly Connection = 'connection'
// static readonly Disconnection = 'disconnection'

// static readonly JoinRoom = 'join-room'
// static readonly LeaveRoom = 'leave-room'
// static readonly ListRoomGuests = 'list-room-guests'
// static readonly KickRoomGuest = 'kick-room-guest'

// static readonly ListRooms = 'list-rooms'
static readonly UpdateUserName = 'update-user-name'
// static readonly UpdateUserColor = 'update-user-color'

static readonly InternalServerError = 'internal-server-error'
// static readonly GuestsList = 'guests-list'
}
13 changes: 13 additions & 0 deletions src/domain/events/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
export * from './connection.event'
// export * from './disconnection.event'

// export * from './join.room.event'
// export * from "./leave.room.event";
// export * from "./list.room.guests.event";
// export * from "./kick.room.guest.event";

// export * from "./list.rooms.event";
export * from './update.user.name.event'
// export * from "./update.user.color.event";

export * from './internal.server.error.event'
// export * from "./guests.list.event";
26 changes: 26 additions & 0 deletions src/domain/events/internal.server.error.event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai'
import { InternalServerErrorEvent } from './internal.server.error.event'

describe('Events', () => {
describe('InternalServerErrorEvent', () => {
it("should have 'internal-server-error' event as name", () => {
expect(InternalServerErrorEvent.eventName).to.equal(
'internal-server-error'
)
})

it('should initialize error object', () => {
// arrange
const message = 'Unhandled error'
const error = new Error(message)

// act
let event = new InternalServerErrorEvent(error)

// assert
expect(event.error).to.not.be.undefined
expect(event.error.name).to.equal('Error')
expect(event.error.message).to.equal(message)
})
})
})
6 changes: 6 additions & 0 deletions src/domain/events/internal.server.error.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Event } from './event'

export class InternalServerErrorEvent {
static eventName: string = Event.InternalServerError
constructor(public readonly error: Error) {}
}
21 changes: 21 additions & 0 deletions src/domain/events/update.user.name.event.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect } from 'chai'
import { UpdateUserNameEvent } from './update.user.name.event'

describe('Events', () => {
describe('UpdateUserNameEvent', () => {
it("should have 'update-user-name' event as name", () => {
expect(UpdateUserNameEvent.eventName).to.equal('update-user-name')
})

it('should initialize event data', () => {
// arrange
let data = { name: 'George' }

// act
let event = new UpdateUserNameEvent(data)

// assert
expect(event.data.name).to.equal('George')
})
})
})
6 changes: 6 additions & 0 deletions src/domain/events/update.user.name.event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Event } from './event'

export class UpdateUserNameEvent {
static readonly eventName: string = Event.UpdateUserName
constructor(public readonly data: { name: string }) {}
}
2 changes: 2 additions & 0 deletions src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './user'
export * from './events/index'
79 changes: 79 additions & 0 deletions src/domain/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import Joi from '@hapi/joi'
import nanoid from 'nanoid'
import rnd from 'randomcolor'

// Validation
const nameSchema = Joi.string().min(1)
const colorSchema = Joi.string().pattern(
new RegExp('^#(?:[0-9a-fA-F]{3}){1,2}$')
)
const checkName = (name: string) => {
const validation = nameSchema.validate(name)
if (validation.error !== undefined) {
throw validation.error
}
return name
}
const checkColor = (color: string) => {
const validation = colorSchema.validate(color)
if (validation.error !== undefined) {
throw validation.error
}
return color
}
// Defaults
const randomName = () => checkName(nanoid(5))
const randomColor = () => checkColor(rnd({ luminosity: 'dark' }))
const MS_UNTIL_EXPIRATION: number = 10 * 60 * 1000

export class User {
private _socketId: SocketIOClient.Socket['id']
private _name: string
private _color: string
private _updated_at: Date
private _expired_at: Date
private _msUntilExpiration: number

constructor(
socketId: SocketIOClient.Socket['id'],
{ msUntilExpiration = MS_UNTIL_EXPIRATION } = {
msUntilExpiration: MS_UNTIL_EXPIRATION,
}
) {
this._socketId = socketId
this._msUntilExpiration = msUntilExpiration
this._name = randomName()
this._color = randomColor()
this.touch()
}

get id() {
return this._socketId
}

get name(): string {
return this._name
}
set name(name: string) {
this._name = checkName(name)
}

get color(): string {
return this._color
}
set color(color: string) {
this._color = checkColor(color)
}

touch() {
this._updated_at = new Date()
this._expired_at = new Date(
this._updated_at.getTime() + this._msUntilExpiration
)
return this
}

hasExpiredAt(date: Date) {
return this._expired_at < date
}
}
Loading

0 comments on commit 4f38e1f

Please sign in to comment.