@typedb/driver-http 3.12.2 → 3.12.3

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
@@ -9,10 +9,11 @@ function remoteOrigin(params) {
9
9
  if (isBasicParams(params)) return `${params.addresses[0]}`;
10
10
  else return `${params.translatedAddresses[0].external}`;
11
11
  }
12
+ var SCHEME_PREFIX_REGEX = /^[a-z][a-z0-9+.-]*:\/\//i;
12
13
  function hostPortFromOrigin(origin) {
14
+ if (!SCHEME_PREFIX_REGEX.test(origin)) return origin;
13
15
  try {
14
- const url = new URL(origin);
15
- return url.host;
16
+ return new URL(origin).host;
16
17
  } catch (e) {
17
18
  return origin;
18
19
  }
@@ -23,14 +24,23 @@ function resolveOrigin(params, primaryAddress) {
23
24
  for (const addr of params.addresses) {
24
25
  if (hostPortFromOrigin(addr) === target) return addr;
25
26
  }
27
+ return originWithScheme(primaryAddress, params.addresses[0]);
26
28
  } else {
27
29
  for (const ta of params.translatedAddresses) {
28
30
  if (hostPortFromOrigin(ta.internal) === target || hostPortFromOrigin(ta.external) === target) {
29
31
  return ta.external;
30
32
  }
31
33
  }
34
+ return originWithScheme(primaryAddress, params.translatedAddresses[0].external);
35
+ }
36
+ }
37
+ function originWithScheme(address, referenceOrigin) {
38
+ if (SCHEME_PREFIX_REGEX.test(address)) return address;
39
+ try {
40
+ return `${new URL(referenceOrigin).protocol}//${address}`;
41
+ } catch (e2) {
42
+ return `https://${address}`;
32
43
  }
33
- return primaryAddress;
34
44
  }
35
45
  function allOrigins(params) {
36
46
  if (isBasicParams(params)) return params.addresses;
@@ -190,6 +200,7 @@ var TypeDBHttpDriver = class {
190
200
  return result;
191
201
  }
192
202
  async apiReqOnce(method, path, body, options) {
203
+ const triedOrigins = /* @__PURE__ */ new Set([this.currentOrigin]);
193
204
  const initial = await this.tryApiReq(method, path, body, options);
194
205
  if (isApiErrResp(initial)) return initial;
195
206
  let result = initial;
@@ -200,7 +211,8 @@ var TypeDBHttpDriver = class {
200
211
  }
201
212
  if (result === null || !result.ok) {
202
213
  const startOrigin = this.currentOrigin;
203
- const fallback = await this.tryFallbackOrigins(method, path, body, options);
214
+ triedOrigins.add(startOrigin);
215
+ const fallback = await this.tryFallbackOrigins(method, path, body, options, triedOrigins);
204
216
  if (isApiErrResp(fallback)) return fallback;
205
217
  if (fallback !== null && (result === null || fallback.ok)) {
206
218
  result = fallback;
@@ -219,7 +231,7 @@ var TypeDBHttpDriver = class {
219
231
  let resp;
220
232
  try {
221
233
  resp = await fetch(url, { method, body: bodyString, headers });
222
- } catch (e2) {
234
+ } catch (e3) {
223
235
  return null;
224
236
  }
225
237
  if (resp.status === HTTP_UNAUTHORIZED) {
@@ -228,7 +240,7 @@ var TypeDBHttpDriver = class {
228
240
  headers = this.authHeaders(tokenResp.ok.token, options);
229
241
  try {
230
242
  resp = await fetch(url, { method, body: bodyString, headers });
231
- } catch (e3) {
243
+ } catch (e4) {
232
244
  return null;
233
245
  }
234
246
  }
@@ -244,7 +256,7 @@ var TypeDBHttpDriver = class {
244
256
  let json;
245
257
  try {
246
258
  json = await resp.json();
247
- } catch (e4) {
259
+ } catch (e5) {
248
260
  return false;
249
261
  }
250
262
  if (!isMisdirectedError(json)) return false;
@@ -253,12 +265,11 @@ var TypeDBHttpDriver = class {
253
265
  this.switchOrigin(newOrigin);
254
266
  return true;
255
267
  }
256
- async tryFallbackOrigins(method, path, body, options) {
268
+ async tryFallbackOrigins(method, path, body, options, triedOrigins) {
257
269
  const origins = allOrigins(this.params);
258
- const failedOrigin = this.currentOrigin;
259
270
  let lastError = null;
260
271
  for (const origin of origins) {
261
- if (origin === failedOrigin) continue;
272
+ if (triedOrigins.has(origin)) continue;
262
273
  this.switchOrigin(origin);
263
274
  const initial = await this.tryApiReq(method, path, body, options);
264
275
  if (initial === null) continue;
@@ -297,7 +308,7 @@ var TypeDBHttpDriver = class {
297
308
  let resp;
298
309
  try {
299
310
  resp = await fetch(url, { method: "POST", body: JSON.stringify(body), headers: { "Content-Type": "application/json" } });
300
- } catch (e5) {
311
+ } catch (e6) {
301
312
  return driverError("HDR2", `Cannot connect to server at ${this.currentOrigin}`);
302
313
  }
303
314
  const json = await this.jsonOrNull(resp);
@@ -311,7 +322,7 @@ var TypeDBHttpDriver = class {
311
322
  async jsonOrNull(resp) {
312
323
  try {
313
324
  return await resp.json();
314
- } catch (e6) {
325
+ } catch (e7) {
315
326
  return null;
316
327
  }
317
328
  }
package/dist/index.d.ts CHANGED
@@ -16,9 +16,7 @@ type DriverParams = DriverParamsBasic | DriverParamsTranslated;
16
16
  declare function isBasicParams(params: DriverParams): params is DriverParamsBasic;
17
17
  declare function isTranslatedParams(params: DriverParams): params is DriverParamsTranslated;
18
18
  declare function remoteOrigin(params: DriverParams): string;
19
- /** Extract host:port from a URL string. "https://127.0.0.1:18000" → "127.0.0.1:18000" */
20
19
  declare function hostPortFromOrigin(origin: string): string;
21
- /** Resolve an advertised primaryAddress (a full origin) to the matching configured address, else itself. */
22
20
  declare function resolveOrigin(params: DriverParams, primaryAddress: string): string;
23
21
  /** Return all configured origins for connection error fallback. */
24
22
  declare function allOrigins(params: DriverParams): string[];
package/dist/index.mjs CHANGED
@@ -9,10 +9,11 @@ function remoteOrigin(params) {
9
9
  if (isBasicParams(params)) return `${params.addresses[0]}`;
10
10
  else return `${params.translatedAddresses[0].external}`;
11
11
  }
12
+ var SCHEME_PREFIX_REGEX = /^[a-z][a-z0-9+.-]*:\/\//i;
12
13
  function hostPortFromOrigin(origin) {
14
+ if (!SCHEME_PREFIX_REGEX.test(origin)) return origin;
13
15
  try {
14
- const url = new URL(origin);
15
- return url.host;
16
+ return new URL(origin).host;
16
17
  } catch {
17
18
  return origin;
18
19
  }
@@ -23,14 +24,23 @@ function resolveOrigin(params, primaryAddress) {
23
24
  for (const addr of params.addresses) {
24
25
  if (hostPortFromOrigin(addr) === target) return addr;
25
26
  }
27
+ return originWithScheme(primaryAddress, params.addresses[0]);
26
28
  } else {
27
29
  for (const ta of params.translatedAddresses) {
28
30
  if (hostPortFromOrigin(ta.internal) === target || hostPortFromOrigin(ta.external) === target) {
29
31
  return ta.external;
30
32
  }
31
33
  }
34
+ return originWithScheme(primaryAddress, params.translatedAddresses[0].external);
35
+ }
36
+ }
37
+ function originWithScheme(address, referenceOrigin) {
38
+ if (SCHEME_PREFIX_REGEX.test(address)) return address;
39
+ try {
40
+ return `${new URL(referenceOrigin).protocol}//${address}`;
41
+ } catch {
42
+ return `https://${address}`;
32
43
  }
33
- return primaryAddress;
34
44
  }
35
45
  function allOrigins(params) {
36
46
  if (isBasicParams(params)) return params.addresses;
@@ -190,6 +200,7 @@ var TypeDBHttpDriver = class {
190
200
  return result;
191
201
  }
192
202
  async apiReqOnce(method, path, body, options) {
203
+ const triedOrigins = /* @__PURE__ */ new Set([this.currentOrigin]);
193
204
  const initial = await this.tryApiReq(method, path, body, options);
194
205
  if (isApiErrResp(initial)) return initial;
195
206
  let result = initial;
@@ -200,7 +211,8 @@ var TypeDBHttpDriver = class {
200
211
  }
201
212
  if (result === null || !result.ok) {
202
213
  const startOrigin = this.currentOrigin;
203
- const fallback = await this.tryFallbackOrigins(method, path, body, options);
214
+ triedOrigins.add(startOrigin);
215
+ const fallback = await this.tryFallbackOrigins(method, path, body, options, triedOrigins);
204
216
  if (isApiErrResp(fallback)) return fallback;
205
217
  if (fallback !== null && (result === null || fallback.ok)) {
206
218
  result = fallback;
@@ -253,12 +265,11 @@ var TypeDBHttpDriver = class {
253
265
  this.switchOrigin(newOrigin);
254
266
  return true;
255
267
  }
256
- async tryFallbackOrigins(method, path, body, options) {
268
+ async tryFallbackOrigins(method, path, body, options, triedOrigins) {
257
269
  const origins = allOrigins(this.params);
258
- const failedOrigin = this.currentOrigin;
259
270
  let lastError = null;
260
271
  for (const origin of origins) {
261
- if (origin === failedOrigin) continue;
272
+ if (triedOrigins.has(origin)) continue;
262
273
  this.switchOrigin(origin);
263
274
  const initial = await this.tryApiReq(method, path, body, options);
264
275
  if (initial === null) continue;
package/package.json CHANGED
@@ -1 +1 @@
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"}}
1
+ {"name": "@typedb/driver-http", "version": "3.12.3", "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"}}