# path-source

> Read files in Node, or fetch URLs in browser, as standard WhatWG streams.

Latest version **0.1.3** (published 2017-09-02) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install path-source
pnpm add path-source
yarn add path-source
bun add path-source
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2017-09-02 |
| First published | 2016-09-06 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Mike Bostock |
| Maintainers | mbostock |
| Keywords | fetch, stream, reader |

## Links

- npm: https://www.npmjs.com/package/path-source
- Repository: https://github.com/mbostock/path-source
- Issues: https://github.com/mbostock/path-source/issues
- npm.io page: https://npm.io/package/path-source

## Dependencies (2)

- [file-source](https://npm.io/package/file-source.md) 0.6
- [array-source](https://npm.io/package/array-source.md) 0.0

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 0.1.3 (latest) — 2017-09-02
- 0.1.2 — 2016-09-07
- 0.1.1 — 2016-09-07
- 0.1.0 — 2016-09-07
- 0.0.2 — 2016-09-06
- 0.0.1 — 2016-09-06

## README

# path-source

A [readable stream reader](https://streams.spec.whatwg.org/#readable-stream-reader) for reading files in Node or fetching URLs in browser. For example, to read a file in Node:

```js
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](https://github.com/mbostock/array-source) if [streaming fetch](https://www.chromestatus.com/feature/5804334163951616) is not supported):

```html
<!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

<a name="path" href="#path">#</a> <b>path</b>(<i>path</i>[, <i>options</i>]) [<>](https://github.com/mbostock/path-source/blob/master/index.js "Source")

In Node, returns a Promise that yields a *source* for the file at the specified *path*; equivalent to [file-source](https://github.com/mbostock/file-source#file). In a browser, returns a Promise that yields a *source* for the resource at the specified *path* URL, using [streaming fetch](https://www.chromestatus.com/feature/5804334163951616) if available, and falling back to a [binary data](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data) [XMLHttpRequest](https://developer.mozilla.org/en-US/docs/Web/API/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.

<a name="source_read" href="#source_read">#</a> <i>source</i>.<b>read</b>() [<>](https://github.com/mbostock/stream-source/blob/master/read.js "Source")

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) (a [Buffer](https://nodejs.org/api/buffer.html)), or undefined if the stream ended
* `done` - a boolean which is true if the stream ended

<a name="source_cancel" href="#source_cancel">#</a> <i>source</i>.<b>cancel</b>() [<>](https://github.com/mbostock/slice-source/blob/master/cancel.js "Source")

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

---
_Source: https://npm.io/package/path-source · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
