Lazy-load storage plugins to avoid side effects on import#773
Open
whaaaley wants to merge 1 commit intoNaturalNode:masterfrom
Open
Lazy-load storage plugins to avoid side effects on import#773whaaaley wants to merge 1 commit intoNaturalNode:masterfrom
whaaaley wants to merge 1 commit intoNaturalNode:masterfrom
Conversation
Author
|
This also addresses #754. The punycode deprecation warning comes from mongoose, which is pulled in by the MongoDB storage plugin. With lazy loading, users who don't call If you're open to it, another option would be moving mongoose, pg, redis, and memjs to peerDependencies or optionalDependencies so they're not installed at all unless the user explicitly adds them. Or the storage backends could be a separate package entirely. Happy to help with either approach. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Importing
naturalfor NLP features (tokenizers, stemmers, TF-IDF, etc.) eagerly loads all five storage plugins (Postgres, MongoDB, Redis, Memcached, File) at module scope inStorageBackend.js. Each plugin callsrequire('dotenv').config(), which reads.envfiles from disk, mutatesprocess.env, and as of dotenv v17, logs promotional messages to stdout on every call.This means anyone who imports
naturalfor basic NLP gets unwanted console output, filesystem reads, and environment mutation -- even if they never use storage. Since dotenv is only used by the storage plugins, lazy-loading them eliminates these side effects entirely for the majority of users.Here is what our production logs look like on every server restart (Fly.io, ord region). We only use
naturalfor tokenizing and stemming:Five log lines, one per storage plugin, injecting 0 env vars, with ads for a product we don't use. There's no way to silence these because the
quietoption only works when you calldotenv.config()yourself -- not when a transitive dependency calls it.For context, dotenv v17's default logging behavior has been a widespread issue:
quiet: trueis ineffective because other libs use dotenv"Solution
Move the
require()calls from the top ofStorageBackend.jsinto theswitchinsidesetStorageType(). Storage plugins are now loaded on demand only when a user explicitly creates a storage backend.This is a non-breaking change. Users of the storage feature see no difference in behavior.
Testing
All 858 existing specs pass.