• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

project-slippi / slippi-js / 19321334711

13 Nov 2025 05:15AM UTC coverage: 81.39% (-2.3%) from 83.652%
19321334711

push

github

web-flow
Fix broken enet loading in CJS bundle and also lazy load reconnect-core import (#149)

* use enet module default if it exists

* load enet module function

* mark enet and reconnect-core as optional dependencies

* dynamically import reconnect-core

* oops add missing file

* fix enet types

688 of 916 branches covered (75.11%)

Branch coverage included in aggregate %.

6 of 59 new or added lines in 4 files covered. (10.17%)

1 existing line in 1 file now uncovered.

1901 of 2265 relevant lines covered (83.93%)

241487.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.76
/src/console/loadEnetModule.ts
1
// Support CJS (v7-) and ESM default export (v8+)
2
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
3
export type EnetModule = typeof import("enet");
4

5
let enetModule: EnetModule | undefined;
6
let loadPromise: Promise<EnetModule | undefined> | undefined;
7

8
// Keep a single dynamic import function that TS won’t downlevel.
9
const dynamicImport: (s: string) => Promise<any> =
10
  // eslint-disable-next-line no-new-func
11
  new Function("s", "return import(s)") as any;
24✔
12

13
async function maybeLoadEnetModule(): Promise<EnetModule | undefined> {
NEW
14
  if (enetModule) {
×
NEW
15
    return enetModule;
×
16
  }
NEW
17
  if (loadPromise) {
×
NEW
18
    return loadPromise;
×
19
  }
20

21
  // Try CJS first (works in both Node & webpack)
NEW
22
  try {
×
23
    // eslint-disable-next-line @typescript-eslint/no-var-requires
NEW
24
    const mod = require("enet") as any;
×
NEW
25
    enetModule = (mod?.default ? mod?.default : mod) as EnetModule | undefined;
×
NEW
26
    return enetModule;
×
27
  } catch (error: any) {
28
    // Ignore and try ESM
29
  }
30

31
  // Fallback: ESM dynamic import (v8+)
NEW
32
  loadPromise = dynamicImport("enet")
×
NEW
33
    .then((m: any) => (m?.default ? m?.default : m) as EnetModule | undefined)
×
NEW
34
    .catch(() => undefined);
×
35

NEW
36
  enetModule = await loadPromise;
×
NEW
37
  return enetModule;
×
38
}
39

40
export async function loadEnetModule(): Promise<EnetModule> {
24✔
NEW
41
  const enetModule = await maybeLoadEnetModule();
×
NEW
42
  if (!enetModule) {
×
NEW
43
    throw new Error("enet is required to connect to Dolphin. Install it with: npm install enet");
×
44
  }
NEW
45
  return enetModule;
×
46
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc