nginScript (njs) feedback

I’m trying to implement a routing logic with nginScript. I haven’t been
successful because I miss important javascript feature.

I would like to give you my feedback just in case it may help to improve
njs (from more important/blocking to less important):

  • There is no way to iterate through the keys of an object. It is not
    supported none of the following methods:
    • Object.keys
    • Object.getOwnPropertyNames()
    • for … in
  • I would like to use/import nginx variables. Something like
    [njs] Allow access to nginx vars via the njs $v variable
    would
    be great.
  • I miss string.split function. Probably map, reduce, and many other
    methods as well.
  • It would be nice to have some “singleton” support where I could set
    a
    variable that is evaluated only once (first time is required or when
    nginx
    is started/reloaded). It’s clear that it should not be related to
    request/response but this approach would improve performance by
    avoiding
    executing same thing for each request. It would be similar to
    importing a
    javascript module (it’s only imported once and global vars and
    requirements
    are satisfied only once).
  • I would like to include javascript modules. I’m using a directive:
    “include /etc/nginx/js/*.js;” but this .js files need to include the
    js_set
    $xxx “…” which makes the file to be invalid javascript (and you may
    miss
    some features from your javascript IDE).
  • It would be nice to access a cookie by name (it wouldn’t be
    necessary
    if I could access to a nginx var like $v.cookie_XXX).
  • Logging function would be nice to help debugging.

Hope it can be helpful

I’ve continued with my attempt to integrate my routing logic as
javascript
code.

Regarding my previous feedback, it is possible to iterate the keys of an
object with “for … in”. However, you cannot use “var” keyword to
assign
the result of the loop. For example, the following code fails:
for (var i = 0; i < elements.length; i++) {…}
but the following one works:
for (i = 0; i < elements.length; i++) {…}

I’ve found a blocking point. There is a limitation of the size of the
script. It could be solved easily if I could import other nginx vars. My
module currently has 4202 chars (or 123 lines). This is really strange
because I don’t consider it to be too big. The error raised is:

2016/06/23 11:48:42 [emerg] 5390#5390: too long parameter, probably
missing
terminating “”" character in /etc/nginx/js/routing.js:1

Is there any solution for this problem?

On 23 Jun 2016, at 13:58, Jorge L. [email protected] wrote:

I’ve continued with my attempt to integrate my routing logic as javascript code.

Regarding my previous feedback, it is possible to iterate the keys of an object
with “for … in”. However, you cannot use “var” keyword to assign the result of
the loop. For example, the following code fails:
for (var i = 0; i < elements.length; i++) {…}
but the following one works:
for (i = 0; i < elements.length; i++) {}

Yes, var declaration inside for is not currently supported, it will be
fixed soon.

I’ve found a blocking point. There is a limitation of the size of the script. It
could be solved easily if I could import other nginx vars. My module currently has
4202 chars (or 123 lines). This is really strange because I don’t consider it to
be too big. The error raised is:

2016/06/23 11:48:42 [emerg] 5390#5390: too long parameter, probably missing
terminating “”" character in /etc/nginx/js/routing.js:1

Is there any solution for this problem?

Currently no. The string parameter length is limited by 4K. We are going
to support js in files.

On Wed, Jun 22, 2016 at 8:35 PM, Jorge L. [email protected] wrote:
I’m trying to implement a routing logic with nginScript. I haven’t been
successful because I miss important javascript feature.

I would like to give you my feedback just in case it may help to improve njs
(from more important/blocking to less important):

There is no way to iterate through the keys of an object. It is not supported
none of the following methods:
Object.keys
Object.getOwnPropertyNames()
for … in
I would like to use/import nginx variables. Something like
[njs] Allow access to nginx vars via the njs $v variable would be
great.
We consider another interface: req.variables object.
I miss string.split function. Probably map, reduce, and many other methods as
well.
It would be nice to have some “singleton” support where I could set a variable
that is evaluated only once (first time is required or when nginx is
started/reloaded). It’s clear that it should not be related to request/response
but this approach would improve performance by avoiding executing same thing for
each request. It would be similar to importing a javascript module (it’s only
imported once and global vars and requirements are satisfied only once).
I would like to include javascript modules. I’m using a directive: “include
/etc/nginx/js/*.js;” but this .js files need to include the js_set $xxx “…”
which makes the file to be invalid javascript (and you may miss some features from
your javascript IDE).
It would be nice to access a cookie by name (it wouldn’t be necessary if I could
access to a nginx var like $v.cookie_XXX).
We consider another interface: req.cookies object.
Logging function would be nice to help debugging.
Hope it can be helpful

Thank you for your feedback!
The full String object support, global objects, logging, and files are
on the way.

​Thanks for your reply. It sounds great!