typespeed 2.0.21 → 2.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/dist/core.decorator.js +1 -30
  2. package/dist/default/default-authentication.class.js +25 -0
  3. package/dist/default/express-server.class.js +11 -4
  4. package/dist/default/log-default.class.js +0 -6
  5. package/dist/factory/authentication-factory.class.js +11 -0
  6. package/dist/factory/log-factory.class.js +9 -0
  7. package/dist/route.decorator.js +92 -2
  8. package/dist/typespeed.d.ts +40 -3
  9. package/dist/typespeed.js +3 -1
  10. package/package.json +1 -1
  11. package/src/core.decorator.ts +1 -29
  12. package/src/default/default-authentication.class.ts +10 -0
  13. package/src/default/express-server.class.ts +12 -5
  14. package/src/default/log-default.class.ts +0 -10
  15. package/src/factory/authentication-factory.class.ts +9 -0
  16. package/src/factory/log-factory.class.ts +9 -2
  17. package/src/route.decorator.ts +91 -3
  18. package/src/typespeed.d.ts +40 -3
  19. package/src/typespeed.ts +1 -0
  20. package/test/node_modules/.package-lock.json +65 -101
  21. package/test/node_modules/@types/body-parser/LICENSE +21 -0
  22. package/test/node_modules/@types/body-parser/README.md +16 -0
  23. package/test/node_modules/@types/body-parser/index.d.ts +107 -0
  24. package/test/node_modules/@types/body-parser/package.json +58 -0
  25. package/test/node_modules/@types/connect/LICENSE +21 -0
  26. package/test/node_modules/@types/connect/README.md +16 -0
  27. package/test/node_modules/@types/connect/index.d.ts +93 -0
  28. package/test/node_modules/@types/connect/package.json +32 -0
  29. package/test/node_modules/@types/express/LICENSE +21 -0
  30. package/test/node_modules/@types/express/README.md +16 -0
  31. package/test/node_modules/@types/express/index.d.ts +136 -0
  32. package/test/node_modules/@types/express/package.json +45 -0
  33. package/test/node_modules/@types/express-serve-static-core/LICENSE +21 -0
  34. package/test/node_modules/@types/express-serve-static-core/README.md +16 -0
  35. package/test/node_modules/@types/express-serve-static-core/index.d.ts +1281 -0
  36. package/test/node_modules/@types/express-serve-static-core/package.json +55 -0
  37. package/test/node_modules/@types/mime/LICENSE +21 -0
  38. package/test/node_modules/@types/mime/Mime.d.ts +10 -0
  39. package/test/node_modules/@types/mime/README.md +16 -0
  40. package/test/node_modules/@types/mime/index.d.ts +35 -0
  41. package/test/node_modules/@types/mime/lite.d.ts +7 -0
  42. package/test/node_modules/@types/mime/package.json +29 -0
  43. package/test/node_modules/@types/qs/LICENSE +21 -0
  44. package/test/node_modules/@types/qs/README.md +16 -0
  45. package/test/node_modules/@types/qs/index.d.ts +62 -0
  46. package/test/node_modules/@types/qs/package.json +65 -0
  47. package/test/node_modules/@types/range-parser/LICENSE +21 -0
  48. package/test/node_modules/@types/range-parser/README.md +55 -0
  49. package/test/node_modules/@types/range-parser/index.d.ts +35 -0
  50. package/test/node_modules/@types/range-parser/package.json +25 -0
  51. package/test/node_modules/@types/send/LICENSE +21 -0
  52. package/test/node_modules/@types/send/README.md +16 -0
  53. package/test/node_modules/@types/send/index.d.ts +231 -0
  54. package/test/node_modules/@types/send/package.json +33 -0
  55. package/test/node_modules/@types/serve-static/LICENSE +21 -0
  56. package/test/node_modules/@types/serve-static/README.md +16 -0
  57. package/test/node_modules/@types/serve-static/index.d.ts +113 -0
  58. package/test/node_modules/@types/serve-static/package.json +38 -0
  59. package/test/src/config.json +5 -0
  60. package/test/src/custom-log.class.ts +6 -0
  61. package/test/src/jwt-authentication.class.ts +29 -0
  62. package/test/src/test-request.class.ts +36 -0
  63. package/test/node_modules/@acuminous/bitsyntax/.github/workflows/node-js-ci.yml +0 -17
  64. package/test/node_modules/@acuminous/bitsyntax/.github/workflows/node-js-publish.yml +0 -30
  65. package/test/node_modules/@acuminous/bitsyntax/LICENSE +0 -6
  66. package/test/node_modules/@acuminous/bitsyntax/LICENSE-MIT +0 -21
  67. package/test/node_modules/@acuminous/bitsyntax/Makefile +0 -15
  68. package/test/node_modules/@acuminous/bitsyntax/README.md +0 -308
  69. package/test/node_modules/@acuminous/bitsyntax/index.js +0 -10
  70. package/test/node_modules/@acuminous/bitsyntax/lib/compile.js +0 -303
  71. package/test/node_modules/@acuminous/bitsyntax/lib/constructor.js +0 -142
  72. package/test/node_modules/@acuminous/bitsyntax/lib/grammar.pegjs +0 -67
  73. package/test/node_modules/@acuminous/bitsyntax/lib/interp.js +0 -232
  74. package/test/node_modules/@acuminous/bitsyntax/lib/parse.js +0 -32
  75. package/test/node_modules/@acuminous/bitsyntax/lib/parser.js +0 -1173
  76. package/test/node_modules/@acuminous/bitsyntax/lib/pattern.js +0 -119
  77. package/test/node_modules/@acuminous/bitsyntax/package.json +0 -36
  78. package/test/node_modules/@acuminous/bitsyntax/test/consing.test.js +0 -50
  79. package/test/node_modules/@acuminous/bitsyntax/test/matching.test.js +0 -193
  80. package/test/node_modules/amqplib/.github/ISSUE_TEMPLATE/bug_report.md +0 -38
  81. package/test/node_modules/amqplib/.github/workflows/test.yml +0 -38
  82. package/test/node_modules/amqplib/CHANGELOG.md +0 -430
  83. package/test/node_modules/amqplib/LICENSE +0 -6
  84. package/test/node_modules/amqplib/LICENSE-MIT +0 -21
  85. package/test/node_modules/amqplib/Makefile +0 -47
  86. package/test/node_modules/amqplib/README.md +0 -157
  87. package/test/node_modules/amqplib/bin/generate-defs.js +0 -722
  88. package/test/node_modules/amqplib/callback_api.js +0 -22
  89. package/test/node_modules/amqplib/channel_api.js +0 -16
  90. package/test/node_modules/amqplib/examples/headers.js +0 -42
  91. package/test/node_modules/amqplib/examples/receive_generator.js +0 -39
  92. package/test/node_modules/amqplib/examples/send_generators.js +0 -42
  93. package/test/node_modules/amqplib/examples/ssl.js +0 -64
  94. package/test/node_modules/amqplib/examples/tutorials/README.md +0 -93
  95. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log.js +0 -28
  96. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log_direct.js +0 -30
  97. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log_topic.js +0 -27
  98. package/test/node_modules/amqplib/examples/tutorials/callback_api/new_task.js +0 -27
  99. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive.js +0 -30
  100. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs.js +0 -36
  101. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs_direct.js +0 -56
  102. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs_topic.js +0 -55
  103. package/test/node_modules/amqplib/examples/tutorials/callback_api/rpc_client.js +0 -49
  104. package/test/node_modules/amqplib/examples/tutorials/callback_api/rpc_server.js +0 -45
  105. package/test/node_modules/amqplib/examples/tutorials/callback_api/send.js +0 -29
  106. package/test/node_modules/amqplib/examples/tutorials/callback_api/worker.js +0 -35
  107. package/test/node_modules/amqplib/examples/tutorials/emit_log.js +0 -19
  108. package/test/node_modules/amqplib/examples/tutorials/emit_log_direct.js +0 -20
  109. package/test/node_modules/amqplib/examples/tutorials/emit_log_topic.js +0 -19
  110. package/test/node_modules/amqplib/examples/tutorials/new_task.js +0 -18
  111. package/test/node_modules/amqplib/examples/tutorials/package.json +0 -16
  112. package/test/node_modules/amqplib/examples/tutorials/receive.js +0 -21
  113. package/test/node_modules/amqplib/examples/tutorials/receive_logs.js +0 -28
  114. package/test/node_modules/amqplib/examples/tutorials/receive_logs_direct.js +0 -44
  115. package/test/node_modules/amqplib/examples/tutorials/receive_logs_topic.js +0 -43
  116. package/test/node_modules/amqplib/examples/tutorials/rpc_client.js +0 -52
  117. package/test/node_modules/amqplib/examples/tutorials/rpc_server.js +0 -39
  118. package/test/node_modules/amqplib/examples/tutorials/send.js +0 -23
  119. package/test/node_modules/amqplib/examples/tutorials/worker.js +0 -28
  120. package/test/node_modules/amqplib/examples/waitForConfirms.js +0 -22
  121. package/test/node_modules/amqplib/lib/api_args.js +0 -314
  122. package/test/node_modules/amqplib/lib/bitset.js +0 -130
  123. package/test/node_modules/amqplib/lib/callback_model.js +0 -327
  124. package/test/node_modules/amqplib/lib/channel.js +0 -499
  125. package/test/node_modules/amqplib/lib/channel_model.js +0 -302
  126. package/test/node_modules/amqplib/lib/codec.js +0 -321
  127. package/test/node_modules/amqplib/lib/connect.js +0 -189
  128. package/test/node_modules/amqplib/lib/connection.js +0 -663
  129. package/test/node_modules/amqplib/lib/credentials.js +0 -42
  130. package/test/node_modules/amqplib/lib/defs.js +0 -4966
  131. package/test/node_modules/amqplib/lib/error.js +0 -24
  132. package/test/node_modules/amqplib/lib/format.js +0 -40
  133. package/test/node_modules/amqplib/lib/frame.js +0 -113
  134. package/test/node_modules/amqplib/lib/heartbeat.js +0 -89
  135. package/test/node_modules/amqplib/lib/mux.js +0 -129
  136. package/test/node_modules/amqplib/package.json +0 -37
  137. package/test/node_modules/amqplib/test/bitset.js +0 -69
  138. package/test/node_modules/amqplib/test/callback_api.js +0 -312
  139. package/test/node_modules/amqplib/test/channel.js +0 -621
  140. package/test/node_modules/amqplib/test/channel_api.js +0 -596
  141. package/test/node_modules/amqplib/test/codec.js +0 -237
  142. package/test/node_modules/amqplib/test/connect.js +0 -197
  143. package/test/node_modules/amqplib/test/connection.js +0 -390
  144. package/test/node_modules/amqplib/test/data.js +0 -254
  145. package/test/node_modules/amqplib/test/frame.js +0 -198
  146. package/test/node_modules/amqplib/test/mux.js +0 -204
  147. package/test/node_modules/amqplib/test/util.js +0 -219
  148. package/test/node_modules/buffer-more-ints/.travis.yml +0 -6
  149. package/test/node_modules/buffer-more-ints/LICENSE +0 -19
  150. package/test/node_modules/buffer-more-ints/README.md +0 -69
  151. package/test/node_modules/buffer-more-ints/buffer-more-ints-tests.js +0 -183
  152. package/test/node_modules/buffer-more-ints/buffer-more-ints.js +0 -432
  153. package/test/node_modules/buffer-more-ints/package.json +0 -19
  154. package/test/node_modules/buffer-more-ints/polyfill.js +0 -71
  155. package/test/node_modules/core-util-is/LICENSE +0 -19
  156. package/test/node_modules/core-util-is/README.md +0 -3
  157. package/test/node_modules/core-util-is/lib/util.js +0 -107
  158. package/test/node_modules/core-util-is/package.json +0 -38
  159. package/test/node_modules/debug/LICENSE +0 -20
  160. package/test/node_modules/debug/README.md +0 -481
  161. package/test/node_modules/debug/package.json +0 -59
  162. package/test/node_modules/debug/src/browser.js +0 -269
  163. package/test/node_modules/debug/src/common.js +0 -274
  164. package/test/node_modules/debug/src/index.js +0 -10
  165. package/test/node_modules/debug/src/node.js +0 -263
  166. package/test/node_modules/inherits/LICENSE +0 -16
  167. package/test/node_modules/inherits/README.md +0 -42
  168. package/test/node_modules/inherits/inherits.js +0 -9
  169. package/test/node_modules/inherits/inherits_browser.js +0 -27
  170. package/test/node_modules/inherits/package.json +0 -29
  171. package/test/node_modules/isarray/README.md +0 -54
  172. package/test/node_modules/isarray/build/build.js +0 -209
  173. package/test/node_modules/isarray/component.json +0 -19
  174. package/test/node_modules/isarray/index.js +0 -3
  175. package/test/node_modules/isarray/package.json +0 -25
  176. package/test/node_modules/ms/index.js +0 -162
  177. package/test/node_modules/ms/license.md +0 -21
  178. package/test/node_modules/ms/package.json +0 -37
  179. package/test/node_modules/ms/readme.md +0 -60
  180. package/test/node_modules/querystringify/LICENSE +0 -22
  181. package/test/node_modules/querystringify/README.md +0 -61
  182. package/test/node_modules/querystringify/index.js +0 -118
  183. package/test/node_modules/querystringify/package.json +0 -38
  184. package/test/node_modules/readable-stream/LICENSE +0 -18
  185. package/test/node_modules/readable-stream/README.md +0 -15
  186. package/test/node_modules/readable-stream/duplex.js +0 -1
  187. package/test/node_modules/readable-stream/float.patch +0 -923
  188. package/test/node_modules/readable-stream/lib/_stream_duplex.js +0 -89
  189. package/test/node_modules/readable-stream/lib/_stream_passthrough.js +0 -46
  190. package/test/node_modules/readable-stream/lib/_stream_readable.js +0 -951
  191. package/test/node_modules/readable-stream/lib/_stream_transform.js +0 -209
  192. package/test/node_modules/readable-stream/lib/_stream_writable.js +0 -477
  193. package/test/node_modules/readable-stream/package.json +0 -32
  194. package/test/node_modules/readable-stream/passthrough.js +0 -1
  195. package/test/node_modules/readable-stream/readable.js +0 -10
  196. package/test/node_modules/readable-stream/transform.js +0 -1
  197. package/test/node_modules/readable-stream/writable.js +0 -1
  198. package/test/node_modules/requires-port/.travis.yml +0 -19
  199. package/test/node_modules/requires-port/LICENSE +0 -22
  200. package/test/node_modules/requires-port/README.md +0 -47
  201. package/test/node_modules/requires-port/index.js +0 -38
  202. package/test/node_modules/requires-port/package.json +0 -47
  203. package/test/node_modules/requires-port/test.js +0 -98
  204. package/test/node_modules/safe-buffer/LICENSE +0 -21
  205. package/test/node_modules/safe-buffer/README.md +0 -584
  206. package/test/node_modules/safe-buffer/index.d.ts +0 -187
  207. package/test/node_modules/safe-buffer/index.js +0 -62
  208. package/test/node_modules/safe-buffer/package.json +0 -37
  209. package/test/node_modules/string_decoder/LICENSE +0 -20
  210. package/test/node_modules/string_decoder/README.md +0 -7
  211. package/test/node_modules/string_decoder/index.js +0 -221
  212. package/test/node_modules/string_decoder/package.json +0 -25
  213. package/test/node_modules/url-parse/LICENSE +0 -22
  214. package/test/node_modules/url-parse/README.md +0 -153
  215. package/test/node_modules/url-parse/dist/url-parse.js +0 -755
  216. package/test/node_modules/url-parse/dist/url-parse.min.js +0 -1
  217. package/test/node_modules/url-parse/dist/url-parse.min.js.map +0 -1
  218. package/test/node_modules/url-parse/index.js +0 -589
  219. package/test/node_modules/url-parse/package.json +0 -49
@@ -1,308 +0,0 @@
1
- # Byte-wise matching for Node.JS
2
-
3
- [![Node.js CI](https://github.com/acuminous/bitsyntax-js/workflows/Node.js%20CI/badge.svg)](https://github.com/acuminous/bitsyntax-js/actions?query=workflow%3A%22Node.js+CI%22)
4
- [![NPM version](https://img.shields.io/npm/v/bitsyntax.svg?style=flat-square)](https://www.npmjs.com/package/@acuminous/bitsyntax)
5
- [![NPM downloads](https://img.shields.io/npm/dm/bitsyntax.svg?style=flat-square)](https://www.npmjs.com/package/@acuminous/bitsyntax)
6
-
7
- Gives a compact syntax for parsing and constructing byte buffers,
8
- derived from [Erlang's bit
9
- syntax](http://www.erlang.org/doc/programming_examples/bit_syntax.html#id64858).
10
-
11
- ```js
12
- var bitsyntax = require('bitsyntax');
13
- var pattern = bitsyntax.matcher('len:8/integer, str:len/binary');
14
- var bound = pattern(new Buffer([4, 0x41, 0x42, 0x43, 0x44]));
15
- bound.str
16
- // => <Buffer 41 42 43 44>
17
- ```
18
-
19
- A typical use of this is parsing byte streams from sockets. For
20
- example, size-prefixed frames:
21
-
22
- ```js
23
- var framePattern = bitsyntax.matcher('len:32/integer, frame:len/binary, rest/binary');
24
- socket.on('data', function process(data) {
25
- var m;
26
- if (m = framePattern(data)) {
27
- emit('frame', m.frame);
28
- process(m.rest);
29
- }
30
- else {
31
- stashForNextData(data);
32
- }
33
- });
34
- ```
35
-
36
- Patterns can also be used to construct byte buffers from supplied
37
- values:
38
-
39
- ```js
40
- var spdyDataFrame = require('bitsyntax')
41
- .builder('streamId:32, flags:8, length:24, data/binary');
42
-
43
- spdyDataFrame({streamId:5, flags:0, length:bin.length, data:bin});
44
- ```
45
-
46
- One or more segments of a pattern may also be supplied in multiple
47
- arguments, if that is more convenient; this makes it easier to split a
48
- long pattern over lines:
49
-
50
- ```js
51
- var p = bitsyntax.matcher('size:8, payload:size/binary',
52
- 'rest/binary');
53
- ```
54
-
55
- ## API
56
-
57
- ### `matcher`
58
-
59
- Compiles a pattern as a string (or strings), to a function that will
60
- return either a map of bindings, or `false`, given a buffer and
61
- optionally an environment. The environment contains values for bound
62
- variables in the pattern (if there are any).
63
-
64
- ```js
65
- var p = bitsyntax.matcher('header:headerSize/binary, rest/binary');
66
- var b = p(new Buffer([1, 2, 3, 4, 5]), {headerSize: 3});
67
- b.header
68
- // => <Buffer 01 02 03>
69
- ```
70
-
71
- A matcher will return `false` if the supplied buffer does not match
72
- the pattern; for example, if it has too few bytes, or a literal is not
73
- present.
74
-
75
- ```js
76
- var p = bitsyntax.matcher('"foo=", str/binary');
77
- p(new Buffer("bar=humbug"));
78
- // => false
79
- ```
80
-
81
- ### `parse` and `match`
82
-
83
- When composed, equivalent to `matcher`; may be useful if you want to
84
- examine the internal structure of patterns.
85
-
86
- `parse` takes strings as for `matcher`, and returns the internal
87
- representation of the pattern. `match` takes this representation, a
88
- buffer, and optionally an environment, and returns the bindings or
89
- `false` (as with `matcher`).
90
-
91
- ```js
92
- var p = bitsyntax.parse('header:headerSize/binary',
93
- 'rest/binary');
94
- var b = bitsyntax.match(p, new Buffer([1, 2, 3, 4, 5]),
95
- {headerSize: 3});
96
- b.header
97
- // => <Buffer 01 02 03>
98
- ```
99
-
100
- ### `builder`
101
-
102
- Takes a pattern and returns a function that will construct a byte
103
- buffer, given values for the variables mentioned in the pattern.
104
-
105
- ```js
106
- var cons = bitsyntax.builder('size:8, bin/binary');
107
- cons({size:6, bin:new Buffer('foobar')});
108
- // => <Buffer 06 66 6f 6f 62 61 72>
109
- ```
110
-
111
- Patterns supplied to builders are slightly different to patterns
112
- supplied for matching, as noted below.
113
-
114
- ### `build`
115
-
116
- Takes a parsed pattern and a map of variable values, and returns a
117
- buffer. As with `match`, may be useful to debug patterns.
118
-
119
- ```js
120
- var pattern = bitsyntax.parse('size:8, bin:size/binary');
121
- bitsyntax.build(pattern, {size:6, bin: new Buffer('foobar')});
122
- // => <Buffer 06 66 6f 6f 62 61 72>
123
- ```
124
-
125
- ### `write`
126
-
127
- Writes variable values into a buffer, at an offset, according to the
128
- parsed pattern given. Returns the finishing offset, i.e., the supplied
129
- offset plus the number of bytes written.
130
-
131
- ```js
132
- var pattern = bitsyntax.parse('size:8, bin/binary');
133
- var buf = new Buffer(7);
134
- bitsyntax.write(buf, 0, pattern,
135
- {size:6, bin: new Buffer('foobar')});
136
- // => 7
137
- buf
138
- // => <Buffer 06 66 6f 6f 62 61 72>
139
- ```
140
-
141
- ## Patterns
142
-
143
- Patterns are sequences of segments, each matching a value. Segments
144
- have the general form
145
-
146
- value:size/type_specifier_list
147
-
148
- The size and type specifier list may be omitted, giving three extra
149
- variations:
150
-
151
- value
152
- value:size
153
- value/type_specifier_list
154
-
155
- The type specifier list is a list of keywords separated by
156
- hyphens. Type specifiers are described below.
157
-
158
- Patterns are generally supplied as strings, with a comma-separated
159
- series of segments.
160
-
161
- ### Variable or value
162
-
163
- The first part of a segment gives a variable name or a literal
164
- value. If a variable name is given, the value matched by the segment
165
- will be bound to that variable name for the rest of the pattern. If a
166
- literal value is given, the matched value must equal that value. If a
167
- variable's value is given in the environment, the matched value must
168
- equal the provided value.
169
-
170
- When used in a builder, the literal value will be copied into the
171
- buffer according to the type it is given. A variable name indicates a
172
- slot into which a value supplied to the builder will be copied.
173
-
174
- The special variable name `_` discards the value matched; i.e., it
175
- simply skips over the appropriate number of bits in the input. '_' is
176
- not allowed in builder patterns.
177
-
178
- ### Size and unit
179
-
180
- The size of a segment is given following the value or variable,
181
- separated with a colon:
182
-
183
- foo:32
184
-
185
- The unit is given in the list of specifiers as `'unit' and
186
- an integer from 0..256, separated by a colon:
187
-
188
- foo:4/integer-unit:8
189
-
190
- The size is the number of units in the value; the unit is given as a
191
- number of bits. Unit can be of use, for example, when you want to
192
- match integers of a number of bytes rather than a number of bits.
193
-
194
- For integers and floats, the default unit is 1 bit; to keep things
195
- aligned on byte boundaries, `unit * size` must currently be a multiple
196
- of 8. For binaries the default unit is 8, and the unit must be a
197
- multiple of 8.
198
-
199
- If the size is omitted and the type is integer, the size defaults to
200
- 8. If the size is omitted and the type is binary, the segment will
201
- match all remaining bytes in the input; such a segment may only be
202
- used at the end of a pattern, when matching.
203
-
204
- The size may also be given as an integer variable matched earlier in
205
- the pattern, as in the example given at the top. When constructing, a
206
- size may be a variable referring to the supplied environment.
207
-
208
- In builders, numbers will be rounded, masked or padded to fit the size
209
- and units given; for example, `'256:8'` gives the binary `Buffer<00>`
210
- because the lowest eight bits are 0; `'255:16` gives the binary
211
- `Buffer<00 ff>`.
212
-
213
- ### Type name specifier
214
-
215
- One of `integer`, `binary`, `string`, `float`. If not given, the
216
- default is `integer`.
217
-
218
- An integer is a big- or little-endian, signed or unsigned
219
- integer. Integers up to 32 bits are supported. Signed integers are
220
- two's complement format. In JavaScript, only integers between -(2^53)
221
- and 2^53 can be represented, and bitwise operators are only defined on
222
- 32-bit signed integers.
223
-
224
- A binary is simply a byte buffer; usually this will result in a slice
225
- of the input buffer being returned, so beware mutation.
226
-
227
- A string is a UTF8 string consisting of the given number of bytes.
228
-
229
- A float is a 32- or 64-bit IEEE754 floating-point value (this is the
230
- standard JavaScript uses, as do Java and Erlang).
231
-
232
- ### Endianness specifier
233
-
234
- Integers may be big- or little-endian; this refers to which 'end' of
235
- the bytes making up the integer are most significant. In network
236
- protocols integers are usually big-endian, meaning the first
237
- (left-most) byte is the most significant, but this is not always the
238
- case.
239
-
240
- A specifier of `big` means the integer will be parsed (or written into
241
- the result) as big-endian, and `little` means the integer will be
242
- parsed or written as little-endian. The default is big-endian.
243
-
244
- ### Signedness specifier
245
-
246
- Integer segments may include a specifier of `signed` or `unsigned`. A
247
- signed integer is parsed as two's complement format. The default is
248
- unsigned.
249
-
250
- Signedness is ignored in builders.
251
-
252
- ### Literal strings
253
-
254
- A quoted string appearing in a pattern is a shorthand for the bytes in
255
- its UTF8 encoding. For example,
256
-
257
- "foobar", _/binary
258
-
259
- matches any buffer that starts with the bytes `0x66, 0x6f, 0x6f, 0x62,
260
- 0x61, 0x72`.
261
-
262
- When used in a builder, a quoted string is copied into the result as
263
- the bytes of its UTF8 encoding.
264
-
265
- ## Examples
266
-
267
- In the following the matched bytes are given in array notation for
268
- convenience. Bear in mind that `match()` actually takes a buffer for
269
- the bytes to match against. The phrase "returns X as Y" or "binds X as
270
- Y" means the return value is an object with value X mapped to the key
271
- Y.
272
-
273
- 54
274
-
275
- Matches the single byte `54`.
276
-
277
- 54:32
278
-
279
- Matches the bytes [0,0,0,54].
280
-
281
- 54:32/little
282
-
283
- Matches the bytes [54,0,0,0].
284
-
285
- 54:4/unit:8
286
-
287
- Matches the bytes [0,0,0,54].
288
-
289
- int:32/signed
290
-
291
- Matches a binary of four bytes, and returns a signed 32-bit integer as
292
- `int`.
293
-
294
- len:16, str:len/binary
295
-
296
- Matches a binary of `2 + len` bytes, and returns an unsigned 16-bit
297
- integer as `len` and a buffer of length `len` as `str`.
298
-
299
- len:16, _:len/binary, rest/binary
300
-
301
- Matches a binary of at least `2 + len` bytes, binds an unsigned 16-bit
302
- integer as `len`, ignores the next `len` bytes, and binds the
303
- remaining (possibly zero-length) binary as `rest`.
304
-
305
- s:8, key:s/binary, value/binary
306
-
307
- When given the environment `{s:6, key: "foobar"}`, will match a binary
308
- starting with [6, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72, ...].
@@ -1,10 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports.parse = require('./lib/parse').parse;
4
- module.exports.match = require('./lib/interp').match;
5
- module.exports.build = require('./lib/constructor').build;
6
- module.exports.write = require('./lib/constructor').write;
7
-
8
- module.exports.matcher = module.exports.compile =
9
- require('./lib/compile').compile;
10
- module.exports.builder = require('./lib/compile').compile_builder;
@@ -1,303 +0,0 @@
1
- // Compile patterns to recognisers and constructors
2
-
3
- 'use strict';
4
-
5
- require('buffer-more-ints');
6
- var $ = require('util').format;
7
-
8
- var parse = require('./parse').parse;
9
- var interp = require('./interp'),
10
- parse_int = interp.parse_int,
11
- parse_float = interp.parse_float;
12
- var construct = require('./constructor'),
13
- write_int = construct.write_int,
14
- write_float = construct.write_float;
15
-
16
- var Buffer = require('safe-buffer').Buffer;
17
-
18
- var lines = [];
19
- function $start() {
20
- lines = [];
21
- }
22
- function $line(/* format , args */) {
23
- lines.push($.apply(null, arguments));
24
- }
25
- function $result() {
26
- return lines.join('\n');
27
- }
28
-
29
- function bits_expr(segment) {
30
- if (typeof segment.size === 'string') {
31
- return $('%s * %d', var_name(segment.size), segment.unit);
32
- }
33
- else {
34
- return (segment.size * segment.unit).toString();
35
- }
36
- }
37
-
38
- function get_number(segment) {
39
- $line('bits = %s;\n', bits_expr(segment));
40
- var parser = (segment.type === 'integer') ?
41
- 'parse_int' : 'parse_float';
42
- var be = segment.bigendian, sg = segment.signed;
43
- $line("byteoffset = offset / 8; offset += bits");
44
- $line("if (offset > binsize) { return false; }");
45
- $line("else { result = %s(bin, byteoffset, bits / 8, %s, %s); }",
46
- parser, be, sg);
47
- }
48
-
49
- function get_binary(segment) {
50
- $line("byteoffset = offset / 8;");
51
- if (segment.size === true) {
52
- $line("offset = binsize;");
53
- $line("result = bin.slice(byteoffset);");
54
- }
55
- else {
56
- $line("bits = %s;", bits_expr(segment));
57
- $line("offset += bits;");
58
- $line("if (offset > binsize) { return false; }");
59
- $line("else { result = bin.slice(byteoffset,",
60
- "byteoffset + bits / 8); }");
61
- }
62
- }
63
-
64
- function get_string(segment) {
65
- $line("byteoffset = offset / 8;");
66
- var strlen = segment.value.length;
67
- var strlenbits = strlen * 8;
68
- $line("offset += %d;", strlenbits);
69
- $line("if (offset > binsize) { return false; }");
70
- $line("else { result = bin.toString(byteoffset,",
71
- $("byteoffset + %d); }", strlen));
72
- }
73
-
74
- function skip_bits(segment) {
75
- if (typeof segment.size === 'string') {
76
- // Damn. Have to look up the size.
77
- $line("var skipbits = %s * %d;",
78
- var_name(segment.size), segment.unit);
79
- $line("if (offset + skipbits > binsize) { return false; }");
80
- $line("else { offset += skipbits; }");
81
- }
82
- else if (segment.size === true) {
83
- $line("if (offset % 8 === 0) { offset = binsize; }");
84
- $line("else { return false; }");
85
- }
86
- else {
87
- var bits = segment.unit * segment.size;
88
- $line("if (offset + %d > binsize) { return false; }", bits);
89
- $line("else { offset += %d; }", bits);
90
- }
91
- }
92
-
93
- function match_seg(segment) {
94
- if (segment.name === '_') {
95
- skip_bits(segment);
96
- }
97
- else {
98
- var assign_result;
99
- switch (segment.type) {
100
- case 'integer':
101
- case 'float':
102
- get_number(segment);
103
- break;
104
- case 'binary':
105
- get_binary(segment);
106
- break;
107
- case 'string':
108
- get_string(segment);
109
- break;
110
- }
111
- $line("if (result === false) return false;");
112
- if (segment.name) {
113
- // variable is given a value in the environment
114
- $line("else if (%s !== undefined) {", var_name(segment.name));
115
- // .. and it is not the same as that matched
116
- $line("if (%s != result) return false;",
117
- var_name(segment.name));
118
- $line("}");
119
- // variable is free
120
- $line('else %s = result;', var_name(segment.name));
121
- }
122
- else {
123
- var repr = JSON.stringify(segment.value);
124
- $line("else if (result != %s) return false;", repr);
125
- }
126
- }
127
- }
128
-
129
- function var_name(name) {
130
- return 'var_' + name;
131
- }
132
-
133
- function variables(segments) {
134
- var names = {};
135
- for (var i = 0; i < segments.length; i++) {
136
- var name = segments[i].name;
137
- if (name && name !== '_') {
138
- names[name] = true;
139
- }
140
- name = segments[i].size;
141
- if (typeof name === 'string') {
142
- names[name] = true;
143
- }
144
- }
145
- return Object.keys(names);
146
- }
147
-
148
- function compile_pattern(segments) {
149
- $start();
150
- $line("return function(binary, env) {");
151
- $line("'use strict';");
152
- $line("var bin = binary, env = env || {};");
153
- $line("var offset = 0, binsize = bin.length * 8;");
154
- $line("var bits, result, byteoffset;");
155
- var varnames = variables(segments);
156
- for (var v = 0; v < varnames.length; v++) {
157
- var name = varnames[v];
158
- $line("var %s = env['%s'];", var_name(name), name);
159
- }
160
-
161
- var len = segments.length;
162
- for (var i = 0; i < len; i++) {
163
- var segment = segments[i];
164
- $line("// " + JSON.stringify(segment));
165
- match_seg(segment);
166
- }
167
-
168
- $line("if (offset == binsize) {");
169
- $line("return {");
170
- for (var v = 0; v < varnames.length; v++) {
171
- var name = varnames[v];
172
- $line("%s: %s,", name, var_name(name));
173
- }
174
- $line('};');
175
- $line('}'); // if offset == binsize
176
- $line("else return false;");
177
- $line("}"); // end function
178
-
179
- var fn = new Function('parse_int', 'parse_float', $result());
180
- return fn(parse_int, parse_float);
181
- }
182
-
183
-
184
- function write_seg(segment) {
185
- switch (segment.type) {
186
- case 'string':
187
- $line("offset += buf.write(%s, offset, 'utf8');",
188
- JSON.stringify(segment.value));
189
- break;
190
- case 'binary':
191
- $line("val = bindings['%s'];", segment.name);
192
- if (segment.size === true) {
193
- $line('size = val.length;');
194
- }
195
- else if (typeof segment.size === 'string') {
196
- $line("size = (bindings['%s'] * %d) / 8;",
197
- segment.size, segment.unit);
198
- }
199
- else {
200
- $line("size = %d;", (segment.size * segment.unit) / 8);
201
- }
202
- $line('val.copy(buf, offset, 0, size);');
203
- $line('offset += size;');
204
- break;
205
- case 'integer':
206
- case 'float':
207
- write_number(segment);
208
- break;
209
- }
210
- }
211
-
212
- function write_number(segment) {
213
- if (segment.name) {
214
- $line("val = bindings['%s'];", segment.name);
215
- }
216
- else {
217
- $line("val = %d", segment.value);
218
- }
219
- var writer = (segment.type === 'integer') ?
220
- 'write_int' : 'write_float';
221
- if (typeof segment.size === 'string') {
222
- $line("size = (bindings['%s'] * %d) / 8;",
223
- segment.size, segment.unit);
224
- }
225
- else {
226
- $line('size = %d;', (segment.size * segment.unit) / 8);
227
- }
228
- $line('%s(buf, val, offset, size, %s);',
229
- writer, segment.bigendian);
230
- $line('offset += size;');
231
- }
232
-
233
- function size_of(segments) {
234
- var variable = [];
235
- var fixed = 0;
236
-
237
- for (var i = 0; i < segments.length; i++) {
238
- var segment = segments[i];
239
- if (typeof segment.size === 'string' ||
240
- segment.size === true) {
241
- variable.push(segment);
242
- }
243
- else if (segment.type === 'string') {
244
- fixed += Buffer.byteLength(segment.value);
245
- }
246
- else {
247
- fixed += (segment.size * segment.unit) / 8;
248
- }
249
- }
250
-
251
- $line('var buffersize = %d;', fixed);
252
-
253
- if (variable.length > 0) {
254
- for (var j = 0; j < variable.length; j++) {
255
- var segment = variable[j];
256
- if (segment.size === true) {
257
- $line("buffersize += bindings['%s'].length;", segment.name);
258
- }
259
- else {
260
- $line("buffersize += (bindings['%s'] * %d) / 8;",
261
- segment.size, segment.unit);
262
- }
263
- }
264
- }
265
- }
266
-
267
- function emit_write(segments) {
268
- $line('var val, size;');
269
-
270
- var len = segments.length;
271
- for (var i = 0; i < len; i++) {
272
- var segment = segments[i];
273
- $line('// %s', JSON.stringify(segment));
274
- write_seg(segment);
275
- }
276
- }
277
-
278
- function compile_ctor(segments) {
279
- $start();
280
- $line('return function(bindings) {');
281
- $line("'use strict';");
282
- size_of(segments);
283
- $line('var buf = Buffer.alloc(buffersize);');
284
- $line('var offset = 0;');
285
- emit_write(segments);
286
- $line('return buf;');
287
- $line('}'); // end function
288
-
289
- return new Function('write_int', 'write_float', 'Buffer',
290
- $result())(write_int, write_float, Buffer);
291
- }
292
-
293
- module.exports.compile_pattern = compile_pattern;
294
- module.exports.compile = function() {
295
- var str = [].join.call(arguments, ',');
296
- var p = parse(str);
297
- return compile_pattern(p);
298
- };
299
- module.exports.compile_builder = function() {
300
- var str = [].join.call(arguments, ',');
301
- var p = parse(str);
302
- return compile_ctor(p);
303
- };