CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-mlly

Missing ECMAScript module utils for Node.js

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

utility-functions.mddocs/

Utility Functions

Core utilities for path/URL conversion, protocol handling, URI sanitization, and Node.js built-in detection. Provides cross-platform file system path normalization and safe URL handling.

Capabilities

File URL to Path

Converts a file URL to a local file system path with normalized slashes.

/**
 * Converts a file URL to a local file system path with normalized slashes
 * @param id - The file URL or local path to convert
 * @returns A normalized file system path
 */
function fileURLToPath(id: string | URL): string;

Usage Examples:

import { fileURLToPath } from "mlly";

// Convert file URL to path
console.log(fileURLToPath("file:///foo/bar.js")); // "/foo/bar.js"

// Handles Windows paths
console.log(fileURLToPath("file:///C:/path/")); // "C:/path"

// Pass through regular paths
console.log(fileURLToPath("/already/a/path")); // "/already/a/path"

Path to File URL

Converts a local file system path to a file URL.

/**
 * Converts a local file system path to a file URL
 * @param id - The file system path to convert
 * @returns The resulting file URL as a string
 */
function pathToFileURL(id: string | URL): string;

Usage Examples:

import { pathToFileURL } from "mlly";

// Convert path to file URL
console.log(pathToFileURL("/foo/bar.js")); // "file:///foo/bar.js"

// Handles Windows paths
console.log(pathToFileURL("C:\\path\\file.js")); // "file:///C:/path/file.js"

Sanitize URI Component

Sanitises a component of a URI by replacing invalid characters.

/**
 * Sanitises a component of a URI by replacing invalid characters
 * @param name - The URI component to sanitise
 * @param replacement - The string to replace invalid characters with
 * @returns The sanitised URI component
 */
function sanitizeURIComponent(name?: string, replacement?: string): string;

Usage Example:

import { sanitizeURIComponent } from "mlly";

// Replace invalid URI characters
console.log(sanitizeURIComponent("foo:bar")); // "foo_bar"
console.log(sanitizeURIComponent("hello world", "-")); // "hello-world"

Sanitize File Path

Cleans a file path string by sanitising each component of the path.

/**
 * Cleans a file path string by sanitising each component of the path
 * @param filePath - The file path to sanitise
 * @returns The sanitised file path
 */
function sanitizeFilePath(filePath?: string): string;

Usage Example:

import { sanitizeFilePath } from "mlly";

// Sanitize problematic file path
console.log(sanitizeFilePath("C:\\te#st\\[...slug].jsx"));
// Result: "C:/te_st/_...slug_.jsx"

Normalize ID

Normalises a module identifier to ensure it has a protocol if missing, handling built-in modules and file paths.

/**
 * Normalises a module identifier to ensure it has a protocol if missing, handling built-in modules and file paths
 * @param id - The identifier to normalise
 * @returns The normalised identifier with the appropriate protocol
 */
function normalizeid(id: string): string;

Usage Examples:

import { normalizeid } from "mlly";

// Add file protocol to paths
console.log(normalizeid("/foo/bar.js")); // "file:///foo/bar.js"

// Handle built-in modules
console.log(normalizeid("fs")); // "node:fs"

// Pass through existing protocols
console.log(normalizeid("https://example.com")); // "https://example.com"

Load URL

Loads the contents of a file from a URL into a string.

/**
 * Loads the contents of a file from a URL into a string
 * @param url - The URL of the file to load
 * @returns A promise that resolves to the content of the file
 */
function loadURL(url: string): Promise<string>;

Usage Example:

import { loadURL, resolve } from "mlly";

// Load file contents
const url = await resolve("./config.json", { url: import.meta.url });
const content = await loadURL(url);
console.log(JSON.parse(content));

To Data URL

Converts a string of code into a data URL that can be used for dynamic imports.

/**
 * Converts a string of code into a data URL that can be used for dynamic imports
 * @param code - The string of code to convert
 * @returns The data URL containing the encoded code
 */
function toDataURL(code: string): string;

Usage Example:

import { toDataURL } from "mlly";

// Convert code to data URL
const code = `export const greeting = "Hello World!";`;
const dataUrl = toDataURL(code);

// Use with dynamic import
const module = await import(dataUrl);
console.log(module.greeting); // "Hello World!"

Is Node Builtin

Checks if a module identifier matches a Node.js built-in module.

/**
 * Checks if a module identifier matches a Node.js built-in module
 * @param id - The identifier to check
 * @returns `true` if the identifier is a built-in module, otherwise `false`
 */
function isNodeBuiltin(id?: string): boolean;

Usage Examples:

import { isNodeBuiltin } from "mlly";

console.log(isNodeBuiltin("fs")); // true
console.log(isNodeBuiltin("node:path")); // true
console.log(isNodeBuiltin("fs/promises")); // true
console.log(isNodeBuiltin("lodash")); // false

Get Protocol

Extracts the protocol portion of a given identifier string.

/**
 * Extracts the protocol portion of a given identifier string
 * @param id - The identifier from which to extract the protocol
 * @returns The protocol part of the identifier, or undefined if no protocol is present
 */
function getProtocol(id: string): string | undefined;

Usage Examples:

import { getProtocol } from "mlly";

console.log(getProtocol("https://example.com")); // "https"
console.log(getProtocol("file:///path/to/file")); // "file"
console.log(getProtocol("node:fs")); // "node"
console.log(getProtocol("relative/path")); // undefined

Key Features

Cross-Platform Path Handling

All path utilities handle cross-platform differences:

  • Normalizes Windows backslashes to forward slashes
  • Properly handles Windows drive letters
  • Ensures consistent path separators across platforms

URI Safety

URI utilities follow RFC standards:

  • sanitizeURIComponent follows RFC 2396 for valid URI characters
  • Removes query strings and normalizes path components
  • Handles special characters safely

Protocol Detection

Smart protocol handling:

  • Detects existing protocols and preserves them
  • Adds appropriate protocols when missing
  • Handles Node.js built-in module detection
  • Supports data:, file:, http:, https:, and node: protocols

Data URL Support

Data URL utilities enable dynamic module evaluation:

  • Base64 encoding for JavaScript code
  • Compatible with dynamic import() statements
  • Proper MIME type handling for JavaScript modules

docs

commonjs-context.md

import-export-analysis.md

index.md

module-evaluation.md

module-resolution.md

syntax-detection.md

utility-functions.md

tile.json