Skip to content

Commit

Permalink
integrate support for GAE dev
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Dec 12, 2014
1 parent 23d399c commit cdc3e2a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
12 changes: 11 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,17 @@ function makeWritableStream(dup, options, onComplete) {
module.exports.makeWritableStream = makeWritableStream;

function makeAuthorizedRequest(config) {
var authorize = gsa(config);
var authorize;
var IS_GAE_DEV = process.env.GAE_LONG_APP_ID && !process.env.GAE_VM;

if (IS_GAE_DEV) {
// Google App Engine Development mode doesn't required authorization.
authorize = function(reqOpts, callback) {
callback(null, reqOpts);
};
} else {
authorize = gsa(config);
}

function makeRequest(reqOpts, callback) {
var tokenRefreshAttempts = 0;
Expand Down
19 changes: 17 additions & 2 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

'use strict';

var http = require('http');
var https = require('https');
var streamEvents = require('stream-events');
var through = require('through2');
Expand Down Expand Up @@ -458,6 +459,8 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
* Make a request to the API endpoint. Properties to indicate a transactional or
* non-transactional operation are added automatically.
*
* @todo Handle non-HTTP 200 cases.
*
* @param {string} method - Datastore action (allocateIds, commit, etc.).
* @param {object=} body - Request configuration object.
* @param {function} callback - The callback function.
Expand All @@ -473,7 +476,8 @@ DatastoreRequest.prototype.allocateIds = function(incompleteKey, n, callback) {
* transaction.makeReq('commit', deleteRequest, function(err) {});
*/
DatastoreRequest.prototype.makeReq_ = function(method, body, callback) {
// TODO: Handle non-HTTP 200 cases.
var IS_GAE_DEV = process.env.GAE_LONG_APP_ID && !process.env.GAE_VM;

if (!callback) {
callback = body;
body = {};
Expand Down Expand Up @@ -514,7 +518,18 @@ DatastoreRequest.prototype.makeReq_ = function(method, body, callback) {
callback(err);
return;
}
var remoteStream = https.request(authorizedReqOpts, function(resp) {

var remoteStream;

if (IS_GAE_DEV) {
authorizedReqOpts.host = process.env.API_HOST;
authorizedReqOpts.port = process.env.GAE_SERVER_PORT;
remoteStream = http.request(authorizedReqOpts);
} else {
remoteStream = https.request(authorizedReqOpts);
}

remoteStream.on('response', function(resp) {
var buffer = new Buffer('');
resp.on('data', function(chunk) {
buffer = Buffer.concat([buffer, chunk]);
Expand Down
11 changes: 7 additions & 4 deletions test/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,14 @@ describe('Request', function() {
pbFakeMethodResponseDecode = function() {
done();
};
httpsRequestOverride = function(req, callback) {
httpsRequestOverride = function(req) {
var responseStream = duplexify();
callback(responseStream);
responseStream.emit('end');
return duplexify();
var requestStream = duplexify();
setImmediate(function() {
requestStream.emit('response', responseStream);
responseStream.emit('end');
});
return requestStream;
};
request.makeReq_('fakeMethod', util.noop);
});
Expand Down

0 comments on commit cdc3e2a

Please sign in to comment.