miniflare 0.0.0-4e33f2cdc → 0.0.0-50437494e

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.
@@ -1494,6 +1494,30 @@ export declare function getGlobalServices({ sharedOptions, allWorkerRoutes, fall
1494
1494
 
1495
1495
  export declare function getMiniflareObjectBindings(unsafeStickyBlobs: boolean): Worker_Binding[];
1496
1496
 
1497
+ /**
1498
+ * Computes the Node.js compatibility mode we are running.
1499
+ *
1500
+ * NOTES:
1501
+ * - The v2 mode is configured via `nodejs_compat_v2` compat flag or via `nodejs_compat` plus a compatibility date of Sept 23rd. 2024 or later.
1502
+ * - See `EnvironmentInheritable` for `nodeCompat` and `noBundle`.
1503
+ *
1504
+ * @param compatibilityDateStr The compatibility date
1505
+ * @param compatibilityFlags The compatibility flags
1506
+ * @param opts.nodeCompat Whether the legacy node_compat arg is being used
1507
+ * @returns the mode and flags to indicate specific configuration for validating.
1508
+ */
1509
+ export declare function getNodeCompat(compatibilityDate: string | undefined, // Default to some arbitrary old date
1510
+ compatibilityFlags: string[], opts?: {
1511
+ nodeCompat?: boolean;
1512
+ }): {
1513
+ mode: NodeJSCompatMode;
1514
+ hasNodejsAlsFlag: boolean;
1515
+ hasNodejsCompatFlag: boolean;
1516
+ hasNodejsCompatV2Flag: boolean;
1517
+ hasNoNodejsCompatV2Flag: boolean;
1518
+ hasExperimentalNodejsCompatV2Flag: boolean;
1519
+ };
1520
+
1497
1521
  export declare function getPersistPath(pluginName: string, tmpPath: string, persist: Persistence): string;
1498
1522
 
1499
1523
  export declare function getRootPath(opts: unknown): string;
@@ -1821,6 +1845,17 @@ export declare interface Network {
1821
1845
 
1822
1846
  export declare const NODE_PLATFORM_IMPL: PlatformImpl<ReadableStream_2>;
1823
1847
 
1848
+ /**
1849
+ * We can provide Node.js compatibility in a number of different modes:
1850
+ * - "legacy" - this mode adds compile-time polyfills that are not well maintained and cannot work with workerd runtime builtins.
1851
+ * - "als": this mode tells the workerd runtime to enable only the Async Local Storage builtin library (accessible via `node:async_hooks`).
1852
+ * - "v1" - this mode tells the workerd runtime to enable some Node.js builtin libraries (accessible only via `node:...` imports) but no globals.
1853
+ * - "v2" - this mode tells the workerd runtime to enable more Node.js builtin libraries (accessible both with and without the `node:` prefix)
1854
+ * and also some Node.js globals such as `Buffer`; it also turns on additional compile-time polyfills for those that are not provided by the runtime.
1855
+ * - null - no Node.js compatibility.
1856
+ */
1857
+ export declare type NodeJSCompatMode = "legacy" | "als" | "v1" | "v2" | null;
1858
+
1824
1859
  export declare class NoOpLog extends Log {
1825
1860
  constructor();
1826
1861
  protected log(): void;
package/dist/src/index.js CHANGED
@@ -2567,6 +2567,7 @@ __export(src_exports, {
2567
2567
  getFreshSourceMapSupport: () => getFreshSourceMapSupport,
2568
2568
  getGlobalServices: () => getGlobalServices,
2569
2569
  getMiniflareObjectBindings: () => getMiniflareObjectBindings,
2570
+ getNodeCompat: () => getNodeCompat,
2570
2571
  getPersistPath: () => getPersistPath,
2571
2572
  getRootPath: () => getRootPath,
2572
2573
  globsToRegExps: () => globsToRegExps,
@@ -5485,6 +5486,51 @@ var import_util = require("util");
5485
5486
  var import_acorn = require("acorn");
5486
5487
  var import_acorn_walk = require("acorn-walk");
5487
5488
  var import_zod9 = require("zod");
5489
+
5490
+ // src/plugins/core/node-compat.ts
5491
+ function getNodeCompat(compatibilityDate = "2000-01-01", compatibilityFlags, opts) {
5492
+ const { nodeCompat = false } = opts ?? {};
5493
+ const {
5494
+ hasNodejsAlsFlag,
5495
+ hasNodejsCompatFlag,
5496
+ hasNodejsCompatV2Flag,
5497
+ hasNoNodejsCompatV2Flag,
5498
+ hasExperimentalNodejsCompatV2Flag
5499
+ } = parseNodeCompatibilityFlags(compatibilityFlags);
5500
+ const nodeCompatSwitchOverDate = "2024-09-23";
5501
+ const legacy = nodeCompat === true;
5502
+ let mode = null;
5503
+ if (hasNodejsCompatV2Flag || hasNodejsCompatFlag && compatibilityDate >= nodeCompatSwitchOverDate && !hasNoNodejsCompatV2Flag) {
5504
+ mode = "v2";
5505
+ } else if (hasNodejsCompatFlag) {
5506
+ mode = "v1";
5507
+ } else if (hasNodejsAlsFlag) {
5508
+ mode = "als";
5509
+ } else if (legacy) {
5510
+ mode = "legacy";
5511
+ }
5512
+ return {
5513
+ mode,
5514
+ hasNodejsAlsFlag,
5515
+ hasNodejsCompatFlag,
5516
+ hasNodejsCompatV2Flag,
5517
+ hasNoNodejsCompatV2Flag,
5518
+ hasExperimentalNodejsCompatV2Flag
5519
+ };
5520
+ }
5521
+ function parseNodeCompatibilityFlags(compatibilityFlags) {
5522
+ return {
5523
+ hasNodejsAlsFlag: compatibilityFlags.includes("nodejs_als"),
5524
+ hasNodejsCompatFlag: compatibilityFlags.includes("nodejs_compat"),
5525
+ hasNodejsCompatV2Flag: compatibilityFlags.includes("nodejs_compat_v2"),
5526
+ hasNoNodejsCompatV2Flag: compatibilityFlags.includes("no_nodejs_compat_v2"),
5527
+ hasExperimentalNodejsCompatV2Flag: compatibilityFlags.includes(
5528
+ "experimental:nodejs_compat_v2"
5529
+ )
5530
+ };
5531
+ }
5532
+
5533
+ // src/plugins/core/modules.ts
5488
5534
  var SUGGEST_BUNDLE = "If you're trying to import an npm package, you'll need to bundle your Worker first.";
5489
5535
  var SUGGEST_NODE = "If you're trying to import a Node.js built-in module, or an npm package that uses Node.js built-ins, you'll either need to:\n- Bundle your Worker, configuring your bundler to polyfill Node.js built-ins\n- Configure your bundler to load Workers-compatible builds by changing the main fields/conditions\n- Enable the `nodejs_compat` compatibility flag and use the `NodeJsCompatModule` module type\n- Find an alternative package that doesn't require Node.js built-ins";
5490
5536
  var builtinModulesWithPrefix = import_module.builtinModules.concat(
@@ -5592,15 +5638,18 @@ function getResolveErrorPrefix(referencingPath) {
5592
5638
  return `Unable to resolve "${relative}" dependency`;
5593
5639
  }
5594
5640
  var ModuleLocator = class {
5595
- constructor(modulesRoot, additionalModuleNames, rules = [], compatibilityFlags) {
5641
+ constructor(modulesRoot, additionalModuleNames, rules = [], compatibilityDate, compatibilityFlags) {
5596
5642
  this.modulesRoot = modulesRoot;
5597
5643
  this.additionalModuleNames = additionalModuleNames;
5598
5644
  rules = rules.concat(DEFAULT_MODULE_RULES);
5599
5645
  this.#compiledRules = compileModuleRules(rules);
5600
- this.#nodejsCompat = compatibilityFlags?.includes("nodejs_compat") ?? false;
5646
+ this.#nodejsCompatMode = getNodeCompat(
5647
+ compatibilityDate,
5648
+ compatibilityFlags ?? []
5649
+ ).mode;
5601
5650
  }
5602
5651
  #compiledRules;
5603
- #nodejsCompat;
5652
+ #nodejsCompatMode;
5604
5653
  #visitedPaths = /* @__PURE__ */ new Set();
5605
5654
  modules = [];
5606
5655
  visitEntrypoint(code, modulePath) {
@@ -5684,7 +5733,14 @@ ${dim(modulesConfig)}`;
5684
5733
  }
5685
5734
  const spec = specExpression.value;
5686
5735
  const isNodeJsCompatModule = referencingType === "NodeJsCompatModule";
5687
- if (this.#nodejsCompat && spec.startsWith("node:") || spec.startsWith("cloudflare:") || spec.startsWith("workerd:") || isNodeJsCompatModule && builtinModulesWithPrefix.includes(spec) || this.additionalModuleNames.includes(spec)) {
5736
+ if (
5737
+ // `cloudflare:` and `workerd:` imports don't need to be included explicitly
5738
+ spec.startsWith("cloudflare:") || spec.startsWith("workerd:") || // Node.js compat v1 requires imports to be prefixed with `node:`
5739
+ this.#nodejsCompatMode === "v1" && spec.startsWith("node:") || // Node.js compat modules and v2 can also handle non-prefixed imports
5740
+ (this.#nodejsCompatMode === "v2" || isNodeJsCompatModule) && builtinModulesWithPrefix.includes(spec) || // Async Local Storage mode (node_als) only deals with `node:async_hooks` imports
5741
+ this.#nodejsCompatMode === "als" && spec === "node:async_hooks" || // Any "additional" external modules can be ignored
5742
+ this.additionalModuleNames.includes(spec)
5743
+ ) {
5688
5744
  return;
5689
5745
  }
5690
5746
  if (maybeGetStringScriptPathIndex(referencingName) !== void 0) {
@@ -7447,6 +7503,7 @@ function getWorkerScript(options, workerIndex, additionalModuleNames) {
7447
7503
  modulesRoot,
7448
7504
  additionalModuleNames,
7449
7505
  options.modulesRules,
7506
+ options.compatibilityDate,
7450
7507
  options.compatibilityFlags
7451
7508
  );
7452
7509
  locator.visitEntrypoint(code, scriptPath);
@@ -10217,6 +10274,7 @@ var Miniflare2 = class {
10217
10274
  getFreshSourceMapSupport,
10218
10275
  getGlobalServices,
10219
10276
  getMiniflareObjectBindings,
10277
+ getNodeCompat,
10220
10278
  getPersistPath,
10221
10279
  getRootPath,
10222
10280
  globsToRegExps,