Returns a stream.Readable from a URI string supporting multiple protocols
npx @tessl/cli install tessl/npm-get-uri@6.0.0get-uri is a high-level URI-to-stream interface that accepts URI strings and returns Node.js Readable stream instances. It supports multiple protocols including data URIs, file URIs, FTP, HTTP, and HTTPS with built-in caching capabilities for performance optimization.
npm install get-uriimport { getUri } from "get-uri";For CommonJS:
const { getUri } = require("get-uri");import { getUri } from "get-uri";
// Get a readable stream from a URI
const stream = await getUri('https://httpbin.org/json');
stream.pipe(process.stdout);
// Use with different protocols
const fileStream = await getUri('file:///path/to/file.txt');
const dataStream = await getUri('data:text/plain;base64,SGVsbG8gV29ybGQ=');
const ftpStream = await getUri('ftp://ftp.example.com/path/to/file.txt');get-uri is built around several key components:
getUri() function provides a unified interface for all URI protocolsMain function for converting URIs to readable streams with protocol-specific options and caching support.
/**
* Returns a readable stream from a URI string
* @param uri - URI string or URL object to retrieve
* @param opts - Protocol-specific options including cache validation
* @returns Promise that resolves to a readable stream
* @throws TypeError for unsupported protocols or missing URI
*/
function getUri<Uri extends string>(
uri: Uri | URL,
opts?: ProtocolOpts<Uri>
): Promise<Readable>;Protocol validation and registry for extensible URI handling.
/**
* Check if a protocol is supported
* @param p - Protocol string to validate
* @returns Type guard indicating if protocol is supported
*/
function isValidProtocol(p: string): p is keyof Protocols;
/**
* Registry of supported protocol handlers
*/
const protocols: {
data: GetUriProtocol<DataOptions>;
file: GetUriProtocol<FileOptions>;
ftp: GetUriProtocol<FTPOptions>;
http: GetUriProtocol<HttpOptions>;
https: GetUriProtocol<HttpOptions>;
};Built-in cache validation and error handling for efficient resource management.
/**
* Error thrown when resource is not found
*/
class NotFoundError extends Error {
code: 'ENOTFOUND';
constructor(message?: string);
}
/**
* Error thrown when resource has not been modified since cache
*/
class NotModifiedError extends Error {
code: 'ENOTMODIFIED';
constructor(message?: string);
}
/**
* Error thrown for HTTP-level errors
*/
class HTTPError extends Error {
code: string;
statusCode: number;
constructor(statusCode: number, message?: string);
}// Node.js built-in types (import from respective modules)
import { Readable } from 'stream';
/**
* Generic protocol handler function type
*/
type GetUriProtocol<T> = (parsed: URL, opts?: T) => Promise<Readable>;
/**
* Union type for all supported protocols
*/
type Protocols = typeof protocols;
/**
* Mapped type for protocol-specific options
*/
type ProtocolsOptions = {
[P in keyof Protocols]: NonNullable<Parameters<Protocols[P]>[1]>;
};
/**
* Conditional type for getting protocol-specific options based on URI
*/
type ProtocolOpts<T> = {
[P in keyof ProtocolsOptions]: Protocol<T> extends P
? ProtocolsOptions[P]
: never;
}[keyof Protocols];