env-runner 0.1.0 → 0.1.1

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.
@@ -26,11 +26,17 @@ var DenoProcessEnvRunner = class extends BaseEnvRunner {
26
26
  }
27
27
  async _closeRuntime() {
28
28
  if (!this.#process) return;
29
- await this._requestGracefulShutdown(() => this.#process.send({ event: "shutdown" }), (resolve) => {
29
+ if (this.#process._exitCode == null) await this._requestGracefulShutdown(() => {
30
+ try {
31
+ this.#process.send({ event: "shutdown" });
32
+ } catch {}
33
+ }, (resolve) => {
30
34
  this.#process?.exited.then(() => resolve());
31
35
  }, () => this.#process?._exitCode != null);
32
36
  this.#process.removeAllListeners?.();
33
- this.#process.kill();
37
+ try {
38
+ this.#process.kill();
39
+ } catch {}
34
40
  this.#process = void 0;
35
41
  }
36
42
  #initProcess(execArgv) {
@@ -73,9 +79,11 @@ var DenoProcessEnvRunner = class extends BaseEnvRunner {
73
79
  handle._exitCode = code;
74
80
  this.close(`process exited with code ${code}`);
75
81
  });
76
- child.once("error", (error) => {
77
- console.error(`Process error:`, error);
78
- this.close(error);
82
+ child.on("error", (error) => {
83
+ if (!this.closed) {
84
+ console.error(`Process error:`, error);
85
+ this.close(error);
86
+ }
79
87
  });
80
88
  child.on("message", (message) => {
81
89
  this._handleMessage(message);
@@ -58,6 +58,7 @@ var MiniflareEnvRunner = class extends BaseEnvRunner {
58
58
  const { Miniflare } = await import("miniflare");
59
59
  const entryPath = this._data?.entry;
60
60
  const options = {
61
+ compatibilityDate: (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
61
62
  modules: true,
62
63
  ...this.#miniflareOptions
63
64
  };
@@ -93,7 +94,7 @@ var MiniflareEnvRunner = class extends BaseEnvRunner {
93
94
  * and bridges the `ipc` hooks via `env.__ENV_RUNNER_IPC` service binding.
94
95
  */
95
96
  function wrapScript(userCode) {
96
- return `${userCode.replace(/export\s+default\s+/, "const __userEntry = ")}
97
+ return `${userCode.replace(/export\s+default\s+/, "const __userEntry = ").replace(/export\s*\{([^}]*?\b(\w+)\b\s+as\s+default\b[^}]*)\}/, (_match, _inner, identifier) => `const __userEntry = ${identifier};`)}
97
98
 
98
99
  // --- env-runner IPC glue ---
99
100
  let __ipcInitialized = false;
@@ -1,11 +1,24 @@
1
1
  import "../../_chunks/libs/std-env.mjs";
2
2
  import { t as BaseEnvRunner } from "../../_chunks/base-runner.mjs";
3
3
  import { existsSync } from "node:fs";
4
- import { spawn } from "node:child_process";
4
+ import { execSync, spawn } from "node:child_process";
5
5
  import { fileURLToPath } from "node:url";
6
+ import { join } from "node:path";
7
+ import { homedir } from "node:os";
6
8
  //#region src/runners/bun-process/runner.ts
7
9
  let _defaultEntry;
10
+ let _bunPath;
8
11
  const _isBun = typeof Bun !== "undefined";
12
+ function resolveBunPath() {
13
+ if (_bunPath) return _bunPath;
14
+ const candidates = [join(homedir(), ".bun", "bin", "bun")];
15
+ for (const p of candidates) if (existsSync(p)) return _bunPath = p;
16
+ try {
17
+ const resolved = execSync("which bun", { encoding: "utf8" }).trim();
18
+ if (resolved) return _bunPath = resolved;
19
+ } catch {}
20
+ return _bunPath = "bun";
21
+ }
9
22
  var BunProcessEnvRunner = class extends BaseEnvRunner {
10
23
  #process;
11
24
  constructor(opts) {
@@ -28,11 +41,17 @@ var BunProcessEnvRunner = class extends BaseEnvRunner {
28
41
  }
29
42
  async _closeRuntime() {
30
43
  if (!this.#process) return;
31
- await this._requestGracefulShutdown(() => this.#process.send({ event: "shutdown" }), (resolve) => {
44
+ if (this.#process._exitCode == null) await this._requestGracefulShutdown(() => {
45
+ try {
46
+ this.#process.send({ event: "shutdown" });
47
+ } catch {}
48
+ }, (resolve) => {
32
49
  this.#process?.exited.then(() => resolve());
33
50
  }, () => this.#process?._exitCode != null);
34
51
  this.#process.removeAllListeners?.();
35
- this.#process.kill();
52
+ try {
53
+ this.#process.kill();
54
+ } catch {}
36
55
  this.#process = void 0;
37
56
  }
38
57
  #initProcess(execArgv) {
@@ -51,7 +70,7 @@ var BunProcessEnvRunner = class extends BaseEnvRunner {
51
70
  #initBunProcess(execArgv, env) {
52
71
  const proc = Bun.spawn({
53
72
  cmd: [
54
- "bun",
73
+ resolveBunPath(),
55
74
  ...execArgv || [],
56
75
  this._workerEntry
57
76
  ],
@@ -79,7 +98,7 @@ var BunProcessEnvRunner = class extends BaseEnvRunner {
79
98
  this.#process = child;
80
99
  }
81
100
  #initNodeProcess(execArgv, env) {
82
- const child = spawn("bun", [...execArgv || [], this._workerEntry], {
101
+ const child = spawn(resolveBunPath(), [...execArgv || [], this._workerEntry], {
83
102
  env,
84
103
  stdio: [
85
104
  "pipe",
@@ -103,9 +122,11 @@ var BunProcessEnvRunner = class extends BaseEnvRunner {
103
122
  handle._exitCode = code;
104
123
  this.close(`process exited with code ${code}`);
105
124
  });
106
- child.once("error", (error) => {
107
- console.error(`Process error:`, error);
108
- this.close(error);
125
+ child.on("error", (error) => {
126
+ if (!this.closed) {
127
+ console.error(`Process error:`, error);
128
+ this.close(error);
129
+ }
109
130
  });
110
131
  child.on("message", (message) => {
111
132
  this._handleMessage(message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "env-runner",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Generic environment runner for JavaScript runtimes.",
5
5
  "license": "MIT",
6
6
  "repository": "unjs/env-runner",