remove runtime dependency on request (#5076)

This commit is contained in:
Florent Vilmart
2018-09-23 12:31:08 -04:00
committed by GitHub
parent 4dc4a3a56d
commit 93a0017b25
13 changed files with 2706 additions and 2779 deletions

4809
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -28,6 +28,7 @@
"commander": "2.18.0",
"deepcopy": "1.0.0",
"express": "4.16.2",
"follow-redirects": "1.5.8",
"intersect": "1.0.1",
"lodash": "4.17.11",
"lru-cache": "4.1.3",
@@ -36,7 +37,6 @@
"parse": "2.1.0",
"pg-promise": "8.4.6",
"redis": "2.8.0",
"request": "2.88.0",
"semver": "5.5.1",
"tv4": "1.3.0",
"uuid": "^3.1.0",
@@ -69,6 +69,7 @@
"nodemon": "1.18.4",
"nyc": "^12.0.2",
"prettier": "1.14.3",
"request": "2.88.0",
"request-promise": "4.2.2",
"supports-color": "^5.4.0"
},

View File

@@ -50,91 +50,48 @@ describe('httpRequest', () => {
it('should do /hello', done => {
httpRequest({
url: httpRequestServer + '/hello',
}).then(
function(httpResponse) {
expect(httpResponse.status).toBe(200);
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
expect(httpResponse.text).toEqual('{"response":"OK"}');
expect(httpResponse.data.response).toEqual('OK');
done();
},
function() {
fail('should not fail');
done();
}
);
});
it('should do /hello with callback and promises', done => {
let calls = 0;
httpRequest({
url: httpRequestServer + '/hello',
success: function() {
calls++;
},
error: function() {
calls++;
},
}).then(
function(httpResponse) {
expect(calls).toBe(1);
expect(httpResponse.status).toBe(200);
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
expect(httpResponse.text).toEqual('{"response":"OK"}');
expect(httpResponse.data.response).toEqual('OK');
done();
},
function() {
fail('should not fail');
done();
}
);
}).then(function(httpResponse) {
expect(httpResponse.status).toBe(200);
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
expect(httpResponse.text).toEqual('{"response":"OK"}');
expect(httpResponse.data.response).toEqual('OK');
done();
}, done.fail);
});
it('should do not follow redirects by default', done => {
httpRequest({
url: httpRequestServer + '/301',
}).then(
function(httpResponse) {
expect(httpResponse.status).toBe(301);
done();
},
function() {
fail('should not fail');
done();
}
);
}).then(function(httpResponse) {
expect(httpResponse.status).toBe(301);
done();
}, done.fail);
});
it('should follow redirects when set', done => {
httpRequest({
url: httpRequestServer + '/301',
followRedirects: true,
}).then(
function(httpResponse) {
expect(httpResponse.status).toBe(200);
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
expect(httpResponse.text).toEqual('{"response":"OK"}');
expect(httpResponse.data.response).toEqual('OK');
done();
},
function() {
fail('should not fail');
done();
}
);
}).then(function(httpResponse) {
expect(httpResponse.status).toBe(200);
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
expect(httpResponse.text).toEqual('{"response":"OK"}');
expect(httpResponse.data.response).toEqual('OK');
done();
}, done.fail);
});
it('should fail on 404', done => {
let calls = 0;
httpRequest({
url: httpRequestServer + '/404',
success: function() {
}).then(
function() {
calls++;
fail('should not succeed');
done();
},
error: function(httpResponse) {
function(httpResponse) {
calls++;
expect(calls).toBe(1);
expect(httpResponse.status).toBe(404);
@@ -142,12 +99,11 @@ describe('httpRequest', () => {
expect(httpResponse.text).toEqual('NO');
expect(httpResponse.data).toBe(undefined);
done();
},
});
}
);
});
it('should post on echo', done => {
let calls = 0;
httpRequest({
method: 'POST',
url: httpRequestServer + '/echo',
@@ -157,15 +113,8 @@ describe('httpRequest', () => {
headers: {
'Content-Type': 'application/json',
},
success: function() {
calls++;
},
error: function() {
calls++;
},
}).then(
function(httpResponse) {
expect(calls).toBe(1);
expect(httpResponse.status).toBe(200);
expect(httpResponse.data).toEqual({ foo: 'bar' });
done();
@@ -220,15 +169,10 @@ describe('httpRequest', () => {
it('should fail gracefully', done => {
httpRequest({
url: 'http://not a good url',
success: function() {
fail('should not succeed');
done();
},
error: function(error) {
expect(error).not.toBeUndefined();
expect(error).not.toBeNull();
done();
},
}).then(done.fail, function(error) {
expect(error).not.toBeUndefined();
expect(error).not.toBeNull();
done();
});
});

View File

@@ -1,4 +1,4 @@
const request = require('request');
const request = require('../lib/request');
function createProduct() {
const file = new Parse.File(
@@ -26,165 +26,136 @@ describe('test validate_receipt endpoint', () => {
beforeEach(done => {
createProduct()
.then(done)
.catch(function() {
.catch(function(err) {
console.error({ err });
done();
});
});
it('should bypass appstore validation', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should bypass appstore validation', async () => {
const httpResponse = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.__type).toEqual('File');
const url = body.url;
request.get(
{
url: url,
},
function(err, res, body) {
expect(body).toEqual('download_file');
done();
}
);
}
}
);
method: 'POST',
url: 'http://localhost:8378/1/validate_purchase',
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
});
const body = httpResponse.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
console.log(body);
expect(body.__type).toEqual('File');
const url = body.url;
const otherResponse = await request({
url: url,
});
expect(otherResponse.text).toBe('download_file');
}
});
it('should fail for missing receipt', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
bypassAppStoreValidation: true,
},
it('should fail for missing receipt', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'a-product',
bypassAppStoreValidation: true,
},
}).then(fail, res => res);
const body = response.data;
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
});
it('should fail for missing product identifier', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should fail for missing product identifier', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
}).then(fail, res => res);
const body = response.data;
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
});
it('should bypass appstore validation and not find product', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'another-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should bypass appstore validation and not find product', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.OBJECT_NOT_FOUND);
expect(body.error).toEqual('Object not found.');
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'another-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf8').toString('base64'),
},
bypassAppStoreValidation: true,
},
}).catch(error => error);
const body = response.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.code).toEqual(Parse.Error.OBJECT_NOT_FOUND);
expect(body.error).toEqual('Object not found.');
}
});
it('should fail at appstore validation', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
it('should fail at appstore validation', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.status).toBe(21002);
expect(body.error).toBe(
'The data in the receipt-data property was malformed or missing.'
);
}
done();
}
);
});
const body = response.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.status).toBe(21002);
expect(body.error).toBe(
'The data in the receipt-data property was malformed or missing.'
);
}
});
it('should not create a _Product', done => {

View File

@@ -1,9 +1,10 @@
{
"apps": [
{
"arg1": "my_app",
"arg2": 8888,
"arg3": "hello",
"arg4": "/1"
}]
{
"arg1": "my_app",
"arg2": 8888,
"arg3": "hello",
"arg4": "/1"
}
]
}

View File

@@ -1,16 +1,16 @@
{
"apps": [
{
"arg1": "my_app",
"arg2": "99999",
"arg3": "hello",
"arg4": "/1"
},
{
"arg1": "my_app2",
"arg2": "9999",
"arg3": "hello",
"arg4": "/1"
}
{
"arg1": "my_app",
"arg2": "99999",
"arg3": "hello",
"arg4": "/1"
},
{
"arg1": "my_app2",
"arg2": "9999",
"arg3": "hello",
"arg4": "/1"
}
]
}

View File

@@ -1,10 +1,6 @@
{
"spec_dir": "spec",
"spec_files": [
"*spec.js"
],
"helpers": [
"helper.js"
],
"spec_files": ["*spec.js"],
"helpers": ["helper.js"],
"random": false
}

View File

@@ -4,7 +4,7 @@ import * as triggers from '../triggers';
// @flow-disable-next
import * as Parse from 'parse/node';
// @flow-disable-next
import * as request from 'request';
import request from '../request';
import { logger } from '../logger';
import http from 'http';
import https from 'https';
@@ -225,10 +225,12 @@ function wrapToHTTPRequest(hook, key) {
jsonBody.original.className = req.original.className;
}
const jsonRequest: any = {
url: hook.url,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(jsonBody),
body: jsonBody,
method: 'POST',
};
const agent = hook.url.startsWith('https')
@@ -243,39 +245,38 @@ function wrapToHTTPRequest(hook, key) {
'Making outgoing webhook request without webhookKey being set!'
);
}
return new Promise((resolve, reject) => {
request.post(hook.url, jsonRequest, function(err, httpResponse, body) {
var result;
if (body) {
if (typeof body === 'string') {
try {
body = JSON.parse(body);
} catch (e) {
err = {
error: 'Malformed response',
code: -1,
partialResponse: body.substring(0, 100),
};
}
}
if (!err) {
result = body.success;
err = body.error;
return request(jsonRequest).then(response => {
let err;
let result;
let body = response.data;
if (body) {
if (typeof body === 'string') {
try {
body = JSON.parse(body);
} catch (e) {
err = {
error: 'Malformed response',
code: -1,
partialResponse: body.substring(0, 100),
};
}
}
if (err) {
return reject(err);
} else if (hook.triggerName === 'beforeSave') {
if (typeof result === 'object') {
delete result.createdAt;
delete result.updatedAt;
}
return resolve({ object: result });
} else {
return resolve(result);
if (!err) {
result = body.success;
err = body.error;
}
});
}
if (err) {
throw err;
} else if (hook.triggerName === 'beforeSave') {
if (typeof result === 'object') {
delete result.createdAt;
delete result.updatedAt;
}
return { object: result };
} else {
return result;
}
});
};
}

View File

@@ -297,39 +297,31 @@ class ParseServer {
static verifyServerUrl(callback) {
// perform a health check on the serverURL value
if (Parse.serverURL) {
const request = require('request');
request(Parse.serverURL.replace(/\/$/, '') + '/health', function(
error,
response,
body
) {
let json;
try {
json = JSON.parse(body);
} catch (e) {
json = null;
}
if (
error ||
response.statusCode !== 200 ||
!json ||
(json && json.status !== 'ok')
) {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
if (callback) {
callback(false);
const request = require('./request');
request({ url: Parse.serverURL.replace(/\/$/, '') + '/health' })
.catch(response => response)
.then(response => {
const json = response.data || null;
if (
response.status !== 200 ||
!json ||
(json && json.status !== 'ok')
) {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
if (callback) {
callback(false);
}
} else {
if (callback) {
callback(true);
}
}
} else {
if (callback) {
callback(true);
}
}
});
});
}
}
}

View File

@@ -1,6 +1,6 @@
import PromiseRouter from '../PromiseRouter';
var request = require('request');
var rest = require('../rest');
const request = require('../request');
const rest = require('../rest');
import Parse from 'parse/node';
// TODO move validation logic in IAPValidationController
@@ -25,23 +25,21 @@ function appStoreError(status) {
}
function validateWithAppStore(url, receipt) {
return new Promise(function(fulfill, reject) {
request.post(
{
url: url,
body: { 'receipt-data': receipt },
json: true,
},
function(err, res, body) {
var status = body.status;
if (status == 0) {
// No need to pass anything, status is OK
return fulfill();
}
// receipt is from test and should go to test
return reject(body);
}
);
return request({
url: url,
method: 'POST',
body: { 'receipt-data': receipt },
headers: {
'Content-Type': 'application/json',
},
}).then(httpResponse => {
const body = httpResponse.data;
if (body && body.status === 0) {
// No need to pass anything, status is OK
return;
}
// receipt is from test and should go to test
throw body;
});
}

View File

@@ -1,9 +1,36 @@
import request from 'request';
import HTTPResponse from './HTTPResponse';
import querystring from 'querystring';
import log from '../logger';
import { http, https } from 'follow-redirects';
import { URL } from 'url';
var encodeBody = function({ body, headers = {} }) {
const clients = {
'http:': http,
'https:': https,
};
function makeCallback(resolve, reject) {
return function(response) {
const chunks = [];
response.on('data', chunk => {
chunks.push(chunk);
});
response.on('end', () => {
const body = Buffer.concat(chunks);
const httpResponse = new HTTPResponse(response, body);
// Consider <200 && >= 400 as errors
if (httpResponse.status < 200 || httpResponse.status >= 400) {
return reject(httpResponse);
} else {
return resolve(httpResponse);
}
});
response.on('error', reject);
};
}
const encodeBody = function({ body, headers = {} }) {
if (typeof body !== 'object') {
return { body, headers };
}
@@ -63,48 +90,48 @@ var encodeBody = function({ body, headers = {} }) {
* @param {Parse.Cloud.HTTPOptions} options The Parse.Cloud.HTTPOptions object that makes the request.
* @return {Promise<Parse.Cloud.HTTPResponse>} A promise that will be resolved with a {@link Parse.Cloud.HTTPResponse} object when the request completes.
*/
module.exports = function(options) {
var callbacks = {
success: options.success,
error: options.error,
};
delete options.success;
delete options.error;
delete options.uri; // not supported
module.exports = function httpRequest(options) {
let url;
try {
url = new URL(options.url);
} catch (e) {
return Promise.reject(e);
}
options = Object.assign(options, encodeBody(options));
// set follow redirects to false by default
options.followRedirect = options.followRedirects == true;
// support params options
if (typeof options.params === 'object') {
options.qs = options.params;
} else if (typeof options.params === 'string') {
options.qs = querystring.parse(options.params);
}
// force the response as a buffer
options.encoding = null;
const client = clients[url.protocol];
if (!client) {
return Promise.reject(`Unsupported protocol ${url.protocol}`);
}
const requestOptions = {
method: options.method,
port: Number(url.port),
path: url.pathname,
hostname: url.hostname,
headers: options.headers,
encoding: null,
followRedirects: options.followRedirects === true,
};
if (options.qs) {
requestOptions.path += `?${querystring.stringify(options.qs)}`;
}
if (options.agent) {
requestOptions.agent = options.agent;
}
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if (error) {
if (callbacks.error) {
callbacks.error(error);
}
return reject(error);
}
const httpResponse = new HTTPResponse(response, body);
// Consider <200 && >= 400 as errors
if (httpResponse.status < 200 || httpResponse.status >= 400) {
if (callbacks.error) {
callbacks.error(httpResponse);
}
return reject(httpResponse);
} else {
if (callbacks.success) {
callbacks.success(httpResponse);
}
return resolve(httpResponse);
}
});
const req = client.request(
requestOptions,
makeCallback(resolve, reject, options)
);
if (options.body) {
req.write(options.body);
}
req.end();
});
};

1
src/request.js Normal file
View File

@@ -0,0 +1 @@
module.exports = require('./cloud-code/httpRequest');

View File

@@ -1,8 +1,8 @@
# mongoUrl
A fork of node's `url` module, with the modification that commas and colons are
allowed in hostnames. While this results in a slightly incorrect parsed result,
as the hostname field for a mongodb should be an array of replica sets, it's
A fork of node's `url` module, with the modification that commas and colons are
allowed in hostnames. While this results in a slightly incorrect parsed result,
as the hostname field for a mongodb should be an array of replica sets, it's
good enough to let us pull out and escape the auth portion of the URL.
https://github.com/parse-community/parse-server/pull/986