Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Defensive cleanup #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Apr 5, 2016
Merged

Defensive cleanup #64

merged 25 commits into from
Apr 5, 2016

Conversation

djih
Copy link
Member

@djih djih commented Apr 2, 2016

No description provided.

@djih djih self-assigned this Apr 2, 2016
@djih djih force-pushed the defensive_cleanup branch from f82d52b to b9dbacd Compare April 4, 2016 23:27
@@ -86,6 +87,9 @@ build: $(TESTS) $(OUT) $(SNIPPET_OUT) $(SEGMENT_SNIPPET_OUT) README.md
@$(DUO) --development test/tests.js > build/tests.js
@$(DUO) --development test/snippet-tests.js > build/snippet-tests.js

docs:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered adding this to the default make command, but the html files are timestamped, so they would be modified with every make. Whenever someone updates the API, they need to remember to update the docs

Amplitude.prototype.runQueuedFunctions = function () {
for (var i = 0; i < this._q.length; i++) {
var fn = this[this._q[i][0]];
if (fn && type(fn) === 'function') {
if (type(fn) === 'function') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can skip the null check since type(null) will return 'null' which fails the === 'function' test

@djih
Copy link
Member Author

djih commented Apr 4, 2016

Refactoring a bunch of stuff, adding lots of sanity checks and more type-checking around user-input, handle more failure cases gracefully.

  1. Use Jsdoc to generate documentation around our SDK classes. This PR looks really big, but 90% of it is just new files (CSS, font, html files) to support the static docs. Also had to update a lot of comments through the SDK to generate the docs.
  2. For the Readme section on config inputs, I added a type column to specify the expected input type. The SDK has a helper function that parses the config input object, validates the keys and input types against the DEFAULT config object.
  3. Refactored a lot of static strings into Constants.js
  4. We can simplify the input validation by leveraging the type library. Before we would do something like this: if (var === null || type(var) !== 'string'), but type(null) returns 'null', so you can simply just do a type(var) !== 'string' and that will also check for nulls and undefined
  5. Following 4, we make the SDK more defensive by aggressively checking the input, for null, for types, for valid values
  6. If we are using a generated deviceId, append an 'R' to the end, to make JS SDK consistent with our iOS and android SDKs
  7. Since we validate eventProperties on init, we might as well validate userProperties.
  8. Handle loading of saved events from localStorage where client is using a broken JSON library, so deserialization into an array fails.
  9. As suggested by Ryan, we add names to all of the anonymous functions.
    Before: var Amplitude.prototype.func = function(blah)
    After: var Amplitude.prototype.func = function func(blah)
  10. Now save initial_UTM parameters too. Following the style of the referrer, we stop sending UTM params on every event since they are a user property. Instead we send them once per session (basically during the init of the SDK), and save the values to sessionStorage. If we see values already existing in sessionStorage, then we know that they were already sent. Refactored code so that UTM and referrer processing can share the same function
  11. Moved a bunch of helper methods into the utils module
  12. user_id should not default to device_id if null
  13. Added tests for 2, 6, 7, 10

@djih djih force-pushed the defensive_cleanup branch from f0df66f to ae572dd Compare April 4, 2016 23:52
// utils.log(e);
}
var events = JSON.parse(savedUnsentEventsString);
if (type(events) === 'array') { // handle case where JSON dumping of unsent events is corrupted
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's supposed to deserialize into an array object, but if client's have broken JSON libraries, it deserializes into a string, which breaks the SDK

@djih djih assigned paladin8 and unassigned djih Apr 5, 2016
@djih djih force-pushed the defensive_cleanup branch from ae572dd to 744ad1b Compare April 5, 2016 00:24
@djih
Copy link
Member Author

djih commented Apr 5, 2016

@paladin8 you can skip everything in the documentation folder

// verifies config value is defined, is the correct type, and some additional value verification
var parseValidateLoad = function parseValidateLoad(key, expectedType) {
var input_value = config[key];
if(input_value === undefined || !utils.validateInput(input_value, key + ' option', expectedType)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space after if.

@paladin8
Copy link
Contributor

paladin8 commented Apr 5, 2016

really solid changes, only worry is that it is pretty big so we just want to make sure we manually test enough and that upgrades work, etc. lgtm from a code perspective though!

@djih djih force-pushed the defensive_cleanup branch from beac0d9 to 938952f Compare April 5, 2016 21:53
@djih djih merged commit 4674ac5 into master Apr 5, 2016
@djih djih deleted the defensive_cleanup branch April 5, 2016 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants