-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess-Worker.ps1
More file actions
104 lines (84 loc) · 3.79 KB
/
Copy pathProcess-Worker.ps1
File metadata and controls
104 lines (84 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env pwsh
# Process-Worker.ps1
# Processes the worker template and injects build-time generated constants
param(
[string]$TemplateFile = "cloudflare-worker.template.js",
[string]$OutputFile = "cloudflare-worker.js",
[string]$ConstantsFile = "Generated/build-constants.js",
[switch]$Verbose
)
$ErrorActionPreference = "Stop"
function Write-BuildLog {
param([string]$Message, [string]$Level = "INFO")
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$logMessage = "[$timestamp] [$Level] $Message"
Write-Host $logMessage
if ($Verbose) {
Write-Host $logMessage -ForegroundColor $(if ($Level -eq "ERROR") { "Red" } elseif ($Level -eq "WARN") { "Yellow" } else { "Green" })
}
}
try {
Write-BuildLog "Starting worker processing..."
# Check if template file exists
if (!(Test-Path $TemplateFile)) {
throw "Template file not found: $TemplateFile"
}
# Check if constants file exists
if (!(Test-Path $ConstantsFile)) {
throw "Constants file not found: $ConstantsFile. Run Generate-AuthKey.ps1 first."
}
Write-BuildLog "Reading template file: $TemplateFile"
$templateContent = Get-Content -Path $TemplateFile -Raw
Write-BuildLog "Reading constants file: $ConstantsFile"
$constantsContent = Get-Content -Path $ConstantsFile -Raw
# Remove the CommonJS export from constants for browser compatibility
$constantsContent = $constantsContent -replace "// Export for use in worker[\s\S]*$", ""
# Inject the constants into the template
$processedContent = $templateContent -replace "__BUILD_CONSTANTS_INJECTION_POINT__", $constantsContent
# Also update the actual AbuseIPDB API URL parameter construction
$urlConstruction = @"
const apiUrl = new URL(ABUSEIPDB_API_URL);
apiUrl.searchParams.set('ipAddress', ipAddress);
apiUrl.searchParams.set('maxAgeInDays', maxAgeInDays);
if (verbose) {
apiUrl.searchParams.set('verbose', '');
}
// Make request to AbuseIPDB API
const abuseIPDBResponse = await fetch(apiUrl.toString(), {
"@
# Replace the fetch call with proper URL construction
$processedContent = $processedContent -replace "// Make request to AbuseIPDB API[\s\S]*?const abuseIPDBResponse = await fetch\(ABUSEIPDB_API_URL, \{", $urlConstruction
Write-BuildLog "Writing processed worker to: $OutputFile"
$processedContent | Out-File -FilePath $OutputFile -Encoding UTF8 -NoNewline
# Validate the generated JavaScript
Write-BuildLog "Validating generated JavaScript..."
# Check for syntax errors using Node.js if available
$nodeCheck = Get-Command "node" -ErrorAction SilentlyContinue
if ($nodeCheck) {
$syntaxCheck = & node -c $OutputFile 2>&1
if ($LASTEXITCODE -eq 0) {
Write-BuildLog "JavaScript syntax validation passed"
} else {
throw "JavaScript syntax error: $syntaxCheck"
}
} else {
Write-BuildLog "Node.js not found, skipping syntax validation" "WARN"
}
# Get file sizes for logging
$templateSize = (Get-Item $TemplateFile).Length
$outputSize = (Get-Item $OutputFile).Length
Write-BuildLog "Worker processing completed successfully"
Write-BuildLog "Template size: $templateSize bytes"
Write-BuildLog "Output size: $outputSize bytes"
# Output information for build systems
if ($env:GITHUB_ACTIONS -eq "true") {
"worker-file=$OutputFile" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding UTF8
"worker-size=$outputSize" | Out-File -FilePath $env:GITHUB_OUTPUT -Append -Encoding UTF8
}
exit 0
}
catch {
Write-BuildLog "Error processing worker: $($_.Exception.Message)" "ERROR"
Write-BuildLog "Stack trace: $($_.ScriptStackTrace)" "ERROR"
exit 1
}