Skip to content

Commit

Permalink
Use async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Apr 28, 2019
1 parent b99e4be commit a369b48
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
39 changes: 13 additions & 26 deletions base-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ class BaseServer {
return new Context(data.nodeId, data.clientId, data.userId, subprotocol)
}

subscribeAction (action, meta, start) {
async subscribeAction (action, meta, start) {
if (typeof action.channel !== 'string') {
this.wrongChannel(action, meta)
return
Expand All @@ -892,25 +892,24 @@ class BaseServer {

let subscribed = false
if (match) {
let ctx = this.createContext(meta)
ctx.params = match
try {
let ctx = this.createContext(meta)
ctx.params = match

forcePromise(() => {
return i.access(ctx, action, meta)
}).then(access => {
let access = await forcePromise(() => i.access(ctx, action, meta))
if (this.wrongChannels[meta.id]) {
delete this.wrongChannels[meta.id]
return false
return
}
if (!access) {
this.denyAction(meta)
return false
return
}

let client = this.clientIds[ctx.clientId]
if (!client) {
this.emitter.emit('subscriptionCancelled')
return false
return
}

let filter = i.filter && i.filter(ctx, action, meta)
Expand All @@ -927,28 +926,16 @@ class BaseServer {
this.subscribers[action.channel][ctx.nodeId] = filter || true
subscribed = true

let emitter = this.emitter
function emitSubscribed () {
emitter.emit('subscribed', action, meta, Date.now() - start)
return true
}

if (i.init) {
return forcePromise(() => {
return i.init(ctx, action, meta)
}).then(() => emitSubscribed())
} else {
return emitSubscribed()
}
}).then(access => {
if (access) this.markAsProcessed(meta)
}).catch(e => {
if (i.init) await forcePromise(() => i.init(ctx, action, meta))
this.emitter.emit('subscribed', action, meta, Date.now() - start)
this.markAsProcessed(meta)
} catch (e) {
this.emitter.emit('error', e, action, meta)
this.undo(meta, 'error')
if (subscribed) {
this.unsubscribeAction(action, meta)
}
})
}
break
}
}
Expand Down
39 changes: 20 additions & 19 deletions server-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,19 +247,19 @@ class ServerClient {
return [action, meta]
}

filter (action, meta) {
async filter (action, meta) {
let ctx = this.app.createContext(meta)

let wrongUser = !this.clientId || this.clientId !== ctx.clientId
let wrongMeta = Object.keys(meta).some(i => ALLOWED_META.indexOf(i) === -1)
if (wrongUser || wrongMeta) {
this.app.denyAction(meta)
return Promise.resolve(false)
return false
}

let type = action.type
if (type === 'logux/subscribe' || type === 'logux/unsubscribe') {
return Promise.resolve(true)
return true
}

let processor = this.app.types[type]
Expand All @@ -268,24 +268,25 @@ class ServerClient {
}
if (!processor) {
this.app.internalUnkownType(action, meta)
return Promise.resolve(false)
return false
}

return forcePromise(() => processor.access(ctx, action, meta))
.then(result => {
if (this.app.unknownTypes[meta.id]) {
delete this.app.unknownTypes[meta.id]
return false
} else if (!result) {
this.app.denyAction(meta)
return false
} else {
return true
}
}).catch(e => {
this.app.undo(meta, 'error')
this.app.emitter.emit('error', e, action, meta)
})
try {
let result = await forcePromise(() => processor.access(ctx, action, meta))
if (this.app.unknownTypes[meta.id]) {
delete this.app.unknownTypes[meta.id]
return false
} else if (!result) {
this.app.denyAction(meta)
return false
} else {
return true
}
} catch (e) {
this.app.undo(meta, 'error')
this.app.emitter.emit('error', e, action, meta)
return false
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions test/base-server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,7 @@ it('checks channel access', async () => {
await test.app.log.add(
{ type: 'logux/subscribe', channel: 'user/10' }, { id: '1 10:uuid 0' }
)
await delay(1)

expect(test.names).toEqual(['add', 'clean', 'denied', 'add', 'clean'])
expect(test.reports[2][1]).toEqual({ actionId: '1 10:uuid 0' })
Expand Down Expand Up @@ -886,6 +887,7 @@ it('subscribes clients', async () => {
await test.app.log.add(
{ type: 'logux/subscribe', channel: 'posts' }, { id: '2 10:a:uuid 0' }
)
await delay(1)

expect(events).toEqual(2)
expect(test.app.subscribers).toEqual({
Expand Down Expand Up @@ -1017,6 +1019,7 @@ it('loads initial actions during subscription', async () => {
await test.app.log.add(
{ type: 'logux/subscribe', channel: 'user/10' }, { id: '1 10:uuid 0' }
)
await delay(1)
expect(userLoaded).toEqual(1)
expect(test.app.subscribers).toEqual({
'user/10': {
Expand Down
6 changes: 3 additions & 3 deletions test/server-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ it('allows subscribe and unsubscribe actions', async () => {
'add',
'add',
'unsubscribed',
'subscribed',
'add',
'subscribed',
'add'
])
expect(test.reports[2][1].actionId).toEqual('3 10:uuid 0')
Expand Down Expand Up @@ -551,7 +551,7 @@ it('checks user access for action', async () => {
{ type: 'FOO' }, { id: [1, '10:uuid', 0], time: 1 },
{ type: 'FOO', bar: true }, { id: [1, '10:uuid', 1], time: 1 }
])
await client.connection.pair.wait('right')
await delay(10)
expect(test.app.log.actions()).toEqual([
{ type: 'FOO', bar: true },
{ type: 'logux/undo', reason: 'denied', id: '1 10:uuid 0' },
Expand All @@ -560,7 +560,7 @@ it('checks user access for action', async () => {
expect(test.names).toEqual([
'connect', 'authenticated', 'denied', 'add', 'add', 'add'])
expect(test.reports[2][1].actionId).toEqual('1 10:uuid 0')
expect(sent(client)[1]).toEqual([
expect(sent(client)[2]).toEqual([
'debug', 'error', 'Action "1 10:uuid 0" was denied'
])
})
Expand Down

0 comments on commit a369b48

Please sign in to comment.