123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = exports.HooksController = void 0;
- var triggers = _interopRequireWildcard(require("../triggers"));
- var Parse = _interopRequireWildcard(require("parse/node"));
- var _request = _interopRequireDefault(require("../request"));
- var _logger = require("../logger");
- var _http = _interopRequireDefault(require("http"));
- var _https = _interopRequireDefault(require("https"));
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
- const DefaultHooksCollectionName = '_Hooks';
- const HTTPAgents = {
- http: new _http.default.Agent({
- keepAlive: true
- }),
- https: new _https.default.Agent({
- keepAlive: true
- })
- };
- class HooksController {
- constructor(applicationId, databaseController, webhookKey) {
- this._applicationId = applicationId;
- this._webhookKey = webhookKey;
- this.database = databaseController;
- }
- load() {
- return this._getHooks().then(hooks => {
- hooks = hooks || [];
- hooks.forEach(hook => {
- this.addHookToTriggers(hook);
- });
- });
- }
- getFunction(functionName) {
- return this._getHooks({
- functionName: functionName
- }).then(results => results[0]);
- }
- getFunctions() {
- return this._getHooks({
- functionName: {
- $exists: true
- }
- });
- }
- getTrigger(className, triggerName) {
- return this._getHooks({
- className: className,
- triggerName: triggerName
- }).then(results => results[0]);
- }
- getTriggers() {
- return this._getHooks({
- className: {
- $exists: true
- },
- triggerName: {
- $exists: true
- }
- });
- }
- deleteFunction(functionName) {
- triggers.removeFunction(functionName, this._applicationId);
- return this._removeHooks({
- functionName: functionName
- });
- }
- deleteTrigger(className, triggerName) {
- triggers.removeTrigger(triggerName, className, this._applicationId);
- return this._removeHooks({
- className: className,
- triggerName: triggerName
- });
- }
- _getHooks(query = {}) {
- return this.database.find(DefaultHooksCollectionName, query).then(results => {
- return results.map(result => {
- delete result.objectId;
- return result;
- });
- });
- }
- _removeHooks(query) {
- return this.database.destroy(DefaultHooksCollectionName, query).then(() => {
- return Promise.resolve({});
- });
- }
- saveHook(hook) {
- var query;
- if (hook.functionName && hook.url) {
- query = {
- functionName: hook.functionName
- };
- } else if (hook.triggerName && hook.className && hook.url) {
- query = {
- className: hook.className,
- triggerName: hook.triggerName
- };
- } else {
- throw new Parse.Error(143, 'invalid hook declaration');
- }
- return this.database.update(DefaultHooksCollectionName, query, hook, {
- upsert: true
- }).then(() => {
- return Promise.resolve(hook);
- });
- }
- addHookToTriggers(hook) {
- var wrappedFunction = wrapToHTTPRequest(hook, this._webhookKey);
- wrappedFunction.url = hook.url;
- if (hook.className) {
- triggers.addTrigger(hook.triggerName, hook.className, wrappedFunction, this._applicationId);
- } else {
- triggers.addFunction(hook.functionName, wrappedFunction, null, this._applicationId);
- }
- }
- addHook(hook) {
- this.addHookToTriggers(hook);
- return this.saveHook(hook);
- }
- createOrUpdateHook(aHook) {
- var hook;
- if (aHook && aHook.functionName && aHook.url) {
- hook = {};
- hook.functionName = aHook.functionName;
- hook.url = aHook.url;
- } else if (aHook && aHook.className && aHook.url && aHook.triggerName && triggers.Types[aHook.triggerName]) {
- hook = {};
- hook.className = aHook.className;
- hook.url = aHook.url;
- hook.triggerName = aHook.triggerName;
- } else {
- throw new Parse.Error(143, 'invalid hook declaration');
- }
- return this.addHook(hook);
- }
- createHook(aHook) {
- if (aHook.functionName) {
- return this.getFunction(aHook.functionName).then(result => {
- if (result) {
- throw new Parse.Error(143, `function name: ${aHook.functionName} already exists`);
- } else {
- return this.createOrUpdateHook(aHook);
- }
- });
- } else if (aHook.className && aHook.triggerName) {
- return this.getTrigger(aHook.className, aHook.triggerName).then(result => {
- if (result) {
- throw new Parse.Error(143, `class ${aHook.className} already has trigger ${aHook.triggerName}`);
- }
- return this.createOrUpdateHook(aHook);
- });
- }
- throw new Parse.Error(143, 'invalid hook declaration');
- }
- updateHook(aHook) {
- if (aHook.functionName) {
- return this.getFunction(aHook.functionName).then(result => {
- if (result) {
- return this.createOrUpdateHook(aHook);
- }
- throw new Parse.Error(143, `no function named: ${aHook.functionName} is defined`);
- });
- } else if (aHook.className && aHook.triggerName) {
- return this.getTrigger(aHook.className, aHook.triggerName).then(result => {
- if (result) {
- return this.createOrUpdateHook(aHook);
- }
- throw new Parse.Error(143, `class ${aHook.className} does not exist`);
- });
- }
- throw new Parse.Error(143, 'invalid hook declaration');
- }
- }
- exports.HooksController = HooksController;
- function wrapToHTTPRequest(hook, key) {
- return req => {
- const jsonBody = {};
- for (var i in req) {
- jsonBody[i] = req[i];
- }
- if (req.object) {
- jsonBody.object = req.object.toJSON();
- jsonBody.object.className = req.object.className;
- }
- if (req.original) {
- jsonBody.original = req.original.toJSON();
- jsonBody.original.className = req.original.className;
- }
- const jsonRequest = {
- url: hook.url,
- headers: {
- 'Content-Type': 'application/json'
- },
- body: jsonBody,
- method: 'POST'
- };
- const agent = hook.url.startsWith('https') ? HTTPAgents['https'] : HTTPAgents['http'];
- jsonRequest.agent = agent;
- if (key) {
- jsonRequest.headers['X-Parse-Webhook-Key'] = key;
- } else {
- _logger.logger.warn('Making outgoing webhook request without webhookKey being set!');
- }
- return (0, _request.default)(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) {
- 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;
- delete result.className;
- }
- return {
- object: result
- };
- } else {
- return result;
- }
- });
- };
- }
- var _default = exports.default = HooksController;
|