Registry / storage / dat-fetch

dat-fetch

JSON →
library5.1.1jsnpmunverified

Implementation of the Fetch API that uses the Dat SDK for loading peer-to-peer content from hyper:// and dat:// URLs. Version 5.1.1 provides a browser-compatible fetch interface for reading and writing data in Dat archives. Release cadence is sporadic. It automatically resolves domain names via dat-dns, handles directory listings, and supports common HTTP methods. Requires the dat-sdk peer dependency. Suitable for building decentralized web applications.

npm install dat-fetch
INSTALL
IMPORT
SIG · DAT-FETCH
D
dat-fetch
storagejavascriptv5.1.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default export
✓ import makeFetch from 'dat-fetch'
✗ const fetch = require('dat-fetch')()
The module exports a factory function that must be called to create a fetch instance. In CJS, require('dat-fetch')() is correct but ESM import is preferred.
makeFetch
✓ import makeFetch from 'dat-fetch'
✗ const { makeFetch } = require('dat-fetch')
makeFetch is the default export, not a named export. Use default import or require and call it.
fetch
✓ const fetch = makeFetch(); const response = await fetch('hyper://...')
✗ const fetch = require('dat-fetch')
The module does not export a fetch function directly; it exports a factory. You must call makeFetch() to get a fetch function.

Shows how to import the factory, create a fetch instance, perform a GET request to a hyper:// URL, parse JSON response, and clean up.

import makeFetch from 'dat-fetch'; const fetch = makeFetch({ // Optional: pass Dat SDK options or Hyperdrive/resolveName }); const url = 'hyper://blog.mauve.moe/index.json'; try { const response = await fetch(url); if (!response.ok) { console.error('Fetch failed:', response.statusText); process.exit(1); } const data = await response.json(); console.log(data); } catch (err) { console.error('Error:', err.message); } finally { await fetch.close(); }
Debug
Known issues
breakingIn version 5.0.0, the package switched from dat:// to hyper:// URLs. Previously supported dat:// URLs will no longer resolve unless you configure a resolver.
fix
Use hyper:// instead of dat://. If you must support dat://, pass a custom resolveName function that converts dat:// to hyper://.
affects: >=5.0.0
gotchaThe module exports a factory function, not a fetch function. Calling require('dat-fetch')() is correct in CJS, but in ESM you must use import makeFetch from 'dat-fetch' and then call makeFetch().
fix
Use default import: import makeFetch from 'dat-fetch'; const fetch = makeFetch();
affects: >=1.0.0
gotchaThe 'dat-sdk' peer dependency must be installed separately. Missing it will cause runtime errors when trying to resolve names or access archives.
fix
Run npm install dat-sdk alongside dat-fetch.
affects: >=5.0.0
deprecatedThe package does not support Node.js 12 and below. Engine requirements are not specified, but dependencies may fail on older Node versions.
fix
Use Node.js 14 or higher.
affects: >=5.0.0
gotchaThe close() method on the fetch instance only releases SDK resources if the SDK was auto-created (i.e., you didn't pass Hyperdrive and resolveName). If you supplied those, close() does nothing.
fix
Always call fetch.close() to be safe, but understand it may be a no-op in advanced usage.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'dat-sdk'
The peer dependency 'dat-sdk' is not installed.
fix
Run 'npm install dat-sdk' in your project.
TypeError: fetch is not a function
The module was imported incorrectly; it exports a factory, not the fetch function.
fix
Use 'const fetch = require('dat-fetch')()' (CJS) or 'import makeFetch from 'dat-fetch'; const fetch = makeFetch();' (ESM).
TypeError: fetch(...).json is not a function
The response may not be a valid JSON response (e.g., content-type is not application/json, or network error).
fix
Check response.ok and response.headers.get('content-type') before calling .json().
Error: Invalid scheme. Expected hyper:// or dat://
URL scheme is not supported. Only hyper:// and dat:// are allowed.
fix
Ensure the URL starts with 'hyper://' or 'dat://'. For other protocols, use a different library.
Upgrade
Version history
5.1.1latest on npm
Audit
Dependencies
dat-sdkrequiredPeer dependency; provides the underlying Dat protocol implementation for reading/writing archives
Agent activity
16 hits · last 30 days
node
14
Amazon
1
Resources
dat-fetch — npm install dat-fetch · libregistry