@typedb/driver-http 3.12.1 → 3.12.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.
package/dist/index.cjs CHANGED
@@ -18,27 +18,19 @@ function hostPortFromOrigin(origin) {
18
18
  }
19
19
  }
20
20
  function resolveOrigin(params, primaryAddress) {
21
+ const target = hostPortFromOrigin(primaryAddress);
21
22
  if (isBasicParams(params)) {
22
23
  for (const addr of params.addresses) {
23
- if (hostPortFromOrigin(addr) === primaryAddress) return addr;
24
+ if (hostPortFromOrigin(addr) === target) return addr;
24
25
  }
25
- return deriveOriginWithScheme(params.addresses[0], primaryAddress);
26
26
  } else {
27
27
  for (const ta of params.translatedAddresses) {
28
- if (hostPortFromOrigin(ta.internal) === primaryAddress || hostPortFromOrigin(ta.external) === primaryAddress) {
28
+ if (hostPortFromOrigin(ta.internal) === target || hostPortFromOrigin(ta.external) === target) {
29
29
  return ta.external;
30
30
  }
31
31
  }
32
- return deriveOriginWithScheme(params.translatedAddresses[0].external, primaryAddress);
33
- }
34
- }
35
- function deriveOriginWithScheme(referenceUrl, hostPort) {
36
- try {
37
- const url = new URL(referenceUrl);
38
- return `${url.protocol}//${hostPort}`;
39
- } catch (e2) {
40
- return `https://${hostPort}`;
41
32
  }
33
+ return primaryAddress;
42
34
  }
43
35
  function allOrigins(params) {
44
36
  if (isBasicParams(params)) return params.addresses;
@@ -73,7 +65,9 @@ function getVariableName(structure, variable) {
73
65
  // ../../../../../../../../../execroot/_main/bazel-out/k8-opt/bin/http-ts/src/index.ts
74
66
  var HTTP_UNAUTHORIZED = 401;
75
67
  var HTTP_MISDIRECTED = 421;
76
- var TRANSIENT_SIGNIN_ERROR_CODES = /* @__PURE__ */ new Set(["HDR2", "SRV14"]);
68
+ var TRANSIENT_SIGNIN_ERROR_CODES = /* @__PURE__ */ new Set(["HDR2", "SRV14", "CSV8", "CSV9"]);
69
+ var PRIMARY_FAILOVER_RETRIES = 3;
70
+ var PRIMARY_SELECTION_WAIT_MS = 2e3;
77
71
  function isApiErrResp(value) {
78
72
  return value !== null && "err" in value;
79
73
  }
@@ -188,6 +182,14 @@ var TypeDBHttpDriver = class {
188
182
  }
189
183
  }
190
184
  async apiReq(method, path, body, options) {
185
+ let result = await this.apiReqOnce(method, path, body, options);
186
+ for (let retry = 0; retry < PRIMARY_FAILOVER_RETRIES && result.status === HTTP_MISDIRECTED; retry++) {
187
+ await new Promise((resolve) => setTimeout(resolve, PRIMARY_SELECTION_WAIT_MS));
188
+ result = await this.apiReqOnce(method, path, body, options);
189
+ }
190
+ return result;
191
+ }
192
+ async apiReqOnce(method, path, body, options) {
191
193
  const initial = await this.tryApiReq(method, path, body, options);
192
194
  if (isApiErrResp(initial)) return initial;
193
195
  let result = initial;
@@ -217,7 +219,7 @@ var TypeDBHttpDriver = class {
217
219
  let resp;
218
220
  try {
219
221
  resp = await fetch(url, { method, body: bodyString, headers });
220
- } catch (e3) {
222
+ } catch (e2) {
221
223
  return null;
222
224
  }
223
225
  if (resp.status === HTTP_UNAUTHORIZED) {
@@ -226,7 +228,7 @@ var TypeDBHttpDriver = class {
226
228
  headers = this.authHeaders(tokenResp.ok.token, options);
227
229
  try {
228
230
  resp = await fetch(url, { method, body: bodyString, headers });
229
- } catch (e4) {
231
+ } catch (e3) {
230
232
  return null;
231
233
  }
232
234
  }
@@ -242,7 +244,7 @@ var TypeDBHttpDriver = class {
242
244
  let json;
243
245
  try {
244
246
  json = await resp.json();
245
- } catch (e5) {
247
+ } catch (e4) {
246
248
  return false;
247
249
  }
248
250
  if (!isMisdirectedError(json)) return false;
@@ -295,23 +297,23 @@ var TypeDBHttpDriver = class {
295
297
  let resp;
296
298
  try {
297
299
  resp = await fetch(url, { method: "POST", body: JSON.stringify(body), headers: { "Content-Type": "application/json" } });
298
- } catch (e6) {
300
+ } catch (e5) {
299
301
  return driverError("HDR2", `Cannot connect to server at ${this.currentOrigin}`);
300
302
  }
301
303
  const json = await this.jsonOrNull(resp);
302
- if (resp.ok) {
304
+ if (resp.ok && typeof _optionalChain([json, 'optionalAccess', _5 => _5.token]) === "string") {
303
305
  this.token = json.token;
304
306
  return { ok: json };
305
307
  }
306
308
  if (isApiError(json)) return { err: json, status: resp.status };
307
- throw resp;
309
+ return driverError("HDR2", `Unexpected signin response from ${this.currentOrigin} (HTTP ${resp.status})`);
308
310
  }
309
311
  async jsonOrNull(resp) {
310
- const contentLengthRaw = resp.headers.get("Content-Length");
311
- if (!contentLengthRaw) return null;
312
- const contentLength = parseInt(contentLengthRaw || "");
313
- if (isNaN(contentLength)) throw `Received invalid Content-Length header: ${contentLengthRaw}`;
314
- return contentLength > 0 ? await resp.json() : null;
312
+ try {
313
+ return await resp.json();
314
+ } catch (e6) {
315
+ return null;
316
+ }
315
317
  }
316
318
  async stringOrNull(resp) {
317
319
  const contentLengthRaw = resp.headers.get("Content-Length");
package/dist/index.d.ts CHANGED
@@ -18,7 +18,7 @@ declare function isTranslatedParams(params: DriverParams): params is DriverParam
18
18
  declare function remoteOrigin(params: DriverParams): string;
19
19
  /** Extract host:port from a URL string. "https://127.0.0.1:18000" → "127.0.0.1:18000" */
20
20
  declare function hostPortFromOrigin(origin: string): string;
21
- /** Resolve a bare primaryAddress (host:port) to a full URL using configured params. */
21
+ /** Resolve an advertised primaryAddress (a full origin) to the matching configured address, else itself. */
22
22
  declare function resolveOrigin(params: DriverParams, primaryAddress: string): string;
23
23
  /** Return all configured origins for connection error fallback. */
24
24
  declare function allOrigins(params: DriverParams): string[];
@@ -498,6 +498,7 @@ declare class TypeDBHttpDriver {
498
498
  private jsonApiReq;
499
499
  private stringApiReq;
500
500
  private apiReq;
501
+ private apiReqOnce;
501
502
  private tryApiReq;
502
503
  private followMisdirected;
503
504
  private switchToRedirectTarget;
package/dist/index.mjs CHANGED
@@ -18,27 +18,19 @@ function hostPortFromOrigin(origin) {
18
18
  }
19
19
  }
20
20
  function resolveOrigin(params, primaryAddress) {
21
+ const target = hostPortFromOrigin(primaryAddress);
21
22
  if (isBasicParams(params)) {
22
23
  for (const addr of params.addresses) {
23
- if (hostPortFromOrigin(addr) === primaryAddress) return addr;
24
+ if (hostPortFromOrigin(addr) === target) return addr;
24
25
  }
25
- return deriveOriginWithScheme(params.addresses[0], primaryAddress);
26
26
  } else {
27
27
  for (const ta of params.translatedAddresses) {
28
- if (hostPortFromOrigin(ta.internal) === primaryAddress || hostPortFromOrigin(ta.external) === primaryAddress) {
28
+ if (hostPortFromOrigin(ta.internal) === target || hostPortFromOrigin(ta.external) === target) {
29
29
  return ta.external;
30
30
  }
31
31
  }
32
- return deriveOriginWithScheme(params.translatedAddresses[0].external, primaryAddress);
33
- }
34
- }
35
- function deriveOriginWithScheme(referenceUrl, hostPort) {
36
- try {
37
- const url = new URL(referenceUrl);
38
- return `${url.protocol}//${hostPort}`;
39
- } catch {
40
- return `https://${hostPort}`;
41
32
  }
33
+ return primaryAddress;
42
34
  }
43
35
  function allOrigins(params) {
44
36
  if (isBasicParams(params)) return params.addresses;
@@ -73,7 +65,9 @@ function getVariableName(structure, variable) {
73
65
  // ../../../../../../../../../execroot/_main/bazel-out/k8-opt/bin/http-ts/src/index.ts
74
66
  var HTTP_UNAUTHORIZED = 401;
75
67
  var HTTP_MISDIRECTED = 421;
76
- var TRANSIENT_SIGNIN_ERROR_CODES = /* @__PURE__ */ new Set(["HDR2", "SRV14"]);
68
+ var TRANSIENT_SIGNIN_ERROR_CODES = /* @__PURE__ */ new Set(["HDR2", "SRV14", "CSV8", "CSV9"]);
69
+ var PRIMARY_FAILOVER_RETRIES = 3;
70
+ var PRIMARY_SELECTION_WAIT_MS = 2e3;
77
71
  function isApiErrResp(value) {
78
72
  return value !== null && "err" in value;
79
73
  }
@@ -188,6 +182,14 @@ var TypeDBHttpDriver = class {
188
182
  }
189
183
  }
190
184
  async apiReq(method, path, body, options) {
185
+ let result = await this.apiReqOnce(method, path, body, options);
186
+ for (let retry = 0; retry < PRIMARY_FAILOVER_RETRIES && result.status === HTTP_MISDIRECTED; retry++) {
187
+ await new Promise((resolve) => setTimeout(resolve, PRIMARY_SELECTION_WAIT_MS));
188
+ result = await this.apiReqOnce(method, path, body, options);
189
+ }
190
+ return result;
191
+ }
192
+ async apiReqOnce(method, path, body, options) {
191
193
  const initial = await this.tryApiReq(method, path, body, options);
192
194
  if (isApiErrResp(initial)) return initial;
193
195
  let result = initial;
@@ -299,19 +301,19 @@ var TypeDBHttpDriver = class {
299
301
  return driverError("HDR2", `Cannot connect to server at ${this.currentOrigin}`);
300
302
  }
301
303
  const json = await this.jsonOrNull(resp);
302
- if (resp.ok) {
304
+ if (resp.ok && typeof json?.token === "string") {
303
305
  this.token = json.token;
304
306
  return { ok: json };
305
307
  }
306
308
  if (isApiError(json)) return { err: json, status: resp.status };
307
- throw resp;
309
+ return driverError("HDR2", `Unexpected signin response from ${this.currentOrigin} (HTTP ${resp.status})`);
308
310
  }
309
311
  async jsonOrNull(resp) {
310
- const contentLengthRaw = resp.headers.get("Content-Length");
311
- if (!contentLengthRaw) return null;
312
- const contentLength = parseInt(contentLengthRaw || "");
313
- if (isNaN(contentLength)) throw `Received invalid Content-Length header: ${contentLengthRaw}`;
314
- return contentLength > 0 ? await resp.json() : null;
312
+ try {
313
+ return await resp.json();
314
+ } catch {
315
+ return null;
316
+ }
315
317
  }
316
318
  async stringOrNull(resp) {
317
319
  const contentLengthRaw = resp.headers.get("Content-Length");
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name": "@typedb/driver-http", "version": "3.12.1", "description": "TypeDB HTTP Driver", "author": "TypeDB Community <community@typedb.com>", "license": "Apache-2.0", "homepage": "https://typedb.com", "repository": {"type": "git", "url": "https://github.com/typedb/typedb-driver"}, "bugs": {"url": "https://github.com/typedb/typedb-driver/issues"}, "keywords": ["database", "typedb"], "main": "dist/index.cjs", "module": "dist/index.mjs", "types": "dist/index.d.ts", "type": "module", "exports": {".": {"types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.cjs"}}, "scripts": {"build:dev": "tsc", "build": "tsup", "test": "cucumber-js", "docs": "typedoc"}, "packageManager": "pnpm@8.15.9", "packageConfig": {"access": "public"}, "devDependencies": {"@cucumber/cucumber": "9.0.0", "@types/node": "24.0.12", "tsup": "8.5.0", "typedoc": "^0.28.7", "typescript": "5.8.3"}}
1
+ {"name": "@typedb/driver-http", "version": "3.12.2", "description": "TypeDB HTTP Driver", "author": "TypeDB Community <community@typedb.com>", "license": "Apache-2.0", "homepage": "https://typedb.com", "repository": {"type": "git", "url": "https://github.com/typedb/typedb-driver"}, "bugs": {"url": "https://github.com/typedb/typedb-driver/issues"}, "keywords": ["database", "typedb"], "main": "dist/index.cjs", "module": "dist/index.mjs", "types": "dist/index.d.ts", "type": "module", "exports": {".": {"types": "./dist/index.d.ts", "import": "./dist/index.mjs", "require": "./dist/index.cjs"}}, "scripts": {"build:dev": "tsc", "build": "tsup", "test": "cucumber-js", "docs": "typedoc"}, "packageManager": "pnpm@8.15.9", "packageConfig": {"access": "public"}, "devDependencies": {"@cucumber/cucumber": "9.0.0", "@types/node": "24.0.12", "tsup": "8.5.0", "typedoc": "^0.28.7", "typescript": "5.8.3"}}