[GH-ISSUE #13] Vendored cuid module causes issues w/Vite bundling of client code #11

Closed
opened 2026-05-23 08:36:25 -06:00 by gitea-mirror · 4 comments
Owner

Originally created by @rcoder on GitHub (May 10, 2022).
Original GitHub issue: https://github.com/appy-one/acebase-core/issues/13

Originally assigned to: @appy-one on GitHub.

I've been trying out AceBase with SvelteKit and really enjoying the combination except for the fact that I can't seem to remove an implicit dependency on the NodeJS process module being included in bundled builds.

This repo has a simple case that shows what's happening: https://github.com/rcoder/dbc

tl;dr: development mode works fine, but when I make a production build of the client, the cuid fingerprint function gets dragged in, which in turn uses process.pid.

The exact error in the browser console is:

Uncaught (in promise) ReferenceError: process is not defined
    <anonymous> http://localhost:3000/_app/chunks/stores-a9318368.js:1

I'm by no means an expert on Vite, or modern bundling in general, but it seems to me that the shimmed browser-specific code in this module isn't being pulled in correctly.

Are there examples or recommended settings for using AceBase as a client in an SSR framwork like SvelteKit? I've certainly been bitten by other modules in this way, but my usual tricks to force ESM loading, change the bundling rules, etc. haven't seemed to help.

I'm also open to the argument that this is a SvelteKit problem, not an AceBase issue. The former is explicitly in beta, but I suspect the fundamental issue is related to Vite, not Svelte, which means it seems likely to pop up in other frameworks and tools that use Vite...which is a lot of them.

P.S.: As an aside, I'm curious what cuid brings to the table that, say, the more general ulid module (which works quite nicely across bundlers + packaging models in my experience) doesn't provide. The precise timing measurements between client and server seem nice in theory, but many of the systems I work with have no reliable clock at all and I'm a bit worried that skew between client nodes could cause issues down the road.

Originally created by @rcoder on GitHub (May 10, 2022). Original GitHub issue: https://github.com/appy-one/acebase-core/issues/13 Originally assigned to: @appy-one on GitHub. I've been trying out AceBase with SvelteKit and really enjoying the combination _except_ for the fact that I can't seem to remove an implicit dependency on the NodeJS `process` module being included in bundled builds. This repo has a simple case that shows what's happening: https://github.com/rcoder/dbc tl;dr: development mode works fine, but when I make a production build of the client, the cuid `fingerprint` function gets dragged in, which in turn uses `process.pid`. The exact error in the browser console is: ``` Uncaught (in promise) ReferenceError: process is not defined <anonymous> http://localhost:3000/_app/chunks/stores-a9318368.js:1 ``` I'm by no means an expert on Vite, or modern bundling in general, but it seems to me that the shimmed browser-specific code in this module isn't being pulled in correctly. Are there examples or recommended settings for using AceBase as a client in an SSR framwork like SvelteKit? I've certainly been bitten by other modules in this way, but my usual tricks to force ESM loading, change the bundling rules, etc. haven't seemed to help. I'm also open to the argument that this is a SvelteKit problem, not an AceBase issue. The former is explicitly in beta, but I suspect the fundamental issue is related to Vite, not Svelte, which means it seems likely to pop up in other frameworks and tools that use Vite...which is a lot of them. P.S.: As an aside, I'm curious what `cuid` brings to the table that, say, the more general `ulid` module (which works quite nicely across bundlers + packaging models in my experience) doesn't provide. The precise timing measurements between client and server seem nice in theory, but many of the systems I work with have no reliable clock at all and I'm a bit worried that skew between client nodes could cause issues down the road.
Author
Owner

@appy-one commented on GitHub (May 11, 2022):

Thanks for reporting this. I think this might have been caused by my recent changes that moved the browser file replacements from the main package.json to the ones in the dist/cjs and dist/esm directories. Webpack and browserify both use those correctly, but I suspect Vite does not and only looks in the root package.json.

Can you try adding the following lines to acebase-core's main package.json :

    "browser": {
        "./dist/cjs/process/index.js": "./dist/cjs/process/browser.js",
        "./dist/esm/process/index.js": "./dist/esm/process/browser.js",
        "./dist/cjs/cuid/fingerprint/index.js": "./dist/cjs/cuid/fingerprint/browser.js"
        "./dist/esm/cuid/fingerprint/index.js": "./dist/esm/cuid/fingerprint/browser.js"
    }

Let me know if that fixes it

Regarding your question about cuid: they are extremely collision resistant and even ID's generated within the same millisecond are sorted the right way. The time bias I added to the implementation allows all clients to use the server's time instead of their own, so they will all generate cuids that have more accurate timestamps - at least according to the server.

<!-- gh-comment-id:1123554369 --> @appy-one commented on GitHub (May 11, 2022): Thanks for reporting this. I think this might have been caused by my recent changes that moved the browser file replacements from the main _package.json_ to the ones in the _dist/cjs_ and _dist/esm_ directories. Webpack and browserify both use those correctly, but I suspect Vite does not and only looks in the root _package.json_. Can you try adding the following lines to acebase-core's main _package.json_ : ```json "browser": { "./dist/cjs/process/index.js": "./dist/cjs/process/browser.js", "./dist/esm/process/index.js": "./dist/esm/process/browser.js", "./dist/cjs/cuid/fingerprint/index.js": "./dist/cjs/cuid/fingerprint/browser.js" "./dist/esm/cuid/fingerprint/index.js": "./dist/esm/cuid/fingerprint/browser.js" } ``` Let me know if that fixes it Regarding your question about cuid: they are extremely collision resistant and even ID's generated within the same millisecond are sorted the right way. The time bias I added to the implementation allows all clients to use the server's time instead of their own, so they will all generate cuids that have more accurate timestamps - at least according to the server.
Author
Owner

@appy-one commented on GitHub (May 11, 2022):

By the way, while browsing your repository (cool stuff!) I noticed in your acebase.json config file your database names have a / in them. In the browser that won't be a problem, but on the server it might because it changes the path where the files are stored on disk. I haven't tested so it might work fine, but I'd still recommend against it!

<!-- gh-comment-id:1123569798 --> @appy-one commented on GitHub (May 11, 2022): By the way, while browsing your repository (cool stuff!) I noticed in your [acebase.json config file](https://github.com/rcoder/dbc/blob/main/conf/acebase.json) your database names have a _/_ in them. In the browser that won't be a problem, but on the server it might because it changes the path where the files are stored on disk. I haven't tested so it _might_ work fine, but I'd still recommend against it!
Author
Owner

@rcoder commented on GitHub (May 11, 2022):

The package.json changes worked perfectly. Thanks!

Here's the exact change that worked for me: 16cedaff8a

Re: DB names with path separators: I wasn't sure myself if it would work but tried on impulse. Not only the browser client but my SvelteKit endpoints and dedicated acebase-server daemon just did "the right thing".

If that's surprising/not something that will necessarily keep working in future revisions I can certainly change to explicitly using the path option on database creation.

<!-- gh-comment-id:1123956174 --> @rcoder commented on GitHub (May 11, 2022): The `package.json` changes worked perfectly. Thanks! Here's the exact change that worked for me: https://github.com/rcoder/dbc/commit/16cedaff8a07f7737688cbc252f1537a942d0b76 Re: DB names with path separators: I wasn't sure myself if it would work but tried on impulse. Not only the browser client but my SvelteKit endpoints _and_ dedicated `acebase-server` daemon just did "the right thing". If that's surprising/not something that will necessarily keep working in future revisions I can certainly change to explicitly using the `path` option on database creation.
Author
Owner

@appy-one commented on GitHub (May 17, 2022):

The issue's fixed with this commit, published changes in v1.16.0

<!-- gh-comment-id:1128555076 --> @appy-one commented on GitHub (May 17, 2022): The issue's fixed with [this commit](https://github.com/appy-one/acebase-core/commit/26a7a2da59edcb405c1777bde3b68d0b493d8e19), published changes in v1.16.0
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/acebase-core#11
No description provided.