@mhjuma/traverse 0.2.0 → 0.4.0
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/CHANGELOG.md +98 -0
- package/package.json +18 -4
- package/src/bootstrap.spec.tsx +191 -1
- package/src/bootstrap.tsx +172 -6
- package/src/components/Explorer/Explorer.spec.tsx +47 -3
- package/src/components/Explorer/Explorer.tsx +33 -0
- package/src/components/Explorer/MonacoConfigErrorBanner.tsx +86 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +53 -17
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +78 -74
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.spec.ts +150 -0
- package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.ts +95 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.spec.ts +322 -0
- package/src/components/ExplorerWorkspace/kustoSchemaQueue.ts +188 -0
- package/src/components/ExplorerWorkspace/monacoLoader.spec.ts +82 -0
- package/src/components/ExplorerWorkspace/monacoLoader.ts +57 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.spec.ts +146 -0
- package/src/components/ExplorerWorkspace/monacoWorkers.ts +179 -0
- package/src/index.ts +3 -0
- package/src/services/kusto.spec.ts +155 -1
- package/src/services/kusto.ts +117 -7
- package/src/services/schema.spec.ts +107 -28
- package/src/services/schema.ts +66 -25
- package/src/test-setup.ts +3 -0
- package/src/vite-plugin.spec.ts +118 -0
- package/src/vite-plugin.ts +114 -0
- package/vitest.config.ts +7 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +0 -321
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +0 -155
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { loader } from '@monaco-editor/react';
|
|
2
|
+
import * as monaco from 'monaco-editor';
|
|
3
|
+
|
|
4
|
+
import { bootstrapKustoLanguage } from './bootstrapKustoLanguage';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Configure `@monaco-editor/react` to use the locally bundled Monaco instance
|
|
8
|
+
* instead of fetching it from the default CDN. This is required so that any
|
|
9
|
+
* Monaco contributions registered by this library (e.g. `@kusto/monaco-kusto`)
|
|
10
|
+
* attach to the same Monaco instance the `<Editor>` component uses — otherwise
|
|
11
|
+
* contributions are silently dropped ("two Monacos" problem).
|
|
12
|
+
*
|
|
13
|
+
* The call is idempotent and a no-op on the server. It is intentionally
|
|
14
|
+
* lightweight — it only configures the loader. Pre-loading the kusto
|
|
15
|
+
* language is gated behind `preloadKustoLanguage()` so consumers using only
|
|
16
|
+
* non-editor APIs (e.g. `createKustoClient`) do not pay for the multi-MB
|
|
17
|
+
* `@kusto/monaco-kusto` chunk.
|
|
18
|
+
*/
|
|
19
|
+
let configured = false;
|
|
20
|
+
export function configureMonacoLoader(): void {
|
|
21
|
+
if (configured || typeof window === 'undefined') {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
configured = true;
|
|
25
|
+
loader.config({ monaco });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
configureMonacoLoader();
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Trigger registration of the kusto language (Monarch tokens provider, themes,
|
|
32
|
+
* completion/hover providers) ahead of the editor first mounting. Call from
|
|
33
|
+
* any component that will render the KQL editor (e.g. `<Explorer>`) so the
|
|
34
|
+
* dynamic import of `@kusto/monaco-kusto` overlaps with React/auth init —
|
|
35
|
+
* the editor's first paint then shows colored keywords/strings/numbers
|
|
36
|
+
* instead of plain text that "upgrades" a beat later.
|
|
37
|
+
*
|
|
38
|
+
* Editor-gated by design: consumers of `@mhjuma/traverse` that use only
|
|
39
|
+
* non-editor APIs (e.g. `createKustoClient`) should not call this and will
|
|
40
|
+
* not incur the kusto chunk download.
|
|
41
|
+
*
|
|
42
|
+
* Idempotent — repeated calls are no-ops. Errors are swallowed here and
|
|
43
|
+
* surfaced by `ExplorerWorkspace`'s mount-time bootstrap on retry.
|
|
44
|
+
*/
|
|
45
|
+
let preloadStarted = false;
|
|
46
|
+
export function preloadKustoLanguage(): void {
|
|
47
|
+
if (preloadStarted || typeof window === 'undefined') {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
preloadStarted = true;
|
|
51
|
+
void bootstrapKustoLanguage(monaco).catch(() => { /* surfaced in ExplorerWorkspace */ });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Test-only: reset preload state so each test exercises the first-call path. */
|
|
55
|
+
export function __resetPreloadStateForTests(): void {
|
|
56
|
+
preloadStarted = false;
|
|
57
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { configureTraverseMonacoWorkers, getMonacoWorkerConfigurationError, __resetWorkerConfigForTests } from './monacoWorkers';
|
|
4
|
+
|
|
5
|
+
interface GlobalWithMonaco {
|
|
6
|
+
MonacoEnvironment?: { getWorker?: (workerId: string, label: string) => Worker };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function getMonacoEnvironment(): GlobalWithMonaco['MonacoEnvironment'] {
|
|
10
|
+
return (globalThis as unknown as GlobalWithMonaco).MonacoEnvironment;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('configureTraverseMonacoWorkers', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
__resetWorkerConfigForTests();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('installs MonacoEnvironment.getWorker on first call', () => {
|
|
19
|
+
expect(getMonacoEnvironment()).toBeUndefined();
|
|
20
|
+
configureTraverseMonacoWorkers({});
|
|
21
|
+
expect(getMonacoEnvironment()?.getWorker).toBeTypeOf('function');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('dispatches the "kusto" label to getKustoWorker', () => {
|
|
25
|
+
const kustoWorker = {} as Worker;
|
|
26
|
+
const editorWorker = {} as Worker;
|
|
27
|
+
const getKusto = vi.fn(() => kustoWorker);
|
|
28
|
+
const getEditor = vi.fn(() => editorWorker);
|
|
29
|
+
configureTraverseMonacoWorkers({ getKustoWorker: getKusto, getEditorWorker: getEditor });
|
|
30
|
+
|
|
31
|
+
const w = getMonacoEnvironment()!.getWorker!('id-1', 'kusto');
|
|
32
|
+
expect(w).toBe(kustoWorker);
|
|
33
|
+
expect(getKusto).toHaveBeenCalledTimes(1);
|
|
34
|
+
expect(getEditor).not.toHaveBeenCalled();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('dispatches any non-kusto label to getEditorWorker', () => {
|
|
38
|
+
const editorWorker = {} as Worker;
|
|
39
|
+
const getEditor = vi.fn(() => editorWorker);
|
|
40
|
+
configureTraverseMonacoWorkers({ getEditorWorker: getEditor });
|
|
41
|
+
|
|
42
|
+
const a = getMonacoEnvironment()!.getWorker!('id-1', 'editorWorkerService');
|
|
43
|
+
const b = getMonacoEnvironment()!.getWorker!('id-2', 'css');
|
|
44
|
+
expect(a).toBe(editorWorker);
|
|
45
|
+
expect(b).toBe(editorWorker);
|
|
46
|
+
expect(getEditor).toHaveBeenCalledTimes(2);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('throws a helpful error when the Kusto worker factory is missing', () => {
|
|
50
|
+
configureTraverseMonacoWorkers({});
|
|
51
|
+
expect(() => getMonacoEnvironment()!.getWorker!('id-1', 'kusto')).toThrow(/Kusto worker factory/);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('throws a helpful error when the editor worker factory is missing and no pre-existing handler is present', () => {
|
|
55
|
+
configureTraverseMonacoWorkers({});
|
|
56
|
+
expect(() => getMonacoEnvironment()!.getWorker!('id-1', 'editorWorkerService'))
|
|
57
|
+
.toThrow(/No Monaco worker factory configured for label "editorWorkerService"/);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('reconfigure swaps the active factories without re-installing the environment', () => {
|
|
61
|
+
const w1 = {} as Worker;
|
|
62
|
+
const w2 = {} as Worker;
|
|
63
|
+
configureTraverseMonacoWorkers({ getKustoWorker: () => w1 });
|
|
64
|
+
const env1 = getMonacoEnvironment();
|
|
65
|
+
configureTraverseMonacoWorkers({ getKustoWorker: () => w2 });
|
|
66
|
+
const env2 = getMonacoEnvironment();
|
|
67
|
+
expect(env1).toBe(env2);
|
|
68
|
+
expect(env2!.getWorker!('id', 'kusto')).toBe(w2);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('falls back to a pre-existing host MonacoEnvironment.getWorker for unknown labels', () => {
|
|
72
|
+
const hostWorker = {} as Worker;
|
|
73
|
+
const hostGetWorker = vi.fn(() => hostWorker);
|
|
74
|
+
(globalThis as unknown as GlobalWithMonaco).MonacoEnvironment = { getWorker: hostGetWorker };
|
|
75
|
+
|
|
76
|
+
configureTraverseMonacoWorkers({});
|
|
77
|
+
const w = getMonacoEnvironment()!.getWorker!('id-1', 'editorWorkerService');
|
|
78
|
+
expect(w).toBe(hostWorker);
|
|
79
|
+
expect(hostGetWorker).toHaveBeenCalledWith('id-1', 'editorWorkerService');
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe('getMonacoWorkerConfigurationError', () => {
|
|
84
|
+
beforeEach(() => {
|
|
85
|
+
__resetWorkerConfigForTests();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('returns an error when configureTraverseMonacoWorkers was never called', () => {
|
|
89
|
+
const err = getMonacoWorkerConfigurationError();
|
|
90
|
+
expect(err).not.toBeNull();
|
|
91
|
+
expect(err!.kind).toBe('missing-kusto-worker');
|
|
92
|
+
expect(err!.summary).toMatch(/not configured/i);
|
|
93
|
+
// The hint must show the consumer the actual snippet to copy.
|
|
94
|
+
expect(err!.hint).toContain('configureTraverseMonacoWorkers');
|
|
95
|
+
expect(err!.hint).toContain('getKustoWorker');
|
|
96
|
+
expect(err!.hint).toContain("'monaco-editor/esm/vs/editor/editor.worker.js?worker'");
|
|
97
|
+
expect(err!.hint).toContain("'@kusto/monaco-kusto/release/esm/kusto.worker.js?worker'");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('returns an error when configure was called but getKustoWorker is missing', () => {
|
|
101
|
+
// Edge case: host calls configure with only an editor worker.
|
|
102
|
+
configureTraverseMonacoWorkers({ getEditorWorker: () => ({}) as Worker });
|
|
103
|
+
const err = getMonacoWorkerConfigurationError();
|
|
104
|
+
expect(err).not.toBeNull();
|
|
105
|
+
expect(err!.kind).toBe('missing-kusto-worker');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('returns missing-editor-worker when getKustoWorker is set but getEditorWorker is missing and no host fallback exists', () => {
|
|
109
|
+
configureTraverseMonacoWorkers({ getKustoWorker: () => ({}) as Worker });
|
|
110
|
+
const err = getMonacoWorkerConfigurationError();
|
|
111
|
+
expect(err).not.toBeNull();
|
|
112
|
+
expect(err!.kind).toBe('missing-editor-worker');
|
|
113
|
+
expect(err!.hint).toContain('getEditorWorker');
|
|
114
|
+
expect(err!.hint).toContain("'monaco-editor/esm/vs/editor/editor.worker.js?worker'");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('does NOT return missing-editor-worker if a host MonacoEnvironment.getWorker was installed BEFORE configure', () => {
|
|
118
|
+
// Host (e.g. another editor wrapper) installed its own getWorker.
|
|
119
|
+
// Configure-with-only-kusto is fine in this case because the host
|
|
120
|
+
// handles non-kusto labels via the fallback chain.
|
|
121
|
+
(globalThis as unknown as GlobalWithMonaco).MonacoEnvironment = {
|
|
122
|
+
getWorker: () => ({}) as Worker,
|
|
123
|
+
};
|
|
124
|
+
configureTraverseMonacoWorkers({ getKustoWorker: () => ({}) as Worker });
|
|
125
|
+
expect(getMonacoWorkerConfigurationError()).toBeNull();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('returns null when both workers are configured', () => {
|
|
129
|
+
configureTraverseMonacoWorkers({
|
|
130
|
+
getEditorWorker: () => ({}) as Worker,
|
|
131
|
+
getKustoWorker: () => ({}) as Worker,
|
|
132
|
+
});
|
|
133
|
+
expect(getMonacoWorkerConfigurationError()).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('does NOT trust a host-installed MonacoEnvironment.getWorker — must be configured via traverse', () => {
|
|
137
|
+
// Pretend the host installed its own getWorker (e.g. for its own
|
|
138
|
+
// non-kusto editors). That doesn't mean kusto is set up.
|
|
139
|
+
(globalThis as unknown as GlobalWithMonaco).MonacoEnvironment = {
|
|
140
|
+
getWorker: () => ({}) as Worker,
|
|
141
|
+
};
|
|
142
|
+
const err = getMonacoWorkerConfigurationError();
|
|
143
|
+
expect(err).not.toBeNull();
|
|
144
|
+
expect(err!.kind).toBe('missing-kusto-worker');
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker factory: returns a fresh `Worker` instance that monaco-editor (or
|
|
3
|
+
* `@kusto/monaco-kusto`) will dispatch messages to.
|
|
4
|
+
*/
|
|
5
|
+
export type MonacoWorkerFactory = () => Worker;
|
|
6
|
+
|
|
7
|
+
export interface MonacoWorkerConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Factory for monaco-editor's base "editor worker service" — required for
|
|
10
|
+
* features like undo coalescing, find/replace, and large-document diffing.
|
|
11
|
+
* Will be invoked for any worker label other than `"kusto"`.
|
|
12
|
+
*/
|
|
13
|
+
getEditorWorker?: MonacoWorkerFactory;
|
|
14
|
+
/**
|
|
15
|
+
* Factory for `@kusto/monaco-kusto`'s language-service worker (parser,
|
|
16
|
+
* binder, IntelliSense, diagnostics). Invoked when Monaco requests a
|
|
17
|
+
* worker with label `"kusto"`.
|
|
18
|
+
*/
|
|
19
|
+
getKustoWorker?: MonacoWorkerFactory;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let currentConfig: MonacoWorkerConfig = {};
|
|
23
|
+
let installed = false;
|
|
24
|
+
let configuredByTraverse = false;
|
|
25
|
+
/** Captured at first install: was there a pre-existing host
|
|
26
|
+
* `MonacoEnvironment.getWorker` we can fall back to for non-kusto labels?
|
|
27
|
+
* Determines whether `getEditorWorker` is strictly required. */
|
|
28
|
+
let hadPreviousHostWorker = false;
|
|
29
|
+
|
|
30
|
+
interface GlobalWithMonaco {
|
|
31
|
+
MonacoEnvironment?: { getWorker?: (workerId: string, label: string) => Worker };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Configure how `@mhjuma/traverse` obtains Monaco web workers. Must be called
|
|
36
|
+
* once, before mounting `<Explorer>`. Bundler-aware hosts (Vite, webpack)
|
|
37
|
+
* typically construct workers via `new Worker(new URL(..., import.meta.url))`
|
|
38
|
+
* or `import Worker from '...?worker'` and pass the factories here.
|
|
39
|
+
*
|
|
40
|
+
* Calling more than once replaces the previous configuration. Subsequent
|
|
41
|
+
* Monaco worker spawns will pick up the new factories.
|
|
42
|
+
*/
|
|
43
|
+
export function configureTraverseMonacoWorkers(config: MonacoWorkerConfig): void {
|
|
44
|
+
currentConfig = config;
|
|
45
|
+
configuredByTraverse = true;
|
|
46
|
+
installMonacoEnvironment();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface MonacoWorkerConfigurationError {
|
|
50
|
+
/**
|
|
51
|
+
* Discriminator:
|
|
52
|
+
* - `'missing-kusto-worker'` — no `getKustoWorker` (or configure was never called).
|
|
53
|
+
* - `'missing-editor-worker'` — `getKustoWorker` is set but `getEditorWorker`
|
|
54
|
+
* is also missing and there is no pre-existing host
|
|
55
|
+
* `MonacoEnvironment.getWorker` to fall back to. Without one of these,
|
|
56
|
+
* Monaco will throw at runtime when it requests the editor worker (e.g.
|
|
57
|
+
* for find-in-buffer, default color detection, etc).
|
|
58
|
+
*/
|
|
59
|
+
kind: 'missing-kusto-worker' | 'missing-editor-worker';
|
|
60
|
+
/** Short summary suitable for the banner heading. */
|
|
61
|
+
summary: string;
|
|
62
|
+
/** Multi-line actionable hint with the exact code the consumer needs. */
|
|
63
|
+
hint: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns `null` when Monaco workers are wired up correctly, otherwise an
|
|
68
|
+
* `Error`-shaped object describing what's missing and how to fix it.
|
|
69
|
+
*
|
|
70
|
+
* We rely on a sentinel (`configuredByTraverse`) rather than inspecting
|
|
71
|
+
* `globalThis.MonacoEnvironment.getWorker` directly. A host may have set
|
|
72
|
+
* `getWorker` for its own non-kusto editors, but if it doesn't route
|
|
73
|
+
* `label === 'kusto'` to a real kusto worker, the kusto language plugin
|
|
74
|
+
* still breaks silently with the cascading errors
|
|
75
|
+
* (`Missing requestHandler or method: normalizeSchema`, etc).
|
|
76
|
+
*
|
|
77
|
+
* Also catches the partial-config case: when `getKustoWorker` is present
|
|
78
|
+
* but `getEditorWorker` is missing AND there's no pre-existing host
|
|
79
|
+
* `MonacoEnvironment.getWorker` to fall back to. Without one of those,
|
|
80
|
+
* Monaco's editor worker requests (find/replace, color detection, large
|
|
81
|
+
* doc diffing) will throw at runtime — same poor UX as the
|
|
82
|
+
* missing-kusto-worker case, just surfaced later.
|
|
83
|
+
*/
|
|
84
|
+
export function getMonacoWorkerConfigurationError(): MonacoWorkerConfigurationError | null {
|
|
85
|
+
if (typeof window === 'undefined') {
|
|
86
|
+
// SSR / Node — no workers to configure.
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
if (!configuredByTraverse || !currentConfig.getKustoWorker) {
|
|
90
|
+
return {
|
|
91
|
+
kind: 'missing-kusto-worker',
|
|
92
|
+
summary: 'Monaco workers are not configured.',
|
|
93
|
+
hint:
|
|
94
|
+
'Call configureTraverseMonacoWorkers({ getEditorWorker, getKustoWorker }) ' +
|
|
95
|
+
'before mounting <Explorer>. The `?worker` import suffix is bundler-specific, ' +
|
|
96
|
+
"so this package can't ship the worker factories itself — each consumer wires " +
|
|
97
|
+
'them through their own bundler.\n\n' +
|
|
98
|
+
'For Vite hosts, add this to a module imported at app startup:\n\n' +
|
|
99
|
+
"import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js?worker';\n" +
|
|
100
|
+
"import KustoWorker from '@kusto/monaco-kusto/release/esm/kusto.worker.js?worker';\n" +
|
|
101
|
+
"import { configureTraverseMonacoWorkers } from '@mhjuma/traverse';\n\n" +
|
|
102
|
+
'configureTraverseMonacoWorkers({\n' +
|
|
103
|
+
' getEditorWorker: () => new EditorWorker(),\n' +
|
|
104
|
+
' getKustoWorker: () => new KustoWorker(),\n' +
|
|
105
|
+
'});',
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
if (!currentConfig.getEditorWorker && !hadPreviousHostWorker) {
|
|
109
|
+
return {
|
|
110
|
+
kind: 'missing-editor-worker',
|
|
111
|
+
summary: 'Monaco editor worker is not configured.',
|
|
112
|
+
hint:
|
|
113
|
+
'configureTraverseMonacoWorkers() was called with `getKustoWorker` but no ' +
|
|
114
|
+
'`getEditorWorker`, and the host has not installed a fallback ' +
|
|
115
|
+
'`globalThis.MonacoEnvironment.getWorker`. Monaco needs an editor worker for ' +
|
|
116
|
+
'features like find/replace, color detection, and large-document diffing — ' +
|
|
117
|
+
'without one it will throw at runtime when those fire.\n\n' +
|
|
118
|
+
'Add `getEditorWorker` alongside your existing `getKustoWorker`:\n\n' +
|
|
119
|
+
"import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker.js?worker';\n\n" +
|
|
120
|
+
'configureTraverseMonacoWorkers({\n' +
|
|
121
|
+
' getEditorWorker: () => new EditorWorker(),\n' +
|
|
122
|
+
' getKustoWorker: /* your existing factory */,\n' +
|
|
123
|
+
'});',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function installMonacoEnvironment(): void {
|
|
130
|
+
if (typeof window === 'undefined') {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (installed) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
installed = true;
|
|
137
|
+
const g = globalThis as unknown as GlobalWithMonaco;
|
|
138
|
+
const previous = g.MonacoEnvironment;
|
|
139
|
+
hadPreviousHostWorker = typeof previous?.getWorker === 'function';
|
|
140
|
+
g.MonacoEnvironment = {
|
|
141
|
+
...(previous ?? {}),
|
|
142
|
+
getWorker(workerId: string, label: string): Worker {
|
|
143
|
+
if (label === 'kusto') {
|
|
144
|
+
if (currentConfig.getKustoWorker) {
|
|
145
|
+
return currentConfig.getKustoWorker();
|
|
146
|
+
}
|
|
147
|
+
throw new Error(
|
|
148
|
+
'[traverse] No Kusto worker factory configured. Call ' +
|
|
149
|
+
'configureTraverseMonacoWorkers({ getKustoWorker }) before mounting <Explorer>.',
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
if (currentConfig.getEditorWorker) {
|
|
153
|
+
return currentConfig.getEditorWorker();
|
|
154
|
+
}
|
|
155
|
+
if (previous?.getWorker) {
|
|
156
|
+
return previous.getWorker(workerId, label);
|
|
157
|
+
}
|
|
158
|
+
throw new Error(
|
|
159
|
+
`[traverse] No Monaco worker factory configured for label "${label}". Call ` +
|
|
160
|
+
'configureTraverseMonacoWorkers({ getEditorWorker }) before mounting <Explorer>.',
|
|
161
|
+
);
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Test-only escape hatch. Resets the module's internal state so each test can
|
|
168
|
+
* exercise the first-install path. Not exported from the package index.
|
|
169
|
+
*/
|
|
170
|
+
export function __resetWorkerConfigForTests(): void {
|
|
171
|
+
currentConfig = {};
|
|
172
|
+
installed = false;
|
|
173
|
+
configuredByTraverse = false;
|
|
174
|
+
hadPreviousHostWorker = false;
|
|
175
|
+
if (typeof window !== 'undefined') {
|
|
176
|
+
const g = globalThis as unknown as GlobalWithMonaco;
|
|
177
|
+
delete g.MonacoEnvironment;
|
|
178
|
+
}
|
|
179
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -14,3 +14,6 @@ export { bootstrapExplorer, useExplorerTheme } from './bootstrap';
|
|
|
14
14
|
export type { BootstrapExplorerOptions } from './bootstrap';
|
|
15
15
|
export { loadSnapshot, saveSnapshot, clearSnapshot, validateAndCleanSnapshot, extractSnapshot, SNAPSHOT_VERSION } from './state/persistence';
|
|
16
16
|
export type { ExplorerTabsSnapshot } from './state/persistence';
|
|
17
|
+
export { configureTraverseMonacoWorkers, getMonacoWorkerConfigurationError } from './components/ExplorerWorkspace/monacoWorkers';
|
|
18
|
+
export type { MonacoWorkerConfig, MonacoWorkerFactory, MonacoWorkerConfigurationError } from './components/ExplorerWorkspace/monacoWorkers';
|
|
19
|
+
export { MonacoConfigErrorBanner } from './components/Explorer/MonacoConfigErrorBanner';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
2
|
|
|
3
|
-
import { extractStats, createKustoClient, parseVisualizationMetadata, buildResultSets } from './kusto';
|
|
3
|
+
import { extractStats, createKustoClient, parseVisualizationMetadata, buildResultSets, parseRetryAfter, abortableSleep } from './kusto';
|
|
4
4
|
import type { QueryStats, KustoClient, KustoStateService, RawTable, KustoVisualization } from './kusto';
|
|
5
5
|
|
|
6
6
|
const KUSTO_CLUSTER = 'https://test-kusto.kusto.windows.net';
|
|
@@ -917,3 +917,157 @@ describe('queryKusto with custom target', () => {
|
|
|
917
917
|
expect(fetchBody.db).toBe('OtherDB');
|
|
918
918
|
});
|
|
919
919
|
});
|
|
920
|
+
|
|
921
|
+
describe('parseRetryAfter', () => {
|
|
922
|
+
const now = Date.parse('2024-01-01T00:00:00Z');
|
|
923
|
+
|
|
924
|
+
it('returns null for absent / empty values', () => {
|
|
925
|
+
expect(parseRetryAfter(null, now)).toBeNull();
|
|
926
|
+
expect(parseRetryAfter(undefined, now)).toBeNull();
|
|
927
|
+
expect(parseRetryAfter('', now)).toBeNull();
|
|
928
|
+
expect(parseRetryAfter(' ', now)).toBeNull();
|
|
929
|
+
});
|
|
930
|
+
|
|
931
|
+
it('parses delta-seconds (integer)', () => {
|
|
932
|
+
expect(parseRetryAfter('5', now)).toBe(5000);
|
|
933
|
+
expect(parseRetryAfter('0', now)).toBe(0);
|
|
934
|
+
});
|
|
935
|
+
|
|
936
|
+
it('parses delta-seconds (decimal)', () => {
|
|
937
|
+
expect(parseRetryAfter('2.5', now)).toBe(2500);
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
it('rejects signed delta-seconds (spec: non-negative integer only)', () => {
|
|
941
|
+
expect(parseRetryAfter('-1', now)).toBeNull();
|
|
942
|
+
expect(parseRetryAfter('+5', now)).toBeNull();
|
|
943
|
+
});
|
|
944
|
+
|
|
945
|
+
it('rejects garbage', () => {
|
|
946
|
+
expect(parseRetryAfter('soon', now)).toBeNull();
|
|
947
|
+
expect(parseRetryAfter('5x', now)).toBeNull();
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
it('parses HTTP-date and returns delta from now', () => {
|
|
951
|
+
const future = new Date(now + 30_000).toUTCString();
|
|
952
|
+
// Allow 1s slack for parsing precision.
|
|
953
|
+
const got = parseRetryAfter(future, now)!;
|
|
954
|
+
expect(got).toBeGreaterThanOrEqual(29_000);
|
|
955
|
+
expect(got).toBeLessThanOrEqual(31_000);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
it('returns 0 for HTTP-date in the past', () => {
|
|
959
|
+
const past = new Date(now - 30_000).toUTCString();
|
|
960
|
+
expect(parseRetryAfter(past, now)).toBe(0);
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
it('caps very large values at 60s', () => {
|
|
964
|
+
expect(parseRetryAfter('3600', now)).toBe(60_000);
|
|
965
|
+
const far = new Date(now + 24 * 3600 * 1000).toUTCString();
|
|
966
|
+
expect(parseRetryAfter(far, now)).toBe(60_000);
|
|
967
|
+
});
|
|
968
|
+
});
|
|
969
|
+
|
|
970
|
+
describe('abortableSleep', () => {
|
|
971
|
+
it('resolves after the specified delay', async () => {
|
|
972
|
+
const start = Date.now();
|
|
973
|
+
await abortableSleep(20);
|
|
974
|
+
expect(Date.now() - start).toBeGreaterThanOrEqual(15);
|
|
975
|
+
});
|
|
976
|
+
|
|
977
|
+
it('rejects immediately with AbortError when signal is already aborted', async () => {
|
|
978
|
+
const ctrl = new AbortController();
|
|
979
|
+
ctrl.abort();
|
|
980
|
+
await expect(abortableSleep(100, ctrl.signal)).rejects.toMatchObject({ name: 'AbortError' });
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
it('rejects with AbortError when signal aborts mid-sleep', async () => {
|
|
984
|
+
const ctrl = new AbortController();
|
|
985
|
+
const p = abortableSleep(500, ctrl.signal);
|
|
986
|
+
setTimeout(() => ctrl.abort(), 10);
|
|
987
|
+
await expect(p).rejects.toMatchObject({ name: 'AbortError' });
|
|
988
|
+
});
|
|
989
|
+
});
|
|
990
|
+
|
|
991
|
+
describe('createKustoClient with custom fetch', () => {
|
|
992
|
+
it('routes requests through the injected fetch implementation', async () => {
|
|
993
|
+
const stateService = createMockStateService();
|
|
994
|
+
const customFetch = vi.fn().mockResolvedValue({
|
|
995
|
+
ok: true,
|
|
996
|
+
status: 200,
|
|
997
|
+
json: () => Promise.resolve({
|
|
998
|
+
Tables: [
|
|
999
|
+
{ Columns: [{ ColumnName: 'Id', ColumnType: 'long' }], Rows: [[1]] },
|
|
1000
|
+
{ Columns: [{ ColumnName: 'Kind' }, { ColumnName: 'Ordinal' }], Rows: [['QueryResult', 0]] },
|
|
1001
|
+
],
|
|
1002
|
+
}),
|
|
1003
|
+
text: () => Promise.resolve(''),
|
|
1004
|
+
});
|
|
1005
|
+
// Don't stub global fetch — verify the custom one was used.
|
|
1006
|
+
vi.stubGlobal('fetch', vi.fn(() => { throw new Error('global fetch should NOT be called'); }));
|
|
1007
|
+
const client = createKustoClient({
|
|
1008
|
+
defaultTarget: { clusterUrl: KUSTO_CLUSTER, database: KUSTO_DATABASE },
|
|
1009
|
+
getToken: vi.fn().mockResolvedValue('mock-kusto-token'),
|
|
1010
|
+
stateService,
|
|
1011
|
+
fetch: customFetch,
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
await client.queryKusto('Tbl | take 1');
|
|
1015
|
+
expect(customFetch).toHaveBeenCalledTimes(1);
|
|
1016
|
+
});
|
|
1017
|
+
});
|
|
1018
|
+
|
|
1019
|
+
describe('fetchWithRetry honours Retry-After (via 429/503)', () => {
|
|
1020
|
+
let realDate: typeof Date.now;
|
|
1021
|
+
beforeEach(() => { realDate = Date.now; });
|
|
1022
|
+
afterEach(() => { Date.now = realDate; });
|
|
1023
|
+
|
|
1024
|
+
function makeResp(status: number, retryAfter?: string): Response {
|
|
1025
|
+
return {
|
|
1026
|
+
ok: status >= 200 && status < 300,
|
|
1027
|
+
status,
|
|
1028
|
+
headers: {
|
|
1029
|
+
get: (name: string) => (name.toLowerCase() === 'retry-after' && retryAfter !== undefined) ? retryAfter : null,
|
|
1030
|
+
},
|
|
1031
|
+
text: () => Promise.resolve(''),
|
|
1032
|
+
json: () => Promise.resolve({
|
|
1033
|
+
Tables: [
|
|
1034
|
+
{ Columns: [{ ColumnName: 'Id', ColumnType: 'long' }], Rows: [[1]] },
|
|
1035
|
+
{ Columns: [{ ColumnName: 'Kind' }, { ColumnName: 'Ordinal' }], Rows: [['QueryResult', 0]] },
|
|
1036
|
+
],
|
|
1037
|
+
}),
|
|
1038
|
+
} as unknown as Response;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
it('waits Retry-After seconds before retrying on 429', async () => {
|
|
1042
|
+
const stateService = createMockStateService();
|
|
1043
|
+
// 1st call → 429 with Retry-After: 0 (so test runs fast), 2nd → 200.
|
|
1044
|
+
const fetchImpl = vi.fn()
|
|
1045
|
+
.mockResolvedValueOnce(makeResp(429, '0'))
|
|
1046
|
+
.mockResolvedValueOnce(makeResp(200));
|
|
1047
|
+
const client = createKustoClient({
|
|
1048
|
+
defaultTarget: { clusterUrl: KUSTO_CLUSTER, database: KUSTO_DATABASE },
|
|
1049
|
+
getToken: vi.fn().mockResolvedValue('mock-kusto-token'),
|
|
1050
|
+
stateService,
|
|
1051
|
+
fetch: fetchImpl as unknown as typeof fetch,
|
|
1052
|
+
});
|
|
1053
|
+
|
|
1054
|
+
await client.queryKusto('Tbl | take 1');
|
|
1055
|
+
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
it('also retries on 503', async () => {
|
|
1059
|
+
const stateService = createMockStateService();
|
|
1060
|
+
const fetchImpl = vi.fn()
|
|
1061
|
+
.mockResolvedValueOnce(makeResp(503, '0'))
|
|
1062
|
+
.mockResolvedValueOnce(makeResp(200));
|
|
1063
|
+
const client = createKustoClient({
|
|
1064
|
+
defaultTarget: { clusterUrl: KUSTO_CLUSTER, database: KUSTO_DATABASE },
|
|
1065
|
+
getToken: vi.fn().mockResolvedValue('mock-kusto-token'),
|
|
1066
|
+
stateService,
|
|
1067
|
+
fetch: fetchImpl as unknown as typeof fetch,
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
await client.queryKusto('Tbl | take 1');
|
|
1071
|
+
expect(fetchImpl).toHaveBeenCalledTimes(2);
|
|
1072
|
+
});
|
|
1073
|
+
});
|