-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Small fixes; Added build files and tests
- Loading branch information
Showing
11 changed files
with
340 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bower_components | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "z-sails", | ||
"version": "0.0.0", | ||
"homepage": "https://github.com/Zeroto/z-sails", | ||
"authors": [ | ||
"Sander Homan <[email protected]>" | ||
], | ||
"description": "A $httpBackend decorator to use sails socket requests", | ||
"main": "dist/z-sails.min.js", | ||
"keywords": [ | ||
"Angular", | ||
"Sails", | ||
"$http" | ||
], | ||
"license": "MIT", | ||
"ignore": [ | ||
"**/.*", | ||
"node_modules", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
], | ||
"dependencies": { | ||
"angular": "~1.2.20" | ||
}, | ||
"devDependencies": { | ||
"angular-mocks": "~1.2.20" | ||
} | ||
} |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Created by Zerot on 7/23/14. | ||
*/ | ||
|
||
(function(module){ | ||
|
||
module.provider('z-sails', [function(){ | ||
var self = this; | ||
self.useFileCheck = true; | ||
self.useFallback = false; | ||
|
||
this.$get = function(){ | ||
return { | ||
useFileCheck: self.useFileCheck, | ||
useFallback: self.useFallback | ||
}; | ||
}; | ||
}]); | ||
module.config(['$provide', function($provide){ | ||
|
||
// replace $httpBackend to have it put out sails socket requests instead of XHR | ||
$provide.decorator('$httpBackend', ['$delegate', '$browser', 'z-sails', '$window', function($delegate, $browser, zsails, $window){ | ||
var $httpBackend = function(method, url, post, callback, headers, timeout, withCredentials, responseType){ | ||
$browser.$$incOutstandingRequestCount(); | ||
url = url || $browser.url(); | ||
|
||
var lowercaseUrl; | ||
if (zsails.useFileCheck) | ||
lowercaseUrl = angular.lowercase(url); | ||
|
||
var methodLowercase = angular.lowercase(method); | ||
if ((methodLowercase !== 'get' && methodLowercase !== 'post' && methodLowercase !== 'put' && methodLowercase !== 'delete') || | ||
(zsails.useFileCheck && lowercaseUrl.length > 5 && (lowercaseUrl[lowercaseUrl.length-5] == '.' || lowercaseUrl[lowercaseUrl.length-4] == '.'))) //check if file. files will have a . at 3rd or 4th last character | ||
{ | ||
return $delegate(method, url, post, callback, headers, timeout, withCredentials, responseType); | ||
} | ||
else | ||
{ | ||
$window.io.socket[methodLowercase](url, angular.fromJson(post), function(data, jwr){ | ||
console.log(jwr); | ||
if (zsails.useFallback && jwr.statusCode != 200) | ||
{ | ||
$delegate(method, url, post, callback, headers, timeout, withCredentials, responseType); | ||
} | ||
else | ||
callback(jwr.statusCode, data, jwr.headers, ""); | ||
}); | ||
} | ||
|
||
}; | ||
|
||
$httpBackend.originalBackend = $delegate; // expose the original backend | ||
|
||
return $httpBackend; | ||
}]); | ||
|
||
}]); | ||
|
||
})(angular.module('z-sails', [])); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
var gulp = require('gulp'); | ||
|
||
var rename = require('gulp-rename'); | ||
var uglify = require('gulp-uglify'); | ||
var concat = require('gulp-concat'); | ||
var karma = require('gulp-karma'); | ||
var sourcemaps = require('gulp-sourcemaps'); | ||
var del = require('del'); | ||
|
||
var paths = { | ||
scripts: ['src/**/*.js'], | ||
}; | ||
|
||
gulp.task('clean', function(cb) { | ||
// You can use multiple globbing patterns as you would with `gulp.src` | ||
del(['dist'], cb); | ||
}); | ||
|
||
gulp.task('scripts', ['clean'], function() { | ||
// Minify and copy all JavaScript | ||
// with sourcemaps all the way down | ||
return gulp.src(paths.scripts) | ||
.pipe(sourcemaps.init()) | ||
.pipe(concat('z-sails.js')) | ||
.pipe(gulp.dest('dist')) | ||
.pipe(rename('z-sails.min.js')) | ||
.pipe(uglify()) | ||
.pipe(sourcemaps.write('maps')) | ||
.pipe(gulp.dest('dist')); | ||
}); | ||
|
||
gulp.task('test', function(){ | ||
return gulp.src('foobar') // invalid name so karma still loads from config | ||
.pipe(karma({ | ||
configFile: 'karma.conf.js', | ||
action: 'run' | ||
})); | ||
}); | ||
|
||
gulp.task('build', ['scripts']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Karma configuration | ||
// Generated on Thu Jul 24 2014 14:07:10 GMT+0200 (W. Europe Daylight Time) | ||
|
||
module.exports = function(config) { | ||
config.set({ | ||
|
||
// base path that will be used to resolve all patterns (eg. files, exclude) | ||
basePath: '', | ||
|
||
|
||
// frameworks to use | ||
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter | ||
frameworks: ['jasmine'], | ||
|
||
|
||
// list of files / patterns to load in the browser | ||
files: [ | ||
'bower_components/angular/angular.js', | ||
'bower_components/angular-mocks/angular-mocks.js', | ||
'dist/z-sails.min.js', | ||
'test/**/*.js' | ||
], | ||
|
||
|
||
// list of files to exclude | ||
exclude: [ | ||
], | ||
|
||
|
||
// preprocess matching files before serving them to the browser | ||
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor | ||
preprocessors: { | ||
}, | ||
|
||
|
||
// test results reporter to use | ||
// possible values: 'dots', 'progress' | ||
// available reporters: https://npmjs.org/browse/keyword/karma-reporter | ||
reporters: ['progress'], | ||
|
||
|
||
// web server port | ||
port: 9876, | ||
|
||
|
||
// enable / disable colors in the output (reporters and logs) | ||
colors: true, | ||
|
||
|
||
// level of logging | ||
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | ||
logLevel: config.LOG_INFO, | ||
|
||
|
||
// enable / disable watching file and executing tests whenever any file changes | ||
autoWatch: false, | ||
|
||
|
||
// start these browsers | ||
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher | ||
browsers: ['Chrome'], | ||
|
||
|
||
// Continuous Integration mode | ||
// if true, Karma captures browsers, runs the tests and exits | ||
singleRun: false | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "z-sails", | ||
"version": "0.0.0", | ||
"description": "A $httpBackend decorator to use sails socket requests instead of XHR", | ||
"main": "dist/z-sails.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/Zeroto/z-sails.git" | ||
}, | ||
"keywords": [ | ||
"Angular", | ||
"Sails", | ||
"$http" | ||
], | ||
"author": "Sander Homan", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/Zeroto/z-sails/issues" | ||
}, | ||
"homepage": "https://github.com/Zeroto/z-sails", | ||
"devDependencies": { | ||
"gulp": "^3.8.6", | ||
"gulp-concat": "^2.3.4", | ||
"gulp-uglify": "^0.3.1", | ||
"gulp-sourcemaps": "^1.1.0", | ||
"del": "^0.1.1", | ||
"gulp-rename": "^1.2.0", | ||
"karma-jasmine": "^0.1.5", | ||
"gulp-karma": "0.0.4", | ||
"gulp-jasmine": "^0.2.0", | ||
"gulp-jshint": "^1.7.1", | ||
"karma": "^0.12.17", | ||
"karma-chrome-launcher": "^0.1.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
describe("z-sails", function(){ | ||
|
||
beforeEach(module('z-sails')); | ||
|
||
var $http, zSails, $httpBackend, $window, socket, $rootScope; | ||
beforeEach(inject(['$injector', function($injector){ | ||
$http = $injector.get('$http'); | ||
$httpBackend = $injector.get('$httpBackend').originalBackend; | ||
$window = $injector.get('$window'); | ||
$rootScope = $injector.get('$rootScope'); | ||
|
||
// set up the mock socket | ||
socket = { | ||
get: function(_, _, cb){cb(null, {statusCode: 404, headers: null})}, | ||
post: function(_, _, cb){cb(null, {statusCode: 404, headers: null})}, | ||
put: function(_, _, cb){cb(null, {statusCode: 404, headers: null})}, | ||
delete: function(_, _, cb){cb(null, {statusCode: 404, headers: null})}, | ||
} | ||
|
||
|
||
|
||
$window.io = { | ||
socket: socket | ||
}; | ||
}])); | ||
|
||
beforeEach(inject(['$injector', function($injector){ | ||
zSails = $injector.get('z-sails'); | ||
}])); | ||
|
||
afterEach(function() { | ||
$httpBackend.verifyNoOutstandingExpectation(); | ||
$httpBackend.verifyNoOutstandingRequest(); | ||
}); | ||
|
||
it ('should use XHR when requesting a file when using the filecheck strategy', function(){ | ||
|
||
zSails.useFileCheck = true; | ||
zSails.useFallback = false; | ||
|
||
$httpBackend.expectGET('/template.html').respond(200, ''); | ||
$http({url: '/template.html', method: 'GET'}); | ||
|
||
$httpBackend.flush(); | ||
}); | ||
|
||
it ('should use sockets when requesting a resource when using the filecheck strategy', function(){ | ||
|
||
zSails.useFileCheck = true; | ||
zSails.useFallback = false; | ||
|
||
spyOn(socket, "get").andCallThrough(); | ||
|
||
$http({url: '/resource', method: 'GET'}).finally(function(){ | ||
expect(socket.get).toHaveBeenCalled(); | ||
}); | ||
|
||
$rootScope.$digest(); | ||
}); | ||
|
||
it ('should use sockets and XHR when using the fallback strategy', function(){ | ||
|
||
zSails.useFileCheck = false; | ||
zSails.useFallback = true; | ||
|
||
spyOn(socket, "get").andCallThrough(); | ||
|
||
$httpBackend.expectGET('/template.html').respond(200, ''); | ||
|
||
$http({url: '/template.html', method: 'GET'}).finally(function(){ | ||
expect(socket.get).toHaveBeenCalled(); | ||
}); | ||
|
||
$httpBackend.flush(); | ||
$rootScope.$digest(); | ||
}); | ||
|
||
it ('should use sockets and XHR when using both strategies when requesting a resource', function(){ | ||
|
||
zSails.useFileCheck = true; | ||
zSails.useFallback = true; | ||
|
||
spyOn(socket, "get").andCallThrough(); | ||
|
||
$httpBackend.expectGET('/resource').respond(200, ''); | ||
|
||
$http({url: '/resource', method: 'GET'}).finally(function(){ | ||
expect(socket.get).toHaveBeenCalled(); | ||
}); | ||
|
||
$httpBackend.flush(); | ||
$rootScope.$digest(); | ||
}); | ||
}); |