Mercurial > hg > nginx-site
annotate xml/en/docs/njs/node_modules.xml @ 2533:617bc29bd759
Removed phrase "This module is not built by default" from js modules.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 22 Apr 2020 16:54:18 +0100 |
parents | bda080989b6c |
children | 4c8d0b37932d |
rev | line source |
---|---|
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
1 <?xml version="1.0"?> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
2 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
3 <!-- |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
4 Copyright (C) Nginx, Inc. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
5 --> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
6 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
8 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
9 <article name="Using node modules with njs" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
10 link="/en/docs/njs/node_modules.html" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
11 lang="en" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
12 rev="3"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
13 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
14 <section id="intro" name="Introduction"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
15 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
16 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
17 Often, a developer wants to use 3rd-party code, usually available as a library |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
18 of some kind. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
19 In the Javascript world, the concept of a module is relatively new, so there |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
20 was no standard until recently. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
21 Many platforms (browsers) still don't support modules, which makes code |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
22 reuse harder. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
23 The njs does not (yet) support modules, too. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
24 This article describes ways to overcome this limitation, using the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
25 <link url="https://nodejs.org/">Node.js</link> ecosystem as an example. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
26 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
27 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
28 <note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
29 Examples in this article use features that appeared in |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
30 <link doc="index.xml">njs</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
31 <link doc="changes.xml" id="njs0.3.8">0.3.8</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
32 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
33 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
34 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
35 There is a number of issues that may arise when 3rd-party code is added to njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
36 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
37 <list type="bullet"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
38 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
39 <listitem>Multiple files that reference each other, and their |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
40 dependencies</listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
41 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
42 <listitem>Platform-specific APIs</listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
43 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
44 <listitem>Modern standard language constructions</listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
45 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
46 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
47 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
48 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
49 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
50 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
51 The good news is that such problems are not something new or |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
52 specific to njs. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
53 Javascript developers face them daily when trying to support multiple |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
54 disparate platforms with very different properties. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
55 There are instruments designed to resolve the above-mentioned issues. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
56 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
57 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
58 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
59 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
60 <list type="bullet"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
61 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
62 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
63 Multiple files that reference each other, and their dependencies |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
64 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
65 This can be solved by merging all the interdependent code into a single file. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
66 Tools like |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
67 <link url="http://browserify.org/">browserify</link> or |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
68 <link url="https://webpack.js.org/">webpack</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
69 accept an entire project and produce a single file containing |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
70 your code and all the dependencies. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
71 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
72 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
73 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
74 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
75 Platform-specific APIs |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
76 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
77 You can use multiple libraries that implement such APIs in a platform-agnostic |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
78 manner (at the expense of performance, though). |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
79 Particular features can also be implemented using the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
80 <link url="https://polyfill.io/v3/">polyfill</link> approach. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
81 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
82 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
83 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
84 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
85 Modern standard language constructions |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
86 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
87 Such code can be transpiled: this means performing a number of transformations |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
88 that rewrite newer language features in accordance with an older standard. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
89 For example, <link url="https://babeljs.io/"> babel</link> project can |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
90 be used to this purpose. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
91 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
92 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
93 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
94 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
95 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
96 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
97 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
98 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
99 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
100 In this guide, we will use two relatively large npm-hosted libraries: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
101 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
102 <list type="bullet"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
103 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
104 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
105 <link url="https://www.npmjs.com/package/protobufjs">protobufjs</link> - |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
106 a library for creating and parsing protobuf messages used by the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
107 <link url="https://grpc.io/">gRPC</link> protocol. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
108 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
109 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
110 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
111 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
112 <link url="https://www.npmjs.com/package/dns-packet">dns-packet</link> - |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
113 a library for processing DNS protocol packets. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
114 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
115 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
116 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
117 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
118 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
119 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
120 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
121 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
122 <section id="environment" name="Environment"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
123 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
124 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
125 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
126 <note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
127 This document mostly employs a generic approach and AVOIDS specific best |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
128 practice advices concerning Node.js and the rapidly evolving JavaScript |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
129 ecosystem. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
130 Make sure to consult the corresponding package's manual BEFORE following the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
131 steps suggested here. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
132 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
133 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
134 First (assuming Node.js is installed and operational), let's create an |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
135 empty project and install some dependencies; the commands below assume we're |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
136 in the working directory: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
137 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
138 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
139 $ mkdir my_project && cd my_project |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
140 $ npx license choose_your_license_here > LICENSE |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
141 $ npx gitignore node |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
142 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
143 $ cat > package.json <<EOF |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
144 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
145 "name": "foobar", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
146 "version": "0.0.1", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
147 "description": "", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
148 "main": "index.js", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
149 "keywords": [], |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
150 "author": "somename <some.email@example.com> (https://example.com)", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
151 "license": "some_license_here", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
152 "private": true, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
153 "scripts": { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
154 "test": "echo \"Error: no test specified\" && exit 1" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
155 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
156 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
157 EOF |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
158 $ npm init -y |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
159 $ npm install browserify |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
160 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
161 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
162 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
163 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
164 |
2481 | 165 <section id="protobuf" name="Protobufjs"> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
166 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
167 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
168 The library provides a parser for the <literal>.proto</literal> interface |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
169 definitions and a code generator for message parsing and generation. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
170 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
171 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
172 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
173 In this example, we will use the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
174 <link url="https://github.com/grpc/grpc/blob/master/examples/protos/helloworld.proto">helloworld.proto</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
175 file from the gRPC examples. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
176 Our goal is to create two messages: <literal>HelloRequest</literal> and |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
177 <literal>HelloResponse</literal>. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
178 We will use the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
179 <link url="https://github.com/protobufjs/protobuf.js/blob/master/README.md#reflection-vs-static-code">static</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
180 mode of protobufjs instead of dynamically generating classes, because |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
181 njs doesn't support adding new functions dynamically due to security |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
182 considerations. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
183 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
184 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
185 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
186 Next, the library is installed and javascript code implementing |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
187 message marshalling is generated from the protocol definition: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
188 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
189 $ npm install protobufjs |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
190 $ npx pbjs -t static-module helloworld.proto > static.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
191 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
192 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
193 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
194 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
195 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
196 Thus, the <literal>static.js</literal> file becomes our new dependency, storing |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
197 all the code we need to implement message processing. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
198 The <literal>set_buffer()</literal> function contains code that uses the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
199 library to create a buffer with the serialized <literal>HelloRequest</literal> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
200 message. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
201 The code resides in the <literal>code.js</literal> file: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
202 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
203 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
204 var pb = require('./static.js'); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
205 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
206 // Example usage of protobuf library: prepare a buffer to send |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
207 function set_buffer(pb) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
208 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
209 // set fields of gRPC payload |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
210 var payload = { name: "TestString" }; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
211 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
212 // create an object |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
213 var message = pb.helloworld.HelloRequest.create(payload); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
214 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
215 // serialize object to buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
216 var buffer = pb.helloworld.HelloRequest.encode(message).finish(); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
217 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
218 var n = buffer.length; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
219 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
220 var frame = new Uint8Array(5 + buffer.length); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
221 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
222 frame[0] = 0; // 'compressed' flag |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
223 frame[1] = (n & 0xFF000000) >>> 24; // length: uint32 in network order |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
224 frame[2] = (n & 0x00FF0000) >>> 16; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
225 frame[3] = (n & 0x0000FF00) >>> 8; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
226 frame[4] = (n & 0x000000FF) >>> 0; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
227 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
228 frame.set(buffer, 5); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
229 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
230 return frame; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
231 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
232 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
233 var frame = set_buffer(pb); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
234 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
235 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
236 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
237 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
238 To ensure it works, we execute the code using node: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
239 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
240 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
241 $ node ./code.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
242 Uint8Array [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
243 0, 0, 0, 0, 12, 10, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
244 10, 84, 101, 115, 116, 83, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
245 116, 114, 105, 110, 103 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
246 ] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
247 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
248 |
2481 | 249 You can see that this got us a properly encoded <literal>gRPC</literal> frame. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
250 Now let's run it with njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
251 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
252 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
253 $ njs ./code.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
254 Thrown: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
255 Error: Cannot find module "./static.js" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
256 at require (native) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
257 at main (native) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
258 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
259 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
260 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
261 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
262 Modules are not supported, so we've received an exception. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
263 To overcome this issue, let's use <literal>browserify</literal> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
264 or other similar tool. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
265 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
266 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
267 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
268 An attempt to process our existing <literal>code.js</literal> file will result |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
269 in a bunch of JS code that is supposed to run in a browser, i.e. immediately |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
270 upon loading. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
271 This isn't something we actually want. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
272 Instead, we want to have an exported function that |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
273 can be referenced from the nginx configuration. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
274 This requires some wrapper code. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
275 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
276 <note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
277 In this guide, we use njs cli in all examples for the sake of simplicity. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
278 In real life, you will be using nginx njs module to run your code. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
279 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
280 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
281 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
282 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
283 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
284 The <literal>load.js</literal> file contains the library-loading code that |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
285 stores its handle in the global namespace: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
286 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
287 global.hello = require('./static.js'); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
288 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
289 This code will be replaced with merged content. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
290 Our code will be using the "<literal>global.hello</literal>" handle to access |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
291 the library. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
292 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
293 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
294 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
295 Next, we process it with browserify to get all dependencies into a single file: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
296 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
297 $ npx browserify load.js -o bundle.js -d |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
298 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
299 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
300 The result is huge file that contains all our dependencies: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
301 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
302 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
303 (function(){function...... |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
304 ... |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
305 ... |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
306 },{"protobufjs/minimal":9}]},{},[1]) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
307 //# sourceMappingURL.............. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
308 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
309 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
310 To get final "<literal>njs_bundle.js</literal>" file we concatenate |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
311 "<literal>bundle.js</literal>" and the following code: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
312 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
313 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
314 // Example usage of protobuf library: prepare a buffer to send |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
315 function set_buffer(pb) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
316 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
317 // set fields of gRPC payload |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
318 var payload = { name: "TestString" }; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
319 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
320 // create an object |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
321 var message = pb.helloworld.HelloRequest.create(payload); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
322 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
323 // serialize object to buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
324 var buffer = pb.helloworld.HelloRequest.encode(message).finish(); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
325 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
326 var n = buffer.length; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
327 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
328 var frame = new Uint8Array(5 + buffer.length); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
329 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
330 frame[0] = 0; // 'compressed' flag |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
331 frame[1] = (n & 0xFF000000) >>> 24; // length: uint32 in network order |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
332 frame[2] = (n & 0x00FF0000) >>> 16; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
333 frame[3] = (n & 0x0000FF00) >>> 8; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
334 frame[4] = (n & 0x000000FF) >>> 0; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
335 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
336 frame.set(buffer, 5); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
337 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
338 return frame; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
339 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
340 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
341 // functions to be called from outside |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
342 function setbuf() |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
343 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
344 return set_buffer(global.hello); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
345 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
346 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
347 // call the code |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
348 var frame = setbuf(); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
349 console.log(frame); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
350 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
351 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
352 Let's run the file using node to make sure things still work: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
353 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
354 $ node ./njs_bundle.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
355 Uint8Array [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
356 0, 0, 0, 0, 12, 10, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
357 10, 84, 101, 115, 116, 83, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
358 116, 114, 105, 110, 103 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
359 ] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
360 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
361 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
362 Now let's proceed further with njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
363 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
364 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
365 $ /njs ./njs_bundle.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
366 Uint8Array [0,0,0,0,12,10,10,84,101,115,116,83,116,114,105,110,103] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
367 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
368 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
369 The last thing will be to use njs-specific API to convert |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
370 array into byte string, so it could be usable by nginx module. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
371 We can add the following snippet before the last line: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
372 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
373 if (global.njs) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
374 return String.bytesFrom(frame) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
375 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
376 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
377 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
378 Finally, we got it working: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
379 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
380 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
381 $ njs ./njs_bundle.js |hexdump -C |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
382 00000000 00 00 00 00 0c 0a 0a 54 65 73 74 53 74 72 69 6e |.......TestStrin| |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
383 00000010 67 0a |g.| |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
384 00000012 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
385 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
386 This is the intended result. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
387 Response parsing can be implemented similarly: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
388 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
389 function parse_msg(pb, msg) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
390 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
391 // convert byte string into integer array |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
392 var bytes = msg.split('').map(v=>v.charCodeAt(0)); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
393 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
394 if (bytes.length < 5) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
395 throw 'message too short'; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
396 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
397 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
398 // first 5 bytes is gRPC frame (compression + length) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
399 var head = bytes.splice(0, 5); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
400 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
401 // ensure we have proper message length |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
402 var len = (head[1] << 24) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
403 + (head[2] << 16) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
404 + (head[3] << 8) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
405 + head[4]; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
406 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
407 if (len != bytes.length) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
408 throw 'header length mismatch'; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
409 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
410 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
411 // invoke protobufjs to decode message |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
412 var response = pb.helloworld.HelloReply.decode(bytes); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
413 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
414 console.log('Reply is:' + response.message); |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
415 } |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
416 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
417 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
418 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
419 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
420 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
421 <section id="dnspacket" name="DNS-packet"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
422 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
423 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
424 This example uses a library for generation and parsing of DNS packets. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
425 This a case worth considering because the library and its dependencies |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
426 use modern language constructions not yet supported by njs. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
427 In turn, this requires from us an extra step: transpiling the source code. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
428 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
429 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
430 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
431 Additional node packages are needed: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
432 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
433 $ npm install @babel/core @babel/cli @babel/preset-env babel-loader |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
434 $ npm install webpack webpack-cli |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
435 $ npm install buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
436 $ npm install dns-packet |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
437 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
438 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
439 The configuration file, webpack.config.js: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
440 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
441 const path = require('path'); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
442 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
443 module.exports = { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
444 entry: './load.js', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
445 mode: 'production', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
446 output: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
447 filename: 'wp_out.js', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
448 path: path.resolve(__dirname, 'dist'), |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
449 }, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
450 optimization: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
451 minimize: false |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
452 }, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
453 node: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
454 global: true, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
455 }, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
456 module : { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
457 rules: [{ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
458 test: /\.m?js$$/, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
459 exclude: /(bower_components)/, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
460 use: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
461 loader: 'babel-loader', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
462 options: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
463 presets: ['@babel/preset-env'] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
464 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
465 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
466 }] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
467 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
468 }; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
469 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
470 Note we are using "<literal>production</literal>" mode. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
471 In this mode webpack does not use "<literal>eval</literal>" construction |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
472 not supported by njs. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
473 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
474 The referenced <literal>load.js</literal> file is our entry point: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
475 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
476 global.dns = require('dns-packet') |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
477 global.Buffer = require('buffer/').Buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
478 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
479 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
480 We start the same way, by producing a single file for the libraries: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
481 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
482 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
483 $ npx browserify load.js -o bundle.js -d |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
484 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
485 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
486 Next, we process the file with webpack, which itself invokes babel: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
487 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
488 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
489 $ npx webpack --config webpack.config.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
490 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
491 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
492 This command produces the <literal>dist/wp_out.js</literal> file, which is a |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
493 transpiled version of <literal>bundle.js</literal>. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
494 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
495 We need to concatenate it with <literal>code.js</literal> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
496 that stores our code: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
497 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
498 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
499 function set_buffer(dnsPacket) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
500 { |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
501 // create DNS packet bytes |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
502 var buf = dnsPacket.encode({ |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
503 type: 'query', |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
504 id: 1, |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
505 flags: dnsPacket.RECURSION_DESIRED, |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
506 questions: [{ |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
507 type: 'A', |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
508 name: 'google.com' |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
509 }] |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
510 }) |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
511 |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
512 return buf; |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
513 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
514 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
515 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
516 Note that in this example generated code is not wrapped into function and we |
2481 | 517 do not need to call it explicitly. |
518 The result is in the "<literal>dist</literal>" directory: | |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
519 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
520 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
521 $ cat dist/wp_out.js code.js > njs_dns_bundle.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
522 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
523 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
524 Let's call our code at the end of a file: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
525 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
526 var b = setbuf(1); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
527 console.log(b); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
528 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
529 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
530 And execute it using node: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
531 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
532 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
533 $ node ./njs_dns_bundle_final.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
534 Buffer [Uint8Array] [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
535 0, 1, 1, 0, 0, 1, 0, 0, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
536 0, 0, 0, 0, 6, 103, 111, 111, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
537 103, 108, 101, 3, 99, 111, 109, 0, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
538 0, 1, 0, 1 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
539 ] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
540 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
541 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
542 Make sure this works as expected, and then run it with njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
543 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
544 $ njs ./njs_dns_bundle_final.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
545 Uint8Array [0,1,1,0,0,1,0,0,0,0,0,0,6,103,111,111,103,108,101,3,99,111,109,0,0,1,0,1] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
546 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
547 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
548 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
549 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
550 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
551 The response can be parsed as follows: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
552 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
553 function parse_response(buf) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
554 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
555 var bytes = buf.split('').map(v=>v.charCodeAt(0)); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
556 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
557 var b = global.Buffer.from(bytes); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
558 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
559 var packet = dnsPacket.decode(b); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
560 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
561 var resolved_name = packet.answers[0].name; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
562 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
563 // expected name is 'google.com', according to our request above |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
564 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
565 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
566 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
567 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
568 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
569 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
570 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
571 </article> |