-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Backend Structure & Fixed Tests
- Loading branch information
1 parent
0962481
commit bb45ef8
Showing
25 changed files
with
221 additions
and
145 deletions.
There are no files selected for viewing
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 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,10 @@ | ||
module.exports = { | ||
root: true, | ||
env: { node: true }, | ||
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'], | ||
ignorePatterns: ['dist', '.eslintrc.cjs'], | ||
parser: '@typescript-eslint/parser', | ||
rules: { | ||
'@typescript-eslint/no-explicit-any': 'off', | ||
}, | ||
}; |
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,4 +1,8 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { pathsToModuleNameMapper } = require('ts-jest'); | ||
|
||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
moduleNameMapper: pathsToModuleNameMapper({ '@src/*': ['./src/*'] }, { prefix: '<rootDir>/' }), | ||
}; |
11 changes: 11 additions & 0 deletions
11
code/server/src/controllers/http/document/deleteDocument.ts
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,11 @@ | ||
import { Request, Response } from 'express'; | ||
import { DocumentService } from '@src/types'; | ||
|
||
function deleteDocument(service: DocumentService) { | ||
return (req: Request, res: Response) => { | ||
service.deleteTree(); | ||
res.status(200).send(); | ||
}; | ||
} | ||
|
||
export default deleteDocument; |
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,11 @@ | ||
import { Request, Response } from 'express'; | ||
import { DocumentService } from '@src/types'; | ||
|
||
function getDocument(service: DocumentService) { | ||
return async (req: Request, res: Response) => { | ||
const tree = await service.getTree(); | ||
res.status(200).send(tree); | ||
}; | ||
} | ||
|
||
export default getDocument; |
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,20 @@ | ||
import express from 'express'; | ||
import { DocumentService } from '@src/types'; | ||
import getDocument from '@src/controllers/http/document/getDocument'; | ||
import deleteDocument from '@src/controllers/http/document/deleteDocument'; | ||
|
||
export default function (service: DocumentService) { | ||
if (!service) { | ||
throw new Error('Service parameter is required'); | ||
} | ||
const router = express.Router(); | ||
router.use(express.urlencoded({ extended: true })); | ||
|
||
router.get('/', (req, res) => { | ||
res.send('Welcome to NoteSpace'); | ||
}); | ||
router.get('/document', getDocument(service)); | ||
router.delete('/document', deleteDocument(service)); | ||
|
||
return router; | ||
} |
16 changes: 16 additions & 0 deletions
16
code/server/src/controllers/socket/document/onCursorChange.ts
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,16 @@ | ||
import { Socket } from 'socket.io'; | ||
|
||
const cursorColorsMap = new Map<string, string>(); | ||
|
||
function onCursorChange() { | ||
return (socket: Socket, position: CursorChangeData) => { | ||
if (!cursorColorsMap.has(socket.id)) { | ||
const randomColor = 'hsl(' + Math.random() * 360 + ', 100%, 75%)'; | ||
cursorColorsMap.set(socket.id, randomColor); | ||
} | ||
const color = cursorColorsMap.get(socket.id); | ||
socket.broadcast.emit('cursorChange', { position, id: socket.id, color }); | ||
}; | ||
} | ||
|
||
export default onCursorChange; |
29 changes: 29 additions & 0 deletions
29
code/server/src/controllers/socket/document/onOperation.ts
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,29 @@ | ||
import { Socket } from 'socket.io'; | ||
import { DocumentService } from '@src/types'; | ||
import { Operation } from 'shared/crdt/types/operations'; | ||
|
||
function onOperation(service: DocumentService) { | ||
return (socket: Socket, operation: Operation) => { | ||
switch (operation.type) { | ||
case 'insert': { | ||
service.insertCharacter(operation); | ||
socket.broadcast.emit('operation', operation); | ||
break; | ||
} | ||
case 'delete': { | ||
service.deleteCharacter(operation); | ||
socket.broadcast.emit('operation', operation); | ||
break; | ||
} | ||
case 'style': { | ||
service.updateStyle(operation); | ||
socket.broadcast.emit('operation', operation); | ||
break; | ||
} | ||
default: | ||
throw new Error('Invalid operation type'); | ||
} | ||
}; | ||
} | ||
|
||
export default onOperation; |
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,13 @@ | ||
import onOperation from '@src/controllers/socket/document/onOperation'; | ||
import onCursorChange from '@src/controllers/socket/document/onCursorChange'; | ||
import { DocumentService, SocketHandler } from '@src/types'; | ||
|
||
export default function events(service: DocumentService): Record<string, SocketHandler> { | ||
if (!service) { | ||
throw new Error('Service parameter is required'); | ||
} | ||
return { | ||
operation: onOperation(service), | ||
cursorChange: onCursorChange, | ||
}; | ||
} |
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,31 @@ | ||
import { Socket } from 'socket.io'; | ||
import { DocumentService, SocketHandler } from '@src/types'; | ||
|
||
function onConnection(service: DocumentService, events: Record<string, SocketHandler>) { | ||
return async (socket: Socket) => { | ||
console.log('a client connected'); | ||
|
||
if (socket.connected) { | ||
const tree = await service.getTree(); | ||
socket.emit('document', tree); | ||
} | ||
|
||
Object.entries(events).forEach(([event, handler]) => { | ||
socket.on(event, data => { | ||
try { | ||
console.log(event); | ||
handler(socket, data); | ||
} catch (e) { | ||
socket.emit('error'); | ||
console.error(e); | ||
} | ||
}); | ||
}); | ||
|
||
socket.on('disconnect', reason => { | ||
console.log('a client disconnected', reason); | ||
}); | ||
}; | ||
} | ||
|
||
export default onConnection; |
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 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 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 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,4 @@ | ||
type CursorChangeData = { | ||
line: number; | ||
column: number; | ||
}; |
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
6 changes: 3 additions & 3 deletions
6
code/server/src/services/services.ts → code/server/src/services/documentService.ts
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 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,18 +1,21 @@ | ||
import { Nodes } from '@notespace/shared/crdt/types'; | ||
import { InsertOperation, DeleteOperation, StyleOperation } from '@notespace/shared/crdt/operations'; | ||
import { Socket } from 'socket.io'; | ||
|
||
type Database = { | ||
type DocumentDatabase = { | ||
getTree: () => Promise<Nodes<string>>; | ||
deleteTree: () => void; | ||
insertCharacter: (operation: InsertOperation<string>) => void; | ||
deleteCharacter: (operation: DeleteOperation) => void; | ||
updateStyle: (operation: StyleOperation) => void; | ||
}; | ||
|
||
type Service = { | ||
type DocumentService = { | ||
getTree: () => Promise<Nodes<string>>; | ||
deleteTree: () => void; | ||
insertCharacter: (operation: InsertOperation) => void; | ||
deleteCharacter: (operation: DeleteOperation) => void; | ||
updateStyle: (operation: StyleOperation) => void; | ||
}; | ||
|
||
type SocketHandler = (socket: Socket, data: any) => void; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.