Coder Social home page Coder Social logo

xhr's Introduction

๐Ÿšง Under construction ๐Ÿ‘ทโ€โ™‚๏ธ

XMLHttpRequest for Node.js

๐ŸŒ XMLHttpRequest polyfill that supports sync requests
๐Ÿ’ก Inspired by driverdan/node-XMLHttpRequest

๐Ÿ“œ [Mostly spec-compliant]
โฑ Supports synchronous requests
๐ŸŒฒ Delegates to DOMParser() for XML parsing

Installation

This package is intended solely for Node.js. You can use install it from npm:

npm install @jcbhmr/xhr

Usage

๐Ÿ›‘ You cannot use this package in the browser! Make sure you conditionally import it!

if (typeof process !== "undefined") {
  await import("@jcbhmr/xhr");
}

After importing the polyfill, you can use the global XMLHttpRequest and related interfaces as normal.

const xhr = new XMLHttpRequest();
xhr.addEventListener("load", (event) => {
  console.log(event.target.responseText);
});
xhr.open("GET", "http://example.org/");
xhr.send();

โš ๏ธ This polyfill does support synchronous network operations. Be careful though! You don't want to cause your Node.js HTTP server to freeze while making a synchronous HTTP request!

const xhr = new XMLHttpRequest();
xhr.open("GET", "http://example.org/", false);
xhr.send();
console.log(xhr.responseText);

โ„น Just like most major browsers, this will print a warning message.

XML parsing

We don't include XML parsing by default. If there is an exposed DOMParser() class in the global scope, this package will use that to parse the response. If this is unavailable, then all of the XML-related operations will throw.

For example, this will work:

import { JSDOM } from "jsdom";

const { window } = new JSDOM();
globalThis.DOMParser = window.DOMParser;

const xhr = new XMLHttpRequest();
xhr.responseType = "document";
xhr.addEventListener("load", (event) => {
  console.log(event.target.responseXML);
});
xhr.open("GET", "http://example.org/");
xhr.send();

But if there's no DOMParser() polyfill, then the xhr.responseType setter will throw.

XML serialization

When sending a request that is an instanceof Document, we will delegate to the native XMLSerializer() if it exists. If not, this will throw an error. Note that the Document class is also required for the instanceof check.

For example, this works:

import { JSDOM } from "jsdom";

const { window } = new JSDOM();
globalThis.Document = window.Document;
globalThis.XMLSerializer = window.XMLSerializer;

const xhr = new XMLHttpRequest();
xhr.addEventListener("load", (event) => {
  console.log(event.target.responseXML);
});
xhr.open("POST", "http://example.org/");
xhr.send(window.document);

node-XMLHttpRequest

Fork of node-XMLHttpRequest by driverdan. Forked and published to npm because a pull request is not being created and merged. Changes made by rase- are needed for engine.io-client.

Usage

Here's how to include the module in your project and use as the browser-based XHR object.

var XMLHttpRequest = require("xmlhttprequest-ssl").XMLHttpRequest;
var xhr = new XMLHttpRequest();

Note: use the lowercase string "xmlhttprequest-ssl" in your require(). On case-sensitive systems (eg Linux) using uppercase letters won't work.

Original README

Usage

Here's how to include the module in your project and use as the browser-based XHR object.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

Note: use the lowercase string "xmlhttprequest" in your require(). On case-sensitive systems (eg Linux) using uppercase letters won't work.

Versions

Version 2.0.0 introduces a potentially breaking change concerning local file system requests. If these requests fail this library now returns the errno (or -1) as the response status code instead of returning status code 0.

Prior to 1.4.0 version numbers were arbitrary. From 1.4.0 on they conform to the standard major.minor.bugfix. 1.x shouldn't necessarily be considered stable just because it's above 0.x.

Since the XMLHttpRequest API is stable this library's API is stable as well. Major version numbers indicate significant core code changes. Minor versions indicate minor core code changes or better conformity to the W3C spec.

License

MIT license. See LICENSE for full details.

Supports

  • Async and synchronous requests
  • GET, POST, PUT, and DELETE requests
  • All spec methods (open, send, abort, getRequestHeader, getAllRequestHeaders, event methods)
  • Requests to all domains

Known Issues / Missing Features

For a list of open issues or to report your own visit the github issues page.

  • Local file access may have unexpected results for non-UTF8 files
  • Synchronous requests don't set headers properly
  • Synchronous requests freeze node while waiting for response (But that's what you want, right? Stick with async!).
  • Some events are missing, such as abort
  • getRequestHeader is case-sensitive
  • Cookies aren't persisted between requests
  • Missing XML support
  • Missing basic auth

xhr's People

Contributors

driverdan avatar mjwwit avatar jcbhmr avatar lfdoherty avatar keverw avatar tootallnate avatar david-clover-com avatar josephg avatar fjakobs avatar coderaiser avatar a2800276 avatar sjakthol avatar robertmirandola avatar shaoster avatar pskucherov avatar mixu avatar maraujop avatar max99x avatar mrcarlberg avatar mhansen avatar jbpros avatar eugendueck avatar chawco avatar brentlintner avatar arthurblake avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.