Merge pull request #3984 from whymarrh/3941-jsdoc

Add a few docblocks to background files
feature/default_network_editable
Dan Finlay 7 years ago committed by GitHub
commit 4544d57f26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      app/scripts/lib/createLoggerMiddleware.js
  2. 12
      app/scripts/lib/createOriginMiddleware.js
  3. 25
      app/scripts/lib/events-proxy.js
  4. 7
      app/scripts/lib/hex-to-bn.js
  5. 37
      app/scripts/lib/local-store.js
  6. 35
      app/scripts/lib/migrator/index.js
  7. 18
      app/scripts/lib/stream-utils.js
  8. 27
      app/scripts/platforms/sw.js
  9. 23
      app/scripts/platforms/window.js

@ -1,16 +1,20 @@
const log = require('loglevel') const log = require('loglevel')
// log rpc activity
module.exports = createLoggerMiddleware module.exports = createLoggerMiddleware
function createLoggerMiddleware ({ origin }) { /**
return function loggerMiddleware (req, res, next, end) { * Returns a middleware that logs RPC activity
next((cb) => { * @param {{ origin: string }} opts - The middleware options
* @returns {Function}
*/
function createLoggerMiddleware (opts) {
return function loggerMiddleware (/** @type {any} */ req, /** @type {any} */ res, /** @type {Function} */ next) {
next((/** @type {Function} */ cb) => {
if (res.error) { if (res.error) {
log.error('Error in RPC response:\n', res) log.error('Error in RPC response:\n', res)
} }
if (req.isMetamaskInternal) return if (req.isMetamaskInternal) return
log.info(`RPC (${origin}):`, req, '->', res) log.info(`RPC (${opts.origin}):`, req, '->', res)
cb() cb()
}) })
} }

@ -1,9 +1,13 @@
// append dapp origin domain to request
module.exports = createOriginMiddleware module.exports = createOriginMiddleware
function createOriginMiddleware ({ origin }) { /**
return function originMiddleware (req, res, next, end) { * Returns a middleware that appends the DApp origin to request
req.origin = origin * @param {{ origin: string }} opts - The middleware options
* @returns {Function}
*/
function createOriginMiddleware (opts) {
return function originMiddleware (/** @type {any} */ req, /** @type {any} */ _, /** @type {Function} */ next) {
req.origin = opts.origin
next() next()
} }
} }

@ -1,26 +1,37 @@
/**
* Returns an EventEmitter that proxies events from the given event emitter
* @param {any} eventEmitter
* @param {object} listeners - The listeners to proxy to
* @returns {any}
*/
module.exports = function createEventEmitterProxy (eventEmitter, listeners) { module.exports = function createEventEmitterProxy (eventEmitter, listeners) {
let target = eventEmitter let target = eventEmitter
const eventHandlers = listeners || {} const eventHandlers = listeners || {}
const proxy = new Proxy({}, { const proxy = /** @type {any} */ (new Proxy({}, {
get: (obj, name) => { get: (_, name) => {
// intercept listeners // intercept listeners
if (name === 'on') return addListener if (name === 'on') return addListener
if (name === 'setTarget') return setTarget if (name === 'setTarget') return setTarget
if (name === 'proxyEventHandlers') return eventHandlers if (name === 'proxyEventHandlers') return eventHandlers
return target[name] return (/** @type {any} */ (target))[name]
}, },
set: (obj, name, value) => { set: (_, name, value) => {
target[name] = value target[name] = value
return true return true
}, },
}) }))
function setTarget (eventEmitter) { function setTarget (/** @type {EventEmitter} */ eventEmitter) {
target = eventEmitter target = eventEmitter
// migrate listeners // migrate listeners
Object.keys(eventHandlers).forEach((name) => { Object.keys(eventHandlers).forEach((name) => {
eventHandlers[name].forEach((handler) => target.on(name, handler)) /** @type {Array<Function>} */ (eventHandlers[name]).forEach((handler) => target.on(name, handler))
}) })
} }
/**
* Attaches a function to be called whenever the specified event is emitted
* @param {string} name
* @param {Function} handler
*/
function addListener (name, handler) { function addListener (name, handler) {
if (!eventHandlers[name]) eventHandlers[name] = [] if (!eventHandlers[name]) eventHandlers[name] = []
eventHandlers[name].push(handler) eventHandlers[name].push(handler)

@ -1,6 +1,11 @@
const ethUtil = require('ethereumjs-util') const ethUtil = (/** @type {object} */ (require('ethereumjs-util')))
const BN = ethUtil.BN const BN = ethUtil.BN
/**
* Returns a [BinaryNumber]{@link BN} representation of the given hex value
* @param {string} hex
* @return {any}
*/
module.exports = function hexToBn (hex) { module.exports = function hexToBn (hex) {
return new BN(ethUtil.stripHexPrefix(hex), 16) return new BN(ethUtil.stripHexPrefix(hex), 16)
} }

@ -1,11 +1,13 @@
// We should not rely on local storage in an extension!
// We should use this instead!
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/storage/local
const extension = require('extensionizer') const extension = require('extensionizer')
const log = require('loglevel') const log = require('loglevel')
/**
* A wrapper around the extension's storage local API
*/
module.exports = class ExtensionStore { module.exports = class ExtensionStore {
/**
* @constructor
*/
constructor() { constructor() {
this.isSupported = !!(extension.storage.local) this.isSupported = !!(extension.storage.local)
if (!this.isSupported) { if (!this.isSupported) {
@ -13,6 +15,10 @@ module.exports = class ExtensionStore {
} }
} }
/**
* Returns all of the keys currently saved
* @return {Promise<*>}
*/
async get() { async get() {
if (!this.isSupported) return undefined if (!this.isSupported) return undefined
const result = await this._get() const result = await this._get()
@ -25,14 +31,24 @@ module.exports = class ExtensionStore {
} }
} }
/**
* Sets the key in local state
* @param {object} state - The state to set
* @return {Promise<void>}
*/
async set(state) { async set(state) {
return this._set(state) return this._set(state)
} }
/**
* Returns all of the keys currently saved
* @private
* @return {object} the key-value map from local storage
*/
_get() { _get() {
const local = extension.storage.local const local = extension.storage.local
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
local.get(null, (result) => { local.get(null, (/** @type {any} */ result) => {
const err = extension.runtime.lastError const err = extension.runtime.lastError
if (err) { if (err) {
reject(err) reject(err)
@ -43,6 +59,12 @@ module.exports = class ExtensionStore {
}) })
} }
/**
* Sets the key in local state
* @param {object} obj - The key to set
* @return {Promise<void>}
* @private
*/
_set(obj) { _set(obj) {
const local = extension.storage.local const local = extension.storage.local
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -58,6 +80,11 @@ module.exports = class ExtensionStore {
} }
} }
/**
* Returns whether or not the given object contains no keys
* @param {object} obj - The object to check
* @returns {boolean}
*/
function isEmpty(obj) { function isEmpty(obj) {
return Object.keys(obj).length === 0 return Object.keys(obj).length === 0
} }

@ -1,7 +1,23 @@
const EventEmitter = require('events') const EventEmitter = require('events')
/**
* @typedef {object} Migration
* @property {number} version - The migration version
* @property {Function} migrate - Returns a promise of the migrated data
*/
/**
* @typedef {object} MigratorOptions
* @property {Array<Migration>} [migrations] - The list of migrations to apply
* @property {number} [defaultVersion] - The version to use in the initial state
*/
class Migrator extends EventEmitter { class Migrator extends EventEmitter {
/**
* @constructor
* @param {MigratorOptions} opts
*/
constructor (opts = {}) { constructor (opts = {}) {
super() super()
const migrations = opts.migrations || [] const migrations = opts.migrations || []
@ -42,19 +58,30 @@ class Migrator extends EventEmitter {
return versionedData return versionedData
// migration is "pending" if it has a higher /**
// version number than currentVersion * Returns whether or not the migration is pending
*
* A migration is considered "pending" if it has a higher
* version number than the current version.
* @param {Migration} migration
* @returns {boolean}
*/
function migrationIsPending (migration) { function migrationIsPending (migration) {
return migration.version > versionedData.meta.version return migration.version > versionedData.meta.version
} }
} }
generateInitialState (initState) { /**
* Returns the initial state for the migrator
* @param {object} [data] - The data for the initial state
* @returns {{meta: {version: number}, data: any}}
*/
generateInitialState (data) {
return { return {
meta: { meta: {
version: this.defaultVersion, version: this.defaultVersion,
}, },
data: initState, data,
} }
} }

@ -8,20 +8,34 @@ module.exports = {
setupMultiplex: setupMultiplex, setupMultiplex: setupMultiplex,
} }
/**
* Returns a stream transform that parses JSON strings passing through
* @return {stream.Transform}
*/
function jsonParseStream () { function jsonParseStream () {
return Through.obj(function (serialized, encoding, cb) { return Through.obj(function (serialized, _, cb) {
this.push(JSON.parse(serialized)) this.push(JSON.parse(serialized))
cb() cb()
}) })
} }
/**
* Returns a stream transform that calls {@code JSON.stringify}
* on objects passing through
* @return {stream.Transform} the stream transform
*/
function jsonStringifyStream () { function jsonStringifyStream () {
return Through.obj(function (obj, encoding, cb) { return Through.obj(function (obj, _, cb) {
this.push(JSON.stringify(obj)) this.push(JSON.stringify(obj))
cb() cb()
}) })
} }
/**
* Sets up stream multiplexing for the given stream
* @param {any} connectionStream - the stream to mux
* @return {stream.Stream} the multiplexed stream
*/
function setupMultiplex (connectionStream) { function setupMultiplex (connectionStream) {
const mux = new ObjectMultiplex() const mux = new ObjectMultiplex()
pump( pump(

@ -1,20 +1,25 @@
class SwPlatform { class SwPlatform {
/**
// * Reloads the platform
// Public */
//
reload () { reload () {
// you cant actually do this // TODO: you can't actually do this
global.location.reload() /** @type {any} */ (global).location.reload()
} }
openWindow ({ url }) { /**
// this doesnt actually work * Opens a window
global.open(url, '_blank') * @param {{url: string}} opts - The window options
*/
openWindow (opts) {
// TODO: this doesn't actually work
/** @type {any} */ (global).open(opts.url, '_blank')
} }
/**
* Returns the platform version
* @returns {string}
*/
getVersion () { getVersion () {
return '<unable to read version>' return '<unable to read version>'
} }

@ -1,18 +1,23 @@
class WindowPlatform { class WindowPlatform {
/**
// * Reload the platform
// Public */
//
reload () { reload () {
global.location.reload() /** @type {any} */ (global).location.reload()
} }
openWindow ({ url }) { /**
global.open(url, '_blank') * Opens a window
* @param {{url: string}} opts - The window options
*/
openWindow (opts) {
/** @type {any} */ (global).open(opts.url, '_blank')
} }
/**
* Returns the platform version
* @returns {string}
*/
getVersion () { getVersion () {
return '<unable to read version>' return '<unable to read version>'
} }

Loading…
Cancel
Save