This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rewriting on the basis of gdyrrahitis/pcard
See https://github.com/gdyrrahitis/pcard In particular: - use typescript - use mocha, chai and nyc for integration and unit tests - modularize
- Loading branch information
Showing
23 changed files
with
1,650 additions
and
473 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './user' | ||
export * from './events/index' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.