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 { 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 MAX_WAIT_FOR_PORT = 1000 * 30;
const POLLING_INTERVAL_FOR_PORT = 250; const POLLING_INTERVAL_FOR_PORT = 250;
...@@ -59,7 +59,10 @@ export class PortFinder { ...@@ -59,7 +59,10 @@ export class PortFinder {
} }
function postRequest() { function postRequest() {
hostFrame.postMessage({ channel, port, source, type: 'request' }, '*'); hostFrame.postMessage(
{ channel, port, source: 'hypothesis', type: 'request' },
'*'
);
} }
const intervalId = setInterval( const intervalId = setInterval(
...@@ -78,7 +81,14 @@ export class PortFinder { ...@@ -78,7 +81,14 @@ export class PortFinder {
// TODO: It would be nice to remove the listener after receiving the port. // TODO: It would be nice to remove the listener after receiving the port.
this._listeners.add(window, 'message', event => { this._listeners.add(window, 'message', event => {
const { data, ports } = /** @type {MessageEvent} */ (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); clearInterval(intervalId);
clearTimeout(timeoutId); clearTimeout(timeoutId);
resolve(ports[0]); resolve(ports[0]);
......
import { TinyEmitter } from 'tiny-emitter'; import { TinyEmitter } from 'tiny-emitter';
import { ListenerCollection } from './listener-collection'; import { ListenerCollection } from './listener-collection';
import { isMessageEqual, SOURCE as source } from './port-util'; import { isMessageEqual } from './port-util';
/** /**
* @typedef {import('../types/config').SidebarConfig} SidebarConfig * @typedef {import('../types/config').SidebarConfig} SidebarConfig
...@@ -220,7 +220,7 @@ export class PortProvider { ...@@ -220,7 +220,7 @@ export class PortProvider {
const allowedMessage = { const allowedMessage = {
channel, channel,
port, port,
source, source: 'hypothesis',
type: 'request', type: 'request',
}; };
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// message and avoid listening to messages that could have the same properties // message and avoid listening to messages that could have the same properties
// but different source. This is not a security feature but an // but different source. This is not a security feature but an
// anti-collision mechanism. // anti-collision mechanism.
export const SOURCE = 'hypothesis'; const SOURCE = 'hypothesis';
/** /**
* These types are the used in by `PortProvider` and `PortFinder` for the * These types are the used in by `PortProvider` and `PortFinder` for the
...@@ -15,7 +15,7 @@ export const SOURCE = 'hypothesis'; ...@@ -15,7 +15,7 @@ export const SOURCE = 'hypothesis';
* @prop {Channel} channel * @prop {Channel} channel
* @prop {Port} port * @prop {Port} port
* @prop {'offer'|'request'} type * @prop {'offer'|'request'} type
* @prop {SOURCE} source * @prop {SOURCE} source -
*/ */
/** /**
......
import { delay } from '../../test-util/wait'; import { delay } from '../../test-util/wait';
import { PortFinder } from '../port-finder'; import { PortFinder } from '../port-finder';
import { SOURCE as source } from '../port-util';
describe('PortFinder', () => { describe('PortFinder', () => {
let portFinder; let portFinder;
...@@ -44,7 +43,7 @@ describe('PortFinder', () => { ...@@ -44,7 +43,7 @@ describe('PortFinder', () => {
.catch(e => (error = e)); .catch(e => (error = e));
portFinder.destroy(); portFinder.destroy();
sendMessage({ sendMessage({
data: { channel, port, source, type: 'offer' }, data: { channel, port, source: 'hypothesis', type: 'offer' },
ports: [port1], ports: [port1],
}); });
clock.tick(30000); clock.tick(30000);
...@@ -100,7 +99,7 @@ describe('PortFinder', () => { ...@@ -100,7 +99,7 @@ describe('PortFinder', () => {
}) })
.then(port => (resolvedPort = port)); .then(port => (resolvedPort = port));
sendMessage({ sendMessage({
data: { channel, port, source, type: 'offer' }, data: { channel, port, source: 'hypothesis', type: 'offer' },
ports: [port1], ports: [port1],
}); });
await delay(0); await delay(0);
...@@ -131,7 +130,7 @@ describe('PortFinder', () => { ...@@ -131,7 +130,7 @@ describe('PortFinder', () => {
assert.callCount(window.postMessage, 121); assert.callCount(window.postMessage, 121);
assert.alwaysCalledWithExactly( assert.alwaysCalledWithExactly(
window.postMessage, window.postMessage,
{ channel, port, source, type: 'request' }, { channel, port, source: 'hypothesis', type: 'request' },
'*' '*'
); );
......
import { delay } from '../../test-util/wait'; import { delay } from '../../test-util/wait';
import { PortProvider } from '../port-provider'; import { PortProvider } from '../port-provider';
import { SOURCE as source } from '../port-util';
const source = 'hypothesis';
describe('PortProvider', () => { describe('PortProvider', () => {
let portProvider; let portProvider;
......
import { isMessageEqual, SOURCE as source } from '../port-util'; import { isMessageEqual } from '../port-util';
const source = 'hypothesis';
describe('port-util', () => { describe('port-util', () => {
describe('isMessageEqual', () => { 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