-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 373bc5a
Showing
68 changed files
with
33,550 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"plugins": [ | ||
["transform-es2015-template-literals", { | ||
"loose": true | ||
}], | ||
"transform-es2015-literals", | ||
"transform-es2015-function-name", | ||
"transform-es2015-arrow-functions", | ||
"transform-es2015-block-scoped-functions", ["transform-es2015-classes", { | ||
"loose": true | ||
}], | ||
"transform-es2015-object-super", | ||
"transform-es2015-shorthand-properties", ["transform-es2015-computed-properties", { | ||
"loose": true | ||
}], | ||
["transform-es2015-for-of", { | ||
"loose": true | ||
}], | ||
"transform-es2015-sticky-regex", | ||
"transform-es2015-unicode-regex", | ||
"check-es2015-constants", ["transform-es2015-spread", { | ||
"loose": true | ||
}], | ||
"transform-es2015-parameters", ["transform-es2015-destructuring", { | ||
"loose": true | ||
}], | ||
"transform-es2015-block-scoping", | ||
"transform-object-rest-spread", | ||
"transform-es3-member-expression-literals", | ||
"transform-es3-property-literals", | ||
"transform-async-to-generator", | ||
"transform-regenerator", | ||
["transform-runtime", { | ||
"polyfill": false, | ||
"regenerator": true | ||
}] | ||
], | ||
"env": { | ||
"es5": { | ||
"presets": ["es2015"] | ||
}, | ||
"es6": { | ||
"presets": ["es2017"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"env": { | ||
"browser": true, | ||
"es6": true, | ||
"node": true | ||
}, | ||
"parserOptions": { | ||
"sourceType": "module" | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
4, { | ||
"MemberExpression": "off", | ||
"SwitchCase": 1 | ||
} | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-console": "off", | ||
"no-unused-vars": "error", | ||
"no-var": "error", | ||
"no-throw-literal": "error" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
node_modules/ | ||
package-lock.json | ||
.idea/ | ||
.vscode/ | ||
.transpile_state.json | ||
.com.google.* | ||
shelljs_* | ||
test_tmp/ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.git/ | ||
config/ | ||
docs/ | ||
test/ | ||
test_tmp/ | ||
tmp/ | ||
.babelrc | ||
.editorconfig | ||
.eslintrc.json | ||
.gitignore | ||
.travis.yml | ||
ISSUE_TEMPLATE.md | ||
PULL_REQUEST_TEMPLATE.md | ||
|
||
log.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
unsafe-perm = true | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
language: node_js | ||
sudo: required | ||
os: | ||
- linux | ||
node_js: | ||
- "9.4.0" | ||
before_install: | ||
- export CHROME_BIN=chromium-browser | ||
- export DISPLAY=:99.0 | ||
- sh -e /etc/init.d/xvfb start | ||
script: | ||
- travis_retry npm install --depth 0 --silent | ||
- npm run lint | ||
- npm run build | ||
- travis_retry npm run test:node | ||
- travis_retry npm run test:browser | ||
- travis_retry npm run test:e2e | ||
- npm run test:typings | ||
- npm run test:size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Daniel Meyer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
|
||
<p align="center"> | ||
<a href="https://github.com/pubkey/broadcast-channel"> | ||
<img src="https://assets-cdn.github.com/images/icons/emoji/unicode/1f4e1.png" width="150px" /> | ||
</a> | ||
</p> | ||
|
||
<h1 align="center">BroadcastChannel</h1> | ||
<p align="center"> | ||
<strong>A BroadcastChannel that works in old browsers, new browsers, WebWorkers and NodeJs</strong> | ||
</p> | ||
|
||
<p align="center"> | ||
<a alt="travis" href="https://travis-ci.org/pubkey/broadcast-channel"> | ||
<img src="https://travis-ci.org/pubkey/broadcast-channel.svg?branch=master" /></a> | ||
<a href="https://twitter.com/pubkeypubkey"> | ||
<img src="https://img.shields.io/twitter/follow/pubkeypubkey.svg?style=social&logo=twitter" | ||
alt="follow on Twitter"></a> | ||
</p> | ||
|
||
<br/> | ||
|
||
* * * | ||
|
||
A BroadcastChannel allows simple communication between browsing contexts with the same origin or different NodeJs processes. | ||
|
||
This implementation works with old browsers, new browsers, WebWorkers and NodeJs. You can use it to send messages between multiple browser-tabs, iframes, WebWorkers and NodeJs-processes. | ||
|
||
This behaves similar to the [BroadcastChannel-API](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) which is currently not featured in all browsers. | ||
|
||
## Usage | ||
|
||
This API behaves similar to the [javascript-standard](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API). | ||
|
||
```bash | ||
npm install --save broadcast-channel | ||
``` | ||
|
||
Create a channel in one tab/process and send a message. | ||
|
||
```js | ||
const BroadcastChannel = require('broadcast-channel'); | ||
const channel = new BroadcastChannel('foobar'); | ||
channel.postMessage('I am not alone'); | ||
``` | ||
|
||
Create a channel with the same name in another tab/process and recieve messages. | ||
|
||
```js | ||
const BroadcastChannel = require('broadcast-channel'); | ||
const channel = new BroadcastChannel('foobar'); | ||
channel.onmessage = msg => console.dir(msg); | ||
// > 'I am not alone' | ||
``` | ||
|
||
|
||
Add and remove multiple eventlisteners | ||
|
||
```js | ||
const BroadcastChannel = require('broadcast-channel'); | ||
const channel = new BroadcastChannel('foobar'); | ||
|
||
const handler = msg => console.log(msg); | ||
channel.addEventListener('message', handler); | ||
|
||
// remove it | ||
channel.removeEventListener('message', handler); | ||
``` | ||
|
||
Close the channel if you do not need it anymore. | ||
|
||
```js | ||
channel.close(); | ||
``` | ||
|
||
Set options when creating a channel (optional): | ||
|
||
```js | ||
const options = { | ||
type: 'localstorage', // (optional) enforce a type, oneOf['native', 'idb', 'localstorage', 'node'] | ||
webWorkerSupport: true; // (optional) set this to false if you know that your channel will never be used in a WebWorker (increases performance) | ||
}; | ||
const channel = new BroadcastChannel('foobar', options); | ||
``` | ||
|
||
Create a typed channel in typescript: | ||
|
||
```typescript | ||
import BroadcastChannel from 'broadcast-channel'; | ||
declare type Message = { | ||
foo: string; | ||
}; | ||
const channel: BroadcastChannel<Message> = new BroadcastChannel('foobar'); | ||
channel.postMessage({ | ||
foo: 'bar' | ||
}); | ||
``` | ||
|
||
## Methods: | ||
|
||
Depending in which environment this is used, a proper method is automatically selected to ensure it always works. | ||
|
||
| Method | Used in | Description | | ||
| ---------------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| **Native** | [Modern Browsers](https://caniuse.com/broadcastchannel) | If the browser supports the BroadcastChannel-API, this method will be used because it is the fastest | | ||
| **IndexedDB** | [Browsers with WebWorkers](https://caniuse.com/#feat=indexeddb) | If there is no native BroadcastChannel support, the IndexedDB method is used because it supports messaging between browser-tabs, iframes and WebWorkers | | ||
| **LocalStorage** | [Older Browsers](https://caniuse.com/#feat=namevalue-storage) | In older browsers that do not support IndexedDb, a localstorage-method is used | | ||
| **Sockets** | NodeJs | In NodeJs the communication is handled by sockets that send each other messages | | ||
|
||
|
||
## What this is | ||
|
||
This module is optimised for: | ||
|
||
- **low latency**: When you postMessage on one channel, it should take as low as possible time until other channels recieve the message. | ||
- **lossless**: When you send a message, it should be impossible that the message is lost before other channels recieved it | ||
- **low idle workload**: During the time when no messages are send, there should be a low processor footprint. | ||
|
||
## What this is not | ||
|
||
- This is not a polyfill. Do not set this module to `window.BroadcastChannel`. This implementation behaves similiar to the [BroadcastChannel-Standard](https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API) with these limitations: | ||
- You can only send data that can be `JSON.stringify`-ed. | ||
- While the offical API emits [onmessage-events](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel/onmessage), this module directly emitts the data which was posted | ||
- This is not a replacement for a message queue. If you use this in NodeJs and want send more than 50 messages per second, you should use proper [IPC-Tooling](https://en.wikipedia.org/wiki/Message_queue) | ||
|
||
|
||
## Browser Support | ||
I have tested this in all browsers that I could find. For ie8 and ie9 you must transpile the code before you can use this. If you want to know if this works with your browser, [open the demo page](https://pubkey.github.io/broadcast-channel/). | ||
|
||
## Thanks | ||
Thanks to [Hemanth.HM](https://github.com/hemanth) for the module name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
../dist/ | ||
../node_modules/ | ||
../tmp/ | ||
../test/scripts/worker.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const configuration = { | ||
basePath: '', | ||
frameworks: [ | ||
'mocha', | ||
'browserify', | ||
'detectBrowsers' | ||
], | ||
files: [ | ||
'../test/index.test.js' | ||
], | ||
// reporters: ['progress'], | ||
port: 9876, | ||
colors: true, | ||
autoWatch: false, | ||
|
||
/** | ||
* see | ||
* @link https://github.com/litixsoft/karma-detect-browsers | ||
*/ | ||
detectBrowsers: { | ||
enabled: true, | ||
usePhantomJS: false, | ||
postDetection: function (availableBrowser) { | ||
// return ['Chrome']; // comment in to test specific browser | ||
// return ['Firefox']; // comment in to test specific browser | ||
const browsers = availableBrowser | ||
.filter(b => !['PhantomJS', 'FirefoxAurora', 'FirefoxNightly'].includes(b)) | ||
.map(b => { | ||
if (process.env.TRAVIS && b === 'Chrome') return 'Chrome_travis_ci'; | ||
else return b; | ||
}); | ||
return browsers; | ||
} | ||
}, | ||
|
||
// Karma plugins loaded | ||
plugins: [ | ||
'karma-mocha', | ||
'karma-browserify', | ||
'karma-chrome-launcher', | ||
'karma-edge-launcher', | ||
'karma-firefox-launcher', | ||
'karma-ie-launcher', | ||
'karma-opera-launcher', | ||
'karma-detect-browsers' | ||
], | ||
|
||
// Source files that you wanna generate coverage for. | ||
// Do not include tests or libraries (these files will be instrumented by Istanbul) | ||
preprocessors: { | ||
'../test/*.test.js': ['browserify'] | ||
}, | ||
|
||
client: { | ||
mocha: { | ||
bail: false, | ||
timeout: 6000 | ||
}, | ||
captureConsole: true | ||
}, | ||
// browsers: ['ChromeNoSandbox'], | ||
browserDisconnectTimeout: 8000, | ||
processKillTimeout: 8000, | ||
customLaunchers: { | ||
Chrome_travis_ci: { | ||
base: 'Chrome', | ||
flags: ['--no-sandbox'] | ||
} | ||
}, | ||
singleRun: true | ||
}; | ||
|
||
module.exports = function (config) { | ||
config.set(configuration); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var BroadcastChannel = require('./index.es5.js'); | ||
|
||
window['BroadcastChannel2'] = BroadcastChannel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* because babel can only export on default-attribute, | ||
* we use this for the non-module-build | ||
* this ensures that users do not have to use | ||
* var BroadcastChannel = require('broadcast-channel').default; | ||
* but | ||
* var BroadcastChannel = require('broadcast-channel'); | ||
*/ | ||
|
||
import BroadcastChannel from './index.js'; | ||
|
||
module.exports = BroadcastChannel; |
Oops, something went wrong.