FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: add browser/MV3 build (dist/index.browser.js) by jdalton · Pull Request #666 · SocketDev/socket-sdk-js · GitHub

feat: add browser/MV3 build (dist/index.browser.js) - #666

Merged
John-David Dalton (jdalton) merged 1 commit into
mainfrom
sdk-browser-build
Jul 24, 2026
Merged

feat: add browser/MV3 build (dist/index.browser.js)#666
John-David Dalton (jdalton) merged 1 commit into
mainfrom
sdk-browser-build

Conversation

Copy link
Copy Markdown
Collaborator

Problem

Published `dist/index.js` can't load in a Chrome MV3 service worker: it statically inlines `@socketsecurity/lib`'s node http transport (`node:http`/`https`/`zlib` + eval-time `createRequire`), so `import`ing the SDK throws `require is not defined` at module load.

Fix

Add a fetch-based, node-free browser bundle selected via a new `browser` export condition.

  • Route the four `httpRequest` imports through the lib ROOT `@socketsecurity/lib/http-request` subpath (has a `browser` condition) instead of the deep `.../http-request/request` (node-only). Node build unchanged — `node.js` re-exports the identical `httpRequest` and still resolves the node transport.
  • New `.config/repo/rolldown.browser.config.mts`: `platform:'browser'` + `conditionNames ['browser','import','module','default']` (mirrors socket-webext) selects lib's fetch transport. A node-builtin shim plugin (`.config/repo/rolldown/browser-node-shims.mts`) swaps every `node:` (and the bare specifiers lib's `node/` wrappers use) for browser-safe virtual modules; `fs`/`crypto`/`stream` throw only when a node-only file method is called. lib's `node/os.js` is stubbed so `constants/platform`'s eager `getOs()` doesn't crash at load.
  • Self-contained ESM bundle (lib inlined, `external: []`); `build.mts` emits it alongside the node build.
  • `bundle-validation` test asserts the browser bundle is node-free (AST scan).

Verification

  • Node build byte-behavior unchanged (`node:http`/`https`/`zlib` transport intact); full unit suite green (the only 3 failures are the pre-existing `resolveBasePath` assertions that hardcode the checkout dir name `socket-sdk-js` — identical on clean `origin/main`, an artifact of the `/tmp` build worktree).
  • Browser bundle: zero executable `node:*`/`require`; loads + constructs `SocketSdk` with only `globalThis.fetch`; `getQuota` routes through `fetch` to `https://api.socket.dev\`.
  • A downstream rolldown `platform:'browser'` bundle of `import { SocketSdk }` is also node-free.

tsgo + lint green.

The published dist/index.js can't load in a Chrome MV3 service worker: it
statically inlines @socketsecurity/lib's node http transport
(node:http/https/zlib + eval-time createRequire), so importing the SDK throws
`require is not defined` at module load.

Add a fetch-based, node-free browser bundle selected via a new `browser`
export condition.

- Route the four httpRequest imports through the lib ROOT
  @socketsecurity/lib/http-request subpath (has a `browser` condition) instead
  of the deep .../http-request/request path (node-only). Node build is
  unchanged: node.js re-exports the identical httpRequest and still resolves
  the node http transport.
- New .config/repo/rolldown.browser.config.mts: platform:'browser' +
  conditionNames ['browser','import','module','default'] (mirrors socket-webext)
  so lib's fetch transport (http-request/browser.js, "Designed for Chrome MV3
  service workers") is selected. A node-builtin shim plugin
  (.config/repo/rolldown/browser-node-shims.mts) replaces every node:* (and the
  bare specifiers lib's node/* wrappers use) with browser-safe virtual modules;
  fs/crypto/stream throw only when a node-only file method is called. lib's
  node/os.js is stubbed so constants/platform's eager getOs() doesn't crash at
  load.
- Self-contained ESM bundle (lib inlined, external []); build.mts emits it
  alongside the node build.
- bundle-validation test asserts the browser bundle is node-free (AST scan).

Verified: node build byte-behavior unchanged (node:http/https/zlib transport
intact, full suite green); browser bundle has zero executable node:* / require;
loads and constructs SocketSdk with only globalThis.fetch; getQuota routes
through fetch to https://api.socket.dev.
John-David Dalton (jdalton) merged commit 419b784 into main Jul 24, 2026
2 checks passed
John-David Dalton (jdalton) deleted the sdk-browser-build branch July 24, 2026 22:21
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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant


Back | FazBrowse Home | New Git URL