Mercurial > hg > nginx-site
annotate xml/en/docs/njs/node_modules.xml @ 2998:050d2e099e16
Added example to r.subrequest in njs Reference.
author | Yaroslav Zhuravlev <yar@nginx.com> |
---|---|
date | Wed, 16 Aug 2023 12:12:22 +0100 |
parents | 5cd72684e5b8 |
children |
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" |
2629
5cd72684e5b8
Fixed typo and updated description in Using node modules.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2592
diff
changeset
|
12 rev="6"> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
13 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
14 <section id="intro"> |
2480
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> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
17 Often, a developer wants to use 3rd-party code, |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
18 usually available as a library of some kind. |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
19 In the JavaScript world, the concept of a module is relatively new, |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
20 so there was no standard until recently. |
2480
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. |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
23 This article describes ways to reuse |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
24 <link url="https://nodejs.org/">Node.js</link> code in njs. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
25 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
26 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
27 <note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
28 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
|
29 <link doc="index.xml">njs</link> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
30 <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
|
31 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
32 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
33 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
34 There is a number of issues |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
35 that may arise when 3rd-party code is added to njs: |
2480
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 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
39 <listitem> |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
40 Multiple files that reference each other and their dependencies |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
41 </listitem> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
42 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
43 <listitem> |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
44 Platform-specific APIs |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
45 </listitem> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
46 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
47 <listitem> |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
48 Modern standard language constructions |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
49 </listitem> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
50 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
51 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
52 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
53 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
54 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
55 The good news is that such problems are not something new or specific to njs. |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
56 JavaScript developers face them daily |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
57 when trying to support multiple disparate platforms |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
58 with very different properties. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
59 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
|
60 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
61 <list type="bullet"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
62 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
63 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
64 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
|
65 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
66 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
|
67 Tools like |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
68 <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
|
69 <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
|
70 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
|
71 your code and all the dependencies. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
72 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
73 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
74 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
75 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
76 Platform-specific APIs |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
77 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
78 You can use multiple libraries that implement such APIs |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
79 in a platform-agnostic manner (at the expense of performance, though). |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
80 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
|
81 <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
|
82 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
83 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
84 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
85 <listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
86 Modern standard language constructions |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
87 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
88 Such code can be transpiled: |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
89 this means performing a number of transformations |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
90 that rewrite newer language features in accordance with an older standard. |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
91 For example, <link url="https://babeljs.io/"> babel</link> project |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
92 can be used to this purpose. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
93 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
94 </listitem> |
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 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
97 </para> |
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> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
105 <link url="https://www.npmjs.com/package/protobufjs">protobufjs</link>— |
2480
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 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
107 <link url="https://grpc.io/">gRPC</link> protocol |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
108 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
109 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
110 <listitem> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
111 <link url="https://www.npmjs.com/package/dns-packet">dns-packet</link>— |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
112 a library for processing DNS protocol packets |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
113 </listitem> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
114 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
115 </list> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
116 </para> |
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 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
119 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
120 |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
121 <section id="environment" name="Environment"> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
122 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
123 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
124 <note> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
125 This document mostly employs a generic approach |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
126 and avoids specific best practice advices concerning Node.js |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
127 and JavaScript. |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
128 Make sure to consult the corresponding package's manual |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
129 before following the steps suggested here. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
130 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
131 First (assuming Node.js is installed and operational), let's create an |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
132 empty project and install some dependencies; |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
133 the commands below assume we are in the working directory: |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
134 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
135 $ mkdir my_project && cd my_project |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
136 $ npx license choose_your_license_here > LICENSE |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
137 $ npx gitignore node |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
138 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
139 $ cat > package.json <<EOF |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
140 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
141 "name": "foobar", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
142 "version": "0.0.1", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
143 "description": "", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
144 "main": "index.js", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
145 "keywords": [], |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
146 "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
|
147 "license": "some_license_here", |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
148 "private": true, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
149 "scripts": { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
150 "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
|
151 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
152 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
153 EOF |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
154 $ npm init -y |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
155 $ npm install browserify |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
156 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
157 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
158 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
159 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
160 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
161 |
2481 | 162 <section id="protobuf" name="Protobufjs"> |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
163 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
164 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
165 The library provides a parser |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
166 for the <literal>.proto</literal> interface definitions |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
167 and a code generator for message parsing and generation. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
168 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
169 |
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 In this example, we will use the |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
172 <link url="https://github.com/grpc/grpc/blob/master/examples/protos/helloworld.proto">helloworld.proto</link> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
173 file |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
174 from the gRPC examples. |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
175 Our goal is to create two messages: |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
176 <literal>HelloRequest</literal> and |
2480
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 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
181 njs doesn't support adding new functions dynamically |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
182 due to security considerations. |
2480
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> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
186 Next, the library is installed and |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
187 the JavaScript code implementing message marshalling |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
188 is generated from the protocol definition: |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
189 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
190 $ npm install protobufjs |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
191 $ 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
|
192 </example> |
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> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
196 Thus, the <literal>static.js</literal> file becomes our new dependency, |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
197 storing all the code we need to implement message processing. |
2480
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 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
199 library to create a buffer with the serialized |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
200 <literal>HelloRequest</literal> message. |
2480
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 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
203 var pb = require('./static.js'); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
204 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
205 // 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
|
206 function set_buffer(pb) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
207 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
208 // set fields of gRPC payload |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
209 var payload = { name: "TestString" }; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
210 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
211 // create an object |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
212 var message = pb.helloworld.HelloRequest.create(payload); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
213 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
214 // serialize object to buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
215 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
|
216 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
217 var n = buffer.length; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
218 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
219 var frame = new Uint8Array(5 + buffer.length); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
220 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
221 frame[0] = 0; // 'compressed' flag |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
222 frame[1] = (n & 0xFF000000) >>> 24; // length: uint32 in network byte order |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
223 frame[2] = (n & 0x00FF0000) >>> 16; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
224 frame[3] = (n & 0x0000FF00) >>> 8; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
225 frame[4] = (n & 0x000000FF) >>> 0; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
226 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
227 frame.set(buffer, 5); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
228 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
229 return frame; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
230 } |
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 var frame = set_buffer(pb); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
233 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
234 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
235 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
236 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
237 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
|
238 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
239 $ node ./code.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
240 Uint8Array [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
241 0, 0, 0, 0, 12, 10, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
242 10, 84, 101, 115, 116, 83, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
243 116, 114, 105, 110, 103 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
244 ] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
245 </example> |
2481 | 246 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
|
247 Now let's run it with njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
248 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
249 $ njs ./code.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
250 Thrown: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
251 Error: Cannot find module "./static.js" |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
252 at require (native) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
253 at main (native) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
254 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
255 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
256 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
257 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
258 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
|
259 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
|
260 or other similar tool. |
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 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
263 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
264 An attempt to process our existing <literal>code.js</literal> file will result |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
265 in a bunch of JS code that is supposed to run in a browser, |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
266 i.e. immediately upon loading. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
267 This isn't something we actually want. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
268 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
|
269 can be referenced from the nginx configuration. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
270 This requires some wrapper code. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
271 <note> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
272 In this guide, we use |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
273 njs <link doc="cli.xml">cli</link> in all examples for the sake of simplicity. |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
274 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
|
275 </note> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
276 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
277 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
278 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
279 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
|
280 stores its handle in the global namespace: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
281 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
282 global.hello = require('./static.js'); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
283 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
284 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
|
285 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
|
286 the library. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
287 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
288 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
289 <para> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
290 Next, we process it with <literal>browserify</literal> |
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
291 to get all dependencies into a single file: |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
292 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
293 $ 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
|
294 </example> |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
295 The result is a huge file that contains all our dependencies: |
2480
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 (function(){function...... |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
298 ... |
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 },{"protobufjs/minimal":9}]},{},[1]) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
301 //# sourceMappingURL.............. |
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 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
|
304 "<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
|
305 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
306 // 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
|
307 function set_buffer(pb) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
308 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
309 // set fields of gRPC payload |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
310 var payload = { name: "TestString" }; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
311 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
312 // create an object |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
313 var message = pb.helloworld.HelloRequest.create(payload); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
314 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
315 // serialize object to buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
316 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
|
317 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
318 var n = buffer.length; |
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 var frame = new Uint8Array(5 + buffer.length); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
321 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
322 frame[0] = 0; // 'compressed' flag |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
323 frame[1] = (n & 0xFF000000) >>> 24; // length: uint32 in network byte order |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
324 frame[2] = (n & 0x00FF0000) >>> 16; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
325 frame[3] = (n & 0x0000FF00) >>> 8; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
326 frame[4] = (n & 0x000000FF) >>> 0; |
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 frame.set(buffer, 5); |
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 return frame; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
331 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
332 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
333 // functions to be called from outside |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
334 function setbuf() |
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 return set_buffer(global.hello); |
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 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
339 // call the code |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
340 var frame = setbuf(); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
341 console.log(frame); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
342 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
343 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
|
344 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
345 $ node ./njs_bundle.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
346 Uint8Array [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
347 0, 0, 0, 0, 12, 10, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
348 10, 84, 101, 115, 116, 83, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
349 116, 114, 105, 110, 103 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
350 ] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
351 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
352 Now let's proceed further with njs: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
353 <example> |
2629
5cd72684e5b8
Fixed typo and updated description in Using node modules.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2592
diff
changeset
|
354 $ njs ./njs_bundle.js |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
355 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
|
356 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
357 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
|
358 array into byte string, so it could be usable by nginx module. |
2629
5cd72684e5b8
Fixed typo and updated description in Using node modules.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2592
diff
changeset
|
359 We can add the following snippet before the line |
5cd72684e5b8
Fixed typo and updated description in Using node modules.
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2592
diff
changeset
|
360 <literal>return frame; }</literal>: |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
361 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
362 if (global.njs) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
363 return String.bytesFrom(frame) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
364 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
365 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
366 Finally, we got it working: |
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 $ njs ./njs_bundle.js |hexdump -C |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
369 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
|
370 00000010 67 0a |g.| |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
371 00000012 |
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 This is the intended result. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
374 Response parsing can be implemented similarly: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
375 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
376 function parse_msg(pb, msg) |
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 // convert byte string into integer array |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
379 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
|
380 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
381 if (bytes.length < 5) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
382 throw 'message too short'; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
383 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
384 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
385 // 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
|
386 var head = bytes.splice(0, 5); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
387 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
388 // ensure we have proper message length |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
389 var len = (head[1] << 24) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
390 + (head[2] << 16) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
391 + (head[3] << 8) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
392 + head[4]; |
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 (len != bytes.length) { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
395 throw 'header length mismatch'; |
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 // invoke protobufjs to decode message |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
399 var response = pb.helloworld.HelloReply.decode(bytes); |
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 console.log('Reply is:' + response.message); |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
402 } |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
403 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
404 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
405 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
406 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
407 |
2576
4c8d0b37932d
Corrected syntax and style of "Using node modules with njs".
Yaroslav Zhuravlev <yar@nginx.com>
parents:
2489
diff
changeset
|
408 |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
409 <section id="dnspacket" name="DNS-packet"> |
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 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
412 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
|
413 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
|
414 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
|
415 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
|
416 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
417 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
418 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
419 Additional node packages are needed: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
420 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
421 $ 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
|
422 $ npm install webpack webpack-cli |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
423 $ npm install buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
424 $ npm install dns-packet |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
425 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
426 The configuration file, webpack.config.js: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
427 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
428 const path = require('path'); |
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 module.exports = { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
431 entry: './load.js', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
432 mode: 'production', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
433 output: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
434 filename: 'wp_out.js', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
435 path: path.resolve(__dirname, 'dist'), |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
436 }, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
437 optimization: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
438 minimize: false |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
439 }, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
440 node: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
441 global: true, |
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 : { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
444 rules: [{ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
445 test: /\.m?js$$/, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
446 exclude: /(bower_components)/, |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
447 use: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
448 loader: 'babel-loader', |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
449 options: { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
450 presets: ['@babel/preset-env'] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
451 } |
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 }] |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
454 } |
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 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
457 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
|
458 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
|
459 not supported by njs. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
460 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
|
461 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
462 global.dns = require('dns-packet') |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
463 global.Buffer = require('buffer/').Buffer |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
464 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
465 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
|
466 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
467 $ 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
|
468 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
469 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
|
470 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
471 $ npx webpack --config webpack.config.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
472 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
473 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
|
474 transpiled version of <literal>bundle.js</literal>. |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
475 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
|
476 that stores our code: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
477 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
478 function set_buffer(dnsPacket) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
479 { |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
480 // create DNS packet bytes |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
481 var buf = dnsPacket.encode({ |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
482 type: 'query', |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
483 id: 1, |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
484 flags: dnsPacket.RECURSION_DESIRED, |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
485 questions: [{ |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
486 type: 'A', |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
487 name: 'google.com' |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
488 }] |
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
489 }) |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
490 |
2489
bda080989b6c
Fixed indentation and braces in njs example.
Vladimir Homutov <vl@nginx.com>
parents:
2481
diff
changeset
|
491 return buf; |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
492 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
493 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
494 Note that in this example generated code is not wrapped into function and we |
2481 | 495 do not need to call it explicitly. |
496 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
|
497 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
498 $ 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
|
499 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
500 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
|
501 <example> |
2592
fca42223b9fc
Fixed example in the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
2576
diff
changeset
|
502 var b = set_buffer(global.dns); |
2480
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
503 console.log(b); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
504 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
505 And execute it using node: |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
506 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
507 $ node ./njs_dns_bundle_final.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
508 Buffer [Uint8Array] [ |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
509 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
|
510 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
|
511 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
|
512 0, 1, 0, 1 |
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 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
|
516 <example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
517 $ njs ./njs_dns_bundle_final.js |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
518 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
|
519 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
520 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
521 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
522 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
523 <para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
524 The response can be parsed as follows: |
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 function parse_response(buf) |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
527 { |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
528 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
|
529 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
530 var b = global.Buffer.from(bytes); |
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 var packet = dnsPacket.decode(b); |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
533 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
534 var resolved_name = packet.answers[0].name; |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
535 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
536 // 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
|
537 } |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
538 </example> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
539 </para> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
540 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
541 </section> |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
542 |
73d254c3376d
Added the "Using node modules with njs" article.
Vladimir Homutov <vl@nginx.com>
parents:
diff
changeset
|
543 </article> |