0

I have written a REST utility method for a client side NW.js application I am working on. I extended the implementation from an example I found on stack overflow, although I can't seem to find it right now.

Here is the logic:

_makeRequest = function(method, url, params, headers, responseType) {
    params = params || null;
    headers = headers || null;
    responseType = responseType || null;

    return new Promise(function (resolve, reject) {
      // We'll need to stringify if we've been given an object
      // If we have a string, ArrayBuffer, Blob, or File, this is skipped.
      if (params && (typeof params === 'object') && !(
          params instanceof ArrayBuffer ||
          params instanceof Blob ||
          params instanceof File
        )) {
        params = Object.keys(params).map(function (key) {
          return encodeURIComponent(key) + '=' + encodeURIComponent( _resolveDatasetNamespace( params[key] ) );
        }).join('&');
      }

      var xhr = new XMLHttpRequest();
      xhr.responseType = (responseType) ? responseType : "";
      xhr.open(method, (method === "GET" && params) ? url + "?" + params : url);
      xhr.onload = function () {
        if (this.status >= 200 && this.status < 300) {
          resolve(xhr.response);
        } else {
          reject({
            status: this.status,
            statusText: xhr.statusText
          });
        }
      };
      xhr.onerror = function () {
        reject({
          status: this.status,
          statusText: xhr.statusText
        });
      };
      if (headers) {
        Object.keys(headers).forEach(function (key) {
          xhr.setRequestHeader(key, headers[key]);
        });
      }
      xhr.send(params);
    });
  };

Overall the method works great, but I noticed that I would trigger the oh so unhelpful onerror callback when I tried to send Blob data.

I verified with MDN that my logic looked sane. Then I tested in the browser javascript console to find the logic worked just fine there.

I finally came across this post and realized the implementation running in the node.js based NW.js framework must be at fault. I totally didn't consider the fact that node.js didn't natively contain an implementation of XMLHttpRequest, so I'm assuming that this is provided by CEF in NW.js.

I verified that instanceof Blob returned false in my NW.js application but true in the browser.

I really wanted to keep this implementation based on browser technology as it is both portable and a breeze to sanity check in a browser. I noticed other issues on the NW.js github so I assume it is intended to be used with the framework.

I know I could fall back to the node.js standard way of handling this problem, but I am curious if there is a way to resolve this issue.

I also noticed this suggestion for a fully node based application conversion.

Any other suggestions?

2
  • 1
    See Screw-FileReader Commented Nov 9, 2016 at 21:48
  • thanks @guest271314 I'll take a look at that. Commented Nov 9, 2016 at 21:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.