Skip to content

Commit

Permalink
Remove json assumption from postMessage handler (#190)
Browse files Browse the repository at this point in the history
Incorrect assumption that all messages received are from desktopJS and are serialized JSON. Remove use of JSON completely so no parse is required and we can look at source to determine whether they are within scope of desktopJS message bus.
  • Loading branch information
bingenito authored Sep 18, 2018
1 parent 0b0c123 commit 8ed38bf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Default/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class DefaultMessageBus implements MessageBus {
}

// Make sure topic received matches the one that was subscribed
const { source, "topic": receivedTopic, message } = JSON.parse(event.data);
const { source, "topic": receivedTopic, message } = event.data;

if (source === DefaultMessageBus.messageSource && topic === receivedTopic) {
listener({ topic: topic }, message);
Expand Down Expand Up @@ -173,7 +173,7 @@ export class DefaultMessageBus implements MessageBus {
continue;
}

win.postMessage(JSON.stringify({ source: DefaultMessageBus.messageSource, topic: topic, message: message }), this.container.globalWindow.location.origin);
win.postMessage({ source: DefaultMessageBus.messageSource, topic: topic, message: message }, this.container.globalWindow.location.origin);
}
}

Expand Down
17 changes: 12 additions & 5 deletions tests/unit/Default/default.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,15 @@ describe("DefaultMessageBus", () => {
});

it("listener callback attached", (done) => {
bus.subscribe("topic", callback).then((subscriber) => {
subscriber.listener({ origin: "origin", data: JSON.stringify({ source: "desktopJS", topic: "topic", message: "message" }) });
}).then(done);
const handler = (e, data) => {
expect(e.topic).toEqual("topic");
expect(data).toEqual("message");
done();
};

bus.subscribe("topic", handler).then((subscriber) => {
subscriber.listener({ origin: "origin", data: { source: "desktopJS", topic: "topic", message: "message" } });
});
});

it("unsubscribe invokes underlying unsubscribe", (done) => {
Expand All @@ -444,8 +450,9 @@ describe("DefaultMessageBus", () => {
it("publish invokes underling publish", (done) => {
let message: any = { data: "data" };
spyOn(mockWindow, "postMessage").and.callThrough();
bus.publish("topic", message).then(done);
expect(mockWindow.postMessage).toHaveBeenCalledWith(JSON.stringify({ source: "desktopJS", topic: "topic", message: message }), "origin");
bus.publish("topic", message).then(() => {
expect(mockWindow.postMessage).toHaveBeenCalledWith({ source: "desktopJS", topic: "topic", message: message }, "origin");
}).then(done);
});

it("publish with non matching optional name does not invoke underling send", (done) => {
Expand Down

0 comments on commit 8ed38bf

Please sign in to comment.