Isomorphic HTTP Stream

0.1.2 · active · verified Wed Apr 22

iso-stream-http is a JavaScript module that provides an isomorphic (browser and Node.js compatible) implementation of Node.js's native `http` module. Currently at version 0.1.2, it aims to replicate the Node.js HTTP client API as closely as possible within browser environments, offering pseudo-streaming capabilities where the entire response is held in memory, and in some modern browsers, true streaming. It is heavily inspired by and intended to replace `stream-http`. The project appears to have a slow release cadence, with the latest update being minor bug fixes. Key differentiators include its isomorphic nature, aiming for Node.js API parity in the browser, and its focus on providing data before the request completes, which is a significant feature for handling larger responses efficiently in both client and server contexts.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to make an HTTP GET request, handle streaming data chunks, and detect the end of the response using `iso-stream-http`'s Node.js-like API.

const { http } = require('iso-stream-http');

console.log('Fetching /bundle.js...');

http.get('http://localhost:8080/bundle.js', function (res) {
  console.log(`Status: ${res.statusCode}`);
  console.log('Headers:', res.headers);

  let data = '';
  res.on('data', function (buf) {
    data += buf.toString();
    process.stdout.write('Received chunk: ' + buf.length + ' bytes\n');
  });

  res.on('end', function () {
    console.log('\n__END__');
    // In a browser, you might update the DOM here:
    // document.getElementById('result').innerHTML += '<br>__END__';
    console.log('Full response data length:', data.length);
  });

  res.on('error', function(err) {
    console.error('Response error:', err);
  });
}).on('error', function(err) {
  console.error('Request error:', err);
});

// To make this runnable, you would need a simple HTTP server
// serving a 'bundle.js' file on http://localhost:8080
// Example server (Node.js):
/*
const server = require('http').createServer((req, res) => {
  if (req.url === '/bundle.js') {
    res.writeHead(200, { 'Content-Type': 'application/javascript' });
    res.end('console.log("hello from bundle.js");');
  } else {
    res.writeHead(404); res.end('Not Found');
  }
});
server.listen(8080, () => console.log('Server listening on port 8080'));
*/

view raw JSON →