package
Intelligent I/O for Node
npm install heya-io-nodeGitHub-flavored package documentation.
Across all versions, last 30 days.
50 in 30 days
Runtime metadata.
Tagged topics.
Estimated unpacked size across 2 packages.
169.2 kB
Includes dependencies.
io-nodeThis is a Node-specific transport for heya-io based on built-in http and https modules. The main purpose of the module is to provide an ergonomic simple light-weight HTTP I/O on Node leveraging existing customization facilities of heya-io where appropriate.
Following heya-io services are supported as is out of the box:
io.track — tracks I/O requests to eliminate duplicates, register an interest without initiating I/O requests, and much more.io.mock — mocks responses to I/O requests without writing a special server courtesy of Mike Wilcox. Very useful for rapid prototyping and writing tests.io.bust — a simple pseudo plugin to generate a randomized query value to bust cache.io.retry — a flexible way to retry requests, e.g., to deal with unreliable servers or to watch for changing values.Additionally it supports:
gzip and deflate are supported out of the box with no extra dependencies using built-in modules.Plain vanilla GET:
const io = require('heya-io-node');
io.get('http://example.com/hello').then(function (value) {
console.log(value);
});
io.get('/hello', {to: 'world', times: 5}).then(value => {
// GET /hello?to=world×=5
console.log(value);
});Some other verbs (REST example):
function done() { console.log('done'); }
io.post('/things', {name: 'Bob', age: 42}).then(done);
io.put('/things/5', {name: 'Alice', age: 33}).then(done);
io.patch('/things/7', {age: 14}).then(done);
io.remove('/things/3').then(done);Streaming (since 1.1.0) + encoding a payload (since 1.2.0):
const ios = require('heya-io-node/stream');
fs.createReadStream('sample.json')
.pipe(ios.post({
url: 'https://example.com/analyze',
headers: {
'Content-Type': 'application/json',
'$-Content-Encoding': 'gzip',
'Accept: plain/text'
}
}))
.pipe(process.stdout);
// or it can be done more granularly:
io.post({
url: 'https://example.com/analyze',
headers: {
'Content-Type': 'application/json',
'$-Content-Encoding': 'gzip',
'Accept: plain/text'
Mock in action:
// set up a mock handler
io.mock('/a*', (options, prep) => {
console.log('Got call: ' + options.method + ' ' + prep.url);
return 42;
});
// let's make a call
io.get('/a/x').then(value => {
console.log(value); // 42
});
// set up a redirect /b => /a/b
io.mock('/b', options => io.get('/a/b', options.query || options.data || null));
// let's make another call
Using url template to sanitize URLs (ES6):
const url = require('heya-io/url');
const client = 'Bob & Jordan & Co';
io.get(url`/api/${client}/details`).then(value => {
// GET /api/Bob%20%26%20Jordan%20%26%20Co/details
console.log(value);
});See more examples in heya-io's Wiki, heya-io-node's Wiki, and the cookbooks of heya-io:
npm install --save heya-io-node
# or: yarn add heya-io-nodeAll documentation can be found in project's wiki.
In order to run tests locally, you should start the test server first:
npm startThen (likely in a different command line window) run tests:
npm testThe server runs indefinitely, and can be stopped by Ctrl+C.
url module with WHATWG URL.yarn.lock.io.retry.IncomeMessage support.getData() and getHeaders() on stream and error objects.Buffer, replaced failure objects with Error-based objects.request, use native http/https, support compression and streaming.BSD or AFL — your choice.