@databricks/sdk-core 0.0.0-dev → 0.1.0-dev.1

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.
Files changed (163) hide show
  1. package/README.md +11 -1
  2. package/dist/api/execute.d.ts +12 -0
  3. package/dist/api/execute.d.ts.map +1 -0
  4. package/dist/api/execute.js +77 -0
  5. package/dist/api/execute.js.map +1 -0
  6. package/dist/api/index.d.ts +12 -0
  7. package/dist/api/index.d.ts.map +1 -0
  8. package/dist/api/index.js +8 -0
  9. package/dist/api/index.js.map +1 -0
  10. package/dist/api/limiter.d.ts +9 -0
  11. package/dist/api/limiter.d.ts.map +1 -0
  12. package/dist/api/limiter.js +2 -0
  13. package/dist/api/limiter.js.map +1 -0
  14. package/dist/api/options.d.ts +22 -0
  15. package/dist/api/options.d.ts.map +1 -0
  16. package/dist/api/options.js +2 -0
  17. package/dist/api/options.js.map +1 -0
  18. package/dist/api/retrier.d.ts +58 -0
  19. package/dist/api/retrier.d.ts.map +1 -0
  20. package/dist/api/retrier.js +70 -0
  21. package/dist/api/retrier.js.map +1 -0
  22. package/dist/apierror/apierror.d.ts +53 -0
  23. package/dist/apierror/apierror.d.ts.map +1 -0
  24. package/dist/apierror/apierror.js +217 -0
  25. package/dist/apierror/apierror.js.map +1 -0
  26. package/dist/apierror/codes/codes.d.ts +140 -0
  27. package/dist/apierror/codes/codes.d.ts.map +1 -0
  28. package/dist/apierror/codes/codes.js +167 -0
  29. package/dist/apierror/codes/codes.js.map +1 -0
  30. package/dist/apierror/codes/index.d.ts +7 -0
  31. package/dist/apierror/codes/index.d.ts.map +1 -0
  32. package/dist/apierror/codes/index.js +7 -0
  33. package/dist/apierror/codes/index.js.map +1 -0
  34. package/dist/apierror/details.d.ts +177 -0
  35. package/dist/apierror/details.d.ts.map +1 -0
  36. package/dist/apierror/details.js +250 -0
  37. package/dist/apierror/details.js.map +1 -0
  38. package/dist/apierror/index.d.ts +8 -0
  39. package/dist/apierror/index.d.ts.map +1 -0
  40. package/dist/apierror/index.js +7 -0
  41. package/dist/apierror/index.js.map +1 -0
  42. package/dist/clientinfo/agent.d.ts +56 -0
  43. package/dist/clientinfo/agent.d.ts.map +1 -0
  44. package/dist/clientinfo/agent.js +120 -0
  45. package/dist/clientinfo/agent.js.map +1 -0
  46. package/dist/clientinfo/base.d.ts +39 -0
  47. package/dist/clientinfo/base.d.ts.map +1 -0
  48. package/dist/clientinfo/base.js +61 -0
  49. package/dist/clientinfo/base.js.map +1 -0
  50. package/dist/clientinfo/clientinfo.d.ts +61 -0
  51. package/dist/clientinfo/clientinfo.d.ts.map +1 -0
  52. package/dist/clientinfo/clientinfo.js +96 -0
  53. package/dist/clientinfo/clientinfo.js.map +1 -0
  54. package/dist/clientinfo/default.browser.d.ts +17 -0
  55. package/dist/clientinfo/default.browser.d.ts.map +1 -0
  56. package/dist/clientinfo/default.browser.js +20 -0
  57. package/dist/clientinfo/default.browser.js.map +1 -0
  58. package/dist/clientinfo/default.d.ts +14 -0
  59. package/dist/clientinfo/default.d.ts.map +1 -0
  60. package/dist/clientinfo/default.js +104 -0
  61. package/dist/clientinfo/default.js.map +1 -0
  62. package/dist/clientinfo/index.browser.d.ts +5 -0
  63. package/dist/clientinfo/index.browser.d.ts.map +1 -0
  64. package/dist/clientinfo/index.browser.js +4 -0
  65. package/dist/clientinfo/index.browser.js.map +1 -0
  66. package/dist/clientinfo/index.d.ts +5 -0
  67. package/dist/clientinfo/index.d.ts.map +1 -0
  68. package/dist/clientinfo/index.js +4 -0
  69. package/dist/clientinfo/index.js.map +1 -0
  70. package/dist/http/http.d.ts +40 -0
  71. package/dist/http/http.d.ts.map +1 -0
  72. package/dist/http/http.js +37 -0
  73. package/dist/http/http.js.map +1 -0
  74. package/dist/http/index.d.ts +8 -0
  75. package/dist/http/index.d.ts.map +1 -0
  76. package/dist/http/index.js +7 -0
  77. package/dist/http/index.js.map +1 -0
  78. package/dist/index.d.ts +6 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.js +7 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/logger/index.d.ts +8 -0
  83. package/dist/logger/index.d.ts.map +1 -0
  84. package/dist/logger/index.js +7 -0
  85. package/dist/logger/index.js.map +1 -0
  86. package/dist/logger/logger.d.ts +49 -0
  87. package/dist/logger/logger.d.ts.map +1 -0
  88. package/dist/logger/logger.js +65 -0
  89. package/dist/logger/logger.js.map +1 -0
  90. package/dist/profiles/errors.d.ts +17 -0
  91. package/dist/profiles/errors.d.ts.map +1 -0
  92. package/dist/profiles/errors.js +19 -0
  93. package/dist/profiles/errors.js.map +1 -0
  94. package/dist/profiles/index.browser.d.ts +10 -0
  95. package/dist/profiles/index.browser.d.ts.map +1 -0
  96. package/dist/profiles/index.browser.js +8 -0
  97. package/dist/profiles/index.browser.js.map +1 -0
  98. package/dist/profiles/index.d.ts +15 -0
  99. package/dist/profiles/index.d.ts.map +1 -0
  100. package/dist/profiles/index.js +13 -0
  101. package/dist/profiles/index.js.map +1 -0
  102. package/dist/profiles/ini.d.ts +36 -0
  103. package/dist/profiles/ini.d.ts.map +1 -0
  104. package/dist/profiles/ini.js +113 -0
  105. package/dist/profiles/ini.js.map +1 -0
  106. package/dist/profiles/profile.d.ts +131 -0
  107. package/dist/profiles/profile.d.ts.map +1 -0
  108. package/dist/profiles/profile.js +307 -0
  109. package/dist/profiles/profile.js.map +1 -0
  110. package/dist/profiles/resolve.d.ts +29 -0
  111. package/dist/profiles/resolve.d.ts.map +1 -0
  112. package/dist/profiles/resolve.js +206 -0
  113. package/dist/profiles/resolve.js.map +1 -0
  114. package/dist/profiles/secret.d.ts +25 -0
  115. package/dist/profiles/secret.d.ts.map +1 -0
  116. package/dist/profiles/secret.js +38 -0
  117. package/dist/profiles/secret.js.map +1 -0
  118. package/dist/wkt/fieldmask.d.ts +32 -0
  119. package/dist/wkt/fieldmask.d.ts.map +1 -0
  120. package/dist/wkt/fieldmask.js +68 -0
  121. package/dist/wkt/fieldmask.js.map +1 -0
  122. package/dist/wkt/index.d.ts +4 -0
  123. package/dist/wkt/index.d.ts.map +1 -0
  124. package/dist/wkt/index.js +2 -0
  125. package/dist/wkt/index.js.map +1 -0
  126. package/dist/wkt/value.d.ts +13 -0
  127. package/dist/wkt/value.d.ts.map +1 -0
  128. package/dist/wkt/value.js +2 -0
  129. package/dist/wkt/value.js.map +1 -0
  130. package/package.json +80 -4
  131. package/src/api/execute.ts +102 -0
  132. package/src/api/index.ts +12 -0
  133. package/src/api/limiter.ts +8 -0
  134. package/src/api/options.ts +22 -0
  135. package/src/api/retrier.ts +108 -0
  136. package/src/apierror/apierror.ts +253 -0
  137. package/src/apierror/codes/codes.ts +189 -0
  138. package/src/apierror/codes/index.ts +7 -0
  139. package/src/apierror/details.ts +459 -0
  140. package/src/apierror/index.ts +24 -0
  141. package/src/clientinfo/agent.ts +131 -0
  142. package/src/clientinfo/base.ts +72 -0
  143. package/src/clientinfo/clientinfo.ts +129 -0
  144. package/src/clientinfo/default.browser.ts +24 -0
  145. package/src/clientinfo/default.ts +128 -0
  146. package/src/clientinfo/index.browser.ts +4 -0
  147. package/src/clientinfo/index.ts +4 -0
  148. package/src/http/http.ts +75 -0
  149. package/src/http/index.ts +8 -0
  150. package/src/index.ts +5 -0
  151. package/src/logger/index.ts +8 -0
  152. package/src/logger/logger.ts +99 -0
  153. package/src/profiles/errors.ts +28 -0
  154. package/src/profiles/index.browser.ts +10 -0
  155. package/src/profiles/index.ts +15 -0
  156. package/src/profiles/ini.ts +126 -0
  157. package/src/profiles/profile.ts +467 -0
  158. package/src/profiles/resolve.ts +251 -0
  159. package/src/profiles/secret.ts +40 -0
  160. package/src/wkt/fieldmask.ts +89 -0
  161. package/src/wkt/index.ts +3 -0
  162. package/src/wkt/value.ts +19 -0
  163. package/index.js +0 -1
@@ -0,0 +1,129 @@
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
+ }
@@ -0,0 +1,24 @@
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
+ }
@@ -0,0 +1,128 @@
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
+ }
@@ -0,0 +1,4 @@
1
+ export type {ClientInfoErrorCode} from './clientinfo';
2
+ export {ClientInfo, ClientInfoError} from './clientinfo';
3
+ export {addToDefault, setPartner, setProduct} from './base';
4
+ export {createDefault} from './default.browser';
@@ -0,0 +1,4 @@
1
+ export type {ClientInfoErrorCode} from './clientinfo';
2
+ export {ClientInfo, ClientInfoError} from './clientinfo';
3
+ export {addToDefault, setPartner, setProduct} from './base';
4
+ export {createDefault} from './default';
@@ -0,0 +1,75 @@
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
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * HTTP transport primitives.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export {newFetchHttpClient} from './http';
8
+ export type {HttpClient, HttpRequest, HttpResponse} from './http';
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Databricks core library.
3
+ *
4
+ * @packageDocumentation
5
+ */
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Logger interface and built-in implementations.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export type {Level, Logger} from './logger';
8
+ export {NoOpLogger, LogLevel} from './logger';
@@ -0,0 +1,99 @@
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
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Error types for Databricks configuration profile operations.
3
+ *
4
+ * @module
5
+ */
6
+
7
+ /** Discriminant codes for {@link ProfileError}. */
8
+ export type ProfileErrorCode =
9
+ | 'CONFIG_FILE_NOT_FOUND'
10
+ | 'PROFILE_NOT_FOUND'
11
+ | 'EMPTY_PATH'
12
+ | 'EMPTY_PROFILE'
13
+ | 'INVALID_PROFILE_NAME';
14
+
15
+ /**
16
+ * Error thrown by profile operations.
17
+ *
18
+ * Use the {@link ProfileError.code} field to distinguish between error causes.
19
+ */
20
+ export class ProfileError extends Error {
21
+ readonly code: ProfileErrorCode;
22
+
23
+ constructor(code: ProfileErrorCode, message: string) {
24
+ super(message);
25
+ this.name = 'ProfileError';
26
+ this.code = code;
27
+ }
28
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Browser entry point for the profiles module.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ export {ProfileError} from './errors';
8
+ export type {ProfileErrorCode} from './errors';
9
+ export type {Profile, ResolveOptions} from './profile';
10
+ export {Secret} from './secret';
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Utility to resolve Databricks configuration profiles.
3
+ *
4
+ * A profile is a named collection of configuration values. It is typically
5
+ * stored in a file called ~/.databrickscfg. Profiles can be resolved from the
6
+ * file and/or environment variables using the {@link resolve} function.
7
+ *
8
+ * @packageDocumentation
9
+ */
10
+
11
+ export {ProfileError} from './errors';
12
+ export type {ProfileErrorCode} from './errors';
13
+ export type {Profile, ResolveOptions} from './profile';
14
+ export {defaultConfigFile, listProfiles, resolve} from './resolve';
15
+ export {Secret} from './secret';
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Minimal INI parser and writer for the Databricks config file format.
3
+ *
4
+ * This matches the behavior of {@link https://pkg.go.dev/gopkg.in/ini.v1 | go-ini}
5
+ * with `SpaceBeforeInlineComment: true`, which is how the Go SDK loads
6
+ * databrickscfg files. Key behavioral details:
7
+ *
8
+ * - Inline comments are only recognized when `#` or `;` is preceded by a
9
+ * space. The parser checks for `" #"` first; only if absent does it check
10
+ * for `" ;"`. This matches Go's `strings.Index` precedence.
11
+ * - Both `=` and `:` are key-value delimiters (Go default). Only the first
12
+ * delimiter on a line splits key from value.
13
+ * - Section names are the raw text between `[` and the last `]` on the line,
14
+ * with no trimming.
15
+ * - Keys and values are trimmed of surrounding whitespace.
16
+ * - Lines without a delimiter throw an error (matching go-ini default).
17
+ *
18
+ * @module
19
+ */
20
+
21
+ /** Parsed INI data as an ordered map of section names to key-value maps. */
22
+ export type IniData = Map<string, Map<string, string>>;
23
+
24
+ /**
25
+ * Parses an INI-formatted string into structured data.
26
+ *
27
+ * Keys before any section header are assigned to the "DEFAULT" section.
28
+ */
29
+ export function parseIni(content: string): IniData {
30
+ const result: IniData = new Map();
31
+ let currentSection = 'DEFAULT';
32
+ result.set(currentSection, new Map());
33
+
34
+ for (const rawLine of content.split(/\r?\n/)) {
35
+ const line = rawLine.trim();
36
+
37
+ // Skip empty lines and full-line comments.
38
+ if (line === '' || line.startsWith('#') || line.startsWith(';')) {
39
+ continue;
40
+ }
41
+
42
+ // Section header: text between '[' and the last ']'.
43
+ if (line.startsWith('[')) {
44
+ const closeIdx = line.lastIndexOf(']');
45
+ if (closeIdx === -1) {
46
+ throw new Error(`unclosed section: ${line}`);
47
+ }
48
+ const name = line.slice(1, closeIdx);
49
+ if (name === '') {
50
+ throw new Error('empty section name');
51
+ }
52
+ currentSection = name;
53
+ if (!result.has(currentSection)) {
54
+ result.set(currentSection, new Map());
55
+ }
56
+ continue;
57
+ }
58
+
59
+ // Key-value pair: first '=' or ':' splits key from value.
60
+ const eqIdx = line.indexOf('=');
61
+ const colonIdx = line.indexOf(':');
62
+ let delimIdx: number;
63
+ if (eqIdx === -1 && colonIdx === -1) {
64
+ throw new Error(`key-value delimiter not found: ${line}`);
65
+ } else if (eqIdx === -1) {
66
+ delimIdx = colonIdx;
67
+ } else if (colonIdx === -1) {
68
+ delimIdx = eqIdx;
69
+ } else {
70
+ delimIdx = Math.min(eqIdx, colonIdx);
71
+ }
72
+
73
+ const key = line.slice(0, delimIdx).trim();
74
+ if (key === '') {
75
+ throw new Error(`empty key name: ${line}`);
76
+ }
77
+ let value = line.slice(delimIdx + 1).trim();
78
+
79
+ // Strip inline comments matching go-ini's SpaceBeforeInlineComment
80
+ // precedence: try " #" first, then " ;" only if " #" is absent.
81
+ let commentIdx = value.indexOf(' #');
82
+ if (commentIdx === -1) {
83
+ commentIdx = value.indexOf(' ;');
84
+ }
85
+ if (commentIdx !== -1) {
86
+ value = value.slice(0, commentIdx).trimEnd();
87
+ }
88
+
89
+ if (!result.has(currentSection)) {
90
+ result.set(currentSection, new Map());
91
+ }
92
+ const section = result.get(currentSection);
93
+ if (section !== undefined) {
94
+ section.set(key, value);
95
+ }
96
+ }
97
+
98
+ return result;
99
+ }
100
+
101
+ /**
102
+ * Formats structured INI data back into a string.
103
+ *
104
+ * Each section's keys are aligned so that all `=` signs appear in the same
105
+ * column, matching the output format of go-ini's `PrettyFormat` default.
106
+ * Sections are separated by blank lines (`PrettySection` default).
107
+ */
108
+ export function formatIni(data: IniData): string {
109
+ const sections: string[] = [];
110
+
111
+ for (const [name, keys] of data) {
112
+ const lines: string[] = [];
113
+ lines.push(`[${name}]`);
114
+
115
+ if (keys.size > 0) {
116
+ const maxKeyLen = Math.max(...[...keys.keys()].map(k => k.length));
117
+ for (const [key, value] of keys) {
118
+ lines.push(`${key.padEnd(maxKeyLen)} = ${value}`);
119
+ }
120
+ }
121
+
122
+ sections.push(lines.join('\n'));
123
+ }
124
+
125
+ return sections.join('\n\n') + '\n';
126
+ }