This repository has been archived by the owner on Jan 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit of tracking ajax requests
- Loading branch information
1 parent
e4dbcbd
commit 77debb3
Showing
13 changed files
with
249 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"typescript.tsdk": "./node_modules/typescript/lib", | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/.archive": true, | ||
"**/.dist": true, | ||
"**/.cache": true | ||
}, | ||
"editor.tabSize": 4, | ||
"typescript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": false, | ||
"prettier.printWidth": 100, | ||
"prettier.tabWidth": 4, | ||
"prettier.singleQuote": true, | ||
"prettier.jsxBracketSameLine": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const chance = require('./fake-chance').instance; | ||
|
||
// proxy event listener to make testing easier | ||
XMLHttpRequest.prototype.addEventListener = function(type, callback) { | ||
if (type == 'readystatechange') { | ||
const currentStatus = chance.httpStatus(); | ||
|
||
const newThis = { | ||
getResponseHeader: this.getResponseHeader, | ||
readyState: 4, | ||
status: currentStatus.code, | ||
statusText: currentStatus.text | ||
}; | ||
|
||
callback.call(newThis); | ||
} | ||
}; | ||
XMLHttpRequest.prototype.getResponseHeader = function(type) { | ||
if (type === 'Content-Type') { | ||
return chance.httpContentType().type; | ||
} | ||
else if (type === 'X-Glimpse-ContextId') { | ||
return chance.guid(); | ||
} | ||
else if (type === 'Content-Length') { | ||
return chance.integerRange(1000, 5000); | ||
} | ||
}; | ||
XMLHttpRequest.prototype.send = function() {}; | ||
|
||
// generate requests on a schedule | ||
function spawnRequest() { | ||
setTimeout(function() { | ||
const httpRequest = new XMLHttpRequest(); | ||
httpRequest.open(chance.httpMethod(), chance.httpPath(), true); | ||
httpRequest.send(); | ||
|
||
spawnRequest(); | ||
}, chance.integerRange(1000, 5000)); | ||
} | ||
spawnRequest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const Chance = require('chance'); | ||
|
||
const chance = new Chance(); | ||
const urls = [ '/news/latest', '/news/item/123', '/news/item/453', '/news/item/add', '/news/item/remove' ]; | ||
const methods = [ 'GET', 'GET', 'GET', 'GET', 'POST', 'POST', 'POST', 'PUT', 'PUSH', 'DELETE' ]; | ||
const statuses = [ 100, 200, 200, 200, 200, 200, 200, 404, 404, 403, 403, 500, 304 ]; | ||
const statusText = { 200: 'OK', 404: 'Not Found', 500: 'Server Error', 304: 'OK', 403: 'Error' }; | ||
const protocols = [ 'http', 'http', 'https' ]; | ||
|
||
chance.mixin({ | ||
'integerRange': (min, max) => { | ||
return chance.integer({ min: min, max: max }); | ||
}, | ||
'durationRange': (min, max) => { | ||
return chance.floating({ min: min, max: max, fixed: 2 }); | ||
}, | ||
'dateRange': (min, max) => { | ||
const time = new Date().getTime(); | ||
const difference = chance.integerRange(min, max); | ||
const newTime = new Date(time + difference); | ||
|
||
return newTime; | ||
}, | ||
'httpPath': () => { | ||
return chance.pickone(urls); | ||
}, | ||
'httpMethod': () => { | ||
return chance.pickone(methods); | ||
}, | ||
'httpStatus': () => { | ||
var code = chance.pickone(statuses); | ||
return { | ||
code: code, | ||
text: statusText[code] | ||
}; | ||
}, | ||
'httpContentType': () => { | ||
return { type: 'text/html', category: { 'document': true } }; | ||
}, | ||
'httpProtocol': () => { | ||
return chance.pickone(protocols); | ||
}, | ||
}); | ||
|
||
module.exports = { | ||
instance: chance | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
'use strict'; | ||
|
||
require('./fake-request-service'); | ||
require('./fake-ajax'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
hasClass: function(el, className) { | ||
return el.classList | ||
? el.classList.contains(className) | ||
: new RegExp('\\b' + className + '\\b').test(el.className); | ||
}, | ||
addClass: function(el, className) { | ||
if (el.classList) { | ||
el.classList.add(className); | ||
} | ||
else if (!hasClass(el, className)) { | ||
el.className += ' ' + className; | ||
} | ||
}, | ||
removeClass: function(el, className) { | ||
if (el.classList) { | ||
el.classList.remove(className); | ||
} | ||
else { | ||
el.className = el.className.replace(new RegExp('\\b' + className + '\\b', 'g'), ''); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const util = require('../lib/util'); | ||
const dom = require('../lib/dom'); | ||
|
||
let count = 0; | ||
let ready = false; | ||
let preRenderCache = []; | ||
let currentTimout = undefined; | ||
|
||
function update(method, uri, duration, size, status, statusText, time, contentType, requestId) { | ||
count++; | ||
|
||
//manage counter value | ||
var counter = document.getElementById('glimpse-ajax-count'); | ||
counter.innerText = count; | ||
dom.addClass(counter, 'glimpse-section-value--update'); | ||
if (currentTimout) { | ||
clearTimeout(currentTimout); | ||
} | ||
currentTimout = setTimeout(function() { | ||
dom.removeClass(counter, 'glimpse-section-value--update'); | ||
}, 2000); | ||
} | ||
|
||
const open = XMLHttpRequest.prototype.open; | ||
XMLHttpRequest.prototype.open = function(method, uri) { | ||
if (util.isLocalUri(uri) && uri.indexOf('/glimpse/') == -1) { | ||
const startTime = new Date().getTime(); | ||
this.addEventListener('readystatechange', function() { | ||
if (this.readyState == 4 && this.getResponseHeader('X-Glimpse-ContextId')) { | ||
// if we can render the data do so, otherwise save for later | ||
if (ready) { | ||
listenerNotification(method, uri, startTime, this); | ||
} | ||
else { | ||
const xhrObj = this; | ||
preRenderCache.push(function() { | ||
listenerNotification(method, uri, startTime, xhrObj); | ||
}); | ||
} | ||
} | ||
}, false); | ||
} | ||
|
||
open.apply(this, arguments); | ||
}; | ||
|
||
function listenerNotification(method, uri, startTime, xhrObj) { | ||
update(method, uri, new Date().getTime() - startTime, xhrObj.getResponseHeader('Content-Length'), xhrObj.status, xhrObj.statusText, new Date(), xhrObj.getResponseHeader('Content-Type'), xhrObj.getResponseHeader('X-Glimpse-ContextId')); | ||
} | ||
|
||
module.exports = { | ||
render: function() { | ||
return ` | ||
<div class="glimpse-section glimpse-ajax"> | ||
<span class="glimpse-section-label"> | ||
Ajax requests | ||
</span> | ||
<span class="glimpse-section-duration glimpse-section-value" id="glimpse-ajax-count"> | ||
${count} | ||
</span> | ||
<span class="glimpse-section-suffix glimpse-section-suffix--text"> | ||
found | ||
</span> | ||
</div> | ||
`; | ||
}, | ||
postRender: function() { | ||
ready = true; | ||
|
||
preRenderCache.forEach(function(task) { | ||
task(); | ||
}); | ||
|
||
preRenderCache = undefined; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters