Skip to content

Add subpath exports to @gigadrive/commons for client-side usage #297

Description

@Zeryther

Problem

@gigadrive/commons only has a single root export (. in package.json exports). Importing anything from the package (e.g. formatFileSize) pulls in the entire bundle, which includes Node.js-only code like encrypt/decrypt (using crypto), BufferedReadableStream, and sha256.

This causes a runtime crash in Next.js client components ('use client'):

Class extends value undefined is not a constructor or null
    at module evaluation (file-preview-panel.tsx:3:1)

The error happens because Node.js globals (crypto.subtle in certain contexts, stream.Readable) are not available in the browser bundle.

Current workaround

Pure utility functions like formatFileSize have to be duplicated locally in consumer packages instead of being imported from @gigadrive/commons. See: https://github.com/Gigadrive/network/blob/feature/storage-management-ui/apps/console.gigadrive.de/src/components/storage/format-bytes.ts

Proposed fix

Add subpath exports to @gigadrive/commons so that pure/universal utilities can be imported without pulling in Node.js-only code:

{
  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "require": "./dist/index.js",
      "import": "./dist/index.mjs"
    },
    "./format": {
      "types": "./dist/format.d.ts",
      "require": "./dist/format.js",
      "import": "./dist/format.mjs"
    }
  }
}

This would allow:

// Works in client components — no Node.js dependencies
import { formatFileSize } from '@gigadrive/commons/format';

Alternatively, consider splitting the package into environment-aware entrypoints (e.g. ./crypto for Node-only, ./format and ./utils for universal code).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions