@manta-eu/lib 0.1.6 → 0.1.7
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/README.md +1 -1
- package/dist/backoff.d.ts +8 -0
- package/dist/backoff.d.ts.map +1 -0
- package/dist/backoff.js +6 -0
- package/dist/connection-string.d.ts +15 -0
- package/dist/connection-string.d.ts.map +1 -0
- package/dist/connection-string.js +71 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@ import { humanBytes, humanDuration } from "@manta-eu/lib/humanize";
|
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Modules include `array`, `date`, `number`, `color`, `file`, `enum`, `humanize`, `present`,
|
|
22
|
-
`countries
|
|
22
|
+
`countries`, `ad-platform-links` and `connection-string`. `present` carries `isPresent`, which is how this repo checks for
|
|
23
23
|
a value rather than testing `.length` or truthiness.
|
|
24
24
|
|
|
25
25
|
This package exists so `@manta-eu/client`, `@manta-eu/react` and `@manta-eu/rdf-lib` share one copy
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backoff.d.ts","sourceRoot":"","sources":["../src/backoff.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAEhB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAE,MAAM,MAAoB,GAAG,MAAM,CAK5G"}
|
package/dist/backoff.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export function backoffDelayMs(attempt, backoff, random = Math.random) {
|
|
2
|
+
const { initialMs, maxMs, factor = 2, jitterRatio = 0 } = backoff;
|
|
3
|
+
const exponential = initialMs * factor ** Math.max(0, attempt);
|
|
4
|
+
const jitter = random() * exponential * jitterRatio;
|
|
5
|
+
return Math.min(maxMs, exponential + jitter);
|
|
6
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type ConnectionString = {
|
|
2
|
+
scheme: string;
|
|
3
|
+
host?: string;
|
|
4
|
+
port?: string;
|
|
5
|
+
username?: string;
|
|
6
|
+
password?: string;
|
|
7
|
+
path: string[];
|
|
8
|
+
params: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
export type ParseOptions = {
|
|
11
|
+
semicolonProperties?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export declare function parseConnectionString(input: string, options?: ParseOptions): ConnectionString | undefined;
|
|
14
|
+
export declare function unescapeBracedValue(value: string): string;
|
|
15
|
+
//# sourceMappingURL=connection-string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection-string.d.ts","sourceRoot":"","sources":["../src/connection-string.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IAEzB,mBAAmB,CAAC,EAAE,OAAO,CAAC;CAC/B,CAAC;AAEF,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,GAAG,gBAAgB,GAAG,SAAS,CAoB7G;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAGzD"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { isPresent } from "./present.js";
|
|
2
|
+
export function parseConnectionString(input, options = {}) {
|
|
3
|
+
const { semicolonProperties = false } = options;
|
|
4
|
+
const schemeAndRest = splitScheme(stripJdbcPrefix(input.trim()));
|
|
5
|
+
if (!schemeAndRest)
|
|
6
|
+
return undefined;
|
|
7
|
+
const { scheme, rest } = schemeAndRest;
|
|
8
|
+
const [head, properties] = semicolonProperties ? splitHead(rest) : [rest, ""];
|
|
9
|
+
if (head.includes("\\"))
|
|
10
|
+
return undefined;
|
|
11
|
+
try {
|
|
12
|
+
const { path, query, ...authority } = headParts(scheme, head, semicolonProperties);
|
|
13
|
+
return {
|
|
14
|
+
scheme,
|
|
15
|
+
...withoutBlankValues(authority),
|
|
16
|
+
path,
|
|
17
|
+
params: Object.fromEntries([...query, ...propertyEntries(properties)]),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export function unescapeBracedValue(value) {
|
|
25
|
+
const braced = /^\{(.*)\}$/s.exec(value);
|
|
26
|
+
return braced?.[1] === undefined ? value : braced[1].replaceAll("}}", "}");
|
|
27
|
+
}
|
|
28
|
+
function stripJdbcPrefix(input) {
|
|
29
|
+
return input.replace(/^jdbc:/i, "");
|
|
30
|
+
}
|
|
31
|
+
const schemePattern = /^([a-z][a-z0-9+.-]*):\/\//i;
|
|
32
|
+
function splitScheme(text) {
|
|
33
|
+
const match = schemePattern.exec(text);
|
|
34
|
+
if (!match?.[1])
|
|
35
|
+
return undefined;
|
|
36
|
+
return { scheme: match[1].toLowerCase(), rest: text.slice(match[0].length) };
|
|
37
|
+
}
|
|
38
|
+
function splitHead(text) {
|
|
39
|
+
const [head = "", ...properties] = text.split(";");
|
|
40
|
+
return [head, properties.join(";")];
|
|
41
|
+
}
|
|
42
|
+
// The BigQuery JDBC head is an endpoint URL, so a head with its own scheme carries no host.
|
|
43
|
+
function headParts(scheme, head, semicolonProperties) {
|
|
44
|
+
if (!isPresent(head) || (semicolonProperties && head.includes("://")))
|
|
45
|
+
return { path: [], query: [] };
|
|
46
|
+
return urlParts(scheme, head);
|
|
47
|
+
}
|
|
48
|
+
function urlParts(scheme, head) {
|
|
49
|
+
const url = new URL(`${scheme}://${head}`);
|
|
50
|
+
return {
|
|
51
|
+
host: url.hostname.replace(/^\[(.*)\]$/, "$1"),
|
|
52
|
+
port: url.port,
|
|
53
|
+
username: decodeURIComponent(url.username),
|
|
54
|
+
password: decodeURIComponent(url.password),
|
|
55
|
+
path: url.pathname.split("/").filter(Boolean).map(decodeURIComponent),
|
|
56
|
+
query: [...url.searchParams],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function withoutBlankValues(entries) {
|
|
60
|
+
return Object.fromEntries(Object.entries(entries).filter(([, value]) => isPresent(value)));
|
|
61
|
+
}
|
|
62
|
+
const propertyName = "([^;=]+)";
|
|
63
|
+
// The SQL Server JDBC driver escapes a value in braces, and "}}" in the braces stands for one "}".
|
|
64
|
+
const bracedValue = String.raw `\{(?:[^}]|\}\})*\}`;
|
|
65
|
+
const plainValue = "[^;]*";
|
|
66
|
+
const propertyPattern = new RegExp(String.raw `${propertyName}=\s*(${bracedValue}|${plainValue})`, "g");
|
|
67
|
+
function propertyEntries(text) {
|
|
68
|
+
return [...text.matchAll(propertyPattern)]
|
|
69
|
+
.map(([, name = "", value = ""]) => [name.trim(), value.trim()])
|
|
70
|
+
.filter(([name]) => isPresent(name));
|
|
71
|
+
}
|