xmemory 3.8.1 → 3.8.2

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.
@@ -8,6 +8,24 @@ exports.xmemoryInstance = xmemoryInstance;
8
8
  const instance_js_1 = require("./instance.js");
9
9
  const types_js_1 = require("./types.js");
10
10
  const DEFAULT_BASE_URL = "https://api.xmemory.ai";
11
+ /**
12
+ * An *own* property of a decoded response, or `undefined`.
13
+ *
14
+ * A decoded body is a plain object, so a field the server omitted is answered by
15
+ * `Object.prototype`. Reading the envelope that way let a polluted prototype supply
16
+ * an `items` array — and a 200 with an empty body then came back as a genuine
17
+ * result. Every read of a server-controlled envelope goes through here.
18
+ */
19
+ function own(source, key) {
20
+ return typeof source === "object" && source !== null && Object.hasOwn(source, key)
21
+ ? source[key]
22
+ : undefined;
23
+ }
24
+ /** An own array field, or an empty one. Never the prototype's. */
25
+ function ownArray(source, key) {
26
+ const value = own(source, key);
27
+ return Array.isArray(value) ? value : [];
28
+ }
11
29
  const DEFAULT_TIMEOUT_MS = 60_000;
12
30
  const ORANGE = "\x1b[38;5;208m";
13
31
  const RESET = "\x1b[0m";
@@ -31,7 +49,7 @@ function extractStructuredError(payload) {
31
49
  details: (p.details ?? null),
32
50
  };
33
51
  }
34
- const errors = p.errors;
52
+ const errors = own(p, "errors");
35
53
  if (Array.isArray(errors) && errors.length > 0 && typeof errors[0] === "object" && errors[0] !== null) {
36
54
  const first = errors[0];
37
55
  return {
@@ -182,15 +200,16 @@ class XmemoryClient {
182
200
  throw new types_js_1.XmemoryAPIError(`HTTP ${res.status}: ${msg}`, res.status, structured.code, structured.details, parseRetryAfter(res));
183
201
  }
184
202
  const response = payload;
185
- if (response.errors?.length) {
186
- const first = response.errors[0];
203
+ const responseErrors = ownArray(response, "errors");
204
+ if (responseErrors.length > 0) {
205
+ const first = responseErrors[0];
187
206
  throw new types_js_1.XmemoryAPIError(`API error: ${first.message} (${first.code})`, res.status, first.code);
188
207
  }
189
208
  return response;
190
209
  }
191
210
  async _requestOne(method, path, options) {
192
211
  const response = await this._request(method, path, options);
193
- const items = response.items ?? [];
212
+ const items = ownArray(response, "items");
194
213
  if (items.length === 0) {
195
214
  throw new types_js_1.XmemoryAPIError(`Expected one item from ${method} ${path}, got none`);
196
215
  }
@@ -201,11 +220,11 @@ class XmemoryClient {
201
220
  }
202
221
  async _requestList(method, path, options) {
203
222
  const response = await this._request(method, path, options);
204
- return (response.items ?? []);
223
+ return ownArray(response, "items");
205
224
  }
206
225
  async _requestIds(method, path, options) {
207
226
  const response = await this._request(method, path, options);
208
- return response.ids ?? [];
227
+ return ownArray(response, "ids");
209
228
  }
210
229
  // -----------------------------------------------------------------------
211
230
  // Private: build admin namespace
@@ -4,6 +4,24 @@
4
4
  import { InstanceHandle } from "./instance.js";
5
5
  import { SetupFormat, XmemoryAPIError, XmemoryHealthCheckError, buildInstanceSchema, } from "./types.js";
6
6
  const DEFAULT_BASE_URL = "https://api.xmemory.ai";
7
+ /**
8
+ * An *own* property of a decoded response, or `undefined`.
9
+ *
10
+ * A decoded body is a plain object, so a field the server omitted is answered by
11
+ * `Object.prototype`. Reading the envelope that way let a polluted prototype supply
12
+ * an `items` array — and a 200 with an empty body then came back as a genuine
13
+ * result. Every read of a server-controlled envelope goes through here.
14
+ */
15
+ function own(source, key) {
16
+ return typeof source === "object" && source !== null && Object.hasOwn(source, key)
17
+ ? source[key]
18
+ : undefined;
19
+ }
20
+ /** An own array field, or an empty one. Never the prototype's. */
21
+ function ownArray(source, key) {
22
+ const value = own(source, key);
23
+ return Array.isArray(value) ? value : [];
24
+ }
7
25
  const DEFAULT_TIMEOUT_MS = 60_000;
8
26
  const ORANGE = "\x1b[38;5;208m";
9
27
  const RESET = "\x1b[0m";
@@ -27,7 +45,7 @@ function extractStructuredError(payload) {
27
45
  details: (p.details ?? null),
28
46
  };
29
47
  }
30
- const errors = p.errors;
48
+ const errors = own(p, "errors");
31
49
  if (Array.isArray(errors) && errors.length > 0 && typeof errors[0] === "object" && errors[0] !== null) {
32
50
  const first = errors[0];
33
51
  return {
@@ -178,15 +196,16 @@ export class XmemoryClient {
178
196
  throw new XmemoryAPIError(`HTTP ${res.status}: ${msg}`, res.status, structured.code, structured.details, parseRetryAfter(res));
179
197
  }
180
198
  const response = payload;
181
- if (response.errors?.length) {
182
- const first = response.errors[0];
199
+ const responseErrors = ownArray(response, "errors");
200
+ if (responseErrors.length > 0) {
201
+ const first = responseErrors[0];
183
202
  throw new XmemoryAPIError(`API error: ${first.message} (${first.code})`, res.status, first.code);
184
203
  }
185
204
  return response;
186
205
  }
187
206
  async _requestOne(method, path, options) {
188
207
  const response = await this._request(method, path, options);
189
- const items = response.items ?? [];
208
+ const items = ownArray(response, "items");
190
209
  if (items.length === 0) {
191
210
  throw new XmemoryAPIError(`Expected one item from ${method} ${path}, got none`);
192
211
  }
@@ -197,11 +216,11 @@ export class XmemoryClient {
197
216
  }
198
217
  async _requestList(method, path, options) {
199
218
  const response = await this._request(method, path, options);
200
- return (response.items ?? []);
219
+ return ownArray(response, "items");
201
220
  }
202
221
  async _requestIds(method, path, options) {
203
222
  const response = await this._request(method, path, options);
204
- return response.ids ?? [];
223
+ return ownArray(response, "ids");
205
224
  }
206
225
  // -----------------------------------------------------------------------
207
226
  // Private: build admin namespace
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xmemory",
3
- "version": "3.8.1",
3
+ "version": "3.8.2",
4
4
  "description": "xmemory",
5
5
  "keywords": [
6
6
  "xmemory"