mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-11-29 00:07:30 +00:00
fix(frontend): dev page and raw app editor message handling
Small cleanups to how the dev page and the raw app editor exchange messages with their host frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e67dd0df53
commit
c883b82fa7
@@ -30,6 +30,7 @@
|
||||
import { onDestroy, onMount, setContext, untrack } from 'svelte'
|
||||
import DarkModeToggle from '$lib/components/sidebar/DarkModeToggle.svelte'
|
||||
import { page } from '$app/state'
|
||||
import { isFromWindowEmbedder } from '$lib/utils/embedderMessage'
|
||||
import { getUserExt } from '$lib/user'
|
||||
import FlowPreviewButtons from './flows/header/FlowPreviewButtons.svelte'
|
||||
import FlowModuleSchemaMap from './flows/map/FlowModuleSchemaMap.svelte'
|
||||
@@ -271,8 +272,8 @@
|
||||
modeInitialized = true
|
||||
}
|
||||
|
||||
const el = (event) => {
|
||||
// sendUserToast(`Received message from parent ${event.data.type}`, true)
|
||||
const el = (event: MessageEvent) => {
|
||||
if (!isFromWindowEmbedder(event)) return
|
||||
if (event.data.type == 'runTest') {
|
||||
runTest()
|
||||
event.preventDefault()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { WorkspaceService } from '$lib/gen'
|
||||
import { randomUUID } from '$lib/utils/uuid'
|
||||
import { uiBuilderFiles } from '$lib/components/raw_apps/utils'
|
||||
|
||||
export type RawAppBundle = {
|
||||
js: string
|
||||
@@ -91,7 +92,7 @@ export function bundleRawAppFiles({
|
||||
postToBundler({
|
||||
type: 'bundleRawApp',
|
||||
requestId,
|
||||
files,
|
||||
files: uiBuilderFiles(files),
|
||||
sharedUiFiles,
|
||||
bundlerType
|
||||
})
|
||||
|
||||
@@ -36,7 +36,8 @@
|
||||
rawAppStorageKey,
|
||||
rawAppCtx,
|
||||
readRawAppStorage,
|
||||
applyRawAppStorageOp
|
||||
applyRawAppStorageOp,
|
||||
uiBuilderFiles
|
||||
} from './utils'
|
||||
import { runDomQueryOnHtml, type RawAppDomQuery, type RawAppDomRequester } from './rawAppDom'
|
||||
import InlinePrompt from '../copilot/chat/InlinePrompt.svelte'
|
||||
@@ -1078,9 +1079,7 @@ addEventListener('message', function (e) {
|
||||
if (!target) return
|
||||
iframeFiles = { ...newFiles }
|
||||
iframeFocusPending = undefined
|
||||
buildTracker.start()
|
||||
const files = Object.fromEntries(
|
||||
Object.entries(newFiles).filter(([path, _]) => !path.endsWith('/'))
|
||||
buildTracker.start()
|
||||
const files = uiBuilderFiles(newFiles)
|
||||
target.postMessage(
|
||||
{
|
||||
@@ -1098,9 +1097,7 @@ addEventListener('message', function (e) {
|
||||
iframeFiles = { ...newFiles }
|
||||
const focus = iframeFocusPending === pathToSelect
|
||||
iframeFocusPending = undefined
|
||||
buildTracker.start()
|
||||
const files = Object.fromEntries(
|
||||
Object.entries(newFiles).filter(([path, _]) => !path.endsWith('/'))
|
||||
buildTracker.start()
|
||||
const files = uiBuilderFiles(newFiles)
|
||||
target.postMessage(
|
||||
{
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
rawAppCtx,
|
||||
rawAppPreviewModules,
|
||||
stripRawAppDiffNoise,
|
||||
uiBuilderFiles,
|
||||
type Runnable
|
||||
} from './utils'
|
||||
|
||||
@@ -290,3 +291,20 @@ describe('canonicalRawAppDiffValue', () => {
|
||||
expect(b.summary).toBe('app EDITED')
|
||||
})
|
||||
})
|
||||
|
||||
describe('uiBuilderFiles', () => {
|
||||
it('keeps app sources and drops directory entries and node_modules files', () => {
|
||||
expect(
|
||||
uiBuilderFiles({
|
||||
'/index.tsx': 'a',
|
||||
'/src/node_modules_helper.ts': 'b',
|
||||
'/src/': '',
|
||||
'/node_modules/tailwindcss/dist/lib.mjs': 'x',
|
||||
'node_modules/pkg/index.js': 'x',
|
||||
'./node_modules/pkg/index.js': 'x',
|
||||
'/src/vendor/node_modules/pkg/index.js': 'x',
|
||||
'\\node_modules\\pkg\\index.js': 'x'
|
||||
})
|
||||
).toEqual({ '/index.tsx': 'a', '/src/node_modules_helper.ts': 'b' })
|
||||
})
|
||||
})
|
||||
|
||||
@@ -448,6 +448,25 @@ function hiddenRunnableToTsType(runnable: Runnable) {
|
||||
}
|
||||
}
|
||||
|
||||
function isNodeModulesPath(path: string): boolean {
|
||||
return path
|
||||
.replace(/^(\.?\/)+/, '')
|
||||
.split(/[/\\]/)
|
||||
.includes('node_modules')
|
||||
}
|
||||
|
||||
/**
|
||||
* The app's stored files as the UI Builder bundler is given them. Its packages come from its
|
||||
* own install into `node_modules/`, so a stored file under any `node_modules` segment would
|
||||
* be imported in place of the installed package: such keys, like directory entries, are not
|
||||
* app sources and stay out.
|
||||
*/
|
||||
export function uiBuilderFiles(files: Record<string, string>): Record<string, string> {
|
||||
return Object.fromEntries(
|
||||
Object.entries(files).filter(([path]) => !path.endsWith('/') && !isNodeModulesPath(path))
|
||||
)
|
||||
}
|
||||
|
||||
/** Shown in the file tree, generated by `genWmillTs` — never a key of `files`. */
|
||||
export const WMILL_TS_PATH = '/wmill.ts'
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { isFromEmbedder } from './embedderMessage'
|
||||
|
||||
const self = {} as Window
|
||||
const parent = {} as Window
|
||||
const other = {} as Window
|
||||
const ownOrigin = 'https://wm.example'
|
||||
|
||||
describe('isFromEmbedder', () => {
|
||||
it('accepts a same-origin embedder', () => {
|
||||
expect(
|
||||
isFromEmbedder(
|
||||
{ source: parent, origin: ownOrigin },
|
||||
{ self, parent, ownOrigin, hasUrlToken: false }
|
||||
)
|
||||
).toBe(true)
|
||||
})
|
||||
|
||||
it('accepts a cross-origin embedder only when the page carries a url token', () => {
|
||||
const event = { source: parent, origin: 'vscode-webview://abc' }
|
||||
expect(isFromEmbedder(event, { self, parent, ownOrigin, hasUrlToken: true })).toBe(true)
|
||||
expect(isFromEmbedder(event, { self, parent, ownOrigin, hasUrlToken: false })).toBe(false)
|
||||
})
|
||||
|
||||
it('ignores messages from windows other than the embedder', () => {
|
||||
expect(
|
||||
isFromEmbedder(
|
||||
{ source: other, origin: 'https://elsewhere.example' },
|
||||
{ self, parent, ownOrigin, hasUrlToken: true }
|
||||
)
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('ignores everything when the page is not embedded', () => {
|
||||
expect(
|
||||
isFromEmbedder(
|
||||
{ source: self, origin: ownOrigin },
|
||||
{ self, parent: self, ownOrigin, hasUrlToken: true }
|
||||
)
|
||||
).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,29 @@
|
||||
type EmbedderMessageContext = {
|
||||
self: Window
|
||||
parent: Window | null
|
||||
ownOrigin: string
|
||||
// The page was opened with a `wm_token`, so its API calls carry that token rather than the
|
||||
// session cookie.
|
||||
hasUrlToken: boolean
|
||||
}
|
||||
|
||||
// The dev pages are driven by whatever embeds them (the VS Code extension's webview). Only that
|
||||
// embedder may drive them, and a cross-origin one only when it handed the page a token: without
|
||||
// one, the page acts with the session cookie, which another site must never steer.
|
||||
export function isFromEmbedder(
|
||||
event: Pick<MessageEvent, 'source' | 'origin'>,
|
||||
ctx: EmbedderMessageContext
|
||||
): boolean {
|
||||
if (!ctx.parent || ctx.parent === ctx.self) return false
|
||||
if (event.source !== ctx.parent) return false
|
||||
return event.origin === ctx.ownOrigin || ctx.hasUrlToken
|
||||
}
|
||||
|
||||
export function isFromWindowEmbedder(event: MessageEvent): boolean {
|
||||
return isFromEmbedder(event, {
|
||||
self: window,
|
||||
parent: window.parent,
|
||||
ownOrigin: window.location.origin,
|
||||
hasUrlToken: !!new URLSearchParams(window.location.search).get('wm_token')
|
||||
})
|
||||
}
|
||||
@@ -20,6 +20,7 @@
|
||||
import { deepEqual } from 'fast-equals'
|
||||
import { findModuleInFlow } from '$lib/components/flows/flowDiff'
|
||||
import { page } from '$app/state'
|
||||
import { isFromWindowEmbedder } from '$lib/utils/embedderMessage'
|
||||
import { userStore, workspaceStore } from '$lib/stores'
|
||||
import { getUserExt } from '$lib/user'
|
||||
import DarkModeToggle from '$lib/components/sidebar/DarkModeToggle.svelte'
|
||||
@@ -140,7 +141,8 @@
|
||||
connectWs()
|
||||
}
|
||||
|
||||
const el = (event) => {
|
||||
const el = (event: MessageEvent) => {
|
||||
if (!isFromWindowEmbedder(event)) return
|
||||
if (event.data.type == 'runTest') {
|
||||
runTest()
|
||||
} else if (event.data.type == 'replaceScript') {
|
||||
|
||||
Reference in New Issue
Block a user