-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4584 from easyops-cn/jojiang/v2
fix(): 支持嵌入 iframe 时通讯
- Loading branch information
Showing
7 changed files
with
197 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { start, sendUrlChange } from "./listen"; | ||
|
||
describe("listen", () => { | ||
let postMessageSpy: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
postMessageSpy = jest.spyOn(window.parent, "postMessage"); | ||
postMessageSpy.mockClear(); | ||
}); | ||
|
||
afterEach(() => { | ||
postMessageSpy.mockRestore(); | ||
}); | ||
|
||
it("should not send message", async () => { | ||
sendUrlChange({ url: "/some-path" }); | ||
expect(postMessageSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("send message after initialize", async () => { | ||
Object.defineProperty(window, "self", { | ||
value: {}, | ||
writable: true, | ||
}); | ||
start("http://example.com"); | ||
|
||
sendUrlChange({ url: "/some-path" }); | ||
|
||
expect(postMessageSpy).toHaveBeenCalledWith( | ||
{ | ||
sender: "next-core", | ||
type: "url-change", | ||
url: "/some-path", | ||
}, | ||
"http://example.com" | ||
); | ||
}); | ||
|
||
it("The cached URL should be sent on initialization", () => { | ||
Object.defineProperty(window, "self", { | ||
value: {}, | ||
writable: true, | ||
}); | ||
|
||
sendUrlChange({ url: "/cached-path" }); | ||
|
||
start("http://example.com"); | ||
|
||
expect(postMessageSpy).toHaveBeenCalledWith( | ||
{ | ||
sender: "next-core", | ||
type: "url-change", | ||
url: "/cached-path", | ||
}, | ||
"http://example.com" | ||
); | ||
}); | ||
|
||
it("应该禁止非 localhost 的跨域消息", () => { | ||
Object.defineProperty(window, "location", { | ||
value: { | ||
origin: "http://example.com", | ||
}, | ||
writable: true, | ||
}); | ||
|
||
start("https://different-domain.com"); | ||
|
||
expect(postMessageSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
interface MessageField { | ||
sender: string; | ||
type: string; | ||
[key: string]: any; | ||
} | ||
|
||
let initialize: boolean; | ||
let origin: string; | ||
let cachedUrl: string; | ||
|
||
const LOCAL_HOST = /^https?:\/\/localhost(?:$|:)/; | ||
|
||
export function start(_origin: string): void { | ||
if ( | ||
location.origin !== _origin && | ||
!LOCAL_HOST.test(location.origin) && | ||
!LOCAL_HOST.test(_origin) | ||
) { | ||
// 禁止除 localhost 之外的跨域消息 | ||
return; | ||
} | ||
|
||
initialize = true; | ||
origin = _origin; | ||
|
||
if (cachedUrl) { | ||
sendMessage({ | ||
sender: "next-core", | ||
type: "url-change", | ||
url: cachedUrl, | ||
}); | ||
} | ||
} | ||
|
||
export function sendMessage(data: MessageField): void { | ||
window.parent.postMessage(data, origin); | ||
} | ||
|
||
export function sendUrlChange(data: Partial<MessageField>): void { | ||
if (window.self !== window.parent && data?.url !== cachedUrl) { | ||
cachedUrl = data.url; | ||
if (initialize) { | ||
sendMessage({ | ||
sender: "next-core", | ||
type: "url-change", | ||
url: data.url, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters