File size: 6,139 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var repl_exports = {};
__export(repl_exports, {
  Repl: () => Repl
});
module.exports = __toCommonJS(repl_exports);
var fs = __toESM(require("fs"));
var net = __toESM(require("net"));
var path = __toESM(require("path"));
var repl = __toESM(require("repl"));
var import_crashlogger = require("./crashlogger");
var import_fs = require("./fs");
/**
 * REPL
 *
 * Documented in logs/repl/README.md
 * https://github.com/smogon/pokemon-showdown/blob/master/logs/repl/README.md
 *
 * @author kota
 * @license MIT
 */
const Repl = new class {
  constructor() {
    /**
     * Contains the pathnames of all active REPL sockets.
     */
    this.socketPathnames = /* @__PURE__ */ new Set();
    this.listenersSetup = false;
  }
  setupListeners(filename) {
    if (Repl.listenersSetup)
      return;
    Repl.listenersSetup = true;
    process.once("exit", (code) => {
      for (const s of Repl.socketPathnames) {
        try {
          fs.unlinkSync(s);
        } catch {
        }
      }
      if (code === 129 || code === 130) {
        process.exitCode = 0;
      }
    });
    if (!process.listeners("SIGHUP").length) {
      process.once("SIGHUP", () => process.exit(128 + 1));
    }
    if (!process.listeners("SIGINT").length) {
      process.once("SIGINT", () => process.exit(128 + 2));
    }
    global.heapdump = (targetPath) => {
      if (!targetPath)
        targetPath = `${filename}-${new Date().toISOString()}`;
      let handler;
      try {
        handler = require("node-oom-heapdump")();
      } catch (e) {
        if (e.code !== "MODULE_NOT_FOUND")
          throw e;
        throw new Error(`node-oom-heapdump is not installed. Run \`npm install --no-save node-oom-heapdump\` and try again.`);
      }
      return handler.createHeapSnapshot(targetPath);
    };
  }
  /**
   * Delete old sockets in the REPL directory (presumably from a crashed
   * previous launch of PS).
   *
   * Does everything synchronously, so that the directory is guaranteed
   * clean and ready for new REPL sockets by the time this function returns.
   */
  cleanup() {
    const config = typeof Config !== "undefined" ? Config : {};
    if (!config.repl)
      return;
    const directory = path.dirname(
      path.resolve(import_fs.FS.ROOT_PATH, config.replsocketprefix || "logs/repl", "app")
    );
    let files;
    try {
      files = fs.readdirSync(directory);
    } catch {
    }
    if (files) {
      for (const file of files) {
        const pathname = path.resolve(directory, file);
        const stat = fs.statSync(pathname);
        if (!stat.isSocket())
          continue;
        const socket = net.connect(pathname, () => {
          socket.end();
          socket.destroy();
        }).on("error", () => {
          fs.unlinkSync(pathname);
        });
      }
    }
  }
  /**
   * Starts a REPL server, using a UNIX socket for IPC. The eval function
   * parametre is passed in because there is no other way to access a file's
   * non-global context.
   */
  start(filename, evalFunction) {
    const config = typeof Config !== "undefined" ? Config : {};
    if (!config.repl)
      return;
    Repl.setupListeners(filename);
    const server = net.createServer((socket) => {
      repl.start({
        input: socket,
        output: socket,
        eval(cmd, context, unusedFilename, callback) {
          try {
            return callback(null, evalFunction(cmd));
          } catch (e) {
            return callback(e, void 0);
          }
        }
      }).on("exit", () => socket.end());
      socket.on("error", () => socket.destroy());
    });
    const pathname = path.resolve(import_fs.FS.ROOT_PATH, Config.replsocketprefix || "logs/repl", filename);
    try {
      server.listen(pathname, () => {
        fs.chmodSync(pathname, Config.replsocketmode || 384);
        Repl.socketPathnames.add(pathname);
      });
      server.once("error", (err) => {
        server.close();
        if (err.code === "EADDRINUSE") {
          fs.unlink(pathname, (_err) => {
            if (_err && _err.code !== "ENOENT") {
              (0, import_crashlogger.crashlogger)(_err, `REPL: ${filename}`);
            }
          });
        } else if (err.code === "EACCES") {
          if (process.platform !== "win32") {
            console.error(`Could not start REPL server "${filename}": Your filesystem doesn't support Unix sockets (everything else will still work)`);
          }
        } else {
          (0, import_crashlogger.crashlogger)(err, `REPL: ${filename}`);
        }
      });
      server.once("close", () => {
        Repl.socketPathnames.delete(pathname);
      });
    } catch (err) {
      console.error(`Could not start REPL server "${filename}": ${err}`);
    }
  }
}();
//# sourceMappingURL=repl.js.map