reload icon indicating copy to clipboard operation
reload copied to clipboard

koa

Open adriangrigore opened this issue 10 years ago • 15 comments

Consider adding support for koa?

adriangrigore avatar Apr 14 '15 14:04 adriangrigore

Sure. PR welcomed.

On Tuesday, April 14, 2015, Adrian Grigore [email protected] wrote:

Consider adding support for koa?

— Reply to this email directly or view it on GitHub https://github.com/jprichardson/reload/issues/15.

Simple & Secure Bitcoin Wallet: https://www.coinbolt.com Bitcoin / JavaScript: http://cryptocoinjs.com Follow JP Richardson on Twitter: https://twitter.com/jprichardson

jprichardson avatar Apr 14 '15 18:04 jprichardson

+1 for this, would love to see support for koa.

nahtnam avatar Apr 27 '17 00:04 nahtnam

@jprichardson I am working on a PR, and it seems to work, but I get these errors in the log.

reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

I don't know this is because reload is waiting for the server to reboot or if there is an error in the websocket implementation with koa. Here is the entire log.

reload.js:7 Reload Script Loaded
reload.js:25 Page Loaded - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:90 WebSocket connection to 'ws://localhost:3000/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED // IS THIS BECAUSE THE SERVER IS RESTARTING OR BECAUSE OF AN ERROR?
(anonymous) @ reload.js:90
reload.js:80 Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
reload.js:68 Socket Closed - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened
reload.js:54 Reloaded
reload.js:33 Navigated away from the current URL
Navigated to http://localhost:3000/
reload.js:7 Reload Script Loaded
reload.js:25 Page Loaded - Calling webSocketWaiter
reload.js:87 Waiting for socket
reload.js:48 Socket Opened

nahtnam avatar Apr 27 '17 01:04 nahtnam

It should wait for the server to be running after a restart of the server before it tries to open a socket connection. It those cases where the error is printing is the server started? The code you are using would be helpful to help you debug

alallier avatar Apr 27 '17 18:04 alallier

@alallier So I got it working, but there is one problem. Koa makes use of async/await which pushes the node requirement to 7.6.0. I dont think it would be a good idea to include it in this library, unless you want to transpile the code.

nahtnam avatar Apr 27 '17 20:04 nahtnam

The travis-ci tests try to build v4 of node, so I don't think that bumping it up to 7.6 is going to be a good idea. I'm only using 6.10.2 at the moment because that is the stable release, and that is what most people would be using

ghost avatar Apr 28 '17 16:04 ghost

Although, it installed on my version of node (6.10.2) so maybe you can use koa, just not the async. on https://www.npmjs.com/package/koa it says that you can use either the async function or the common function. Maybe this will work, just using the common function rather than the async.

ghost avatar Apr 28 '17 16:04 ghost

@alallier What are we doing about this?

ghost avatar Jul 20 '17 21:07 ghost

Sorry, I have turned around and started working in other languages. I do not think I will be able to complete this.

nahtnam avatar Jul 20 '17 22:07 nahtnam

@adriangrigore Do you think that you can do anything about this?

ghost avatar Jul 20 '17 22:07 ghost

@nahtnam That is okay. Do you mind if we take your work (from #90) and adapt and build from it?

alallier avatar Jul 20 '17 22:07 alallier

Of course not, please go ahead and do anything you would like. I could also answer any questions you have.

Most of the code you will need is here: https://github.com/alallier/reload/pull/90/commits/6c15d717cf853547323dc08a0d0d8dc972ca6111

It would be a good idea to tell anyone using koa to put the middleware near the top in the README.

nahtnam avatar Jul 20 '17 22:07 nahtnam

I studied Koa to find it can process middleware just as Express processes middleware.

The problem with reload, is it straps itself to Express and/or to Koa INSTEAD of offering a middleware plugin to allow others to strap it to there.

I solved this in my fork by removing Express and offering a middleware function as seen here: https://github.com/AckerApple/reload/blob/master/lib/reload.js#L47

AckerApple avatar Jul 20 '17 22:07 AckerApple

@yamboy1 Sorry, didn't get the chance to even build anything with koa, was just experimenting at the time.I don't think I can be of much use.

adriangrigore avatar Jul 21 '17 11:07 adriangrigore

Here is a small wrapper to pretend koa-router is actually an Express server:


function wrapRouterForReload(router) {
  const wrappedRouter = function() { }
  wrappedRouter.get = (route, callback) => {
    router.get(route, (ctx) => {
      callback(undefined, {
        type: (type) => ctx.type = type,
        send: (body) => ctx.body = body
      });
    });
  }
  return wrappedRouter;
}

You can then do:

const wrappedRouter = wrapRouterForReload(router);
reloader = reload(wrappedRouter);

Obviously this may break with future reload versions.

mkalam-alami avatar Mar 15 '20 08:03 mkalam-alami