@schmock/core 2.1.1 → 2.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/builder.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { Server } from "node:http";
2
- import { createServer } from "node:http";
3
2
  import { normalizePath, toHttpMethod } from "./constants.js";
4
3
  import {
5
4
  errorMessage,
@@ -321,6 +320,17 @@ export class CallableMockInstance {
321
320
  );
322
321
  }
323
322
 
323
+ // Lazy-load node:http so browser bundles never pull it in. See issue #395.
324
+ return import("node:http").then(({ createServer }) =>
325
+ this.#startHttpServer(createServer, port, hostname),
326
+ );
327
+ }
328
+
329
+ #startHttpServer(
330
+ createServer: typeof import("node:http").createServer,
331
+ port: number,
332
+ hostname: string,
333
+ ): Promise<Schmock.ServerInfo> {
324
334
  const httpServer = createServer((req, res) => {
325
335
  const handleRequest = async () => {
326
336
  const url = new URL(req.url ?? "/", `http://${req.headers.host}`);
@@ -0,0 +1,35 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { resolve } from "node:path";
3
+ import { describe, expect, it } from "vitest";
4
+
5
+ /**
6
+ * Regression coverage for issue #395 — a top-level `import { createServer }
7
+ * from "node:http"` in `dist/builder.js` was preserved by browser bundlers
8
+ * (esbuild / Angular / Vite) even when `.listen()` was never called, causing
9
+ * `node:http` resolution failures in browsers.
10
+ *
11
+ * Inspect published artifacts: top-level `node:http` imports must not exist.
12
+ * `node:http` may only appear inside dynamic imports (`await import(...)`).
13
+ */
14
+ const distRoot = resolve(__dirname, "..", "dist");
15
+
16
+ // Static ESM imports from node: modules — `from "node:..."`. Dynamic imports
17
+ // (`await import("node:...")`) have no `from` clause and are not matched.
18
+ function staticNodeImports(src: string): string[] {
19
+ return src.match(/from\s*["']node:[^"']+["']/g) ?? [];
20
+ }
21
+
22
+ describe.skipIf(!existsSync(distRoot))(
23
+ "dist shape — browser compatibility (#395)",
24
+ () => {
25
+ it("dist/builder.js has no static node:* imports", () => {
26
+ const src = readFileSync(resolve(distRoot, "builder.js"), "utf8");
27
+ expect(staticNodeImports(src)).toEqual([]);
28
+ });
29
+
30
+ it("dist/index.js has no static node:* imports", () => {
31
+ const src = readFileSync(resolve(distRoot, "index.js"), "utf8");
32
+ expect(staticNodeImports(src)).toEqual([]);
33
+ });
34
+ },
35
+ );