-
Notifications
You must be signed in to change notification settings - Fork 1.1k
App Hosting Emulator Prototype #7505
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b7bece4
set up App Hosting emulator boilerplate
mathu97 608e3cb
mock out apphosting emulator functions
mathu97 12bb8d6
working nextjs setupwq
mathu97 111b328
move apphosting emulator behind a flag and refactor
mathu97 a6a2a6f
remove comments
mathu97 9ff7a3d
clean pup
mathu97 e9a353a
remove unused logger
mathu97 15f9756
add test for start func
mathu97 1129f27
fix
mathu97 bdcd151
update schema
mathu97 3d6acc4
address comments
mathu97 7db8187
Update src/emulator/apphosting/serve.ts
mathu97 e3b48fd
Merge branch 'master' into feature/apphosting-emulator
mathu97 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { EmulatorLogger } from "../emulatorLogger"; | ||
| import { EmulatorInfo, EmulatorInstance, Emulators } from "../types"; | ||
| import { start as apphostingStart } from "./serve"; | ||
| interface AppHostingEmulatorArgs { | ||
| options?: any; | ||
| port?: number; | ||
| host?: string; | ||
| } | ||
|
|
||
| /** | ||
| * An emulator instance for Firebase's App Hosting product. This class provides a simulated | ||
| * environment for testing App Hosting features locally. | ||
| */ | ||
| export class AppHostingEmulator implements EmulatorInstance { | ||
| private logger = EmulatorLogger.forEmulator(Emulators.APPHOSTING); | ||
| constructor(private args: AppHostingEmulatorArgs) {} | ||
|
|
||
| async start(): Promise<void> { | ||
| this.args.options.host = this.args.host; | ||
| this.args.options.port = this.args.port; | ||
|
|
||
| this.logger.logLabeled("INFO", Emulators.APPHOSTING, "starting apphosting emulator"); | ||
| const { port } = await apphostingStart(this.args.options); | ||
| this.logger.logLabeled("INFO", Emulators.APPHOSTING, `serving on port ${port}`); | ||
| } | ||
|
|
||
| connect(): Promise<void> { | ||
| this.logger.logLabeled("INFO", Emulators.APPHOSTING, "connecting apphosting emulator"); | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| stop(): Promise<void> { | ||
| this.logger.logLabeled("INFO", Emulators.APPHOSTING, "stopping apphosting emulator"); | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| getInfo(): EmulatorInfo { | ||
| return { | ||
| name: Emulators.APPHOSTING, | ||
| host: this.args.host!, | ||
| port: this.args.port!, | ||
| }; | ||
| } | ||
|
|
||
| getName(): Emulators { | ||
| return Emulators.APPHOSTING; | ||
| } | ||
| } | ||
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,33 @@ | ||
| import * as portUtils from "../portUtils"; | ||
| import * as sinon from "sinon"; | ||
| import * as spawn from "../../init/spawn"; | ||
| import { expect } from "chai"; | ||
| import * as serve from "./serve"; | ||
|
|
||
| describe("serve", () => { | ||
| let checkListenableStub: sinon.SinonStub; | ||
| let wrapSpawnStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| checkListenableStub = sinon.stub(portUtils, "checkListenable"); | ||
| wrapSpawnStub = sinon.stub(spawn, "wrapSpawn"); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| checkListenableStub.restore(); | ||
| wrapSpawnStub.restore(); | ||
| }); | ||
|
|
||
| describe("start", () => { | ||
| it("should only select an available port to serve", async () => { | ||
| checkListenableStub.onFirstCall().returns(false); | ||
| checkListenableStub.onSecondCall().returns(false); | ||
| checkListenableStub.onThirdCall().returns(true); | ||
|
|
||
| wrapSpawnStub.returns(Promise.resolve()); | ||
|
|
||
| const res = await serve.start({ host: "127.0.0.1", port: 5000 }); | ||
| expect(res.port).to.equal(5002); | ||
| }); | ||
| }); | ||
| }); |
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,38 @@ | ||
| /** | ||
| * Start the App Hosting server. | ||
| * @param options the Firebase CLI options. | ||
| */ | ||
| import { isIPv4 } from "net"; | ||
| import { checkListenable } from "../portUtils"; | ||
| import { wrapSpawn } from "../../init/spawn"; | ||
|
|
||
| /** | ||
| * Spins up a project locally by running the project's dev command. | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export async function start(options: any): Promise<{ port: number }> { | ||
| let port = options.port; | ||
| while (!(await availablePort(options.host, port))) { | ||
| port += 1; | ||
| } | ||
|
|
||
| serve(options, port); | ||
|
|
||
| return { port }; | ||
| } | ||
|
|
||
| function availablePort(host: string, port: number): Promise<boolean> { | ||
| return checkListenable({ | ||
| address: host, | ||
| port, | ||
| family: isIPv4(host) ? "IPv4" : "IPv6", | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Exported for unit testing | ||
| */ | ||
| export async function serve(options: any, port: string) { | ||
| // TODO: update to support other package managers and frameworks other than NextJS | ||
| await wrapSpawn("npm", ["run", "dev", "--", "-H", options.host, "-p", port], process.cwd()); | ||
| } |
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.