Registry / http-networking / http-file

http-file

JSON →
library0.1.7jsnpmunverified

`http-file` is a TypeScript library designed for serializing and deserializing complete HTTP request and response objects to and from a structured file format. It provides core classes like `RequestFile` and `ResponseFile` to encapsulate all relevant HTTP interaction data, including method, URL, headers, and body, along with utility functions such as `readRequestFile`, `writeRequestFile`, `readResponseFile`, and `writeResponseFile` for file system operations. This capability is particularly useful for scenarios requiring the persistence of HTTP traffic, such as creating mock servers from recorded interactions, replaying requests for testing or debugging, or sharing specific HTTP payloads. The package is currently at version 1.0.3. While it ships with full TypeScript types and offers a stable API, its last release was over two years ago, indicating a mature, low-cadence maintenance or stable phase rather than active feature development. Its key differentiator lies in its focused approach to structured file-based storage of entire HTTP dialogues, distinct from general-purpose HTTP clients or static file servers.

npm install http-file
INSTALL
IMPORT
SIG · HTTP-FILE
H
http-file
http-networkingjavascriptv0.1.7
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

RequestFile
import { RequestFile } from 'http-file';
const RequestFile = require('http-file').RequestFile;
Primarily designed for ESM and TypeScript; CommonJS requires dot-notation access to named exports.
readRequestFile
import { readRequestFile } from 'http-file';
import readRequestFile from 'http-file/readRequestFile';
All public functions and classes are named exports from the main package entry point.
ResponseFile
import { ResponseFile, writeResponseFile } from 'http-file';
import * as httpFile from 'http-file'; const myResponse = new httpFile.ResponseFile();
Destructuring named imports is the recommended pattern for clarity and tree-shaking.

This quickstart demonstrates how to create `RequestFile` and `ResponseFile` objects, write them to disk, and then read them back, showcasing the library's core serialization and deserialization capabilities.

import { RequestFile, ResponseFile, writeRequestFile, readRequestFile, writeResponseFile, readResponseFile } from 'http-file'; import * as fs from 'fs/promises'; // For file cleanup async function exampleUsage() { const requestFilePath = 'example-request.http.json'; const responseFilePath = 'example-response.http.json'; // 1. Create a RequestFile instance const requestData: RequestFile = { method: 'GET', url: 'https://api.example.com/data/items/1?cache=false', headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': `Bearer ${process.env.API_KEY ?? 'YOUR_FALLBACK_TOKEN'}`, // Use env var for sensitive data }, body: null, }; // 2. Write the RequestFile to disk await writeRequestFile(requestFilePath, requestData); console.log(`Request file written to ${requestFilePath}`); // 3. Read the RequestFile from disk const loadedRequest = await readRequestFile(requestFilePath); console.log('Loaded request URL:', loadedRequest.url); // 4. Create a ResponseFile instance const responseData: ResponseFile = { statusCode: 200, headers: { 'Content-Type': 'application/json', 'X-Request-ID': 'unique-abc-123', 'Cache-Control': 'no-cache' }, body: JSON.stringify({ message: 'Data fetched successfully', data: { id: 1, name: 'Item 1' } }), }; // 5. Write the ResponseFile to disk await writeResponseFile(responseFilePath, responseData); console.log(`Response file written to ${responseFilePath}`); // 6. Read the ResponseFile from disk const loadedResponse = await readResponseFile(responseFilePath); console.log('Loaded response status:', loadedResponse.statusCode); console.log('Loaded response body:', loadedResponse.body ? JSON.parse(loadedResponse.body.toString()) : null); // Clean up created files await fs.unlink(requestFilePath); await fs.unlink(responseFilePath); console.log('Cleaned up example files.'); } exampleUsage().catch(console.error);
Debug
Known issues
gotchaStoring sensitive HTTP request or response data (e.g., authorization tokens, user credentials, private data in bodies) directly to files on disk can be a security risk. Ensure these files are stored in secure locations with appropriate access controls and are not committed to version control systems.
fix
Never commit files containing sensitive information to public repositories. Implement robust access controls for local storage and consider encrypting files if they contain highly sensitive data.
affects: >=1.0.0
gotchaThe library primarily operates on file paths. Incorrect or invalid file paths (e.g., non-existent directories, permission issues) will result in standard Node.js file system errors (e.g., `ENOENT`, `EACCES`) rather than custom library errors. These errors will be thrown by the underlying `fs/promises` module.
fix
Always validate file paths and ensure necessary directory structures exist and have correct permissions before attempting write operations. Wrap file operations in `try-catch` blocks to handle potential `fs` errors gracefully.
affects: >=1.0.0
gotchaThe `body` property for `RequestFile` and `ResponseFile` supports `string | null | Buffer`. When dealing with non-UTF-8 textual content or binary data, it is crucial to pass a `Buffer` instance to preserve data integrity. Storing binary data as a plain string might lead to corruption or incorrect interpretation.
fix
For binary HTTP bodies (e.g., images, downloads), ensure you convert the data to a Node.js `Buffer` before assigning it to the `body` property. When reading, check `Buffer.isBuffer(loadedObject.body)` and handle accordingly.
affects: >=1.0.0
Errors
Common errors & fixes
Error: ENOENT: no such file or directory, open 'path/to/non-existent-file.json'
The specified file path for reading or writing does not exist or refers to an invalid location.
fix
Ensure the directory exists before writing, and the file exists before reading. Use `fs.mkdir(path, { recursive: true })` for directories.
TypeError: The "path" argument must be of type string. Received type object
A non-string value (e.g., `null`, `undefined`, an object) was passed as the file path argument to `readRequestFile`, `writeRequestFile`, or related functions.
fix
Always pass a valid string representing the file path to the library's file I/O functions.
Upgrade
Version history
0.1.7latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
http-file — npm install http-file · libregistry