@databricks/sdk-core 0.1.0-dev.4 → 0.1.0-dev.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/clientinfo/agent.d.ts +6 -8
- package/dist/clientinfo/agent.d.ts.map +1 -1
- package/dist/clientinfo/agent.js +31 -18
- package/dist/clientinfo/agent.js.map +1 -1
- package/dist/profiles/profile.d.ts +10 -49
- package/dist/profiles/profile.d.ts.map +1 -1
- package/dist/profiles/profile.js +0 -218
- package/dist/profiles/profile.js.map +1 -1
- package/package.json +25 -14
- package/src/apierror/apierror.ts +0 -253
- package/src/apierror/codes/codes.ts +0 -189
- package/src/apierror/codes/index.ts +0 -7
- package/src/apierror/details.ts +0 -459
- package/src/apierror/index.ts +0 -24
- package/src/clientinfo/agent.ts +0 -125
- package/src/clientinfo/base.ts +0 -73
- package/src/clientinfo/clientinfo.ts +0 -129
- package/src/clientinfo/default.browser.ts +0 -24
- package/src/clientinfo/default.ts +0 -128
- package/src/clientinfo/index.browser.ts +0 -4
- package/src/clientinfo/index.ts +0 -4
- package/src/http/http.ts +0 -75
- package/src/http/index.ts +0 -8
- package/src/index.ts +0 -5
- package/src/logger/index.ts +0 -8
- package/src/logger/logger.ts +0 -99
- package/src/ops/execute.ts +0 -99
- package/src/ops/index.ts +0 -12
- package/src/ops/limiter.ts +0 -8
- package/src/ops/options.ts +0 -22
- package/src/ops/retrier.ts +0 -108
- package/src/profiles/errors.ts +0 -28
- package/src/profiles/index.browser.ts +0 -10
- package/src/profiles/index.ts +0 -15
- package/src/profiles/ini.ts +0 -126
- package/src/profiles/profile.ts +0 -467
- package/src/profiles/resolve.ts +0 -251
- package/src/profiles/secret.ts +0 -40
- package/src/wkt/fieldmask.ts +0 -89
- package/src/wkt/index.ts +0 -3
- package/src/wkt/value.ts +0 -19
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Collects information about the client and its environment into an
|
|
3
|
-
* immutable {@link ClientInfo} value.
|
|
4
|
-
*
|
|
5
|
-
* {@link ClientInfo.with} derives a new value with additional key/value
|
|
6
|
-
* segments; it never mutates the original.
|
|
7
|
-
*
|
|
8
|
-
* @module
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
export type ClientInfoErrorCode =
|
|
12
|
-
| 'INVALID_KEY'
|
|
13
|
-
| 'INVALID_VALUE'
|
|
14
|
-
| 'INVALID_VERSION';
|
|
15
|
-
|
|
16
|
-
export class ClientInfoError extends Error {
|
|
17
|
-
readonly code: ClientInfoErrorCode;
|
|
18
|
-
|
|
19
|
-
constructor(code: ClientInfoErrorCode, message: string) {
|
|
20
|
-
super(message);
|
|
21
|
-
this.name = 'ClientInfoError';
|
|
22
|
-
this.code = code;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface Segment {
|
|
27
|
-
readonly key: string;
|
|
28
|
-
readonly value: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const SEMVER_CORE = String.raw`(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)`;
|
|
32
|
-
|
|
33
|
-
const SEMVER_PRERELEASE = String.raw`(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?`;
|
|
34
|
-
|
|
35
|
-
const SEMVER_BUILDMETADATA = String.raw`(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?`;
|
|
36
|
-
|
|
37
|
-
const REGEXP_SEMVER = new RegExp(
|
|
38
|
-
'^' + SEMVER_CORE + SEMVER_PRERELEASE + SEMVER_BUILDMETADATA + '$'
|
|
39
|
-
);
|
|
40
|
-
const REGEXP_VALID_SEGMENT = /^[-0-9A-Za-z_.+]+$/;
|
|
41
|
-
const REGEXP_INVALID_SEGMENT_CHAR = /[^-0-9A-Za-z_.+]/g;
|
|
42
|
-
|
|
43
|
-
export function isSemVer(s: string): boolean {
|
|
44
|
-
return REGEXP_SEMVER.test(s);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export function isValidSegment(s: string): boolean {
|
|
48
|
-
return REGEXP_VALID_SEGMENT.test(s);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Replaces characters that are not valid in segment values with
|
|
53
|
-
* hyphens. Used for environment-sourced values (runtime version,
|
|
54
|
-
* upstream) that we do not control and cannot reject.
|
|
55
|
-
*/
|
|
56
|
-
export function sanitize(s: string): string {
|
|
57
|
-
return s.replace(REGEXP_INVALID_SEGMENT_CHAR, '-');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* ClientInfo is an immutable, ordered list of key/value segments. Use
|
|
62
|
-
* {@link ClientInfo.with} to derive new values with additional segments.
|
|
63
|
-
*/
|
|
64
|
-
export class ClientInfo {
|
|
65
|
-
static readonly EMPTY = new ClientInfo();
|
|
66
|
-
|
|
67
|
-
readonly segments: readonly Segment[];
|
|
68
|
-
|
|
69
|
-
private constructor(
|
|
70
|
-
segments: readonly {readonly key: string; readonly value: string}[] = []
|
|
71
|
-
) {
|
|
72
|
-
this.segments = [...segments];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Returns a new {@link ClientInfo} with the given items appended. Accepts
|
|
77
|
-
* either individual key/value pairs or another {@link ClientInfo} whose
|
|
78
|
-
* segments are merged in order. The original is not modified; mixing the
|
|
79
|
-
* two forms in a single call is supported.
|
|
80
|
-
*
|
|
81
|
-
* Keys and values on pair arguments must contain only alphanumeric
|
|
82
|
-
* characters plus `_`, `.`, `+`, or `-`. Exact key+value duplicates are
|
|
83
|
-
* silently ignored. On error, an exception is thrown (all-or-nothing).
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* base.with({key: 'partner', value: 'acme'});
|
|
88
|
-
* base.with(pkgClientInfo);
|
|
89
|
-
* base.with(pkgClientInfo, {key: 'sdk-feature', value: 'pagination'});
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
with(...items: (ClientInfo | Segment)[]): ClientInfo {
|
|
93
|
-
if (items.length === 0) {
|
|
94
|
-
return this;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const newSegments: Segment[] = [...this.segments];
|
|
98
|
-
|
|
99
|
-
for (const item of items) {
|
|
100
|
-
const pairs = item instanceof ClientInfo ? item.segments : [item];
|
|
101
|
-
for (const {key, value} of pairs) {
|
|
102
|
-
if (!isValidSegment(key)) {
|
|
103
|
-
throw new ClientInfoError('INVALID_KEY', `Invalid key: ${key}.`);
|
|
104
|
-
}
|
|
105
|
-
if (!isValidSegment(value)) {
|
|
106
|
-
throw new ClientInfoError(
|
|
107
|
-
'INVALID_VALUE',
|
|
108
|
-
`Invalid value for "${key}": ${value}.`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
if (newSegments.some(s => s.key === key && s.value === value)) {
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
newSegments.push({key, value});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return new ClientInfo(newSegments);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Returns a string representation of the client info suitable for
|
|
123
|
-
* inclusion in HTTP headers. Key/value pairs are formatted as
|
|
124
|
-
* "key/value" and joined by spaces in the order they were inserted.
|
|
125
|
-
*/
|
|
126
|
-
toString(): string {
|
|
127
|
-
return this.segments.map(s => `${s.key}/${s.value}`).join(' ');
|
|
128
|
-
}
|
|
129
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Browser-compatible {@link createDefault}. Contains no Node.js-specific
|
|
3
|
-
* APIs (no `process.version`, `process.platform`, or `process.env`); only
|
|
4
|
-
* the segments registered via {@link setProduct}, {@link setPartner}, and
|
|
5
|
-
* {@link addToDefault} are returned, plus the core SDK identity.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import {ClientInfo} from './clientinfo';
|
|
11
|
-
import {MODULE_NAME, VERSION, getBase} from './base';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Returns a {@link ClientInfo} populated with SDK metadata and segments
|
|
15
|
-
* registered via {@link addToDefault}. Unlike the Node.js variant, this
|
|
16
|
-
* does not auto-detect runtime, OS, CI/CD, or agent because those signals
|
|
17
|
-
* are not available in a browser.
|
|
18
|
-
*/
|
|
19
|
-
export function createDefault(): ClientInfo {
|
|
20
|
-
return ClientInfo.EMPTY.with(
|
|
21
|
-
{key: MODULE_NAME, value: VERSION},
|
|
22
|
-
...getBase().segments
|
|
23
|
-
);
|
|
24
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import {ClientInfo, sanitize} from './clientinfo';
|
|
2
|
-
import {MODULE_NAME, VERSION, getBase} from './base';
|
|
3
|
-
import {agentProvider} from './agent';
|
|
4
|
-
|
|
5
|
-
interface EnvCheck {
|
|
6
|
-
readonly name: string;
|
|
7
|
-
readonly expectedValue: string;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface CicdDef {
|
|
11
|
-
readonly name: string;
|
|
12
|
-
readonly envVars: readonly EnvCheck[];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const CICD_PROVIDERS: readonly CicdDef[] = [
|
|
16
|
-
{
|
|
17
|
-
name: 'github',
|
|
18
|
-
envVars: [{name: 'GITHUB_ACTIONS', expectedValue: 'true'}],
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
name: 'gitlab',
|
|
22
|
-
envVars: [{name: 'GITLAB_CI', expectedValue: 'true'}],
|
|
23
|
-
},
|
|
24
|
-
{name: 'jenkins', envVars: [{name: 'JENKINS_URL', expectedValue: ''}]},
|
|
25
|
-
{
|
|
26
|
-
name: 'azure-devops',
|
|
27
|
-
envVars: [{name: 'TF_BUILD', expectedValue: 'True'}],
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: 'circle',
|
|
31
|
-
envVars: [{name: 'CIRCLECI', expectedValue: 'true'}],
|
|
32
|
-
},
|
|
33
|
-
{name: 'travis', envVars: [{name: 'TRAVIS', expectedValue: 'true'}]},
|
|
34
|
-
{
|
|
35
|
-
name: 'bitbucket',
|
|
36
|
-
envVars: [{name: 'BITBUCKET_BUILD_NUMBER', expectedValue: ''}],
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
name: 'google-cloud-build',
|
|
40
|
-
envVars: [
|
|
41
|
-
{name: 'PROJECT_ID', expectedValue: ''},
|
|
42
|
-
{name: 'BUILD_ID', expectedValue: ''},
|
|
43
|
-
{name: 'PROJECT_NUMBER', expectedValue: ''},
|
|
44
|
-
{name: 'LOCATION', expectedValue: ''},
|
|
45
|
-
],
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
name: 'aws-code-build',
|
|
49
|
-
envVars: [{name: 'CODEBUILD_BUILD_ARN', expectedValue: ''}],
|
|
50
|
-
},
|
|
51
|
-
{name: 'tf-cloud', envVars: [{name: 'TFC_RUN_ID', expectedValue: ''}]},
|
|
52
|
-
];
|
|
53
|
-
|
|
54
|
-
function detectCicd(): string {
|
|
55
|
-
for (const p of CICD_PROVIDERS) {
|
|
56
|
-
const allMatch = p.envVars.every(ev => {
|
|
57
|
-
const v = process.env[ev.name];
|
|
58
|
-
return (
|
|
59
|
-
v !== undefined && (ev.expectedValue === '' || v === ev.expectedValue)
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
if (allMatch) {
|
|
63
|
-
return p.name;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return '';
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Converts a Node.js version string (e.g., "v22.0.0") into a bare
|
|
71
|
-
* semver string (e.g., "22.0.0").
|
|
72
|
-
*/
|
|
73
|
-
export function normalizeNodeVersion(raw: string): string {
|
|
74
|
-
if (!raw.startsWith('v')) {
|
|
75
|
-
return '0.0.0-dev';
|
|
76
|
-
}
|
|
77
|
-
return raw.slice(1);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Computed once at module load because process.version never changes
|
|
81
|
-
// during a process lifetime.
|
|
82
|
-
export const CACHED_NODE_VERSION = normalizeNodeVersion(process.version);
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Returns a {@link ClientInfo} populated with SDK metadata, runtime
|
|
86
|
-
* information, segments registered via {@link addToDefault}, and
|
|
87
|
-
* automatically detected environment properties.
|
|
88
|
-
*/
|
|
89
|
-
export function createDefault(): ClientInfo {
|
|
90
|
-
const pairs: {key: string; value: string}[] = [
|
|
91
|
-
{key: MODULE_NAME, value: VERSION},
|
|
92
|
-
{key: 'node', value: CACHED_NODE_VERSION},
|
|
93
|
-
{key: 'os', value: process.platform},
|
|
94
|
-
...getBase().segments,
|
|
95
|
-
];
|
|
96
|
-
|
|
97
|
-
// DATABRICKS_SDK_UPSTREAM and DATABRICKS_SDK_UPSTREAM_VERSION are set
|
|
98
|
-
// by tools built on top of this SDK (e.g. Terraform provider, Pulumi)
|
|
99
|
-
// to identify themselves as the upstream product. Both must be present
|
|
100
|
-
// for the upstream segment to be included.
|
|
101
|
-
const upstream = process.env.DATABRICKS_SDK_UPSTREAM;
|
|
102
|
-
if (upstream !== undefined) {
|
|
103
|
-
const upstreamVersion = process.env.DATABRICKS_SDK_UPSTREAM_VERSION;
|
|
104
|
-
if (upstreamVersion !== undefined) {
|
|
105
|
-
pairs.push(
|
|
106
|
-
{key: 'upstream', value: sanitize(upstream)},
|
|
107
|
-
{key: 'upstream-version', value: sanitize(upstreamVersion)}
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const cicd = detectCicd();
|
|
113
|
-
if (cicd !== '') {
|
|
114
|
-
pairs.push({key: 'cicd', value: cicd});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const runtime = process.env.DATABRICKS_RUNTIME_VERSION;
|
|
118
|
-
if (runtime !== undefined && runtime !== '') {
|
|
119
|
-
pairs.push({key: 'runtime', value: sanitize(runtime)});
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const agent = agentProvider();
|
|
123
|
-
if (agent !== '') {
|
|
124
|
-
pairs.push({key: 'agent', value: agent});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return ClientInfo.EMPTY.with(...pairs);
|
|
128
|
-
}
|
package/src/clientinfo/index.ts
DELETED
package/src/http/http.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* HTTP transport primitives. Defines the {@link HttpClient} interface and a
|
|
3
|
-
* {@link newFetchHttpClient} implementation backed by the Fetch API.
|
|
4
|
-
*
|
|
5
|
-
* @module
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** HttpRequest represents an outgoing HTTP request. */
|
|
9
|
-
export interface HttpRequest {
|
|
10
|
-
/** The URL to send the request to. */
|
|
11
|
-
url: string;
|
|
12
|
-
|
|
13
|
-
/** The HTTP method (GET, POST, etc.). */
|
|
14
|
-
method: string;
|
|
15
|
-
|
|
16
|
-
/** The request headers. */
|
|
17
|
-
headers: Headers;
|
|
18
|
-
|
|
19
|
-
/** The request body. */
|
|
20
|
-
body?: string | ArrayBuffer | Uint8Array | ReadableStream<Uint8Array> | null;
|
|
21
|
-
|
|
22
|
-
/** An optional signal to abort the request. */
|
|
23
|
-
signal?: AbortSignal;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** HttpResponse represents the response from an HTTP request. */
|
|
27
|
-
export interface HttpResponse {
|
|
28
|
-
/** The HTTP status code. */
|
|
29
|
-
statusCode: number;
|
|
30
|
-
|
|
31
|
-
/** The response headers. */
|
|
32
|
-
headers: Headers;
|
|
33
|
-
|
|
34
|
-
/** The raw response body stream. */
|
|
35
|
-
body: ReadableStream<Uint8Array> | null;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* HttpClient sends HTTP requests and returns responses.
|
|
40
|
-
*/
|
|
41
|
-
export interface HttpClient {
|
|
42
|
-
/** Sends an HTTP request and returns the response. */
|
|
43
|
-
send(request: HttpRequest): Promise<HttpResponse>;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Creates a new HttpClient that uses the Fetch API as its transport.
|
|
48
|
-
*/
|
|
49
|
-
export function newFetchHttpClient(): HttpClient {
|
|
50
|
-
return {
|
|
51
|
-
async send(request: HttpRequest): Promise<HttpResponse> {
|
|
52
|
-
const init: RequestInit = {
|
|
53
|
-
method: request.method,
|
|
54
|
-
headers: request.headers,
|
|
55
|
-
};
|
|
56
|
-
if (request.body !== undefined) {
|
|
57
|
-
init.body = request.body;
|
|
58
|
-
// The Fetch spec requires duplex: 'half' for streaming request bodies.
|
|
59
|
-
// See https://fetch.spec.whatwg.org/#dom-requestinit-duplex.
|
|
60
|
-
if (request.body instanceof ReadableStream) {
|
|
61
|
-
init.duplex = 'half';
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (request.signal !== undefined) {
|
|
65
|
-
init.signal = request.signal;
|
|
66
|
-
}
|
|
67
|
-
const response = await fetch(request.url, init);
|
|
68
|
-
return {
|
|
69
|
-
statusCode: response.status,
|
|
70
|
-
headers: response.headers,
|
|
71
|
-
body: response.body,
|
|
72
|
-
};
|
|
73
|
-
},
|
|
74
|
-
};
|
|
75
|
-
}
|
package/src/http/index.ts
DELETED
package/src/index.ts
DELETED
package/src/logger/index.ts
DELETED
package/src/logger/logger.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/** Logger interface and built-in implementations. */
|
|
2
|
-
|
|
3
|
-
/** Supported log levels in order of increasing severity. */
|
|
4
|
-
export type Level = 'debug' | 'info' | 'warn' | 'error' | 'off';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* A logger that receives messages at different severity levels.
|
|
8
|
-
*
|
|
9
|
-
* The method signatures are intentionally compatible with the global
|
|
10
|
-
* {@link Console} object so that `console` can be used as a Logger when level
|
|
11
|
-
* filtering is not needed.
|
|
12
|
-
*/
|
|
13
|
-
export interface Logger {
|
|
14
|
-
/** Logs a debug-level message. */
|
|
15
|
-
debug(message: string, ...args: unknown[]): void;
|
|
16
|
-
|
|
17
|
-
/** Logs an info-level message. */
|
|
18
|
-
info(message: string, ...args: unknown[]): void;
|
|
19
|
-
|
|
20
|
-
/** Logs a warn-level message. */
|
|
21
|
-
warn(message: string, ...args: unknown[]): void;
|
|
22
|
-
|
|
23
|
-
/** Logs an error-level message. */
|
|
24
|
-
error(message: string, ...args: unknown[]): void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** A logger that silently discards all messages. */
|
|
28
|
-
export class NoOpLogger implements Logger {
|
|
29
|
-
debug(): void {
|
|
30
|
-
// Intentionally empty.
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
info(): void {
|
|
34
|
-
// Intentionally empty.
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
warn(): void {
|
|
38
|
-
// Intentionally empty.
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
error(): void {
|
|
42
|
-
// Intentionally empty.
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Numeric severity used by LogLevel to gate calls.
|
|
47
|
-
const LEVEL_SEVERITY: Record<Level, number> = {
|
|
48
|
-
debug: 0,
|
|
49
|
-
info: 1,
|
|
50
|
-
warn: 2,
|
|
51
|
-
error: 3,
|
|
52
|
-
off: 4,
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* A decorator that adds level filtering to any {@link Logger}.
|
|
57
|
-
*
|
|
58
|
-
* Only messages at or above the configured minimum level are forwarded to the
|
|
59
|
-
* underlying logger. The default underlying logger is `console`.
|
|
60
|
-
*
|
|
61
|
-
* @example
|
|
62
|
-
* ```typescript
|
|
63
|
-
* // Only warn and error go to console.
|
|
64
|
-
* const logger = new LogLevel('warn');
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export class LogLevel implements Logger {
|
|
68
|
-
private readonly threshold: number;
|
|
69
|
-
private readonly logger: Logger;
|
|
70
|
-
|
|
71
|
-
constructor(level: Level, logger: Logger = console) {
|
|
72
|
-
this.threshold = LEVEL_SEVERITY[level];
|
|
73
|
-
this.logger = logger;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
debug(message: string, ...args: unknown[]): void {
|
|
77
|
-
if (this.threshold <= LEVEL_SEVERITY.debug) {
|
|
78
|
-
this.logger.debug(message, ...args);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
info(message: string, ...args: unknown[]): void {
|
|
83
|
-
if (this.threshold <= LEVEL_SEVERITY.info) {
|
|
84
|
-
this.logger.info(message, ...args);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
warn(message: string, ...args: unknown[]): void {
|
|
89
|
-
if (this.threshold <= LEVEL_SEVERITY.warn) {
|
|
90
|
-
this.logger.warn(message, ...args);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
error(message: string, ...args: unknown[]): void {
|
|
95
|
-
if (this.threshold <= LEVEL_SEVERITY.error) {
|
|
96
|
-
this.logger.error(message, ...args);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
package/src/ops/execute.ts
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import type {Options} from './options';
|
|
2
|
-
import type {Retrier} from './retrier';
|
|
3
|
-
|
|
4
|
-
// Coerces an unknown value to an Error instance.
|
|
5
|
-
function toError(value: unknown): Error {
|
|
6
|
-
return value instanceof Error ? value : new Error(String(value));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Sleeps for the given duration. It is mostly equivalent to setTimeout, but
|
|
11
|
-
* can be interrupted by the AbortSignal if the signal aborts before the
|
|
12
|
-
* duration elapses.
|
|
13
|
-
*/
|
|
14
|
-
export function sleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
15
|
-
return new Promise<void>((resolve, reject) => {
|
|
16
|
-
if (signal?.aborted === true) {
|
|
17
|
-
reject(toError(signal.reason));
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
if (signal === undefined) {
|
|
21
|
-
setTimeout(resolve, ms);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
const onAbort = (): void => {
|
|
25
|
-
clearTimeout(timer);
|
|
26
|
-
reject(toError(signal.reason));
|
|
27
|
-
};
|
|
28
|
-
const timer = setTimeout(() => {
|
|
29
|
-
signal.removeEventListener('abort', onAbort);
|
|
30
|
-
resolve();
|
|
31
|
-
}, ms);
|
|
32
|
-
signal.addEventListener('abort', onAbort, {once: true});
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Sleeper is a convenience type for readability.
|
|
37
|
-
type Sleeper = (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
38
|
-
|
|
39
|
-
/** Executes operation op with the given options. */
|
|
40
|
-
export async function execute(
|
|
41
|
-
signal: AbortSignal | undefined,
|
|
42
|
-
op: (signal?: AbortSignal) => Promise<void>,
|
|
43
|
-
options?: Options
|
|
44
|
-
): Promise<void> {
|
|
45
|
-
const opts: Options = {...options};
|
|
46
|
-
return executeImpl(signal, op, opts, sleep);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* The actual implementation of execute. Its purpose is to ease testing by
|
|
51
|
-
* providing a convenient way to mock the sleeping logic.
|
|
52
|
-
*/
|
|
53
|
-
async function executeImpl(
|
|
54
|
-
signal: AbortSignal | undefined,
|
|
55
|
-
op: (signal?: AbortSignal) => Promise<void>,
|
|
56
|
-
opts: Options,
|
|
57
|
-
sleep: Sleeper
|
|
58
|
-
): Promise<void> {
|
|
59
|
-
// Optionally combine the signal with a timeout signal. If the signal
|
|
60
|
-
// already has a deadline, that deadline is updated to the minimum of the
|
|
61
|
-
// signal's deadline and the timeout.
|
|
62
|
-
if (opts.timeout !== undefined && opts.timeout > 0) {
|
|
63
|
-
const timeoutSignal = AbortSignal.timeout(opts.timeout);
|
|
64
|
-
signal = signal ? AbortSignal.any([signal, timeoutSignal]) : timeoutSignal;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// Get a new retrier for this specific execution. This is instantiated
|
|
68
|
-
// lazily if and when the first operation execution returns an error.
|
|
69
|
-
let retrier: Retrier | undefined;
|
|
70
|
-
|
|
71
|
-
for (;;) {
|
|
72
|
-
if (opts.rateLimiter) {
|
|
73
|
-
await opts.rateLimiter.wait(signal);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
await op(signal);
|
|
78
|
-
return; // Nothing to retry.
|
|
79
|
-
} catch (err: unknown) {
|
|
80
|
-
const error = toError(err);
|
|
81
|
-
|
|
82
|
-
if (retrier === undefined) {
|
|
83
|
-
if (opts.retrier) {
|
|
84
|
-
retrier = opts.retrier(); // Lazily instantiate the retrier.
|
|
85
|
-
}
|
|
86
|
-
if (retrier === undefined) {
|
|
87
|
-
throw error; // No retrier == no retry.
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const delay = retrier.isRetriable(error);
|
|
92
|
-
if (delay === undefined) {
|
|
93
|
-
throw error; // Not retriable.
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
await sleep(delay, signal);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
package/src/ops/index.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utilities to execute Databricks operations with retry, timeout, and rate
|
|
3
|
-
* limiting.
|
|
4
|
-
*
|
|
5
|
-
* @packageDocumentation
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
export {execute} from './execute';
|
|
9
|
-
export type {Limiter} from './limiter';
|
|
10
|
-
export type {Options} from './options';
|
|
11
|
-
export {BackoffPolicy, retryOn} from './retrier';
|
|
12
|
-
export type {BackoffPolicyOptions, Retrier} from './retrier';
|
package/src/ops/limiter.ts
DELETED
package/src/ops/options.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type {Limiter} from './limiter';
|
|
2
|
-
import type {Retrier} from './retrier';
|
|
3
|
-
|
|
4
|
-
/** Options that control the behavior of an operation. */
|
|
5
|
-
export interface Options {
|
|
6
|
-
/**
|
|
7
|
-
* Provides a new Retrier to be used to execute an operation. The function
|
|
8
|
-
* is called for each operation and must be safe to call concurrently. The
|
|
9
|
-
* retrier must be fresh within the context of an execute call (e.g. no
|
|
10
|
-
* need to reset a BackoffPolicy).
|
|
11
|
-
*/
|
|
12
|
-
retrier?: (() => Retrier) | undefined;
|
|
13
|
-
/** The rate limiter used to potentially rate limit the operation. */
|
|
14
|
-
rateLimiter?: Limiter | undefined;
|
|
15
|
-
/**
|
|
16
|
-
* Timeout duration in milliseconds. If the signal already has a deadline,
|
|
17
|
-
* that deadline is updated to the minimum of the signal's deadline and the
|
|
18
|
-
* timeout. The timeout covers the whole operation execution; it is not a
|
|
19
|
-
* timeout for each intermediary call.
|
|
20
|
-
*/
|
|
21
|
-
timeout?: number | undefined;
|
|
22
|
-
}
|