-
Notifications
You must be signed in to change notification settings - Fork 1
feat: embedded local dev script #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
const { spawn } = require("child_process"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const dotenv = require("dotenv"); | ||
|
||
// Load environment variables from .env file | ||
const envPath = path.resolve(__dirname, "../.env"); | ||
dotenv.config({ path: envPath }); | ||
|
||
// Get or set default WEBAPP_URL from environment | ||
const WEBAPP_URL = process.env.AB_WEBAPP_URL || "https://local.airbyte.dev"; | ||
console.log(`Using WEBAPP_URL: ${WEBAPP_URL}`); | ||
|
||
// Function to decode base64 (Node.js replacement for browser's atob) | ||
function atob(str) { | ||
return Buffer.from(str, "base64").toString("binary"); | ||
} | ||
|
||
// Function to parse and manipulate the embedded token | ||
function manipulateEmbeddedToken(token) { | ||
try { | ||
console.log("Intercepting and manipulating embedded token..."); | ||
|
||
// Make sure the base64 string is properly padded | ||
const padded = token.padEnd(token.length + ((4 - (token.length % 4)) % 4), "="); | ||
|
||
// Replace URL-safe characters | ||
const base64 = padded.replace(/-/g, "+").replace(/_/g, "/"); | ||
|
||
const decodedToken = JSON.parse(atob(base64)); | ||
|
||
// Log original token data | ||
console.log("Original widget URL:", decodedToken.widgetUrl); | ||
|
||
// Extract and modify the widget URL | ||
const originalWidgetUrl = new URL(decodedToken.widgetUrl); | ||
|
||
const newWidgetUrl = new URL( | ||
`${WEBAPP_URL}${originalWidgetUrl.pathname.toString()}?${originalWidgetUrl.searchParams.toString()}` | ||
); | ||
|
||
decodedToken.widgetUrl = newWidgetUrl.toString(); | ||
|
||
console.log("Modifying token with admin token from .env"); | ||
// Allow optionally overriding the embedded scoped token with an instance admin one | ||
decodedToken.token = process.env.AB_ADMIN_TOKEN ?? decodedToken.token; | ||
joeykmh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
joeykmh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
// Log the modified widget URL | ||
console.log("Modified widget URL:", decodedToken.widgetUrl); | ||
|
||
// Return the manipulated token | ||
return btoa(JSON.stringify(decodedToken)); | ||
} catch (error) { | ||
console.error("Error manipulating token:", error); | ||
return token; // Return original token if there's an error | ||
} | ||
} | ||
|
||
// Get the current embedded token | ||
const originalToken = process.env.VITE_AB_EMBEDDED_TOKEN || ""; | ||
|
||
// Manipulate the token | ||
const manipulatedToken = manipulateEmbeddedToken(originalToken); | ||
|
||
// Create a temporary .env file with the manipulated token | ||
const tempEnvPath = path.resolve(__dirname, "../.env.local"); | ||
const envContent = `VITE_AB_EMBEDDED_TOKEN="${manipulatedToken}"`; | ||
|
||
fs.writeFileSync(tempEnvPath, envContent); | ||
console.log("Created temporary .env.local with manipulated token"); | ||
|
||
// Start the Vite dev server | ||
console.log("Starting Vite dev server..."); | ||
const viteProcess = spawn("pnpm", ["dev"], { | ||
stdio: "inherit", | ||
shell: true, | ||
cwd: path.resolve(__dirname, ".."), | ||
env: { | ||
...process.env, | ||
VITE_AB_EMBEDDED_TOKEN: manipulatedToken, | ||
}, | ||
}); | ||
|
||
// Handle process exit | ||
process.on("SIGINT", () => { | ||
console.log("Cleaning up..."); | ||
|
||
// Clean up temporary .env file | ||
if (fs.existsSync(tempEnvPath)) { | ||
fs.unlinkSync(tempEnvPath); | ||
console.log("Removed temporary .env.local file"); | ||
} | ||
|
||
viteProcess.kill("SIGINT"); | ||
process.exit(); | ||
}); | ||
|
||
viteProcess.on("exit", () => { | ||
// Clean up temporary .env file | ||
if (fs.existsSync(tempEnvPath)) { | ||
fs.unlinkSync(tempEnvPath); | ||
console.log("Removed temporary .env.local file"); | ||
} | ||
|
||
process.exit(); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@josephkmh I don't have a super strong opinion about how we solve for this. If you'd rather go about this another way, I am all ears.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine to me!