You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@rsbuild/plugin-node-polyfill

Package Overview
Dependencies
Maintainers
0
Versions
180
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rsbuild/plugin-node-polyfill

An Rsbuild plugin to automatically inject polyfills for [Node.js builtin modules](https://nodejs.org/api/modules.html#built-in-modules) into the browser side.


Version published
Weekly downloads
15K
increased by50.6%
Maintainers
0
Created
Weekly downloads
 

Readme

Source

@rsbuild/plugin-node-polyfill

An Rsbuild plugin to automatically inject polyfills for Node.js builtin modules into the browser side.

npm version license

When to use

Normally, we don't need to use Node builtin modules on the browser side. However, it is possible to use some Node builtin modules when the code will run on both the Node side and the browser side, and this plugin provides browser versions of polyfills for these Node builtin modules.

By using the Node Polyfill plugin, polyfills for Node builtin modules are automatically injected into the browser-side, allowing you to use these modules on the browser side with confidence.

Usage

Install:

npm add @rsbuild/plugin-node-polyfill -D

Add plugin to your rsbuild.config.ts:

// rsbuild.config.ts
import { pluginNodePolyfill } from "@rsbuild/plugin-node-polyfill";

export default {
  plugins: [pluginNodePolyfill()],
};

Node Polyfills

Globals

  • Buffer
  • process

When you use the above global variables in your code, the corresponding polyfill will be automatically injected.

For instance, the following code would inject the Buffer polyfill:

const bufferData = Buffer.from("abc");

You can disable this behavior through the globals option of the plugin:

pluginNodePolyfill({
  globals: {
    Buffer: false,
    process: false,
  },
});

Modules

  • assert
  • buffer
  • console
  • constants
  • crypto
  • domain
  • events
  • http
  • https
  • os
  • path
  • punycode
  • process
  • querystring
  • stream
  • _stream_duplex
  • _stream_passthrough
  • _stream_readable
  • _stream_transform
  • _stream_writable
  • string_decoder
  • sys
  • timers
  • tty
  • url
  • util
  • vm
  • zlib

When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.

import { Buffer } from "buffer";

const bufferData = Buffer.from("abc");

Fallbacks

  • child_process
  • cluster
  • dgram
  • dns
  • fs
  • module
  • net
  • readline
  • repl
  • tls

Currently there is no polyfill for the above modules on the browser side, so when you import the above modules, it will automatically fallback to an empty object.

import fs from "fs";

console.log(fs); // -> {}

Options

globals

Used to specify whether to inject polyfills for global variables.

  • Type:
type Globals = {
  process?: boolean;
  Buffer?: boolean;
};
  • Default:
const defaultGlobals = {
  Buffer: true,
  process: true,
};

protocolImports

Whether to polyfill Node.js builtin modules starting with node:.

  • Type: boolean
  • Default: true

For example, if you disable protocolImports, modules such as node:path, node:http, etc. will not be polyfilled.

pluginNodePolyfill({
  protocolImports: false,
});

License

MIT.

FAQs

Package last updated on 14 Jul 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc