Commit 3fb6843d authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Do not export SOURCE from port-util

Sometimes there was some confusion between the `source` in the
`window#postMessage` context and the property in the message sent
through it. We believe it is better to replace the `SOURCE` by the
'hypothesis' string.
parent 00c59e0e
import { ListenerCollection } from './listener-collection';
import { isMessageEqual, SOURCE as source } from './port-util';
import { isMessageEqual } from './port-util';
const MAX_WAIT_FOR_PORT = 1000 * 30;
const POLLING_INTERVAL_FOR_PORT = 250;
......@@ -59,7 +59,10 @@ export class PortFinder {
}
function postRequest() {
hostFrame.postMessage({ channel, port, source, type: 'request' }, '*');
hostFrame.postMessage(
{ channel, port, source: 'hypothesis', type: 'request' },
'*'
);
}
const intervalId = setInterval(
......@@ -78,7 +81,14 @@ export class PortFinder {
// TODO: It would be nice to remove the listener after receiving the port.
this._listeners.add(window, 'message', event => {
const { data, ports } = /** @type {MessageEvent} */ (event);
if (isMessageEqual(data, { channel, port, source, type: 'offer' })) {
if (
isMessageEqual(data, {
channel,
port,
source: 'hypothesis',
type: 'offer',
})
) {
clearInterval(intervalId);
clearTimeout(timeoutId);
resolve(ports[0]);
......
import { TinyEmitter } from 'tiny-emitter';
import { ListenerCollection } from './listener-collection';
import { isMessageEqual, SOURCE as source } from './port-util';
import { isMessageEqual } from './port-util';
/**
* @typedef {import('../types/config').SidebarConfig} SidebarConfig
......@@ -220,7 +220,7 @@ export class PortProvider {
const allowedMessage = {
channel,
port,
source,
source: 'hypothesis',
type: 'request',
};
......
......@@ -3,7 +3,7 @@
// message and avoid listening to messages that could have the same properties
// but different source. This is not a security feature but an
// anti-collision mechanism.
export const SOURCE = 'hypothesis';
const SOURCE = 'hypothesis';
/**
* These types are the used in by `PortProvider` and `PortFinder` for the
......@@ -15,7 +15,7 @@ export const SOURCE = 'hypothesis';
* @prop {Channel} channel
* @prop {Port} port
* @prop {'offer'|'request'} type
* @prop {SOURCE} source
* @prop {SOURCE} source -
*/
/**
......
import { delay } from '../../test-util/wait';
import { PortFinder } from '../port-finder';
import { SOURCE as source } from '../port-util';
describe('PortFinder', () => {
let portFinder;
......@@ -44,7 +43,7 @@ describe('PortFinder', () => {
.catch(e => (error = e));
portFinder.destroy();
sendMessage({
data: { channel, port, source, type: 'offer' },
data: { channel, port, source: 'hypothesis', type: 'offer' },
ports: [port1],
});
clock.tick(30000);
......@@ -100,7 +99,7 @@ describe('PortFinder', () => {
})
.then(port => (resolvedPort = port));
sendMessage({
data: { channel, port, source, type: 'offer' },
data: { channel, port, source: 'hypothesis', type: 'offer' },
ports: [port1],
});
await delay(0);
......@@ -131,7 +130,7 @@ describe('PortFinder', () => {
assert.callCount(window.postMessage, 121);
assert.alwaysCalledWithExactly(
window.postMessage,
{ channel, port, source, type: 'request' },
{ channel, port, source: 'hypothesis', type: 'request' },
'*'
);
......
import { delay } from '../../test-util/wait';
import { PortProvider } from '../port-provider';
import { SOURCE as source } from '../port-util';
const source = 'hypothesis';
describe('PortProvider', () => {
let portProvider;
......
import { isMessageEqual, SOURCE as source } from '../port-util';
import { isMessageEqual } from '../port-util';
const source = 'hypothesis';
describe('port-util', () => {
describe('isMessageEqual', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment