Skip to content

Commit

Permalink
Finished the tests.
Browse files Browse the repository at this point in the history
Refs #3
  • Loading branch information
James Hiscock committed Feb 7, 2015
1 parent 89b53f4 commit 00943a7
Show file tree
Hide file tree
Showing 10 changed files with 657 additions and 151 deletions.
4 changes: 2 additions & 2 deletions .zuul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ browsers:
version: latest
- name: android
version: latest
# html: ./tests/parent.html
builder: zuulnice
browserify:
extensions:
- .js
- .js
server: './tests/support/server.js'
243 changes: 118 additions & 125 deletions dist/bundled.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,101 @@
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Postie=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
// Generated by CoffeeScript 1.6.3
(function() {
var EventEmitter, Postie,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice;
module.exports = Postie

EventEmitter = require('events').EventEmitter;
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var postMessage = require('./postmessage')

Postie = (function(_super) {
__extends(Postie, _super);
function Postie (target, origin) {
if (!(this instanceof Postie)) return new Postie(target, origin)

Postie.prototype.target = null;
// '*' is a wildcard origin
if (origin == null) origin = '*'

Postie.prototype.origin = null;
EventEmitter.call(this)

function Postie(target, origin) {
if (origin == null) {
origin = '*';
}
this.handleMessage = __bind(this.handleMessage, this);
if (!(this instanceof Postie)) {
return new Postie(target, origin);
}
Postie.__super__.constructor.call(this);
this.target = target;
this.origin = origin;
this.listen();
}

/*
Sends a package over channel.
- `channel` (String): The channel to send the package over
- `pkg...` (Array...): The package to send. It will take any arguments after the first,
stick them in a JSON array and then on the other end call the callback
with those arguments applied to the callback.
Returns the result of the postMessage call.
*/


Postie.prototype.post = function() {
var channel, packed, pkg;
channel = arguments[0], pkg = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
packed = this.pack(channel, pkg);
if (this.target.postMessage) {
return this.target.postMessage(packed, this.origin);
}
};

/*
Sets up the postMessage handler
*/


Postie.prototype.listen = function() {
if (window.addEventListener) {
return window.addEventListener('message', this.handleMessage);
} else {
return window.attachEvent('onmessage', this.handleMessage);
}
};
this.target = target
this.origin = origin

/*
Handles a postmessage event. Attempt to unpack it, and if we can emit an
event.
- `event` (Event): The event to handle.
*/
this.listen()
}
inherits(Postie, EventEmitter)

Postie.prototype.post = post
Postie.prototype.listen = listen
Postie.prototype.stopListening = stopListening
Postie.prototype.handleMessage = handleMessage
Postie.prototype.unpack = unpack
Postie.prototype.pack = pack

// Get the channel and arguments and send it to the target
// Channel is the event that the other side will be listening for
function post (channel) {
var args = Array.prototype.slice.call(arguments, 1)
var packed = this.pack(channel, args)
postMessage(this.target, packed, this.origin)
}

// Listens in a cross-browser fashion. When postmessage isn't available
// we'll either have to change listen or fake message events somehow.
function listen () {
var _this = this

Postie.prototype.handleMessage = function(event) {
var unpackaged;
if (unpackaged = this.unpack(event.data)) {
return this.emit.apply(this, [unpackaged.channel].concat(__slice.call(unpackaged["package"])));
}
};

/*
Takes a string from a postmessage event and tries to unpack it. If it is
successful it will return the unpacked object, otherwise it will return
false.
- `data` (String): The data to attempt to unpack.
Returns the unpacked data as an Object, or `false`.
*/


Postie.prototype.unpack = function(data) {
var error, pkg;
try {
pkg = JSON.parse(data);
return {
channel: pkg._postie.channel,
"package": pkg._postie["package"]
};
} catch (_error) {
error = _error;
return false;
}
};

/*
Packs a channel string and a package to send into a String that we can send
over postMessage to be unpacked on the other side.
- `channel` (String): The channel the package is being sent on.
- `pkg` (Mixed): The package to pack with the channel into the string.
Returns a String which can be unpacked into its old representation via
`@unpack()`.
*/


Postie.prototype.pack = function(channel, pkg) {
return JSON.stringify({
_postie: {
channel: channel,
"package": pkg
}
});
};
this.handler = function () {
_this.handleMessage.apply(_this, arguments)
}

return Postie;
if (window.addEventListener) {
window.addEventListener('message', this.handler)
}
else {
window.attachEvent('onmessage', this.handler)
}
}

})(EventEmitter);
// Cleans up event listeners
function stopListening () {
if (window.removeEventListener) {
window.removeEventListener('message', this.handler)
}
else {
window.detachEvent('onmessage', this.handler)
}
}

module.exports = Postie;
// Unpacks and emits
function handleMessage (ev) {
var unpacked = this.unpack(ev.data)
if (unpacked) {
var args = [ unpacked.channel ]
args.push.apply(args, unpacked.args)
this.emit.apply(this, args)
}
}

}).call(this);
// Takes a message data string and deserialises it
function unpack (data) {
// We don't control all message events, they won't always be JSON
try {
var unpacked = JSON.parse(data)
if (unpacked.__postie) return unpacked.__postie
return false
}
catch (e) {
return false
}
}

},{"events":2}],2:[function(require,module,exports){
// Takes a channel and the arguments to emit with and serialises it
// for transmission
function pack (channel, args) {
return JSON.stringify({
__postie: {
channel: channel
, args: args
}
})
}
},{"./postmessage":4,"events":2,"inherits":3}],2:[function(require,module,exports){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
Expand Down Expand Up @@ -193,10 +155,8 @@ EventEmitter.prototype.emit = function(type) {
er = arguments[1];
if (er instanceof Error) {
throw er; // Unhandled 'error' event
} else {
throw TypeError('Uncaught, unspecified "error" event.');
}
return false;
throw TypeError('Uncaught, unspecified "error" event.');
}
}

Expand Down Expand Up @@ -438,5 +398,38 @@ function isUndefined(arg) {
return arg === void 0;
}

},{}],3:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
}

},{}],4:[function(require,module,exports){
module.exports = postMessage

// Separating it into a function so we can shim postmessage when
// it's not available
function postMessage (target, packed, origin) {
if (target.postMessage) return target.postMessage(packed, origin)
}
},{}]},{},[1])(1)
});
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ inherits(Postie, EventEmitter)

Postie.prototype.post = post
Postie.prototype.listen = listen
Postie.prototype.stopListening = stopListening
Postie.prototype.handleMessage = handleMessage
Postie.prototype.unpack = unpack
Postie.prototype.pack = pack
Expand All @@ -38,15 +39,25 @@ function post (channel) {
function listen () {
var _this = this

function handler () {
this.handler = function () {
_this.handleMessage.apply(_this, arguments)
}

if (window.addEventListener) {
window.addEventListener('message', handler)
window.addEventListener('message', this.handler)
}
else {
window.attachEvent('onmessage', handler)
window.attachEvent('onmessage', this.handler)
}
}

// Cleans up event listeners
function stopListening () {
if (window.removeEventListener) {
window.removeEventListener('message', this.handler)
}
else {
window.detachEvent('onmessage', this.handler)
}
}

Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "Abstracts postmessage into streams and or event emitters.",
"main": "index.js",
"scripts": {
"bundle": "browserify -s Postie index.js > dist/bundled.js",
"test": "zuul -- tests/index.js",
"test-local": "zuul --local 8080 -- tests/index.js"
"bundle": "browserify -s Postie index.js > dist/bundled.js; cp dist/bundled.js tests/support/postie.js",
"test": "npm run bundle; zuul -- tests/index.js",
"test-local": "npm run bundle; zuul --local 8080 -- tests/index.js"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +26,8 @@
},
"devDependencies": {
"browserify": "^8.1.3",
"connect": "^3.3.4",
"serve-static": "^1.8.1",
"tape": "^3.5.0",
"zuul": "^1.17.1",
"zuulnice": "^1.0.0"
Expand Down
9 changes: 0 additions & 9 deletions tests/frame.html

This file was deleted.

Loading

0 comments on commit 00943a7

Please sign in to comment.