env-runner 0.1.3 → 0.1.4

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.
@@ -1,6 +1,9 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { pathToFileURL } from "node:url";
3
+ import { isAbsolute } from "node:path";
1
4
  //#region src/common/worker-utils.ts
2
5
  async function resolveEntry(entryPath) {
3
- const mod = await import(entryPath);
6
+ const mod = await import(_toImportPath(entryPath));
4
7
  const entry = mod.default || mod;
5
8
  if (typeof entry.fetch !== "function") throw new Error(`[env-runner] Entry module "${entryPath}" must export a \`fetch\` handler (export default { fetch(req) { ... } }).`);
6
9
  return entry;
@@ -18,9 +21,23 @@ function parseServerAddress(server) {
18
21
  */
19
22
  async function reloadEntryModule(entryPath, currentEntry, sendMessage) {
20
23
  await currentEntry.ipc?.onClose?.();
21
- const newEntry = await resolveEntry(entryPath + "?t=" + Date.now());
24
+ const newEntry = await _importFresh(entryPath);
22
25
  await newEntry.ipc?.onOpen?.({ sendMessage });
23
26
  return newEntry;
24
27
  }
28
+ function _toImportPath(entryPath) {
29
+ const qIndex = entryPath.indexOf("?");
30
+ const filePath = qIndex === -1 ? entryPath : entryPath.slice(0, qIndex);
31
+ const query = qIndex === -1 ? "" : entryPath.slice(qIndex);
32
+ if (isAbsolute(filePath)) return pathToFileURL(filePath).href + query;
33
+ return entryPath;
34
+ }
35
+ async function _importFresh(entryPath) {
36
+ const code = readFileSync(entryPath, "utf8");
37
+ const mod = await import("data:text/javascript;base64," + Buffer.from(code).toString("base64"));
38
+ const entry = mod.default || mod;
39
+ if (typeof entry.fetch !== "function") throw new Error(`[env-runner] Entry module "${entryPath}" must export a \`fetch\` handler (export default { fetch(req) { ... } }).`);
40
+ return entry;
41
+ }
25
42
  //#endregion
26
43
  export { reloadEntryModule as n, resolveEntry as r, parseServerAddress as t };
@@ -5,6 +5,7 @@ import { n as reloadEntryModule, r as resolveEntry } from "../../_chunks/worker-
5
5
  var SelfEnvRunner = class extends BaseEnvRunner {
6
6
  #active = false;
7
7
  #entry;
8
+ #wsAdapter;
8
9
  constructor(opts) {
9
10
  super({
10
11
  ...opts,
@@ -18,8 +19,12 @@ var SelfEnvRunner = class extends BaseEnvRunner {
18
19
  return this.#entry.fetch(request);
19
20
  }
20
21
  async upgrade(context) {
21
- if (!this.#entry?.upgrade || this.closed) return;
22
- this.#entry.upgrade(context);
22
+ if (!this.#entry || this.closed) return;
23
+ if (this.#entry.websocket) {
24
+ await (await this.#resolveWSAdapter()).handleUpgrade(context.node.req, context.node.socket, context.node.head);
25
+ return;
26
+ }
27
+ this.#entry.upgrade?.(context);
23
28
  }
24
29
  sendMessage(message) {
25
30
  if (!this.#active) throw new Error("Self env runner should be initialized before sending messages.");
@@ -38,6 +43,8 @@ var SelfEnvRunner = class extends BaseEnvRunner {
38
43
  const sendFn = (message) => {
39
44
  queueMicrotask(() => this._handleMessage(message));
40
45
  };
46
+ this.#wsAdapter?.closeAll(1001, void 0, true);
47
+ this.#wsAdapter = void 0;
41
48
  this.#entry = await reloadEntryModule(entryPath, this.#entry, sendFn);
42
49
  }
43
50
  _hasRuntime() {
@@ -49,9 +56,18 @@ var SelfEnvRunner = class extends BaseEnvRunner {
49
56
  async _closeRuntime() {
50
57
  if (!this.#active) return;
51
58
  this.#active = false;
59
+ this.#wsAdapter?.closeAll(1001, void 0, true);
60
+ this.#wsAdapter = void 0;
52
61
  await this.#entry?.ipc?.onClose?.();
53
62
  this.#entry = void 0;
54
63
  }
64
+ async #resolveWSAdapter() {
65
+ if (!this.#wsAdapter) {
66
+ const { default: nodeAdapter } = await import("crossws/adapters/node");
67
+ this.#wsAdapter = nodeAdapter({ hooks: this.#entry.websocket });
68
+ }
69
+ return this.#wsAdapter;
70
+ }
55
71
  #init() {
56
72
  const entryPath = this._data?.entry;
57
73
  if (!entryPath) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "env-runner",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Generic environment runner for JavaScript runtimes.",
5
5
  "license": "MIT",
6
6
  "repository": "unjs/env-runner",