Skip to content

Commit

Permalink
fix: MessageHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Sep 30, 2020
1 parent 989e469 commit 21a78e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
28 changes: 24 additions & 4 deletions packages/daf-cli/src/msg.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import { getAgent } from './setup'
import program from 'commander'
const fs = require('fs')

program
.command('msg <raw>')
.description('Handle raw message (JWT)')
.action(async (raw) => {
.command('msg')
.description('Handle raw message ')
.option('-r, --raw <string>', 'String containing raw message')
.option('-f, --file <string>', 'Path to a file containing raw message')
.option('--save <boolean>', 'Save message', true)

.action(async (options) => {
const agent = getAgent(program.config)
try {
const message = await agent.handleMessage({ raw, metaData: [{ type: 'cli' }], save: true })
let raw

if (options.file) {
raw = fs.readFileSync(options.file).toString()
} else if (options.raw) {
raw = options.raw
} else {
throw Error('Missing file path or raw string')
}

const message = await agent.handleMessage({
raw,
metaData: [{ type: 'cli' }],
save: options.save,
})

console.dir(message, { depth: 10 })
} catch (e) {
console.error(e.message)
Expand Down
3 changes: 2 additions & 1 deletion packages/daf-did-comm/src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ export class DIDCommMessageHandler extends AbstractMessageHandler {
if (parsed['id']) message.id = parsed['id']
if (parsed['type']) message.type = parsed['type']
message.addMetaData({ type: 'DIDComm' })
debug('JSON message with id and type', message)
return super.handle(message, context)
}
} catch (e) {
// not a JSON string
debug('Raw message is not a JSON string')
}
}

Expand Down
5 changes: 3 additions & 2 deletions packages/daf-message-handler/src/message-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ export class MessageHandler extends EventEmitter implements IAgentPlugin {
if (save) {
await context.agent.dataStoreSaveMessage(message)
debug('Emitting event', EventTypes.savedMessage)
// this.emit(EventTypes.savedMessage, message)
this.emit(EventTypes.savedMessage, message)
}
return message
} catch (error) {
// this.emit(EventTypes.error, error)
debug('Error', error)
this.emit(EventTypes.error, error)
return Promise.reject(error)
}
}
Expand Down

0 comments on commit 21a78e3

Please sign in to comment.