dasu: Consistent XHR API for Client and Server
dasu is a JavaScript library that aimed to provide a consistent XMLHttpRequest (XHR) API for both client-side (browser) and server-side (Node.js) environments. It abstracts away the underlying differences between `window.XMLHttpRequest` and Node's `http.request`, presenting a unified interface. The library, currently at version 0.4.3, was last published in June 2014. Its primary differentiator was simplifying basic HTTP requests with a single API across environments, which was particularly useful before the widespread adoption of Fetch API or more mature cross-platform libraries. Due to its age and lack of updates, it does not support modern JavaScript features like Promises or `async/await` and relies exclusively on CommonJS modules.
Common errors
-
TypeError: dasu is not a function or dasu.req is undefined
cause Attempting to use `import dasu from 'dasu'` or `import { req } from 'dasu'` with a CommonJS-only library.fixUse CommonJS `require` syntax: `const dasu = require('dasu'); const req = dasu.req;` -
Error: Protocol 'http:' not supported. Expected 'http:' or 'https:'
cause Incorrect or missing `protocol` in the request `params` object, especially when making requests to non-standard ports or local services.fixEnsure `params.protocol` is correctly specified as `'http:'` or `'https:'` (including the colon) and matches the target server's protocol. -
Callback must be a function.
cause The second argument to `dasu.req` is not a function, or a Promise/async/await syntax was mistakenly used.fixEnsure the second argument passed to `dasu.req` is a standard Node.js-style callback function with `(err, res, data)` arguments.
Warnings
- breaking The `dasu` package is severely outdated, with its last release (0.4.3) dating back to June 2014. It has not received updates for over a decade and is considered abandoned. It lacks modern JavaScript features, security patches, and bug fixes.
- gotcha dasu relies on a traditional Node.js callback pattern for asynchronous operations (`(err, res, data) => {}`). It does not support Promises, async/await syntax, or other modern async control flow mechanisms, making integration with contemporary JavaScript codebases challenging.
- gotcha The library primarily supports CommonJS module syntax (`require`). There are no official ES module (ESM) exports, meaning direct `import` statements will fail without a CommonJS-to-ESM transpilation step, which is generally not recommended for abandoned libraries.
- gotcha The `dasu.mode` property allows forcing 'node', 'browser', or 'auto'. In environments like Electron or certain testing setups, `dasu.mode = 'auto'` might incorrectly detect the environment, leading to unexpected behavior or API mismatches.
Install
-
npm install dasu -
yarn add dasu -
pnpm add dasu
Imports
- req
import { req } from 'dasu';const dasu = require('dasu'); const req = dasu.req; - dasu
import dasu from 'dasu';
const dasu = require('dasu');
Quickstart
const dasu = require('dasu');
const req = dasu.req;
// Same parameters as Node's require('http').request
const params = {
method: 'GET',
protocol: 'http',
hostname: 'uinames.com',
port: 80,
path: '/api/',
};
req(params, function (err, res, data) {
if (err) {
console.error('Request failed:', err.message);
return;
}
console.log('Status Code:', res.statusCode);
console.log('Headers:', res.headers);
try {
const json = JSON.parse(data);
console.log('Response Data:', json);
} catch (parseError) {
console.error('Failed to parse JSON:', parseError.message);
console.log('Raw Data:', data.toString());
}
});
// Optionally, disable auto-follow redirects
dasu.follow = false;
// Optionally, force mode 'node', 'browser', 'auto'
dasu.mode = 'auto'; // Uses window.XMLHttpRequest if available