@drakkar.software/starfish-client 3.0.0-alpha.4 → 3.0.0-alpha.6

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/index.js CHANGED
@@ -39,6 +39,7 @@ function encodeCapAuth(cap) {
39
39
  }
40
40
  var StarfishClient = class {
41
41
  baseUrl;
42
+ namespace;
42
43
  capProvider;
43
44
  fetch;
44
45
  /**
@@ -48,6 +49,7 @@ var StarfishClient = class {
48
49
  plugins;
49
50
  constructor(options) {
50
51
  this.baseUrl = options.baseUrl.replace(/\/$/, "");
52
+ this.namespace = options.namespace || void 0;
51
53
  this.capProvider = options.capProvider;
52
54
  this.fetch = options.fetch ?? globalThis.fetch.bind(globalThis);
53
55
  this.plugins = options.plugins ? [...options.plugins] : [];
@@ -71,6 +73,20 @@ var StarfishClient = class {
71
73
  return "";
72
74
  }
73
75
  }
76
+ /**
77
+ * Rewrite a request path for the configured namespace. A no-op when no
78
+ * namespace is set; otherwise `/{action}/…` becomes `/v1/{namespace}/{action}/…`
79
+ * (the `/v1` protocol-version segment is part of the namespaced route, matching
80
+ * the Python client and the server's namespace mount).
81
+ *
82
+ * Applied to the path used for BOTH the signature and the URL so the canonical
83
+ * path the client signs equals the path the server reconstructs from the URL.
84
+ * Covers SDK-helper-built paths too — that's the point: a namespace-unaware
85
+ * helper passing `/push/spaces/x/_keyring` reaches `/v1/{ns}/push/spaces/x/_keyring`.
86
+ */
87
+ applyNamespace(path) {
88
+ return this.namespace ? `/v1/${this.namespace}${path}` : path;
89
+ }
74
90
  /**
75
91
  * Build auth headers for a request. When a `capProvider` is set, signs the
76
92
  * request with the device's Ed25519 private key and returns the v3 header
@@ -107,7 +123,7 @@ var StarfishClient = class {
107
123
  return {};
108
124
  }
109
125
  async pull(path, checkpointOrOptions) {
110
- let pathAndQuery = path;
126
+ let pathAndQuery = this.applyNamespace(path);
111
127
  let appendField;
112
128
  if (typeof checkpointOrOptions === "number") {
113
129
  if (checkpointOrOptions) pathAndQuery += `?checkpoint=${checkpointOrOptions}`;
@@ -166,8 +182,9 @@ var StarfishClient = class {
166
182
  data,
167
183
  baseHash
168
184
  });
169
- const authHeaders = await this.buildAuthHeaders("POST", path, body);
170
- const res = await this.fetch(`${this.baseUrl}${path}`, {
185
+ const sendPath = this.applyNamespace(path);
186
+ const authHeaders = await this.buildAuthHeaders("POST", sendPath, body);
187
+ const res = await this.fetch(`${this.baseUrl}${sendPath}`, {
171
188
  method: "POST",
172
189
  headers: {
173
190
  "Content-Type": "application/json",
@@ -198,15 +215,18 @@ var StarfishClient = class {
198
215
  * @param opts.ts - optional client-supplied element timestamp (ms). Must be a
199
216
  * non-negative integer strictly greater than the latest stored element's ts
200
217
  * (else the server responds 409). Omit to let the server assign one.
201
- * @throws {StarfishHttpError} on a non-2xx response (e.g. 409 for a
202
- * non-monotonic timestamp).
218
+ * @throws {StarfishHttpError} on a non-2xx response e.g. 409
219
+ * `{ error: "non_monotonic_timestamp" }` for a non-monotonic timestamp, or
220
+ * `{ error: "append_limit_exceeded", limit }` if the collection's `maxItems`
221
+ * cap is reached (partition by a path parameter for higher volume).
203
222
  */
204
223
  async append(path, data, opts = {}) {
205
224
  const bodyObj = { data };
206
225
  if (opts.ts !== void 0) bodyObj["ts"] = opts.ts;
207
226
  const body = JSON.stringify(bodyObj);
208
- const authHeaders = await this.buildAuthHeaders("POST", path, body);
209
- const res = await this.fetch(`${this.baseUrl}${path}`, {
227
+ const sendPath = this.applyNamespace(path);
228
+ const authHeaders = await this.buildAuthHeaders("POST", sendPath, body);
229
+ const res = await this.fetch(`${this.baseUrl}${sendPath}`, {
210
230
  method: "POST",
211
231
  headers: {
212
232
  "Content-Type": "application/json",
@@ -225,8 +245,9 @@ var StarfishClient = class {
225
245
  * Returns raw bytes with the content hash from the ETag header.
226
246
  */
227
247
  async pullBlob(path) {
228
- const authHeaders = await this.buildAuthHeaders("GET", path, void 0);
229
- const res = await this.fetch(`${this.baseUrl}${path}`, {
248
+ const sendPath = this.applyNamespace(path);
249
+ const authHeaders = await this.buildAuthHeaders("GET", sendPath, void 0);
250
+ const res = await this.fetch(`${this.baseUrl}${sendPath}`, {
230
251
  method: "GET",
231
252
  headers: { Accept: "*/*", ...authHeaders }
232
253
  });
@@ -243,8 +264,9 @@ var StarfishClient = class {
243
264
  * Binary collections use last-write-wins (no conflict detection).
244
265
  */
245
266
  async pushBlob(path, data, contentType) {
246
- const authHeaders = await this.buildAuthHeaders("POST", path, void 0);
247
- const res = await this.fetch(`${this.baseUrl}${path}`, {
267
+ const sendPath = this.applyNamespace(path);
268
+ const authHeaders = await this.buildAuthHeaders("POST", sendPath, void 0);
269
+ const res = await this.fetch(`${this.baseUrl}${sendPath}`, {
248
270
  method: "POST",
249
271
  headers: {
250
272
  "Content-Type": contentType,