xypriss 9.14.18 → 9.14.19

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.
package/dist/cjs/index.js CHANGED
@@ -18,7 +18,6 @@ var events = require('events');
18
18
  var vm = require('vm');
19
19
  var compression = require('xypriss-compression');
20
20
  var strulink = require('strulink');
21
- var readline = require('node:readline');
22
21
  var promises = require('node:fs/promises');
23
22
  var http = require('http');
24
23
  var stream = require('stream');
@@ -50,7 +49,6 @@ var net__namespace = /*#__PURE__*/_interopNamespaceDefault(net);
50
49
  var fs__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(fs$1);
51
50
  var path__namespace$1 = /*#__PURE__*/_interopNamespaceDefault(path$1);
52
51
  var vm__namespace = /*#__PURE__*/_interopNamespaceDefault(vm);
53
- var readline__namespace = /*#__PURE__*/_interopNamespaceDefault(readline);
54
52
 
55
53
  /**
56
54
  * Type definitions for Console Interception System
@@ -34990,6 +34988,9 @@ class XemsError extends Error {
34990
34988
  class XemsRunner {
34991
34989
  constructor(options = {}) {
34992
34990
  this.process = null;
34991
+ this.synapxClient = null;
34992
+ this.socketPath = null;
34993
+ this.initPromise = null;
34993
34994
  this.queue = [];
34994
34995
  this.isReady = false;
34995
34996
  this.options = {};
@@ -35052,9 +35053,21 @@ class XemsRunner {
35052
35053
  // Remove the close listener before killing to prevent auto-respawn in a loop
35053
35054
  this.process.removeAllListeners("close");
35054
35055
  this.process.kill();
35056
+ this.process = null;
35057
+ }
35058
+ if (this.synapxClient) {
35059
+ try {
35060
+ this.synapxClient.destroy();
35061
+ }
35062
+ catch { }
35063
+ this.synapxClient = null;
35055
35064
  }
35065
+ this.isReady = false;
35066
+ this.initPromise = null;
35056
35067
  // Re-init immediately
35057
- this.init();
35068
+ this.init().catch((err) => {
35069
+ this.logger.error("xems", `Failed to restart XEMS with persistence: ${err}`);
35070
+ });
35058
35071
  }
35059
35072
  /**
35060
35073
  * Finds the XEMS binary location.
@@ -35120,90 +35133,167 @@ class XemsRunner {
35120
35133
  // 5. Global fallback to PATH (Binary must be globally available)
35121
35134
  return binName;
35122
35135
  }
35123
- init() {
35124
- if (!fs.existsSync(this.binaryPath) &&
35125
- !this.binaryPath.endsWith("xems")) {
35126
- this.logger.error("xems", `Critical: Binary not found at ${this.binaryPath}`);
35127
- return; // Cannot spawn
35136
+ async init() {
35137
+ if (this.initPromise) {
35138
+ return this.initPromise;
35128
35139
  }
35129
- try {
35130
- const args = [];
35131
- if (this.options.persistPath) {
35132
- args.push("--persist", this.options.persistPath);
35133
- }
35134
- if (this.options.secret) {
35135
- args.push("--secret", this.options.secret);
35136
- }
35137
- if (this.options.cacheSize) {
35138
- args.push("--cache-size", this.options.cacheSize.toString());
35140
+ this.initPromise = (async () => {
35141
+ if (!fs.existsSync(this.binaryPath) &&
35142
+ !this.binaryPath.endsWith("xems")) {
35143
+ this.logger.error("xems", `Critical: Binary not found at ${this.binaryPath}`);
35144
+ return;
35139
35145
  }
35140
- this.process = node_child_process.spawn(this.binaryPath, args);
35141
- const rl = readline__namespace.createInterface({
35142
- input: this.process.stdout,
35143
- terminal: false,
35144
- });
35145
- rl.on("line", (line) => {
35146
- if (line.trim()) {
35147
- this.handleResponse(line);
35146
+ try {
35147
+ // Determine isolated session directory (inherits from libXESS confinement environment)
35148
+ const sessionDir = process.env.XYPRISS_USER_TMP ||
35149
+ process.env.XESS_SESSION_TMP ||
35150
+ path.resolve(process.cwd(), ".private");
35151
+ if (!fs.existsSync(sessionDir)) {
35152
+ try {
35153
+ fs.mkdirSync(sessionDir, { recursive: true });
35154
+ }
35155
+ catch { }
35148
35156
  }
35149
- });
35150
- this.process.stderr?.on("data", (data) => {
35151
- this.logger.error("xems", `[XEMS Log] ${data}`);
35152
- });
35153
- this.process.on("close", (code) => {
35154
- this.isReady = false;
35155
- if (code !== 0 && code !== null) {
35156
- this.logger.warn("xems", `Process exited unexpectedly with code ${code}. Restarting in 2s...`);
35157
- setTimeout(() => this.init(), 2000);
35157
+ // Dedicated private socket inside the mode 0700 session folder
35158
+ const hash = process.env.XYPRISS_SESSION_HASH || String(process.pid);
35159
+ this.socketPath = path.join(sessionDir, `xems_${hash}.sock`);
35160
+ // Clean stale socket if it exists
35161
+ if (fs.existsSync(this.socketPath)) {
35162
+ try {
35163
+ fs.unlinkSync(this.socketPath);
35164
+ }
35165
+ catch { }
35158
35166
  }
35159
- });
35160
- this.isReady = true;
35161
- }
35162
- catch (e) {
35163
- this.logger.error("xems", `Failed to spawn process ${e}`);
35164
- }
35165
- }
35166
- handleResponse(jsonString) {
35167
- if (this.queue.length > 0) {
35168
- const resolver = this.queue.shift();
35169
- try {
35170
- const response = JSON.parse(jsonString);
35171
- if (response.status === "error") {
35172
- // We resolve even on error to let the caller handle it via the response object,
35173
- // or we could reject. For XyPriss style, let's just return the response object.
35174
- resolver && resolver(response, null);
35167
+ const args = ["--socket", this.socketPath];
35168
+ if (this.options.persistPath) {
35169
+ args.push("--persist", this.options.persistPath);
35175
35170
  }
35176
- else {
35177
- resolver && resolver(response, null);
35171
+ // CRITICAL SECURITY REMEDIATION:
35172
+ // NEVER pass this.options.secret in CLI args!
35173
+ // It is transmitted exclusively over the authenticated libSynapx private IPC stream.
35174
+ if (this.options.cacheSize) {
35175
+ args.push("--cache-size", this.options.cacheSize.toString());
35178
35176
  }
35177
+ this.process = node_child_process.spawn(this.binaryPath, args, {
35178
+ stdio: ["ignore", "pipe", "pipe"],
35179
+ });
35180
+ this.process.stderr?.on("data", (data) => {
35181
+ this.logger.error("xems", `[XEMS Log] ${data}`);
35182
+ });
35183
+ this.process.on("close", (code) => {
35184
+ this.isReady = false;
35185
+ if (this.synapxClient) {
35186
+ try {
35187
+ this.synapxClient.destroy();
35188
+ }
35189
+ catch { }
35190
+ this.synapxClient = null;
35191
+ }
35192
+ this.initPromise = null;
35193
+ if (code !== 0 && code !== null) {
35194
+ this.logger.warn("xems", `Process exited unexpectedly with code ${code}. Restarting in 2s...`);
35195
+ setTimeout(() => this.init(), 2000);
35196
+ }
35197
+ });
35198
+ // Wait for socket to become active (up to 3 seconds)
35199
+ let connected = false;
35200
+ for (let i = 0; i < 30; i++) {
35201
+ if (fs.existsSync(this.socketPath)) {
35202
+ connected = true;
35203
+ break;
35204
+ }
35205
+ await new Promise((r) => setTimeout(r, 100));
35206
+ }
35207
+ if (!connected) {
35208
+ throw new Error(`Timeout waiting for XEMS IPC socket at ${this.socketPath}`);
35209
+ }
35210
+ // Connect SynapxClient to xems.sock
35211
+ this.synapxClient = new SynapxClient(this.socketPath);
35212
+ await this.synapxClient.connect();
35213
+ // Zero-Trust Handshake: Transmit vault secret over private IPC stream
35214
+ if (this.options.secret || this.options.persistPath) {
35215
+ await this.synapxClient.call("xems", "init", [], {
35216
+ secret: this.options.secret || "",
35217
+ persistPath: this.options.persistPath || "",
35218
+ });
35219
+ }
35220
+ this.isReady = true;
35179
35221
  }
35180
35222
  catch (e) {
35181
- this.logger.error("xems", `Failed to parse response ${e}`);
35223
+ this.logger.error("xems", `Failed to spawn/initialize XEMS process: ${e}`);
35224
+ this.initPromise = null;
35182
35225
  }
35183
- }
35226
+ })();
35227
+ return this.initPromise;
35184
35228
  }
35185
35229
  async execute(cmd) {
35186
- // Auto-initialize if process not started
35187
- if (!this.process) {
35188
- this.init();
35230
+ if (!this.process || !this.synapxClient) {
35231
+ await this.init();
35189
35232
  }
35190
- // Wait a bit if not ready (startup grace period)
35191
35233
  if (!this.isReady) {
35192
- for (let i = 0; i < 10; i++) {
35234
+ for (let i = 0; i < 30; i++) {
35193
35235
  if (this.isReady)
35194
35236
  break;
35195
35237
  await new Promise((r) => setTimeout(r, 100));
35196
35238
  }
35197
35239
  }
35198
- return new Promise((resolve, reject) => {
35199
- if (!this.isReady || !this.process || !this.process.stdin) {
35200
- reject(new XemsError(cmd.action, "XEMS not ready (process dead or initializing)"));
35201
- return;
35240
+ if (!this.isReady || !this.synapxClient) {
35241
+ throw new XemsError(cmd.action, "XEMS not ready (process dead or initializing)");
35242
+ }
35243
+ try {
35244
+ switch (cmd.action) {
35245
+ case "ping": {
35246
+ const res = await this.synapxClient.call("xems", "ping");
35247
+ return { status: "ok", data: typeof res === "object" ? res.data : res };
35248
+ }
35249
+ case "set": {
35250
+ const args = [cmd.sandbox || "default", cmd.key || "", cmd.value || ""];
35251
+ const opts = {};
35252
+ if (cmd.ttl)
35253
+ opts.ttl = cmd.ttl;
35254
+ const res = await this.synapxClient.call("xems", "set", args, opts);
35255
+ return { status: "ok", data: typeof res === "object" ? res.data : res };
35256
+ }
35257
+ case "get": {
35258
+ const args = [cmd.sandbox || "default", cmd.key || ""];
35259
+ const opts = {};
35260
+ if (cmd.rotate)
35261
+ opts.rotate = true;
35262
+ if (cmd.grace_period)
35263
+ opts.gracePeriod = cmd.grace_period;
35264
+ if (cmd.ttl)
35265
+ opts.ttl = cmd.ttl;
35266
+ const res = await this.synapxClient.call("xems", "get", args, opts);
35267
+ if (typeof res === "object" && res !== null) {
35268
+ return {
35269
+ status: "ok",
35270
+ data: res.data !== undefined ? res.data : res.value,
35271
+ new_token: res.new_token || res.newToken,
35272
+ };
35273
+ }
35274
+ return { status: "ok", data: res };
35275
+ }
35276
+ case "del": {
35277
+ const args = [cmd.sandbox || "default", cmd.key || ""];
35278
+ const res = await this.synapxClient.call("xems", "del", args);
35279
+ return { status: "ok", data: typeof res === "object" ? res.data : res };
35280
+ }
35281
+ default: {
35282
+ const res = await this.synapxClient.call("xems", cmd.action, [
35283
+ cmd.sandbox || "",
35284
+ cmd.key || "",
35285
+ cmd.value || "",
35286
+ ]);
35287
+ return { status: "ok", data: typeof res === "object" ? res.data : res };
35288
+ }
35202
35289
  }
35203
- this.queue.push((res, _err) => resolve(res));
35204
- const payload = JSON.stringify(cmd) + "\n";
35205
- this.process.stdin.write(payload);
35206
- });
35290
+ }
35291
+ catch (err) {
35292
+ return {
35293
+ status: "error",
35294
+ error: err?.message || String(err),
35295
+ };
35296
+ }
35207
35297
  }
35208
35298
  // --- Public API Sugar ---
35209
35299
  async ping() {
@@ -35318,12 +35408,26 @@ class XemsRunner {
35318
35408
  * Forcefully stops the XEMS process and cleans up resources.
35319
35409
  */
35320
35410
  destroy() {
35411
+ if (this.synapxClient) {
35412
+ try {
35413
+ this.synapxClient.destroy();
35414
+ }
35415
+ catch { }
35416
+ this.synapxClient = null;
35417
+ }
35321
35418
  if (this.process) {
35322
35419
  this.process.removeAllListeners("close");
35323
35420
  this.process.kill();
35324
35421
  this.process = null;
35325
- this.isReady = false;
35326
35422
  }
35423
+ if (this.socketPath && fs.existsSync(this.socketPath)) {
35424
+ try {
35425
+ fs.unlinkSync(this.socketPath);
35426
+ }
35427
+ catch { }
35428
+ }
35429
+ this.isReady = false;
35430
+ this.initPromise = null;
35327
35431
  }
35328
35432
  }
35329
35433
  /**