broadcast-channel
Advanced tools
Comparing version 1.3.0 to 2.0.0
@@ -35,2 +35,28 @@ import { isPromise } from './util.js'; | ||
// STATICS | ||
/** | ||
* used to identify if someone overwrites | ||
* window.BroadcastChannel with this | ||
* See methods/native.js | ||
*/ | ||
BroadcastChannel._pubkey = true; | ||
/** | ||
* clears the tmp-folder if is node | ||
* @return {Promise<boolean>} true if has run, false if not node | ||
*/ | ||
BroadcastChannel.clearNodeFolder = function (options) { | ||
options = fillOptionsWithDefaults(options); | ||
var method = chooseMethod(options); | ||
if (method.type === 'node') { | ||
return method.clearNodeFolder().then(function () { | ||
return true; | ||
}); | ||
} else { | ||
return Promise.resolve(false); | ||
} | ||
}; | ||
// PROTOTYPE | ||
BroadcastChannel.prototype = { | ||
@@ -141,2 +167,3 @@ _post: function _post(type, msg) { | ||
} | ||
function _removeListenerObject(channel, type, obj) { | ||
@@ -143,0 +170,0 @@ channel._addEventListeners[type] = channel._addEventListeners[type].filter(function (o) { |
@@ -46,3 +46,8 @@ var isNode = require('detect-node'); | ||
if (typeof BroadcastChannel === 'function') return true; | ||
if (typeof BroadcastChannel === 'function') { | ||
if (BroadcastChannel._pubkey) { | ||
throw new Error('BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill'); | ||
} | ||
return true; | ||
} else return false; | ||
} | ||
@@ -49,0 +54,0 @@ |
@@ -24,22 +24,10 @@ import _regeneratorRuntime from 'babel-runtime/regenerator'; | ||
case 5: | ||
_context.t0 = Promise; | ||
_context.next = 8; | ||
return mkdir(paths.readers)['catch'](function () { | ||
_context.next = 7; | ||
return Promise.all([mkdir(paths.readers)['catch'](function () { | ||
return null; | ||
}); | ||
case 8: | ||
_context.t1 = _context.sent; | ||
_context.next = 11; | ||
return mkdir(paths.messages)['catch'](function () { | ||
}), mkdir(paths.messages)['catch'](function () { | ||
return null; | ||
}); | ||
})]); | ||
case 11: | ||
_context.t2 = _context.sent; | ||
_context.t3 = [_context.t1, _context.t2]; | ||
_context.next = 15; | ||
return _context.t0.all.call(_context.t0, _context.t3); | ||
case 15: | ||
case 7: | ||
case 'end': | ||
@@ -58,2 +46,45 @@ return _context.stop(); | ||
/** | ||
* removes the tmp-folder | ||
* @return {Promise<true>} | ||
*/ | ||
var clearNodeFolder = function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() { | ||
var paths, removePath; | ||
return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
paths = getPaths('foobar'); | ||
removePath = paths.base; | ||
if (!(!removePath || removePath === '' || removePath === '/')) { | ||
_context2.next = 4; | ||
break; | ||
} | ||
throw new Error('BroadcastChannel.clearNodeFolder(): path is wrong'); | ||
case 4: | ||
_context2.next = 6; | ||
return removeDir(paths.base); | ||
case 6: | ||
return _context2.abrupt('return', true); | ||
case 7: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
return function clearNodeFolder() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
/** | ||
* creates the socket-file and subscribes to it | ||
@@ -63,7 +94,7 @@ * @return {{emitter: EventEmitter, server: any}} | ||
var createSocketEventEmitter = function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(channelName, readerUuid) { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(channelName, readerUuid) { | ||
var pathToSocket, emitter, server; | ||
return _regeneratorRuntime.wrap(function _callee2$(_context2) { | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
@@ -74,3 +105,2 @@ pathToSocket = socketPath(channelName, readerUuid); | ||
stream.on('end', function () {}); | ||
stream.on('data', function (msg) { | ||
@@ -80,6 +110,7 @@ emitter.emit('data', msg.toString()); | ||
}); | ||
_context2.next = 5; | ||
return new Promise(function (res) { | ||
server.listen(pathToSocket, function () { | ||
res(); | ||
_context3.next = 5; | ||
return new Promise(function (resolve, reject) { | ||
server.listen(pathToSocket, function (err, res) { | ||
if (err) reject(err);else resolve(res); | ||
}); | ||
@@ -91,3 +122,3 @@ }); | ||
return _context2.abrupt('return', { | ||
return _context3.abrupt('return', { | ||
path: pathToSocket, | ||
@@ -100,10 +131,10 @@ emitter: emitter, | ||
case 'end': | ||
return _context2.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
}, _callee3, this); | ||
})); | ||
return function createSocketEventEmitter(_x2, _x3) { | ||
return _ref2.apply(this, arguments); | ||
return _ref3.apply(this, arguments); | ||
}; | ||
@@ -113,11 +144,11 @@ }(); | ||
var openClientConnection = function () { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(channelName, readerUuid) { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(channelName, readerUuid) { | ||
var pathToSocket, client; | ||
return _regeneratorRuntime.wrap(function _callee3$(_context3) { | ||
return _regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
pathToSocket = socketPath(channelName, readerUuid); | ||
client = new net.Socket(); | ||
_context3.next = 4; | ||
_context4.next = 4; | ||
return new Promise(function (res) { | ||
@@ -128,14 +159,14 @@ client.connect(pathToSocket, res); | ||
case 4: | ||
return _context3.abrupt('return', client); | ||
return _context4.abrupt('return', client); | ||
case 5: | ||
case 'end': | ||
return _context3.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
}, _callee4, this); | ||
})); | ||
return function openClientConnection(_x4, _x5) { | ||
return _ref3.apply(this, arguments); | ||
return _ref4.apply(this, arguments); | ||
}; | ||
@@ -151,7 +182,7 @@ }(); | ||
var writeMessage = function () { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(channelName, readerUuid, messageJson) { | ||
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(channelName, readerUuid, messageJson) { | ||
var time, writeObject, token, fileName, msgPath; | ||
return _regeneratorRuntime.wrap(function _callee4$(_context4) { | ||
return _regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
@@ -167,7 +198,7 @@ time = microSeconds(); | ||
msgPath = path.join(getPaths(channelName).messages, fileName); | ||
_context4.next = 7; | ||
_context5.next = 7; | ||
return writeFile(msgPath, JSON.stringify(writeObject)); | ||
case 7: | ||
return _context4.abrupt('return', { | ||
return _context5.abrupt('return', { | ||
time: time, | ||
@@ -181,10 +212,10 @@ uuid: readerUuid, | ||
case 'end': | ||
return _context4.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
}, _callee5, this); | ||
})); | ||
return function writeMessage(_x6, _x7, _x8) { | ||
return _ref4.apply(this, arguments); | ||
return _ref5.apply(this, arguments); | ||
}; | ||
@@ -200,15 +231,15 @@ }(); | ||
var getReadersUuids = function () { | ||
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(channelName) { | ||
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(channelName) { | ||
var readersPath, files; | ||
return _regeneratorRuntime.wrap(function _callee5$(_context5) { | ||
return _regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
readersPath = getPaths(channelName).readers; | ||
_context5.next = 3; | ||
_context6.next = 3; | ||
return readdir(readersPath); | ||
case 3: | ||
files = _context5.sent; | ||
return _context5.abrupt('return', files.map(function (file) { | ||
files = _context6.sent; | ||
return _context6.abrupt('return', files.map(function (file) { | ||
return file.split('.'); | ||
@@ -224,10 +255,10 @@ }).filter(function (split) { | ||
case 'end': | ||
return _context5.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee6, this); | ||
})); | ||
return function getReadersUuids(_x9) { | ||
return _ref5.apply(this, arguments); | ||
return _ref6.apply(this, arguments); | ||
}; | ||
@@ -237,22 +268,22 @@ }(); | ||
var messagePath = function () { | ||
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(channelName, time, token, writerUuid) { | ||
var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(channelName, time, token, writerUuid) { | ||
var fileName, msgPath; | ||
return _regeneratorRuntime.wrap(function _callee6$(_context6) { | ||
return _regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
fileName = time + '_' + writerUuid + '_' + token + '.json'; | ||
msgPath = path.join(getPaths(channelName).messages, fileName); | ||
return _context6.abrupt('return', msgPath); | ||
return _context7.abrupt('return', msgPath); | ||
case 3: | ||
case 'end': | ||
return _context6.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee7, this); | ||
})); | ||
return function messagePath(_x10, _x11, _x12, _x13) { | ||
return _ref6.apply(this, arguments); | ||
return _ref7.apply(this, arguments); | ||
}; | ||
@@ -262,15 +293,15 @@ }(); | ||
var getAllMessages = function () { | ||
var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(channelName) { | ||
var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(channelName) { | ||
var messagesPath, files; | ||
return _regeneratorRuntime.wrap(function _callee7$(_context7) { | ||
return _regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
messagesPath = getPaths(channelName).messages; | ||
_context7.next = 3; | ||
_context8.next = 3; | ||
return readdir(messagesPath); | ||
case 3: | ||
files = _context7.sent; | ||
return _context7.abrupt('return', files.map(function (file) { | ||
files = _context8.sent; | ||
return _context8.abrupt('return', files.map(function (file) { | ||
var fileName = file.split('.')[0]; | ||
@@ -289,10 +320,10 @@ var split = fileName.split('_'); | ||
case 'end': | ||
return _context7.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
}, _callee8, this); | ||
})); | ||
return function getAllMessages(_x14) { | ||
return _ref7.apply(this, arguments); | ||
return _ref8.apply(this, arguments); | ||
}; | ||
@@ -302,10 +333,10 @@ }(); | ||
var cleanOldMessages = function () { | ||
var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(messageObjects, ttl) { | ||
var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(messageObjects, ttl) { | ||
var olderThen; | ||
return _regeneratorRuntime.wrap(function _callee8$(_context8) { | ||
return _regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
olderThen = Date.now() - ttl; | ||
_context8.next = 3; | ||
_context9.next = 3; | ||
return Promise.all(messageObjects.filter(function (obj) { | ||
@@ -321,10 +352,10 @@ return obj.time / 1000 < olderThen; | ||
case 'end': | ||
return _context8.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee9, this); | ||
})); | ||
return function cleanOldMessages(_x15, _x16) { | ||
return _ref8.apply(this, arguments); | ||
return _ref9.apply(this, arguments); | ||
}; | ||
@@ -334,14 +365,14 @@ }(); | ||
var create = function () { | ||
var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(channelName) { | ||
var _ref10 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(channelName) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var time, uuid, state, _ref10, socketEE, infoFilePath; | ||
var time, uuid, state, _ref11, socketEE, infoFilePath; | ||
return _regeneratorRuntime.wrap(function _callee9$(_context9) { | ||
return _regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
options = fillOptionsWithDefaults(options); | ||
time = microSeconds(); | ||
_context9.next = 4; | ||
_context10.next = 4; | ||
return ensureFoldersExist(channelName); | ||
@@ -374,9 +405,9 @@ | ||
_context9.next = 10; | ||
_context10.next = 10; | ||
return Promise.all([createSocketEventEmitter(channelName, uuid), createSocketInfoFile(channelName, uuid), refreshReaderClients(state)]); | ||
case 10: | ||
_ref10 = _context9.sent; | ||
socketEE = _ref10[0]; | ||
infoFilePath = _ref10[1]; | ||
_ref11 = _context10.sent; | ||
socketEE = _ref11[0]; | ||
infoFilePath = _ref11[1]; | ||
@@ -404,14 +435,14 @@ state.socketEE = socketEE; | ||
return _context9.abrupt('return', state); | ||
return _context10.abrupt('return', state); | ||
case 17: | ||
case 'end': | ||
return _context9.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
}, _callee10, this); | ||
})); | ||
return function create(_x17) { | ||
return _ref9.apply(this, arguments); | ||
return _ref10.apply(this, arguments); | ||
}; | ||
@@ -425,14 +456,14 @@ }(); | ||
var handleMessagePing = function () { | ||
var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(state, msgObj) { | ||
var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(state, msgObj) { | ||
var messages, useMessages; | ||
return _regeneratorRuntime.wrap(function _callee10$(_context10) { | ||
return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context10.next = 2; | ||
_context11.next = 2; | ||
break; | ||
} | ||
return _context10.abrupt('return'); | ||
return _context11.abrupt('return'); | ||
@@ -443,12 +474,12 @@ case 2: | ||
if (msgObj) { | ||
_context10.next = 9; | ||
_context11.next = 9; | ||
break; | ||
} | ||
_context10.next = 6; | ||
_context11.next = 6; | ||
return getAllMessages(state.channelName); | ||
case 6: | ||
messages = _context10.sent; | ||
_context10.next = 10; | ||
messages = _context11.sent; | ||
_context11.next = 10; | ||
break; | ||
@@ -465,3 +496,3 @@ | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
}); // sort by time | ||
@@ -472,10 +503,10 @@ | ||
if (!(!useMessages.length || !state.messagesCallback)) { | ||
_context10.next = 13; | ||
_context11.next = 13; | ||
break; | ||
} | ||
return _context10.abrupt('return'); | ||
return _context11.abrupt('return'); | ||
case 13: | ||
_context10.next = 15; | ||
_context11.next = 15; | ||
return Promise.all(useMessages.map(function (msgObj) { | ||
@@ -500,10 +531,10 @@ return readMessage(msgObj).then(function (content) { | ||
case 'end': | ||
return _context10.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee10, this); | ||
}, _callee11, this); | ||
})); | ||
return function handleMessagePing(_x19, _x20) { | ||
return _ref11.apply(this, arguments); | ||
return _ref12.apply(this, arguments); | ||
}; | ||
@@ -513,15 +544,15 @@ }(); | ||
var refreshReaderClients = function () { | ||
var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(channelState) { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14(channelState) { | ||
var _this = this; | ||
var otherReaders; | ||
return _regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
return _regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context13.next = 2; | ||
_context14.next = 2; | ||
return getReadersUuids(channelState.channelName); | ||
case 2: | ||
otherReaders = _context13.sent; | ||
otherReaders = _context14.sent; | ||
@@ -533,18 +564,18 @@ | ||
}).forEach(function () { | ||
var _ref13 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(readerUuid) { | ||
return _regeneratorRuntime.wrap(function _callee11$(_context11) { | ||
var _ref14 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(readerUuid) { | ||
return _regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context11.prev = 0; | ||
_context11.next = 3; | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context11.next = 7; | ||
_context12.next = 7; | ||
break; | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11['catch'](0); | ||
_context12.prev = 5; | ||
_context12.t0 = _context12['catch'](0); | ||
@@ -556,14 +587,14 @@ case 7: | ||
case 'end': | ||
return _context11.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee11, _this, [[0, 5]]); | ||
}, _callee12, _this, [[0, 5]]); | ||
})); | ||
return function (_x22) { | ||
return _ref13.apply(this, arguments); | ||
return _ref14.apply(this, arguments); | ||
}; | ||
}()); | ||
_context13.next = 6; | ||
_context14.next = 6; | ||
return Promise.all(otherReaders.filter(function (readerUuid) { | ||
@@ -576,42 +607,42 @@ return readerUuid !== channelState.uuid; | ||
.map(function () { | ||
var _ref14 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(readerUuid) { | ||
var _ref15 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(readerUuid) { | ||
var client; | ||
return _regeneratorRuntime.wrap(function _callee12$(_context12) { | ||
return _regeneratorRuntime.wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
_context12.prev = 0; | ||
_context13.prev = 0; | ||
if (!channelState.closed) { | ||
_context12.next = 3; | ||
_context13.next = 3; | ||
break; | ||
} | ||
return _context12.abrupt('return'); | ||
return _context13.abrupt('return'); | ||
case 3: | ||
_context12.next = 5; | ||
_context13.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 5: | ||
client = _context12.sent; | ||
client = _context13.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context12.next = 11; | ||
_context13.next = 11; | ||
break; | ||
case 9: | ||
_context12.prev = 9; | ||
_context12.t0 = _context12['catch'](0); | ||
_context13.prev = 9; | ||
_context13.t0 = _context13['catch'](0); | ||
case 11: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee12, _this, [[0, 9]]); | ||
}, _callee13, _this, [[0, 9]]); | ||
})); | ||
return function (_x23) { | ||
return _ref14.apply(this, arguments); | ||
return _ref15.apply(this, arguments); | ||
}; | ||
@@ -625,10 +656,10 @@ }() | ||
case 'end': | ||
return _context13.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
}, _callee14, this); | ||
})); | ||
return function refreshReaderClients(_x21) { | ||
return _ref12.apply(this, arguments); | ||
return _ref13.apply(this, arguments); | ||
}; | ||
@@ -649,3 +680,3 @@ }(); | ||
var micro = require('nano-time'); | ||
var rimraf = require('rimraf'); | ||
var sha3_224 = require('js-sha3').sha3_224; | ||
@@ -680,17 +711,26 @@ var isNode = require('detect-node'); | ||
var readdir = util.promisify(fs.readdir); | ||
var removeDir = util.promisify(rimraf); | ||
var TMP_FOLDER_NAME = 'pubkey.broadcast-channel'; | ||
var OTHER_INSTANCES = {}; | ||
var TMP_FOLDER_NAME = 'pubkey.bc'; | ||
var TMP_FOLDER_BASE = path.join(os.tmpdir(), TMP_FOLDER_NAME); | ||
var getPathsCache = new Map(); | ||
var getPathsCache = new Map(); | ||
function getPaths(channelName) { | ||
if (!getPathsCache.has(channelName)) { | ||
var folderPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME); | ||
var channelPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME, sha3_224(channelName) // use hash incase of strange characters | ||
); | ||
var folderPathReaders = path.join(channelPathBase, 'readers'); | ||
var channelHash = sha3_224(channelName); // use hash incase of strange characters | ||
/** | ||
* because the lenght of socket-paths is limited, we use only the first 20 chars | ||
* and also start with A to ensure we do not start with a number | ||
* @link https://serverfault.com/questions/641347/check-if-a-path-exceeds-maximum-for-unix-domain-socket | ||
*/ | ||
var channelFolder = 'A' + channelHash.substring(0, 20); | ||
var channelPathBase = path.join(TMP_FOLDER_BASE, channelFolder); | ||
var folderPathReaders = path.join(channelPathBase, 'rdrs'); | ||
var folderPathMessages = path.join(channelPathBase, 'messages'); | ||
var ret = { | ||
base: folderPathBase, | ||
base: TMP_FOLDER_BASE, | ||
channelBase: channelPathBase, | ||
@@ -768,10 +808,10 @@ readers: folderPathReaders, | ||
channelState.writeBlockPromise = channelState.writeBlockPromise.then(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee14() { | ||
var _ref16, msgObj, pingStr; | ||
channelState.writeBlockPromise = channelState.writeBlockPromise.then(_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee15() { | ||
var _ref17, msgObj, pingStr; | ||
return _regeneratorRuntime.wrap(function _callee14$(_context14) { | ||
return _regeneratorRuntime.wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context14.next = 2; | ||
_context15.next = 2; | ||
return new Promise(function (res) { | ||
@@ -782,3 +822,3 @@ return setTimeout(res, 0); | ||
case 2: | ||
_context14.next = 4; | ||
_context15.next = 4; | ||
return new Promise(function (res) { | ||
@@ -789,12 +829,12 @@ return setTimeout(res, 0); | ||
case 4: | ||
_context14.next = 6; | ||
_context15.next = 6; | ||
return Promise.all([writePromise, refreshReaderClients(channelState)]); | ||
case 6: | ||
_ref16 = _context14.sent; | ||
msgObj = _ref16[0]; | ||
_ref17 = _context15.sent; | ||
msgObj = _ref17[0]; | ||
emitOverFastPath(channelState, msgObj, messageJson); | ||
pingStr = '{"t":' + msgObj.time + ',"u":"' + msgObj.uuid + '","to":"' + msgObj.token + '"}|'; | ||
_context14.next = 12; | ||
_context15.next = 12; | ||
return Promise.all(Object.values(channelState.otherReaderClients).filter(function (client) { | ||
@@ -817,3 +857,4 @@ return client.writable; | ||
if (randomInt(0, 20) === 0) { | ||
/* await */getAllMessages(channelState.channelName).then(function (allMessages) { | ||
/* await */ | ||
getAllMessages(channelState.channelName).then(function (allMessages) { | ||
return cleanOldMessages(allMessages, channelState.options.node.ttl); | ||
@@ -828,6 +869,6 @@ }); | ||
case 'end': | ||
return _context14.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee14, _this2); | ||
}, _callee15, _this2); | ||
}))); | ||
@@ -918,2 +959,3 @@ | ||
ensureFoldersExist: ensureFoldersExist, | ||
clearNodeFolder: clearNodeFolder, | ||
socketPath: socketPath, | ||
@@ -920,0 +962,0 @@ socketInfoPath: socketInfoPath, |
@@ -41,2 +41,28 @@ 'use strict'; | ||
// STATICS | ||
/** | ||
* used to identify if someone overwrites | ||
* window.BroadcastChannel with this | ||
* See methods/native.js | ||
*/ | ||
BroadcastChannel._pubkey = true; | ||
/** | ||
* clears the tmp-folder if is node | ||
* @return {Promise<boolean>} true if has run, false if not node | ||
*/ | ||
BroadcastChannel.clearNodeFolder = function (options) { | ||
options = (0, _options.fillOptionsWithDefaults)(options); | ||
var method = (0, _methodChooser.chooseMethod)(options); | ||
if (method.type === 'node') { | ||
return method.clearNodeFolder().then(function () { | ||
return true; | ||
}); | ||
} else { | ||
return Promise.resolve(false); | ||
} | ||
}; | ||
// PROTOTYPE | ||
BroadcastChannel.prototype = { | ||
@@ -147,2 +173,3 @@ _post: function _post(type, msg) { | ||
} | ||
function _removeListenerObject(channel, type, obj) { | ||
@@ -149,0 +176,0 @@ channel._addEventListeners[type] = channel._addEventListeners[type].filter(function (o) { |
@@ -59,3 +59,8 @@ 'use strict'; | ||
if (typeof BroadcastChannel === 'function') return true; | ||
if (typeof BroadcastChannel === 'function') { | ||
if (BroadcastChannel._pubkey) { | ||
throw new Error('BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill'); | ||
} | ||
return true; | ||
} else return false; | ||
} | ||
@@ -62,0 +67,0 @@ |
@@ -35,22 +35,10 @@ 'use strict'; | ||
case 5: | ||
_context.t0 = Promise; | ||
_context.next = 8; | ||
return mkdir(paths.readers)['catch'](function () { | ||
_context.next = 7; | ||
return Promise.all([mkdir(paths.readers)['catch'](function () { | ||
return null; | ||
}); | ||
case 8: | ||
_context.t1 = _context.sent; | ||
_context.next = 11; | ||
return mkdir(paths.messages)['catch'](function () { | ||
}), mkdir(paths.messages)['catch'](function () { | ||
return null; | ||
}); | ||
})]); | ||
case 11: | ||
_context.t2 = _context.sent; | ||
_context.t3 = [_context.t1, _context.t2]; | ||
_context.next = 15; | ||
return _context.t0.all.call(_context.t0, _context.t3); | ||
case 15: | ||
case 7: | ||
case 'end': | ||
@@ -69,2 +57,45 @@ return _context.stop(); | ||
/** | ||
* removes the tmp-folder | ||
* @return {Promise<true>} | ||
*/ | ||
var clearNodeFolder = function () { | ||
var _ref2 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee2() { | ||
var paths, removePath; | ||
return _regenerator2['default'].wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
paths = getPaths('foobar'); | ||
removePath = paths.base; | ||
if (!(!removePath || removePath === '' || removePath === '/')) { | ||
_context2.next = 4; | ||
break; | ||
} | ||
throw new Error('BroadcastChannel.clearNodeFolder(): path is wrong'); | ||
case 4: | ||
_context2.next = 6; | ||
return removeDir(paths.base); | ||
case 6: | ||
return _context2.abrupt('return', true); | ||
case 7: | ||
case 'end': | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
return function clearNodeFolder() { | ||
return _ref2.apply(this, arguments); | ||
}; | ||
}(); | ||
/** | ||
* creates the socket-file and subscribes to it | ||
@@ -74,7 +105,7 @@ * @return {{emitter: EventEmitter, server: any}} | ||
var createSocketEventEmitter = function () { | ||
var _ref2 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee2(channelName, readerUuid) { | ||
var _ref3 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee3(channelName, readerUuid) { | ||
var pathToSocket, emitter, server; | ||
return _regenerator2['default'].wrap(function _callee2$(_context2) { | ||
return _regenerator2['default'].wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
@@ -85,3 +116,2 @@ pathToSocket = socketPath(channelName, readerUuid); | ||
stream.on('end', function () {}); | ||
stream.on('data', function (msg) { | ||
@@ -91,6 +121,7 @@ emitter.emit('data', msg.toString()); | ||
}); | ||
_context2.next = 5; | ||
return new Promise(function (res) { | ||
server.listen(pathToSocket, function () { | ||
res(); | ||
_context3.next = 5; | ||
return new Promise(function (resolve, reject) { | ||
server.listen(pathToSocket, function (err, res) { | ||
if (err) reject(err);else resolve(res); | ||
}); | ||
@@ -102,3 +133,3 @@ }); | ||
return _context2.abrupt('return', { | ||
return _context3.abrupt('return', { | ||
path: pathToSocket, | ||
@@ -111,10 +142,10 @@ emitter: emitter, | ||
case 'end': | ||
return _context2.stop(); | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
}, _callee3, this); | ||
})); | ||
return function createSocketEventEmitter(_x2, _x3) { | ||
return _ref2.apply(this, arguments); | ||
return _ref3.apply(this, arguments); | ||
}; | ||
@@ -124,11 +155,11 @@ }(); | ||
var openClientConnection = function () { | ||
var _ref3 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee3(channelName, readerUuid) { | ||
var _ref4 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee4(channelName, readerUuid) { | ||
var pathToSocket, client; | ||
return _regenerator2['default'].wrap(function _callee3$(_context3) { | ||
return _regenerator2['default'].wrap(function _callee4$(_context4) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
switch (_context4.prev = _context4.next) { | ||
case 0: | ||
pathToSocket = socketPath(channelName, readerUuid); | ||
client = new net.Socket(); | ||
_context3.next = 4; | ||
_context4.next = 4; | ||
return new Promise(function (res) { | ||
@@ -139,14 +170,14 @@ client.connect(pathToSocket, res); | ||
case 4: | ||
return _context3.abrupt('return', client); | ||
return _context4.abrupt('return', client); | ||
case 5: | ||
case 'end': | ||
return _context3.stop(); | ||
return _context4.stop(); | ||
} | ||
} | ||
}, _callee3, this); | ||
}, _callee4, this); | ||
})); | ||
return function openClientConnection(_x4, _x5) { | ||
return _ref3.apply(this, arguments); | ||
return _ref4.apply(this, arguments); | ||
}; | ||
@@ -162,7 +193,7 @@ }(); | ||
var writeMessage = function () { | ||
var _ref4 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee4(channelName, readerUuid, messageJson) { | ||
var _ref5 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee5(channelName, readerUuid, messageJson) { | ||
var time, writeObject, token, fileName, msgPath; | ||
return _regenerator2['default'].wrap(function _callee4$(_context4) { | ||
return _regenerator2['default'].wrap(function _callee5$(_context5) { | ||
while (1) { | ||
switch (_context4.prev = _context4.next) { | ||
switch (_context5.prev = _context5.next) { | ||
case 0: | ||
@@ -178,7 +209,7 @@ time = microSeconds(); | ||
msgPath = path.join(getPaths(channelName).messages, fileName); | ||
_context4.next = 7; | ||
_context5.next = 7; | ||
return writeFile(msgPath, JSON.stringify(writeObject)); | ||
case 7: | ||
return _context4.abrupt('return', { | ||
return _context5.abrupt('return', { | ||
time: time, | ||
@@ -192,10 +223,10 @@ uuid: readerUuid, | ||
case 'end': | ||
return _context4.stop(); | ||
return _context5.stop(); | ||
} | ||
} | ||
}, _callee4, this); | ||
}, _callee5, this); | ||
})); | ||
return function writeMessage(_x6, _x7, _x8) { | ||
return _ref4.apply(this, arguments); | ||
return _ref5.apply(this, arguments); | ||
}; | ||
@@ -211,15 +242,15 @@ }(); | ||
var getReadersUuids = function () { | ||
var _ref5 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee5(channelName) { | ||
var _ref6 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee6(channelName) { | ||
var readersPath, files; | ||
return _regenerator2['default'].wrap(function _callee5$(_context5) { | ||
return _regenerator2['default'].wrap(function _callee6$(_context6) { | ||
while (1) { | ||
switch (_context5.prev = _context5.next) { | ||
switch (_context6.prev = _context6.next) { | ||
case 0: | ||
readersPath = getPaths(channelName).readers; | ||
_context5.next = 3; | ||
_context6.next = 3; | ||
return readdir(readersPath); | ||
case 3: | ||
files = _context5.sent; | ||
return _context5.abrupt('return', files.map(function (file) { | ||
files = _context6.sent; | ||
return _context6.abrupt('return', files.map(function (file) { | ||
return file.split('.'); | ||
@@ -235,10 +266,10 @@ }).filter(function (split) { | ||
case 'end': | ||
return _context5.stop(); | ||
return _context6.stop(); | ||
} | ||
} | ||
}, _callee5, this); | ||
}, _callee6, this); | ||
})); | ||
return function getReadersUuids(_x9) { | ||
return _ref5.apply(this, arguments); | ||
return _ref6.apply(this, arguments); | ||
}; | ||
@@ -248,22 +279,22 @@ }(); | ||
var messagePath = function () { | ||
var _ref6 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee6(channelName, time, token, writerUuid) { | ||
var _ref7 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee7(channelName, time, token, writerUuid) { | ||
var fileName, msgPath; | ||
return _regenerator2['default'].wrap(function _callee6$(_context6) { | ||
return _regenerator2['default'].wrap(function _callee7$(_context7) { | ||
while (1) { | ||
switch (_context6.prev = _context6.next) { | ||
switch (_context7.prev = _context7.next) { | ||
case 0: | ||
fileName = time + '_' + writerUuid + '_' + token + '.json'; | ||
msgPath = path.join(getPaths(channelName).messages, fileName); | ||
return _context6.abrupt('return', msgPath); | ||
return _context7.abrupt('return', msgPath); | ||
case 3: | ||
case 'end': | ||
return _context6.stop(); | ||
return _context7.stop(); | ||
} | ||
} | ||
}, _callee6, this); | ||
}, _callee7, this); | ||
})); | ||
return function messagePath(_x10, _x11, _x12, _x13) { | ||
return _ref6.apply(this, arguments); | ||
return _ref7.apply(this, arguments); | ||
}; | ||
@@ -273,15 +304,15 @@ }(); | ||
var getAllMessages = function () { | ||
var _ref7 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee7(channelName) { | ||
var _ref8 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee8(channelName) { | ||
var messagesPath, files; | ||
return _regenerator2['default'].wrap(function _callee7$(_context7) { | ||
return _regenerator2['default'].wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context7.prev = _context7.next) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
messagesPath = getPaths(channelName).messages; | ||
_context7.next = 3; | ||
_context8.next = 3; | ||
return readdir(messagesPath); | ||
case 3: | ||
files = _context7.sent; | ||
return _context7.abrupt('return', files.map(function (file) { | ||
files = _context8.sent; | ||
return _context8.abrupt('return', files.map(function (file) { | ||
var fileName = file.split('.')[0]; | ||
@@ -300,10 +331,10 @@ var split = fileName.split('_'); | ||
case 'end': | ||
return _context7.stop(); | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee7, this); | ||
}, _callee8, this); | ||
})); | ||
return function getAllMessages(_x14) { | ||
return _ref7.apply(this, arguments); | ||
return _ref8.apply(this, arguments); | ||
}; | ||
@@ -313,10 +344,10 @@ }(); | ||
var cleanOldMessages = function () { | ||
var _ref8 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee8(messageObjects, ttl) { | ||
var _ref9 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee9(messageObjects, ttl) { | ||
var olderThen; | ||
return _regenerator2['default'].wrap(function _callee8$(_context8) { | ||
return _regenerator2['default'].wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
olderThen = Date.now() - ttl; | ||
_context8.next = 3; | ||
_context9.next = 3; | ||
return Promise.all(messageObjects.filter(function (obj) { | ||
@@ -332,10 +363,10 @@ return obj.time / 1000 < olderThen; | ||
case 'end': | ||
return _context8.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee9, this); | ||
})); | ||
return function cleanOldMessages(_x15, _x16) { | ||
return _ref8.apply(this, arguments); | ||
return _ref9.apply(this, arguments); | ||
}; | ||
@@ -345,14 +376,14 @@ }(); | ||
var create = function () { | ||
var _ref9 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee9(channelName) { | ||
var _ref10 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee10(channelName) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var time, uuid, state, _ref10, _ref11, socketEE, infoFilePath; | ||
var time, uuid, state, _ref11, _ref12, socketEE, infoFilePath; | ||
return _regenerator2['default'].wrap(function _callee9$(_context9) { | ||
return _regenerator2['default'].wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
options = fillOptionsWithDefaults(options); | ||
time = microSeconds(); | ||
_context9.next = 4; | ||
_context10.next = 4; | ||
return ensureFoldersExist(channelName); | ||
@@ -385,10 +416,10 @@ | ||
_context9.next = 10; | ||
_context10.next = 10; | ||
return Promise.all([createSocketEventEmitter(channelName, uuid), createSocketInfoFile(channelName, uuid), refreshReaderClients(state)]); | ||
case 10: | ||
_ref10 = _context9.sent; | ||
_ref11 = (0, _slicedToArray3['default'])(_ref10, 2); | ||
socketEE = _ref11[0]; | ||
infoFilePath = _ref11[1]; | ||
_ref11 = _context10.sent; | ||
_ref12 = (0, _slicedToArray3['default'])(_ref11, 2); | ||
socketEE = _ref12[0]; | ||
infoFilePath = _ref12[1]; | ||
@@ -416,14 +447,14 @@ state.socketEE = socketEE; | ||
return _context9.abrupt('return', state); | ||
return _context10.abrupt('return', state); | ||
case 18: | ||
case 'end': | ||
return _context9.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
}, _callee10, this); | ||
})); | ||
return function create(_x18) { | ||
return _ref9.apply(this, arguments); | ||
return _ref10.apply(this, arguments); | ||
}; | ||
@@ -437,14 +468,14 @@ }(); | ||
var handleMessagePing = function () { | ||
var _ref12 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee10(state, msgObj) { | ||
var _ref13 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee11(state, msgObj) { | ||
var messages, useMessages; | ||
return _regenerator2['default'].wrap(function _callee10$(_context10) { | ||
return _regenerator2['default'].wrap(function _callee11$(_context11) { | ||
while (1) { | ||
switch (_context10.prev = _context10.next) { | ||
switch (_context11.prev = _context11.next) { | ||
case 0: | ||
if (state.messagesCallback) { | ||
_context10.next = 2; | ||
_context11.next = 2; | ||
break; | ||
} | ||
return _context10.abrupt('return'); | ||
return _context11.abrupt('return'); | ||
@@ -455,12 +486,12 @@ case 2: | ||
if (msgObj) { | ||
_context10.next = 9; | ||
_context11.next = 9; | ||
break; | ||
} | ||
_context10.next = 6; | ||
_context11.next = 6; | ||
return getAllMessages(state.channelName); | ||
case 6: | ||
messages = _context10.sent; | ||
_context10.next = 10; | ||
messages = _context11.sent; | ||
_context11.next = 10; | ||
break; | ||
@@ -477,3 +508,3 @@ | ||
return msgObjA.time - msgObjB.time; | ||
}); // sort by time | ||
}); // sort by time | ||
@@ -484,10 +515,10 @@ | ||
if (!(!useMessages.length || !state.messagesCallback)) { | ||
_context10.next = 13; | ||
_context11.next = 13; | ||
break; | ||
} | ||
return _context10.abrupt('return'); | ||
return _context11.abrupt('return'); | ||
case 13: | ||
_context10.next = 15; | ||
_context11.next = 15; | ||
return Promise.all(useMessages.map(function (msgObj) { | ||
@@ -512,10 +543,10 @@ return readMessage(msgObj).then(function (content) { | ||
case 'end': | ||
return _context10.stop(); | ||
return _context11.stop(); | ||
} | ||
} | ||
}, _callee10, this); | ||
}, _callee11, this); | ||
})); | ||
return function handleMessagePing(_x19, _x20) { | ||
return _ref12.apply(this, arguments); | ||
return _ref13.apply(this, arguments); | ||
}; | ||
@@ -525,15 +556,15 @@ }(); | ||
var refreshReaderClients = function () { | ||
var _ref13 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(channelState) { | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14(channelState) { | ||
var _this = this; | ||
var otherReaders; | ||
return _regenerator2['default'].wrap(function _callee13$(_context13) { | ||
return _regenerator2['default'].wrap(function _callee14$(_context14) { | ||
while (1) { | ||
switch (_context13.prev = _context13.next) { | ||
switch (_context14.prev = _context14.next) { | ||
case 0: | ||
_context13.next = 2; | ||
_context14.next = 2; | ||
return getReadersUuids(channelState.channelName); | ||
case 2: | ||
otherReaders = _context13.sent; | ||
otherReaders = _context14.sent; | ||
@@ -545,18 +576,18 @@ | ||
}).forEach(function () { | ||
var _ref14 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee11(readerUuid) { | ||
return _regenerator2['default'].wrap(function _callee11$(_context11) { | ||
var _ref15 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee12(readerUuid) { | ||
return _regenerator2['default'].wrap(function _callee12$(_context12) { | ||
while (1) { | ||
switch (_context11.prev = _context11.next) { | ||
switch (_context12.prev = _context12.next) { | ||
case 0: | ||
_context11.prev = 0; | ||
_context11.next = 3; | ||
_context12.prev = 0; | ||
_context12.next = 3; | ||
return channelState.otherReaderClients[readerUuid].destroy(); | ||
case 3: | ||
_context11.next = 7; | ||
_context12.next = 7; | ||
break; | ||
case 5: | ||
_context11.prev = 5; | ||
_context11.t0 = _context11['catch'](0); | ||
_context12.prev = 5; | ||
_context12.t0 = _context12['catch'](0); | ||
@@ -568,14 +599,14 @@ case 7: | ||
case 'end': | ||
return _context11.stop(); | ||
return _context12.stop(); | ||
} | ||
} | ||
}, _callee11, _this, [[0, 5]]); | ||
}, _callee12, _this, [[0, 5]]); | ||
})); | ||
return function (_x22) { | ||
return _ref14.apply(this, arguments); | ||
return _ref15.apply(this, arguments); | ||
}; | ||
}()); | ||
_context13.next = 6; | ||
_context14.next = 6; | ||
return Promise.all(otherReaders.filter(function (readerUuid) { | ||
@@ -588,42 +619,42 @@ return readerUuid !== channelState.uuid; | ||
.map(function () { | ||
var _ref15 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee12(readerUuid) { | ||
var _ref16 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(readerUuid) { | ||
var client; | ||
return _regenerator2['default'].wrap(function _callee12$(_context12) { | ||
return _regenerator2['default'].wrap(function _callee13$(_context13) { | ||
while (1) { | ||
switch (_context12.prev = _context12.next) { | ||
switch (_context13.prev = _context13.next) { | ||
case 0: | ||
_context12.prev = 0; | ||
_context13.prev = 0; | ||
if (!channelState.closed) { | ||
_context12.next = 3; | ||
_context13.next = 3; | ||
break; | ||
} | ||
return _context12.abrupt('return'); | ||
return _context13.abrupt('return'); | ||
case 3: | ||
_context12.next = 5; | ||
_context13.next = 5; | ||
return openClientConnection(channelState.channelName, readerUuid); | ||
case 5: | ||
client = _context12.sent; | ||
client = _context13.sent; | ||
channelState.otherReaderClients[readerUuid] = client; | ||
_context12.next = 11; | ||
_context13.next = 11; | ||
break; | ||
case 9: | ||
_context12.prev = 9; | ||
_context12.t0 = _context12['catch'](0); | ||
_context13.prev = 9; | ||
_context13.t0 = _context13['catch'](0); | ||
case 11: | ||
case 'end': | ||
return _context12.stop(); | ||
return _context13.stop(); | ||
} | ||
} | ||
}, _callee12, _this, [[0, 9]]); | ||
}, _callee13, _this, [[0, 9]]); | ||
})); | ||
return function (_x23) { | ||
return _ref15.apply(this, arguments); | ||
return _ref16.apply(this, arguments); | ||
}; | ||
@@ -637,10 +668,10 @@ }() | ||
case 'end': | ||
return _context13.stop(); | ||
return _context14.stop(); | ||
} | ||
} | ||
}, _callee13, this); | ||
}, _callee14, this); | ||
})); | ||
return function refreshReaderClients(_x21) { | ||
return _ref13.apply(this, arguments); | ||
return _ref14.apply(this, arguments); | ||
}; | ||
@@ -663,3 +694,3 @@ }(); | ||
var micro = require('nano-time'); | ||
var rimraf = require('rimraf'); | ||
var sha3_224 = require('js-sha3').sha3_224; | ||
@@ -694,17 +725,26 @@ var isNode = require('detect-node'); | ||
var readdir = util.promisify(fs.readdir); | ||
var removeDir = util.promisify(rimraf); | ||
var TMP_FOLDER_NAME = 'pubkey.broadcast-channel'; | ||
var OTHER_INSTANCES = {}; | ||
var TMP_FOLDER_NAME = 'pubkey.bc'; | ||
var TMP_FOLDER_BASE = path.join(os.tmpdir(), TMP_FOLDER_NAME); | ||
var getPathsCache = new Map(); | ||
var getPathsCache = new Map(); | ||
function getPaths(channelName) { | ||
if (!getPathsCache.has(channelName)) { | ||
var folderPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME); | ||
var channelPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME, sha3_224(channelName) // use hash incase of strange characters | ||
); | ||
var folderPathReaders = path.join(channelPathBase, 'readers'); | ||
var channelHash = sha3_224(channelName); // use hash incase of strange characters | ||
/** | ||
* because the lenght of socket-paths is limited, we use only the first 20 chars | ||
* and also start with A to ensure we do not start with a number | ||
* @link https://serverfault.com/questions/641347/check-if-a-path-exceeds-maximum-for-unix-domain-socket | ||
*/ | ||
var channelFolder = 'A' + channelHash.substring(0, 20); | ||
var channelPathBase = path.join(TMP_FOLDER_BASE, channelFolder); | ||
var folderPathReaders = path.join(channelPathBase, 'rdrs'); | ||
var folderPathMessages = path.join(channelPathBase, 'messages'); | ||
var ret = { | ||
base: folderPathBase, | ||
base: TMP_FOLDER_BASE, | ||
channelBase: channelPathBase, | ||
@@ -782,10 +822,10 @@ readers: folderPathReaders, | ||
channelState.writeBlockPromise = channelState.writeBlockPromise.then((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee14() { | ||
var _ref17, _ref18, msgObj, pingStr; | ||
channelState.writeBlockPromise = channelState.writeBlockPromise.then((0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee15() { | ||
var _ref18, _ref19, msgObj, pingStr; | ||
return _regenerator2['default'].wrap(function _callee14$(_context14) { | ||
return _regenerator2['default'].wrap(function _callee15$(_context15) { | ||
while (1) { | ||
switch (_context14.prev = _context14.next) { | ||
switch (_context15.prev = _context15.next) { | ||
case 0: | ||
_context14.next = 2; | ||
_context15.next = 2; | ||
return new Promise(function (res) { | ||
@@ -796,3 +836,3 @@ return setTimeout(res, 0); | ||
case 2: | ||
_context14.next = 4; | ||
_context15.next = 4; | ||
return new Promise(function (res) { | ||
@@ -803,13 +843,13 @@ return setTimeout(res, 0); | ||
case 4: | ||
_context14.next = 6; | ||
_context15.next = 6; | ||
return Promise.all([writePromise, refreshReaderClients(channelState)]); | ||
case 6: | ||
_ref17 = _context14.sent; | ||
_ref18 = (0, _slicedToArray3['default'])(_ref17, 1); | ||
msgObj = _ref18[0]; | ||
_ref18 = _context15.sent; | ||
_ref19 = (0, _slicedToArray3['default'])(_ref18, 1); | ||
msgObj = _ref19[0]; | ||
emitOverFastPath(channelState, msgObj, messageJson); | ||
pingStr = '{"t":' + msgObj.time + ',"u":"' + msgObj.uuid + '","to":"' + msgObj.token + '"}|'; | ||
_context14.next = 13; | ||
_context15.next = 13; | ||
return Promise.all(Object.values(channelState.otherReaderClients).filter(function (client) { | ||
@@ -832,3 +872,4 @@ return client.writable; | ||
if (randomInt(0, 20) === 0) { | ||
/* await */getAllMessages(channelState.channelName).then(function (allMessages) { | ||
/* await */ | ||
getAllMessages(channelState.channelName).then(function (allMessages) { | ||
return cleanOldMessages(allMessages, channelState.options.node.ttl); | ||
@@ -843,6 +884,6 @@ }); | ||
case 'end': | ||
return _context14.stop(); | ||
return _context15.stop(); | ||
} | ||
} | ||
}, _callee14, _this2); | ||
}, _callee15, _this2); | ||
}))); | ||
@@ -933,2 +974,3 @@ | ||
ensureFoldersExist: ensureFoldersExist, | ||
clearNodeFolder: clearNodeFolder, | ||
socketPath: socketPath, | ||
@@ -935,0 +977,0 @@ socketInfoPath: socketInfoPath, |
@@ -47,4 +47,7 @@ declare type MethodType = 'node' | 'idb' | 'native' | 'localstorage'; | ||
removeEventListener(type: EventType, handler: OnMessageHandler<T>): void; | ||
// statics | ||
static clearNodeFolder(opts?: BroadcastChannelOptions): Promise<boolean>; | ||
} | ||
export default BroadcastChannel; |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -62,4 +62,5 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", | ||
"custom-idle-queue": "2.0.1", | ||
"detect-node": "2.0.3", | ||
"js-sha3": "0.7.0", | ||
"detect-node": "2.0.4", | ||
"rimraf": "2.6.2", | ||
"js-sha3": "0.8.0", | ||
"microseconds": "0.1.0", | ||
@@ -70,2 +71,3 @@ "nano-time": "1.0.0", | ||
"devDependencies": { | ||
"@babel/core": "7.0.0", | ||
"@babel/types": "7.0.0-beta.51", | ||
@@ -77,4 +79,3 @@ "@types/core-js": "2.5.0", | ||
"babel-core": "6.26.3", | ||
"babel-eslint": "8.2.6", | ||
"babel-loader": "7.1.5", | ||
"babel-loader": "8.0.2", | ||
"babel-plugin-transform-async-to-generator": "6.24.1", | ||
@@ -98,11 +99,11 @@ "babel-plugin-transform-class-properties": "6.24.1", | ||
"child-process-promise": "2.2.1", | ||
"clone": "2.1.1", | ||
"concurrently": "3.6.1", | ||
"clone": "2.1.2", | ||
"concurrently": "4.0.1", | ||
"convert-hrtime": "2.0.0", | ||
"copyfiles": "2.0.0", | ||
"cross-env": "5.2.0", | ||
"eslint": "5.2.0", | ||
"eslint": "5.5.0", | ||
"gzip-size-cli": "3.0.0", | ||
"http-server": "0.11.1", | ||
"karma": "2.0.5", | ||
"karma": "3.0.0", | ||
"karma-babel-preprocessor": "7.0.0", | ||
@@ -120,11 +121,12 @@ "karma-browserify": "5.3.0", | ||
"mocha": "5.2.0", | ||
"node": "10.9.0", | ||
"pre-commit": "1.2.2", | ||
"random-int": "1.0.0", | ||
"random-token": "0.0.8", | ||
"rimraf": "2.6.2", | ||
"testcafe": "0.20.4", | ||
"testcafe-hammerhead": "14.1.1", | ||
"ts-node": "7.0.0", | ||
"typescript": "3.0.1", | ||
"webpack": "4.16.3", | ||
"testcafe": "0.21.1", | ||
"testcafe-hammerhead": "14.2.6", | ||
"ts-node": "7.0.1", | ||
"typescript": "3.0.3", | ||
"watchify": "3.11.0", | ||
"webpack": "4.17.2", | ||
"webpack-cli": "3.1.0" | ||
@@ -131,0 +133,0 @@ }, |
@@ -41,3 +41,3 @@ | ||
Create a channel in one tab/process and send a message. | ||
#### Create a channel in one tab/process and send a message. | ||
@@ -50,3 +50,3 @@ ```js | ||
Create a channel with the same name in another tab/process and recieve messages. | ||
#### Create a channel with the same name in another tab/process and recieve messages. | ||
@@ -61,3 +61,3 @@ ```js | ||
Add and remove multiple eventlisteners | ||
#### Add and remove multiple eventlisteners | ||
@@ -75,3 +75,3 @@ ```js | ||
Close the channel if you do not need it anymore. | ||
#### Close the channel if you do not need it anymore. | ||
@@ -82,3 +82,3 @@ ```js | ||
Set options when creating a channel (optional): | ||
#### Set options when creating a channel (optional): | ||
@@ -93,3 +93,3 @@ ```js | ||
Create a typed channel in typescript: | ||
#### Create a typed channel in typescript: | ||
@@ -107,2 +107,13 @@ ```typescript | ||
#### Clear tmp-folder: | ||
When used in NodeJs, the BroadcastChannel will communicate with other processes over filesystem based sockets. | ||
When you create a huge amount of channels, like you would do when running unit tests, you might get problems because there are too many folders in the tmp-directory. Calling `BroadcastChannel.clearNodeFolder()` will clear the tmp-folder and it is recommended to run this at the beginning of your test-suite. | ||
```javascript | ||
import BroadcastChannel from 'broadcast-channel'; | ||
const hasRun = await BroadcastChannel.clearNodeFolder(); | ||
console.log(hasRun); // > true on NodeJs, false on Browsers | ||
``` | ||
## Methods: | ||
@@ -109,0 +120,0 @@ |
@@ -15,3 +15,3 @@ import { | ||
const BroadcastChannel = function (name, options) { | ||
const BroadcastChannel = function(name, options) { | ||
this.name = name; | ||
@@ -44,2 +44,26 @@ this.options = fillOptionsWithDefaults(options); | ||
// STATICS | ||
/** | ||
* used to identify if someone overwrites | ||
* window.BroadcastChannel with this | ||
* See methods/native.js | ||
*/ | ||
BroadcastChannel._pubkey = true; | ||
/** | ||
* clears the tmp-folder if is node | ||
* @return {Promise<boolean>} true if has run, false if not node | ||
*/ | ||
BroadcastChannel.clearNodeFolder = function(options) { | ||
options = fillOptionsWithDefaults(options); | ||
const method = chooseMethod(options); | ||
if (method.type === 'node') { | ||
return method.clearNodeFolder().then(() => true); | ||
} else { | ||
return Promise.resolve(false); | ||
} | ||
}; | ||
// PROTOTYPE | ||
BroadcastChannel.prototype = { | ||
@@ -150,2 +174,3 @@ _post(type, msg) { | ||
} | ||
function _removeListenerObject(channel, type, obj) { | ||
@@ -202,2 +227,2 @@ channel._addEventListeners[type] = channel._addEventListeners[type].filter(o => o !== obj); | ||
export default BroadcastChannel; | ||
export default BroadcastChannel; |
@@ -49,3 +49,10 @@ const isNode = require('detect-node'); | ||
if (typeof BroadcastChannel === 'function') return true; | ||
if (typeof BroadcastChannel === 'function') { | ||
if (BroadcastChannel._pubkey) { | ||
throw new Error( | ||
'BroadcastChannel: Do not overwrite window.BroadcastChannel with this module, this is not a polyfill' | ||
); | ||
} | ||
return true; | ||
} else return false; | ||
} | ||
@@ -56,2 +63,2 @@ | ||
return 100; | ||
} | ||
} |
@@ -13,3 +13,3 @@ /** | ||
const micro = require('nano-time'); | ||
const rimraf = require('rimraf'); | ||
const sha3_224 = require('js-sha3').sha3_224; | ||
@@ -48,21 +48,30 @@ const isNode = require('detect-node'); | ||
const readdir = util.promisify(fs.readdir); | ||
const removeDir = util.promisify(rimraf); | ||
const TMP_FOLDER_NAME = 'pubkey.broadcast-channel'; | ||
const OTHER_INSTANCES = {}; | ||
const TMP_FOLDER_NAME = 'pubkey.bc'; | ||
const TMP_FOLDER_BASE = path.join( | ||
os.tmpdir(), | ||
TMP_FOLDER_NAME | ||
); | ||
const getPathsCache = new Map(); | ||
const getPathsCache = new Map(); | ||
function getPaths(channelName) { | ||
if (!getPathsCache.has(channelName)) { | ||
const folderPathBase = path.join( | ||
os.tmpdir(), | ||
TMP_FOLDER_NAME | ||
); | ||
const channelHash = sha3_224(channelName); // use hash incase of strange characters | ||
/** | ||
* because the lenght of socket-paths is limited, we use only the first 20 chars | ||
* and also start with A to ensure we do not start with a number | ||
* @link https://serverfault.com/questions/641347/check-if-a-path-exceeds-maximum-for-unix-domain-socket | ||
*/ | ||
const channelFolder = 'A' + channelHash.substring(0, 20); | ||
const channelPathBase = path.join( | ||
os.tmpdir(), | ||
TMP_FOLDER_NAME, | ||
sha3_224(channelName) // use hash incase of strange characters | ||
TMP_FOLDER_BASE, | ||
channelFolder | ||
); | ||
const folderPathReaders = path.join( | ||
channelPathBase, | ||
'readers' | ||
'rdrs' | ||
); | ||
@@ -75,3 +84,3 @@ const folderPathMessages = path.join( | ||
const ret = { | ||
base: folderPathBase, | ||
base: TMP_FOLDER_BASE, | ||
channelBase: channelPathBase, | ||
@@ -92,7 +101,22 @@ readers: folderPathReaders, | ||
await Promise.all([ | ||
await mkdir(paths.readers).catch(() => null), | ||
await mkdir(paths.messages).catch(() => null) | ||
mkdir(paths.readers).catch(() => null), | ||
mkdir(paths.messages).catch(() => null) | ||
]); | ||
} | ||
/** | ||
* removes the tmp-folder | ||
* @return {Promise<true>} | ||
*/ | ||
async function clearNodeFolder() { | ||
const paths = getPaths('foobar'); | ||
const removePath = paths.base; | ||
if (!removePath || removePath === '' || removePath === '/') { | ||
throw new Error('BroadcastChannel.clearNodeFolder(): path is wrong'); | ||
} | ||
await removeDir(paths.base); | ||
return true; | ||
} | ||
function socketPath(channelName, readerUuid) { | ||
@@ -143,6 +167,4 @@ | ||
.createServer(stream => { | ||
stream.on('end', function () { | ||
}); | ||
stream.on('data', function (msg) { | ||
stream.on('end', function() {}); | ||
stream.on('data', function(msg) { | ||
emitter.emit('data', msg.toString()); | ||
@@ -152,9 +174,10 @@ }); | ||
await new Promise(res => { | ||
server.listen(pathToSocket, () => { | ||
res(); | ||
await new Promise((resolve, reject) => { | ||
server.listen(pathToSocket, (err, res) => { | ||
if (err) reject(err); | ||
else resolve(res); | ||
}); | ||
}); | ||
server.on('connection', () => { | ||
}); | ||
server.on('connection', () => {}); | ||
@@ -281,4 +304,4 @@ return { | ||
messageObjects | ||
.filter(obj => (obj.time / 1000) < olderThen) | ||
.map(obj => unlink(obj.path).catch(() => null)) | ||
.filter(obj => (obj.time / 1000) < olderThen) | ||
.map(obj => unlink(obj.path).catch(() => null)) | ||
); | ||
@@ -384,3 +407,3 @@ } | ||
.filter(msgObj => _filterMessage(msgObj, state)) | ||
.sort((msgObjA, msgObjB) => msgObjA.time - msgObjB.time); // sort by time | ||
.sort((msgObjA, msgObjB) => msgObjA.time - msgObjB.time); // sort by time | ||
@@ -394,5 +417,5 @@ | ||
useMessages | ||
.map( | ||
msgObj => readMessage(msgObj).then(content => msgObj.content = content) | ||
) | ||
.map( | ||
msgObj => readMessage(msgObj).then(content => msgObj.content = content) | ||
) | ||
); | ||
@@ -420,3 +443,3 @@ | ||
await channelState.otherReaderClients[readerUuid].destroy(); | ||
} catch (err) { } | ||
} catch (err) {} | ||
delete channelState.otherReaderClients[readerUuid]; | ||
@@ -427,14 +450,14 @@ }); | ||
otherReaders | ||
.filter(readerUuid => readerUuid !== channelState.uuid) // not own | ||
.filter(readerUuid => !channelState.otherReaderClients[readerUuid]) // not already has client | ||
.map(async (readerUuid) => { | ||
try { | ||
if (channelState.closed) return; | ||
const client = await openClientConnection(channelState.channelName, readerUuid); | ||
channelState.otherReaderClients[readerUuid] = client; | ||
} catch (err) { | ||
// this might throw if the other channel is closed at the same time when this one is running refresh | ||
// so we do not throw an error | ||
} | ||
}) | ||
.filter(readerUuid => readerUuid !== channelState.uuid) // not own | ||
.filter(readerUuid => !channelState.otherReaderClients[readerUuid]) // not already has client | ||
.map(async (readerUuid) => { | ||
try { | ||
if (channelState.closed) return; | ||
const client = await openClientConnection(channelState.channelName, readerUuid); | ||
channelState.otherReaderClients[readerUuid] = client; | ||
} catch (err) { | ||
// this might throw if the other channel is closed at the same time when this one is running refresh | ||
// so we do not throw an error | ||
} | ||
}) | ||
); | ||
@@ -466,8 +489,8 @@ } | ||
Object.values(channelState.otherReaderClients) | ||
.filter(client => client.writable) // client might have closed in between | ||
.map(client => { | ||
return new Promise(res => { | ||
client.write(pingStr, res); | ||
}); | ||
}) | ||
.filter(client => client.writable) // client might have closed in between | ||
.map(client => { | ||
return new Promise(res => { | ||
client.write(pingStr, res); | ||
}); | ||
}) | ||
); | ||
@@ -481,3 +504,4 @@ | ||
if (randomInt(0, 20) === 0) { | ||
/* await */ getAllMessages(channelState.channelName) | ||
/* await */ | ||
getAllMessages(channelState.channelName) | ||
.then(allMessages => cleanOldMessages(allMessages, channelState.options.node.ttl)); | ||
@@ -564,2 +588,3 @@ } | ||
ensureFoldersExist, | ||
clearNodeFolder, | ||
socketPath, | ||
@@ -589,2 +614,2 @@ socketInfoPath, | ||
microSeconds | ||
}; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
207255
5067
179
8
58
+ Addedrimraf@2.6.2
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addeddetect-node@2.0.4(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedjs-sha3@0.8.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedrimraf@2.6.2(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removeddetect-node@2.0.3(transitive)
- Removedjs-sha3@0.7.0(transitive)
Updateddetect-node@2.0.4
Updatedjs-sha3@0.8.0