nginx / njs

A subset of JavaScript language to use in nginx
http://nginx.org/en/docs/njs/
BSD 2-Clause "Simplified" License
1.26k stars 156 forks source link

Support Web APIs. #299

Open hongzhidao opened 4 years ago

hongzhidao commented 4 years ago

@xeioex @drsm

Web APIs is an alternative usage. https://developer.mozilla.org/en-US/docs/Web/API/Response/Response It will behave in njs like below.

function hello(request) {
      return new Response('hello');
}

function subrequest(request) {
      var sr = new Request('/sub');
      sr.fetch()
      .then(response) {
          return response;
      };
}

In the beginning, njs has individual response object. foo(req, res). After that, res is merged into req, so we have to rename some methods like headersIn, headersOut. If we introduce these things, Request, Response, Headers, Body, I believe it can be easier to use, and we just implement the standard methods with them. The second change is that the subrequest is also easier to use. Indeed, users need not know what subrequest is, they just want to send a request. (But the implementation will be upon on nginx subrequest). Still, further, it can be welcome after async, await are introduced.

Highly welcome to your suggestions. I'd like to try it without changing current usage by put them in precontent phase.

I'm still not familiar with async, await, Promise, it's good to have a better example of how to write with subrequest below. @drsm

BTW, do you know whether the above objects are standard belong to js? I'm wondering which software has already implemented them, where are they used?