Skip to content
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

Merged
merged 29 commits into from
Aug 26, 2019
Merged

fix(compat): node compatibility #140

merged 29 commits into from
Aug 26, 2019

Conversation

eunjae-lee
Copy link
Contributor

@eunjae-lee eunjae-lee commented Jul 24, 2019

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 because cjs 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:

  • navigator.sendBeacon
  • XMLHttpRequest
  • "http" or "https" from node

@Haroenv
Copy link
Contributor

Haroenv commented Jul 24, 2019

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

@eunjae-lee
Copy link
Contributor Author

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.

@eunjae-lee eunjae-lee requested a review from a team July 25, 2019 14:46
@ghost ghost requested review from Haroenv and tkrugg and removed request for a team July 25, 2019 14:46
@eunjae-lee
Copy link
Contributor Author

  • making two entries: efafd97
  • expose aa function to node build: 0f8252e

The things like passing requester at the entry points will be done in part-2 and part-3.
what do you think? @tkrugg @samouss

@eunjae-lee eunjae-lee force-pushed the fix/node-compat-rollup branch from ade891e to f231802 Compare July 31, 2019 15:24
@eunjae-lee eunjae-lee requested review from Haroenv and samouss August 21, 2019 11:35
Copy link
Contributor

@Haroenv Haroenv left a 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 = {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aa("init", credentials);
});

it("throws when user token is not set", () => {
Copy link
Member

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.

Copy link
Contributor

@tkrugg tkrugg Aug 21, 2019

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.

Copy link
Member

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?

Copy link
Contributor Author

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.");
Copy link
Contributor

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.

Copy link
Contributor Author

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

@tkrugg
Copy link
Contributor

tkrugg commented Aug 21, 2019 via email

@eunjae-lee
Copy link
Contributor Author

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?

@tkrugg tkrugg merged commit e804bd1 into develop Aug 26, 2019
@tkrugg tkrugg deleted the fix/node-compat-rollup branch August 26, 2019 16:00
eunjae-lee pushed a commit that referenced this pull request Sep 9, 2019
* 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
@tkrugg tkrugg mentioned this pull request Jan 15, 2020
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.

5 participants