remove runtime dependency on request (#5076)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -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
1
src/request.js
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require('./cloud-code/httpRequest');
|
||||
6
src/vendor/README.md
vendored
6
src/vendor/README.md
vendored
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user