@panomc/sdk 1.0.0-dev.9 → 2.0.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.
- package/api/routes.core.json +3702 -0
- package/bin/api/__tests__/api-compat.test.mjs +97 -0
- package/bin/api/__tests__/check-paths.test.mjs +113 -0
- package/bin/api/__tests__/cli.test.mjs +44 -0
- package/bin/api/__tests__/extract-routes.test.mjs +132 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
- package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
- package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
- package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
- package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
- package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
- package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
- package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
- package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
- package/bin/api/__tests__/helpers.mjs +48 -0
- package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
- package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
- package/bin/api/api-compat.mjs +204 -0
- package/bin/api/check-paths.mjs +135 -0
- package/bin/api/cli.mjs +83 -0
- package/bin/api/extract-routes.mjs +129 -0
- package/bin/api/js-scan.mjs +337 -0
- package/bin/api/kotlin.mjs +366 -0
- package/bin/api/migrate-v1.mjs +330 -0
- package/bin/api/openapi-snapshot.mjs +112 -0
- package/bin/api/project.mjs +172 -0
- package/bin/api/rename-table.mjs +66 -0
- package/bin/api/util.mjs +194 -0
- package/bin/pano-api.js +5 -0
- package/build/plugin-api-rollup.js +46 -0
- package/core/css/pano-tokens.css +225 -0
- package/core/favicon/apple-touch-icon.png +0 -0
- package/core/favicon/favicon-96x96.png +0 -0
- package/core/favicon/favicon.ico +0 -0
- package/core/favicon/favicon.svg +1 -0
- package/core/favicon/site.webmanifest +21 -0
- package/core/favicon/web-app-manifest-192x192.png +0 -0
- package/core/favicon/web-app-manifest-512x512.png +0 -0
- package/core/favicon_old/android-icon-144x144.png +0 -0
- package/core/favicon_old/android-icon-192x192.png +0 -0
- package/core/favicon_old/android-icon-36x36.png +0 -0
- package/core/favicon_old/android-icon-48x48.png +0 -0
- package/core/favicon_old/android-icon-72x72.png +0 -0
- package/core/favicon_old/android-icon-96x96.png +0 -0
- package/core/favicon_old/apple-icon-114x114.png +0 -0
- package/core/favicon_old/apple-icon-120x120.png +0 -0
- package/core/favicon_old/apple-icon-144x144.png +0 -0
- package/core/favicon_old/apple-icon-152x152.png +0 -0
- package/core/favicon_old/apple-icon-180x180.png +0 -0
- package/core/favicon_old/apple-icon-57x57.png +0 -0
- package/core/favicon_old/apple-icon-60x60.png +0 -0
- package/core/favicon_old/apple-icon-72x72.png +0 -0
- package/core/favicon_old/apple-icon-76x76.png +0 -0
- package/core/favicon_old/apple-icon-precomposed.png +0 -0
- package/core/favicon_old/apple-icon.png +0 -0
- package/core/favicon_old/browserconfig.xml +11 -0
- package/core/favicon_old/favicon-16x16.png +0 -0
- package/core/favicon_old/favicon-32x32.png +0 -0
- package/core/favicon_old/favicon-96x96.png +0 -0
- package/core/favicon_old/favicon.ico +0 -0
- package/core/favicon_old/manifest.json +41 -0
- package/core/favicon_old/ms-icon-144x144.png +0 -0
- package/core/favicon_old/ms-icon-150x150.png +0 -0
- package/core/favicon_old/ms-icon-310x310.png +0 -0
- package/core/favicon_old/ms-icon-70x70.png +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
- package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
- package/core/js/ControllerRegistry.js +421 -0
- package/core/js/NotificationManager.js +62 -0
- package/core/js/PluginAPI.js +39 -0
- package/core/js/PluginManager.js +851 -0
- package/core/js/RouteMatcher.js +142 -0
- package/core/js/__tests__/ControllerRegistry.test.js +378 -0
- package/core/js/__tests__/NotificationManager.test.js +57 -0
- package/core/js/__tests__/PluginAPI.test.js +25 -0
- package/core/js/__tests__/RouteMatcher.test.js +33 -0
- package/core/js/__tests__/accessClient.test.js +582 -0
- package/core/js/__tests__/api.util.v1.test.js +336 -0
- package/core/js/__tests__/appStores.test.js +47 -0
- package/core/js/__tests__/devBounce.test.js +28 -0
- package/core/js/__tests__/html.util.test.js +18 -0
- package/core/js/__tests__/pluginFolder.util.test.js +106 -0
- package/core/js/__tests__/pluginPackage.test.js +134 -0
- package/core/js/__tests__/pluginUiZip.test.js +47 -0
- package/core/js/__tests__/sdkLevel.test.js +34 -0
- package/core/js/api.util.js +433 -0
- package/core/js/appStores.js +93 -0
- package/core/js/basePath.js +10 -0
- package/core/js/html.util.js +26 -0
- package/core/js/pluginFolder.util.js +95 -0
- package/core/js/tooltip.util.js +59 -0
- package/core/js/variables.js +95 -0
- package/core/js/version.util.js +100 -0
- package/core/scss/_pano-tokens.scss +51 -0
- package/core/scss/custom-bootstrap.scss +210 -0
- package/core/scss/fonts.scss +85 -0
- package/core/scss/main.scss +389 -0
- package/core/scss/themes.scss +122 -0
- package/package.json +63 -11
- package/src/__tests__/controllers.test.js +220 -0
- package/src/__tests__/views.spike.test.js +472 -0
- package/src/components/{index.js → panel.js} +9 -7
- package/src/components/theme.js +18 -0
- package/src/controllers.js +127 -0
- package/src/index.js +1 -1
- package/src/runtime-specifiers.js +88 -0
- package/src/svelte.js +3 -2
- package/src/types.js +5 -3
- package/src/utils/api.js +42 -1
- package/src/utils/auth.js +8 -0
- package/src/utils/component.js +4 -2
- package/src/utils/route.js +16 -0
- package/src/utils/text.js +8 -0
- package/src/views.js +29 -0
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// Scanning of JS / Svelte sources for the codemod and check-paths: API path literals, API client names,
|
|
2
|
+
// and the old->new client path mapping. Regex based on purpose (Svelte markup is not valid JS).
|
|
3
|
+
import { segments, applyRenames } from './rename-table.mjs';
|
|
4
|
+
import { escapeRe, lineIndex } from './util.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Index of the closing quote of the JS literal that opens at `q`, or -1 (unterminated on its line for quotes).
|
|
8
|
+
* @param {string} src @param {number} q
|
|
9
|
+
*/
|
|
10
|
+
export function literalEnd(src, q) {
|
|
11
|
+
const quote = src[q];
|
|
12
|
+
let j = q + 1;
|
|
13
|
+
if (quote === '`') {
|
|
14
|
+
while (j < src.length) {
|
|
15
|
+
const c = src[j];
|
|
16
|
+
if (c === '\\') j += 2;
|
|
17
|
+
else if (c === '`') return j;
|
|
18
|
+
else if (c === '$' && src[j + 1] === '{') {
|
|
19
|
+
let depth = 0;
|
|
20
|
+
for (; j < src.length; j++) {
|
|
21
|
+
if (src[j] === '{') depth++;
|
|
22
|
+
else if (src[j] === '}' && --depth === 0) break;
|
|
23
|
+
}
|
|
24
|
+
j++;
|
|
25
|
+
} else j++;
|
|
26
|
+
}
|
|
27
|
+
return -1;
|
|
28
|
+
}
|
|
29
|
+
while (j < src.length) {
|
|
30
|
+
const c = src[j];
|
|
31
|
+
if (c === '\\') j += 2;
|
|
32
|
+
else if (c === quote) return j;
|
|
33
|
+
else if (c === '\n') return -1;
|
|
34
|
+
else j++;
|
|
35
|
+
}
|
|
36
|
+
return -1;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Index of the paren closing the one at `open`, skipping strings, templates and comments.
|
|
41
|
+
* @param {string} src @param {number} open
|
|
42
|
+
*/
|
|
43
|
+
export function matchParenJs(src, open) {
|
|
44
|
+
let depth = 0;
|
|
45
|
+
for (let i = open; i < src.length; i++) {
|
|
46
|
+
const c = src[i];
|
|
47
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
48
|
+
const e = literalEnd(src, i);
|
|
49
|
+
if (e === -1) return -1;
|
|
50
|
+
i = e;
|
|
51
|
+
} else if (c === '/' && src[i + 1] === '/') {
|
|
52
|
+
const e = src.indexOf('\n', i);
|
|
53
|
+
if (e === -1) return -1;
|
|
54
|
+
i = e;
|
|
55
|
+
} else if (c === '/' && src[i + 1] === '*') {
|
|
56
|
+
const e = src.indexOf('*/', i + 2);
|
|
57
|
+
if (e === -1) return -1;
|
|
58
|
+
i = e + 1;
|
|
59
|
+
} else if (c === '(') depth++;
|
|
60
|
+
else if (c === ')' && --depth === 0) return i;
|
|
61
|
+
}
|
|
62
|
+
return -1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const CONTEXTS = [
|
|
66
|
+
{ kind: 'path', re: /\bpath\s*:\s*(['"`])/g },
|
|
67
|
+
{ kind: 'url', re: /\b(?:src|href|action|poster)\s*=\s*\{?\s*(['"`])/g },
|
|
68
|
+
{ kind: 'url', re: /\bfetch\s*\(\s*(['"`])/g },
|
|
69
|
+
{ kind: 'url', re: /\bnew\s+(?:WebSocket|EventSource)\s*\(\s*(['"`])/g },
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Finds the API path literals of a source: `path:` values (relative to /api/v1 after the cutover) and raw URL
|
|
74
|
+
* strings (`src=`, `href=`, `fetch`, WebSocket).
|
|
75
|
+
* @param {string} src
|
|
76
|
+
* @returns {{ kind: 'path' | 'url', start: number, end: number, content: string }[]}
|
|
77
|
+
*/
|
|
78
|
+
export function findPathLiterals(src) {
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const { kind, re } of CONTEXTS) {
|
|
81
|
+
re.lastIndex = 0;
|
|
82
|
+
let m;
|
|
83
|
+
while ((m = re.exec(src))) {
|
|
84
|
+
const q = m.index + m[0].length - 1;
|
|
85
|
+
const e = literalEnd(src, q);
|
|
86
|
+
if (e === -1) continue;
|
|
87
|
+
out.push({ kind, start: q + 1, end: e, content: src.slice(q + 1, e) });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const seen = new Set();
|
|
91
|
+
return out
|
|
92
|
+
.sort((a, b) => a.start - b.start)
|
|
93
|
+
.filter((l) => (seen.has(l.start) ? false : (seen.add(l.start), true)));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** `<prefix>/api<rest>` of a legacy URL; the prefix is empty or an origin / template hole. */
|
|
97
|
+
export const LEGACY_URL = /^((?:\$\{[^}]*\}|(?:https?|wss?):\/\/[^/'"`?#\s]*)?)(\/(?:panel\/)?api)(?=\/|$|\?|#)/;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Splits a path tail (query / hash) off, ignoring `?` inside template holes.
|
|
101
|
+
* @param {string} s @returns {[string, string]}
|
|
102
|
+
*/
|
|
103
|
+
export function splitTail(s) {
|
|
104
|
+
let depth = 0;
|
|
105
|
+
for (let i = 0; i < s.length; i++) {
|
|
106
|
+
if (s[i] === '$' && s[i + 1] === '{') depth++;
|
|
107
|
+
else if (s[i] === '}' && depth) depth--;
|
|
108
|
+
else if (depth === 0 && (s[i] === '?' || s[i] === '#')) return [s.slice(0, i), s.slice(i)];
|
|
109
|
+
}
|
|
110
|
+
return [s, ''];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* @typedef {{ method?: string, old: string, new: string, relative: string, class?: string, file?: string }} PathPair
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Maps an old client path (the part after `/api`) to its path relative to /api/v1.
|
|
119
|
+
* Order: the repository's own old->new pairs, the core rename table, then the `--strip` plugin segment.
|
|
120
|
+
* @param {string} oldRest e.g. `/panel/plugins/search`, `/market/store/products/${id}`
|
|
121
|
+
* @param {{ pairs?: PathPair[], strip?: string | null, pluginId?: string | null }} ctx
|
|
122
|
+
* @returns {{ path: string, how: 'pair' | 'core' | 'strip' | 'generic' }}
|
|
123
|
+
*/
|
|
124
|
+
export function mapClientPath(oldRest, ctx) {
|
|
125
|
+
const exact = mapExactPath(oldRest, ctx);
|
|
126
|
+
if (exact.how !== 'generic') return exact;
|
|
127
|
+
// `/staff/config${query}`: the text after the matched path stays where it is
|
|
128
|
+
const last = segments(oldRest).pop() ?? '';
|
|
129
|
+
const tail = /^([^$/]+)(\$\{.*)$/.exec(last);
|
|
130
|
+
if (tail) {
|
|
131
|
+
const base = mapExactPath(oldRest.slice(0, oldRest.length - tail[2].length), ctx);
|
|
132
|
+
if (base.how !== 'generic') return { path: base.path + tail[2], how: base.how };
|
|
133
|
+
}
|
|
134
|
+
return exact;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A client path segment against a route segment: a segment with a `${...}` hole fits only a route parameter
|
|
139
|
+
* (a hole is a value, so `/staff/${id}` is never the fixed route `/staff/config`).
|
|
140
|
+
* @param {string} lit @param {string} pat
|
|
141
|
+
*/
|
|
142
|
+
const fitsSegment = (lit, pat) => (pat.startsWith(':') ? lit.length > 0 : !lit.includes('${') && lit === pat);
|
|
143
|
+
|
|
144
|
+
/** @param {string} oldRest @param {{ pairs?: PathPair[], strip?: string | null, pluginId?: string | null }} ctx */
|
|
145
|
+
function mapExactPath(oldRest, ctx) {
|
|
146
|
+
const lit = segments(oldRest);
|
|
147
|
+
let best = null;
|
|
148
|
+
let bestScore = -1;
|
|
149
|
+
for (const pair of ctx.pairs || []) {
|
|
150
|
+
const pat = segments(pair.old.replace(/^\/api/, ''));
|
|
151
|
+
if (pat.length !== lit.length) continue;
|
|
152
|
+
let score = 0;
|
|
153
|
+
let ok = true;
|
|
154
|
+
for (let i = 0; i < pat.length; i++) {
|
|
155
|
+
if (!fitsSegment(lit[i], pat[i])) {
|
|
156
|
+
ok = false;
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
if (!pat[i].startsWith(':') && lit[i] === pat[i]) score++;
|
|
160
|
+
}
|
|
161
|
+
if (ok && score > bestScore) {
|
|
162
|
+
best = pair;
|
|
163
|
+
bestScore = score;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (best) {
|
|
167
|
+
const pat = segments(best.old.replace(/^\/api/, ''));
|
|
168
|
+
const names = {};
|
|
169
|
+
pat.forEach((s, i) => {
|
|
170
|
+
if (s.startsWith(':')) names[s] = lit[i];
|
|
171
|
+
});
|
|
172
|
+
const out = segments(best.relative).map((s) => (s.startsWith(':') ? names[s] ?? s : s));
|
|
173
|
+
return { path: '/' + out.join('/'), how: 'pair' };
|
|
174
|
+
}
|
|
175
|
+
const renamed = applyRenames(oldRest, fitsSegment);
|
|
176
|
+
if (renamed.renamed) return { path: renamed.path, how: 'core' };
|
|
177
|
+
if (ctx.strip && ctx.pluginId) {
|
|
178
|
+
if (lit[0] === 'panel' && lit[1] === ctx.strip) return { path: '/' + ['plugins', ctx.pluginId, 'panel', ...lit.slice(2)].join('/'), how: 'strip' };
|
|
179
|
+
if (lit[0] === ctx.strip) return { path: '/' + ['plugins', ctx.pluginId, ...lit.slice(1)].join('/'), how: 'strip' };
|
|
180
|
+
}
|
|
181
|
+
return { path: oldRest || '/', how: 'generic' };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* A path of a plugin's own endpoints (`/plugins/<id>/...`, not core's `/plugins/<id>/_/...`): unversioned, so a full
|
|
186
|
+
* URL for it starts with `/api`, not `/api/v1` (decision 81).
|
|
187
|
+
* @param {string} rel
|
|
188
|
+
*/
|
|
189
|
+
export const isPluginOwnPath = (rel) => /^\/plugins\/[^/]+\/(?!_(?:\/|$))[^/]/.test(rel);
|
|
190
|
+
|
|
191
|
+
/** `/api/plugins/<id>/...` is the final form of a plugin's own URL (decision 81), not a legacy path. */
|
|
192
|
+
export const isMigratedPluginUrl = (content) => /^(?:\$\{[^}]*\}|(?:https?|wss?):\/\/[^/'"`?#\s]*)?\/api\/plugins\/[^/?#]+/.test(content);
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Rewrites the legacy path literals of a source (step e of migrate-v1).
|
|
196
|
+
* @param {string} src
|
|
197
|
+
* @param {{ pairs?: PathPair[], strip?: string | null, pluginId?: string | null }} ctx
|
|
198
|
+
* @returns {{ text: string, count: number, generic: string[] }}
|
|
199
|
+
*/
|
|
200
|
+
export function rewritePathLiterals(src, ctx) {
|
|
201
|
+
let out = '';
|
|
202
|
+
let at = 0;
|
|
203
|
+
let count = 0;
|
|
204
|
+
const generic = [];
|
|
205
|
+
for (const lit of findPathLiterals(src)) {
|
|
206
|
+
const m = LEGACY_URL.exec(lit.content);
|
|
207
|
+
if (!m) continue;
|
|
208
|
+
if (lit.kind === 'path' && m[1]) continue;
|
|
209
|
+
const rest = lit.content.slice(m[0].length);
|
|
210
|
+
if (rest === '/v1' || rest.startsWith('/v1/') || rest.startsWith('/v1?')) continue;
|
|
211
|
+
if (isMigratedPluginUrl(lit.content)) continue;
|
|
212
|
+
const [pathPart, tail] = splitTail(rest);
|
|
213
|
+
const mapped = mapClientPath(pathPart, ctx);
|
|
214
|
+
if (mapped.how === 'generic' && pathPart) generic.push(lit.content);
|
|
215
|
+
const rel = mapped.path === '/' ? '' : mapped.path;
|
|
216
|
+
const next = lit.kind === 'path' ? `${m[1]}${rel || '/'}${tail}` : `${m[1]}${isPluginOwnPath(rel) ? '/api' : '/api/v1'}${rel}${tail}`;
|
|
217
|
+
out += src.slice(at, lit.start) + next;
|
|
218
|
+
at = lit.end;
|
|
219
|
+
count++;
|
|
220
|
+
}
|
|
221
|
+
return { text: out + src.slice(at), count, generic };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Names under which API clients are reachable in a file.
|
|
226
|
+
* `core` clients take paths relative to /api/v1; `plugin` clients prefix `/api/plugins/<id>` themselves.
|
|
227
|
+
* @param {string} src
|
|
228
|
+
* @returns {{ core: Set<string>, plugin: Map<string, string | null> }}
|
|
229
|
+
*/
|
|
230
|
+
export function detectClients(src) {
|
|
231
|
+
const core = new Set(['ApiUtil']);
|
|
232
|
+
/** @type {Map<string, string | null>} */
|
|
233
|
+
const plugin = new Map();
|
|
234
|
+
for (const m of src.matchAll(/import\s+([A-Za-z_$][\w$]*)\s*(?:,\s*\{[^}]*\})?\s+from\s+['"][^'"]*utils\/api(?:\.js)?['"]/g)) core.add(m[1]);
|
|
235
|
+
for (const m of src.matchAll(/import\s*\{([^}]*)\}\s*from\s*['"]@panomc\/sdk\/plugin-api['"]/g)) {
|
|
236
|
+
for (const part of m[1].split(',')) {
|
|
237
|
+
const p = /^\s*api(?:\s+as\s+([A-Za-z_$][\w$]*))?\s*$/.exec(part);
|
|
238
|
+
if (p) plugin.set(p[1] || 'api', null);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
if (!plugin.has('api') && /\bapi\.(?:panel\.)?(?:get|post|put|delete|customRequest)\s*\(/.test(src) && /plugin-api/.test(src)) plugin.set('api', null);
|
|
242
|
+
for (const m of src.matchAll(/(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*createPluginApi\(\s*(['"`])([^'"`]+)\2\s*\)/g)) plugin.set(m[1], m[3]);
|
|
243
|
+
return { core, plugin };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const METHODS = '(?:get|post|put|delete|customRequest)';
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Finds calls on API clients.
|
|
250
|
+
* @param {string} src
|
|
251
|
+
* @returns {{ name: string, kind: 'core' | 'plugin', pluginId: string | null, panel: boolean, open: number, close: number }[]}
|
|
252
|
+
*/
|
|
253
|
+
export function findApiCalls(src) {
|
|
254
|
+
const { core, plugin } = detectClients(src);
|
|
255
|
+
const names = [...core, ...plugin.keys()];
|
|
256
|
+
if (!names.length) return [];
|
|
257
|
+
const re = new RegExp(`(?<![\\w$.])(${names.map(escapeRe).join('|')})(\\.panel)?\\.${METHODS}\\s*\\(`, 'g');
|
|
258
|
+
const out = [];
|
|
259
|
+
let m;
|
|
260
|
+
while ((m = re.exec(src))) {
|
|
261
|
+
const open = m.index + m[0].length - 1;
|
|
262
|
+
const close = matchParenJs(src, open);
|
|
263
|
+
if (close === -1) continue;
|
|
264
|
+
const isPlugin = plugin.has(m[1]);
|
|
265
|
+
out.push({ name: m[1], kind: isPlugin ? 'plugin' : 'core', pluginId: isPlugin ? plugin.get(m[1]) ?? null : null, panel: !!m[2], open, close });
|
|
266
|
+
}
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Variable names that hold values returned by API client calls in this file.
|
|
272
|
+
* @param {string} src
|
|
273
|
+
* @returns {Set<string>}
|
|
274
|
+
*/
|
|
275
|
+
export function resultVariables(src) {
|
|
276
|
+
const vars = new Set();
|
|
277
|
+
for (const call of findApiCalls(src)) {
|
|
278
|
+
const start = src.lastIndexOf(call.name, call.open);
|
|
279
|
+
const before = src.slice(Math.max(0, start - 80), start);
|
|
280
|
+
const a = /([A-Za-z_$][\w$]*)\s*=\s*(?:await\s+)?$/.exec(before);
|
|
281
|
+
if (a) vars.add(a[1]);
|
|
282
|
+
const after = src.slice(call.close + 1, call.close + 240);
|
|
283
|
+
const t = /^\s*\.then\(\s*(?:async\s*)?\(?\s*([A-Za-z_$][\w$]*)\s*\)?\s*=>/.exec(after);
|
|
284
|
+
if (t) vars.add(t[1]);
|
|
285
|
+
}
|
|
286
|
+
return vars;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Rewrites result reads on API values (step f): error codes to `error.code`, `result` checks to `error` checks,
|
|
291
|
+
* `errors` to `error.fields`. Only variables that receive an API client call result are touched.
|
|
292
|
+
* @param {string} src
|
|
293
|
+
* @returns {{ text: string, count: number }}
|
|
294
|
+
*/
|
|
295
|
+
export function rewriteResults(src) {
|
|
296
|
+
let text = src;
|
|
297
|
+
let count = 0;
|
|
298
|
+
for (const v of resultVariables(src)) {
|
|
299
|
+
const acc = `(?<![\\w$.])${escapeRe(v)}(?:\\s*\\?)?`;
|
|
300
|
+
const rules = [
|
|
301
|
+
[new RegExp(`(${acc})\\.result\\s*(?:===|==)\\s*(['"])ok\\2`, 'g'), (_m, a) => `!${a}.error`],
|
|
302
|
+
[new RegExp(`(${acc})\\.result\\s*(?:!==|!=)\\s*(['"])ok\\2`, 'g'), (_m, a) => `!!${a}.error`],
|
|
303
|
+
[new RegExp(`(${acc})\\.result\\s*(?:===|==)\\s*(['"])errors?\\2`, 'g'), (_m, a) => `!!${a}.error`],
|
|
304
|
+
[new RegExp(`(${acc})\\.result\\s*(?:!==|!=)\\s*(['"])errors?\\2`, 'g'), (_m, a) => `!${a}.error`],
|
|
305
|
+
[new RegExp(`(${acc})\\.error(\\s*)(===|==|!==|!=)(\\s*)(['"\`])`, 'g'), (_m, a, s1, op, s2, q) => `${a}.error?.code${s1}${op}${s2}${q}`],
|
|
306
|
+
[new RegExp(`(${acc})\\.errors\\b`, 'g'), (_m, a) => `${a}.error?.fields`],
|
|
307
|
+
];
|
|
308
|
+
for (const [re, fn] of rules) {
|
|
309
|
+
text = text.replace(re, (...args) => {
|
|
310
|
+
count++;
|
|
311
|
+
return fn(...args);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return { text, count };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Leftovers a migration aid reports: legacy path literals, `.result ===` reads, `totalPage`.
|
|
320
|
+
* @param {string} src
|
|
321
|
+
* @returns {{ line: number, what: string, text: string }[]}
|
|
322
|
+
*/
|
|
323
|
+
export function findJsLeftovers(src) {
|
|
324
|
+
const lineOf = lineIndex(src);
|
|
325
|
+
const out = [];
|
|
326
|
+
for (const lit of findPathLiterals(src)) {
|
|
327
|
+
const m = LEGACY_URL.exec(lit.content);
|
|
328
|
+
if (!m || (lit.kind === 'path' && m[1])) continue;
|
|
329
|
+
const rest = lit.content.slice(m[0].length);
|
|
330
|
+
if (rest === '/v1' || rest.startsWith('/v1/')) continue;
|
|
331
|
+
if (isMigratedPluginUrl(lit.content)) continue;
|
|
332
|
+
out.push({ line: lineOf(lit.start), what: '/api/ path literal', text: lit.content });
|
|
333
|
+
}
|
|
334
|
+
for (const m of src.matchAll(/\.result\s*(?:===|!==|==|!=)/g)) out.push({ line: lineOf(m.index), what: '.result comparison', text: m[0] });
|
|
335
|
+
for (const m of src.matchAll(/\btotalPage\b/g)) out.push({ line: lineOf(m.index), what: 'totalPage', text: 'totalPage' });
|
|
336
|
+
return out.sort((a, b) => a.line - b.line);
|
|
337
|
+
}
|