refactor: move realtime related code to directory

Signed-off-by: BoHong Li <raccoon@hackmd.io>
This commit is contained in:
BoHong Li 2020-01-05 06:14:03 +08:00
parent 68fcf35c70
commit 2135cfcd18
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38
20 changed files with 54 additions and 54 deletions

2
app.js
View File

@ -66,7 +66,7 @@ io.engine.ws = new (require('ws').Server)({
})
// others
var realtime = require('./lib/realtime.js')
var realtime = require('./lib/realtime/realtime.js')
// assign socket io to realtime
realtime.io = io

View File

@ -12,13 +12,13 @@ const moment = require('moment')
const get = require('lodash/get')
// core
const config = require('./config')
const logger = require('./logger')
const history = require('./web/history')
const models = require('./models')
const config = require('../config')
const logger = require('../logger')
const history = require('../web/history')
const models = require('../models')
// ot
const ot = require('./ot')
const ot = require('../ot')
const { ProcessQueue } = require('./processQueue')
const { RealtimeClientConnection } = require('./realtimeClientConnection')

View File

@ -1,8 +1,8 @@
'use strict'
const async = require('async')
const config = require('./config')
const logger = require('./logger')
const config = require('../config')
const logger = require('../logger')
/**
* clean when user not in any rooms or user not in connected list

View File

@ -2,9 +2,9 @@
const get = require('lodash/get')
const config = require('./config')
const models = require('./models')
const logger = require('./logger')
const config = require('../config')
const models = require('../models')
const logger = require('../logger')
class RealtimeClientConnection {
constructor (socket) {

View File

@ -1,7 +1,7 @@
'use strict'
const models = require('./models')
const logger = require('./logger')
const models = require('../models')
const logger = require('../logger')
/**
* clean when user not in any rooms or user not in connected list

View File

@ -1,7 +1,7 @@
'use strict'
const config = require('./config')
const logger = require('./logger')
const config = require('../config')
const logger = require('../logger')
const moment = require('moment')
class UpdateDirtyNoteJob {

View File

@ -1,6 +1,6 @@
'use strict'
const realtime = require('../../realtime')
const realtime = require('../../realtime/realtime')
const config = require('../../config')
exports.getStatus = async (req, res) => {

View File

@ -4,7 +4,7 @@
const assert = require('assert')
const sinon = require('sinon')
const { ProcessQueue } = require('../lib/processQueue')
const { ProcessQueue } = require('../lib/realtime/processQueue')
describe('ProcessQueue', function () {
let clock

View File

@ -31,13 +31,13 @@ describe('cleanDanglingUser', function () {
afterEach(() => {
clock.restore()
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtime')
mock.stopAll()
sinon.restore()
})
it('should call queueForDisconnectSpy when user is dangling', (done) => {
const realtime = require('../../lib/realtime')
const realtime = require('../../lib/realtime/realtime')
const queueForDisconnectSpy = sinon.spy(realtime, 'queueForDisconnect')
realtime.io = {
to: sinon.stub().callsFake(function () {

View File

@ -31,7 +31,7 @@ describe('realtime#connection', function () {
mock('../../lib/realtimeCleanDanglingUserJob', realtimeJobStub)
mock('../../lib/realtimeSaveRevisionJob', realtimeJobStub)
mock('../../lib/ot', require('../testDoubles/otFake'))
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
})
afterEach(() => {

View File

@ -27,7 +27,7 @@ describe('realtime#update note is dirty timer', function () {
}
})
mock('../../lib/config', {})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.io = {
to: sinon.stub().callsFake(function () {
@ -39,8 +39,8 @@ describe('realtime#update note is dirty timer', function () {
})
afterEach(() => {
removeModuleFromRequireCache('../../lib/realtimeUpdateDirtyNoteJob')
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtimeUpdateDirtyNoteJob')
removeModuleFromRequireCache('../../lib/realtime/realtime')
mock.stopAll()
clock.restore()
})

View File

@ -28,7 +28,7 @@ describe('realtime#disconnect', function () {
})
mock('../../lib/config', {})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
updateNoteStub = sinon.stub(realtime, 'updateNote').callsFake((note, callback) => {
callback(null, note)
})
@ -60,7 +60,7 @@ describe('realtime#disconnect', function () {
})
afterEach(() => {
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtime')
mock.stopAll()
sinon.restore()
})

View File

@ -14,14 +14,14 @@ describe('realtime#extractNoteIdFromSocket', function () {
})
afterEach(() => {
delete require.cache[require.resolve('../../lib/realtime')]
delete require.cache[require.resolve('../../lib/realtime/realtime')]
mock.stopAll()
})
describe('urlPath not set', function () {
beforeEach(() => {
mock('../../lib/config', {})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
})
let realtime
@ -76,7 +76,7 @@ describe('realtime#extractNoteIdFromSocket', function () {
mock('../../lib/config', {
urlPath: urlPath
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const incomingNoteId = 'myNoteId'
const incomingSocket = makeMockSocket({
referer: `https://localhost:3000/${urlPath}/${incomingNoteId}`

View File

@ -81,7 +81,7 @@ describe('realtime#ifMayEdit', function () {
client.request.user.id = noteOwnerId
}
client.noteId = noteId
const realtime = require('../../lib/realtime')
const realtime = require('../../lib/realtime/realtime')
realtime.getNotePool()[noteId] = note
const callback = sinon.stub()
realtime.ifMayEdit(client, callback)
@ -98,7 +98,7 @@ describe('realtime#ifMayEdit', function () {
client.noteId = noteId
const callback = sinon.stub()
client.origin = 'operation'
const realtime = require('../../lib/realtime')
const realtime = require('../../lib/realtime/realtime')
realtime.getNotePool()[noteId] = note
realtime.ifMayEdit(client, callback)
assert(callback.calledOnce)
@ -116,7 +116,7 @@ describe('realtime#ifMayEdit', function () {
client.request.user.id = loggedInUserId
const callback = sinon.stub()
client.origin = 'operation'
const realtime = require('../../lib/realtime')
const realtime = require('../../lib/realtime/realtime')
realtime.getNotePool()[noteId] = note
realtime.ifMayEdit(client, callback)
assert(callback.calledOnce)

View File

@ -23,12 +23,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
})
afterEach(() => {
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtime')
mock.stopAll()
})
it('should return null when socket not send noteId', async function () {
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const mockSocket = makeMockSocket()
try {
const notes = await realtime.parseNoteIdFromSocketAsync(mockSocket)
@ -49,12 +49,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
})
})
it('should return noteId when noteId exists', async function () {
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
let parsedNoteId
try {
parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
@ -76,12 +76,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
})
})
it('should return null when noteId not exists', async function () {
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const parsedNoteId = await realtime.parseNoteIdFromSocketAsync(mockSocket)
assert(parsedNoteId === null)
})
@ -99,12 +99,12 @@ describe('realtime#parseNoteIdFromSocketAsync', function () {
})
})
it('should return error when noteId parse error', async function () {
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const noteId = '123456'
const mockSocket = makeMockSocket(undefined, {
noteId: noteId
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
try {
await realtime.parseNoteIdFromSocketAsync(mockSocket)
} catch (err) {

View File

@ -50,7 +50,7 @@ describe('realtime', function () {
}
})
mock('../../lib/config', {})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
})
Object.keys(viewPermission).forEach(function (permission) {

View File

@ -34,8 +34,8 @@ describe('save revision job', function () {
afterEach(() => {
clock.restore()
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtimeSaveRevisionJob')
removeModuleFromRequireCache('../../lib/realtime/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtimeSaveRevisionJob')
mock.stopAll()
sinon.restore()
})
@ -44,7 +44,7 @@ describe('save revision job', function () {
mockModels.Revision.saveAllNotesRevision.callsFake((callback) => {
callback(null, [])
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
clock.tick(5 * 60 * 1000)
clock.restore()
setTimeout(() => {
@ -58,7 +58,7 @@ describe('save revision job', function () {
mockModels.Revision.saveAllNotesRevision.callsFake((callback) => {
callback(null, [1])
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
clock.tick(5 * 60 * 1000)
clock.restore()
setTimeout(() => {

View File

@ -76,7 +76,7 @@ describe('realtime#socket event', function () {
mock('../../lib/models', modelsMock)
mock('../../lib/config', configMock)
mock('../../lib/ot', require('../testDoubles/otFake'))
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
// get all socket event handler
clientSocket = makeMockSocket(null, {
@ -122,8 +122,8 @@ describe('realtime#socket event', function () {
})
afterEach(function () {
removeModuleFromRequireCache('../../lib/realtime')
removeModuleFromRequireCache('../../lib/realtimeClientConnection')
removeModuleFromRequireCache('../../lib/realtime/realtime')
removeModuleFromRequireCache('../../lib/realtime/realtimeClientConnection')
mock.stopAll()
sinon.restore()
clock.restore()

View File

@ -47,7 +47,7 @@ describe('realtime#updateNote', function () {
it('should save history to each edited user', function (done) {
modelsStub.Note.findOne.returns(Promise.resolve({}))
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
const updateHistoryStub = sinon.stub(realtime, 'updateHistory')
const callback = sinon.stub()
@ -86,7 +86,7 @@ describe('realtime#updateNote', function () {
name: 'User 01'
})
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
clock.restore()
@ -125,7 +125,7 @@ describe('realtime#updateNote', function () {
})
clock.tick(1000)
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
setTimeout(() => {
assert(note.lastchangeuserprofile.name === 'User 01')
@ -159,7 +159,7 @@ describe('realtime#updateNote', function () {
})
clock.tick(1000)
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
setTimeout(() => {
assert(modelsStub.User.findOne.callCount === 0)
@ -196,7 +196,7 @@ describe('realtime#updateNote', function () {
})
clock.tick(1000)
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
setTimeout(() => {
assert(modelsStub.User.findOne.callCount === 0)
@ -233,7 +233,7 @@ describe('realtime#updateNote', function () {
})
clock.tick(1000)
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
setTimeout(() => {
assert(modelsStub.User.findOne.called)
@ -272,7 +272,7 @@ describe('realtime#updateNote', function () {
})
clock.tick(1000)
realtime = require('../../lib/realtime')
realtime = require('../../lib/realtime/realtime')
realtime.updateNote(note, callback)
setTimeout(() => {
assert(note.lastchangeuserprofile.name === 'User 01')