A generic messaging component for orchestrating promise based messages to/from a hosting application and an iframed component over the window.postMessage API.
Перейти к файлу
Or Shemesh 91015868fd
Merge pull request #22 from microsoft/readme_support_links
Update README.md with correct support links
2024-10-14 16:48:21 +03:00
.config Merged PR 408088: PR: CodeQL TSA Integration - window-post-message-proxy 2023-06-21 13:36:34 +00:00
.vscode remove pipelines folder 2024-07-21 15:36:00 +03:00
src Merged PR 395893: add sendingWindow null check 2023-05-15 07:15:41 +00:00
test Merged PR 260485: Migrate the window-post-message-proxy library from TSLint to ESLint 2022-05-16 05:13:31 +00:00
.eslintignore Merged PR 260485: Migrate the window-post-message-proxy library from TSLint to ESLint 2022-05-16 05:13:31 +00:00
.eslintrc.js Merged PR 471056: Window-post-message-proxy corrupted package structure bug. 2023-11-08 19:35:42 +00:00
.gitignore removed untracked files 2024-07-21 15:39:37 +03:00
.travis.yml Update encrypted api_key with proper token 2016-08-05 14:50:08 -07:00
CONTRIBUTING.md Merged PR 265445: [window-post-message-proxy] Fix remaining vulnerabilities 2022-05-18 19:48:40 +00:00
LICENSE Add package name to LICENSE for OSS policy 2016-06-23 12:45:56 -07:00
README.md Update README.md with correct support links 2024-10-14 15:13:35 +03:00
SECURITY.md Merged PR 144025: Added SECURITY.md 2021-02-03 09:02:52 +00:00
gulpfile.js Merged PR 570838: [Fix S230 alerts] - removed gh-pages and replaced gulp-header with gulp-prepend 2024-06-27 08:54:08 +00:00
karma.conf.js Merged PR 312629: Upgrade karma to use Chrome instead of Phantom JS for windows-post-message-proxy 2022-11-09 10:38:32 +00:00
package.json Merged PR 581794: version bump 2.0.9 2024-07-07 19:02:19 +00:00
tsconfig.json Merged PR 471056: Window-post-message-proxy corrupted package structure bug. 2023-11-08 19:35:42 +00:00
webpack.config.js Merged PR 255180: [1ES Open Source vulnerabilities] Update package.json 2022-04-24 07:01:29 +00:00
webpack.test.config.js Merged PR 255180: [1ES Open Source vulnerabilities] Update package.json 2022-04-24 07:01:29 +00:00
webpack.test.tsconfig.json Merged PR 471056: Window-post-message-proxy corrupted package structure bug. 2023-11-08 19:35:42 +00:00

README.md

window-post-message-proxy

npm Total Downloads Monthly Downloads GitHub tag

A library used in place of the native window.postMessage, which when used on both the sending and receiving windows allows for nicer asynchronous promise messaging between the windows.

When sending messages using the proxy, it will apply a unique ID to the message, create a deferred object referenced by the ID, and pass the message on to the target window. The target window will also have an instance of the windowPostMessage proxy setup, which will send back messages and preserve the unique ID. The original sending instance then receives the response message with the ID and looks to see if there is a matching id in its cache. If so, it resolves the deferred object with the response.

Documentation

https://microsoft.github.io/window-post-message-proxy

Installation

npm install --save window-post-message-proxy

Basic Usage

// Setup
const iframe = document.getElementById("myFrame");
const windowPostMessageProxy = new WindowPostMessageProxy();

// Send message
const message = {
    key: "Value"
};

windowPostMessageProxy.postMessage(iframe.contentWindow, message)
    .then(response => {

    });

Advanced Customization

Customizing how tracking properties are added to the method

By default, the windowPostMessage proxy will store the tracking properties as an object on the message named windowPostMessageProxy.

This means if you call:

const message = {
    key: "Value"
};

windowPostMessageProxy.postMessage(iframe.contentWindow, message);

The message is actually modified before it's sent to become:

{
    windowPostMessageProxy: {
        id: "ebixvvlbwa3tvtjra4i"
    },
    key: "Value"
};

If you want to customize how the tracking properties are added to and retrieved from the message, you can pass settings to the constructor in the form of an object with two functions:

export interface IProcessTrackingProperties {
  addTrackingProperties<T>(message: T, trackingProperties: ITrackingProperties): T;
  getTrackingProperties(message: any): ITrackingProperties;
}

addTrackingProperties takes a message and adds the tracking properties object and returns the message. getTrackingProperties takes a message and extracts the tracking properties.

Example:

const customProcessTrackingProperties = {
    addTrackingProperties(message, trackingProperties) {
        message.headers = {
            'tracking-id': trackingProperties.id
        };

        return message;
    },
    getTrackingProperties(message): ITrackingProperties {
        return {
            id: message.headers['tracking-id']
        };
    }
};
const windowPostMessageProxy = new WindowPostMessageProxy(customProcessTrackingProperties);

Customizing how messages are detected as error responses.

By default, response messages are considered error messages if they contain an error property.

You can override this behavior by passing an isErrorMessage function at construction time:

export interface IIsErrorMessage {
  (message: any): boolean;
}

Example:

function isErrorMessage(message: any) {
    return !(200 <= message.status && message.status < 300);
}

const windowPostMessageProxy = new WindowPostMessageProxy({ isErrorMessage });

Logging messages

By default, messages are not logged, but you can override this behavior by passing logMessages: true in the options object.

const windowPostMessageProxy = new WindowPostMessageProxy({ logMessages: true });

This will print out a stringified JSON of each object that is received or sent by the specific instance.

Supplying custom name

Each windowPostMessageProxy gives itself a randomly generated name so you can see which instance is communicating in the log messages. Often times you may want to pass a custom name for the window on which the windowPostMessageProxy instance is running.

You can provided a name by passing name: 'Iframe' in the options object.

const windowPostMessageProxy = new WindowPostMessageProxy({ name: 'Iframe' });

Supress Warning Message about unhandled messages

By default, the window post message proxy will warn you if it receives a message that was not handled, since this is usually an indication of error. However, if you register multiple window message handlers, the message may in fact be handled despite being unknown to the windowPostMessageProxy. In cases like this, this warning no longer applies, so you can disable it by setting suppressWarnings: true:

const windowPostMessageProxy = new WindowPostMessageProxy({ suppressWarnings: true });

Support

  • Feature Requests: Submit your ideas and suggestions to the Fabric Ideas Portal, where you can also vote on ideas from other developers.
  • Bug Reports and Technical Assistance: Visit the Fabric Developer Community Forum. Our team and community experts are ready to assist you.
  • Additional Support: Contact your account manager or reach out to the Fabric Support Team.