-
Notifications
You must be signed in to change notification settings - Fork 317
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
Improvements for getApi() #253
Conversation
…nce of NgrokClient
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.
I'm not so sure of the getProcessUrl
method here. It seems like it may not work everywhere, and even if it does it will be a breaking change.
If you need to check whether the process is running, we could look into something like tcp-port-used.
But in reality, I'm not sure exactly how useful checking the port is in use would be beforehand. You have to handle whether you get a client back or not, so would it be worse to handle the issue when you try to fetch the client or when you make the API call? Especially since the API could be closed down later and you wouldn't know about it.
I would suggest that perhaps the getApi
method stays the same, with the intended use that it is called in a program where ngrok.connect
is also called. Then we expose the NgrokClient
object from the main index so you can create your own. And finally, we need to extend the error handling of NgrokClient
so that if it tries to make a call and receives an error with the code
ECONNREFUSED
then it raises a different error (an NgrokApiMissing
or NgrokConnectionRefused
error, something like that) so that it can be handled by applications trying to use the API when they can't know if it is running.
What do you think?
|
||
const child = platform === 'win32' | ||
? spawn(`(Get-NetTCPConnection -LocalPort ${port}).LocalAddress`, { shell: 'powershell.exe' }) | ||
: spawn('sh', ['-c', `lsof -n -i :${port} | grep LISTEN`]) |
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 like it could be fairly brittle? It requires PowerShell to be installed, which I don't think is the default for Windows.
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.
The initial research that led me down this route turned up that PowerShell is installed by default for all versions of Windows after Windows 7 and Server 2008, both of which are officially unsupported at this point. And this was simpler to implement with PowerShell than CMD.
However, I recognize your points above. Happy to scrap this and focus more on simply exporting NgrokClient
, and extending its error handling. More questions to come about that.
@@ -39,7 +39,7 @@ declare module "ngrok" { | |||
/** | |||
* Gets the ngrok client API. | |||
*/ | |||
export function getApi(): NgrokClient | null; | |||
export function getApi(): Promise<NgrokClient | null>; |
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 would be a breaking change to the public API.
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.
Ack. Going to dial back on this as mentioned above.
Closing this old PR out and will submit a fresh one with the feedback received |
Solution for #252
Changes
index.js
-getApi()
ngrokClient
is defined, return if so. Else, CallgetProcessUrl
to get the process URL for port 4040, and create a new instance ofNgrokClient
. Fallback to null.src/process.js
getProcessUrl
which spawns a child process and makes the platform appropriate query for port 4040's process URL.ngrok.d.ts
getApi()
type definition to return a nullable promise.test/ngrok.guest.spec.js
"getting internal api wrapper"
test case to callgetApi()
with async/await."should be an instance of NgrokClient"
.Tests run successfully on Mac, Windows 10, and Linux. Open to any improvements!