View on GitHub

spas

Super Proxy Asset Server

Download this project as a .zip file Download this project as a tar.gz file

Keep Your Old Code

You don't have to start over or even modify your existing JS code. Say for example that you are using a simple script tag to make a JSONP call like this:

<script>
  var doCoolStuff = function(data) {
    // Your awesome code is here
  }
</script>
// You should do this next part asynchronously
<script src="http://search.twitter.com/search.json?q=nko3&callback=doCoolStuff"></script>

Just wrap it with another function like this:

<script>
  var spasCallBack = function(data) {
  	doCoolStuff(data.searchTweets.result);
  }
  
  var doCoolStuff = function(data) {
    // Your awesome code is here
  }
</script>
// You should do this next part asynchronously
<script src="http://spas.yoursite.com/sample?callback=spasCallBack"></script>

Filter Your Results

This is a basic feature of spas, but it's very important and should not be overlooked. Even API's that allow you to select which properties are returned in the results will invariably have some cruft that you just don't need to send to the client. For example, this bundle:

var spashttp = require("spas-http");

exports.sample = {
  "searchTweets": {
    "resource": spashttp.request,
    "params": {
      "url": "http://search.twitter.com/search.json",
      "q":"spas",
      "lang": "en" 
    },
    "cacheduration": 3600
  }
};

Will return:

{
	"expires":"2013-05-31T17:11:20.417Z",
	"searchTweets":{
		"expires":"2013-05-31T17:11:20.474Z",
		"result":{
			"completed_in":0.067,
			"max_id":340499259960999940,
			"max_id_str":"340499259960999937",
			"next_page":"?page=2&max_id=340499259960999937&q=spas&lang=en",
			"page":1,
			"query":"spas",
			"refresh_url":"?since_id=340499259960999937&q=spas&lang=en",
			"results":[
				{
					"created_at":"Fri, 31 May 2013 16:05:24 +0000",
					"from_user":"Jen_Durham",
					"from_user_id":614768772,
					"from_user_id_str":"614768772",
					"from_user_name":"Jen Durham",
					"geo":null,
					"id":340499259960999940,
					"id_str":"340499259960999937",
					"iso_language_code":"en",
					"metadata":{
						"result_type":"recent"
					},
					"profile_image_url":"http://a0.twimg.com/profile_images/2913098521/3fa3895454551bde815731f6bfb4f8df_normal.jpeg",
					"profile_image_url_https":"https://si0.twimg.com/profile_images/2913098521/3fa3895454551bde815731f6bfb4f8df_normal.jpeg",
					"source":"<a href="http://twitter.com/download/android">Twitter for Android</a>",
					"text":"RT @REALTORS: RT @houselogic: With pool season comes pool safety. Here are some tips on making your pool safe http://t.co/I56sbF20qP"
				},
				// ... +49 more of these results objects
			],
			"results_per_page":15,
			"since_id":0,
			"since_id_str":"0",
			"size":11584
		},
		"cname":"searchTweets"
	},
	"secleft":3599
}

But maybe all we need are from_user and text. To easily do this we just add a filter property to the bundle description, like so:

var spashttp = require("spas-http");

exports.sample = {
  "searchTweets": {
    "resource": spashttp.request,
    "params": {
      "url": "http://search.twitter.com/search.json",
      "q":"spas",
      "lang": "en" 
    },
    "cacheduration": 3600,
    "filter": {
      "results": [{
        "from_user": true,
        "text": true }]
    }
  }
};

And that will give us a much leaner result:

{
	"expires":"2013-05-31T17:11:20.417Z",
	"searchTweets":{
		"expires":"2013-05-31T17:11:20.474Z",
		"result":{
			"results":[
				{
					"from_user":"Jen_Durham",
					"text":"RT @REALTORS: RT @houselogic: With pool season comes pool safety. Here are some tips on making your pool safe http://t.co/I56sbF20qP"
				},
				// ... +49 more of these much smaller results objects
			]
		},
		"cname":"searchTweets"
	},
	"secleft":3599
}

filter is a quick an easy way to make huge improvements in performance.