Giter Site home page Giter Site logo

ykzts / node-xmlhttprequest Goto Github PK

View Code? Open in Web Editor NEW
42.0 42.0 12.0 1.69 MB

server-side XMLHttpRequest for Node.

Home Page: https://www.npmjs.com/package/w3c-xmlhttprequest

License: MIT License

JavaScript 1.24% TypeScript 98.48% Shell 0.28%

node-xmlhttprequest's Introduction

Hi there

Yamagishi Kazutoshi is a software engineer working in Japan, specializing in front-end development using TypeScript. I also have a broad range of experience in web application development, not only in the front-end area but also in the back-end and infrastructure areas.

Artwork by Kannazuki Diru

node-xmlhttprequest's People

Contributors

benjamin-dobell avatar dbergey avatar dcsaszar avatar dependabot[bot] avatar jansuchy avatar kapouer avatar mihop avatar ouhouhsami avatar skovhus avatar xhrbot avatar ykzts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-xmlhttprequest's Issues

Returns an exception when given an EventListenerObject

minimal reproduction

code

import { XMLHttpRequest } from 'w3c-xmlhttprequest'

const client = new XMLHttpRequest()
client.addEventListener('load', {
  handleEvent() {}
});
client.open('GET', 'https://example.com/')
client.send(null)

output

/tmp/tmp.yq5z5JKhRC/node_modules/w3c-xmlhttprequest/dist/index.js:520
        listener.call(this, event);
                 ^

TypeError: listener.call is not a function
    at XMLHttpRequest.dispatchEvent (/tmp/tmp.yq5z5JKhRC/node_modules/w3c-xmlhttprequest/dist/index.js:520:18)
    at IncomingMessage.<anonymous> (/tmp/tmp.yq5z5JKhRC/node_modules/w3c-xmlhttprequest/dist/index.js:831:14)
    at IncomingMessage.emit (events.js:333:22)
    at endReadableNT (_stream_readable.js:1204:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

V 3.0.3 is incompatible with ECMAScript modules

test.mjs:

import "w3c-xmlhttprequest";

Using node v16.9.1:

node test.mjs          
(node:15491) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/node_modules/w3c-xmlhttprequest/dist/index.modern.js:1
import * as http from 'http';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:196:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
    at async Loader.import (node:internal/modules/esm/loader:178:24)
    at async Object.loadESM (node:internal/process/esm_loader:68:5)

With V 3.0.0 this worked as expected.

I did some digging and it looks like this issue may be caused by the microbundle setup. Here's another library which had a similar issue: GillianPerard/typescript-json-serializer#129 (comment)

Want support for `async` option

We're running into issues at the moment, because we want to force a synchronous request via the XMLHttpRequest module if we're on the server-side for a particular scenario. The issue with this is that it does not seem like w3c-xmlhttprequest will allow for this. It is understandable in some respects, but in certain specific scenarios this may be required. It is in the W3C specification as well, which I was under the impression this library implemented.

Please add this feature. It is a must-have for our project.

Thanks!

request.abort() throws Exception

When I try to cancel the request using abort(), I get following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^

Example code :

var XMLHttpRequest = require('w3c-xmlhttprequest').XMLHttpRequest;

var req = new XMLHttpRequest();
req.open('GET', 'http://httpbin.org/get', true);
req.addEventListener('load', function () {
    console.log("Content", req.responseText);
}, false);
req.addEventListener('abort', function (event) {
    console.error("Abort this request.");
}, false);
req.send();

// abort
if(req.readyState !== XMLHttpRequest.UNSENT) {
    req.abort();
}
/*
Abort this request.

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: socket hang up
    at createHangUpError (http.js:1472:15)
    at Socket.socketCloseListener (http.js:1522:23)
    at Socket.EventEmitter.emit (events.js:117:20)
    at TCP.close (net.js:465:12)
*/
$ node -v
v0.10.26

file upload example

can you put a file upload example in the readme? based on the source it's not clear whether this is supported or not.

POST results in "TypeError: Cannot read property 'lengthComputable' of undefined"

Summary

POST with body results in "TypeError: Cannot read property 'lengthComputable' of undefined" because XMLHttpRequestUpload doesn't have _properties.

Sample Code

const http = require('http');
const XMLHttpRequest = require('w3c-xmlhttprequest').XMLHttpRequest;

const server = http.createServer((req, res) => {
  res.end();
});
server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);

const client = new XMLHttpRequest();

client.open("POST", "http://localhost:8000");
client.send("abcdefg");

Result

~/src/node-xmlhttprequest/lib/xmlhttprequest.js:129
      lengthComputable: _properties.lengthComputable,
                                   ^

TypeError: Cannot read property 'lengthComputable' of undefined
    at Socket.<anonymous> (/home/takuo/src/node-xmlhttprequest/lib/xmlhttprequest.js:129:36)
    at emitOne (events.js:82:20)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)

Version

2.1.0 and master (383319c)

Cause

_setDispatchProgressEvents read this._properties while this is bound to XMLHttpRequest#upload which doesn't have _properties.

function _setDispatchProgressEvents(stream) {
  const _properties = this._properties;
      client.on('socket', _setDispatchProgressEvents.bind(this.upload));

Possible Fix

Defining _properties on XMLHttpRequestUpload.

Tests

Would love to contribute to the project but without tests its hard to dive in and make changes... Do you have any plans for adding at least some basic ones?

Should handle errors

var XMLHttpRequest = require('w3c-xmlhttprequest').XMLHttpRequest;
var client = new XMLHttpRequest();
client.open('GET', 'http://some.invalid-url.com');
client.addEventListener('load', function(event) {
    console.log('Loaded.');
});
client.addEventListener('error', function(err) {
    console.log('Error:', err);
});
client.send();

Results in:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: getaddrinfo ENOTFOUND
    at errnoException (dns.js:37:11)
    at Object.onanswer [as oncomplete] (dns.js:124:16)

I should receive an XMLHttpRequestProgressEvent and be able to handle it, not have my app crash.

Can't set withCredentials

When trying to use this library with the xhr library, I get
TypeError: Cannot assign to read only property 'withCredentials' of [object Object]].

Digging into this, it appears that this library is the only XmlHttpRequest implementation that marks this parameter read-only.

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.