Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

broadcast-channel

Package Overview
Dependencies
Maintainers
1
Versions
98
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

broadcast-channel - npm Package Compare versions

Comparing version 2.1.1 to 2.1.2

422

dist/es/methods/node.js

@@ -80,3 +80,2 @@ import _regeneratorRuntime from "@babel/runtime/regenerator";

var ret = {
base: TMP_FOLDER_BASE,
channelBase: channelPathBase,

@@ -95,2 +94,32 @@ readers: folderPathReaders,

function ensureBaseFolderExists() {
return _ensureBaseFolderExists.apply(this, arguments);
}
function _ensureBaseFolderExists() {
_ensureBaseFolderExists = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee4() {
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(TMP_FOLDER_BASE)["catch"](function () {
return null;
});
}
return _context4.abrupt("return", ENSURE_BASE_FOLDER_EXISTS_PROMISE);
case 2:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
return _ensureBaseFolderExists.apply(this, arguments);
}
function ensureFoldersExist(_x, _x2) {

@@ -108,20 +137,13 @@ return _ensureFoldersExist.apply(this, arguments);

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee4(channelName, paths) {
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
_regeneratorRuntime.mark(function _callee5(channelName, paths) {
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context5.prev = _context5.next) {
case 0:
paths = paths || getPaths(channelName);
_context5.next = 3;
return ensureBaseFolderExists();
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(paths.base)["catch"](function () {
return null;
});
}
_context4.next = 4;
return ENSURE_BASE_FOLDER_EXISTS_PROMISE;
case 4:
_context4.next = 6;
case 3:
_context5.next = 5;
return mkdir(paths.channelBase)["catch"](function () {

@@ -131,4 +153,4 @@ return null;

case 6:
_context4.next = 8;
case 5:
_context5.next = 7;
return Promise.all([mkdir(paths.readers)["catch"](function () {

@@ -140,8 +162,8 @@ return null;

case 8:
case 7:
case "end":
return _context4.stop();
return _context5.stop();
}
}
}, _callee4, this);
}, _callee5, this);
}));

@@ -158,13 +180,9 @@ return _ensureFoldersExist.apply(this, arguments);

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee5() {
var paths, removePath;
return _regeneratorRuntime.wrap(function _callee5$(_context5) {
_regeneratorRuntime.mark(function _callee6() {
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context5.prev = _context5.next) {
switch (_context6.prev = _context6.next) {
case 0:
paths = getPaths('foobar');
removePath = paths.base;
if (!(!removePath || removePath === '' || removePath === '/')) {
_context5.next = 4;
if (!(!TMP_FOLDER_BASE || TMP_FOLDER_BASE === '' || TMP_FOLDER_BASE === '/')) {
_context6.next = 2;
break;

@@ -175,17 +193,17 @@ }

case 4:
case 2:
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
_context5.next = 7;
return removeDir(paths.base);
_context6.next = 5;
return removeDir(TMP_FOLDER_BASE);
case 7:
case 5:
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
return _context5.abrupt("return", true);
return _context6.abrupt("return", true);
case 9:
case 7:
case "end":
return _context5.stop();
return _context6.stop();
}
}
}, _callee5, this);
}, _callee6, this);
}));

@@ -222,2 +240,45 @@ return _clearNodeFolder.apply(this, arguments);

/**
* returns the amount of channel-folders in the tmp-directory
* @return {Promise<number>}
*/
function countChannelFolders() {
return _countChannelFolders.apply(this, arguments);
}
function _countChannelFolders() {
_countChannelFolders = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee7() {
var folders;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return ensureBaseFolderExists();
case 2:
_context7.next = 4;
return readdir(TMP_FOLDER_BASE);
case 4:
folders = _context7.sent;
return _context7.abrupt("return", folders.length);
case 6:
case "end":
return _context7.stop();
}
}
}, _callee7, this);
}));
return _countChannelFolders.apply(this, arguments);
}
function connectionError(_x3) {
return _connectionError.apply(this, arguments);
}
/**
* creates the socket-file and subscribes to it

@@ -228,3 +289,46 @@ * @return {{emitter: EventEmitter, server: any}}

function createSocketEventEmitter(_x3, _x4, _x5) {
function _connectionError() {
_connectionError = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee8(originalError) {
var count, addObj, text, newError;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return countChannelFolders();
case 2:
count = _context8.sent;
if (!(count < 30)) {
_context8.next = 5;
break;
}
return _context8.abrupt("return", originalError);
case 5:
addObj = {};
Object.entries(originalError).forEach(function (_ref5) {
var k = _ref5[0],
v = _ref5[1];
return addObj[k] = v;
});
text = 'BroadcastChannel.create(): error: ' + 'This might happen if you have created to many channels, ' + 'like when you use BroadcastChannel in unit-tests.' + 'Try using BroadcastChannel.clearNodeFolder() to clear the tmp-folder before each test.' + 'See https://github.com/pubkey/broadcast-channel#clear-tmp-folder';
newError = new Error(text + ': ' + JSON.stringify(addObj, null, 2));
return _context8.abrupt("return", newError);
case 10:
case "end":
return _context8.stop();
}
}
}, _callee8, this);
}));
return _connectionError.apply(this, arguments);
}
function createSocketEventEmitter(_x4, _x5, _x6) {
return _createSocketEventEmitter.apply(this, arguments);

@@ -236,7 +340,7 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee6(channelName, readerUuid, paths) {
_regeneratorRuntime.mark(function _callee11(channelName, readerUuid, paths) {
var pathToSocket, emitter, server;
return _regeneratorRuntime.wrap(function _callee6$(_context6) {
return _regeneratorRuntime.wrap(function _callee11$(_context11) {
while (1) {
switch (_context6.prev = _context6.next) {
switch (_context11.prev = _context11.next) {
case 0:

@@ -251,11 +355,78 @@ pathToSocket = socketPath(channelName, readerUuid, paths);

});
_context6.next = 5;
_context11.next = 5;
return new Promise(function (resolve, reject) {
server.listen(pathToSocket, function (err, res) {
if (err) reject(err);else resolve(res);
});
server.on('error',
/*#__PURE__*/
function () {
var _ref6 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee9(err) {
var useErr;
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return connectionError(err);
case 2:
useErr = _context9.sent;
reject(useErr);
case 4:
case "end":
return _context9.stop();
}
}
}, _callee9, this);
}));
return function (_x24) {
return _ref6.apply(this, arguments);
};
}());
server.listen(pathToSocket,
/*#__PURE__*/
function () {
var _ref7 = _asyncToGenerator(
/*#__PURE__*/
_regeneratorRuntime.mark(function _callee10(err, res) {
var useErr;
return _regeneratorRuntime.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!err) {
_context10.next = 7;
break;
}
_context10.next = 3;
return connectionError(err);
case 3:
useErr = _context10.sent;
reject(useErr);
_context10.next = 8;
break;
case 7:
resolve(res);
case 8:
case "end":
return _context10.stop();
}
}
}, _callee10, this);
}));
return function (_x25, _x26) {
return _ref7.apply(this, arguments);
};
}());
});
case 5:
return _context6.abrupt("return", {
return _context11.abrupt("return", {
path: pathToSocket,

@@ -268,6 +439,6 @@ emitter: emitter,

case "end":
return _context6.stop();
return _context11.stop();
}
}
}, _callee6, this);
}, _callee11, this);
}));

@@ -277,3 +448,3 @@ return _createSocketEventEmitter.apply(this, arguments);

function openClientConnection(_x6, _x7) {
function openClientConnection(_x7, _x8) {
return _openClientConnection.apply(this, arguments);

@@ -291,11 +462,11 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee7(channelName, readerUuid) {
_regeneratorRuntime.mark(function _callee12(channelName, readerUuid) {
var pathToSocket, client;
return _regeneratorRuntime.wrap(function _callee7$(_context7) {
return _regeneratorRuntime.wrap(function _callee12$(_context12) {
while (1) {
switch (_context7.prev = _context7.next) {
switch (_context12.prev = _context12.next) {
case 0:
pathToSocket = socketPath(channelName, readerUuid);
client = new net.Socket();
_context7.next = 4;
_context12.next = 4;
return new Promise(function (res) {

@@ -306,10 +477,10 @@ client.connect(pathToSocket, res);

case 4:
return _context7.abrupt("return", client);
return _context12.abrupt("return", client);
case 5:
case "end":
return _context7.stop();
return _context12.stop();
}
}
}, _callee7, this);
}, _callee12, this);
}));

@@ -345,3 +516,3 @@ return _openClientConnection.apply(this, arguments);

function getReadersUuids(_x8, _x9) {
function getReadersUuids(_x9, _x10) {
return _getReadersUuids.apply(this, arguments);

@@ -353,16 +524,16 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee8(channelName, paths) {
_regeneratorRuntime.mark(function _callee13(channelName, paths) {
var readersPath, files;
return _regeneratorRuntime.wrap(function _callee8$(_context8) {
return _regeneratorRuntime.wrap(function _callee13$(_context13) {
while (1) {
switch (_context8.prev = _context8.next) {
switch (_context13.prev = _context13.next) {
case 0:
paths = paths || getPaths(channelName);
readersPath = paths.readers;
_context8.next = 4;
_context13.next = 4;
return readdir(readersPath);
case 4:
files = _context8.sent;
return _context8.abrupt("return", files.map(function (file) {
files = _context13.sent;
return _context13.abrupt("return", files.map(function (file) {
return file.split('.');

@@ -378,6 +549,6 @@ }).filter(function (split) {

case "end":
return _context8.stop();
return _context13.stop();
}
}
}, _callee8, this);
}, _callee13, this);
}));

@@ -387,3 +558,3 @@ return _getReadersUuids.apply(this, arguments);

function messagePath(_x10, _x11, _x12, _x13) {
function messagePath(_x11, _x12, _x13, _x14) {
return _messagePath.apply(this, arguments);

@@ -395,18 +566,18 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee9(channelName, time, token, writerUuid) {
_regeneratorRuntime.mark(function _callee14(channelName, time, token, writerUuid) {
var fileName, msgPath;
return _regeneratorRuntime.wrap(function _callee9$(_context9) {
return _regeneratorRuntime.wrap(function _callee14$(_context14) {
while (1) {
switch (_context9.prev = _context9.next) {
switch (_context14.prev = _context14.next) {
case 0:
fileName = time + '_' + writerUuid + '_' + token + '.json';
msgPath = path.join(getPaths(channelName).messages, fileName);
return _context9.abrupt("return", msgPath);
return _context14.abrupt("return", msgPath);
case 3:
case "end":
return _context9.stop();
return _context14.stop();
}
}
}, _callee9, this);
}, _callee14, this);
}));

@@ -416,3 +587,3 @@ return _messagePath.apply(this, arguments);

function getAllMessages(_x14, _x15) {
function getAllMessages(_x15, _x16) {
return _getAllMessages.apply(this, arguments);

@@ -424,16 +595,16 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee10(channelName, paths) {
_regeneratorRuntime.mark(function _callee15(channelName, paths) {
var messagesPath, files;
return _regeneratorRuntime.wrap(function _callee10$(_context10) {
return _regeneratorRuntime.wrap(function _callee15$(_context15) {
while (1) {
switch (_context10.prev = _context10.next) {
switch (_context15.prev = _context15.next) {
case 0:
paths = paths || getPaths(channelName);
messagesPath = paths.messages;
_context10.next = 4;
_context15.next = 4;
return readdir(messagesPath);
case 4:
files = _context10.sent;
return _context10.abrupt("return", files.map(function (file) {
files = _context15.sent;
return _context15.abrupt("return", files.map(function (file) {
var fileName = file.split('.')[0];

@@ -451,6 +622,6 @@ var split = fileName.split('_');

case "end":
return _context10.stop();
return _context15.stop();
}
}
}, _callee10, this);
}, _callee15, this);
}));

@@ -476,3 +647,3 @@ return _getAllMessages.apply(this, arguments);

function cleanOldMessages(_x16, _x17) {
function cleanOldMessages(_x17, _x18) {
return _cleanOldMessages.apply(this, arguments);

@@ -484,10 +655,10 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee11(messageObjects, ttl) {
_regeneratorRuntime.mark(function _callee16(messageObjects, ttl) {
var olderThen;
return _regeneratorRuntime.wrap(function _callee11$(_context11) {
return _regeneratorRuntime.wrap(function _callee16$(_context16) {
while (1) {
switch (_context11.prev = _context11.next) {
switch (_context16.prev = _context16.next) {
case 0:
olderThen = Date.now() - ttl;
_context11.next = 3;
_context16.next = 3;
return Promise.all(messageObjects.filter(function (obj) {

@@ -503,6 +674,6 @@ return obj.time / 1000 < olderThen;

case "end":
return _context11.stop();
return _context16.stop();
}
}
}, _callee11, this);
}, _callee16, this);
}));

@@ -518,3 +689,3 @@ return _cleanOldMessages.apply(this, arguments);

function create(_x18) {
function create(_x19) {
return _create.apply(this, arguments);

@@ -526,3 +697,3 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee12(channelName) {
_regeneratorRuntime.mark(function _callee17(channelName) {
var options,

@@ -534,12 +705,12 @@ time,

state,
_ref5,
_ref8,
socketEE,
infoFilePath,
_args12 = arguments;
_args17 = arguments;
return _regeneratorRuntime.wrap(function _callee12$(_context12) {
return _regeneratorRuntime.wrap(function _callee17$(_context17) {
while (1) {
switch (_context12.prev = _context12.next) {
switch (_context17.prev = _context17.next) {
case 0:
options = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : {};
options = _args17.length > 1 && _args17[1] !== undefined ? _args17[1] : {};
options = fillOptionsWithDefaults(options);

@@ -571,13 +742,13 @@ time = microSeconds();

OTHER_INSTANCES[channelName].push(state);
_context12.next = 11;
_context17.next = 11;
return ensureFolderExistsPromise;
case 11:
_context12.next = 13;
_context17.next = 13;
return Promise.all([createSocketEventEmitter(channelName, uuid, paths), createSocketInfoFile(channelName, uuid, paths), refreshReaderClients(state)]);
case 13:
_ref5 = _context12.sent;
socketEE = _ref5[0];
infoFilePath = _ref5[1];
_ref8 = _context17.sent;
socketEE = _ref8[0];
infoFilePath = _ref8[1];
state.socketEE = socketEE;

@@ -601,10 +772,10 @@ state.infoFilePath = infoFilePath; // when new message comes in, we read it and emit it

});
return _context12.abrupt("return", state);
return _context17.abrupt("return", state);
case 20:
case "end":
return _context12.stop();
return _context17.stop();
}
}
}, _callee12, this);
}, _callee17, this);
}));

@@ -634,3 +805,3 @@ return _create.apply(this, arguments);

function handleMessagePing(_x19, _x20) {
function handleMessagePing(_x20, _x21) {
return _handleMessagePing.apply(this, arguments);

@@ -647,27 +818,27 @@ }

/*#__PURE__*/
_regeneratorRuntime.mark(function _callee13(state, msgObj) {
_regeneratorRuntime.mark(function _callee18(state, msgObj) {
var messages, useMessages;
return _regeneratorRuntime.wrap(function _callee13$(_context13) {
return _regeneratorRuntime.wrap(function _callee18$(_context18) {
while (1) {
switch (_context13.prev = _context13.next) {
switch (_context18.prev = _context18.next) {
case 0:
if (state.messagesCallback) {
_context13.next = 2;
_context18.next = 2;
break;
}
return _context13.abrupt("return");
return _context18.abrupt("return");
case 2:
if (msgObj) {
_context13.next = 8;
_context18.next = 8;
break;
}
_context13.next = 5;
_context18.next = 5;
return getAllMessages(state.channelName, state.paths);
case 5:
messages = _context13.sent;
_context13.next = 9;
messages = _context18.sent;
_context18.next = 9;
break;

@@ -688,10 +859,10 @@

if (!(!useMessages.length || !state.messagesCallback)) {
_context13.next = 12;
_context18.next = 12;
break;
}
return _context13.abrupt("return");
return _context18.abrupt("return");
case 12:
_context13.next = 14;
_context18.next = 14;
return Promise.all(useMessages.map(function (msgObj) {

@@ -715,6 +886,6 @@ return readMessage(msgObj).then(function (content) {

case "end":
return _context13.stop();
return _context18.stop();
}
}
}, _callee13, this);
}, _callee18, this);
}));

@@ -762,3 +933,3 @@ return _handleMessagePing.apply(this, arguments);

return function (_x21) {
return function (_x22) {
return _ref.apply(this, arguments);

@@ -816,3 +987,3 @@ };

return function (_x22) {
return function (_x23) {
return _ref2.apply(this, arguments);

@@ -953,5 +1124,12 @@ };

}
/**
* on node we use a relatively height averageResponseTime,
* because the file-io might be in use.
* Also it is more important that the leader-election is reliable,
* then to have a fast election.
*/
function averageResponseTime() {
return 50;
return 200;
}

@@ -964,2 +1142,3 @@

module.exports = {
TMP_FOLDER_BASE: TMP_FOLDER_BASE,
cleanPipeName: cleanPipeName,

@@ -972,2 +1151,3 @@ getPaths: getPaths,

createSocketInfoFile: createSocketInfoFile,
countChannelFolders: countChannelFolders,
createSocketEventEmitter: createSocketEventEmitter,

@@ -974,0 +1154,0 @@ openClientConnection: openClientConnection,

@@ -85,3 +85,2 @@ "use strict";

var ret = {
base: TMP_FOLDER_BASE,
channelBase: channelPathBase,

@@ -100,2 +99,32 @@ readers: folderPathReaders,

function ensureBaseFolderExists() {
return _ensureBaseFolderExists.apply(this, arguments);
}
function _ensureBaseFolderExists() {
_ensureBaseFolderExists = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee4() {
return _regenerator["default"].wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(TMP_FOLDER_BASE)["catch"](function () {
return null;
});
}
return _context4.abrupt("return", ENSURE_BASE_FOLDER_EXISTS_PROMISE);
case 2:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
return _ensureBaseFolderExists.apply(this, arguments);
}
function ensureFoldersExist(_x, _x2) {

@@ -113,20 +142,13 @@ return _ensureFoldersExist.apply(this, arguments);

/*#__PURE__*/
_regenerator["default"].mark(function _callee4(channelName, paths) {
return _regenerator["default"].wrap(function _callee4$(_context4) {
_regenerator["default"].mark(function _callee5(channelName, paths) {
return _regenerator["default"].wrap(function _callee5$(_context5) {
while (1) {
switch (_context4.prev = _context4.next) {
switch (_context5.prev = _context5.next) {
case 0:
paths = paths || getPaths(channelName);
_context5.next = 3;
return ensureBaseFolderExists();
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(paths.base)["catch"](function () {
return null;
});
}
_context4.next = 4;
return ENSURE_BASE_FOLDER_EXISTS_PROMISE;
case 4:
_context4.next = 6;
case 3:
_context5.next = 5;
return mkdir(paths.channelBase)["catch"](function () {

@@ -136,4 +158,4 @@ return null;

case 6:
_context4.next = 8;
case 5:
_context5.next = 7;
return Promise.all([mkdir(paths.readers)["catch"](function () {

@@ -145,8 +167,8 @@ return null;

case 8:
case 7:
case "end":
return _context4.stop();
return _context5.stop();
}
}
}, _callee4, this);
}, _callee5, this);
}));

@@ -163,13 +185,9 @@ return _ensureFoldersExist.apply(this, arguments);

/*#__PURE__*/
_regenerator["default"].mark(function _callee5() {
var paths, removePath;
return _regenerator["default"].wrap(function _callee5$(_context5) {
_regenerator["default"].mark(function _callee6() {
return _regenerator["default"].wrap(function _callee6$(_context6) {
while (1) {
switch (_context5.prev = _context5.next) {
switch (_context6.prev = _context6.next) {
case 0:
paths = getPaths('foobar');
removePath = paths.base;
if (!(!removePath || removePath === '' || removePath === '/')) {
_context5.next = 4;
if (!(!TMP_FOLDER_BASE || TMP_FOLDER_BASE === '' || TMP_FOLDER_BASE === '/')) {
_context6.next = 2;
break;

@@ -180,17 +198,17 @@ }

case 4:
case 2:
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
_context5.next = 7;
return removeDir(paths.base);
_context6.next = 5;
return removeDir(TMP_FOLDER_BASE);
case 7:
case 5:
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
return _context5.abrupt("return", true);
return _context6.abrupt("return", true);
case 9:
case 7:
case "end":
return _context5.stop();
return _context6.stop();
}
}
}, _callee5, this);
}, _callee6, this);
}));

@@ -227,2 +245,45 @@ return _clearNodeFolder.apply(this, arguments);

/**
* returns the amount of channel-folders in the tmp-directory
* @return {Promise<number>}
*/
function countChannelFolders() {
return _countChannelFolders.apply(this, arguments);
}
function _countChannelFolders() {
_countChannelFolders = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee7() {
var folders;
return _regenerator["default"].wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return ensureBaseFolderExists();
case 2:
_context7.next = 4;
return readdir(TMP_FOLDER_BASE);
case 4:
folders = _context7.sent;
return _context7.abrupt("return", folders.length);
case 6:
case "end":
return _context7.stop();
}
}
}, _callee7, this);
}));
return _countChannelFolders.apply(this, arguments);
}
function connectionError(_x3) {
return _connectionError.apply(this, arguments);
}
/**
* creates the socket-file and subscribes to it

@@ -233,3 +294,46 @@ * @return {{emitter: EventEmitter, server: any}}

function createSocketEventEmitter(_x3, _x4, _x5) {
function _connectionError() {
_connectionError = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee8(originalError) {
var count, addObj, text, newError;
return _regenerator["default"].wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
_context8.next = 2;
return countChannelFolders();
case 2:
count = _context8.sent;
if (!(count < 30)) {
_context8.next = 5;
break;
}
return _context8.abrupt("return", originalError);
case 5:
addObj = {};
Object.entries(originalError).forEach(function (_ref5) {
var k = _ref5[0],
v = _ref5[1];
return addObj[k] = v;
});
text = 'BroadcastChannel.create(): error: ' + 'This might happen if you have created to many channels, ' + 'like when you use BroadcastChannel in unit-tests.' + 'Try using BroadcastChannel.clearNodeFolder() to clear the tmp-folder before each test.' + 'See https://github.com/pubkey/broadcast-channel#clear-tmp-folder';
newError = new Error(text + ': ' + JSON.stringify(addObj, null, 2));
return _context8.abrupt("return", newError);
case 10:
case "end":
return _context8.stop();
}
}
}, _callee8, this);
}));
return _connectionError.apply(this, arguments);
}
function createSocketEventEmitter(_x4, _x5, _x6) {
return _createSocketEventEmitter.apply(this, arguments);

@@ -241,7 +345,7 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee6(channelName, readerUuid, paths) {
_regenerator["default"].mark(function _callee11(channelName, readerUuid, paths) {
var pathToSocket, emitter, server;
return _regenerator["default"].wrap(function _callee6$(_context6) {
return _regenerator["default"].wrap(function _callee11$(_context11) {
while (1) {
switch (_context6.prev = _context6.next) {
switch (_context11.prev = _context11.next) {
case 0:

@@ -256,11 +360,78 @@ pathToSocket = socketPath(channelName, readerUuid, paths);

});
_context6.next = 5;
_context11.next = 5;
return new Promise(function (resolve, reject) {
server.listen(pathToSocket, function (err, res) {
if (err) reject(err);else resolve(res);
});
server.on('error',
/*#__PURE__*/
function () {
var _ref6 = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee9(err) {
var useErr;
return _regenerator["default"].wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return connectionError(err);
case 2:
useErr = _context9.sent;
reject(useErr);
case 4:
case "end":
return _context9.stop();
}
}
}, _callee9, this);
}));
return function (_x24) {
return _ref6.apply(this, arguments);
};
}());
server.listen(pathToSocket,
/*#__PURE__*/
function () {
var _ref7 = (0, _asyncToGenerator2["default"])(
/*#__PURE__*/
_regenerator["default"].mark(function _callee10(err, res) {
var useErr;
return _regenerator["default"].wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
if (!err) {
_context10.next = 7;
break;
}
_context10.next = 3;
return connectionError(err);
case 3:
useErr = _context10.sent;
reject(useErr);
_context10.next = 8;
break;
case 7:
resolve(res);
case 8:
case "end":
return _context10.stop();
}
}
}, _callee10, this);
}));
return function (_x25, _x26) {
return _ref7.apply(this, arguments);
};
}());
});
case 5:
return _context6.abrupt("return", {
return _context11.abrupt("return", {
path: pathToSocket,

@@ -273,6 +444,6 @@ emitter: emitter,

case "end":
return _context6.stop();
return _context11.stop();
}
}
}, _callee6, this);
}, _callee11, this);
}));

@@ -282,3 +453,3 @@ return _createSocketEventEmitter.apply(this, arguments);

function openClientConnection(_x6, _x7) {
function openClientConnection(_x7, _x8) {
return _openClientConnection.apply(this, arguments);

@@ -296,11 +467,11 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee7(channelName, readerUuid) {
_regenerator["default"].mark(function _callee12(channelName, readerUuid) {
var pathToSocket, client;
return _regenerator["default"].wrap(function _callee7$(_context7) {
return _regenerator["default"].wrap(function _callee12$(_context12) {
while (1) {
switch (_context7.prev = _context7.next) {
switch (_context12.prev = _context12.next) {
case 0:
pathToSocket = socketPath(channelName, readerUuid);
client = new net.Socket();
_context7.next = 4;
_context12.next = 4;
return new Promise(function (res) {

@@ -311,10 +482,10 @@ client.connect(pathToSocket, res);

case 4:
return _context7.abrupt("return", client);
return _context12.abrupt("return", client);
case 5:
case "end":
return _context7.stop();
return _context12.stop();
}
}
}, _callee7, this);
}, _callee12, this);
}));

@@ -350,3 +521,3 @@ return _openClientConnection.apply(this, arguments);

function getReadersUuids(_x8, _x9) {
function getReadersUuids(_x9, _x10) {
return _getReadersUuids.apply(this, arguments);

@@ -358,16 +529,16 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee8(channelName, paths) {
_regenerator["default"].mark(function _callee13(channelName, paths) {
var readersPath, files;
return _regenerator["default"].wrap(function _callee8$(_context8) {
return _regenerator["default"].wrap(function _callee13$(_context13) {
while (1) {
switch (_context8.prev = _context8.next) {
switch (_context13.prev = _context13.next) {
case 0:
paths = paths || getPaths(channelName);
readersPath = paths.readers;
_context8.next = 4;
_context13.next = 4;
return readdir(readersPath);
case 4:
files = _context8.sent;
return _context8.abrupt("return", files.map(function (file) {
files = _context13.sent;
return _context13.abrupt("return", files.map(function (file) {
return file.split('.');

@@ -383,6 +554,6 @@ }).filter(function (split) {

case "end":
return _context8.stop();
return _context13.stop();
}
}
}, _callee8, this);
}, _callee13, this);
}));

@@ -392,3 +563,3 @@ return _getReadersUuids.apply(this, arguments);

function messagePath(_x10, _x11, _x12, _x13) {
function messagePath(_x11, _x12, _x13, _x14) {
return _messagePath.apply(this, arguments);

@@ -400,18 +571,18 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee9(channelName, time, token, writerUuid) {
_regenerator["default"].mark(function _callee14(channelName, time, token, writerUuid) {
var fileName, msgPath;
return _regenerator["default"].wrap(function _callee9$(_context9) {
return _regenerator["default"].wrap(function _callee14$(_context14) {
while (1) {
switch (_context9.prev = _context9.next) {
switch (_context14.prev = _context14.next) {
case 0:
fileName = time + '_' + writerUuid + '_' + token + '.json';
msgPath = path.join(getPaths(channelName).messages, fileName);
return _context9.abrupt("return", msgPath);
return _context14.abrupt("return", msgPath);
case 3:
case "end":
return _context9.stop();
return _context14.stop();
}
}
}, _callee9, this);
}, _callee14, this);
}));

@@ -421,3 +592,3 @@ return _messagePath.apply(this, arguments);

function getAllMessages(_x14, _x15) {
function getAllMessages(_x15, _x16) {
return _getAllMessages.apply(this, arguments);

@@ -429,16 +600,16 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee10(channelName, paths) {
_regenerator["default"].mark(function _callee15(channelName, paths) {
var messagesPath, files;
return _regenerator["default"].wrap(function _callee10$(_context10) {
return _regenerator["default"].wrap(function _callee15$(_context15) {
while (1) {
switch (_context10.prev = _context10.next) {
switch (_context15.prev = _context15.next) {
case 0:
paths = paths || getPaths(channelName);
messagesPath = paths.messages;
_context10.next = 4;
_context15.next = 4;
return readdir(messagesPath);
case 4:
files = _context10.sent;
return _context10.abrupt("return", files.map(function (file) {
files = _context15.sent;
return _context15.abrupt("return", files.map(function (file) {
var fileName = file.split('.')[0];

@@ -456,6 +627,6 @@ var split = fileName.split('_');

case "end":
return _context10.stop();
return _context15.stop();
}
}
}, _callee10, this);
}, _callee15, this);
}));

@@ -481,3 +652,3 @@ return _getAllMessages.apply(this, arguments);

function cleanOldMessages(_x16, _x17) {
function cleanOldMessages(_x17, _x18) {
return _cleanOldMessages.apply(this, arguments);

@@ -489,10 +660,10 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee11(messageObjects, ttl) {
_regenerator["default"].mark(function _callee16(messageObjects, ttl) {
var olderThen;
return _regenerator["default"].wrap(function _callee11$(_context11) {
return _regenerator["default"].wrap(function _callee16$(_context16) {
while (1) {
switch (_context11.prev = _context11.next) {
switch (_context16.prev = _context16.next) {
case 0:
olderThen = Date.now() - ttl;
_context11.next = 3;
_context16.next = 3;
return Promise.all(messageObjects.filter(function (obj) {

@@ -508,6 +679,6 @@ return obj.time / 1000 < olderThen;

case "end":
return _context11.stop();
return _context16.stop();
}
}
}, _callee11, this);
}, _callee16, this);
}));

@@ -523,3 +694,3 @@ return _cleanOldMessages.apply(this, arguments);

function create(_x18) {
function create(_x19) {
return _create.apply(this, arguments);

@@ -531,3 +702,3 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee12(channelName) {
_regenerator["default"].mark(function _callee17(channelName) {
var options,

@@ -539,12 +710,12 @@ time,

state,
_ref5,
_ref8,
socketEE,
infoFilePath,
_args12 = arguments;
_args17 = arguments;
return _regenerator["default"].wrap(function _callee12$(_context12) {
return _regenerator["default"].wrap(function _callee17$(_context17) {
while (1) {
switch (_context12.prev = _context12.next) {
switch (_context17.prev = _context17.next) {
case 0:
options = _args12.length > 1 && _args12[1] !== undefined ? _args12[1] : {};
options = _args17.length > 1 && _args17[1] !== undefined ? _args17[1] : {};
options = fillOptionsWithDefaults(options);

@@ -576,13 +747,13 @@ time = microSeconds();

OTHER_INSTANCES[channelName].push(state);
_context12.next = 11;
_context17.next = 11;
return ensureFolderExistsPromise;
case 11:
_context12.next = 13;
_context17.next = 13;
return Promise.all([createSocketEventEmitter(channelName, uuid, paths), createSocketInfoFile(channelName, uuid, paths), refreshReaderClients(state)]);
case 13:
_ref5 = _context12.sent;
socketEE = _ref5[0];
infoFilePath = _ref5[1];
_ref8 = _context17.sent;
socketEE = _ref8[0];
infoFilePath = _ref8[1];
state.socketEE = socketEE;

@@ -606,10 +777,10 @@ state.infoFilePath = infoFilePath; // when new message comes in, we read it and emit it

});
return _context12.abrupt("return", state);
return _context17.abrupt("return", state);
case 20:
case "end":
return _context12.stop();
return _context17.stop();
}
}
}, _callee12, this);
}, _callee17, this);
}));

@@ -639,3 +810,3 @@ return _create.apply(this, arguments);

function handleMessagePing(_x19, _x20) {
function handleMessagePing(_x20, _x21) {
return _handleMessagePing.apply(this, arguments);

@@ -652,27 +823,27 @@ }

/*#__PURE__*/
_regenerator["default"].mark(function _callee13(state, msgObj) {
_regenerator["default"].mark(function _callee18(state, msgObj) {
var messages, useMessages;
return _regenerator["default"].wrap(function _callee13$(_context13) {
return _regenerator["default"].wrap(function _callee18$(_context18) {
while (1) {
switch (_context13.prev = _context13.next) {
switch (_context18.prev = _context18.next) {
case 0:
if (state.messagesCallback) {
_context13.next = 2;
_context18.next = 2;
break;
}
return _context13.abrupt("return");
return _context18.abrupt("return");
case 2:
if (msgObj) {
_context13.next = 8;
_context18.next = 8;
break;
}
_context13.next = 5;
_context18.next = 5;
return getAllMessages(state.channelName, state.paths);
case 5:
messages = _context13.sent;
_context13.next = 9;
messages = _context18.sent;
_context18.next = 9;
break;

@@ -693,10 +864,10 @@

if (!(!useMessages.length || !state.messagesCallback)) {
_context13.next = 12;
_context18.next = 12;
break;
}
return _context13.abrupt("return");
return _context18.abrupt("return");
case 12:
_context13.next = 14;
_context18.next = 14;
return Promise.all(useMessages.map(function (msgObj) {

@@ -720,6 +891,6 @@ return readMessage(msgObj).then(function (content) {

case "end":
return _context13.stop();
return _context18.stop();
}
}
}, _callee13, this);
}, _callee18, this);
}));

@@ -767,3 +938,3 @@ return _handleMessagePing.apply(this, arguments);

return function (_x21) {
return function (_x22) {
return _ref.apply(this, arguments);

@@ -821,3 +992,3 @@ };

return function (_x22) {
return function (_x23) {
return _ref2.apply(this, arguments);

@@ -958,5 +1129,12 @@ };

}
/**
* on node we use a relatively height averageResponseTime,
* because the file-io might be in use.
* Also it is more important that the leader-election is reliable,
* then to have a fast election.
*/
function averageResponseTime() {
return 50;
return 200;
}

@@ -969,2 +1147,3 @@

module.exports = {
TMP_FOLDER_BASE: TMP_FOLDER_BASE,
cleanPipeName: cleanPipeName,

@@ -977,2 +1156,3 @@ getPaths: getPaths,

createSocketInfoFile: createSocketInfoFile,
countChannelFolders: countChannelFolders,
createSocketEventEmitter: createSocketEventEmitter,

@@ -979,0 +1159,0 @@ openClientConnection: openClientConnection,

{
"name": "broadcast-channel",
"version": "2.1.1",
"version": "2.1.2",
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js",

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

"build:leader-iframe": "browserify test_tmp/scripts/leader-iframe.js > docs/leader-iframe.js",
"build": "npm run clear && concurrently \"npm run build:es6\" \"npm run build:es5\" \"npm run build:test\" && concurrently \"npm run build:browser\" \"npm run build:worker\" \"npm run build:iframe\" \"npm run build:leader-iframe\"",
"build:leader-crown": "browserify test_tmp/scripts/leader-crown.js > docs/leader-crown.js",
"build": "npm run clear && concurrently \"npm run build:es6\" \"npm run build:es5\" \"npm run build:test\" && concurrently \"npm run build:browser\" \"npm run build:worker\" \"npm run build:iframe\" \"npm run build:leader-iframe\" \"npm run build:leader-crown\"",
"build:min": "uglifyjs --compress --mangle --output dist/lib/browserify.min.js -- dist/lib/browserify.index.js",

@@ -63,8 +64,7 @@ "docs:only": "http-server ./docs --silent",

"@babel/runtime": "7.0.0",
"custom-idle-queue": "2.0.1",
"detect-node": "2.0.4",
"rimraf": "2.6.2",
"js-sha3": "0.8.0",
"microseconds": "0.1.0",
"nano-time": "1.0.0",
"rimraf": "2.6.2",
"unload": "2.0.1"

@@ -109,3 +109,3 @@ },

"mocha": "5.2.0",
"node": "10.9.0",
"node": "10.10.0",
"pre-commit": "1.2.2",

@@ -115,3 +115,3 @@ "random-int": "1.0.0",

"testcafe": "0.22.0",
"testcafe-hammerhead": "14.2.6",
"testcafe-hammerhead": "14.2.7",
"ts-node": "7.0.1",

@@ -118,0 +118,0 @@ "typescript": "3.0.3",

@@ -130,2 +130,6 @@

In this example the leader is marked with the crown ♛:
![leader-election.gif](docs/files/leader-election.gif)
Create a channel and an elector.

@@ -132,0 +136,0 @@

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

const ret = {
base: TMP_FOLDER_BASE,
channelBase: channelPathBase,

@@ -94,9 +93,13 @@ readers: folderPathReaders,

let ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
async function ensureBaseFolderExists() {
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(TMP_FOLDER_BASE).catch(() => null);
}
return ENSURE_BASE_FOLDER_EXISTS_PROMISE;
}
async function ensureFoldersExist(channelName, paths) {
paths = paths || getPaths(channelName);
if (!ENSURE_BASE_FOLDER_EXISTS_PROMISE) {
ENSURE_BASE_FOLDER_EXISTS_PROMISE = mkdir(paths.base).catch(() => null);
}
await ENSURE_BASE_FOLDER_EXISTS_PROMISE;
await ensureBaseFolderExists();

@@ -115,9 +118,7 @@ await mkdir(paths.channelBase).catch(() => null);

async function clearNodeFolder() {
const paths = getPaths('foobar');
const removePath = paths.base;
if (!removePath || removePath === '' || removePath === '/') {
if (!TMP_FOLDER_BASE || TMP_FOLDER_BASE === '' || TMP_FOLDER_BASE === '/') {
throw new Error('BroadcastChannel.clearNodeFolder(): path is wrong');
}
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;
await removeDir(paths.base);
await removeDir(TMP_FOLDER_BASE);
ENSURE_BASE_FOLDER_EXISTS_PROMISE = null;

@@ -163,2 +164,31 @@ return true;

/**
* returns the amount of channel-folders in the tmp-directory
* @return {Promise<number>}
*/
async function countChannelFolders() {
await ensureBaseFolderExists();
const folders = await readdir(TMP_FOLDER_BASE);
return folders.length;
}
async function connectionError(originalError) {
const count = await countChannelFolders();
// we only show the augmented message if there are more then 30 channels
// because we then assume that BroadcastChannel is used in unit-tests
if (count < 30) return originalError;
const addObj = {};
Object.entries(originalError).forEach(([k, v]) => addObj[k] = v);
const text = 'BroadcastChannel.create(): error: ' +
'This might happen if you have created to many channels, ' +
'like when you use BroadcastChannel in unit-tests.' +
'Try using BroadcastChannel.clearNodeFolder() to clear the tmp-folder before each test.' +
'See https://github.com/pubkey/broadcast-channel#clear-tmp-folder';
const newError = new Error(text + ': ' + JSON.stringify(addObj, null, 2));
return newError;
}
/**
* creates the socket-file and subscribes to it

@@ -180,6 +210,13 @@ * @return {{emitter: EventEmitter, server: any}}

await new Promise((resolve, reject) => {
server.listen(pathToSocket, (err, res) => {
if (err) reject(err);
else resolve(res);
server.on('error', async (err) => {
const useErr = await connectionError(err);
reject(useErr);
});
server.listen(pathToSocket, async (err, res) => {
if (err) {
const useErr = await connectionError(err);
reject(useErr);
} else resolve(res);
});
});

@@ -589,4 +626,10 @@

/**
* on node we use a relatively height averageResponseTime,
* because the file-io might be in use.
* Also it is more important that the leader-election is reliable,
* then to have a fast election.
*/
function averageResponseTime() {
return 50;
return 200;
}

@@ -599,2 +642,3 @@

module.exports = {
TMP_FOLDER_BASE,
cleanPipeName,

@@ -607,2 +651,3 @@ getPaths,

createSocketInfoFile,
countChannelFolders,
createSocketEventEmitter,

@@ -609,0 +654,0 @@ openClientConnection,

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc