Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
sleewoo committed Jan 5, 2015
2 parents f0efb02 + 20b1356 commit 2fa574e
Show file tree
Hide file tree
Showing 78 changed files with 4,499 additions and 6,511 deletions.
192 changes: 192 additions & 0 deletions 0.11-migration-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# v0.11 Migration Guide


**tldr;**

v0.11 comes with many minor improvements, as well as some internal cleanup in core. The biggest change is that Sails core is now using Socket.io v1.

Almost none of this should affect the existing code in project, but there are a few important differences and new features to be aware of. We've listed them below.


## Differences

#### Upgrade the Socket.io / Sails.io browser client

Old v0.9 socket.io client will no longer work, so consequently you'll need to upgrade your sails.io.js client from v0.9 or v0.10 to v0.11.

To do this, just remove your sails.io.js client and install the new one. We've bundled a helper generator that will do this for you, assuming your sails.io.js client is in the conventional location at `assets/js/dependencies/sails.io.js` (i.e. if you haven't moved or renamed it):

```sh
sails generate sails.io.js --force
```


#### `onConnect` lifecycle callback

> **tldr;**
>
> Remove your `onConnect` function from `config/sockets.js`.
The `onConnect` lifecycle callback has been deprecated. Instead, if you need to do something when a new socket is connected, send a request from the newly-connected client to do so. The purpose of `onConnect` was always for optimizing performance (eliminating the need to do this initial extra round-trip with the server), yet its use can lead to confusion and race conditions. If you desperately need to eliminate the server roundtrip, you can bind a handler directly on `sails.io.on('connect', function (newlyConnectedSocket){})` in your bootstrap function (`config/bootstrap.js`). However, note that this is discouraged. Unless you're facing _true_ production performance issues, you should use the strategy mentioned above for your "on connection" logic (i.e. send an initial request from the client after the socket connects). Socket requests are lightweight, so this doesn't add any tangible overhead to your application, and it will help make your code more predictable.



#### `onDisconnect` lifecycle callback

The `onDisconnect` lifecycle callback has been deprecated in favor of `afterDisconnect`.

If you were using `onDisconnect` previously, you might have had to change the `session`, then call `session.save()` manually. In v0.11, this works in almost exactly the same way, except that `afterDisconnect` receives an additional 3rd argument: a callback function. This way, you can just call the provided callback when your `afterDisconnect` logic has finished, so that Sails can persist any changes you've made to the session automatically. Finally, as you might expect, you won't need to call `session.save()` manually anymore- it is now taken care of for you (just like `req.session` in a normal route, action, or policy.)


> **tldr;**
> Rename your `onDisconnect` function in `config/sockets.js` with the following:
>
> ```
> afterDisconnect: function (session, socket, cb) {
> // Be sure to call the callback
> return cb();
> }
> ```
#### Other configuration in `config/sockets.js`
Many of the configuration options in Socket.io v1 have changed, so you'll want to update your `config/sockets.js` file accordingly.
+ if you were using a custom `authorization` function to restrict socket connections, you'll now want to use `beforeConnect`. `authorization` was deprecated by Socket.io v1, but `beforeConnect` (which maps to the `allowRequest` option from Engine.io) works just the same way.
+ if you were using other low-level socket configuration that was passed directly to socket.io v1, be sure and check out the [reference page on sailsjs.org](http://sailsjs.org/#/documentation/reference/sails.config/sails.config.sockets.html) where all of the new configuration options are covered in detail.
#### The "firehose"
The "firehose" feature for testing with sockets has been deprecated. If you don't know what that means, you have nothing to worry about. The basic usage will continue to work for a while, but it will soon be removed from core and should not be relied upon in your app. This also applies to the following methods:
+ sails.sockets.subscribeToFirehose()
+ sails.sockets.unsubscribeFromFirehose()
+ sails.sockets.drink()
+ sails.sockets.spit()
+ sails.sockets.squirt()
> If you want the "firehose" back, let [Mike know on twitter](http://twitter.com/mikermcneil) (it can be brought back as a separate hook).
## New features
Sails v0.11 also comes with some new stuff that we thought you'd like to know about:
#### User-level hooks
Hooks can now be installed directly from NPM.
This means you can now install hooks with a single command in your terminal. For instance, consider the [`autoreload` hook](https://github.com/sgress454/sails-hook-autoreload) by [@sgress454](https://twitter.com/sgress454), which watches for changes to your backend code so you don't need to kill and re-lift the server every time you change your controllers, routes, models, etc.
To install the `autoreload` hook, run:
```sh
npm install sails-hook-autoreload
```
This is just one example of what's possible. As you might already know, hooks are the lowest-level pluggable abstraction in Sails. They allow authors to tap into the lift process, listen for events, inject custom "shadow" routes, and, in general, take advantage of raw access to the `sails` runtime.
Most of the features you're familiar with in Sails have actually already been implemented as "core" hooks for over a year, including:

+ `blueprints` _(which provides the blueprint API)_
+ `sockets` _(which provides socket.io integration)_
+ `grunt` _(which provides Grunt integration)_
+ `orm` _(which provides integration with the Waterline ORM, and imports your projects adapters, models, etc.)_
+ `http` _(which provides an HTTP server)_
+ and 16 others.

You can read more about how to write your own hooks in the new and improved "Extending Sails" documentation on http://sailsjs.org.


#### Socket.io v1.x

The upgrade to Socket.io v1.0 shouldn't actually affect your app-level code, provided you are using the layer of abstraction provided by Sails itself; everything from the `sails.sockets.*` wrapper methods and "up" (resourceful pubsub, blueprints)
If you are using underlying socket.io methods in your apps, or are just curious about what changed in Socket.io v1.0, be sure and check out the [complete Socket.io 1.0 migration guide](http://socket.io/docs/migrating-from-0-9/) from Guillermo and the socket.io team.

#### Ever-increasing modularity

As part of the upgrade to Socket.io v1.0, we pulled out the core `sockets` hook into a separate repository. This allowed us to write some modular, hook-specific tests for the socket.io interpreter, which will make things easier to maintain, customize, and override.
This also allows the hook to grow at its own pace, and puts related issues in one place.

Consider this a test of the pros and cons of pulling other hooks out of the sails core repo over the next few months. This will make Sails core lighter, faster, and more extensible, with fewer core dependencies, shorter "lift" time for most apps, and faster `npm install`s.


#### Testing, the "virtual" request interpreter, and the `sails.request()` method

In the process of pulling the `sockets` hook _out_ of core, the logic which interprets requests has been normalized and is now located _in_ Sails core. As a result, the `sails.request()` method is much more powerful.

This method allows you to communicate directly with the request interpreter in Sails without lifting your server onto a port. It's the same mechanism that Sails uses to map incoming messages from Socket.io to "virtual requests" that have the familiar `req` and `res` streams.

The primary use case for `sails.request()` is in writing faster-running unit and integration tests, but it's also handy for proxying to mounted apps (or "sub-apps").

For instance, here is an example (using mocha) of how you might test one of your app's routes:

```js
var assert = require('assert');
var Sails = require('sails').Sails;

before(function beforeRunningAnyTests (done){

// Load the app (no need to "lift" to a port)
sails.load({
log: {
level: 'warn'
},
hooks: {
grunt: false
}
}, function whenAppIsReady(err){
if (err) return done(err);

// At this point, the `sails` global is exposed, although we
// could have disabled it above with our config overrides to
// `sails.load()`. In fact, you can actually use this technique
// to set any configuration setting you like.
return done();
});
});

after(function afterTestsFinish (done) {
sails.lower(done);
});

describe('GET /hotpockets', function (){

it('should respond with a 200 status code', function (done){

sails.request({
method: 'get',
url: '/hotpockets',
params: {
limit: 10,
sort: 'price ASC'
}
}, function (err, clientRes, body) {
if (err) return done(err);

assert.equal(clientRes.statusCode, 200);
return done();
});

});
});
```


## Questions?

As always, if you run into issues upgrading, or if any of the notes above don't make sense, let me know and I'll do what I can to clarify.

Finally, to those of you that have contributed to the project since the v0.10 release in August: I can't stress enough how much I value your continued support and encouragement. There is a pretty massive stream of issues, pull requests, documentation tweaks, and questions, but it always helps to know that we're in this together :)

Thanks.

-@mikermcneil


8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### master

* [ENHANCEMENT] Support partials and layout with Handlebars for the `backend` generator
* [BUGFIX] Blueprint creation returns 201 status code instead of 200
* [BUGFIX] `ractive.toHTML()` replaces `ractive.renderHTML()` for Ractive template engine
* [BUGFIX] Fix arguments for publishAdd, publishRemove and publishUpdate
* [ENHANCEMENT] Enable views hook for all methods
* [BUGFIX] Resolve depreciation warnings
* [BUGFIX] Fix dependency for npm 2.0.0
* [BUGFIX] Fix Grunt launching when it's a peer dep
* [ENHANCEMENT] Upgrade express and skipper because of security vulnerabilities
* [BUGFIX] Fix Sails crashes if Redis goes down [#2277](https://github.com/balderdashy/sails/pull/2277)
* [BUGFIX] Fix crash when using sessionless requests over WebSockets [#2107](https://github.com/balderdashy/sails/pull/2107)
Expand Down
2 changes: 1 addition & 1 deletion MODULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Below, you'll find an overview of the modules maintained by the core team and co
| [**waterline**](http://github.com/balderdashy/waterline) | [![Build Status](https://travis-ci.org/balderdashy/waterline.png?branch=master)](https://travis-ci.org/balderdashy/waterline) | [![NPM version](https://badge.fury.io/js/waterline.png)](http://badge.fury.io/js/waterline) |
| [**anchor**](http://github.com/balderdashy/anchor) | [![Build Status](https://travis-ci.org/balderdashy/anchor.png?branch=master)](https://travis-ci.org/balderdashy/anchor) | [![NPM version](https://badge.fury.io/js/anchor.png)](http://badge.fury.io/js/anchor) |
| [**waterline-criteria**](http://github.com/balderdashy/waterline-criteria) | [![Build Status](https://travis-ci.org/balderdashy/waterline-criteria.png?branch=master)](https://travis-ci.org/balderdashy/waterline-criteria) | [![NPM version](https://badge.fury.io/js/waterline-criteria.png)](http://badge.fury.io/js/waterline-criteria) |
| [**waterline-errors**](http://github.com/balderdashy/waterline-errors) | NEEDS_TRAVIS_SETUP | [![NPM version](https://badge.fury.io/js/waterline-errors.png)](http://badge.fury.io/js/waterline-errors) |
| [**waterline-errors**](http://github.com/vanetix/waterline-errors) | NEEDS_TRAVIS_SETUP | [![NPM version](https://badge.fury.io/js/waterline-errors.png)](http://badge.fury.io/js/waterline-errors) |
| [**waterline-schema**](http://github.com/balderdashy/waterline-schema) | NEEDS_TRAVIS_SETUP | [![NPM version](https://badge.fury.io/js/waterline-schema.png)](http://badge.fury.io/js/waterline-schema) |
| [**sails-generate**](http://github.com/balderdashy/sails-generate) | [![Build Status](https://travis-ci.org/balderdashy/sails-generate.png?branch=master)](https://travis-ci.org/balderdashy/sails-generate) | [![NPM version](https://badge.fury.io/js/sails-generate.png)](http://badge.fury.io/js/sails-generate) |
| [**sails-build-dictionary**](http://github.com/balderdashy/sails-build-dictionary) | N/A | [![NPM version](https://badge.fury.io/js/sails-build-dictionary.png)](http://badge.fury.io/js/sails-build-dictionary) |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### [Website](http://sailsjs.org/)   [Getting Started](http://sailsjs.org/#!getStarted)   [Docs](http://sailsjs.org/#!documentation)   [Submit Issue](https://github.com/balderdashy/sails/blob/master/README.md#issue-submission)

[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/balderdashy/sails?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)


Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.

Expand Down
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The backlog consists of features which are not currently in the immediate-term r
Feature | Owner | Details
:---------------------------------------------- | :------------------------------------------------- | :------
Watch+reload controllers, models, etc. w/o re-lifting | [@jbielick](https://github.com/jbielick) | Reload controllers/models/config/services/etc. without restarting the server. Show a "rebuilding" page while re-bootstrapping.
Support for multiple blueprint prefixes | [@mnaughto](https://github.com/mnaughto) | https://github.com/balderdashy/sails/issues/2031
Support for multiple blueprint prefixes | [@mnaughto](https://github.com/konstantinzolotarev) | https://github.com/balderdashy/sails/issues/2031
SPDY protocol support | [@mikermcneil](https://github.com/mikermcneil) | https://github.com/balderdashy/sails/issues/80
Sockets hook: drop-in Primus alternative | [@alejandroiglesias](https://github.com/alejandroiglesias) | https://github.com/balderdashy/sails/issues/945
Have a `sails migrate` or `sails create-db` command | [@globegitter](https://github.com/Globegitter) | For production environments it would be nice to have a save/secure command that creates the db automatically for you
3 changes: 1 addition & 2 deletions bin/sails-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ module.exports = function() {
// Pass the original CLI arguments down to the generator
// (but first, remove commander's extra argument)
// (also peel off the `generatorType` arg)
var cliArguments = Array.prototype.slice.call(arguments);
cliArguments.pop();
var cliArguments = _.initial(arguments);
scope.generatorType = cliArguments.shift();
scope.args = cliArguments;

Expand Down
5 changes: 4 additions & 1 deletion bin/sails-lift.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ module.exports = function() {


function afterwards (err, sails) {
if (err) { sails ? sails.log.error(err) : log.error(err); process.exit(1); }
if (err) {
var message = err.stack ? err.stack : err;
sails ? sails.log.error(message) : log.error(message); process.exit(1);
}
// try {console.timeEnd('cli_lift');}catch(e){}
}
};
Expand Down
4 changes: 2 additions & 2 deletions bin/sails-www.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var CaptainsLog = require('captains-log');
var Sails = require('../lib/app');
var rconf = require('../lib/app/configuration/rc');
var __Grunt = require('../lib/hooks/grunt');

var Err = require('../errors');

/**
* `sails www`
Expand All @@ -36,7 +36,7 @@ module.exports = function() {
if (err) return Err.fatal.failedToLoadSails(err);

var overrideGruntTask = (sails.config.environment == 'production' ? GRUNT_TASK_PROD_NAME : GRUNT_TASK_NAME)

// Run Grunt task
var Grunt = __Grunt(sails);

Expand Down
2 changes: 1 addition & 1 deletion lib/EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Called when a route is unbound.

### Runtime

> NOTE: these event should only be relied on by attached servers without their own routers, or when a hook
> NOTE: these events should only be relied on by attached servers without their own routers, or when a hook
> implementation prefers to use the built-in Sails router.
>
> The optimal behavior for the http hook implemented on Express, for instance, is to listen to `router:bind`
Expand Down
24 changes: 17 additions & 7 deletions lib/app/Sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,26 @@ Sails.prototype.getBaseUrl = Sails.prototype.getBaseurl;
Sails.prototype.request = require('./request');

// Expose Express-esque synonyms for low-level usage of router
var _routerBindWrapper = function (path, action) {
Sails.prototype.all = function(path, action) {
this.router.bind(path, action);
return this;
};
Sails.prototype.all = _routerBindWrapper;
Sails.prototype.get = _routerBindWrapper;
Sails.prototype.post = _routerBindWrapper;
Sails.prototype.put = _routerBindWrapper;
Sails.prototype.del = Sails.prototype['delete'] = _routerBindWrapper;

Sails.prototype.get = function(path, action) {
this.router.bind(path, action, 'get');
return this;
};
Sails.prototype.post = function(path, action) {
this.router.bind(path, action, 'post');
return this;
};
Sails.prototype.put = function(path, action) {
this.router.bind(path, action, 'put');
return this;
};
Sails.prototype.del = Sails.prototype['delete'] = function(path, action) {
this.router.bind(path, action, 'delete');
return this;
};

// Private methods:
////////////////////////////////////////////////////////
Expand Down
33 changes: 33 additions & 0 deletions lib/app/configuration/default-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Default hooks
*
* (order still matters for now for some of these)
*
* TODO:
* make order _not_ matter
* (it pretty much doesn't already b/c of our use of events...
* ...but for a few core hooks, e.g. `moduleloader`, it still does)
*/

module.exports = {
'moduleloader': true,
'logger': true,
'request': true,
'orm': true,
'views': true,
'blueprints': true,
'responses': true,
'controllers': true,
'sockets': 'sails-hook-sockets',
'pubsub': true,
'policies': true,
'services': true,
'csrf': true,
'cors': true,
'i18n': true,
'userconfig': true,
'session': true,
'grunt': true,
'http': true,
'userhooks': true
};
28 changes: 0 additions & 28 deletions lib/app/configuration/defaultHooks.js

This file was deleted.

Loading

0 comments on commit 2fa574e

Please sign in to comment.