@ohos-ports/synpress-cache 0.0.0-stage → 0.0.14-beta.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/LICENSE.md +21 -0
- package/dist/cli/index.js +1407 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/index.js +1421 -0
- package/dist/index.js.map +1 -0
- package/package.json +74 -4
- package/src/cli/cliEntrypoint.ts +104 -0
- package/src/cli/compilationFixes.ts +17 -0
- package/src/cli/compileWalletSetupFunctions.ts +73 -0
- package/src/cli/footer.ts +5 -0
- package/src/cli/index.ts +15 -0
- package/src/constants.ts +3 -0
- package/src/createCache.ts +20 -0
- package/src/defineWalletSetup.ts +28 -0
- package/src/downloadFile.ts +67 -0
- package/src/ensureCacheDirExists.ts +9 -0
- package/src/index.ts +11 -0
- package/src/prepareExtension.ts +21 -0
- package/src/prepareExtensionPhantom.ts +20 -0
- package/src/unzipArchive.ts +100 -0
- package/src/utils/buildWalletSetupFunction.ts +15 -0
- package/src/utils/bytesToMegabytes.ts +4 -0
- package/src/utils/createCacheForWalletSetupFunction.ts +45 -0
- package/src/utils/createTempContextDir.ts +14 -0
- package/src/utils/extractWalletSetupFunction.ts +12 -0
- package/src/utils/getUniqueWalletSetupFunctions.ts +60 -0
- package/src/utils/getWalletSetupFiles.ts +31 -0
- package/src/utils/getWalletSetupFuncHash.ts +12 -0
- package/src/utils/importWalletSetupFile.ts +32 -0
- package/src/utils/isDirEmpty.ts +15 -0
- package/src/utils/onDownloadProgress.ts +40 -0
- package/src/utils/removeTempContextDir.ts +23 -0
- package/src/utils/triggerCacheCreation.ts +50 -0
- package/src/utils/waitForExtensionOnLoadPage.ts +192 -0
- package/types/cli/cliEntrypoint.d.ts +2 -0
- package/types/cli/cliEntrypoint.d.ts.map +1 -0
- package/types/cli/compilationFixes.d.ts +2 -0
- package/types/cli/compilationFixes.d.ts.map +1 -0
- package/types/cli/compileWalletSetupFunctions.d.ts +5 -0
- package/types/cli/compileWalletSetupFunctions.d.ts.map +1 -0
- package/types/cli/footer.d.ts +2 -0
- package/types/cli/footer.d.ts.map +1 -0
- package/types/cli/index.d.ts +3 -0
- package/types/cli/index.d.ts.map +1 -0
- package/types/constants.d.ts +3 -0
- package/types/constants.d.ts.map +1 -0
- package/types/createCache.d.ts +2 -0
- package/types/createCache.d.ts.map +1 -0
- package/types/defineWalletSetup.d.ts +17 -0
- package/types/defineWalletSetup.d.ts.map +1 -0
- package/types/downloadFile.d.ts +13 -0
- package/types/downloadFile.d.ts.map +1 -0
- package/types/ensureCacheDirExists.d.ts +2 -0
- package/types/ensureCacheDirExists.d.ts.map +1 -0
- package/types/index.d.ts +12 -0
- package/types/index.d.ts.map +1 -0
- package/types/prepareExtension.d.ts +4 -0
- package/types/prepareExtension.d.ts.map +1 -0
- package/types/prepareExtensionPhantom.d.ts +3 -0
- package/types/prepareExtensionPhantom.d.ts.map +1 -0
- package/types/unzipArchive.d.ts +18 -0
- package/types/unzipArchive.d.ts.map +1 -0
- package/types/utils/buildWalletSetupFunction.d.ts +2 -0
- package/types/utils/buildWalletSetupFunction.d.ts.map +1 -0
- package/types/utils/bytesToMegabytes.d.ts +2 -0
- package/types/utils/bytesToMegabytes.d.ts.map +1 -0
- package/types/utils/createCacheForWalletSetupFunction.d.ts +3 -0
- package/types/utils/createCacheForWalletSetupFunction.d.ts.map +1 -0
- package/types/utils/createTempContextDir.d.ts +9 -0
- package/types/utils/createTempContextDir.d.ts.map +1 -0
- package/types/utils/extractWalletSetupFunction.d.ts +2 -0
- package/types/utils/extractWalletSetupFunction.d.ts.map +1 -0
- package/types/utils/getUniqueWalletSetupFunctions.d.ts +6 -0
- package/types/utils/getUniqueWalletSetupFunctions.d.ts.map +1 -0
- package/types/utils/getWalletSetupFiles.d.ts +2 -0
- package/types/utils/getWalletSetupFiles.d.ts.map +1 -0
- package/types/utils/getWalletSetupFuncHash.d.ts +3 -0
- package/types/utils/getWalletSetupFuncHash.d.ts.map +1 -0
- package/types/utils/importWalletSetupFile.d.ts +6 -0
- package/types/utils/importWalletSetupFile.d.ts.map +1 -0
- package/types/utils/isDirEmpty.d.ts +2 -0
- package/types/utils/isDirEmpty.d.ts.map +1 -0
- package/types/utils/onDownloadProgress.d.ts +3 -0
- package/types/utils/onDownloadProgress.d.ts.map +1 -0
- package/types/utils/removeTempContextDir.d.ts +8 -0
- package/types/utils/removeTempContextDir.d.ts.map +1 -0
- package/types/utils/triggerCacheCreation.d.ts +6 -0
- package/types/utils/triggerCacheCreation.d.ts.map +1 -0
- package/types/utils/waitForExtensionOnLoadPage.d.ts +6 -0
- package/types/utils/waitForExtensionOnLoadPage.d.ts.map +1 -0
- package/README.md +0 -3
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import axios from 'axios'
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
import { onDownloadProgress } from './utils/onDownloadProgress'
|
|
5
|
+
|
|
6
|
+
type DownloaderOptions = {
|
|
7
|
+
url: string
|
|
8
|
+
outputDir: string
|
|
9
|
+
fileName: string
|
|
10
|
+
overrideFile?: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type DownloadFileResult = {
|
|
14
|
+
filePath: string
|
|
15
|
+
downloadSkipped: boolean
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function downloadFile(options: DownloaderOptions) {
|
|
19
|
+
const { url, outputDir, fileName, overrideFile } = options
|
|
20
|
+
|
|
21
|
+
const returnPromise = new Promise<DownloadFileResult>((resolve, reject) => {
|
|
22
|
+
const filePath = path.join(outputDir, fileName)
|
|
23
|
+
|
|
24
|
+
const fileExists = fs.existsSync(filePath)
|
|
25
|
+
if (fileExists && !overrideFile) {
|
|
26
|
+
resolve({
|
|
27
|
+
filePath,
|
|
28
|
+
downloadSkipped: true
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
console.log(`[DEBUG] Downloading file from ${url}`)
|
|
35
|
+
|
|
36
|
+
axios
|
|
37
|
+
.get(url, {
|
|
38
|
+
responseType: 'stream',
|
|
39
|
+
onDownloadProgress: onDownloadProgress(url, fileName)
|
|
40
|
+
})
|
|
41
|
+
.then((response) => {
|
|
42
|
+
const writer = fs.createWriteStream(filePath)
|
|
43
|
+
|
|
44
|
+
response.data.pipe(writer)
|
|
45
|
+
|
|
46
|
+
writer.on('finish', () => {
|
|
47
|
+
resolve({
|
|
48
|
+
filePath,
|
|
49
|
+
downloadSkipped: false
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
writer.on('error', (error) => {
|
|
53
|
+
// TODO: Handle errors in a more sophisticated way
|
|
54
|
+
reject(new Error(`[Writer] ${error.message}`))
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
.catch((error: Error) => {
|
|
58
|
+
// TODO: Handle errors in a more sophisticated way
|
|
59
|
+
reject(new Error(`[Axios] ${error.message}`))
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// TODO: This is a workaround to handle errors from both `writer` and `axios` and get 100% test coverage.
|
|
64
|
+
return returnPromise.catch((error: Error) => {
|
|
65
|
+
throw new Error(`[DownloadFile] Error downloading the file - ${error.message}`)
|
|
66
|
+
})
|
|
67
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'fs-extra'
|
|
3
|
+
import { CACHE_DIR_NAME } from './constants'
|
|
4
|
+
|
|
5
|
+
export function ensureCacheDirExists() {
|
|
6
|
+
const cacheDirPath = path.join(process.cwd(), CACHE_DIR_NAME)
|
|
7
|
+
fs.ensureDirSync(cacheDirPath)
|
|
8
|
+
return cacheDirPath
|
|
9
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './constants'
|
|
2
|
+
export * from './downloadFile'
|
|
3
|
+
export * from './unzipArchive'
|
|
4
|
+
export * from './ensureCacheDirExists'
|
|
5
|
+
export * from './createCache'
|
|
6
|
+
export * from './defineWalletSetup'
|
|
7
|
+
export * from './utils/createTempContextDir'
|
|
8
|
+
export * from './utils/removeTempContextDir'
|
|
9
|
+
export * from './prepareExtension'
|
|
10
|
+
export * from './cli/cliEntrypoint'
|
|
11
|
+
export * from './prepareExtensionPhantom'
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { downloadFile, ensureCacheDirExists, unzipArchive } from '.'
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_METAMASK_VERSION = '13.13.1'
|
|
4
|
+
export const EXTENSION_DOWNLOAD_URL = `https://github.com/MetaMask/metamask-extension/releases/download/v${DEFAULT_METAMASK_VERSION}/metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`
|
|
5
|
+
|
|
6
|
+
// NOTE: This function is copied from `wallets/metamask/src/prepareExtension.ts` only TEMPORARILY!
|
|
7
|
+
export async function prepareExtension() {
|
|
8
|
+
const cacheDirPath = ensureCacheDirExists()
|
|
9
|
+
|
|
10
|
+
const downloadResult = await downloadFile({
|
|
11
|
+
url: EXTENSION_DOWNLOAD_URL,
|
|
12
|
+
outputDir: cacheDirPath,
|
|
13
|
+
fileName: `metamask-chrome-${DEFAULT_METAMASK_VERSION}.zip`
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
const unzipResult = await unzipArchive({
|
|
17
|
+
archivePath: downloadResult.filePath
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
return unzipResult.outputPath
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { downloadFile, ensureCacheDirExists, unzipArchivePhantom } from '.'
|
|
2
|
+
|
|
3
|
+
export const PHANTOM_EXTENSION_DOWNLOAD_URL = 'https://crx-backup.phantom.dev/latest.crx'
|
|
4
|
+
|
|
5
|
+
// NOTE: This function is copied from `wallets/phantom/src/prepareExtensionPhantom.ts` only TEMPORARILY!
|
|
6
|
+
export async function prepareExtensionPhantom() {
|
|
7
|
+
const cacheDirPath = ensureCacheDirExists()
|
|
8
|
+
|
|
9
|
+
const downloadResult = await downloadFile({
|
|
10
|
+
url: PHANTOM_EXTENSION_DOWNLOAD_URL,
|
|
11
|
+
outputDir: cacheDirPath,
|
|
12
|
+
fileName: 'phantom-chrome-latest.crx'
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
const unzipResult = await unzipArchivePhantom({
|
|
16
|
+
archivePath: downloadResult.filePath
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
return unzipResult.outputPath
|
|
20
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'fs-extra'
|
|
3
|
+
import unzipCrx from 'unzip-crx-3'
|
|
4
|
+
import unzippper from 'unzipper'
|
|
5
|
+
|
|
6
|
+
type UnzipArchiveOptions = {
|
|
7
|
+
archivePath: string
|
|
8
|
+
overwrite?: boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
type UnzipArchiveResult = {
|
|
12
|
+
outputPath: string
|
|
13
|
+
unzipSkipped: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function unzipArchive(options: UnzipArchiveOptions) {
|
|
17
|
+
const { archivePath, overwrite } = options
|
|
18
|
+
|
|
19
|
+
const resultPromise = new Promise<UnzipArchiveResult>((resolve, reject) => {
|
|
20
|
+
const archiveFileExtension = archivePath.split('.').slice(-1)
|
|
21
|
+
const outputPath = archivePath.replace(`.${archiveFileExtension}`, '')
|
|
22
|
+
|
|
23
|
+
const fileExists = fs.existsSync(outputPath)
|
|
24
|
+
if (fileExists && !overwrite) {
|
|
25
|
+
resolve({
|
|
26
|
+
outputPath,
|
|
27
|
+
unzipSkipped: true
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
return
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Creates the output directory
|
|
34
|
+
fs.mkdirSync(outputPath, { recursive: true })
|
|
35
|
+
|
|
36
|
+
fs.createReadStream(archivePath)
|
|
37
|
+
.pipe(unzippper.Parse())
|
|
38
|
+
.on('entry', function (entry) {
|
|
39
|
+
const fileName = entry.path
|
|
40
|
+
const type = entry.type as 'Directory' | 'File'
|
|
41
|
+
|
|
42
|
+
if (type === 'Directory') {
|
|
43
|
+
fs.mkdirSync(path.join(outputPath, fileName), { recursive: true })
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (type === 'File') {
|
|
48
|
+
const outputFilePath = path.join(outputPath, fileName)
|
|
49
|
+
const outputFilePathDir = path.dirname(outputFilePath)
|
|
50
|
+
|
|
51
|
+
// Handles the rare case when a file is in a directory which has no entry ¯\_(ツ)_/¯
|
|
52
|
+
if (!fs.existsSync(outputFilePathDir)) {
|
|
53
|
+
fs.mkdirSync(outputFilePathDir, { recursive: true })
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
entry.pipe(fs.createWriteStream(outputFilePath))
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.promise()
|
|
60
|
+
.then(() => {
|
|
61
|
+
resolve({
|
|
62
|
+
outputPath,
|
|
63
|
+
unzipSkipped: false
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
.catch((error: Error) => {
|
|
67
|
+
fs.unlinkSync(outputPath)
|
|
68
|
+
reject(new Error(`[Pipe] ${error.message}`))
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// TODO: Handle errors in a more sophisticated way
|
|
73
|
+
return resultPromise.catch((error: Error) => {
|
|
74
|
+
throw new Error(`[UnzipFile] Error unzipping the file - ${error.message}`)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export async function unzipArchivePhantom(options: UnzipArchiveOptions) {
|
|
79
|
+
const { archivePath, overwrite } = options
|
|
80
|
+
|
|
81
|
+
const archiveFileExtension = archivePath.split('.').slice(-1)
|
|
82
|
+
const outputPath = archivePath.replace(`.${archiveFileExtension}`, '')
|
|
83
|
+
|
|
84
|
+
const fileExists = fs.existsSync(outputPath)
|
|
85
|
+
if (fileExists && !overwrite) {
|
|
86
|
+
return {
|
|
87
|
+
outputPath,
|
|
88
|
+
unzipSkipped: true
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Creates the output directory
|
|
93
|
+
fs.mkdirSync(outputPath, { recursive: true })
|
|
94
|
+
|
|
95
|
+
await unzipCrx(archivePath, outputPath)
|
|
96
|
+
|
|
97
|
+
// TODO: Handle errors
|
|
98
|
+
|
|
99
|
+
return { outputPath }
|
|
100
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { transformSync } from 'esbuild'
|
|
2
|
+
|
|
3
|
+
export default function buildWalletSetupFunction(walletSetupFunctionString: string) {
|
|
4
|
+
const { code } = transformSync(walletSetupFunctionString, {
|
|
5
|
+
format: 'esm',
|
|
6
|
+
minifyWhitespace: true,
|
|
7
|
+
target: 'es2022',
|
|
8
|
+
drop: ['console', 'debugger'],
|
|
9
|
+
loader: 'ts',
|
|
10
|
+
logLevel: 'silent',
|
|
11
|
+
platform: 'node'
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
return code
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { WalletSetupFunction } from '../defineWalletSetup'
|
|
2
|
+
import { waitForExtensionOnLoadPage } from './waitForExtensionOnLoadPage'
|
|
3
|
+
|
|
4
|
+
// Inlining the sleep function here cause this is one of the few places in the entire codebase where sleep should be used!
|
|
5
|
+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
|
|
6
|
+
|
|
7
|
+
export async function createCacheForWalletSetupFunction(
|
|
8
|
+
extensionPath: string,
|
|
9
|
+
contextCacheDirPath: string,
|
|
10
|
+
walletSetup: WalletSetupFunction,
|
|
11
|
+
fileName: string
|
|
12
|
+
) {
|
|
13
|
+
const { chromium } = await import('playwright-core')
|
|
14
|
+
|
|
15
|
+
// TODO: Extract & Make a constant.
|
|
16
|
+
const browserArgs = [`--disable-extensions-except=${extensionPath}`, `--load-extension=${extensionPath}`]
|
|
17
|
+
|
|
18
|
+
if (process.env.HEADLESS) {
|
|
19
|
+
browserArgs.push('--headless=new')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const context = await chromium.launchPersistentContext(contextCacheDirPath, {
|
|
23
|
+
headless: false,
|
|
24
|
+
args: browserArgs
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const extensionPage = await waitForExtensionOnLoadPage(context, extensionPath)
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
await walletSetup(context, extensionPage)
|
|
31
|
+
} catch (e) {
|
|
32
|
+
throw new Error(
|
|
33
|
+
`[CORE] Encountered an error while executing wallet setup function from file ${fileName}. Error message: ${
|
|
34
|
+
(e as Error).message
|
|
35
|
+
}`
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// We sleep here to give the browser enough time to save the context to the disk.
|
|
40
|
+
await sleep(3000) // TODO: Extract & Make this timeout configurable.
|
|
41
|
+
|
|
42
|
+
await context.close()
|
|
43
|
+
|
|
44
|
+
return
|
|
45
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import fs from 'fs-extra'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Creates a temporary context directory.
|
|
7
|
+
* This directory should be created per-worker basis.
|
|
8
|
+
*
|
|
9
|
+
* This function mirrors the one found in the Playwright source code:
|
|
10
|
+
* https://github.com/microsoft/playwright/blob/d1d5fc67dc684a5d4b682749e59bba8cc0ad14de/packages/playwright-core/src/server/browserType.ts#L161
|
|
11
|
+
*/
|
|
12
|
+
export async function createTempContextDir(browserName: string, testId: string) {
|
|
13
|
+
return await fs.mkdtemp(path.join(os.tmpdir(), `synpress_${browserName}_${testId}_`))
|
|
14
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export function extractWalletSetupFunction(sourceCode: string): string {
|
|
2
|
+
const match = sourceCode.match(
|
|
3
|
+
/defineWalletSetup\s*\([^,]*,\s*(async\s*\([^)]*\)\s*=>\s*{(?:[^{}]*|{(?:[^{}]*|{[^{}]*})*})*})\s*\)/
|
|
4
|
+
)
|
|
5
|
+
|
|
6
|
+
if (!match || !match[1]) {
|
|
7
|
+
console.log('Failed to extract defineWalletSetup callback from:', sourceCode)
|
|
8
|
+
throw new Error('Could not find defineWalletSetup callback')
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return match[1]
|
|
12
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import type { WalletSetupFunction } from '../defineWalletSetup'
|
|
3
|
+
import { getWalletSetupFiles } from './getWalletSetupFiles'
|
|
4
|
+
import { importWalletSetupFile } from './importWalletSetupFile'
|
|
5
|
+
|
|
6
|
+
type DuplicatesMap = Record<string, { fileName: string; setupFunction: WalletSetupFunction }[]>
|
|
7
|
+
|
|
8
|
+
export async function getUniqueWalletSetupFunctions(walletSetupDirPath: string) {
|
|
9
|
+
const fileList = await getWalletSetupFiles(walletSetupDirPath)
|
|
10
|
+
|
|
11
|
+
const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>()
|
|
12
|
+
|
|
13
|
+
const duplicatesMap: DuplicatesMap = {}
|
|
14
|
+
|
|
15
|
+
for (const fileName of fileList) {
|
|
16
|
+
const walletSetupFilePath = path.join(walletSetupDirPath, fileName)
|
|
17
|
+
const { hash, fn } = await importWalletSetupFile(walletSetupFilePath)
|
|
18
|
+
|
|
19
|
+
const entry = {
|
|
20
|
+
fileName,
|
|
21
|
+
setupFunction: fn
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Since we're checking for duplicates later, we can safely forget about overwriting.
|
|
25
|
+
setupFunctions.set(hash, entry)
|
|
26
|
+
|
|
27
|
+
if (duplicatesMap[hash]) {
|
|
28
|
+
duplicatesMap[hash]?.push(entry)
|
|
29
|
+
} else {
|
|
30
|
+
duplicatesMap[hash] = [entry]
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
throwIfThereAreDuplicatedHashes(duplicatesMap)
|
|
35
|
+
|
|
36
|
+
return setupFunctions
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function throwIfThereAreDuplicatedHashes(duplicatesMap: DuplicatesMap) {
|
|
40
|
+
const duplicatedHashes = Object.entries(duplicatesMap).filter(
|
|
41
|
+
([, setupFunctionsArray]) => setupFunctionsArray.length > 1
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
if (duplicatedHashes.length === 0) {
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const sortedDuplicatedHashes = duplicatedHashes.sort(([hashA], [hashB]) => hashA.localeCompare(hashB))
|
|
49
|
+
|
|
50
|
+
throw new Error(
|
|
51
|
+
[
|
|
52
|
+
'[GetUniqueWalletSetupFunctions] There are identical wallet setup functions:',
|
|
53
|
+
sortedDuplicatedHashes
|
|
54
|
+
.map(([hash, setupFunctionsArray]) => {
|
|
55
|
+
return `\t${hash} - [${setupFunctionsArray.map(({ fileName }) => fileName).join(', ')}]`
|
|
56
|
+
})
|
|
57
|
+
.join('\n')
|
|
58
|
+
].join('\n')
|
|
59
|
+
)
|
|
60
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
|
|
3
|
+
const SETUP_FILE_FILTER_REGEX = /\.setup\.(ts|js|mjs)$/
|
|
4
|
+
|
|
5
|
+
export async function getWalletSetupFiles(walletSetupDirPath: string) {
|
|
6
|
+
await fs.access(walletSetupDirPath).catch((e) => {
|
|
7
|
+
// TODO: This should utilize the `e.code`.
|
|
8
|
+
// TODO: See: https://nodejs.org/api/fs.html#fsexistspath-callback
|
|
9
|
+
// TODO: See fix: https://stackoverflow.com/a/49562477
|
|
10
|
+
// TODO: Replace ALL occurrences of `fs.exists` with `fs.access`.
|
|
11
|
+
if (e instanceof Error && e.message.includes('ENOENT')) {
|
|
12
|
+
throw new Error(`[GetWalletSetupFiles] Wallet setup directory does not exist at ${walletSetupDirPath}`)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw e
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const setupFilesFilter = (file: string) => file.match(SETUP_FILE_FILTER_REGEX)
|
|
19
|
+
const fileList = (await fs.readdir(walletSetupDirPath)).filter(setupFilesFilter)
|
|
20
|
+
|
|
21
|
+
if (!fileList.length) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
[
|
|
24
|
+
`[GetWalletSetupFiles] No wallet setup files found at ${walletSetupDirPath}`,
|
|
25
|
+
'Remember that all wallet setup files must end with `.setup.{ts,js,mjs}` extension!'
|
|
26
|
+
].join('\n')
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return fileList.sort()
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
// Same length as the file part (first part before the `-`) of a Playwright Test ID.
|
|
4
|
+
export const WALLET_SETUP_FUNC_HASH_LENGTH = 10
|
|
5
|
+
|
|
6
|
+
export function getWalletSetupFuncHash(walletSetupString: string) {
|
|
7
|
+
const hash = createHash('shake256', {
|
|
8
|
+
outputLength: WALLET_SETUP_FUNC_HASH_LENGTH
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
return hash.update(walletSetupString).digest('hex')
|
|
12
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import type { WalletSetupFunction } from '../defineWalletSetup'
|
|
3
|
+
|
|
4
|
+
// TODO: Add hash length validation.
|
|
5
|
+
const WalletSetupModule = z.object({
|
|
6
|
+
default: z.object({
|
|
7
|
+
hash: z.string(),
|
|
8
|
+
fn: z.function().returns(z.promise(z.void()))
|
|
9
|
+
})
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
export async function importWalletSetupFile(walletSetupFilePath: string) {
|
|
13
|
+
const walletSetupModule = await import(walletSetupFilePath)
|
|
14
|
+
|
|
15
|
+
const result = WalletSetupModule.safeParse(walletSetupModule)
|
|
16
|
+
if (!result.success) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
[
|
|
19
|
+
`[ImportWalletSetupFile] Invalid wallet setup function at ${walletSetupFilePath}`,
|
|
20
|
+
'Remember that all wallet setup files must export the wallet setup function as a default export!'
|
|
21
|
+
].join('\n')
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const { hash, fn } = result.data.default
|
|
26
|
+
|
|
27
|
+
// TODO: Can we somehow validate this function type with Zod?
|
|
28
|
+
return {
|
|
29
|
+
hash,
|
|
30
|
+
fn: fn as WalletSetupFunction
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import fs from 'fs-extra'
|
|
2
|
+
|
|
3
|
+
export async function isDirEmpty(dirPath: string) {
|
|
4
|
+
try {
|
|
5
|
+
const files = await fs.readdir(dirPath)
|
|
6
|
+
|
|
7
|
+
return files.length === 0
|
|
8
|
+
} catch (e) {
|
|
9
|
+
if (e instanceof Error && e.message.includes('ENOENT')) {
|
|
10
|
+
return true
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
throw e
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AxiosProgressEvent } from 'axios'
|
|
2
|
+
import ProgressBar from 'progress'
|
|
3
|
+
import { bytesToMegabytes } from './bytesToMegabytes'
|
|
4
|
+
|
|
5
|
+
export function onDownloadProgress(url: string, fileName: string) {
|
|
6
|
+
let progressBar: ProgressBar
|
|
7
|
+
let lastDownloadedBytes = 0
|
|
8
|
+
|
|
9
|
+
return ({ loaded: downloadedBytes, total: totalDownloadBytes }: AxiosProgressEvent) => {
|
|
10
|
+
if (!totalDownloadBytes) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
`[DownloadFile] Request returned total download bytes as 0. This should never happen, and it means that the target file is empty. URL: ${url}`
|
|
13
|
+
)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!progressBar) {
|
|
17
|
+
progressBar = getDownloadProgressBar(url, fileName, totalDownloadBytes)
|
|
18
|
+
} else {
|
|
19
|
+
const delta = downloadedBytes - lastDownloadedBytes
|
|
20
|
+
lastDownloadedBytes = downloadedBytes
|
|
21
|
+
progressBar.tick(delta)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getDownloadProgressBar(url: string, fileName: string, totalBytes: number) {
|
|
27
|
+
// TODO: This header should be based on the wallet config.
|
|
28
|
+
const barHeader = url.startsWith('https://github.com/MetaMask/metamask-extension/releases/download/')
|
|
29
|
+
? '🦊 MetaMask'
|
|
30
|
+
: fileName
|
|
31
|
+
|
|
32
|
+
const downloadSize = `${bytesToMegabytes(totalBytes)} MB`
|
|
33
|
+
|
|
34
|
+
return new ProgressBar(`${barHeader} (${downloadSize}) [:bar] :percent :etas`, {
|
|
35
|
+
width: 20,
|
|
36
|
+
complete: '=',
|
|
37
|
+
incomplete: ' ',
|
|
38
|
+
total: totalBytes
|
|
39
|
+
})
|
|
40
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RimrafAsyncOptions, rimraf } from 'rimraf'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sometimes the browser is still closing when we remove the context directory which results in an error/unwanted behaviour.
|
|
5
|
+
* We retry a few times to make sure it's removed.
|
|
6
|
+
*/
|
|
7
|
+
const opts: RimrafAsyncOptions = {
|
|
8
|
+
maxRetries: 10
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Removes the temporary context directory created per-worker basis.
|
|
13
|
+
*
|
|
14
|
+
* This function mirrors the one found in the Playwright source code:
|
|
15
|
+
* https://github.com/microsoft/playwright/blob/d1d5fc67dc684a5d4b682749e59bba8cc0ad14de/packages/playwright-core/src/utils/processLauncher.ts#L142
|
|
16
|
+
*/
|
|
17
|
+
export async function removeTempContextDir(contextDirPath: string) {
|
|
18
|
+
return new Promise<null | Error>((resolve) => {
|
|
19
|
+
rimraf(contextDirPath, opts)
|
|
20
|
+
.then(() => resolve(null))
|
|
21
|
+
.catch((e: Error) => resolve(e))
|
|
22
|
+
})
|
|
23
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'fs-extra'
|
|
3
|
+
import type { WalletSetupFunction } from '../defineWalletSetup'
|
|
4
|
+
import { ensureCacheDirExists } from '../ensureCacheDirExists'
|
|
5
|
+
import { createCacheForWalletSetupFunction } from './createCacheForWalletSetupFunction'
|
|
6
|
+
import { isDirEmpty } from './isDirEmpty'
|
|
7
|
+
|
|
8
|
+
export async function triggerCacheCreation(
|
|
9
|
+
setupFunctions: Map<string, { fileName: string; setupFunction: WalletSetupFunction }>,
|
|
10
|
+
hashes: string[],
|
|
11
|
+
downloadExtension: () => Promise<string>,
|
|
12
|
+
force: boolean
|
|
13
|
+
) {
|
|
14
|
+
const cacheDirPath = ensureCacheDirExists()
|
|
15
|
+
const extensionPath = await downloadExtension()
|
|
16
|
+
|
|
17
|
+
return await Promise.all(
|
|
18
|
+
Array.from(setupFunctions).map(async ([_, { fileName, setupFunction }], index) => {
|
|
19
|
+
if (!hashes[index]) {
|
|
20
|
+
throw new Error(`No hash found for ${fileName}`)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const funcHash = hashes[index]
|
|
24
|
+
|
|
25
|
+
const cachePath = path.join(cacheDirPath, funcHash || 'unknown')
|
|
26
|
+
const doesCacheDirExist = await fs.exists(cachePath)
|
|
27
|
+
const isCacheDirEmpty = await isDirEmpty(cachePath)
|
|
28
|
+
|
|
29
|
+
if (doesCacheDirExist) {
|
|
30
|
+
if (isCacheDirEmpty) {
|
|
31
|
+
// In case of incorrect Playwright setup, the cache dir will be empty. For now, we're just deleting it.
|
|
32
|
+
await fs.remove(cachePath)
|
|
33
|
+
} else {
|
|
34
|
+
if (!force) {
|
|
35
|
+
console.log(`Cache already exists for ${funcHash}. Skipping...`)
|
|
36
|
+
return
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.log(`Cache already exists for ${funcHash} but force flag is set. Deleting cache...`)
|
|
40
|
+
await fs.remove(cachePath)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const fileNameWithCorrectExtension = fileName.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}')
|
|
45
|
+
console.log(`Triggering cache creation for: ${funcHash} (${fileNameWithCorrectExtension})`)
|
|
46
|
+
// We're not inferring the return type here to make sure we don't accidentally await the function.
|
|
47
|
+
return createCacheForWalletSetupFunction(extensionPath, cachePath, setupFunction, fileNameWithCorrectExtension)
|
|
48
|
+
})
|
|
49
|
+
)
|
|
50
|
+
}
|