Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
encrypted-postmate
Advanced tools
A powerful, simple, promise-based, encrypted
postMessage
library.
EncryptedPostmate is a promise-based API built on postMessage
. It allows a parent page to speak with a child iFrame
across origins with minimal effort.
model
object that the parent can access.call
functions within a child
EncryptedPostmate can be installed via NPM.
NPM
$ yarn add encrypted-postmate # Install via Yarn
$ npm i encrypted-postmate --save # Install via NPM
Also you can download the compiled javascript directly here
Parent
: The top level page that will embed an iFrame
, creating a Child
.Child
: The bottom level page loaded within the iFrame
.Model
: The object that the Child
exposes to the Parent
.Handshake
: The process by which the parent frame identifies itself to the child, and vice versa. When a handshake is complete, the two contexts have bound their event listeners and identified one another.The Parent
begins communication with the Child
. A handshake is sent, the Child
responds with a handshake reply, finishing Parent
/Child
initialization. The two are bound and ready to communicate securely.
The Parent
fetches values from the Child
by property name. The Child
can emit messages to the parent. The Parent
can call
functions in the Child
Model
.
parent.com
// Kick off the handshake with the iFrame
const handshake = new EncryptedPostmate({
container: document.getElementById('some-div'), // Element to inject frame into
url: 'http://child.com/page.html' postmate.spec.jstion.
});
// When parent <-> child handshake is complete, data may be requested from the child
handshake.then(child => {
// Fetch the height property in child.html and set it to the iFrames height
child.get('height')
.then(height => child.frame.style.height = `${height}px`);
// Listen to a particular event from the child
child.on('some-event', data => console.log(data)); // Logs "Hello, World!"
});
child.com/page.html
const handshake = new EncryptedPostmate.Model({
// Expose your model to the Parent. Property values may be functions, promises, or regular values
height: () => document.height || document.body.offsetHeight
});
// When parent <-> child handshake is complete, events may be emitted to the parent
handshake.then(parent => {
parent.emit('some-event', 'Hello, World!');
});
Postmate.debug
// parent.com or child.com
EncryptedPostmate.debug = true;
new EncryptedPostmate(options);
Name | Type | Description | Default |
---|---|---|---|
debug | Boolean | Set to true to enable logging of additional information | false |
Postmate(options)
// parent.com
new EncryptedPostmate({
container: document.body,
url: 'http://child.com/',
model: { foo: 'bar' }
});
This is written in the parent page. Initiates a connection with the child. Returns a Promise that signals when the handshake is complete and communication is ready to begin.
Returns: Promise(child)
Name | Type | Description | Default |
---|---|---|---|
container (optional) | DOM Node Element | An element to append the iFrame to | document.body |
url | String | _A URL to load in the iFrame. The origin of this URL will also be used for securing message transport | none_ |
model | Object | An object literal to represent the default values of the Childs model | none |
Postmate.Model(model)
// child.com new EncryptedPostmate.Model({ // Serializable values foo: "bar",
// Functions height: () => document.height || document.body.offsetHeight,
// Promises data: fetch(new Request('data.json')) });
> This is written in the child page. Calling `Postmate.Model` initiates a handshake request listener from the `Parent`. Once the handshake is complete, an event listener is bound to receive requests from the `Parent`. The `Child` model is _extended_ from the `model` provided by the `Parent`.
#### Parameters
Name | Type | Description | Default
:--- | :--- | :--- | :---
**`model`** | `Object` | _An object of gettable properties to expose to the parent. Value types may be anything accepted in `postMessage`. Promises may also be set as values or returned from functions._ | `{}`
***
> ## `child.get(key)`
```javascript
// parent.com
new EncryptedPostmate({
container: document.body,
url: 'http://child.com/'
}).then(child => {
child.get('something').then(value => console.log(value));
});
Retrieves a value by property name from the
Childs
model
object.
Returns: Promise(value)
Name | Type | Description |
---|---|---|
key | String (required) | The string property to lookup in the childs model |
child.call(key, data)
// parent.com
new EncryptedPostmate({
container: document.body,
url: 'http://child.com/'
}).then(child => {
child.call('sayHi', 'Hello, World!');
});
Calls the function
sayHi
in theChild
Model
with the parameterHello, World!
Returns: undefined
Name | Type | Description |
---|---|---|
key | String (required) | The string property to lookup in the childs model |
data | Mixed | The optional data to send to the child function |
child.destroy()
// parent.com
new EncryptedPostmate({
container: document.body,
url: 'http://child.com/'
}).then(child => child.destroy());
Removes the
iFrame
element and destroys anymessage
event listeners
Returns: undefined
##
child.frame
new EncryptedPostmate(options).then(child => {
child.get('height')
.then(height => child.frame.style.height = `${height}px`);
});
The iFrame Element that the parent is communicating with
Promises provide a clear API for fetching data. Using an evented approach often starts backwards. if the parent wants to know the childs height, the child would need to alert the parent, whereas with EncryptedPostmate, the Parent will request that information from the child in a synchronous-like manner. The child can emit events to the parent as well, for those other use-cases that still need to be handled.
EncryptedPostmate.debug needs to be set in both the parent and child for each of them to log their respective information
Make sure that you have initialized EncryptedPostmate.Model in your child page.
EncryptedPostmate (by design) is restrictive in its modes of communication. This enforces a simplistic approach: The parent is responsible for logic contained within the parent, and the child is responsible for logic contained within the child. If you need to retrieve information from parent -> child, consider setting a default
model
in the parent that the child may extend.
This is specifically what the
call
function is for.
By default, all
message
events received by any (parent) page can come from any (child) location. This means that theParent
must always enforce security within its message event, ensuring that thechild
(origin) is who we expect them to be, that the message is a response from an original request, and that our message is valid. The handshake routine solves this by saving the identities of the child and parent and ensuring that no changes are made to either.
The origin of the request, the message type, the postMessage mime-type, and in some cases the message response, are all verified against the original data made when the handshake was completed.
MIT
FAQs
A powerful, simple, promise-based, encrypted postMessage library
The npm package encrypted-postmate receives a total of 9 weekly downloads. As such, encrypted-postmate popularity was classified as not popular.
We found that encrypted-postmate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.