Skip to content

Commit

Permalink
Remove current-context API
Browse files Browse the repository at this point in the history
Change all current-context APIs to throw a helpful error.
  • Loading branch information
bajtos committed Aug 10, 2016
1 parent 5fd1766 commit b087c93
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 441 deletions.
41 changes: 41 additions & 0 deletions 3.0-RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,44 @@ We have removed `loopback#errorhandler` middleware, users should use `strong-err
```
See also strong-error-handler's [options](https://github.com/strongloop/strong-error-handler#options) and the [related code change](https://github.com/strongloop/loopback/pull/2411).
## Remove current context API and middleware
We have removed the following current-context-related APIs:
- `loopback.getCurrentContext`
- `loopback.createContext`
- `loopback.runInContext`
Additionaly, `loopback#context` middleware and `remoting.context` server
config were removed too.
To setup "current context" feature in your LoopBack 3.x application, you
should use [loopback-context](https://www.npmjs.com/package/loopback-context)
module:
1. Add `loopback-context` to your dependencies
2. Configure the new context middleware in your `server/middleware-config.json` file
```json
{
"initial": {
"loopback-context#per-request": {}
}
}
```
3. Replace all usages of `loopback.getCurrentContext` with the following:
```js
// at the top of your file
var LoopBackContext = require('loopback-context');

// in your code
var ctx = LoopBackContext.getCurrentContext();
if (ctx) {
// use the context
}
```
See also [loopback#2564](https://github.com/strongloop/loopback/pull/2564)
and the official [documentation](https://docs.strongloop.com/display/APIC/Using+current+context)
3 changes: 0 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ module.exports = function(grunt) {
common: {
src: ['common/**/*.js'],
},
browser: {
src: ['browser/**/*.js'],
},
server: {
src: ['server/**/*.js'],
},
Expand Down
17 changes: 0 additions & 17 deletions browser/current-context.js

This file was deleted.

32 changes: 32 additions & 0 deletions lib/current-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var juggler = require('loopback-datasource-juggler');
var remoting = require('strong-remoting');

module.exports = function(loopback) {
juggler.getCurrentContext =
remoting.getCurrentContext =
loopback.getCurrentContext = function() {
throw new Error(
'loopback.getCurrentContext() was removed in version 3.0. See ' +
'https://docs.strongloop.com/display/APIC/Using%20current%20context ' +
'for more details.');
};

loopback.runInContext = function(fn) {
throw new Error(
'loopback.runInContext() was removed in version 3.0. See ' +
'https://docs.strongloop.com/display/APIC/Using%20current%20context ' +
'for more details.');
};

loopback.createContext = function(scopeName) {
throw new Error(
'loopback.createContext() was removed in version 3.0. See ' +
'https://docs.strongloop.com/display/APIC/Using%20current%20context ' +
'for more details.');
};
};
2 changes: 1 addition & 1 deletion lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ loopback.template = function(file) {
});
};

require('../server/current-context')(loopback);
require('../lib/current-context')(loopback);

/**
* Create a named vanilla JavaScript class constructor with an attached
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"bluebird": "^3.1.1",
"body-parser": "^1.12.0",
"canonical-json": "0.0.4",
"continuation-local-storage": "^3.1.3",
"debug": "^2.1.2",
"depd": "^1.0.0",
"ejs": "^2.3.1",
Expand Down Expand Up @@ -82,6 +81,7 @@
"karma-phantomjs-launcher": "^1.0.0",
"karma-script-launcher": "^1.0.0",
"loopback-boot": "^2.7.0",
"loopback-context": "^1.0.0",
"mocha": "^3.0.0",
"phantomjs-prebuilt": "^2.1.7",
"sinon": "^1.13.0",
Expand All @@ -97,7 +97,6 @@
"browser": {
"express": "./lib/browser-express.js",
"./lib/server-app.js": "./lib/browser-express.js",
"./server/current-context.js": "./browser/current-context.js",
"connect": false,
"nodemailer": false,
"supertest": false,
Expand Down
141 changes: 0 additions & 141 deletions server/current-context.js

This file was deleted.

57 changes: 5 additions & 52 deletions server/middleware/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,8 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

var loopback = require('../../lib/loopback');

module.exports = context;

var name = 'loopback';

/**
* Context middleware.
* ```js
* var app = loopback();
* app.use(loopback.context(options);
* app.use(loopback.rest());
* app.listen();
* ```
* @options {Object} [options] Options for context
* @property {String} name Context scope name.
* @property {Boolean} enableHttpContext Whether HTTP context is enabled. Default is false.
* @header loopback.context([options])
*/

function context(options) {
options = options || {};
var scope = options.name || name;
var enableHttpContext = options.enableHttpContext || false;
var ns = loopback.createContext(scope);

// Return the middleware
return function contextHandler(req, res, next) {
if (req.loopbackContext) {
return next();
}

loopback.runInContext(function processRequestInContext(ns, domain) {
req.loopbackContext = ns;

// Bind req/res event emitters to the given namespace
ns.bindEmitter(req);
ns.bindEmitter(res);

// Add req/res event emitters to the current domain
domain.add(req);
domain.add(res);

// Run the code in the context of the namespace
if (enableHttpContext) {
// Set up the transport context
ns.set('http', { req: req, res: res });
}
next();
});
};
}
module.exports = function() {
throw new Error('loopback#context middleware was removed in version 3.0. ' +
'See https://docs.strongloop.com/display/APIC/Using%20current%20context ' +
'for more details.');
};
10 changes: 5 additions & 5 deletions server/middleware/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ function rest() {
var remotingOptions = app.get('remoting') || {};

var contextOptions = remotingOptions.context;
if (contextOptions !== false) {
if (typeof contextOptions !== 'object') {
contextOptions = {};
}
handlers.push(loopback.context(contextOptions));
if (contextOptions !== undefined && contextOptions !== false) {
throw new Error(
'remoting.context option was removed in version 3.0. See ' +
'https://docs.strongloop.com/display/APIC/Using%20current%20context ' +
'for more details.');
}

if (app.isAuthEnabled) {
Expand Down
2 changes: 1 addition & 1 deletion server/middleware/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function token(options) {
TokenModel.findForRequest(req, options, function(err, token) {
req.accessToken = token || null;
rewriteUserLiteral(req, currentUserLiteral);
var ctx = loopback.getCurrentContext();
var ctx = req.loopbackContext;
if (ctx) ctx.set('accessToken', token);
next(err);
});
Expand Down
7 changes: 5 additions & 2 deletions test/access-token.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// License text available at https://opensource.org/licenses/MIT

var cookieParser = require('cookie-parser');
var LoopBackContext = require('loopback-context');
var contextMiddleware = require('loopback-context').perRequest;
var loopback = require('../');
var extend = require('util')._extend;
var Token = loopback.AccessToken.extend('MyToken');
Expand Down Expand Up @@ -477,7 +479,8 @@ describe('app.enableAuth()', function() {
it('stores token in the context', function(done) {
var TestModel = loopback.createModel('TestModel', { base: 'Model' });
TestModel.getToken = function(cb) {
cb(null, loopback.getCurrentContext().get('accessToken') || null);
var ctx = LoopBackContext.getCurrentContext();
cb(null, ctx && ctx.get('accessToken') || null);
};
TestModel.remoteMethod('getToken', {
returns: { arg: 'token', type: 'object' },
Expand All @@ -488,7 +491,7 @@ describe('app.enableAuth()', function() {
app.model(TestModel, { dataSource: null });

app.enableAuth();
app.use(loopback.context());
app.use(contextMiddleware());
app.use(loopback.token({ model: Token }));
app.use(loopback.rest());

Expand Down
Loading

0 comments on commit b087c93

Please sign in to comment.