forked from hull-ships/hull-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompute.js
More file actions
243 lines (216 loc) · 6.78 KB
/
compute.js
File metadata and controls
243 lines (216 loc) · 6.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import vm from "vm";
import _ from "lodash";
import moment from "moment";
import urijs from "urijs";
import raven from "raven";
import deepDiff from "deep-diff";
import deepFreeze from "deep-freeze";
import deepMerge from "deepmerge";
import request from "request";
import Promise from "bluebird";
const TOP_LEVEL_FIELDS = ["tags", "name", "description", "extra", "picture", "settings", "username", "email", "contact_email", "image", "first_name", "last_name", "address", "created_at", "phone", "domain", "accepts_marketing"];
function applyUtils(sandbox = {}) {
const lodash = _.functions(_).reduce((l, key) => {
l[key] = (...args) => _[key](...args);
return l;
}, {});
sandbox.moment = deepFreeze((...args) => { return moment(...args); });
sandbox.urijs = deepFreeze((...args) => { return urijs(...args); });
sandbox._ = deepFreeze(lodash);
}
const buildPayload = (pld, pl = {}) => {
const { properties, context = {} } = pl;
if (properties) {
const { source } = context;
if (source) {
pld[source] = { ...pld[source], ...properties };
} else {
_.map(properties, (v, k) => {
const path = k.replace("/", ".");
if (path.indexOf(".") > -1) {
_.setWith(pld, path, v, Object);
} else if (_.includes(TOP_LEVEL_FIELDS, k)) {
pld[k] = v;
} else {
pld.traits = {
...pld.traits,
[k]: v
};
}
return;
});
}
}
return pld;
};
const updateChanges = (payload) => {
return (memo, d) => {
if (d.kind === "N" || d.kind === "E") {
_.set(memo, d.path, d.rhs);
}
// when we have an array updated we set the whole
// array in `changed` constant
if (d.kind === "A") {
_.set(memo, d.path, _.get(payload, d.path, []));
}
return memo;
};
};
function isInSegment(segments = [], segmentName) {
return _.includes(_.map(segments, "name"), segmentName);
}
const sandboxes = {};
function getSandbox(ship) {
const s = sandboxes[ship.id];
if (!s) sandboxes[ship.id] = vm.createContext({});
return sandboxes[ship.id];
}
module.exports = function compute({ changes = {}, user, account, segments, account_segments, events = [] }, ship = {}, options = {}) {
const { preview } = options;
const { private_settings = {} } = ship;
const { code = "", sentry_dsn: sentryDsn } = private_settings;
// Manually add traits hash if not already there
user.traits = user.traits || {};
account = account || user.account || {};
delete user.account;
const sandbox = getSandbox(ship);
sandbox.changes = changes;
sandbox.user = user;
sandbox.account = account;
sandbox.events = events;
sandbox.segments = segments;
sandbox.account_segments = account_segments || [];
sandbox.ship = ship;
sandbox.payload = {};
sandbox.isInSegment = isInSegment.bind(null, segments);
applyUtils(sandbox);
let tracks = [];
const userTraits = [];
const accountTraits = [];
let accountClaims = {};
const logs = [];
const errors = [];
let isAsync = false;
sandbox.results = [];
sandbox.errors = errors;
sandbox.logs = logs;
sandbox.track = (eventName, properties = {}, context = {}) => {
if (eventName) tracks.push({ eventName, properties, context });
};
sandbox.traits = (properties = {}, context = {}) => {
userTraits.push({ properties, context });
};
sandbox.hull = {
account: (claims = null) => {
if (claims) accountClaims = claims;
return {
traits: (properties = {}, context = {}) => {
accountTraits.push({ properties, context });
},
isInSegment: isInSegment.bind(null, account_segments)
};
},
traits: (properties = {}, context = {}) => {
userTraits.push({ properties, context });
},
track: (eventName, properties = {}, context = {}) => {
if (eventName) tracks.push({ eventName, properties, context });
}
};
sandbox.request = (opts, callback) => {
isAsync = true;
return request.defaults({ timeout: 3000 })(opts, (error, response, body) => {
try {
callback(error, response, body);
} catch (err) {
errors.push(err.toString());
}
});
};
function log(...args) {
logs.push(args);
}
function debug(...args) {
// Only show debug logs in preview mode
if (options.preview) {
logs.push(args);
}
}
function logError(...args) {
errors.push(args);
}
sandbox.console = { log, warn: log, error: logError, debug };
sandbox.captureException = function captureException(e) {
if (sentryDsn) {
const client = new raven.Client(sentryDsn);
client.setExtraContext({ user, segments, events });
client.captureException(e);
}
};
sandbox.captureMessage = function captureMessage(msg) {
if (sentryDsn) {
const client = new raven.Client(sentryDsn);
client.captureMessage(msg);
}
};
try {
const script = new vm.Script(`
try {
results.push(function() {
"use strict";
${code}
}());
} catch (err) {
errors.push(err.toString());
captureException(err);
}`);
script.runInContext(sandbox);
} catch (err) {
errors.push(err.toString());
sandbox.captureException(err);
}
if (isAsync && !_.some(_.compact(sandbox.results), (r) => _.isFunction(r.then))) {
errors.push("It seems you’re using 'request' which is asynchronous.");
errors.push("You need to return a 'new Promise' and 'resolve' or 'reject' it in you 'request' callback.");
}
return Promise.all(sandbox.results)
.catch((err) => {
errors.push(err.toString());
sandbox.captureException(err);
})
.then(() => {
if (preview && tracks.length > 10) {
logs.unshift([tracks]);
logs.unshift([`You're trying to send ${tracks.length} calls at a time. We will only process the first 10`]);
logs.unshift(["You can't send more than 10 tracking calls in one batch."]);
tracks = _.slice(tracks, 0, 10);
}
const payload = {
user: _.reduce(userTraits, buildPayload, {}),
account: _.reduce(accountTraits, buildPayload, {}),
};
// we don't concatenate arrays, we use only new values:
const arrayMerge = (destinationArray, sourceArray) => sourceArray;
const updated = {
user: deepMerge(user, payload.user, { arrayMerge }),
account: deepMerge(account, payload.account, { arrayMerge }),
};
const diff = {
user: deepDiff(user, updated.user) || [],
account: deepDiff(account, updated.account) || [],
};
const changed = {
user: _.reduce(diff.user, updateChanges(payload.user), {}),
account: _.reduce(diff.account, updateChanges(payload.account), {}),
};
return {
logs,
errors,
changes: changed,
events: tracks,
payload: sandbox.payload,
...updated,
accountClaims
};
});
};