Socket
Socket
Sign inDemoInstall

broadcast-channel

Package Overview
Dependencies
8
Maintainers
1
Versions
98
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.0.1

51

dist/es/index.js

@@ -9,5 +9,3 @@ import { isPromise } from './util.js';

var BroadcastChannel = function BroadcastChannel(name) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var BroadcastChannel = function BroadcastChannel(name, options) {
this.name = name;

@@ -18,26 +16,9 @@ this.options = fillOptionsWithDefaults(options);

this._preparePromise = null;
this._prepare();
_prepareChannel(this);
};
BroadcastChannel.prototype = {
_prepare: function _prepare() {
postMessage: function postMessage(msg) {
var _this = this;
var maybePromise = this.method.create(this.name, this.options);
if (isPromise(maybePromise)) {
this._preparePromise = maybePromise;
maybePromise.then(function (s) {
// used in tests to simulate slow runtime
if (_this.options.prepareDelay) {
// await new Promise(res => setTimeout(res, this.options.prepareDelay));
}
_this._state = s;
});
} else {
this._state = maybePromise;
}
},
postMessage: function postMessage(msg) {
var _this2 = this;
var msgObj = {

@@ -54,3 +35,3 @@ time: new Date().getTime(),

return awaitPrepare.then(function () {
return _this2.method.postMessage(_this2._state, msgObj);
return _this.method.postMessage(_this._state, msgObj);
});

@@ -60,3 +41,3 @@ },

set onmessage(fn) {
var _this3 = this;
var _this2 = this;

@@ -66,3 +47,3 @@ var time = new Date().getTime() - 5;

this._preparePromise.then(function () {
_this3.method.onMessage(_this3._state, messageHandler(fn, time), time);
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time);
});

@@ -74,3 +55,3 @@ } else {

close: function close() {
var _this4 = this;
var _this3 = this;

@@ -80,3 +61,3 @@ this.closed = true;

return awaitPrepare.then(function () {
return _this4.method.close(_this4._state);
return _this3.method.close(_this3._state);
});

@@ -90,2 +71,18 @@ },

function _prepareChannel(channel) {
var maybePromise = channel.method.create(channel.name, channel.options);
if (isPromise(maybePromise)) {
channel._preparePromise = maybePromise;
maybePromise.then(function (s) {
// used in tests to simulate slow runtime
/*if (channel.options.prepareDelay) {
await new Promise(res => setTimeout(res, this.options.prepareDelay));
}*/
channel._state = s;
});
} else {
channel._state = maybePromise;
}
}
function messageHandler(fn, minTime) {

@@ -92,0 +89,0 @@ return function (msgObj) {

@@ -5,12 +5,7 @@ /**

* @link https://github.com/w3c/IndexedDB/issues/51
* So we use the localstorage 'storage'-event
* to ping other tabs when a message comes in
*/
var isNode = require('detect-node');
var randomToken = require('random-token');
var randomInt = require('random-int');
var IdleQueue = require('custom-idle-queue');
import { sleep } from '../util.js';
import { sleep, randomInt, randomToken } from '../util.js';

@@ -168,5 +163,2 @@ import { fillOptionsWithDefaults } from '../options';

// ensures we do not read messages in parrallel
var readQueue = new IdleQueue(1);
return createDatabase(channelName).then(function (db) {

@@ -182,3 +174,3 @@ var state = {

messagesCallback: null,
readQueue: readQueue,
readQueuePromises: [],
db: db

@@ -201,3 +193,3 @@ };

return handleMessagePing(state).then(function () {
return readNewMessages(state).then(function () {
return sleep(state.options.idb.fallbackInterval);

@@ -210,25 +202,5 @@ }).then(function () {

/**
* when the storage-event pings, so that we now new messages came,
* run this
* reads all new messages from the database and emits them
*/
export function handleMessagePing(state) {
/**
* when there are no listener, we do nothing
*/
if (!state.messagesCallback) return Promise.resolve();
/**
* if we have 2 or more read-tasks in the queue,
* we do not have to set more
*/
if (state.readQueue._idleCalls.size > 1) return Promise.resolve();
return state.readQueue.requestIdlePromise().then(function () {
return state.readQueue.wrapCall(function () {
return _handleMessagePingInner(state);
});
});
}
function _handleMessagePingInner(state) {
function readNewMessages(state) {
return getMessagesHigherThen(state.db, state.lastCursorId).then(function (newerMessages) {

@@ -271,3 +243,2 @@ var useMessages = newerMessages.map(function (msgObj) {

channelState.closed = true;
channelState.readQueue.clear();
channelState.db.close();

@@ -287,3 +258,3 @@ }

channelState.messagesCallback = fn;
handleMessagePing(channelState);
readNewMessages(channelState);
}

@@ -290,0 +261,0 @@

@@ -10,7 +10,6 @@ /**

var isNode = require('detect-node');
var randomToken = require('random-token');
import { fillOptionsWithDefaults } from '../options';
import { sleep } from '../util';
import { sleep, randomToken } from '../util';

@@ -17,0 +16,0 @@ var KEY_PREFIX = 'pubkey.broadcastChannel-';

@@ -17,4 +17,2 @@ import _regeneratorRuntime from 'babel-runtime/regenerator';

import isNode from 'detect-node';
import randomToken from 'random-token';
import randomInt from 'random-int';
import IdleQueue from 'custom-idle-queue';

@@ -25,4 +23,18 @@ import unload from 'unload';

import { cleanPipeName } from '../util';
import { randomInt, randomToken } from '../util';
/**
* windows sucks, so we have handle windows-type of socket-paths
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
*/
export function cleanPipeName(str) {
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
var mkdir = util.promisify(fs.mkdir);

@@ -29,0 +41,0 @@ var writeFile = util.promisify(fs.writeFile);

@@ -12,16 +12,2 @@ /**

/**
* windows sucks
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
*/
export function cleanPipeName(str) {
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
export function sleep(time) {

@@ -32,2 +18,18 @@ if (!time) time = 0;

});
}
export function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* https://stackoverflow.com/a/1349426/3443137
*/
export function randomToken(length) {
var text = '';
var possible = 'abcdefghijklmnopqrstuvwxzy0123456789';
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}return text;
}

@@ -11,5 +11,3 @@ 'use strict';

var BroadcastChannel = function BroadcastChannel(name) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var BroadcastChannel = function BroadcastChannel(name, options) {
this.name = name;

@@ -20,26 +18,9 @@ this.options = (0, _options.fillOptionsWithDefaults)(options);

this._preparePromise = null;
this._prepare();
_prepareChannel(this);
};
BroadcastChannel.prototype = {
_prepare: function _prepare() {
postMessage: function postMessage(msg) {
var _this = this;
var maybePromise = this.method.create(this.name, this.options);
if ((0, _util.isPromise)(maybePromise)) {
this._preparePromise = maybePromise;
maybePromise.then(function (s) {
// used in tests to simulate slow runtime
if (_this.options.prepareDelay) {
// await new Promise(res => setTimeout(res, this.options.prepareDelay));
}
_this._state = s;
});
} else {
this._state = maybePromise;
}
},
postMessage: function postMessage(msg) {
var _this2 = this;
var msgObj = {

@@ -56,3 +37,3 @@ time: new Date().getTime(),

return awaitPrepare.then(function () {
return _this2.method.postMessage(_this2._state, msgObj);
return _this.method.postMessage(_this._state, msgObj);
});

@@ -62,3 +43,3 @@ },

set onmessage(fn) {
var _this3 = this;
var _this2 = this;

@@ -68,3 +49,3 @@ var time = new Date().getTime() - 5;

this._preparePromise.then(function () {
_this3.method.onMessage(_this3._state, messageHandler(fn, time), time);
_this2.method.onMessage(_this2._state, messageHandler(fn, time), time);
});

@@ -76,3 +57,3 @@ } else {

close: function close() {
var _this4 = this;
var _this3 = this;

@@ -82,3 +63,3 @@ this.closed = true;

return awaitPrepare.then(function () {
return _this4.method.close(_this4._state);
return _this3.method.close(_this3._state);
});

@@ -92,2 +73,18 @@ },

function _prepareChannel(channel) {
var maybePromise = channel.method.create(channel.name, channel.options);
if ((0, _util.isPromise)(maybePromise)) {
channel._preparePromise = maybePromise;
maybePromise.then(function (s) {
// used in tests to simulate slow runtime
/*if (channel.options.prepareDelay) {
await new Promise(res => setTimeout(res, this.options.prepareDelay));
}*/
channel._state = s;
});
} else {
channel._state = maybePromise;
}
}
function messageHandler(fn, minTime) {

@@ -94,0 +91,0 @@ return function (msgObj) {

@@ -16,3 +16,2 @@ 'use strict';

exports.create = create;
exports.handleMessagePing = handleMessagePing;
exports.close = close;

@@ -31,10 +30,5 @@ exports.postMessage = postMessage;

* @link https://github.com/w3c/IndexedDB/issues/51
* So we use the localstorage 'storage'-event
* to ping other tabs when a message comes in
*/
var isNode = require('detect-node');
var randomToken = require('random-token');
var randomInt = require('random-int');
var IdleQueue = require('custom-idle-queue');

@@ -188,7 +182,4 @@ var DB_PREFIX = 'pubkey.broadcast-channel-0-';

var uuid = randomToken(10);
var uuid = (0, _util.randomToken)(10);
// ensures we do not read messages in parrallel
var readQueue = new IdleQueue(1);
return createDatabase(channelName).then(function (db) {

@@ -204,3 +195,3 @@ var state = {

messagesCallback: null,
readQueue: readQueue,
readQueuePromises: [],
db: db

@@ -223,3 +214,3 @@ };

return handleMessagePing(state).then(function () {
return readNewMessages(state).then(function () {
return (0, _util.sleep)(state.options.idb.fallbackInterval);

@@ -232,25 +223,5 @@ }).then(function () {

/**
* when the storage-event pings, so that we now new messages came,
* run this
* reads all new messages from the database and emits them
*/
function handleMessagePing(state) {
/**
* when there are no listener, we do nothing
*/
if (!state.messagesCallback) return Promise.resolve();
/**
* if we have 2 or more read-tasks in the queue,
* we do not have to set more
*/
if (state.readQueue._idleCalls.size > 1) return Promise.resolve();
return state.readQueue.requestIdlePromise().then(function () {
return state.readQueue.wrapCall(function () {
return _handleMessagePingInner(state);
});
});
}
function _handleMessagePingInner(state) {
function readNewMessages(state) {
return getMessagesHigherThen(state.db, state.lastCursorId).then(function (newerMessages) {

@@ -293,3 +264,2 @@ var useMessages = newerMessages.map(function (msgObj) {

channelState.closed = true;
channelState.readQueue.clear();
channelState.db.close();

@@ -300,3 +270,3 @@ }

return writeMessage(channelState.db, channelState.uuid, messageJson).then(function () {
if (randomInt(0, 10) === 0) {
if ((0, _util.randomInt)(0, 10) === 0) {
/* await (do not await) */cleanOldMessages(channelState.db, channelState.options.idb.ttl);

@@ -310,3 +280,3 @@ }

channelState.messagesCallback = fn;
handleMessagePing(channelState);
readNewMessages(channelState);
}

@@ -313,0 +283,0 @@

@@ -30,3 +30,2 @@ 'use strict';

var isNode = require('detect-node');
var randomToken = require('random-token');

@@ -67,3 +66,3 @@ var KEY_PREFIX = 'pubkey.broadcastChannel-';

var writeObj = {
token: randomToken(10),
token: (0, _util.randomToken)(10),
time: new Date().getTime(),

@@ -113,3 +112,3 @@ data: messageJson,

var startTime = new Date().getTime();
var uuid = randomToken(10);
var uuid = (0, _util.randomToken)(10);

@@ -116,0 +115,0 @@ // contains all messages that have been emitted before

@@ -216,3 +216,3 @@ 'use strict';

};
token = (0, _randomToken2['default'])(12);
token = (0, _util2.randomToken)(12);
fileName = time + '_' + readerUuid + '_' + token + '.json';

@@ -422,3 +422,3 @@ msgPath = path.join(getPaths(channelName).messages, fileName);

case 3:
uuid = (0, _randomToken2['default'])(10);
uuid = (0, _util2.randomToken)(10);
_context12.next = 6;

@@ -809,3 +809,3 @@ return Promise.all([getReadersUuids(channelName), createSocketEventEmitter(channelName, uuid), createSocketInfoFile(channelName, uuid)]);

case 8:
if (!((0, _randomInt2['default'])(0, 10) === 0)) {
if (!((0, _util2.randomInt)(0, 10) === 0)) {
_context18.next = 14;

@@ -882,2 +882,3 @@ break;

exports.cleanPipeName = cleanPipeName;
exports.getPaths = getPaths;

@@ -920,10 +921,2 @@ exports.socketPath = socketPath;

var _randomToken = require('random-token');
var _randomToken2 = _interopRequireDefault(_randomToken);
var _randomInt = require('random-int');
var _randomInt2 = _interopRequireDefault(_randomInt);
var _customIdleQueue = require('custom-idle-queue');

@@ -946,2 +939,6 @@

/**
* windows sucks, so we have handle windows-type of socket-paths
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
*/
/**
* this method is used in nodejs-environments.

@@ -951,2 +948,12 @@ * The ipc is handled via sockets and file-writes to the tmp-folder

function cleanPipeName(str) {
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
var mkdir = util.promisify(fs.mkdir);

@@ -981,3 +988,3 @@ var writeFile = util.promisify(fs.writeFile);

var socketPath = path.join(paths.readers, readerUuid + '.s');
return (0, _util2.cleanPipeName)(socketPath);
return cleanPipeName(socketPath);
}

@@ -984,0 +991,0 @@

@@ -7,4 +7,5 @@ 'use strict';

exports.isPromise = isPromise;
exports.cleanPipeName = cleanPipeName;
exports.sleep = sleep;
exports.randomInt = randomInt;
exports.randomToken = randomToken;
/**

@@ -21,16 +22,2 @@ * returns true if the given object is a promise

/**
* windows sucks
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
*/
function cleanPipeName(str) {
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
function sleep(time) {

@@ -41,2 +28,18 @@ if (!time) time = 0;

});
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* https://stackoverflow.com/a/1349426/3443137
*/
function randomToken(length) {
var text = '';
var possible = 'abcdefghijklmnopqrstuvwxzy0123456789';
for (var i = 0; i < 5; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}return text;
}
{
"name": "broadcast-channel",
"version": "1.0.0",
"version": "1.0.1",
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js",

@@ -59,4 +59,2 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme",

"js-sha3": "0.7.0",
"random-int": "1.0.0",
"random-token": "0.0.8",
"unload": "1.3.6"

@@ -112,2 +110,4 @@ },

"pre-commit": "1.2.2",
"random-int": "1.0.0",
"random-token": "0.0.8",
"rimraf": "2.6.2",

@@ -114,0 +114,0 @@ "testcafe": "0.20.3",

@@ -59,1 +59,11 @@ BEFORE:

AFTER: 4077
====== build-size 2
BEFORE: 4077
AFTER: 3795
====== build-size 3
BEFORE: 3795
AFTER: 3110

@@ -17,3 +17,3 @@ import {

const BroadcastChannel = function (name, options = {}) {
const BroadcastChannel = function (name, options) {
this.name = name;

@@ -24,21 +24,6 @@ this.options = fillOptionsWithDefaults(options);

this._preparePromise = null;
this._prepare();
_prepareChannel(this);
};
BroadcastChannel.prototype = {
_prepare() {
const maybePromise = this.method.create(this.name, this.options);
if (isPromise(maybePromise)) {
this._preparePromise = maybePromise;
maybePromise.then(s => {
// used in tests to simulate slow runtime
if (this.options.prepareDelay) {
// await new Promise(res => setTimeout(res, this.options.prepareDelay));
}
this._state = s;
});
} else {
this._state = maybePromise;
}
},
postMessage(msg) {

@@ -97,2 +82,18 @@ const msgObj = {

function _prepareChannel(channel){
const maybePromise = channel.method.create(channel.name, channel.options);
if (isPromise(maybePromise)) {
channel._preparePromise = maybePromise;
maybePromise.then(s => {
// used in tests to simulate slow runtime
/*if (channel.options.prepareDelay) {
await new Promise(res => setTimeout(res, this.options.prepareDelay));
}*/
channel._state = s;
});
} else {
channel._state = maybePromise;
}
}
function messageHandler(fn, minTime) {

@@ -99,0 +100,0 @@ return msgObj => {

@@ -5,13 +5,10 @@ /**

* @link https://github.com/w3c/IndexedDB/issues/51
* So we use the localstorage 'storage'-event
* to ping other tabs when a message comes in
*/
const isNode = require('detect-node');
const randomToken = require('random-token');
const randomInt = require('random-int');
const IdleQueue = require('custom-idle-queue');
import {
sleep
sleep,
randomInt,
randomToken
} from '../util.js';

@@ -167,6 +164,2 @@

// ensures we do not read messages in parrallel
const readQueue = new IdleQueue(1);
return createDatabase(channelName).then(db => {

@@ -182,3 +175,3 @@ const state = {

messagesCallback: null,
readQueue,
readQueuePromises: [],
db

@@ -201,3 +194,3 @@ };

return handleMessagePing(state)
return readNewMessages(state)
.then(() => sleep(state.options.idb.fallbackInterval))

@@ -207,26 +200,6 @@ .then(() => _readLoop(state));

/**
* when the storage-event pings, so that we now new messages came,
* run this
* reads all new messages from the database and emits them
*/
export function handleMessagePing(state) {
/**
* when there are no listener, we do nothing
*/
if (!state.messagesCallback) return Promise.resolve();
/**
* if we have 2 or more read-tasks in the queue,
* we do not have to set more
*/
if (state.readQueue._idleCalls.size > 1) return Promise.resolve();
return state.readQueue.requestIdlePromise()
.then(() => state.readQueue.wrapCall(
() => _handleMessagePingInner(state)
));
}
function _handleMessagePingInner(state) {
function readNewMessages(state) {
return getMessagesHigherThen(state.db, state.lastCursorId)

@@ -265,3 +238,2 @@ .then(newerMessages => {

channelState.closed = true;
channelState.readQueue.clear();
channelState.db.close();

@@ -288,3 +260,3 @@ }

channelState.messagesCallback = fn;
handleMessagePing(channelState);
readNewMessages(channelState);
}

@@ -291,0 +263,0 @@

@@ -10,3 +10,2 @@ /**

const isNode = require('detect-node');
const randomToken = require('random-token');

@@ -18,3 +17,4 @@ import {

import {
sleep
sleep,
randomToken
} from '../util';

@@ -21,0 +21,0 @@

@@ -17,4 +17,2 @@ /**

import isNode from 'detect-node';
import randomToken from 'random-token';
import randomInt from 'random-int';
import IdleQueue from 'custom-idle-queue';

@@ -28,5 +26,23 @@ import unload from 'unload';

import {
cleanPipeName
randomInt,
randomToken
} from '../util';
/**
* windows sucks, so we have handle windows-type of socket-paths
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
*/
export function cleanPipeName(str) {
if (
process.platform === 'win32' &&
!str.startsWith('\\\\.\\pipe\\')
) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
const mkdir = util.promisify(fs.mkdir);

@@ -33,0 +49,0 @@ const writeFile = util.promisify(fs.writeFile);

@@ -13,22 +13,22 @@ /**

export function sleep(time) {
if (!time) time = 0;
return new Promise(res => setTimeout(res, time));
}
export function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
* windows sucks
* @link https://gist.github.com/domenic/2790533#gistcomment-331356
* https://stackoverflow.com/a/1349426/3443137
*/
export function cleanPipeName(str) {
if (
process.platform === 'win32' &&
!str.startsWith('\\\\.\\pipe\\')
) {
str = str.replace(/^\//, '');
str = str.replace(/\//g, '-');
return '\\\\.\\pipe\\' + str;
} else {
return str;
}
};
export function randomToken(length) {
let text = '';
const possible = 'abcdefghijklmnopqrstuvwxzy0123456789';
export function sleep(time) {
if (!time) time = 0;
return new Promise(res => setTimeout(res, time));
for (let i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc