broadcast-channel
Advanced tools
Comparing version 1.2.11 to 1.3.0
@@ -18,5 +18,10 @@ var isNode = require('detect-node'); | ||
if (isNode) { | ||
var NodeMethod = REQUIRE_FUN('./methods/node.js'); | ||
/** | ||
* we use the non-transpiled code for nodejs | ||
* because it runs faster | ||
*/ | ||
var NodeMethod = REQUIRE_FUN('../../src/methods/node.js'); | ||
/** | ||
* this will be false for webpackbuilds | ||
@@ -23,0 +28,0 @@ * which will shim the node-method with an empty object {} |
import _regeneratorRuntime from 'babel-runtime/regenerator'; | ||
import _asyncToGenerator from 'babel-runtime/helpers/asyncToGenerator'; | ||
/** | ||
* this method is used in nodejs-environments. | ||
* The ipc is handled via sockets and file-writes to the tmp-folder | ||
*/ | ||
import * as util from 'util'; | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as events from 'events'; | ||
import * as net from 'net'; | ||
import * as path from 'path'; | ||
import micro from 'nano-time'; | ||
import { sha3_224 } from 'js-sha3'; | ||
import isNode from 'detect-node'; | ||
var unload = require('unload'); | ||
import { fillOptionsWithDefaults } from '../options'; | ||
import { randomInt, randomToken } from '../util'; | ||
import ObliviousSet from '../oblivious-set'; | ||
/** | ||
* windows sucks, so we have handle windows-type of socket-paths | ||
* @link https://gist.github.com/domenic/2790533#gistcomment-331356 | ||
*/ | ||
export function cleanPipeName(str) { | ||
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) { | ||
str = str.replace(/^\//, ''); | ||
str = str.replace(/\//g, '-'); | ||
return '\\\\.\\pipe\\' + str; | ||
} else { | ||
return str; | ||
} | ||
} | ||
var mkdir = util.promisify(fs.mkdir); | ||
var writeFile = util.promisify(fs.writeFile); | ||
var readFile = util.promisify(fs.readFile); | ||
var unlink = util.promisify(fs.unlink); | ||
var readdir = util.promisify(fs.readdir); | ||
var TMP_FOLDER_NAME = 'pubkey.broadcast-channel'; | ||
var OTHER_INSTANCES = {}; | ||
var getPathsCache = new Map(); | ||
export 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 folderPathMessages = path.join(channelPathBase, 'messages'); | ||
var ret = { | ||
base: folderPathBase, | ||
channelBase: channelPathBase, | ||
readers: folderPathReaders, | ||
messages: folderPathMessages | ||
}; | ||
getPathsCache.set(channelName, ret); | ||
return ret; | ||
} | ||
return getPathsCache.get(channelName); | ||
} | ||
export var ensureFoldersExist = function () { | ||
var ensureFoldersExist = function () { | ||
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(channelName) { | ||
@@ -123,34 +56,7 @@ var paths; | ||
export function socketPath(channelName, readerUuid) { | ||
var paths = getPaths(channelName); | ||
var socketPath = path.join(paths.readers, readerUuid + '.s'); | ||
return cleanPipeName(socketPath); | ||
} | ||
export function socketInfoPath(channelName, readerUuid) { | ||
var paths = getPaths(channelName); | ||
var socketPath = path.join(paths.readers, readerUuid + '.json'); | ||
return socketPath; | ||
} | ||
/** | ||
* Because it is not possible to get all socket-files in a folder, | ||
* when used under fucking windows, | ||
* we have to set a normal file so other readers know our socket exists | ||
*/ | ||
export function createSocketInfoFile(channelName, readerUuid) { | ||
var pathToFile = socketInfoPath(channelName, readerUuid); | ||
return writeFile(pathToFile, JSON.stringify({ | ||
time: microSeconds() | ||
})).then(function () { | ||
return pathToFile; | ||
}); | ||
} | ||
/** | ||
* creates the socket-file and subscribes to it | ||
* @return {{emitter: EventEmitter, server: any}} | ||
*/ | ||
export var createSocketEventEmitter = function () { | ||
var createSocketEventEmitter = function () { | ||
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(channelName, readerUuid) { | ||
@@ -200,3 +106,3 @@ var pathToSocket, emitter, server; | ||
export var openClientConnection = function () { | ||
var openClientConnection = function () { | ||
var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(channelName, readerUuid) { | ||
@@ -235,3 +141,5 @@ var pathToSocket, client; | ||
*/ | ||
export var writeMessage = function () { | ||
var writeMessage = function () { | ||
var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(channelName, readerUuid, messageJson) { | ||
@@ -280,3 +188,5 @@ var time, writeObject, token, fileName, msgPath; | ||
*/ | ||
export var getReadersUuids = function () { | ||
var getReadersUuids = function () { | ||
var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(channelName) { | ||
@@ -316,3 +226,3 @@ var readersPath, files; | ||
export var messagePath = function () { | ||
var messagePath = function () { | ||
var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(channelName, time, token, writerUuid) { | ||
@@ -341,3 +251,3 @@ var fileName, msgPath; | ||
export var getAllMessages = function () { | ||
var getAllMessages = function () { | ||
var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(channelName) { | ||
@@ -380,20 +290,3 @@ var messagesPath, files; | ||
export function getSingleMessage(channelName, msgObj) { | ||
var messagesPath = getPaths(channelName).messages; | ||
return { | ||
path: path.join(messagesPath, msgObj.t + '_' + msgObj.u + '_' + msgObj.to + '.json'), | ||
time: msgObj.t, | ||
senderUuid: msgObj.u, | ||
token: msgObj.to | ||
}; | ||
} | ||
export function readMessage(messageObj) { | ||
return readFile(messageObj.path, 'utf8').then(function (content) { | ||
return JSON.parse(content); | ||
}); | ||
} | ||
export var cleanOldMessages = function () { | ||
var cleanOldMessages = function () { | ||
var _ref8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(messageObjects, ttl) { | ||
@@ -428,5 +321,3 @@ var olderThen; | ||
export var type = 'node'; | ||
export var create = function () { | ||
var create = function () { | ||
var _ref9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(channelName) { | ||
@@ -515,13 +406,2 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
export function _filterMessage(msgObj, state) { | ||
if (msgObj.senderUuid === state.uuid) return false; // not send by own | ||
if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted | ||
if (!state.messagesCallback) return false; // no listener | ||
if (msgObj.time < state.messagesCallbackTime) return false; // not older then onMessageCallback | ||
if (msgObj.time < state.time) return false; // msgObj is older then channel | ||
state.emittedMessagesIds.add(msgObj.token); | ||
return true; | ||
} | ||
/** | ||
@@ -531,3 +411,3 @@ * when the socket pings, so that we now new messages came, | ||
*/ | ||
export var handleMessagePing = function () { | ||
var handleMessagePing = function () { | ||
var _ref11 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(state, msgObj) { | ||
@@ -615,3 +495,3 @@ var messages, useMessages; | ||
export var refreshReaderClients = function () { | ||
var refreshReaderClients = function () { | ||
var _ref12 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee13(channelState) { | ||
@@ -736,3 +616,127 @@ var _this = this; | ||
export function postMessage(channelState, messageJson) { | ||
/** | ||
* this method is used in nodejs-environments. | ||
* The ipc is handled via sockets and file-writes to the tmp-folder | ||
*/ | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var events = require('events'); | ||
var net = require('net'); | ||
var path = require('path'); | ||
var micro = require('nano-time'); | ||
var sha3_224 = require('js-sha3').sha3_224; | ||
var isNode = require('detect-node'); | ||
var unload = require('unload'); | ||
var fillOptionsWithDefaults = require('../../dist/lib/options.js').fillOptionsWithDefaults; | ||
var ownUtil = require('../../dist/lib/util.js'); | ||
var randomInt = ownUtil.randomInt; | ||
var randomToken = ownUtil.randomToken; | ||
var ObliviousSet = require('../../dist/lib/oblivious-set')['default']; | ||
/** | ||
* windows sucks, so we have handle windows-type of socket-paths | ||
* @link https://gist.github.com/domenic/2790533#gistcomment-331356 | ||
*/ | ||
function cleanPipeName(str) { | ||
if (process.platform === 'win32' && !str.startsWith('\\\\.\\pipe\\')) { | ||
str = str.replace(/^\//, ''); | ||
str = str.replace(/\//g, '-'); | ||
return '\\\\.\\pipe\\' + str; | ||
} else { | ||
return str; | ||
} | ||
} | ||
var mkdir = util.promisify(fs.mkdir); | ||
var writeFile = util.promisify(fs.writeFile); | ||
var readFile = util.promisify(fs.readFile); | ||
var unlink = util.promisify(fs.unlink); | ||
var readdir = util.promisify(fs.readdir); | ||
var TMP_FOLDER_NAME = 'pubkey.broadcast-channel'; | ||
var OTHER_INSTANCES = {}; | ||
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 folderPathMessages = path.join(channelPathBase, 'messages'); | ||
var ret = { | ||
base: folderPathBase, | ||
channelBase: channelPathBase, | ||
readers: folderPathReaders, | ||
messages: folderPathMessages | ||
}; | ||
getPathsCache.set(channelName, ret); | ||
return ret; | ||
} | ||
return getPathsCache.get(channelName); | ||
} | ||
function socketPath(channelName, readerUuid) { | ||
var paths = getPaths(channelName); | ||
var socketPath = path.join(paths.readers, readerUuid + '.s'); | ||
return cleanPipeName(socketPath); | ||
} | ||
function socketInfoPath(channelName, readerUuid) { | ||
var paths = getPaths(channelName); | ||
var socketPath = path.join(paths.readers, readerUuid + '.json'); | ||
return socketPath; | ||
} | ||
/** | ||
* Because it is not possible to get all socket-files in a folder, | ||
* when used under fucking windows, | ||
* we have to set a normal file so other readers know our socket exists | ||
*/ | ||
function createSocketInfoFile(channelName, readerUuid) { | ||
var pathToFile = socketInfoPath(channelName, readerUuid); | ||
return writeFile(pathToFile, JSON.stringify({ | ||
time: microSeconds() | ||
})).then(function () { | ||
return pathToFile; | ||
}); | ||
} | ||
function getSingleMessage(channelName, msgObj) { | ||
var messagesPath = getPaths(channelName).messages; | ||
return { | ||
path: path.join(messagesPath, msgObj.t + '_' + msgObj.u + '_' + msgObj.to + '.json'), | ||
time: msgObj.t, | ||
senderUuid: msgObj.u, | ||
token: msgObj.to | ||
}; | ||
} | ||
function readMessage(messageObj) { | ||
return readFile(messageObj.path, 'utf8').then(function (content) { | ||
return JSON.parse(content); | ||
}); | ||
} | ||
var type = 'node'; | ||
function _filterMessage(msgObj, state) { | ||
if (msgObj.senderUuid === state.uuid) return false; // not send by own | ||
if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted | ||
if (!state.messagesCallback) return false; // no listener | ||
if (msgObj.time < state.messagesCallbackTime) return false; // not older then onMessageCallback | ||
if (msgObj.time < state.time) return false; // msgObj is older then channel | ||
state.emittedMessagesIds.add(msgObj.token); | ||
return true; | ||
} | ||
function postMessage(channelState, messageJson) { | ||
var _this2 = this; | ||
@@ -813,3 +817,3 @@ | ||
*/ | ||
export function emitOverFastPath(state, msgObj, messageJson) { | ||
function emitOverFastPath(state, msgObj, messageJson) { | ||
if (!state.options.node.useFastPath) return; // disabled | ||
@@ -833,3 +837,3 @@ var others = OTHER_INSTANCES[state.channelName].filter(function (s) { | ||
export function onMessage(channelState, fn) { | ||
function onMessage(channelState, fn) { | ||
var time = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : microSeconds(); | ||
@@ -842,3 +846,3 @@ | ||
export function close(channelState) { | ||
function close(channelState) { | ||
if (channelState.closed) return; | ||
@@ -874,12 +878,42 @@ channelState.closed = true; | ||
export function canBeUsed() { | ||
function canBeUsed() { | ||
return isNode; | ||
} | ||
export function averageResponseTime() { | ||
function averageResponseTime() { | ||
return 50; | ||
} | ||
export function microSeconds() { | ||
function microSeconds() { | ||
return parseInt(micro.microseconds()); | ||
} | ||
} | ||
module.exports = { | ||
cleanPipeName: cleanPipeName, | ||
getPaths: getPaths, | ||
ensureFoldersExist: ensureFoldersExist, | ||
socketPath: socketPath, | ||
socketInfoPath: socketInfoPath, | ||
createSocketInfoFile: createSocketInfoFile, | ||
createSocketEventEmitter: createSocketEventEmitter, | ||
openClientConnection: openClientConnection, | ||
writeMessage: writeMessage, | ||
getReadersUuids: getReadersUuids, | ||
messagePath: messagePath, | ||
getAllMessages: getAllMessages, | ||
getSingleMessage: getSingleMessage, | ||
readMessage: readMessage, | ||
cleanOldMessages: cleanOldMessages, | ||
type: type, | ||
create: create, | ||
_filterMessage: _filterMessage, | ||
handleMessagePing: handleMessagePing, | ||
refreshReaderClients: refreshReaderClients, | ||
postMessage: postMessage, | ||
emitOverFastPath: emitOverFastPath, | ||
onMessage: onMessage, | ||
close: close, | ||
canBeUsed: canBeUsed, | ||
averageResponseTime: averageResponseTime, | ||
microSeconds: microSeconds | ||
}; |
@@ -24,5 +24,10 @@ 'use strict'; | ||
if (isNode) { | ||
var NodeMethod = REQUIRE_FUN('./methods/node.js'); | ||
/** | ||
* we use the non-transpiled code for nodejs | ||
* because it runs faster | ||
*/ | ||
var NodeMethod = REQUIRE_FUN('../../src/methods/node.js'); | ||
/** | ||
* this will be false for webpackbuilds | ||
@@ -29,0 +34,0 @@ * which will shim the node-method with an empty object {} |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.refreshReaderClients = exports.handleMessagePing = exports.create = exports.type = exports.cleanOldMessages = exports.getAllMessages = exports.messagePath = exports.getReadersUuids = exports.writeMessage = exports.openClientConnection = exports.createSocketEventEmitter = exports.ensureFoldersExist = undefined; | ||
var _slicedToArray2 = require('babel-runtime/helpers/slicedToArray'); | ||
@@ -20,3 +15,3 @@ | ||
var ensureFoldersExist = exports.ensureFoldersExist = function () { | ||
var ensureFoldersExist = function () { | ||
var _ref = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee(channelName) { | ||
@@ -77,3 +72,3 @@ var paths; | ||
*/ | ||
var createSocketEventEmitter = exports.createSocketEventEmitter = function () { | ||
var createSocketEventEmitter = function () { | ||
var _ref2 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee2(channelName, readerUuid) { | ||
@@ -123,3 +118,3 @@ var pathToSocket, emitter, server; | ||
var openClientConnection = exports.openClientConnection = function () { | ||
var openClientConnection = function () { | ||
var _ref3 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee3(channelName, readerUuid) { | ||
@@ -160,3 +155,3 @@ var pathToSocket, client; | ||
var writeMessage = exports.writeMessage = function () { | ||
var writeMessage = function () { | ||
var _ref4 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee4(channelName, readerUuid, messageJson) { | ||
@@ -174,3 +169,3 @@ var time, writeObject, token, fileName, msgPath; | ||
}; | ||
token = (0, _util2.randomToken)(12); | ||
token = randomToken(12); | ||
fileName = time + '_' + readerUuid + '_' + token + '.json'; | ||
@@ -208,3 +203,3 @@ msgPath = path.join(getPaths(channelName).messages, fileName); | ||
var getReadersUuids = exports.getReadersUuids = function () { | ||
var getReadersUuids = function () { | ||
var _ref5 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee5(channelName) { | ||
@@ -244,3 +239,3 @@ var readersPath, files; | ||
var messagePath = exports.messagePath = function () { | ||
var messagePath = function () { | ||
var _ref6 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee6(channelName, time, token, writerUuid) { | ||
@@ -269,3 +264,3 @@ var fileName, msgPath; | ||
var getAllMessages = exports.getAllMessages = function () { | ||
var getAllMessages = function () { | ||
var _ref7 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee7(channelName) { | ||
@@ -308,3 +303,3 @@ var messagesPath, files; | ||
var cleanOldMessages = exports.cleanOldMessages = function () { | ||
var cleanOldMessages = function () { | ||
var _ref8 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee8(messageObjects, ttl) { | ||
@@ -339,3 +334,3 @@ var olderThen; | ||
var create = exports.create = function () { | ||
var create = function () { | ||
var _ref9 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee9(channelName) { | ||
@@ -350,3 +345,3 @@ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
case 0: | ||
options = (0, _options.fillOptionsWithDefaults)(options); | ||
options = fillOptionsWithDefaults(options); | ||
time = microSeconds(); | ||
@@ -357,3 +352,3 @@ _context9.next = 4; | ||
case 4: | ||
uuid = (0, _util2.randomToken)(10); | ||
uuid = randomToken(10); | ||
state = { | ||
@@ -365,3 +360,3 @@ time: time, | ||
// contains all messages that have been emitted before | ||
emittedMessagesIds: new _obliviousSet2['default'](options.node.ttl * 2), | ||
emittedMessagesIds: new ObliviousSet(options.node.ttl * 2), | ||
messagesCallbackTime: null, | ||
@@ -432,3 +427,3 @@ messagesCallback: null, | ||
*/ | ||
var handleMessagePing = exports.handleMessagePing = function () { | ||
var handleMessagePing = function () { | ||
var _ref12 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee10(state, msgObj) { | ||
@@ -516,3 +511,3 @@ var messages, useMessages; | ||
var refreshReaderClients = exports.refreshReaderClients = function () { | ||
var refreshReaderClients = function () { | ||
var _ref13 = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee13(channelState) { | ||
@@ -637,69 +632,27 @@ var _this = this; | ||
exports.cleanPipeName = cleanPipeName; | ||
exports.getPaths = getPaths; | ||
exports.socketPath = socketPath; | ||
exports.socketInfoPath = socketInfoPath; | ||
exports.createSocketInfoFile = createSocketInfoFile; | ||
exports.getSingleMessage = getSingleMessage; | ||
exports.readMessage = readMessage; | ||
exports._filterMessage = _filterMessage; | ||
exports.postMessage = postMessage; | ||
exports.emitOverFastPath = emitOverFastPath; | ||
exports.onMessage = onMessage; | ||
exports.close = close; | ||
exports.canBeUsed = canBeUsed; | ||
exports.averageResponseTime = averageResponseTime; | ||
exports.microSeconds = microSeconds; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _util = require('util'); | ||
/** | ||
* this method is used in nodejs-environments. | ||
* The ipc is handled via sockets and file-writes to the tmp-folder | ||
*/ | ||
var util = _interopRequireWildcard(_util); | ||
var util = require('util'); | ||
var fs = require('fs'); | ||
var os = require('os'); | ||
var events = require('events'); | ||
var net = require('net'); | ||
var path = require('path'); | ||
var micro = require('nano-time'); | ||
var _fs = require('fs'); | ||
var sha3_224 = require('js-sha3').sha3_224; | ||
var isNode = require('detect-node'); | ||
var unload = require('unload'); | ||
var fs = _interopRequireWildcard(_fs); | ||
var fillOptionsWithDefaults = require('../../dist/lib/options.js').fillOptionsWithDefaults; | ||
var ownUtil = require('../../dist/lib/util.js'); | ||
var randomInt = ownUtil.randomInt; | ||
var randomToken = ownUtil.randomToken; | ||
var ObliviousSet = require('../../dist/lib/oblivious-set')['default']; | ||
var _os = require('os'); | ||
var os = _interopRequireWildcard(_os); | ||
var _events = require('events'); | ||
var events = _interopRequireWildcard(_events); | ||
var _net = require('net'); | ||
var net = _interopRequireWildcard(_net); | ||
var _path = require('path'); | ||
var path = _interopRequireWildcard(_path); | ||
var _nanoTime = require('nano-time'); | ||
var _nanoTime2 = _interopRequireDefault(_nanoTime); | ||
var _jsSha = require('js-sha3'); | ||
var _detectNode = require('detect-node'); | ||
var _detectNode2 = _interopRequireDefault(_detectNode); | ||
var _options = require('../options'); | ||
var _util2 = require('../util'); | ||
var _obliviousSet = require('../oblivious-set'); | ||
var _obliviousSet2 = _interopRequireDefault(_obliviousSet); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var unload = require('unload'); /** | ||
* this method is used in nodejs-environments. | ||
* The ipc is handled via sockets and file-writes to the tmp-folder | ||
*/ | ||
/** | ||
@@ -732,3 +685,3 @@ * windows sucks, so we have handle windows-type of socket-paths | ||
var folderPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME); | ||
var channelPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME, (0, _jsSha.sha3_224)(channelName) // use hash incase of strange characters | ||
var channelPathBase = path.join(os.tmpdir(), TMP_FOLDER_NAME, sha3_224(channelName) // use hash incase of strange characters | ||
); | ||
@@ -775,3 +728,5 @@ var folderPathReaders = path.join(channelPathBase, 'readers'); | ||
}); | ||
}function getSingleMessage(channelName, msgObj) { | ||
} | ||
function getSingleMessage(channelName, msgObj) { | ||
var messagesPath = getPaths(channelName).messages; | ||
@@ -793,3 +748,3 @@ | ||
var type = exports.type = 'node'; | ||
var type = 'node'; | ||
@@ -805,3 +760,5 @@ function _filterMessage(msgObj, state) { | ||
return true; | ||
}function postMessage(channelState, messageJson) { | ||
} | ||
function postMessage(channelState, messageJson) { | ||
var _this2 = this; | ||
@@ -857,3 +814,3 @@ | ||
*/ | ||
if ((0, _util2.randomInt)(0, 20) === 0) { | ||
if (randomInt(0, 20) === 0) { | ||
/* await */getAllMessages(channelState.channelName).then(function (allMessages) { | ||
@@ -943,3 +900,3 @@ return cleanOldMessages(allMessages, channelState.options.node.ttl); | ||
function canBeUsed() { | ||
return _detectNode2['default']; | ||
return isNode; | ||
} | ||
@@ -952,3 +909,33 @@ | ||
function microSeconds() { | ||
return parseInt(_nanoTime2['default'].microseconds()); | ||
} | ||
return parseInt(micro.microseconds()); | ||
} | ||
module.exports = { | ||
cleanPipeName: cleanPipeName, | ||
getPaths: getPaths, | ||
ensureFoldersExist: ensureFoldersExist, | ||
socketPath: socketPath, | ||
socketInfoPath: socketInfoPath, | ||
createSocketInfoFile: createSocketInfoFile, | ||
createSocketEventEmitter: createSocketEventEmitter, | ||
openClientConnection: openClientConnection, | ||
writeMessage: writeMessage, | ||
getReadersUuids: getReadersUuids, | ||
messagePath: messagePath, | ||
getAllMessages: getAllMessages, | ||
getSingleMessage: getSingleMessage, | ||
readMessage: readMessage, | ||
cleanOldMessages: cleanOldMessages, | ||
type: type, | ||
create: create, | ||
_filterMessage: _filterMessage, | ||
handleMessagePing: handleMessagePing, | ||
refreshReaderClients: refreshReaderClients, | ||
postMessage: postMessage, | ||
emitOverFastPath: emitOverFastPath, | ||
onMessage: onMessage, | ||
close: close, | ||
canBeUsed: canBeUsed, | ||
averageResponseTime: averageResponseTime, | ||
microSeconds: microSeconds | ||
}; |
{ | ||
"name": "broadcast-channel", | ||
"version": "1.2.11", | ||
"version": "1.3.0", | ||
"description": "A BroadcastChannel implementation that works with new browsers, older browsers and Node.js", | ||
@@ -128,2 +128,3 @@ "homepage": "https://github.com/pubkey/broadcast-channel#readme", | ||
"browser": { | ||
"./src/methods/node.js": false, | ||
"./dist/es/methods/node.js": false, | ||
@@ -130,0 +131,0 @@ "./dist/lib/methods/node.js": false |
@@ -21,5 +21,10 @@ const isNode = require('detect-node'); | ||
if (isNode) { | ||
const NodeMethod = REQUIRE_FUN('./methods/node.js'); | ||
/** | ||
* we use the non-transpiled code for nodejs | ||
* because it runs faster | ||
*/ | ||
const NodeMethod = REQUIRE_FUN('../../src/methods/node.js'); | ||
/** | ||
* this will be false for webpackbuilds | ||
@@ -26,0 +31,0 @@ * which will shim the node-method with an empty object {} |
@@ -6,28 +6,21 @@ /** | ||
import * as util from 'util'; | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as events from 'events'; | ||
import * as net from 'net'; | ||
import * as path from 'path'; | ||
import micro from 'nano-time'; | ||
const util = require('util'); | ||
const fs = require('fs'); | ||
const os = require('os'); | ||
const events = require('events'); | ||
const net = require('net'); | ||
const path = require('path'); | ||
const micro = require('nano-time'); | ||
import { | ||
sha3_224 | ||
} from 'js-sha3'; | ||
import isNode from 'detect-node'; | ||
const sha3_224 = require('js-sha3').sha3_224; | ||
const isNode = require('detect-node'); | ||
const unload = require('unload'); | ||
import { | ||
fillOptionsWithDefaults | ||
} from '../options'; | ||
const fillOptionsWithDefaults = require('../../dist/lib/options.js').fillOptionsWithDefaults; | ||
const ownUtil = require('../../dist/lib/util.js'); | ||
const randomInt = ownUtil.randomInt; | ||
const randomToken = ownUtil.randomToken; | ||
const ObliviousSet = require('../../dist/lib/oblivious-set').default; | ||
import { | ||
randomInt, | ||
randomToken | ||
} from '../util'; | ||
import ObliviousSet from '../oblivious-set'; | ||
/** | ||
@@ -37,3 +30,3 @@ * windows sucks, so we have handle windows-type of socket-paths | ||
*/ | ||
export function cleanPipeName(str) { | ||
function cleanPipeName(str) { | ||
if ( | ||
@@ -61,3 +54,3 @@ process.platform === 'win32' && | ||
const getPathsCache = new Map(); | ||
export function getPaths(channelName) { | ||
function getPaths(channelName) { | ||
if (!getPathsCache.has(channelName)) { | ||
@@ -94,3 +87,3 @@ const folderPathBase = path.join( | ||
export async function ensureFoldersExist(channelName) { | ||
async function ensureFoldersExist(channelName) { | ||
const paths = getPaths(channelName); | ||
@@ -105,3 +98,3 @@ await mkdir(paths.base).catch(() => null); | ||
export function socketPath(channelName, readerUuid) { | ||
function socketPath(channelName, readerUuid) { | ||
@@ -116,3 +109,3 @@ const paths = getPaths(channelName); | ||
export function socketInfoPath(channelName, readerUuid) { | ||
function socketInfoPath(channelName, readerUuid) { | ||
const paths = getPaths(channelName); | ||
@@ -132,3 +125,3 @@ const socketPath = path.join( | ||
*/ | ||
export function createSocketInfoFile(channelName, readerUuid) { | ||
function createSocketInfoFile(channelName, readerUuid) { | ||
const pathToFile = socketInfoPath(channelName, readerUuid); | ||
@@ -147,3 +140,3 @@ return writeFile( | ||
*/ | ||
export async function createSocketEventEmitter(channelName, readerUuid) { | ||
async function createSocketEventEmitter(channelName, readerUuid) { | ||
const pathToSocket = socketPath(channelName, readerUuid); | ||
@@ -177,3 +170,3 @@ | ||
export async function openClientConnection(channelName, readerUuid) { | ||
async function openClientConnection(channelName, readerUuid) { | ||
const pathToSocket = socketPath(channelName, readerUuid); | ||
@@ -195,3 +188,3 @@ const client = new net.Socket(); | ||
*/ | ||
export async function writeMessage(channelName, readerUuid, messageJson) { | ||
async function writeMessage(channelName, readerUuid, messageJson) { | ||
const time = microSeconds(); | ||
@@ -229,3 +222,3 @@ const writeObject = { | ||
*/ | ||
export async function getReadersUuids(channelName) { | ||
async function getReadersUuids(channelName) { | ||
const readersPath = getPaths(channelName).readers; | ||
@@ -240,3 +233,3 @@ const files = await readdir(readersPath); | ||
export async function messagePath(channelName, time, token, writerUuid) { | ||
async function messagePath(channelName, time, token, writerUuid) { | ||
const fileName = time + '_' + writerUuid + '_' + token + '.json'; | ||
@@ -251,3 +244,3 @@ | ||
export async function getAllMessages(channelName) { | ||
async function getAllMessages(channelName) { | ||
const messagesPath = getPaths(channelName).messages; | ||
@@ -271,3 +264,3 @@ const files = await readdir(messagesPath); | ||
export function getSingleMessage(channelName, msgObj) { | ||
function getSingleMessage(channelName, msgObj) { | ||
const messagesPath = getPaths(channelName).messages; | ||
@@ -287,3 +280,3 @@ | ||
export function readMessage(messageObj) { | ||
function readMessage(messageObj) { | ||
return readFile(messageObj.path, 'utf8') | ||
@@ -293,3 +286,3 @@ .then(content => JSON.parse(content)); | ||
export async function cleanOldMessages(messageObjects, ttl) { | ||
async function cleanOldMessages(messageObjects, ttl) { | ||
const olderThen = Date.now() - ttl; | ||
@@ -305,5 +298,5 @@ await Promise.all( | ||
export const type = 'node'; | ||
const type = 'node'; | ||
export async function create(channelName, options = {}) { | ||
async function create(channelName, options = {}) { | ||
options = fillOptionsWithDefaults(options); | ||
@@ -366,3 +359,3 @@ const time = microSeconds(); | ||
export function _filterMessage(msgObj, state) { | ||
function _filterMessage(msgObj, state) { | ||
if (msgObj.senderUuid === state.uuid) return false; // not send by own | ||
@@ -382,3 +375,3 @@ if (state.emittedMessagesIds.has(msgObj.token)) return false; // not already emitted | ||
*/ | ||
export async function handleMessagePing(state, msgObj) { | ||
async function handleMessagePing(state, msgObj) { | ||
@@ -427,3 +420,3 @@ /** | ||
export async function refreshReaderClients(channelState) { | ||
async function refreshReaderClients(channelState) { | ||
// ensure we have subscribed to all readers | ||
@@ -459,3 +452,3 @@ const otherReaders = await getReadersUuids(channelState.channelName); | ||
export function postMessage(channelState, messageJson) { | ||
function postMessage(channelState, messageJson) { | ||
@@ -514,3 +507,3 @@ const writePromise = writeMessage( | ||
*/ | ||
export function emitOverFastPath(state, msgObj, messageJson) { | ||
function emitOverFastPath(state, msgObj, messageJson) { | ||
if (!state.options.node.useFastPath) return; // disabled | ||
@@ -533,3 +526,3 @@ const others = OTHER_INSTANCES[state.channelName].filter(s => s !== state); | ||
export function onMessage(channelState, fn, time = microSeconds()) { | ||
function onMessage(channelState, fn, time = microSeconds()) { | ||
channelState.messagesCallbackTime = time; | ||
@@ -540,3 +533,3 @@ channelState.messagesCallback = fn; | ||
export function close(channelState) { | ||
function close(channelState) { | ||
if (channelState.closed) return; | ||
@@ -566,12 +559,42 @@ channelState.closed = true; | ||
export function canBeUsed() { | ||
function canBeUsed() { | ||
return isNode; | ||
} | ||
export function averageResponseTime() { | ||
function averageResponseTime() { | ||
return 50; | ||
} | ||
export function microSeconds() { | ||
function microSeconds() { | ||
return parseInt(micro.microseconds()); | ||
} | ||
} | ||
module.exports = { | ||
cleanPipeName, | ||
getPaths, | ||
ensureFoldersExist, | ||
socketPath, | ||
socketInfoPath, | ||
createSocketInfoFile, | ||
createSocketEventEmitter, | ||
openClientConnection, | ||
writeMessage, | ||
getReadersUuids, | ||
messagePath, | ||
getAllMessages, | ||
getSingleMessage, | ||
readMessage, | ||
cleanOldMessages, | ||
type, | ||
create, | ||
_filterMessage, | ||
handleMessagePing, | ||
refreshReaderClients, | ||
postMessage, | ||
emitOverFastPath, | ||
onMessage, | ||
close, | ||
canBeUsed, | ||
averageResponseTime, | ||
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
200339
4888