sendproof-core 0.1.0.alpha.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.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +55 -0
- data/lib/sendproof/assets.rb +70 -0
- data/lib/sendproof/attribution.rb +98 -0
- data/lib/sendproof/capture.rb +66 -0
- data/lib/sendproof/clock.rb +54 -0
- data/lib/sendproof/configuration.rb +241 -0
- data/lib/sendproof/data_store.rb +34 -0
- data/lib/sendproof/dynamodb/store.rb +183 -0
- data/lib/sendproof/dynamodb.rb +17 -0
- data/lib/sendproof/elasticsearch/store.rb +24 -0
- data/lib/sendproof/elasticsearch.rb +17 -0
- data/lib/sendproof/firestore/interceptor.rb +38 -0
- data/lib/sendproof/firestore/store.rb +150 -0
- data/lib/sendproof/firestore.rb +99 -0
- data/lib/sendproof/handler.rb +23 -0
- data/lib/sendproof/identity.rb +37 -0
- data/lib/sendproof/limits.rb +113 -0
- data/lib/sendproof/middleware.rb +68 -0
- data/lib/sendproof/mongo/engine.rb +692 -0
- data/lib/sendproof/mongo/guarded.rb +100 -0
- data/lib/sendproof/mongo/plain.rb +45 -0
- data/lib/sendproof/mongo/store.rb +54 -0
- data/lib/sendproof/mongo/subscriber.rb +21 -0
- data/lib/sendproof/mongo.rb +41 -0
- data/lib/sendproof/mongoid/store.rb +58 -0
- data/lib/sendproof/mongoid.rb +65 -0
- data/lib/sendproof/observed_request.rb +108 -0
- data/lib/sendproof/opensearch/store.rb +21 -0
- data/lib/sendproof/opensearch.rb +17 -0
- data/lib/sendproof/operation_matcher.rb +28 -0
- data/lib/sendproof/pool_hooks.rb +80 -0
- data/lib/sendproof/preview_request.rb +44 -0
- data/lib/sendproof/preview_transport.rb +331 -0
- data/lib/sendproof/protocol.rb +81 -0
- data/lib/sendproof/provider.rb +83 -0
- data/lib/sendproof/recorder.rb +298 -0
- data/lib/sendproof/redaction.rb +98 -0
- data/lib/sendproof/redis/commands.rb +76 -0
- data/lib/sendproof/redis/store.rb +205 -0
- data/lib/sendproof/redis.rb +18 -0
- data/lib/sendproof/search/middleware.rb +39 -0
- data/lib/sendproof/search/store.rb +213 -0
- data/lib/sendproof/search.rb +82 -0
- data/lib/sendproof/stores/classify.rb +346 -0
- data/lib/sendproof/stores/guard.rb +127 -0
- data/lib/sendproof/stores/tables.rb +1328 -0
- data/lib/sendproof/stores.rb +12 -0
- data/lib/sendproof/trace_store.rb +110 -0
- data/lib/sendproof/transaction_control.rb +187 -0
- data/lib/sendproof/ui/action-bar.js +91 -0
- data/lib/sendproof/ui/api.js +239 -0
- data/lib/sendproof/ui/app.css +848 -0
- data/lib/sendproof/ui/app.js +742 -0
- data/lib/sendproof/ui/autofill.js +137 -0
- data/lib/sendproof/ui/code-copy.js +71 -0
- data/lib/sendproof/ui/code-picker.js +149 -0
- data/lib/sendproof/ui/commands.js +197 -0
- data/lib/sendproof/ui/components.js +77 -0
- data/lib/sendproof/ui/context-bar.js +82 -0
- data/lib/sendproof/ui/copy.js +92 -0
- data/lib/sendproof/ui/coverage-popover.js +35 -0
- data/lib/sendproof/ui/crumb-fit.js +80 -0
- data/lib/sendproof/ui/deprecation.js +164 -0
- data/lib/sendproof/ui/display-settings.js +175 -0
- data/lib/sendproof/ui/doc-diff.js +209 -0
- data/lib/sendproof/ui/docs-view.js +347 -0
- data/lib/sendproof/ui/favicon.png +0 -0
- data/lib/sendproof/ui/favicon.svg +1 -0
- data/lib/sendproof/ui/field-help.js +52 -0
- data/lib/sendproof/ui/field-modules.js +23 -0
- data/lib/sendproof/ui/fields/controls.js +179 -0
- data/lib/sendproof/ui/fields/dom.js +36 -0
- data/lib/sendproof/ui/fields/infer.js +71 -0
- data/lib/sendproof/ui/fields/lookup-source.js +63 -0
- data/lib/sendproof/ui/fields/lookup.js +285 -0
- data/lib/sendproof/ui/fields/render.js +496 -0
- data/lib/sendproof/ui/fields/select.js +175 -0
- data/lib/sendproof/ui/fields/validate.js +363 -0
- data/lib/sendproof/ui/fields/validation-formats.js +164 -0
- data/lib/sendproof/ui/fields/validation-references.js +68 -0
- data/lib/sendproof/ui/fields/validation-values.js +137 -0
- data/lib/sendproof/ui/generate-menu.js +44 -0
- data/lib/sendproof/ui/generate.js +184 -0
- data/lib/sendproof/ui/header-editor.js +777 -0
- data/lib/sendproof/ui/headers.js +188 -0
- data/lib/sendproof/ui/highlight.js +257 -0
- data/lib/sendproof/ui/history-popover.js +24 -0
- data/lib/sendproof/ui/i18n.js +253 -0
- data/lib/sendproof/ui/icons.js +87 -0
- data/lib/sendproof/ui/index.html +70 -0
- data/lib/sendproof/ui/issues.js +187 -0
- data/lib/sendproof/ui/json-view.js +166 -0
- data/lib/sendproof/ui/lang-icons.js +62 -0
- data/lib/sendproof/ui/layout.js +262 -0
- data/lib/sendproof/ui/listbox.js +334 -0
- data/lib/sendproof/ui/locales/ar-capture.js +366 -0
- data/lib/sendproof/ui/locales/ar-stores.js +116 -0
- data/lib/sendproof/ui/locales/ar.js +561 -0
- data/lib/sendproof/ui/locales/cs-capture.js +442 -0
- data/lib/sendproof/ui/locales/cs-stores.js +116 -0
- data/lib/sendproof/ui/locales/cs.js +689 -0
- data/lib/sendproof/ui/locales/da-capture.js +347 -0
- data/lib/sendproof/ui/locales/da-stores.js +116 -0
- data/lib/sendproof/ui/locales/da.js +522 -0
- data/lib/sendproof/ui/locales/de-capture.js +347 -0
- data/lib/sendproof/ui/locales/de-stores.js +116 -0
- data/lib/sendproof/ui/locales/de.js +519 -0
- data/lib/sendproof/ui/locales/el-capture.js +347 -0
- data/lib/sendproof/ui/locales/el-stores.js +116 -0
- data/lib/sendproof/ui/locales/el.js +520 -0
- data/lib/sendproof/ui/locales/en-capture.js +349 -0
- data/lib/sendproof/ui/locales/en-stores.js +95 -0
- data/lib/sendproof/ui/locales/en.js +522 -0
- data/lib/sendproof/ui/locales/es-capture.js +353 -0
- data/lib/sendproof/ui/locales/es-stores.js +116 -0
- data/lib/sendproof/ui/locales/es.js +525 -0
- data/lib/sendproof/ui/locales/fa-capture.js +348 -0
- data/lib/sendproof/ui/locales/fa-stores.js +116 -0
- data/lib/sendproof/ui/locales/fa.js +521 -0
- data/lib/sendproof/ui/locales/fi-capture.js +347 -0
- data/lib/sendproof/ui/locales/fi-stores.js +116 -0
- data/lib/sendproof/ui/locales/fi.js +523 -0
- data/lib/sendproof/ui/locales/fr-capture.js +353 -0
- data/lib/sendproof/ui/locales/fr-stores.js +116 -0
- data/lib/sendproof/ui/locales/fr.js +525 -0
- data/lib/sendproof/ui/locales/he-capture.js +358 -0
- data/lib/sendproof/ui/locales/he-stores.js +116 -0
- data/lib/sendproof/ui/locales/he.js +529 -0
- data/lib/sendproof/ui/locales/hi-capture.js +348 -0
- data/lib/sendproof/ui/locales/hi-stores.js +116 -0
- data/lib/sendproof/ui/locales/hi.js +523 -0
- data/lib/sendproof/ui/locales/id-capture.js +347 -0
- data/lib/sendproof/ui/locales/id-stores.js +116 -0
- data/lib/sendproof/ui/locales/id.js +521 -0
- data/lib/sendproof/ui/locales/it-capture.js +361 -0
- data/lib/sendproof/ui/locales/it-stores.js +116 -0
- data/lib/sendproof/ui/locales/it.js +523 -0
- data/lib/sendproof/ui/locales/ja-capture.js +346 -0
- data/lib/sendproof/ui/locales/ja-stores.js +116 -0
- data/lib/sendproof/ui/locales/ja.js +521 -0
- data/lib/sendproof/ui/locales/ko-capture.js +346 -0
- data/lib/sendproof/ui/locales/ko-stores.js +116 -0
- data/lib/sendproof/ui/locales/ko.js +522 -0
- data/lib/sendproof/ui/locales/nb-capture.js +349 -0
- data/lib/sendproof/ui/locales/nb-stores.js +116 -0
- data/lib/sendproof/ui/locales/nb.js +521 -0
- data/lib/sendproof/ui/locales/nl-capture.js +355 -0
- data/lib/sendproof/ui/locales/nl-stores.js +116 -0
- data/lib/sendproof/ui/locales/nl.js +520 -0
- data/lib/sendproof/ui/locales/pl-capture.js +432 -0
- data/lib/sendproof/ui/locales/pl-stores.js +116 -0
- data/lib/sendproof/ui/locales/pl.js +528 -0
- data/lib/sendproof/ui/locales/pt-BR-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-BR-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-BR.js +523 -0
- data/lib/sendproof/ui/locales/pt-PT-capture.js +350 -0
- data/lib/sendproof/ui/locales/pt-PT-stores.js +116 -0
- data/lib/sendproof/ui/locales/pt-PT.js +523 -0
- data/lib/sendproof/ui/locales/ru-capture.js +437 -0
- data/lib/sendproof/ui/locales/ru-stores.js +116 -0
- data/lib/sendproof/ui/locales/ru.js +624 -0
- data/lib/sendproof/ui/locales/sv-capture.js +349 -0
- data/lib/sendproof/ui/locales/sv-stores.js +116 -0
- data/lib/sendproof/ui/locales/sv.js +518 -0
- data/lib/sendproof/ui/locales/th-capture.js +347 -0
- data/lib/sendproof/ui/locales/th-stores.js +116 -0
- data/lib/sendproof/ui/locales/th.js +521 -0
- data/lib/sendproof/ui/locales/tr-capture.js +348 -0
- data/lib/sendproof/ui/locales/tr-stores.js +116 -0
- data/lib/sendproof/ui/locales/tr.js +521 -0
- data/lib/sendproof/ui/locales/uk-capture.js +442 -0
- data/lib/sendproof/ui/locales/uk-stores.js +116 -0
- data/lib/sendproof/ui/locales/uk.js +630 -0
- data/lib/sendproof/ui/locales/vi-capture.js +346 -0
- data/lib/sendproof/ui/locales/vi-stores.js +116 -0
- data/lib/sendproof/ui/locales/vi.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hans-capture.js +347 -0
- data/lib/sendproof/ui/locales/zh-Hans-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hans.js +523 -0
- data/lib/sendproof/ui/locales/zh-Hant-capture.js +346 -0
- data/lib/sendproof/ui/locales/zh-Hant-stores.js +116 -0
- data/lib/sendproof/ui/locales/zh-Hant.js +522 -0
- data/lib/sendproof/ui/manifest.json +866 -0
- data/lib/sendproof/ui/markdown.js +39 -0
- data/lib/sendproof/ui/model-view.js +182 -0
- data/lib/sendproof/ui/navigation.js +233 -0
- data/lib/sendproof/ui/operation-header.js +76 -0
- data/lib/sendproof/ui/operations.js +7 -0
- data/lib/sendproof/ui/overview-view.js +63 -0
- data/lib/sendproof/ui/personas.js +59 -0
- data/lib/sendproof/ui/popover.js +71 -0
- data/lib/sendproof/ui/preview-false.js +108 -0
- data/lib/sendproof/ui/request-editor.js +496 -0
- data/lib/sendproof/ui/response-metadata.js +354 -0
- data/lib/sendproof/ui/response-viewer.js +618 -0
- data/lib/sendproof/ui/result-blocked.js +155 -0
- data/lib/sendproof/ui/result-capture.js +25 -0
- data/lib/sendproof/ui/result-database.js +185 -0
- data/lib/sendproof/ui/result-effects.js +176 -0
- data/lib/sendproof/ui/result-headers.js +106 -0
- data/lib/sendproof/ui/result-logs.js +408 -0
- data/lib/sendproof/ui/result-states.js +213 -0
- data/lib/sendproof/ui/results.js +371 -0
- data/lib/sendproof/ui/router.js +118 -0
- data/lib/sendproof/ui/schema-model.js +336 -0
- data/lib/sendproof/ui/schema-tree.js +344 -0
- data/lib/sendproof/ui/schema.js +397 -0
- data/lib/sendproof/ui/security.js +31 -0
- data/lib/sendproof/ui/seeded-menu.js +69 -0
- data/lib/sendproof/ui/settings.js +93 -0
- data/lib/sendproof/ui/shortcuts-sheet.js +73 -0
- data/lib/sendproof/ui/shortcuts.js +190 -0
- data/lib/sendproof/ui/sidebar.js +289 -0
- data/lib/sendproof/ui/signin.js +259 -0
- data/lib/sendproof/ui/snippets.js +530 -0
- data/lib/sendproof/ui/store-marks.js +47 -0
- data/lib/sendproof/ui/stores.js +131 -0
- data/lib/sendproof/ui/styles.css +971 -0
- data/lib/sendproof/ui/toggletip.js +53 -0
- data/lib/sendproof/ui/uuid.js +10 -0
- data/lib/sendproof/ui/view.js +122 -0
- data/lib/sendproof/ui/views.css +653 -0
- data/lib/sendproof/version.rb +9 -0
- data/lib/sendproof/workbench.rb +102 -0
- data/lib/sendproof-core.rb +3 -0
- data/lib/sendproof.rb +121 -0
- metadata +292 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Whether the host lets this browser use its `personas` (seeded accounts) source. A host may
|
|
2
|
+
// refuse it, for example the Acme Store example answers 403 when the workbench is opened from another
|
|
3
|
+
// machine. A refusal (401, 403, 404) means "this option does not exist here": every place that
|
|
4
|
+
// offers seeded accounts hides it and points to password sign-in instead, with no error. Any other
|
|
5
|
+
// failure (network error, 5xx) says nothing about access, so those places keep their own handling.
|
|
6
|
+
//
|
|
7
|
+
// The answer is found once while the workbench boots, before the first paint, so nothing appears
|
|
8
|
+
// and then disappears.
|
|
9
|
+
|
|
10
|
+
/** Statuses that mean the seeded-account list is not offered to this caller. */
|
|
11
|
+
const REFUSALS = new Set([401, 403, 404]);
|
|
12
|
+
|
|
13
|
+
/** True when `status` is a refusal rather than a failure. */
|
|
14
|
+
export const isPersonaRefusal = status => REFUSALS.has(status);
|
|
15
|
+
|
|
16
|
+
/** The personas provider advertised by discovery, if any. */
|
|
17
|
+
export const personasProvider = capabilities =>
|
|
18
|
+
(capabilities?.authProviders || []).find(provider => provider.kind?.toLowerCase() === 'personas' && provider.url);
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Asks the personas source whether it answers this caller. Resolves to `'available'` (2xx),
|
|
22
|
+
* `'refused'` (401, 403 or 404) or `'unknown'` (network error, timeout, 5xx or anything else).
|
|
23
|
+
* It never rejects, and gives up after `timeout` ms so a slow host cannot hold the first paint.
|
|
24
|
+
*/
|
|
25
|
+
export async function probePersonas(provider, { timeout = 3000, signal } = {}) {
|
|
26
|
+
const controller = new AbortController();
|
|
27
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
28
|
+
signal?.addEventListener?.('abort', () => controller.abort(), { once: true });
|
|
29
|
+
try {
|
|
30
|
+
const response = await fetch(provider.url, { credentials: 'include', redirect: 'error', signal: controller.signal });
|
|
31
|
+
if (response.ok) return 'available';
|
|
32
|
+
return isPersonaRefusal(response.status) ? 'refused' : 'unknown';
|
|
33
|
+
} catch {
|
|
34
|
+
return 'unknown';
|
|
35
|
+
} finally {
|
|
36
|
+
clearTimeout(timer);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Returns the capabilities to use for the session. When the host refuses the personas source the
|
|
42
|
+
* provider is removed from `authProviders` (so the sidebar notice, the request editor, the
|
|
43
|
+
* sign-in page and the seeded menu all stop offering it) and `personasRefused` is set so the
|
|
44
|
+
* sign-in page can say where to go instead. Otherwise the capabilities are returned unchanged.
|
|
45
|
+
*/
|
|
46
|
+
export async function resolvePersonaAccess(capabilities, options) {
|
|
47
|
+
const provider = personasProvider(capabilities);
|
|
48
|
+
if (!provider) return capabilities;
|
|
49
|
+
return withPersonasRefused(capabilities, (await probePersonas(provider, options)) === 'refused');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Removes the personas provider from `capabilities` when `refused`, and says so. */
|
|
53
|
+
export function withPersonasRefused(capabilities, refused = true) {
|
|
54
|
+
if (!refused) return capabilities;
|
|
55
|
+
return {
|
|
56
|
+
...capabilities, personasRefused: true,
|
|
57
|
+
authProviders: (capabilities.authProviders || []).filter(provider => provider.kind?.toLowerCase() !== 'personas'),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Anchored popovers: Display settings, History, field toggletips and the Generate menu. One is
|
|
2
|
+
// open at a time. It is placed below its anchor (or above when there isn't room), stays inside
|
|
3
|
+
// the viewport, closes on Esc or a click outside, and returns focus to its opener.
|
|
4
|
+
let open;
|
|
5
|
+
|
|
6
|
+
/** Positions a fixed popover next to an anchor, mirroring the mock's placement. */
|
|
7
|
+
export function place(anchor, pop, { align = 'start', above = false, gap = 6, arrow = false } = {}) {
|
|
8
|
+
const a = anchor.getBoundingClientRect();
|
|
9
|
+
const rtl = document.documentElement.dir === 'rtl';
|
|
10
|
+
pop.style.position = 'fixed';
|
|
11
|
+
pop.style.insetInlineStart = 'auto';
|
|
12
|
+
const width = pop.offsetWidth;
|
|
13
|
+
let left = align === 'end' ? (rtl ? a.left : a.right - width) : align === 'center' ? a.left + a.width / 2 - width / 2 : (rtl ? a.right - width : a.left);
|
|
14
|
+
left = Math.max(12, Math.min(left, innerWidth - width - 12));
|
|
15
|
+
pop.style.left = left + 'px';
|
|
16
|
+
pop.classList.remove('is-above');
|
|
17
|
+
pop.style.top = pop.style.bottom = '';
|
|
18
|
+
if (!above && a.bottom + gap + pop.offsetHeight > innerHeight - 12 && a.top - gap - pop.offsetHeight > 12) above = true;
|
|
19
|
+
if (above) { pop.classList.add('is-above'); pop.style.bottom = (innerHeight - a.top + gap) + 'px'; }
|
|
20
|
+
else pop.style.top = (a.bottom + gap) + 'px';
|
|
21
|
+
if (arrow) pop.style.setProperty('--arrow', (rtl ? left + width - (a.left + a.width / 2) - 6 : a.left + a.width / 2 - left - 6) + 'px');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Closes the open popover. `restore` puts focus back on its opener. */
|
|
25
|
+
export function closePopover(restore = false) {
|
|
26
|
+
if (!open) return;
|
|
27
|
+
const { pop, anchor, onClose } = open;
|
|
28
|
+
open = undefined;
|
|
29
|
+
pop.remove();
|
|
30
|
+
anchor?.setAttribute('aria-expanded', 'false');
|
|
31
|
+
onClose?.();
|
|
32
|
+
if (restore) anchor?.focus();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The popover that is open now, if any. */
|
|
36
|
+
export const openPopover = () => open;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Opens `html` anchored to `anchor`. Returns the popover element. `hover` popovers (toggletips
|
|
40
|
+
* opened by pointer) close when the pointer leaves both the anchor and the popover.
|
|
41
|
+
*/
|
|
42
|
+
export function openPopoverAt(anchor, html, { align, above, gap, arrow, onClose, focus = true, kind = '' } = {}) {
|
|
43
|
+
closePopover();
|
|
44
|
+
const template = document.createElement('template');
|
|
45
|
+
template.innerHTML = html.trim();
|
|
46
|
+
const pop = template.content.firstElementChild;
|
|
47
|
+
pop.dataset.popover = kind;
|
|
48
|
+
document.body.append(pop);
|
|
49
|
+
place(anchor, pop, { align, above, gap, arrow });
|
|
50
|
+
anchor.setAttribute('aria-expanded', 'true');
|
|
51
|
+
open = { pop, anchor, onClose, kind };
|
|
52
|
+
if (focus) (pop.querySelector('[autofocus], [aria-checked="true"], [aria-selected="true"], .is-active, button, select, input, a[href]') || pop).focus?.();
|
|
53
|
+
return pop;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Global listeners are added once, in a browser only (unit tests import this module in Node).
|
|
57
|
+
if (typeof document !== 'undefined') {
|
|
58
|
+
document.addEventListener('keydown', event => {
|
|
59
|
+
if (open && event.key === 'Escape' && !event.defaultPrevented) {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
event.stopPropagation();
|
|
62
|
+
closePopover(true);
|
|
63
|
+
}
|
|
64
|
+
}, true);
|
|
65
|
+
document.addEventListener('pointerdown', event => {
|
|
66
|
+
// A listbox opened from inside this popover (the language picker) is a layer above it.
|
|
67
|
+
if (event.target.closest?.('[data-layer]')) return;
|
|
68
|
+
if (open && !open.pop.contains(event.target) && !open.anchor.contains(event.target)) closePopover();
|
|
69
|
+
}, true);
|
|
70
|
+
addEventListener('resize', () => { if (open) place(open.anchor, open.pop); });
|
|
71
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// The "mark this route preview: false" snippet, in the language of the host application.
|
|
2
|
+
//
|
|
3
|
+
// An API author marks an operation `preview: false` so Preview explains itself before anyone runs
|
|
4
|
+
// it (spec/data-stores.md). Every binding spells that mark in its own idiom, taken from the
|
|
5
|
+
// binding's route metadata (docs/plans/data-stores.md, adapters/*). Capabilities `binding`
|
|
6
|
+
// ({ language, framework }) says which binding serves the page, so the workbench writes the one
|
|
7
|
+
// snippet that applies; without it the 409 result offers a small language switch instead.
|
|
8
|
+
import { authorReason } from './stores.js';
|
|
9
|
+
|
|
10
|
+
const FALLBACK_REASON = 'Writes to a store that can’t roll back';
|
|
11
|
+
|
|
12
|
+
/** The bindings' language ids (spec/capabilities.schema.json `binding.language`) with their display names. */
|
|
13
|
+
export const PREVIEW_FALSE_LANGUAGES = [
|
|
14
|
+
{ id: 'dotnet', name: '.NET' },
|
|
15
|
+
{ id: 'node', name: 'Node.js' },
|
|
16
|
+
{ id: 'python', name: 'Python' },
|
|
17
|
+
{ id: 'java', name: 'Java' },
|
|
18
|
+
{ id: 'kotlin', name: 'Kotlin' },
|
|
19
|
+
{ id: 'go', name: 'Go' },
|
|
20
|
+
{ id: 'php', name: 'PHP' },
|
|
21
|
+
{ id: 'ruby', name: 'Ruby' },
|
|
22
|
+
{ id: 'rust', name: 'Rust' },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/** What the switch shows first when discovery names no binding and nothing was chosen before. */
|
|
26
|
+
export const DEFAULT_LANGUAGE = 'dotnet';
|
|
27
|
+
|
|
28
|
+
const STORAGE_KEY = 'sendproof-preview-false-language';
|
|
29
|
+
const known = id => PREVIEW_FALSE_LANGUAGES.some(language => language.id === id);
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The host's binding from capabilities: `{ language, framework }` when `binding.language` is one
|
|
33
|
+
* the workbench knows, otherwise null (an older binding, or a language added later). `framework`
|
|
34
|
+
* is the binding's own free-text name for the web framework ("ASP.NET Core", "Express"), or ''.
|
|
35
|
+
*/
|
|
36
|
+
export function bindingOf(capabilities) {
|
|
37
|
+
const binding = capabilities?.binding;
|
|
38
|
+
if (!binding || typeof binding !== 'object' || !known(binding.language)) return null;
|
|
39
|
+
return { language: binding.language, framework: typeof binding.framework === 'string' ? binding.framework.trim() : '' };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The language the switch starts on: the one chosen before, else .NET (the first binding). */
|
|
43
|
+
export function chosenLanguage() {
|
|
44
|
+
try {
|
|
45
|
+
const stored = globalThis.localStorage?.getItem(STORAGE_KEY);
|
|
46
|
+
return known(stored) ? stored : DEFAULT_LANGUAGE;
|
|
47
|
+
} catch {
|
|
48
|
+
return DEFAULT_LANGUAGE; // Storage can be blocked; the choice then lasts only for this page.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Remembers the switch's language for the next 409. */
|
|
53
|
+
export function chooseLanguage(id) {
|
|
54
|
+
if (!known(id)) return;
|
|
55
|
+
try { globalThis.localStorage?.setItem(STORAGE_KEY, id); } catch { /* Blocked storage: keep going. */ }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The display name of a language id. */
|
|
59
|
+
export const languageName = id => PREVIEW_FALSE_LANGUAGES.find(language => language.id === id)?.name ?? id;
|
|
60
|
+
|
|
61
|
+
// String literals, as each language reads them. A double-quoted literal escapes backslash, quote
|
|
62
|
+
// and newline, and `$` (Kotlin templates) or `#{` (Ruby interpolation) where the language has them.
|
|
63
|
+
const double = (text, { dollar = false, hash = false } = {}) => {
|
|
64
|
+
let out = JSON.stringify(text);
|
|
65
|
+
if (dollar) out = out.replace(/\$/g, '\\$');
|
|
66
|
+
if (hash) out = out.replace(/#\{/g, '\\#{');
|
|
67
|
+
return out;
|
|
68
|
+
};
|
|
69
|
+
const single = text => `'${String(text).replace(/\\/g, '\\\\').replace(/'/g, "\\'").replace(/\r?\n/g, '\\n')}'`;
|
|
70
|
+
|
|
71
|
+
const has = (framework, pattern) => pattern.test(framework || '');
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* The snippet that marks one operation `preview: false` with a reason, for one language.
|
|
75
|
+
* `framework` picks between a language's frameworks where they differ (Express and Fastify, Laravel
|
|
76
|
+
* and Symfony, minimal APIs and MVC); `method` and `path` (the OpenAPI template) fill the bindings
|
|
77
|
+
* that name the route in the mark, and read `POST /path` placeholders when the operation is unknown.
|
|
78
|
+
* @param {{ language?: string, framework?: string, reason?: string, method?: string, path?: string }} options
|
|
79
|
+
*/
|
|
80
|
+
export function previewFalseSnippet({ language = DEFAULT_LANGUAGE, framework = '', reason, method, path } = {}) {
|
|
81
|
+
const why = authorReason(reason) || FALLBACK_REASON;
|
|
82
|
+
const verb = String(method || 'POST').toUpperCase();
|
|
83
|
+
const route = path || '/path';
|
|
84
|
+
switch (language) {
|
|
85
|
+
case 'node':
|
|
86
|
+
if (has(framework, /nest/i)) return `@SendproofPreview(${single(verb)}, ${single(route)}, { enabled: false, reason: ${single(why)} })`;
|
|
87
|
+
if (has(framework, /fastify/i)) return `app.sendproof.unavailable(${single(verb)}, ${single(route)}, { reason: ${single(why)} }, handler);`;
|
|
88
|
+
return `bench.unavailable(${single(verb)}, ${single(route)}, { reason: ${single(why)} }, handler);`;
|
|
89
|
+
case 'python': return `@preview(False, reason=${double(why)})`;
|
|
90
|
+
case 'java': return `@SendproofPreview(enabled = false, reason = ${double(why)})`;
|
|
91
|
+
case 'kotlin': return `sendproofPreview(enabled = false, reason = ${double(why, { dollar: true })}) { ${verb.toLowerCase()}(${double(route, { dollar: true })}) { … } }`;
|
|
92
|
+
case 'go':
|
|
93
|
+
// net/http registers the route and marks it; chi, Gin and Echo register their own routes, so the mark is separate.
|
|
94
|
+
return has(framework, /chi|gin|echo/i)
|
|
95
|
+
? `bench.Unavailable(${double(verb)}, ${double(route)}, ${double(why)})`
|
|
96
|
+
: `bench.HandleUnavailable(mux, ${double(`${verb} ${route}`)}, ${double(why)}, handler)`;
|
|
97
|
+
case 'php':
|
|
98
|
+
return has(framework, /symfony/i)
|
|
99
|
+
? `#[AllowPreview(false, reason: ${single(why)})]`
|
|
100
|
+
: `Route::${verb.toLowerCase()}(${single(route)}, $action)->sendproofPreview(false, ${single(why)});`;
|
|
101
|
+
case 'ruby': return `sendproof_no_preview only: :action, reason: ${double(why, { hash: true })}`;
|
|
102
|
+
case 'rust': return `.preview_unavailable(${double(verb)}, ${double(route)}, ${double(why)})`;
|
|
103
|
+
default: // dotnet
|
|
104
|
+
return has(framework, /minimal/i)
|
|
105
|
+
? `.WithSendproofPreview(enabled: false, reason: ${double(why)})`
|
|
106
|
+
: `[SendproofPreview(Enabled = false, Reason = ${double(why)})]`;
|
|
107
|
+
}
|
|
108
|
+
}
|