Skip to content
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

Add logic to represent booting state within preview panel and parse stdout from vite to get port #349

Merged
merged 2 commits into from
Oct 12, 2024

Conversation

1egoman
Copy link
Collaborator

@1egoman 1egoman commented Oct 11, 2024

Currently, the preview panel in the app builder is a bit unreliable - when it opens, the iframe sometimes is pointing to the wrong vite server or nothing at all.

This pull request attempts to make this behavior more consistient, by doing three things:

  1. When the vite server starts, the srcbook code which invokes the process monitors the stdout coming from the process, and picks up the port from stdout using a regex. This port is then what the client opens, not a hardcoded port like how the logic worked previously.
  2. When the panel is opened and the preview:start message is sent to the server, the server now sends back a preview:status message including a status of "booting". - the client then shows a loading indicator. Once a port is detected in the stdout, a secondary message is sent to the client including this port, and the loading indicator goes away, and an iframe is rendered going to this port.
  3. When the app builder is reloaded, the port previously extracted from 1. is sent back to the client again, resulting in the preview panel opening back up to the correct url.

Comment on lines +25 to +30
type ProcessMetadata = {
process: ChildProcess;
port: number | null;
};

const processMetadata = new Map<string, ProcessMetadata>();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that now, processes is called processMetadata and includes a process plus associated metadata (currently just the port it is running on).

Comment on lines 11 to 19
switch (status) {
case "booting":
case "connecting":
return (
<div className={cn("flex justify-center items-center w-full h-full", props.className)}>
<span className="text-tertiary-foreground">Booting...</span>
</div>
);
case "running":
Copy link
Collaborator Author

@1egoman 1egoman Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, this is what the loading state looks like. An official design (I couldn't find one for something like this in figma) would be appreciated if something less plain is desired!

Screenshot 2024-10-11 at 4 44 14 PM

@@ -18,9 +18,16 @@ import { loadApp } from '../../apps/app.mjs';
import { fileUpdated, pathToApp } from '../../apps/disk.mjs';
import { vite } from '../../exec.mjs';

const VITE_PORT_REGEX = /Local:.*http:\/\/localhost:([0-9]{1,4})/;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the regex for extracting the port vite picks from stdout.

@1egoman 1egoman marked this pull request as ready for review October 11, 2024 20:55
const potentialPortMatch = VITE_PORT_REGEX.exec(encodedData);
if (potentialPortMatch) {
const portString = potentialPortMatch[1]!;
const port = parseInt(portString, 10);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number(portString) :)

const encodedData = data.toString('utf8');
console.log(encodedData);

const potentialPortMatch = VITE_PORT_REGEX.exec(encodedData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that chunks of data are not guaranteed to arrive deterministically in a way that we expect. This is why eg stream parsing is typically stateful. While I expect this to behave as we want it to, it's not guaranteed.

Copy link
Contributor

@nichochar nichochar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, can we get this in?

type AppContextType = MessageContextType<'appId'>;

const processes = new Map<string, ChildProcess>();
type ProcessMetadata = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also have the status "running"?

And potentially exit code

args: [],
cwd: pathToApp(app.externalId),
stdout: (data) => {
console.log(data.toString('utf8'));
const encodedData = data.toString('utf8');
console.log(encodedData);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the 2 lines? Is there a behavior change?

@nichochar nichochar merged commit d32b748 into main Oct 12, 2024
3 checks passed
@nichochar nichochar deleted the preview-pane branch October 12, 2024 18:17
BeRecursive22 pushed a commit to BeRecursive22/srcbook that referenced this pull request Oct 13, 2024
…tdout from vite to get port (srcbookdev#349)

* feat: add logic to represent booting state within preview panel and parse stdout from vite to get port

* fix: run npm run format
nichochar pushed a commit that referenced this pull request Oct 14, 2024
#333)

* [FIX]:285 - Type proccess.env based off the available secrets in a notebook

* [ADD] - changeset

* [FIX] - linting and formatting

* Style apps page sidebar and header (#340)

* Update sans font (#341)

* File tree functionality and styles (#344)

* Add support for deleting and renaming files (#345)

* Add support for deleting and renaming files

* Fix lint

* Exclude a few directories

* Implement top level create file and folder (#346)

* Homepage UI Rework! (#348)

* inital homepage rework, sections, card style, srcbook to notebook

* fix lil linter error

* move user facing Srcbook (in ref to notebook) to Notebook

* style up to match figma

* some cleanup around purples on create app (#351)

* fix CI bug with pnpm CI mismatch in release (#354)

* Add logic to represent booting state within preview panel and parse stdout from vite to get port (#349)

* feat: add logic to represent booting state within preview panel and parse stdout from vite to get port

* fix: run npm run format

* Add folder delete and rename behavior (#355)

* Add folder delete and rename behavior

* Remove unused files state

* Fix lint warnings

* [FIX] - env type errors

* put the env.d.ts file under root directory instead of src

* remove cell:format

---------

Co-authored-by: Ben Reinhart <[email protected]>
Co-authored-by: versecafe <[email protected]>
Co-authored-by: Ryan Gaus <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants