postmate.js 14.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/**
  postmate - A powerful, simple, promise-based postMessage library
  @version v1.5.2
  @link https://github.com/dollarshaveclub/postmate
  @author Jacob Kelley <jakie8@gmail.com>
  @license MIT
**/
layui.define(function (exports) {
  'use strict'

  /**
   * The type of messages our frames our sending
   * @type {String}
   */
  var messageType = 'application/x-postmate-v1+json'
  /**
   * The maximum number of attempts to send a handshake request to the parent
   * @type {Number}
   */

  var maxHandshakeRequests = 5
  /**
   * A unique message ID that is used to ensure responses are sent to the correct requests
   * @type {Number}
   */

  var _messageId = 0
  /**
   * Increments and returns a message ID
   * @return {Number} A unique ID for a message
   */

  var generateNewMessageId = function generateNewMessageId() {
    return ++_messageId
  }
  /**
   * Postmate logging function that enables/disables via config
   * @param  {Object} ...args Rest Arguments
   */

  var log = function log() {
    var _console

    return Postmate.debug ? (_console = console).log.apply(_console, arguments) : null
  } // eslint-disable-line no-console

  /**
   * Takes a URL and returns the origin
   * @param  {String} url The full URL being requested
   * @return {String}     The URLs origin
   */

  var resolveOrigin = function resolveOrigin(url) {
    var a = document.createElement('a')
    a.href = url
    var protocol = a.protocol.length > 4 ? a.protocol : window.location.protocol
    var host = a.host.length ? (a.port === '80' || a.port === '443' ? a.hostname : a.host) : window.location.host
    return a.origin || protocol + '//' + host
  }
  var messageTypes = {
    handshake: 1,
    'handshake-reply': 1,
    call: 1,
    emit: 1,
    reply: 1,
    request: 1
    /**
     * Ensures that a message is safe to interpret
     * @param  {Object} message The postmate message being sent
     * @param  {String|Boolean} allowedOrigin The whitelisted origin or false to skip origin check
     * @return {Boolean}
     */
  }
  var sanitize = function sanitize(message, allowedOrigin) {
    if (typeof allowedOrigin === 'string' && message.origin !== allowedOrigin) return false
    if (!message.data) return false
    if (typeof message.data === 'object' && !('postmate' in message.data)) return false
    if (message.data.type !== messageType) return false
    if (!messageTypes[message.data.postmate]) return false
    return true
  }
  /**
   * Takes a model, and searches for a value by the property
   * @param  {Object} model     The dictionary to search against
   * @param  {String} property  A path within a dictionary (i.e. 'window.location.href')
   * @param  {Object} data      Additional information from the get request that is
   *                            passed to functions in the child model
   * @return {Promise}
   */

  var resolveValue = function resolveValue(model, property) {
    var unwrappedContext = typeof model[property] === 'function' ? model[property]() : model[property]
    return Postmate.Promise.resolve(unwrappedContext)
  }
  /**
   * Composes an API to be used by the parent
   * @param {Object} info Information on the consumer
   */

  var ParentAPI =
    /*#__PURE__*/
    (function () {
      function ParentAPI(info) {
        var _this = this

        this.parent = info.parent
        this.frame = info.frame
        this.child = info.child
        this.childOrigin = info.childOrigin
        this.events = {}

        // if (process.env.NODE_ENV !== 'production') {
        //   log('Parent: Registering API')
        //   log('Parent: Awaiting messages...')
        // }

        this.listener = function (e) {
          if (!sanitize(e, _this.childOrigin)) return false
          /**
           * the assignments below ensures that e, data, and value are all defined
           */

          var _ref = ((e || {}).data || {}).value || {},
            data = _ref.data,
            name = _ref.name

          if (e.data.postmate === 'emit') {
            // if (process.env.NODE_ENV !== 'production') {
            //   log('Parent: Received event emission: ' + name)
            // }

            if (name in _this.events) {
              _this.events[name].call(_this, data)
            }
          }
        }

        this.parent.addEventListener('message', this.listener, false)

        // if (process.env.NODE_ENV !== 'production') {
        //   log('Parent: Awaiting event emissions from Child')
        // }
      }

      var _proto = ParentAPI.prototype

      _proto.get = function get(property) {
        var _this2 = this

        return new Postmate.Promise(function (resolve) {
          // Extract data from response and kill listeners
          var uid = generateNewMessageId()

          var transact = function transact(e) {
            if (e.data.uid === uid && e.data.postmate === 'reply') {
              _this2.parent.removeEventListener('message', transact, false)

              resolve(e.data.value)
            }
          } // Prepare for response from Child...

          _this2.parent.addEventListener('message', transact, false) // Then ask child for information

          _this2.child.postMessage(
            {
              postmate: 'request',
              type: messageType,
              property: property,
              uid: uid
            },
            _this2.childOrigin
          )
        })
      }

      _proto.call = function call(property, data) {
        // Send information to the child
        this.child.postMessage(
          {
            postmate: 'call',
            type: messageType,
            property: property,
            data: data
          },
          this.childOrigin
        )
      }

      _proto.on = function on(eventName, callback) {
        this.events[eventName] = callback
      }

      _proto.destroy = function destroy() {
        // if (process.env.NODE_ENV !== 'production') {
        //   log('Parent: Destroying Postmate instance')
        // }

        window.removeEventListener('message', this.listener, false)
        this.frame.parentNode.removeChild(this.frame)
      }

      return ParentAPI
    })()
  /**
   * Composes an API to be used by the child
   * @param {Object} info Information on the consumer
   */

  var ChildAPI =
    /*#__PURE__*/
    (function () {
      function ChildAPI(info) {
        var _this3 = this

        this.model = info.model
        this.parent = info.parent
        this.parentOrigin = info.parentOrigin
        this.child = info.child

        // if (process.env.NODE_ENV !== 'production') {
        //   log('Child: Registering API')
        //   log('Child: Awaiting messages...')
        // }

        this.child.addEventListener('message', function (e) {
          if (!sanitize(e, _this3.parentOrigin)) return

          // if (process.env.NODE_ENV !== 'production') {
          //   log('Child: Received request', e.data)
          // }

          var _e$data = e.data,
            property = _e$data.property,
            uid = _e$data.uid,
            data = _e$data.data

          if (e.data.postmate === 'call') {
            if (property in _this3.model && typeof _this3.model[property] === 'function') {
              _this3.model[property](data)
            }

            return
          } // Reply to Parent

          resolveValue(_this3.model, property).then(function (value) {
            return e.source.postMessage(
              {
                property: property,
                postmate: 'reply',
                type: messageType,
                uid: uid,
                value: value
              },
              e.origin
            )
          })
        })
      }

      var _proto2 = ChildAPI.prototype

      _proto2.emit = function emit(name, data) {
        // if (process.env.NODE_ENV !== 'production') {
        //   log('Child: Emitting Event "' + name + '"', data)
        // }

        this.parent.postMessage(
          {
            postmate: 'emit',
            type: messageType,
            value: {
              name: name,
              data: data
            }
          },
          this.parentOrigin
        )
      }

      return ChildAPI
    })()
  /**
   * The entry point of the Parent.
   * @type {Class}
   */

  var Postmate =
    /*#__PURE__*/
    (function () {
      // eslint-disable-line no-undef
      // Internet Explorer craps itself

      /**
       * Sets options related to the Parent
       * @param {Object} object The element to inject the frame into, and the url
       * @return {Promise}
       */
      function Postmate(_ref2) {
        var _ref2$container = _ref2.container,
          container = _ref2$container === void 0 ? (typeof container !== 'undefined' ? container : document.body) : _ref2$container,
          model = _ref2.model,
          url = _ref2.url,
          name = _ref2.name,
          _ref2$classListArray = _ref2.classListArray,
          classListArray = _ref2$classListArray === void 0 ? [] : _ref2$classListArray
        // eslint-disable-line no-undef
        this.parent = window
        this.frame = document.createElement('iframe')
        this.frame.name = name || ''
        this.frame.classList.add.apply(this.frame.classList, classListArray)
        container.appendChild(this.frame)
        this.child = this.frame.contentWindow || this.frame.contentDocument.parentWindow
        this.model = model || {}
        return this.sendHandshake(url)
      }
      /**
       * Begins the handshake strategy
       * @param  {String} url The URL to send a handshake request to
       * @return {Promise}     Promise that resolves when the handshake is complete
       */

      var _proto3 = Postmate.prototype

      _proto3.sendHandshake = function sendHandshake(url) {
        var _this4 = this

        var childOrigin = resolveOrigin(url)
        var attempt = 0
        var responseInterval
        return new Postmate.Promise(function (resolve, reject) {
          var reply = function reply(e) {
            if (!sanitize(e, childOrigin)) return false

            if (e.data.postmate === 'handshake-reply') {
              clearInterval(responseInterval)

              // if (process.env.NODE_ENV !== 'production') {
              //   log('Parent: Received handshake reply from Child')
              // }

              _this4.parent.removeEventListener('message', reply, false)

              _this4.childOrigin = e.origin

              if (process.env.NODE_ENV !== 'production') {
                log('Parent: Saving Child origin', _this4.childOrigin)
              }

              return resolve(new ParentAPI(_this4))
            } // Might need to remove since parent might be receiving different messages
            // from different hosts

            if (process.env.NODE_ENV !== 'production') {
              log('Parent: Invalid handshake reply')
            }

            return reject('Failed handshake')
          }

          _this4.parent.addEventListener('message', reply, false)

          var doSend = function doSend() {
            attempt++

            // if (process.env.NODE_ENV !== 'production') {
            //   log('Parent: Sending handshake attempt ' + attempt, {
            //     childOrigin: childOrigin
            //   })
            // }

            _this4.child.postMessage(
              {
                postmate: 'handshake',
                type: messageType,
                model: _this4.model
              },
              childOrigin
            )

            if (attempt === maxHandshakeRequests) {
              clearInterval(responseInterval)
            }
          }

          var loaded = function loaded() {
            doSend()
            responseInterval = setInterval(doSend, 500)
          }

          if (_this4.frame.attachEvent) {
            _this4.frame.attachEvent('onload', loaded)
          } else {
            _this4.frame.onload = loaded
          }

          // if (process.env.NODE_ENV !== 'production') {
          //   log('Parent: Loading frame', {
          //     url: url
          //   })
          // }

          _this4.frame.src = url
        })
      }

      return Postmate
    })()
  /**
   * The entry point of the Child
   * @type {Class}
   */

  Postmate.debug = false

  Postmate.Promise = (function () {
    try {
      return window ? window.Promise : Promise
    } catch (e) {
      return null
    }
  })()

  Postmate.Model =
    /*#__PURE__*/
    (function () {
      /**
       * Initializes the child, model, parent, and responds to the Parents handshake
       * @param {Object} model Hash of values, functions, or promises
       * @return {Promise}       The Promise that resolves when the handshake has been received
       */
      function Model(model) {
        this.child = window
        this.model = model
        this.parent = this.child.parent
        return this.sendHandshakeReply()
      }
      /**
       * Responds to a handshake initiated by the Parent
       * @return {Promise} Resolves an object that exposes an API for the Child
       */

      var _proto4 = Model.prototype

      _proto4.sendHandshakeReply = function sendHandshakeReply() {
        var _this5 = this

        return new Postmate.Promise(function (resolve, reject) {
          var shake = function shake(e) {
            if (!e.data.postmate) {
              return
            }

            if (e.data.postmate === 'handshake') {
              // if (process.env.NODE_ENV !== 'production') {
              //   log('Child: Received handshake from Parent')
              // }

              _this5.child.removeEventListener('message', shake, false)

              // if (process.env.NODE_ENV !== 'production') {
              //   log('Child: Sending handshake reply to Parent')
              // }

              e.source.postMessage(
                {
                  postmate: 'handshake-reply',
                  type: messageType
                },
                e.origin
              )
              _this5.parentOrigin = e.origin // Extend model with the one provided by the parent

              var defaults = e.data.model

              if (defaults) {
                Object.keys(defaults).forEach(function (key) {
                  _this5.model[key] = defaults[key]
                })

                // if (process.env.NODE_ENV !== 'production') {
                //   log('Child: Inherited and extended model from Parent')
                // }
              }

              // if (process.env.NODE_ENV !== 'production') {
              //   log('Child: Saving Parent origin', _this5.parentOrigin)
              // }

              return resolve(new ChildAPI(_this5))
            }

            return reject('Handshake Reply Failed')
          }

          _this5.child.addEventListener('message', shake, false)
        })
      }

      return Model
    })()
  exports('postmate', Postmate)
})