Skip to content
This repository has been archived by the owner on Jun 1, 2020. It is now read-only.

xmlToJson: xml2json -> Xml2js (fix #130) #143

Merged
merged 11 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 8 additions & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"typescript.tsdk": "./node_modules/typescript/lib"
, "update.channel": "none"
, "files.exclude": {
"dist/": true
, "doc/": true
, "node_modules/": true
, "package/": true
}
, "alignment": {
"typescript.tsdk": "./node_modules/typescript/lib"
, "files.exclude": {
"dist/": true
, "doc/": true
, "node_modules/": true
, "package/": true
}
, "alignment": {
"operatorPadding": "right"
, "indentBase": "firstline"
, "surroundSpace": {
Expand All @@ -18,8 +17,4 @@
// If this value is negative, it means don't align the trailing comment.
}
}
, "eslint.autoFixOnSave": false
, "tslint.autoFixOnSave": false
, "editor.formatOnSave": false
, "typescript.extension.sortImports.sortOnSave": false
}
2 changes: 1 addition & 1 deletion examples/wechaty-padchat-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { generate } from 'qrcode-terminal'
const puppet = new PuppetPadchat()

const bot = new Wechaty({
profile : 'padchat-demo',
name : 'padchat-demo',
puppet,
})

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-padchat",
"version": "0.9.18",
"version": "0.9.21",
"description": "Padchat Puppet for Wechaty",
"directories": {
"example": "examples"
Expand Down Expand Up @@ -56,7 +56,7 @@
"@types/semver": "^5.5.0",
"@types/sinon": "^5.0.1",
"@types/ws": "^5.1.2",
"@types/xml2json": "^0.10.0",
"@types/xml2js": "^0.4.3",
"blue-tape": "^1.0.0",
"file-box": "^0.8.23",
"git-scripts": "^0.2.1",
Expand All @@ -68,8 +68,8 @@
"ts-node": "^7.0.0",
"tslint": "^5.10.0",
"tslint-config-standard": "^7.1.0",
"typescript": "^2.9.2",
"wechaty": "^0.19.127",
"typescript": "^3.0.1",
"wechaty": "^0.19.139",
"wechaty-puppet": "^0.11.4"
},
"git": {
Expand Down Expand Up @@ -99,6 +99,6 @@
"rx-queue": "^0.4.26",
"rxjs": "^6.2.1",
"ws": "^5.2.1",
"xml2json": "^0.11.2"
"xml2js": "^0.4.19"
}
}
4 changes: 2 additions & 2 deletions scripts/npm-pack-testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install --production \
@types/lru-cache \
@types/node \
@types/normalize-package-data \
@types/xml2json \
@types/xml2js \
clone-class \
brolog \
file-box \
Expand All @@ -32,7 +32,7 @@ npm install --production \
wechaty-puppet@next \
watchdog \
qr-image \
xml2json \
xml2js \

./node_modules/.bin/tsc \
--esModuleInterop \
Expand Down
6 changes: 3 additions & 3 deletions src/puppet-padchat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class PuppetPadchat extends Puppet {

protected async onPadchatMessageRoomInvitation (rawPayload: PadchatMessagePayload): Promise<void> {
log.verbose('PuppetPadchat', 'onPadchatMessageRoomInvitation(%s)', rawPayload)
const roomInviteEvent = roomInviteEventMessageParser(rawPayload)
const roomInviteEvent = await roomInviteEventMessageParser(rawPayload)

if (!this.padchatManager) {
throw new Error('no padchat manager')
Expand All @@ -349,7 +349,7 @@ export class PuppetPadchat extends Puppet {
protected async onPadchatMessageRoomEventJoin (rawPayload: PadchatMessagePayload): Promise<void> {
log.verbose('PuppetPadchat', 'onPadchatMessageRoomEventJoin({id=%s})', rawPayload.msg_id)

const roomJoinEvent = roomJoinEventMessageParser(rawPayload)
const roomJoinEvent = await roomJoinEventMessageParser(rawPayload)

if (roomJoinEvent) {
const inviteeNameList = roomJoinEvent.inviteeNameList
Expand Down Expand Up @@ -491,7 +491,7 @@ export class PuppetPadchat extends Puppet {
/**
* 2. Look for friendship receive event
*/
const friendshipReceiveContactId = friendshipReceiveEventMessageParser(rawPayload)
const friendshipReceiveContactId = await friendshipReceiveEventMessageParser(rawPayload)
/**
* 3. Look for friendship verify event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ test('friendshipReceiveEventMessageParser()', async t => {

const EXPECTED_CONTACT_ID = 'lizhuohuan'

const contactName = friendshipReceiveEventMessageParser(MESSAGE_PAYLOAD)
const contactName = await friendshipReceiveEventMessageParser(MESSAGE_PAYLOAD)
t.equal(contactName, EXPECTED_CONTACT_ID, 'should parse message to receive contact id')
})
25 changes: 13 additions & 12 deletions src/pure-function-helpers/friendship-event-message-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,68 @@ import {

test('friendshipConfirmEventMessageParser()', async t => {
t.equal(
friendshipConfirmEventMessageParser(undefined as any),
await friendshipConfirmEventMessageParser(undefined as any),
null,
'should parse `undefined`',
)
t.equal(
friendshipConfirmEventMessageParser(null as any),
await friendshipConfirmEventMessageParser(null as any),
null,
'should parse `null`',
)
t.equal(
friendshipConfirmEventMessageParser({} as any),
await friendshipConfirmEventMessageParser({} as any),
null,
'should parse `{}`',
)
t.equal(
friendshipConfirmEventMessageParser({ content: 'fadsfsfasfs' } as any),
await friendshipConfirmEventMessageParser({ content: 'fadsfsfasfs' } as any),
null,
'should parse invalid content',
)
})

test('friendshipReceiveEventMessageParser()', async t => {
t.equal(
friendshipReceiveEventMessageParser(undefined as any),
await friendshipReceiveEventMessageParser(undefined as any),
null,
'should parse `undefined`',
)
t.equal(
friendshipReceiveEventMessageParser(null as any),
await friendshipReceiveEventMessageParser(null as any),
null,
'should parse `null`',
)
t.equal(
friendshipReceiveEventMessageParser({} as any),
await friendshipReceiveEventMessageParser({} as any),
null,
'should parse `{}`',
)
t.equal(
friendshipReceiveEventMessageParser({ content: 'fadsfsfasfs' } as any),
await friendshipReceiveEventMessageParser({ content: 'fadsfsfasfs' } as any),
null,
'should parse invalid content',
)
})

test('friendshipVerifyEventMessageParser()', async t => {
t.equal(
friendshipVerifyEventMessageParser(undefined as any),
await friendshipVerifyEventMessageParser(undefined as any),
null,
'should parse `undefined`',
)
t.equal(
friendshipVerifyEventMessageParser(null as any),
await friendshipVerifyEventMessageParser(null as any),
null,
'should parse `null`',
)
t.equal(
friendshipVerifyEventMessageParser({} as any),
await friendshipVerifyEventMessageParser({} as any),
null,
'should parse `{}`',
)
t.equal(
friendshipVerifyEventMessageParser({ content: 'fadsfsfasfs' } as any),
await friendshipVerifyEventMessageParser({ content: 'fadsfsfasfs' } as any),
null,
'should parse invalid content',
)
Expand Down
24 changes: 12 additions & 12 deletions src/pure-function-helpers/friendship-event-message-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toJson } from 'xml2json'
import { xmlToJson } from './xml-to-json'

import { PadchatMessagePayload } from '../padchat-schemas'

Expand Down Expand Up @@ -52,31 +52,31 @@ export function friendshipConfirmEventMessageParser (
*
*/

export function friendshipReceiveEventMessageParser (
export async function friendshipReceiveEventMessageParser (
rawPayload: PadchatMessagePayload,
): null | string {
): Promise<null | string> {

if (!isPayload(rawPayload)) {
return null
}

interface XmlSchema {
msg: {
fromusername: string,
encryptusername: string,
content: string,
ticket: string,
$: {
fromusername : string,
encryptusername : string,
content : string,
ticket : string,
},
}
}

try {
const jsonPayload: XmlSchema = JSON.parse(
toJson(
rawPayload.content,
),
const jsonPayload: XmlSchema = await xmlToJson(
rawPayload.content,
)

const contactId = jsonPayload.msg.fromusername
const contactId = jsonPayload.msg.$.fromusername

if (isContactId(contactId)) {
return contactId
Expand Down
22 changes: 12 additions & 10 deletions src/pure-function-helpers/friendship-raw-payload-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toJson } from 'xml2json'
import { xmlToJson } from './xml-to-json'

import {
FriendshipType
Expand All @@ -19,9 +19,9 @@ import {
friendshipVerifyEventMessageParser,
} from './friendship-event-message-parser'

export function friendshipRawPayloadParser (
export async function friendshipRawPayloadParser (
rawPayload: PadchatMessagePayload,
) : FriendshipPayload {
) : Promise<FriendshipPayload> {

if (friendshipConfirmEventMessageParser(rawPayload)) {
/**
Expand All @@ -35,7 +35,7 @@ export function friendshipRawPayloadParser (
*/
return friendshipRawPayloadParserVerify(rawPayload)

} else if (friendshipReceiveEventMessageParser(rawPayload)) {
} else if (await friendshipReceiveEventMessageParser(rawPayload)) {
/**
* 3. Receive Event
*/
Expand All @@ -46,9 +46,9 @@ export function friendshipRawPayloadParser (
}
}

function friendshipRawPayloadParserConfirm (
async function friendshipRawPayloadParserConfirm (
rawPayload: PadchatMessagePayload,
): FriendshipPayload {
): Promise<FriendshipPayload> {
const payload: FriendshipPayloadConfirm = {
contactId : rawPayload.from_user,
id : rawPayload.msg_id,
Expand All @@ -68,21 +68,23 @@ function friendshipRawPayloadParserVerify (
return payload
}

function friendshipRawPayloadParserReceive (
async function friendshipRawPayloadParserReceive (
rawPayload: PadchatMessagePayload,
) {
const tryXmlText = rawPayload.content

interface XmlSchema {
msg?: PadchatFriendshipPayload,
msg?: {
$: PadchatFriendshipPayload,
},
}

const jsonPayload: XmlSchema = toJson(tryXmlText, { object: true })
const jsonPayload: XmlSchema = await xmlToJson(tryXmlText) // , { object: true })

if (!jsonPayload.msg) {
throw new Error('no msg found')
}
const padchatFriendshipPayload: PadchatFriendshipPayload = jsonPayload.msg
const padchatFriendshipPayload: PadchatFriendshipPayload = jsonPayload.msg.$

const friendshipPayload: FriendshipPayloadReceive = {
contactId : padchatFriendshipPayload.fromusername,
Expand Down
4 changes: 2 additions & 2 deletions src/pure-function-helpers/message-raw-payload-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ test.skip('attachment file with ext .xlsx', async t => {
const payload = messageRawPayloadParser(MESSAGE_PAYLOAD)
console.log(payload)

const { toJson } = require('xml2json')
console.log(JSON.parse(toJson(payload.text)))
const { xml2json } = require('./xml-to-json')
console.log(await xml2json(payload.text))

t.deepEqual(payload, EXPECTED_PAYLOAD, 'should parse share card message peer to peer')
})
2 changes: 0 additions & 2 deletions src/pure-function-helpers/message-raw-payload-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import { toJson } from 'xml2json'

import {
MessagePayload,
MessageType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test('roomInviteEventMessageParser() ZH', async t => {
url: 'http://support.weixin.qq.com/cgi-bin/mmsupport-bin/addchatroombyinvite?ticket=AVs3velIDxTkA0EOhKogxg%3D%3D',
}

const event = roomInviteEventMessageParser(MESSAGE_PAYLOAD)
const event = await roomInviteEventMessageParser(MESSAGE_PAYLOAD)
t.deepEqual(event, EXPECTED_EVENT, 'should parse event')
})

Expand Down Expand Up @@ -63,6 +63,6 @@ test('roomInviteEventMessageParser() EN', async t => {
url: 'http://support.weixin.qq.com/cgi-bin/mmsupport-bin/addchatroombyinvite?ticket=AV5L4pEpxU7L8XAEFRxuHw%3D%3D',
}

const event = roomInviteEventMessageParser(MESSAGE_PAYLOAD)
const event = await roomInviteEventMessageParser(MESSAGE_PAYLOAD)
t.deepEqual(event, EXPECTED_EVENT, 'should parse event')
})
8 changes: 4 additions & 4 deletions src/pure-function-helpers/room-event-invite-message-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toJson } from 'xml2json'
import { xmlToJson } from './xml-to-json'

import {
PadchatMessagePayload,
Expand Down Expand Up @@ -69,9 +69,9 @@ const ROOM_OTHER_INVITE_LIST_EN = [
/"(.+)" invited you to join the group chat "(.+)"\. Enter to view details\./
]

export const roomInviteEventMessageParser = (
export const roomInviteEventMessageParser = async (
rawPayload: PadchatMessagePayload,
): null | PadchatRoomInviteEvent => {
): Promise<null | PadchatRoomInviteEvent> => {

if (!isPayload(rawPayload)) {
return null
Expand All @@ -94,7 +94,7 @@ export const roomInviteEventMessageParser = (

let jsonPayload: XmlSchema
try {
jsonPayload = toJson(tryXmlText, { object: true }) as XmlSchema
jsonPayload = await xmlToJson(tryXmlText) // toJson(tryXmlText, { object: true }) as XmlSchema
} catch (e) {
return null
}
Expand Down