New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

djax

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

djax - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

test/browser/unit.djax.html

128

djax.js

@@ -1,2 +0,2 @@

;(function(undefined) {
(function(undefined) {
'use strict';

@@ -79,2 +79,4 @@

if (+xhr.readyState === 4) {
done = true;
if (timer)

@@ -84,3 +86,5 @@ clearTimeout(timer);

if (/^2/.test(xhr.status)) {
done = true;
data = xhr.responseText;
if (/json/.test(dataType)) {

@@ -100,6 +104,5 @@ try {

conclude = function(successes, errors) {
successes.forEach(function(fn) {
fn(data, 'ok', xhr);
fn(data, 'success', xhr);
});

@@ -136,4 +139,16 @@ };

opt.beforeSend(xhr, opt) === false
)
) {
done = true;
conclude = function(successes, errors) {
errors.forEach(function(fn) {
fn(
xhr,
'abort',
xhr.responseText
);
});
};
conclude(null, errors);
return xhr.abort();
}

@@ -143,2 +158,3 @@ // Check "timeout":

timer = setTimeout(function() {
done = true;
xhr.onreadystatechange = function() {};

@@ -158,70 +174,64 @@ xhr.abort();

// Promise:
var promise = {
xhr: xhr,
done: function(callback) {
xhr.done = function(callback) {
if (typeof callback === 'function')
successes.push(callback);
else if (Array.isArray(callback))
successes = successes.concat(callback);
else
throw new Error('Wrong arguments.');
// If the call has already been received:
if (done) {
if (typeof callback === 'function')
successes.push(callback);
conclude([callback]);
else if (Array.isArray(callback))
successes = successes.concat(callback);
else
throw new Error('Wrong arguments.');
conclude(callback);
}
// If the call has already been received:
if (done) {
if (typeof callback === 'function')
conclude([callback]);
else if (Array.isArray(callback))
conclude(callback);
}
return this;
};
xhr.fail = function(callback) {
if (typeof callback === 'function')
errors.push(callback);
else if (Array.isArray(callback))
errors = errors.concat(callback);
else
throw new Error('Wrong arguments.');
return this;
},
fail: function(callback) {
// If the call has already been received:
if (done) {
if (typeof callback === 'function')
errors.push(callback);
conclude(null, [callback]);
else if (Array.isArray(callback))
errors = errors.concat(callback);
else
throw new Error('Wrong arguments.');
conclude(null, callback);
}
// If the call has already been received:
if (done) {
if (typeof callback === 'function')
conclude(null, [callback]);
else if (Array.isArray(callback))
conclude(null, callback);
}
return this;
};
xhr.then = function(success, error) {
this.done(success);
this.fail(error);
return this;
},
then: function(success, error) {
this.done(success);
this.fail(error);
// If the call has already been received:
if (done)
conclude(
Array.isArray(success) ?
success :
typeof success === 'function' ?
[success] : null,
Array.isArray(error) ?
error :
typeof error === 'function' ?
[error] : null
);
// If the call has already been received:
if (done)
conclude(
Array.isArray(success) ?
success :
typeof success === 'function' ?
[success] : null,
Array.isArray(error) ?
error :
typeof error === 'function' ?
[error] : null
);
return this;
}
return this;
};
// Kill promise next frame:
setTimeout(function() {
for (var k in promise)
promise[k] = undefined;
}, 0);
return promise;
return xhr;
}
// Djax version:
ajax.version = '1.0.1';
// Check XMLHttpRequest presence:

@@ -228,0 +238,0 @@ if (typeof XMLHttpRequest !== 'undefined')

/**
* djax - A lightweight jQuery.ajax subset
* @version v1.0.0
* @version v1.0.1
* @link https://github.com/jacomyal/djax
* @license MIT
*/
(function(e){"use strict";function t(r,n){if(!t.xhr)throw new Error("XMLHttpRequest not found. You can specify which XMLHttpRequest you want to use by using `ajax.xhr = myXHR`.");var o=[],s=[];if("string"==typeof r)r={url:r},2===arguments.length&&("function"==typeof n?o.push(n):Array.isArray(n)&&(o=o.concat(n)));else if("object"!=typeof r||!r)throw new Error("Wrong arguments");"function"==typeof r.success?o.push(r.success):Array.isArray(r.success)&&(o=o.concat(r.success)),"function"==typeof r.error?s.push(r.error):Array.isArray(r.error)&&(s=s.concat(r.error));var i,a,u,f,c=!1,y=r.url,p=new t.xhr,d=r.type||"GET",h=r.dataType||"json",l=r.contentType||"application/x-www-form-urlencoded";if(!y||"string"!=typeof y)throw new Error("Wrong arguments");if(r.data){if("string"==typeof r.data)a=r.data;else if(/json/.test(l))a=JSON.stringify(r.data);else{a=[];for(i in r.data)a.push(encodeURIComponent(i)+"="+encodeURIComponent(r.data[i]));a=a.join("&")}/GET|DELETE/i.test(d)&&(y+=/\?/.test(y)?"&"+a:"?"+a,a="")}if(p.onreadystatechange=function(){if(4===+p.readyState)if(u&&clearTimeout(u),/^2/.test(p.status)){if(a=p.responseText,/json/.test(h))try{a=JSON.parse(p.responseText)}catch(e){return f=function(e,t){t.forEach(function(e){e(p,"parsererror")})},void f(null,s)}f=function(e){e.forEach(function(e){e(a,"ok",p)})},f(o)}else(f=function(e,t){t.forEach(function(e){e(p,+p.status?"error":"abort",p.responseText)})})(null,s)},p.open(d,y,!0),p.setRequestHeader("Content-Type",l),r.headers)for(i in r.headers)p.setRequestHeader(i,r.headers[i]);if("function"==typeof r.beforeSend&&r.beforeSend(p,r)===!1)return p.abort();r.timeout&&(u=setTimeout(function(){p.onreadystatechange=function(){},p.abort(),(f=function(e,t){t.forEach(function(e){e(p,"timeout")})})(null,s)},r.timeout)),p.send(a);var m={xhr:p,done:function(e){if("function"==typeof e)o.push(e);else{if(!Array.isArray(e))throw new Error("Wrong arguments.");o=o.concat(e)}return c&&("function"==typeof e?f([e]):Array.isArray(e)&&f(e)),this},fail:function(e){if("function"==typeof e)s.push(e);else{if(!Array.isArray(e))throw new Error("Wrong arguments.");s=s.concat(e)}return c&&("function"==typeof e?f(null,[e]):Array.isArray(e)&&f(null,e)),this},then:function(e,t){return this.done(e),this.fail(t),c&&f(Array.isArray(e)?e:"function"==typeof e?[e]:null,Array.isArray(t)?t:"function"==typeof t?[t]:null),this}};return setTimeout(function(){for(var t in m)m[t]=e},0),m}"undefined"!=typeof XMLHttpRequest&&(t.xhr=XMLHttpRequest),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=t),exports.ajax=t):"function"==typeof define&&define.amd?define("djax",[],function(){return t}):this.ajax=t}).call(this);
(function(){"use strict";function e(t,n){if(!e.xhr)throw new Error("XMLHttpRequest not found. You can specify which XMLHttpRequest you want to use by using `ajax.xhr = myXHR`.");var r=[],o=[];if("string"==typeof t)t={url:t},2===arguments.length&&("function"==typeof n?r.push(n):Array.isArray(n)&&(r=r.concat(n)));else if("object"!=typeof t||!t)throw new Error("Wrong arguments");"function"==typeof t.success?r.push(t.success):Array.isArray(t.success)&&(r=r.concat(t.success)),"function"==typeof t.error?o.push(t.error):Array.isArray(t.error)&&(o=o.concat(t.error));var s,i,u,a,f=!1,c=t.url,p=new e.xhr,y=t.type||"GET",d=t.dataType||"json",h=t.contentType||"application/x-www-form-urlencoded";if(!c||"string"!=typeof c)throw new Error("Wrong arguments");if(t.data){if("string"==typeof t.data)i=t.data;else if(/json/.test(h))i=JSON.stringify(t.data);else{i=[];for(s in t.data)i.push(encodeURIComponent(s)+"="+encodeURIComponent(t.data[s]));i=i.join("&")}/GET|DELETE/i.test(y)&&(c+=/\?/.test(c)?"&"+i:"?"+i,i="")}if(p.onreadystatechange=function(){if(4===+p.readyState)if(f=!0,u&&clearTimeout(u),/^2/.test(p.status)){if(f=!0,i=p.responseText,/json/.test(d))try{i=JSON.parse(p.responseText)}catch(e){return a=function(e,t){t.forEach(function(e){e(p,"parsererror")})},void a(null,o)}a=function(e){e.forEach(function(e){e(i,"success",p)})},a(r)}else(a=function(e,t){t.forEach(function(e){e(p,+p.status?"error":"abort",p.responseText)})})(null,o)},p.open(y,c,!0),p.setRequestHeader("Content-Type",h),t.headers)for(s in t.headers)p.setRequestHeader(s,t.headers[s]);return"function"==typeof t.beforeSend&&t.beforeSend(p,t)===!1?(f=!0,a=function(e,t){t.forEach(function(e){e(p,"abort",p.responseText)})},a(null,o),p.abort()):(t.timeout&&(u=setTimeout(function(){f=!0,p.onreadystatechange=function(){},p.abort(),(a=function(e,t){t.forEach(function(e){e(p,"timeout")})})(null,o)},t.timeout)),p.send(i),p.done=function(e){if("function"==typeof e)r.push(e);else{if(!Array.isArray(e))throw new Error("Wrong arguments.");r=r.concat(e)}return f&&("function"==typeof e?a([e]):Array.isArray(e)&&a(e)),this},p.fail=function(e){if("function"==typeof e)o.push(e);else{if(!Array.isArray(e))throw new Error("Wrong arguments.");o=o.concat(e)}return f&&("function"==typeof e?a(null,[e]):Array.isArray(e)&&a(null,e)),this},p.then=function(e,t){return this.done(e),this.fail(t),f&&a(Array.isArray(e)?e:"function"==typeof e?[e]:null,Array.isArray(t)?t:"function"==typeof t?[t]:null),this},p)}e.version="1.0.1","undefined"!=typeof XMLHttpRequest&&(e.xhr=XMLHttpRequest),"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=e),exports.ajax=e):"function"==typeof define&&define.amd?define("djax",[],function(){return e}):this.ajax=e}).call(this);

@@ -6,4 +6,7 @@ var gulp = require('gulp'),

header = require('gulp-header'),
gjslint = require('gulp-gjslint'),
browserify = require('gulp-browserify'),
mochaPhantomJS = require('gulp-mocha-phantomjs');
mochaPhantomJS = require('gulp-mocha-phantomjs'),
api = require('./test/api-mockup.js'),
server;

@@ -17,23 +20,29 @@ // Files

var jshintConfig = {
'-W055': true,
'-W040': true,
'-W064': true,
node: true,
browser: true
};
'-W055': true,
'-W040': true,
'-W064': true,
node: true,
browser: true
},
gjslintConfig = {
flags: ['--nojsdoc', '--disable 211,212']
};
return gulp.src(indexFile)
.pipe(jshint(jshintConfig))
.pipe(jshint.reporter('default'));
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'))
.pipe(gjslint(gjslintConfig))
.pipe(gjslint.reporter('console'), {fail: true});
});
gulp.task('build', function() {
var banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @link <%= pkg.homepage %>',
' * @license <%= pkg.license %>',
' */',
''].join('\n'),
pkg = require('./package.json');
var banner = ['/**',
' * <%= pkg.name %> - <%= pkg.description %>',
' * @version v<%= pkg.version %>',
' * @link <%= pkg.homepage %>',
' * @license <%= pkg.license %>',
' */',
''].join('\n'),
pkg = require('./package.json');

@@ -53,10 +62,24 @@ return gulp.src(indexFile)

gulp.task('test', ['build-tests'], function() {
gulp.task('run-test', ['build-tests'], function() {
// Launching API server
server = api.listen(8001);
// Launching mocha tests through phantomjs
var stream = mochaPhantomJS();
stream.write({path: 'http://localhost:8001/browser/unit.html'});
stream.on('error', function() {
// Tearing down server if an error occurred
server.close();
});
stream.end();
return stream;
})
});
gulp.task('test', ['run-test'], function() {
// Tests are over, we close the server
server.close();
});
// Macro tasks
gulp.task('default', ['lint', 'build']);
gulp.task('default', ['lint', 'test', 'build']);
{
"name": "djax",
"version": "1.0.0",
"version": "1.0.1",
"description": "A lightweight jQuery.ajax subset",
"main": "djax.js",
"scripts": {
"pretest": "forever start -m 1 test/api-mockup.js",
"posttest": "forever stop test/api-mockup.js",
"test": "gulp test || :",
"test": "gulp test",
"build": "gulp build"

@@ -30,5 +28,5 @@ },

"express": "^4.9.7",
"forever": "^0.11.1",
"gulp": "^3.8.8",
"gulp-browserify": "^0.5.0",
"gulp-gjslint": "^0.1.4",
"gulp-header": "^1.1.1",

@@ -39,4 +37,4 @@ "gulp-jshint": "^1.8.5",

"gulp-uglify": "^1.0.1",
"http": "0.0.0"
"jshint-stylish": "^1.0.0"
}
}
var express = require('express'),
bodyParser = require('body-parser'),
http = require('http'),
app = express(),

@@ -193,2 +192,9 @@ server;

server = http.createServer(app).listen('8001');
/**
* EXPORTING:
* **********
*/
module.exports = app;

@@ -6,8 +6,8 @@ var assert = require('assert'),

it('should work as ajax({ url, success, error })', function(done) {
ajax({
var res = ajax({
url: '/data/1',
success: function(data, textStatus, xhr) {
assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -23,8 +23,8 @@ done();

it('should work as ajax(url).then(success, error)', function(done) {
ajax('/data/1')
var res = ajax('/data/1')
.then(
function(data, textStatus, xhr) {
assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -39,8 +39,8 @@ done();

it('should work as ajax(url).done(success).fail(error)', function(done) {
ajax('/data/1')
var res = ajax('/data/1')
.done(
function(data, textStatus, xhr) {
assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -56,3 +56,3 @@ done();

it('should work as ajax(url).fail(error).done(success)', function(done) {
ajax('/data/1')
var res = ajax('/data/1')
.fail(

@@ -65,4 +65,4 @@ function() {

assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -72,2 +72,20 @@ done();

});
it('should work with res.done called after success', function(done) {
var res = ajax('/data/1')
.fail(
function() {
throw new Error('Unexpected error.');
})
.done(
function() {
res.done(function(data, textStatus, xhr) {
assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);
done();
});
});
});
});

@@ -77,8 +95,8 @@

it('should work with GET calls', function(done) {
ajax({
var res = ajax({
url: '/data/1',
success: function(data, textStatus, xhr) {
assert.deepEqual(data.result, { data: 'abcde', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -94,3 +112,3 @@ done();

it('should work with POST calls', function(done) {
ajax({
var res = ajax({
url: '/data/1',

@@ -104,4 +122,4 @@ type: 'POST',

assert.deepEqual(data.result, { data: 'DEFGH', id: '1' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -117,3 +135,3 @@ done();

it('should work with PUT calls', function(done) {
ajax({
var res = ajax({
url: '/data/',

@@ -127,4 +145,4 @@ type: 'PUT',

assert.deepEqual(data.result, { data: 'First entry', id: '3' });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -140,3 +158,3 @@ done();

it('should work with DELETE calls', function(done) {
ajax({
var res = ajax({
url: '/data/2',

@@ -146,4 +164,4 @@ type: 'DELETE',

assert.deepEqual(data, { ok: true });
assert(textStatus === 'ok');
assert(xhr instanceof ajax.xhr);
assert(textStatus === 'success');
assert(xhr === res);
assert(arguments.length === 3);

@@ -159,3 +177,3 @@ done();

it('should trigger the error callback when status is 404', function(done) {
ajax({
var res = ajax({
url: '/data/inexisting_id',

@@ -167,3 +185,3 @@ type: 'POST',

error: function(xhr, textStatus, errorThrown) {
assert(xhr instanceof ajax.xhr);
assert(xhr === res);
assert(textStatus === 'error');

@@ -170,0 +188,0 @@ assert(errorThrown === 'Bad request');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc