fix(jsr): resolve require from source, not from tsup's banner
JSR publishes `src/` and runs no bundler, so the `createRequire` banner tsup gives
the ESM build (tsup.config.ts) never exists there. Every lazy `require(...)` in the
source had nothing to resolve, and one of them was unguarded: `@Html('file')` and
template mode died at boot on Deno with `ReferenceError: require is not defined`.
The two guarded sites were worse than the crash, because they answered confidently
and wrongly. `static` reported *"needs a filesystem and is unavailable on this
runtime (edge)"* while running on Deno, which has one; multipart reported busboy as
not installed while it sat in node_modules. Both blamed the runtime for a packaging
problem, and both named a runtime the reader was not on.
Route all ten call sites through one `nodeRequire`, which prefers the ambient
`require` — so the CJS build and the banner-carrying ESM build behave exactly as
before — and otherwise rebuilds one from `process.getBuiltinModule('node:module')`.
Node 22.3+, Deno and Bun all expose that synchronously, which is the requirement:
every caller sits on a sync path a dynamic `import()` could not serve. It resolves
from `process.cwd()`, since a peer dependency like busboy belongs to the application,
not to a module served over https from a registry.
Where neither exists — workerd, where esbuild rewrites the call into its own throwing
stub — the behaviour is unchanged: the guarded sites still catch, and their "edge has
no filesystem" story is finally the true one.
Covered by a Deno suite that imports `src/` exactly as JSR serves it. The other Deno
tests already did that; none of them touched a path that loads a Node builtin lazily,
which is how a package that died at boot kept a green suite.
Closes #69.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
11 files changed
+145
-23
No files matched your search
+5
-4
@@ -9,6 +9,7 @@ import { acquireBody, type BodyReader } from './body';
|
||||
import type { RouteDef, HttpOptions } from './types';
|
||||
import type { StreamEncoder } from '../encoders';
|
||||
import type { Bus } from '../bus';
|
||||
import { nodeRequire } from '../node-require';
|
||||
|
||||
/**
|
||||
* `randomUUID`, from wherever this runtime keeps it. Resolved once, at import.
|
||||
@@ -23,15 +24,15 @@ import type { Bus } from '../bus';
|
||||
* CI's Node 18 job caught it; every newer Node and every other runtime hides it, which is exactly
|
||||
* why that job is pinned to the floor rather than to the current release.
|
||||
*
|
||||
* The fallback is reached only by a runtime that lacks the global, so workerd never evaluates the
|
||||
* `require` — and the ESM build has a real one, from the `createRequire` banner in `tsup.config.ts`.
|
||||
* The fallback is reached only by a runtime that lacks the global, so workerd never evaluates it.
|
||||
* It goes through {@link nodeRequire} rather than a bare `require`, which is what makes it work on
|
||||
* every distribution of this package and not just the bundled ones.
|
||||
*/
|
||||
const randomUUID: () => string = (() => {
|
||||
const webCrypto = (globalThis as { crypto?: { randomUUID?: () => string } }).crypto;
|
||||
if (typeof webCrypto?.randomUUID === 'function') return () => webCrypto.randomUUID!();
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
return (require('node:crypto') as { randomUUID: () => string }).randomUUID;
|
||||
return nodeRequire<{ randomUUID: () => string }>('node:crypto').randomUUID;
|
||||
})();
|
||||
|
||||
/**
|
||||
|
||||
+3
-4
@@ -11,6 +11,7 @@ import { mergeInjectedHeaders } from './headers';
|
||||
import { handle, correlateRequest, type HandleResult, type Preflight } from './core';
|
||||
import type { BodyFailure, BodyReader } from './body';
|
||||
import type { RouteDef, WsRouteDef, MeshControl, HttpOptions } from './types';
|
||||
import { nodeRequire } from '../node-require';
|
||||
|
||||
interface HandlerConfig {
|
||||
routes: RouteDef[];
|
||||
@@ -44,10 +45,8 @@ export function createHttpServer(
|
||||
|
||||
// Node http/https are loaded lazily so importing createApp stays edge/workerd-safe
|
||||
// (workerd's nodejs_compat provides no node:http/node:https). This path only runs under listen().
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const nodeHttp = require('http') as typeof import('http');
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const nodeHttps = require('https') as typeof import('https');
|
||||
const nodeHttp = nodeRequire<typeof import('http')>('http');
|
||||
const nodeHttps = nodeRequire<typeof import('https')>('https');
|
||||
|
||||
// runtime-compatible; only @types differ (https.Server lacks the http-only timeout props)
|
||||
const server: http.Server = (opts?.tls
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { channel } from '../channel';
|
||||
import type { App } from '../app/types';
|
||||
import type { WsRequest, WsSocket } from './ws-core';
|
||||
import { nodeRequire } from '../node-require';
|
||||
|
||||
/**
|
||||
* Shared plumbing for the runtime adapters (Bun/Deno/edge) that bridge a host WebSocket
|
||||
@@ -123,8 +124,7 @@ export function loadWebSocketCtor(): SocketCtor {
|
||||
|
||||
if (platform) return platform;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
return require('ws') as SocketCtor;
|
||||
return nodeRequire<SocketCtor>('ws');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -3,14 +3,14 @@ import { channel } from '../channel';
|
||||
import type { Bus } from '../bus';
|
||||
import { runWsConnection, matchWsRoute, trackUntil, type WsSocket, type WsRequest } from './ws-core';
|
||||
import type { WsRouteDef, MeshControl } from './types';
|
||||
import { nodeRequire } from '../node-require';
|
||||
|
||||
/** Lazily loads the optional `ws` peer dependency's `WebSocketServer`, or null when it is not installed. */
|
||||
function loadWss(): any | null {
|
||||
try {
|
||||
// `ws` is an OPTIONAL peer dependency: lazy-required so installs without WebSocket
|
||||
// support don't fail. When absent this throws and we fall back to null (501 on upgrade).
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
return require('ws').WebSocketServer;
|
||||
return nodeRequire<{ WebSocketServer: unknown }>('ws').WebSocketServer;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user