ladjs / superagent

Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.
https://ladjs.github.io/superagent/
MIT License
16.59k stars 1.33k forks source link

Extend support for Titanium SDK #529

Open yuchi opened 9 years ago

yuchi commented 9 years ago

At @smclab we extended the support of superagent to Titanium SDK on a project called ti-superagent.

Are the maintainers of superagent willing to accept some contribution in order to either

  1. make the required changes easier to maintain or
  2. make the package itself compatible with TitaniumSDK?

In case we chose to extend superagent’s itself, are you willing to accept to have another test environment? Those tests would run as native iOS, Android and (later this year) Windows applications.

Don’t hesitate in asking everything about either Titanium SDK, the changes we made and how we plan to have native tests running.


Some background about Titanium SDK

Titanium SDK is a platform to build cross-platform mobile apps using JavaScript and native views and components. No WebViews are harmed in a Titanium SDK app.

That means that they use just JSC on iOS and V8 on Android, and many things had to be re-implemented. Among those we can highlight Titanium.Network.HTTPClient which is an API almost, but not quite, entirely unlike XMLHttpRequest.

To make superagent work we needed to

To solve the packaging issue we built titaniumifier.

mpociot commented 9 years ago

+1

rborn commented 9 years ago

+1

FokkeZB commented 9 years ago

+1 (excellent speech)

defunctzombie commented 9 years ago

What type of changes are needed?

yuchi commented 9 years ago

First of all something that was not explicitly stated, but it’s really important. By now Titanium SDK has no compatibility with Node.js builtins, and as a de-facto philosophy is more oriented towards reducing the learning gap for web devs rather than node devs. For that reason we’re extending and using the browser version of superagent.

We have three different kind of changes that can be made:

Changes to simplify ti-superagent maintenance

Make superagent itself compatible with Titanium APIs

These are the most evident changes. I know that some of them are ominous, and we can (should) actually create a good test-suite and solve this from Titanium APIs standpoint. But we have many, many, many applications that are still locked to older Titanium SDK versions.

Make superagent installable as a Ti module

You don’t need to explicitly publish your module somewhere, gitTio (which is the module manager for Titanium) does it for you every time you push a new version and git tag.

emilioicai commented 9 years ago

+1

yuchi commented 9 years ago

So, what’s your opinion on this, @defunctzombie?

defunctzombie commented 9 years ago

Sorry I am really busy at work. Have not had a chance to review yet but will try to next week.

Apologies and I do appreciate the detailed explanation!

On Thursday, January 22, 2015, Pier Paolo Ramon notifications@github.com wrote:

So, what’s your opinion on this, @defunctzombie https://github.com/defunctzombie?

— Reply to this email directly or view it on GitHub https://github.com/visionmedia/superagent/issues/529#issuecomment-71006241 .

defunctzombie commented 9 years ago

getXHR() is not exposed on the request object (no release yet but that code is in master)

defunctzombie commented 9 years ago

parseHeader(str) and splitting .end() into smaller pieces could use PRs.

I think making you life for ti-superagent easier should be the first goal (we can do that with relative ease).

I am also onboard with most of the items in Make superagent itself compatible with Titanium APIs but some of them will need to be tweaked.

yuchi commented 9 years ago

Great. I’ll try to transform every task in a relative PR.

euforic commented 9 years ago

There is no need to add titanium support to superagent. All you have to do is polyfill Xhr in your TI project. Especially since the Titanium Xhr lib only deviates a little.

Ti-xhrfix.js

var Emitter = require('emitter');

// Global Context
var globalCTX = Function('return this')();

// Patch global scope

globalCTX.XMLHttpRequest = XMLHttpRequest;

// sub for browser global location property.

globalCTX.location = {};

// expose XMLHttpRequest

module.exports = XMLHttpRequest;

/**
 * XMLHttpRequest
 */

function XMLHttpRequest() {
  var self = this;
  // titanium xhr client
  this._proxy =  Ti.Network.createHTTPClient();
  this.upload = {
    onprogress: function(){}
  }
  this._proxy.onsendstream = function(e){
    self.upload.onprogress({loaded:e.progress, total:1});
  }
}

XMLHttpRequest.prototype.__proto__ = Emitter.prototype;

XMLHttpRequest.prototype.abort = function() {
  this._proxy.abort();
};

XMLHttpRequest.prototype.open = function(method, url, async) {
  this._proxy.open(method, url, async);
};

XMLHttpRequest.prototype.getResponseHeader = function(name) {
  return this._proxy.getResponseHeader(name);
};

XMLHttpRequest.prototype.send = function(data) {
  this._proxy.send(data);
};

XMLHttpRequest.prototype.setRequestHeader = function(key, val) {
  this._proxy.setRequestHeader(key, val);
};

Object.defineProperties(XMLHttpRequest.prototype, {
   'onreadystatechange' : {
      set: function (val) {
        return this._proxy.onreadystatechange = val
      }
    },
    'readyState': {
      get: function () {
        return this._proxy.readyState;
      }
    },
    'responseText': {
      get: function () {
        return this._proxy.responseText;
      }
    },
    'responseXML': {
      get: function () {
        return this._proxy.responseXML;
      }
    },
    'status': {
      get: function () {
        return this._proxy.status;
      }
    }
});

XMLHttpRequest.prototype.getAllResponseHeaders = function() {
  return '';
};

Then you can just wrap it all

ti-superagent.js

require('./TI-xhrfix');
module.exports = require('superagent');
yuchi commented 9 years ago

This is in fact a great solution, but you’re not dealing with timeouts and Android ≠2xx issue. Could be nonetheless the right direction.

yuchi commented 9 years ago

BTW, where does that code comes from? Could add a small CC license to it or refer to the original author? I’d like to use it in the future.

euforic commented 9 years ago

I wrote it over a year ago. Had it on github but i no longer use titanium so it got dropped on accident. It was MIT though so go nuts , :-) I have a better version with timeouts and multipart I just have to dig it up. I will put it back up on github when I find it.