npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webcontainer/env

v1.1.1

Published

Set of environment utilities for WebContainers

Downloads

13,541

Readme

@webcontainer/env

⚠️ Important ⚠️

We strongly recommend that you update to version 1.1.0 or greater as we recently introduced a breaking change in WebContainer, making older versions of @webcontainer/api generate invalid HostURL.href and HostURL.hostname when those methods are called inside a WebContainer.

Set of environment utilities for WebContainers.

Install

$ npm install @webcontainer/env

API

isWebContainer(): boolean (method)

Returns a boolean indicating whether the program runs in a WebContainer.

HostURL (class)

The HostURL class represents a host specific URL. It can be used to parse a regular URL, such as http://localhost:1234, into a HostURL. Only if the program is executed in a WebContainer, the hostname is resolved to a WebContainer hostname, e.g. http://blitz--1234.local.webcontainer.io. This can be useful to create platform-dependent OAuth callback URLs.

HostURL.port(): string (getter)

HostURL.hash(): string (getter)

HostURL.host(): string (getter)

HostURL.hostname(): string (getter)

HostURL.href(): string (getter)

HostURL.origin(): string (getter)

HostURL.username(): string (getter)

HostURL.password(): string (getter)

HostURL.pathname(): string (getter)

HostURL.protocol(): string (getter)

HostURL.search(): string (getter)

HostURL.searchParams(): URLSearchParams (getter)

HostURL.parse(url: string | URL): HostURL (static method)

Parses a url into a HostURL. On local this is a no-op but when running in a WebContainer it resolves localhost to a WebContainer hostname.

Example

import { HostURL, isWebContainer } from '@webcontainer/env';

const hostURL = HostURL.parse('http://localhost:1234');

/**
 * Note that this branching would not be necessary as the host URL gets parsed
 * and resolved automatically through `HostURL.parse()` (see above). So `href`
 * will return a different value depending on the environment. This is illustrated
 * with the following `if` statement.
 */
if (isWebContainer()) {
  console.log(hostURL.href); // http://blitz--1234.local.webcontainer.io
} else {
  console.log(hostURL.href); // http://localhost:1234
}

HostURL.update(change: Partial<UpdateableURLProperties>): HostURL (method)

Updates the HostURL.

change

Object containing the URL changes. Note that some properties are immutable (read-only), e.g. origin or searchParams. When updating the port it automatically updates the host.

Type: Partial<UpdateableURLProperties>

interface UpdateableURLProperties {
  hash: string;
  host: string;
  hostname: string;
  href: string;
  password: string;
  pathname: string;
  port: string;
  protocol: string;
  search: string;
  username: string;
}

HostURL.toString(): string (method)

Stringifies the HostURL. It is effectively a read-only version of HostURL.href.

HostURL.toJSON(): string (method)

Returns a string containing a serialized version of the HostURL.