🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis
Socket
Book a DemoInstallSign in
Socket

bippy

Package Overview
Dependencies
Maintainers
1
Versions
112
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bippy

a hacky way to get fibers from react

Source
npmnpm
Version
0.0.23
Version published
Weekly downloads
463K
-1.6%
Maintainers
1
Weekly downloads
 
Created
Source

bippy

a hacky way to get fibers from react. used internally for react-scan.

bippy works by setting a "fake" version of the __REACT_DEVTOOLS_GLOBAL_HOOK__ object. this gives us access to react internals without actually using react devtools.

[!WARNING] this project uses react internals, which can change at any time. this is not recommended for usage and may break production apps - unless you acknowledge this risk and know exactly you're doing.

example

here i wrote a getRenderInfo function, where you're able to pass any component identifier and get back the number of times it rendered, as well as the total and self time. this is done by traversing the fiber tree during a commit via onCommitFiberRoot.

inspect it live here.

import {
  instrument,
  createFiberVisitor,
  getTimings,
  getDisplayName,
} from 'bippy'; // must be imported BEFORE react
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';

const componentRenderMap = new WeakMap();

const visitor = createFiberVisitor({
  onRender(fiber, phase) {
    const componentType = fiber.elementType;
    if (
      typeof componentType !== 'function' &&
      (typeof componentType !== 'object' || !componentType)
    ) {
      return;
    }
    const render = componentRenderMap.get(componentType) || {
      count: 0,
      selfTime: 0,
      totalTime: 0,
      displayName: getDisplayName(componentType),
    };
    render.count++;
    const { selfTime, totalTime } = getTimings(fiber);
    render.selfTime += selfTime;
    render.totalTime += totalTime;
    componentRenderMap.set(componentType, render);
    console.log(phase, render);
  },
});

instrument({
  onCommitFiberRoot: (rendererID, fiberRoot) => {
    visitor(rendererID, fiberRoot);
  },
});

export const getRenderInfo = (componentType) => {
  return componentRenderMap.get(componentType);
};

function App() {
  const [count, setCount] = useState(0);
  const renderInfo = getRenderInfo(App);
  return (
    <button onClick={() => setCount(count + 1)}>
      rendered: {JSON.stringify(renderInfo, null, 2)}
    </button>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<App />);

misc

the original bippy character is owned and created by @dairyfreerice. this project is not related to the bippy brand, i just think the character is cute

Keywords

react

FAQs

Package last updated on 18 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts