Skip to content

Commit

Permalink
refactor: Tree-shake lodash (#1101)
Browse files Browse the repository at this point in the history
Reduces client browser bundle side:
- before: 4078 kB
- after: 4010 kB
  • Loading branch information
teogeb committed Feb 27, 2023
1 parent 4fdb594 commit 26f3102
Show file tree
Hide file tree
Showing 45 changed files with 61 additions and 54 deletions.
3 changes: 2 additions & 1 deletion packages/broker/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { StreamrClientConfig } from 'streamr-client'
import path from 'path'
import * as os from 'os'
import { camelCase, set } from 'lodash'
import camelCase from 'lodash/camelCase'
import set from 'lodash/set'

export interface Config {
client?: StreamrClientConfig
Expand Down
2 changes: 1 addition & 1 deletion packages/broker/src/config/migration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs'
import path from 'path'
import { cloneDeep } from 'lodash'
import cloneDeep from 'lodash/cloneDeep'
import { ConfigFile, getDefaultFile, getLegacyDefaultFile } from './config'

export const CURRENT_CONFIGURATION_VERSION = 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema } from 'ajv'
import { Plugin } from '../../Plugin'
import PLUGIN_CONFIG_SCHEMA from './config.schema.json'
import { Logger, MetricsReport } from '@streamr/utils'
import { omit } from 'lodash'
import omit from 'lodash/omit'

const logger = new Logger(module)

Expand Down
6 changes: 3 additions & 3 deletions packages/broker/src/plugins/mqtt/Bridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from 'lodash'
import without from 'lodash/without'
import { MessageMetadata, StreamrClient, Subscription } from 'streamr-client'
import { StreamPartIDUtils } from '@streamr/protocol'
import { Logger } from '@streamr/utils'
Expand Down Expand Up @@ -105,10 +105,10 @@ export class Bridge implements MqttServerListener {
const streamId = this.getStreamId(topic)
const existingSubscription = this.getSubscription(streamId)
if (existingSubscription !== undefined) {
existingSubscription.clientIds = _.without(existingSubscription.clientIds, clientId)
existingSubscription.clientIds = without(existingSubscription.clientIds, clientId)
if (existingSubscription.clientIds.length === 0) {
existingSubscription.streamrClientSubscription.unsubscribe()
this.subscriptions = _.without(this.subscriptions, existingSubscription)
this.subscriptions = without(this.subscriptions, existingSubscription)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { Broker } from '../../src/broker'
import { startBroker, createClient, createTestStream, startTestTracker } from '../utils'
import { fastPrivateKey } from '@streamr/test-utils'
import { wait, waitForEvent, waitForCondition } from '@streamr/utils'
import { range, sample } from 'lodash'
import sample from 'lodash/sample'
import range from 'lodash/range'

const MESSAGE_COUNT = 120
const trackerPort = 13610
Expand Down
2 changes: 1 addition & 1 deletion packages/broker/test/unit/configMigration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { merge } from 'lodash'
import merge from 'lodash/merge'
import { validateConfig as validateClientConfig } from 'streamr-client'
import { createMigratedConfig, CURRENT_CONFIGURATION_VERSION, formSchemaUrl, needsMigration } from '../../src/config/migration'
import BROKER_CONFIG_SCHEMA from '../../src/config/config.schema.json'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StorageConfig } from '../../../../src/plugins/storage/StorageConfig'
import { StorageNodeAssignmentEvent, Stream, StreamrClient, StreamrClientEvents } from 'streamr-client'
import { StreamPartID, StreamPartIDUtils, toStreamID, toStreamPartID } from '@streamr/protocol'
import { EthereumAddress, toEthereumAddress, wait } from '@streamr/utils'
import { range } from 'lodash'
import range from 'lodash/range'

const { parse } = StreamPartIDUtils

Expand Down
4 changes: 2 additions & 2 deletions packages/broker/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import StreamrClient, {
StreamMetadata,
StreamrClientConfig
} from 'streamr-client'
import _ from 'lodash'
import padEnd from 'lodash/padEnd'
import { Wallet } from 'ethers'
import { Tracker, startTracker } from '@streamr/network-tracker'
import { Broker, createBroker } from '../src/broker'
Expand Down Expand Up @@ -104,7 +104,7 @@ export const startBroker = async (testConfig: TestConfig): Promise<Broker> => {
}

export const createEthereumAddress = (id: number): EthereumAddress => {
return toEthereumAddress('0x' + _.padEnd(String(id), 40, '0'))
return toEthereumAddress('0x' + padEnd(String(id), 40, '0'))
}

export const createClient = async (
Expand Down
3 changes: 2 additions & 1 deletion packages/cli-tools/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { merge, omit } from 'lodash'
import omit from 'lodash/omit'
import merge from 'lodash/merge'
import { StreamrClientConfig, StreamrClient, CONFIG_TEST } from 'streamr-client'
import { GlobalCommandLineArgs } from './common'
import { getConfig } from './config'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/Stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StreamPartID,
toStreamPartID
} from '@streamr/protocol'
import { range } from 'lodash'
import range from 'lodash/range'
import { StrictStreamrClientConfig } from './Config'
import { PermissionAssignment, PublicPermissionQuery, UserPermissionQuery } from './permission'
import { Subscriber } from './subscribe/Subscriber'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/StreamrClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { EthereumAddress, toEthereumAddress } from '@streamr/utils'
import { LoggerFactory } from './utils/LoggerFactory'
import { convertStreamMessageToMessage, Message } from './Message'
import { ErrorCode } from './HttpUtil'
import { omit } from 'lodash'
import omit from 'lodash/omit'
import { StreamrClientError } from './StreamrClientError'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/encryption/PublisherKeyExchange.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { without } from 'lodash'
import without from 'lodash/without'
import {
EncryptedGroupKey,
EncryptionType,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/publish/MessageFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { random } from 'lodash'
import random from 'lodash/random'
import {
createSignaturePayload,
EncryptedGroupKey,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/publish/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StreamIDBuilder } from '../StreamIDBuilder'
import { Authentication, AuthenticationInjectionToken } from '../Authentication'
import { NetworkNodeFacade } from '../NetworkNodeFacade'
import { MessageFactory } from './MessageFactory'
import { isString } from 'lodash'
import isString from 'lodash/isString'
import { StreamRegistryCached } from '../registry/StreamRegistryCached'
import { GroupKeyQueue } from './GroupKeyQueue'
import { Mapping } from '../utils/Mapping'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/registry/StreamStorageRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { EthereumAddress, Logger, toEthereumAddress } from '@streamr/utils'
import { LoggerFactory } from '../utils/LoggerFactory'
import { StreamFactory } from '../StreamFactory'
import { collect } from '../utils/iterators'
import { min } from 'lodash'
import min from 'lodash/min'

export interface StorageNodeAssignmentEvent {
readonly streamId: StreamID
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/registry/trackerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StrictStreamrClientConfig, TrackerRegistryContract } from '../Config'
import { getMainnetProviders } from '../Ethereum'
import * as trackerRegistryConfig from '../ethereumArtifacts/TrackerRegistry.json'
import { tryInSequence } from '../utils/promises'
import { shuffle } from 'lodash'
import shuffle from 'lodash/shuffle'

async function fetchTrackers(contractAddress: EthereumAddress, jsonRpcProvider: Provider) {
// check that provider is connected and has some valid blockNumber
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/subscribe/MessageStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { PushPipeline } from '../utils/PushPipeline'
import { StreamMessage } from '@streamr/protocol'
import * as G from '../utils/GeneratorUtils'
import { convertStreamMessageToMessage, Message, MessageMetadata } from './../Message'
import { omit } from 'lodash'
import omit from 'lodash/omit'

export type MessageListener = (content: unknown, metadata: MessageMetadata) => unknown | Promise<unknown>

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/subscribe/Resends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MessageStream } from './MessageStream'
import { createSubscribePipeline } from './subscribePipeline'

import { StorageNodeRegistry } from '../registry/StorageNodeRegistry'
import { random } from 'lodash'
import random from 'lodash/random'
import { ConfigInjectionToken, StrictStreamrClientConfig } from '../Config'
import { HttpUtil } from '../HttpUtil'
import { StreamStorageRegistry } from '../registry/StreamStorageRegistry'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NameDirectory } from '@streamr/network-node'
import pLimit from 'p-limit'
import { LoggerFactory } from './LoggerFactory'
import { tryInSequence } from './promises'
import { shuffle } from 'lodash'
import shuffle from 'lodash/shuffle'

export interface ContractEvent {
onMethodExecute: (methodName: string) => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { get, set, createStore, UseStore } from 'idb-keyval'
import { Persistence } from './Persistence'
import { StreamID } from '@streamr/protocol'
import { memoize } from 'lodash'
import memoize from 'lodash/memoize'

export default class BrowserPersistence implements Persistence<string, string> {
private getStore: (streamId: StreamID) => UseStore
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/utils/waitForAssignmentsToPropagate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { collect, unique } from './GeneratorUtils'
import { StreamID, StreamMessage, StreamPartIDUtils } from '@streamr/protocol'
import { identity } from 'lodash'
import identity from 'lodash/identity'
import { MessageStream } from '../subscribe/MessageStream'

export function waitForAssignmentsToPropagate(
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/end-to-end/resend.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fastPrivateKey, fetchPrivateKeyWithGas } from '@streamr/test-utils'
import { createTestStream } from '../test-utils/utils'
import { range } from 'lodash'
import range from 'lodash/range'
import { CONFIG_TEST, DOCKER_DEV_STORAGE_NODE } from '../../src/ConfigTest'
import { wait, waitForCondition } from '@streamr/utils'
import { StreamrClient } from '../../src/StreamrClient'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'reflect-metadata'

import { Wallet } from '@ethersproject/wallet'
import { wait } from '@streamr/utils'
import { range } from 'lodash'
import range from 'lodash/range'
import { StreamMessageType } from '@streamr/protocol'
import { fastWallet } from '@streamr/test-utils'
import { createPrivateKeyAuthentication } from '../../src/Authentication'
Expand Down
3 changes: 2 additions & 1 deletion packages/client/test/integration/parallel-publish.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'reflect-metadata'

import { wait } from '@streamr/utils'
import { random, uniq } from 'lodash'
import random from 'lodash/random'
import uniq from 'lodash/uniq'
import { fastWallet } from '@streamr/test-utils'
import { Stream } from '../../src/Stream'
import { StreamrClient } from '../../src/StreamrClient'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/integration/unsubscribe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'reflect-metadata'
import { Wallet } from '@ethersproject/wallet'
import { StreamMessage } from '@streamr/protocol'
import { fastWallet } from '@streamr/test-utils'
import { range } from 'lodash'
import range from 'lodash/range'
import { Message } from '../../src/Message'
import { StreamPermission } from '../../src/permission'
import { Stream } from '../../src/Stream'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/test-utils/customMatchers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@jest/globals'
import { printExpected, printReceived } from 'jest-matcher-utils'
import { isFunction } from 'lodash'
import isFunction from 'lodash/isFunction'
import { StreamrClientError, StreamrClientErrorCode } from './../../src/StreamrClientError'

interface ExpectationResult {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/test-utils/fake/FakeEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { container, DependencyContainer } from 'tsyringe'
import { merge } from 'lodash'
import merge from 'lodash/merge'
import { fastPrivateKey, fastWallet } from '@streamr/test-utils'
import { StreamrClientConfig } from '../../../src/Config'
import { StorageNodeRegistry } from '../../../src/registry/StorageNodeRegistry'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/test-utils/fake/FakeNetworkNode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Lifecycle, scoped } from 'tsyringe'
import { pull } from 'lodash'
import pull from 'lodash/pull'
import { ProxyDirection, StreamMessage, StreamPartID } from '@streamr/protocol'
import { MetricsContext } from '@streamr/utils'
import { NodeId, NetworkNodeOptions } from '@streamr/network-node'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/test-utils/fake/FakeStorageNode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { range } from 'lodash'
import range from 'lodash/range'
import {
StreamID,
StreamMessage,
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/GroupKeyStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { addAfterFn } from '../test-utils/jest-utils'
import LeakDetector from 'jest-leak-detector' // requires weak-napi
import { StreamID, toStreamID } from '@streamr/protocol'
import { randomEthereumAddress } from '@streamr/test-utils'
import { range } from 'lodash'
import range from 'lodash/range'
import { EthereumAddress } from '@streamr/utils'

describe('GroupKeyStore', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/HttpUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'reflect-metadata'

import { once } from 'events'
import express from 'express'
import { range } from 'lodash'
import range from 'lodash/range'
import { StreamPartIDUtils } from '@streamr/protocol'
import { fastWallet } from '@streamr/test-utils'
import { HttpUtil } from '../../src/HttpUtil'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/MessageFactory.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { keyToArrayIndex, toEthereumAddress } from '@streamr/utils'
import { random } from 'lodash'
import random from 'lodash/random'
import { ContentType, EncryptionType, MAX_PARTITION_COUNT, StreamMessage, StreamMessageType, toStreamID } from '@streamr/protocol'
import { fastWallet } from '@streamr/test-utils'
import { createPrivateKeyAuthentication } from '../../src/Authentication'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/MessageStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MessageStream } from '../../src/subscribe/MessageStream'
import { Msg } from '../test-utils/publish'
import { createRandomAuthentication, waitForCalls } from '../test-utils/utils'
import { convertStreamMessageToMessage } from './../../src/Message'
import { omit } from 'lodash'
import omit from 'lodash/omit'

const PUBLISHER_ID = toEthereumAddress('0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')

Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/StreamrClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata'

import { merge } from 'lodash'
import merge from 'lodash/merge'
import { container } from 'tsyringe'
import { StreamrClientConfig } from '../../src/Config'
import { CONFIG_TEST } from '../../src/ConfigTest'
Expand Down
2 changes: 1 addition & 1 deletion packages/client/test/unit/contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { wait } from '@streamr/utils'
import { range } from 'lodash'
import range from 'lodash/range'
import { createDecoratedContract, ObservableContract } from '../../src/utils/contract'
import { mockLoggerFactory } from '../test-utils/utils'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
toStreamID,
toStreamPartID
} from '@streamr/protocol'
import { range, shuffle } from 'lodash'
import range from 'lodash/range'
import shuffle from 'lodash/shuffle'
import { wait } from '@streamr/utils'
import { createSignedMessage } from '../../src/publish/MessageFactory'
import { createRandomAuthentication } from '../test-utils/utils'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash'
import debounce from 'lodash/debounce'
import type { DebouncedFunc } from 'lodash'
import { StreamPartID } from '@streamr/protocol'
import { NodeId } from '@streamr/network-node'
import { Logger, MetricsContext, MetricsDefinition, Metric, RateMetric } from '@streamr/utils'
Expand Down Expand Up @@ -43,10 +44,10 @@ function isInstruction(entry: Instruction | StatusAck): entry is Instruction {

class Buffer {
private readonly entries = new Map<NodeId, Instruction | StatusAck>()
private readonly debouncedOnReady: _.DebouncedFunc<() => void>
private readonly debouncedOnReady: DebouncedFunc<() => void>

constructor(options: TopologyStabilizationOptions, onReady: () => void) {
this.debouncedOnReady = _.debounce(onReady, options.debounceWait, {
this.debouncedOnReady = debounce(onReady, options.debounceWait, {
maxWait: options.maxWait
})
}
Expand Down
4 changes: 2 additions & 2 deletions packages/network-tracker/src/logic/OverlayTopology.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NodeId } from '@streamr/network-node'
import _ from 'lodash'
import uniq from 'lodash/uniq'

// From: https://gist.github.com/guilhermepontes/17ae0cc71fa2b13ea8c20c94c5c35dc4
const shuffleArray = <T>(arr: Array<T>): Array<T> => arr
Expand Down Expand Up @@ -50,7 +50,7 @@ export class OverlayTopology {
}

update(nodeId: NodeId, neighbors: NodeId[]): void {
const newNeighbors = _.uniq(neighbors)
const newNeighbors = uniq(neighbors)
.filter((n) => n in this.nodes)
.filter((n) => n !== nodeId) // in case nodeId is reporting itself as neighbor

Expand Down
4 changes: 2 additions & 2 deletions packages/network/src/logic/StreamPartManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { StreamPartStatus } from '../identifiers'
import { DuplicateMessageDetector, NumberPair } from './DuplicateMessageDetector'
import { NodeId } from '../identifiers'
import { COUNTER_UNSUBSCRIBE } from '../constants'
import _ from 'lodash'
import uniq from 'lodash/uniq'

interface StreamPartState {
detectors: Map<string, DuplicateMessageDetector> // "publisherId-msgChainId" => DuplicateMessageDetector
Expand Down Expand Up @@ -176,7 +176,7 @@ export class StreamPartManager {
this.streamParts.forEach(({ neighbors }) => {
nodes.push(...neighbors)
})
return _.uniq(nodes)
return uniq(nodes)
}

hasNeighbor(streamPartId: StreamPartID, node: NodeId): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@streamr/protocol'
import { Logger, wait, waitForEvent, withTimeout } from "@streamr/utils"
import { Propagation } from '../propagation/Propagation'
import { sampleSize } from 'lodash'
import sampleSize from 'lodash/sampleSize'
import { EventEmitter } from "events"

const logger = new Logger(module)
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/Defer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { once } from 'lodash'
import once from 'lodash/once'

type ResolveFn<T> = (value: T | PromiseLike<T>) => void

Expand Down
Loading

0 comments on commit 26f3102

Please sign in to comment.