Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions packages/driver/src/cy/commands/prompt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,29 @@ const initializeModule = async (Cypress: Cypress.Cypress): Promise<CyPromptDrive
})
}

let module: CyPromptDriver | null = null

// Once the cy prompt bundle is downloaded and ready,
// we can initialize it via the module federation runtime
init({
remotes: [{
alias: 'cy-prompt',
type: 'module',
name: 'cy-prompt',
entryGlobalName: 'cy-prompt',
entry: '/__cypress-cy-prompt/driver/cy-prompt.js',
shareScope: 'default',
}],
name: 'driver',
})

// This cy-prompt.js file and any subsequent files are
// served from the cy prompt bundle.
const module = await loadRemote<CyPromptDriver>('cy-prompt')
try {
init({
remotes: [{
alias: 'cy-prompt',
type: 'module',
name: 'cy-prompt',
entryGlobalName: 'cy-prompt',
entry: '/__cypress-cy-prompt/driver/cy-prompt.js',
shareScope: 'default',
}],
name: 'driver',
})

// This cy-prompt.js file and any subsequent files are
// served from the cy prompt bundle.
module = await loadRemote<CyPromptDriver>('cy-prompt')
} catch (error) {
$errUtils.throwErrByPath('prompt.promptBundleNeedsRefresh')
}

if (!module?.default) {
$errUtils.throwErrByPath('prompt.promptDownloadError', {
Expand Down
5 changes: 5 additions & 0 deletions packages/driver/src/cypress/error_messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,11 @@ export default {
docsUrl: 'https://on.cypress.io/prompt-download-error',
}
},
promptBundleNeedsRefresh: stripIndent`\
Your \`cy.prompt\` Cloud code needs to be refreshed.

Please restart the app to get the latest version.
`,
promptProxyError: {
message: stripIndent`\
\`cy.prompt\` requires an internet connection. To continue, you may need to configure Cypress with your proxy settings.
Expand Down
12 changes: 3 additions & 9 deletions packages/server/lib/cloud/cy-prompt/ensure_cy_prompt_bundle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { remove, ensureDir, readFile, pathExists } from 'fs-extra'

import tar from 'tar'
import { ensureDir, readFile, pathExists } from 'fs-extra'
import { getCyPromptBundle } from '../api/cy-prompt/get_cy_prompt_bundle'
import path from 'path'
import { verifySignature } from '../encryption'
import { extractAtomic } from '../extract_atomic'

interface EnsureCyPromptBundleOptions {
cyPromptPath: string
Expand All @@ -21,8 +20,6 @@ interface EnsureCyPromptBundleOptions {
export const ensureCyPromptBundle = async ({ cyPromptPath, cyPromptUrl, projectId }: EnsureCyPromptBundleOptions): Promise<Record<string, string>> => {
const bundlePath = path.join(cyPromptPath, 'bundle.tar')

// First remove cyPromptPath to ensure we have a clean slate
await remove(cyPromptPath)
await ensureDir(cyPromptPath)

const responseManifestSignature: string = await getCyPromptBundle({
Expand All @@ -31,10 +28,7 @@ export const ensureCyPromptBundle = async ({ cyPromptPath, cyPromptUrl, projectI
bundlePath,
})

await tar.extract({
file: bundlePath,
cwd: cyPromptPath,
})
await extractAtomic(bundlePath, cyPromptPath)

const manifestPath = path.join(cyPromptPath, 'manifest.json')

Expand Down
52 changes: 52 additions & 0 deletions packages/server/lib/cloud/extract_atomic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { createReadStream } from 'fs'
import tar from 'tar'
import { ensureDir } from 'fs-extra'
import path from 'path'
import writeFileAtomic from 'write-file-atomic'

export const extractAtomic = async (archivePath: string, destinationPath: string) => {
const entryPromises: Promise<void>[] = []

const parser = new tar.Parse()

parser.on('entry', (entry) => {
if (entry.type !== 'File') {
entry.resume() // skip non-files

return
}

const targetPath = path.join(destinationPath, entry.path)

const p = (async () => {
await ensureDir(path.dirname(targetPath))

const chunks: Buffer[] = []

for await (const chunk of entry) {
chunks.push(chunk)
}

const content = Buffer.concat(chunks)

await writeFileAtomic(targetPath, content, {
mode: entry.mode || 0o644,
})
})()

entryPromises.push(p)
})

// Pipe archive into parser
const stream = createReadStream(archivePath)

stream.pipe(parser)

// Wait for parser to finish and all entry writes to complete
await new Promise<void>((resolve, reject) => {
parser.on('end', resolve)
stream.on('error', reject)
})

await Promise.all(entryPromises)
}
8 changes: 2 additions & 6 deletions packages/server/lib/cloud/studio/ensure_studio_bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import tar from 'tar'
import { getStudioBundle } from '../api/studio/get_studio_bundle'
import path from 'path'
import { verifySignature } from '../encryption'
import { extractAtomic } from '../extract_atomic'

interface EnsureStudioBundleOptions {
studioUrl: string
Expand All @@ -25,19 +26,14 @@ export const ensureStudioBundle = async ({
}: EnsureStudioBundleOptions): Promise<Record<string, string>> => {
const bundlePath = path.join(studioPath, 'bundle.tar')

// First remove studioPath to ensure we have a clean slate
await remove(studioPath)
await ensureDir(studioPath)

const responseManifestSignature = await getStudioBundle({
studioUrl,
bundlePath,
})

await tar.extract({
file: bundlePath,
cwd: studioPath,
})
await extractAtomic(bundlePath, studioPath)

const manifestPath = path.join(studioPath, 'manifest.json')

Expand Down
3 changes: 2 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@
"wait-port": "1.1.0",
"webdriver": "9.14.0",
"webpack-virtual-modules": "0.5.0",
"widest-line": "3.1.0"
"widest-line": "3.1.0",
"write-file-atomic": "7.0.0"
},
"devDependencies": {
"@babel/core": "7.28.0",
Expand Down
Loading
Loading