-
Notifications
You must be signed in to change notification settings - Fork 20
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
fix(compat): node compatibility #140
Conversation
Just FYI, I don't think this is the best use for a stacked PR, since they all three are unrelated, and thus should be their own commit on master. You can make PRs that target master directly individually when there's no dependency on the previous PR |
You're right. There's no dependencies between the PRs. I did it just for practice and thought they're okay to be merged into one commit named 'node compatibility'. If you think it should be splitted into their own commits, let me know. |
ade891e
to
f231802
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks mostly good
lib/insights.ts
Outdated
@@ -56,6 +57,10 @@ declare global { | |||
} | |||
} | |||
|
|||
type AlgoliaAnalyticsOptions = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't reused, so can be inline, but fine like this too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-Authored-By: Haroen Viaene <[email protected]>
Co-Authored-By: Haroen Viaene <[email protected]>
…sights.js into fix/node-compat-rollup
aa("init", credentials); | ||
}); | ||
|
||
it("throws when user token is not set", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is userToken: ''
a valid case? It's not tested but it works with this value.
We should track corner cases like this in tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's already handled by the API, you get {"status":422,"message":"The userToken field is required"}
so it's not a big deal if the client lets it slip.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the difference with an undefined
userToken
then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
userToken: "", | ||
...defaultPayload | ||
}); | ||
}).toThrowError("`userToken` cannot be an empty string."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be 100% rigorous I would split this into to different it
to fully reset the state between the first call and second call in the beforeEach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right. I didn't think of resetting the state between cases. ad4bea2
Co-Authored-By: Youcef Mammar <[email protected]>
…sights.js into fix/node-compat-rollup
it’d be good to later add a test for this to make sure it will never
happen. But we can do it in another PR, this one is big enough.
|
You mean adding a test to check if the bundle output is correct, right? |
* docs(readme): update documentation (#153) * docs(readme): update documentation * docs(migration): add migration file * chore(package): update description * docs(readme): fix license link (#154) * chore(deps): update dependency html-webpack-plugin to v3 (#85) * chore(deps): update dependency @types/jest to v24.0.18 (#147) * chore(deps): update dependency dotenv to v8.1.0 (#152) * chore(deps): update dependency jest to v24.9.0 (#150) * chore(deps): update node.js to v8.16.1 (#151) * chore(deps): update dependency puppeteer to v1.19.0 (#124) * docs(readme): convert `positions` to numbers (#156) * fix(compat): node compatibility (#140) * chore: configure rollup to generate two builds * chore: remove unused option (browser) from rollup config * chore: add process.maybeNode * chore: add __BROWSER_BUILD__ instead of process.maybeNode * chore: make two entries * chore: export aa function in node build * chore: instantiate AlgoliaInsights every time in tests * chore: rename entry files * chore: use top-level beforeEach * chore: rename getAa to getFuctionalInterface * fix(compat): node compatibility [PART-2] (#141) * fix(compat): disable anonymous tracking on env without cookies * chore: move supportsCookies to featureDetection * chore: process queue only on browser * chore: throw when user token is not set * chore: add jest watch typeahead plugin (#143) * chore: add jest watch typeahead plugin * chore: pin dependency * chore: add __BROWSER_BUILD__ instead of process.maybeNode * chore: add __BROWSER_BUILD__ condition * chore: fix broken test * chore: remove __BROWSER_BUILD__ * chore: process queue only on browser build * fix: process queue inside browser module * chore: fix test cases * chore: update travis config regarding xvfb (#144) * chore: rename getAa to getFuctionalInterface * chore: remove the duplicates * fix: remove useless argument * chore: move getInsatnceXX functions to _instance * fix(compat): node compatibility [PART-3] (#142) * chore: add util functions and tests * chore: add request function which wraps sendBeacon, xhr and node http * chore: apply feedback Co-Authored-By: Youcef Mammar <[email protected]> * chore: apply feedbacks Co-Authored-By: Youcef Mammar <[email protected]> * chore: require http/https dynamically to clean browser build * chore: remove node http related code from browser build * chore: remove __BROWSER_BUILD__ * chore: fix tests for sendEvent in node * fix: pass requestFn to AlgoliaAnalytics * chore: remove _instance and use AlgoliaAnalytics instead * chore: split getRequester * fix: change parameters of request for node 8.x node 8.x and 10.x have different signautures for 'request' function. - node 8.x: https://nodejs.org/docs/latest-v8.x/api/https.html#https_https_request_options_callback - node 10.x: https://nodejs.org/dist/latest-v10.x/docs/api/https.html#https_https_request_options_callback * fix: allow userToken in event payload * fix: remove the duplicated assignment * fix: check userToken for only undefined * chore: rename AlgoliaInsights with analyticsInstance in test cases * fix: add `new` at `TypeError`s Co-Authored-By: Haroen Viaene <[email protected]> * chore: apply suggestions from code review Co-Authored-By: Haroen Viaene <[email protected]> * fix: update inline snapshot of test case * fix: add `new` at `TypeError`s * chore: inline AlgoliaAnalyticsOptions type * chore: use `isUndefined` helper * fix: throw when userToken is an empty string * fix: improve test case * fix: split a test case * chore: update feedback from review Co-Authored-By: Youcef Mammar <[email protected]> * docs: add a section for Node.js module (#145) * docs: add a section for node module * docs: add TOC * docs: apply feedbacks Co-Authored-By: Youcef Mammar <[email protected]> * docs: move Node.js part under getting started * docs: update setUserToken part * docs: apply changes again * docs: apply suggestions from code review Co-Authored-By: François Chalifour <[email protected]> * docs: apply feedbacks * docs: update TOC * chore: update version
Node compatibility
1. Rollup Config
This modifies the rollup config so that it can produce two builds, one for umd and another one for cjs.
Note
I thought it's unnecessary to have codes like
document.cookie
in the build output for node environment, then realized there is no way to figure out the environment in the building stage becausecjs
build can be used both in node and browser.So the final rollup config is just building cjs and umd with no special configuration.
2. Handling cookie
This disables anonymous tracking on env without cookies.
It's almost copy&paste from #103
3. Handling sendBeacon
This provides fallbacks to sendBeacon function.
It tries in this order: