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

Separate token specific code into its own export #1739

Merged
merged 10 commits into from
Oct 28, 2024

Conversation

flevi29
Copy link
Collaborator

@flevi29 flevi29 commented Oct 4, 2024

Pull Request

Related issue

Fixes #1690

What does this PR do?

  • generalizes the MeiliSearch client into a single universal export by stripping Node.js crypto specific token code into its own separate exported module
  • apiKey option is now mandatory in generateTenantToken function

Migration

Old:

import { MeiliSearch } from "meilisearch";

const client = new MeiliSearch({ host: "http://127.0.0.1:7700", apiKey: "masterKey" });
const token = await client.generateTenantToken("e489fe16-3381-431b-bee3-00430192915d");

// ...

New:

import { generateTenantToken } from "meilisearch/token";

const token = await generateTenantToken("e489fe16-3381-431b-bee3-00430192915d", [], { apiKey: "masterKey" });

// ...

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

@flevi29 flevi29 added the breaking-change The related changes are breaking for the users label Oct 4, 2024
@flevi29 flevi29 requested a review from brunoocasali October 4, 2024 17:43
Copy link

codecov bot commented Oct 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.81%. Comparing base (e224f5c) to head (834fd67).
Report is 11 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1739      +/-   ##
==========================================
+ Coverage   97.55%   97.81%   +0.26%     
==========================================
  Files          20       17       -3     
  Lines        1513     1465      -48     
  Branches      319      307      -12     
==========================================
- Hits         1476     1433      -43     
+ Misses         37       32       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@brunoocasali
Copy link
Member

What will happen if I try to use it on the web? It will just not even compile or what?

@brunoocasali
Copy link
Member

By the way do you know if this could solve the issue: meilisearch/meilisearch-js-plugins#1330 ?

@flevi29
Copy link
Collaborator Author

flevi29 commented Oct 16, 2024

By the way do you know if this could solve the issue: meilisearch/meilisearch-js-plugins#1330 ?

@brunoocasali It should solve it, because they would no longer be importing any file associated with node:crypto when they are importing from "meilisearch". For this error to occur they would have to import from "meilisearch/token" with these changes.

What will happen if I try to use it on the web? It will just not even compile or what?

It will err, because there is no such thing as "node:crypto" to import from on the web. Same way as it would currently if we didn't strip it in the browser client. Although users can still polyfill it to some extent, possibly entirely. Parcel will automatically try to polyfill it with crypto-browserify (even though it's extremely outdated) for instance.
https://parceljs.org/features/node-emulation/#polyfilling-%26-excluding-builtin-node-modules
https://www.npmjs.com/package/crypto-browserify

Copy link
Member

@brunoocasali brunoocasali left a comment

Choose a reason for hiding this comment

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

LGTM!

Can you mention in the transition path tutorial about the requirement of the apiKey (in a clear example)

@brunoocasali
Copy link
Member

@flevi29 I would just wait to merge this after the next release. Unless we merge this and create a new release before the main one, wtdyt?

@flevi29
Copy link
Collaborator Author

flevi29 commented Oct 16, 2024

Okay, we can wait.

@flevi29
Copy link
Collaborator Author

flevi29 commented Oct 28, 2024

bors merge

Copy link
Contributor

meili-bors bot commented Oct 28, 2024

@meili-bors meili-bors bot merged commit 4fe62f3 into meilisearch:main Oct 28, 2024
7 checks passed
@flevi29 flevi29 deleted the crypto-export branch October 28, 2024 21:45
meili-bors bot added a commit that referenced this pull request Jan 8, 2025
1740: Switch from rollup to vite, from commonjs to ecmascript modules r=Strift a=flevi29

# Pull Request

## Related issue
Fixes #1626
~~Waiting on #1739~~

## What does this PR do?
- switches bundler from [`rollup`](https://rollupjs.org/) to [`vite`](https://vite.dev/), which internally uses `rollup`, but makes things a lot shorter and simpler
  - removes the need for separate `vitest.config.js`
  - removes the need for [`scripts/file-size.js`](https://github.com/meilisearch/meilisearch-js/blob/main/scripts/file-size.js), packages `kleur`, `pretty-bytes`, `brotli-size`, `gzip-size`, as it prints sizes by default
  - removes the need for all of the `rollup` plugins and dependencies (``@rollup/plugin-terser`,` ``@babel/core`,` ``@babel/preset-env`,` ``@rollup/plugin-babel`,` ``@rollup/plugin-commonjs`,` ``@rollup/plugin-json`,` ``@rollup/plugin-node-resolve`,` `rollup-plugin-typescript2`)
  - removes the need for `shx` package, and `cleanup` script, as it clears `dist` itself
- switches from CJS to ESM ([`"type": "module"`](https://nodejs.org/api/packages.html#type))
  - every `.js` project file now needs to be written in ESM syntax, the web standard syntax
  - TypeScript files were already written in ESM syntax, so this just makes things more consistent
- updates TypeScript and makes some changes to `tsconfig.json` according to https://www.typescriptlang.org/docs/handbook/modules/guides/choosing-compiler-options.html#im-writing-a-library
  - is now used to transpile ESM version of this package
  - most importantly sets `module` to `node16` and `verbatimModuleSyntax` to `true`, this requires changes to all TypeScript files so they may be compatible with the web standard ESM
    - all imported files now require a `.js` extension
    - there is no more support for `index` directory imports, as that is only a Node.js thing, the web doesn't support it
    - when importing types, we need to explicitly specify that we're doing so with the `type` keyword

> [!WARNING]
> - DEPRECATE default export #1789
> - DEPRECATION: UMD bundle will no longer extend `window` with the exports in the future, instead it will only extend it with property `meilisearch` which contains the exports #1806
> ### Old
> ```html
> <script>
>   const client = new MeiliSearch(/* ... */);
> </script>
> ```
> ### New
> ```html
> <script>
>   const client = new meilisearch.MeiliSearch(/* ... */);
> </script>
> ```

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: F. Levi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change The related changes are breaking for the users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generalize MeiliSearch client for all borwsers/runtimes
2 participants