Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fetch-with-file-support

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-with-file-support

📂 fetch() but with support for file:///my/file.txt URLs

  • 2.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

fetch() with file: support

📂 fetch() but with support for file:///my/file.txt URLs

// Native Node.js fetch
const response = await fetch(import.meta.resolve("./data.txt"));
const text = await response.text();
console.log(text);
// 🛑 TypeError: fetch failed
//   cause: Error: not implemented... yet...
// Using fetch-with-file-support in Node.js
import { fetch } from "fetch-with-file-support";
const response = await fetch(import.meta.resolve("./data.txt"));
const text = await response.text();
console.log(text);
// 'Hello from data.txt!'

🔎 Works great for writing isomorphic fetch(import.meta.resolve()) code
🌊 Uses fs.createReadStream() for streaming reads
🦕 Mirrors Deno's implementation of fetch() for file: URLs

Installation

npm pnpm Yarn Bun Deno

You can install this package using your favorite npm package manager like npm, Yarn, pnpm, or Bun.

npm install fetch-with-file-support

ℹ Deno already supports fetch()-ing file: URLs. This package will delegate to the native fetch() implementation.

🛑 In browsers this will delegate to the global fetch() function. Some browsers support fetch()-ing file: URLs while others do not. Some support file: URLs only under special circumstances.

Usage

Node.js Deno Bun

The quickest way to get started is to import the fetch() function straight from the module:

import { fetch } from "fetch-with-file-support";

const response = await fetch(import.meta.resolve("./package.json"));
const json = await response.json();

This is very useful when writing code that imports configuration, data, or other information from a file located right next to the .js source file. WASM is a good example.

import { fetch } from "fetch-with-file-support";

// Normally 'fetch("file:///mypkg/app_bg.wasm")' would fail in Node.js!
const { module, instance } = await WebAssembly.instantiateStreaming(
  fetch(import.meta.resolve("./app_bg.wasm")),
  imports,
);

🌎 If you prefer global polyfills, you can import fetch-with-file-support/auto.

import "fetch-with-file-support/auto";
const response = await fetch(import.meta.resolve("./data.json"));
const json = await response.json();

Development

Node.js TypeScript

Keywords

FAQs

Package last updated on 10 Jan 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

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc