
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Unenv is a framework agnostic system that allows converting JavaScript code to be platform agnostic and working in any environment including Browsers, Workers, Node.js or pure JavaScript runtime.
The 'unenv' npm package is designed to provide a universal environment for JavaScript and TypeScript projects. It allows developers to create isomorphic code that can run seamlessly in both Node.js and browser environments by providing polyfills and shims for various APIs.
Polyfills for Node.js APIs
This feature provides polyfills for Node.js APIs, allowing you to use Node.js-specific features in a browser environment. The code sample demonstrates how to use the Buffer API from Node.js in a universal environment.
const { Buffer } = require('unenv/runtime/polyfills/buffer');
const buffer = Buffer.from('Hello, world!');
console.log(buffer.toString('utf-8'));
Environment Detection
This feature allows you to detect the current environment (Node.js or browser) and execute code conditionally based on that. The code sample shows how to check if the code is running in Node.js or a browser.
const { isNode, isBrowser } = require('unenv');
if (isNode) {
console.log('Running in Node.js');
} else if (isBrowser) {
console.log('Running in the browser');
}
Universal Fetch API
This feature provides a universal Fetch API that works in both Node.js and browser environments. The code sample demonstrates how to make a fetch request and handle the response.
const fetch = require('unenv/runtime/polyfills/fetch');
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
The 'isomorphic-fetch' package provides a Fetch API implementation that works in both Node.js and browser environments. It is similar to the Fetch API polyfill provided by 'unenv', but it focuses solely on fetch functionality.
The 'universal-env' package helps in detecting the current environment (Node.js or browser) and provides utility functions for environment-specific code. It is similar to the environment detection feature of 'unenv'.
The 'node-polyfill-webpack-plugin' package provides polyfills for Node.js core modules in a Webpack environment. It is similar to the polyfills provided by 'unenv', but it is specifically designed for use with Webpack.
Unenv is a framework agnostic system that allows converting JavaScript code to be platform agnostic and working in any environment including Browsers, Workers, Node.js or pure JavaScript runtime.
# Using npm
npm i -D unenv
# Using yarn
yarn add --dev unenv
Using env
utility and builtin presets ( and nodeless), unenv will provide an abstract config that can be used in building pipelines (rollup.js, webpack, etc)
import { env, node, nodeless } from 'unenv'
const { alias, inject, polyfill, external } = env(...presets)
node
Suitable to convert universal libraries working in Node.js. (preset)
fetch
APInodeless
Using this preset, we can convert a code that is depending on Node.js to work anywhere else.
Unenv provides a replacement for all Node.js built-ins for cross-platform compatiblity.
Unenv provides a replacement for common npm packages for cross platform compatibility.
Package | Status | Source |
---|---|---|
npm/consola | Use native console | unenv/runtime/npm/consola |
npm/cross-fetch | Use native fetch | unenv/runtime/npm/cross-fetch |
npm/debug | Mocked with console.debug | unenv/runtime/npm/debug |
npm/fsevents | Mocked | unenv/runtime/npm/fsevents |
npm/inherits | Inlined | unenv/runtime/npm/inherits |
npm/mime-db | Minimized | unenv/runtime/npm/mime-db |
npm/mime | Minimized | unenv/runtime/npm/mime |
npm/node-fetch | Use native fetch | unenv/runtime/npm/node-fetch |
npm/whatwg-url | Use native URL | unenv/runtime/npm/whatwg-url |
import MockProxy from 'unenv/runtime/mock/proxy'
console.log(MockProxy().foo.bar()[0])
Above package doesn't works outside of Node.js and neither we need any platform specific logic! When aliasing os
to mock/proxy-cjs
, it will be auto mocked using a Proxy Object which can be recursively traversed like an Object
, called like a Function
, Iterated like an Array
, or instantiated like a Class
.
We use this proxy for auto mocking unimplemented internals. Imagine a packages does this:
const os = require('os')
if (os.platform() === 'windows') { /* do some fix */ }
module.exports = () => 'Hello world'
By aliasing os
to unenv/runtime/mock/proxy-cjs
, code will be compatible in other platforms.
Please check ./src/runtime to discover other polyfills.
MIT
FAQs
`unenv` is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.
The npm package unenv receives a total of 1,355,312 weekly downloads. As such, unenv popularity was classified as popular.
We found that unenv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.