1. path-source
Read files in Node, or fetch URLs in browser, as standard WhatWG streams.
path-source
Package: path-source
Created by: mbostock
Last modified: Thu, 23 Jun 2022 14:41:31 GMT
Version: 0.1.3
License: BSD-3-Clause
Downloads: 480,985
Repository: https://github.com/mbostock/path-source

Install

npm install path-source
yarn add path-source

path-source

A readable stream reader for reading files in Node or fetching URLs in browser. For example, to read a file in Node:

 var path = require("path-source");

path("README.md")
  .then(function read(source) {
    return source.read().then(result => {
      if (result.done) return;
      process.stdout.write(result.value);
      return read(source);
    });
  })
  .catch(error => console.error(error.stack));

Similarly, to fetch a resource in a browser (requires array-source if streaming fetch is not supported):

 <!DOCTYPE html>
<script src="https://unpkg.com/array-source@0"></script>
<script src="https://unpkg.com/path-source@0"></script>
<script>

sources.path("README.md")
  .then(function read(source) {
    return source.read().then(result => {
      if (result.done) return;
      console.log(result.value);
      return read(source);
    });
  })
  .catch(error => console.error(error.stack));

</script>

API Reference

# path(path[, options]) <>

In Node, returns a Promise that yields a source for the file at the specified path; equivalent to file-source. In a browser, returns a Promise that yields a source for the resource at the specified path URL, using streaming fetch if available, and falling back to a binary data XMLHttpRequest.

In Node, the following options are supported:

  • highWaterMark - the stream’s internal buffer size; defaults to 65,536

In a browser, no options are currently supported.

# source.read() <>

Returns a Promise for the next chunk of data from the underlying stream. The yielded result is an object with the following properties:

  • value - a Uint8Array (a Buffer), or undefined if the stream ended
  • done - a boolean which is true if the stream ended

# source.cancel() <>

Returns a Promise which is resolved when the underlying stream has been destroyed.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code