Skip to content

Commit

Permalink
Merge pull request #26 from ylesaout/add-file-protocol-support
Browse files Browse the repository at this point in the history
add support for file protocol
  • Loading branch information
Aaronius authored Jan 18, 2019
2 parents 5720e2a + 3adf53a commit 3811461
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const DEFAULT_PORTS = {
'https:': '443'
};

const URL_REGEX = /^(https?:)?\/\/([^/:]+)(:(\d+))?/;
const URL_REGEX = /^(https?:|file:)?\/\/([^/:]+)?(:(\d+))?/;

const Penpal = {
ERR_CONNECTION_DESTROYED,
Expand Down Expand Up @@ -85,6 +85,14 @@ const getOriginFromUrl = url => {
port = location.port;
}

// If the protocol is file, the origin is "null"
// The origin of a document with file protocol is an opaque origin
// and its serialization "null" [1]
// [1] https://html.spec.whatwg.org/multipage/origin.html#origin
if (protocol === "file:") {
return "null";
}

// If the port is the default for the protocol, we don't want to add it to the origin string
// or it won't match the message's event.origin.
const portSuffix = port && port !== DEFAULT_PORTS[protocol] ? `:${port}` : '';
Expand Down Expand Up @@ -379,19 +387,24 @@ Penpal.connectToChild = ({ url, appendTo, methods = {}, timeout }) => {
) {
log('Parent: Received handshake, sending reply');

// If event.origin is "null", the remote protocol is file:
// and we must post messages with "*" as targetOrigin [1]
// [1] https://developer.mozilla.org/fr/docs/Web/API/Window/postMessage#Utiliser_window.postMessage_dans_les_extensions
const remoteOrigin = event.origin === "null" ? "*" : event.origin;

event.source.postMessage(
{
penpal: HANDSHAKE_REPLY,
methodNames: Object.keys(methods)
},
event.origin
remoteOrigin
);

const info = {
localName: 'Parent',
local: parent,
remote: child,
remoteOrigin: event.origin
remoteOrigin: remoteOrigin
};

// If the child reconnected, we need to destroy the previous call receiver before setting
Expand Down

0 comments on commit 3811461

Please sign in to comment.