Skip to content

Commit

Permalink
Merge pull request #10 from Moesif/cleanup-variable-names-appid
Browse files Browse the repository at this point in the history
More Fixes for CloudFlare worker
  • Loading branch information
dgilling authored Dec 16, 2020
2 parents f1bb23b + 5c4d193 commit efbff4b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 48 deletions.
60 changes: 32 additions & 28 deletions MoesifWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (typeof INSTALL_OPTIONS === 'undefined') {
* MOESIF_INSTALL
* Set Your Moesif Application Id
*********************************/
"appId": "",
"applicationId": "",

// Only used by CloudFlare App Worker Framework. Modify identifyUser() function instead.
"userIdHeader": "",
Expand Down Expand Up @@ -60,7 +60,7 @@ if (typeof INSTALL_OPTIONS === 'undefined') {
}

let {
appId,
applicationId,
hideCreditCards,
disableTransactionId,
sessionTokenHeader,
Expand Down Expand Up @@ -116,36 +116,36 @@ if (typeof INSTALL_PRODUCT === 'undefined') {
INSTALL_PRODUCT = undefined;
}

urlPatterns = urlPatterns.map(({ appId, regex }) => {
urlPatterns = urlPatterns.map(({ applicationId, regex }) => {
try {
return {
regex: new RegExp(regex),
appId
applicationId
};
} catch (e) {
console.error(e);
}
}).filter(x => x && x.regex); // filter invalid regular expressions / blank entries

if (!appId && urlPatterns.length === 0) {
console.error('Cannot track events. No App ID or valid URL Pattern specified.');
if (!applicationId && urlPatterns.length === 0) {
console.error('Cannot log API calls. No Moesif Application Id or valid URL Patterns specified.');
}

const overrideApplicationId = moesifEvent => {
// you may want to use a different app ID based on the request being made
const pattern = urlPatterns.find(({ regex }) => regex.test(moesifEvent.request.uri));

return pattern
? pattern.appId // may be an empty string, which means don't track this
: appId;
? pattern.applicationId // may be an empty string, which means don't track this
: applicationId;
};

const BATCH_URL = 'https://api.moesif.net/v1/events/batch';
let batchRunning = false;
let jobs = [];

function isMoesif(request) {
return request.url.indexOf('https://api.moesif.net') !== -1;
return request.url.indexOf('moesif.net') !== -1;
}

function sleep(ms) {
Expand Down Expand Up @@ -318,7 +318,7 @@ async function makeMoesifEvent(request, response, before, after, txId) {
headers: headersToObject(request.headers),
ip_address: request.headers.get('cf-connecting-ip')
},
response: {
response: response.isEmpty ? undefined : {
time: after,
body: responseBody ? doHideCreditCards(responseBody) : undefined,
status: response.status,
Expand Down Expand Up @@ -354,36 +354,40 @@ function batch() {
moesifLog(`batch start`)

if (jobs.length > 0) {
const appIdMap = {};
const applicationIdMap = {};
moesifLog(`batch has jobs`)

jobs.forEach(({ applicationId, moesifEvent }) => {
if (!(applicationId in appIdMap)) {
appIdMap[applicationId] = [];
jobs.forEach(({ appId, moesifEvent }) => {
if (!(appId in applicationIdMap)) {
applicationIdMap[appId] = [];
}

if ((moesifEvent.direction === 'Outgoing' && logOutgoingRequests) ||
(moesifEvent.direction === 'Incoming' && logIncomingRequests)) {
appIdMap[applicationId].push(moesifEvent);
applicationIdMap[appId].push(moesifEvent);
}
});

let promises = [];

Object.keys(appIdMap).forEach(applicationId => {
const body = JSON.stringify(appIdMap[applicationId]);
Object.keys(applicationIdMap).forEach(appId => {

const moesifHeaders = {
'Accept': 'application/json; charset=utf-8',
'X-Moesif-Application-Id': appId,
'User-Agent': 'moesif-cloudflare',
'X-Moesif-Cf-Install-Id': INSTALL_ID,
'X-Moesif-Cf-Install-Product': (INSTALL_PRODUCT && INSTALL_PRODUCT.id),
'X-Moesif-Cf-Install-Type': INSTALL_TYPE,
}
moesifLog(JSON.stringify(moesifHeaders));

const body = JSON.stringify(applicationIdMap[appId]);
moesifLog(body);

const options = {
method: 'POST',
headers: {
'Accept': 'application/json; charset=utf-8',
'X-Moesif-Application-Id': applicationId,
'User-Agent': 'moesif-cloudflare',
'X-Moesif-Cf-Install-Id': INSTALL_ID,
'X-Moesif-Cf-Install-Product': (INSTALL_PRODUCT && INSTALL_PRODUCT.id),
'X-Moesif-Cf-Install-Type': INSTALL_TYPE,
},
headers: moesifHeaders,
body: body
};

Expand All @@ -401,15 +405,15 @@ async function tryTrackRequest(event, request, response, before, after, txId) {
moesifLog(`tryTrackRequest start url=${request.url}`)

const moesifEvent = await makeMoesifEvent(request, response, before, after, txId);
const applicationId = runHook(() => overrideApplicationId(moesifEvent), 'overrideApplicationId', appId);
const appId = runHook(() => overrideApplicationId(moesifEvent), 'overrideApplicationId', applicationId);
event.waitUntil(moesifEvent);

if (applicationId) {
if (appId) {
// only track this if there's an associated applicationId
// services may want to not report certain requests

jobs.push({
applicationId,
appId,
moesifEvent
});

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ We recommend matching all requests for your domain. For example if your domain i

### 3. Set Moesif options

Set the `INSTALL_OPTIONS.appId` field to your Moesif Application Id.
Set the `INSTALL_OPTIONS.applicationId` field to your Moesif Application Id.
Your Moesif Application Id will be displayed during the onboarding steps when signing up for [Moesif](https://www.moesif.com/).

Additional options are below:
```javascript
INSTALL_OPTIONS = {
// Your Moesif Application Id
"appId": "Your Moesif Application Id",
"applicationId": "Your Moesif Application Id",

// Only used by CloudFlare App Worker Framework. Modify identifyUser() function instead.
"userIdHeader": "",
Expand Down
57 changes: 39 additions & 18 deletions install.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,92 @@
],
"options": {
"properties": {
"appId": {
"applicationId": {
"order": 10,
"title": "Default Moesif App Id",
"description": "Requred, unless \"App Id Overrides\" are used",
"title": "Your Moesif Application Id",
"description": "Required, unless you set the urlPatterns option. sign up for free on <a href='https://www.moesif.com/?language=cloudflare-workers'>Moesif's website</a>",
"type": "string",
"default": ""
},
"userIdHeader": {
"order": 20,
"title": "Request / Response Header to Identify User",
"description": "Optional<br>For more info, see <a href='https://www.moesif.com/docs/getting-started/users/'>User Profiles</a> and <a href='https://www.moesif.com/docs/getting-started/companies/'>Company Profiles</a>.",
"title": "Request or Response header name to identify users",
"description": "Optional<br>For more info, see <a href='https://www.moesif.com/docs/getting-started/companies/'>Company Profiles</a>.",
"type": "string",
"default": ""
},
"companyIdHeader": {
"order": 40,
"title": "Request / Response Header to Identify Company",
"description": "Optional",
"title": "Request or Response header name to identify companies",
"description": "Optional<br>For more info, see <a href='https://www.moesif.com/docs/getting-started/users/'>User Profiles</a>.",
"type": "string",
"default": ""
},
"sessionTokenHeader": {
"order": 50,
"title": "Request / Response Header Containing Session Token",
"title": "Request or Response header name containing a user's API key or session token",
"description": "Optional",
"type": "string",
"default": ""
},
"hideCreditCards": {
"order": 60,
"title": "Remove Credit Card Numbers from API Requests. Leave this enabled if your API processes credit card information.",
"title": "Remove Credit Card Numbers from API Requests based on a Luhn check.",
"type": "boolean",
"default": true
},
"disableTransactionId": {
"order": 65,
"title": "Disable Transaction Id generation",
"description": "Setting to true will prevent insertion of the <code>X-Moesif-Transaction-Id</code> header.",
"description": "Setting to true will prevent insertion of the <code>X-Moesif-Transaction-Id</code> headers.",
"type": "boolean",
"default": false
},
"urlPatterns": {
"order": 70,
"title": "App Id Overrides",
"description": "Advanced usage.<br>For assistance with advanced configuration, please contact <a href='mailto:[email protected]'>support@moesif.com</a>. Regular expressions are evaluated in order (top to bottom), and the appId for the first matching regex will be used for a given request. <br> To manually test your regular expressions, you can use the following JavaScript code: new RegExp(urlRegex).test(moesifAppId)",
"title": "Mapping of URL RegEx to Moesif Application Ids",
"description": "Use multiple applications ids based on URL patterns. For example, if your worker has multiple environments like a staging at <i>staging.api.acmeinc.com</i> and production at <i>api.acmeinc.com</i>, you can log to different application in Moesif. Patterns are prioritized in order from top. <br> You can manually test your expression via <a href='https://regex101.com/'>this site</a>.",
"type": "array",
"items": {
"title": "Item",
"type": "object",
"properties": {
"regex": {
"order": 1,
"title": "URL Regex",
"placeholder": "^https://mydomain.com/api",
"title": "URL RegEx",
"placeholder": "^https://staging.acmeinc.com/api",
"type": "string"
},
"appId": {
"applicationId": {
"order": 2,
"title": "Moesif App Id for Requests with URLs Matching Above Regular Expression",
"placeholder": "leave blank to ignore requests matching this pattern",
"title": "Your Moesif Application Id. If blank, requests are not logged to Moesif.",
"placeholder": "Your Moesif Application Id",
"type": "string"
}
}
}
},
"logIncomingRequests": {
"order": 75,
"title": "Log Incoming API Requests",
"description": "Log incoming API calls hitting your Cloudflare Worker.",
"type": "boolean",
"default": true
},
"logOutgoingRequests": {
"order": 80,
"title": "Log Outgoing API Requests",
"description": "Log outgoing calls to your origin server from your Cloudflare Worker.",
"type": "boolean",
"default": true
},
"debug": {
"order": 100,
"title": "Enable debug mode",
"description": "Print debug messages to console which may be helpful when contacting Moesif support.",
"type": "boolean",
"default": false
}
}
}
}
}

0 comments on commit efbff4b

Please sign in to comment.