├── .gitignore ├── LICENSE ├── README.md ├── amfUtils.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Deployed apps should consider commenting this line out: 24 | # see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | node-amfutils 2 | ============= 3 | 4 | Node.JS module that provides an API for encoding and decoding of AMF0 and AMF3 protocols 5 | 6 | This little module provides few tools helping encoding and decoding of AMF0/3 using Node's buffers. 7 | 8 | Usage 9 | == 10 | 11 | var amfUtils = require('node-amfutils'); 12 | 13 | var buffer = amfUtils.amf0Encode([{ a: "xxx"},b: null]); 14 | 15 | 16 | 17 | Here is a short list of the utils it provide: 18 | 19 | decodeAmf3Cmd 20 | ==== 21 | 22 | Decodes AMF3 encoded command 23 | 24 | encodeAmf3Cmd 25 | ==== 26 | 27 | Encodes AMF3 encoded command 28 | 29 | decodeAmf0Cmd 30 | ==== 31 | 32 | Decodes AMF3 encoded command 33 | 34 | encodeAmf0Cmd 35 | ==== 36 | 37 | Encodes AMF3 encoded command 38 | 39 | 40 | amf0Encode / amf3Encode 41 | ==== 42 | 43 | Encode Javascript object in AMF0/3 44 | 45 | amf0EncodeOne / amf3EncodeOne 46 | ==== 47 | 48 | Encode one JavaScript variable in AMF0/3 49 | 50 | amf0Decode / amf3Decode 51 | ==== 52 | 53 | Decode AMF0/3 into JavaScript object 54 | 55 | amf0DecodeOne / amf3DecodeOne 56 | ==== 57 | 58 | Decode one only amf0/3 command into JavaScript 59 | 60 | Other tools 61 | ==== 62 | 63 | amf0cnvA2O 64 | amf0cnvO2A 65 | amf0markSArray 66 | amf0decArray 67 | amf0decBool 68 | amf0decDate 69 | amf0decLongString 70 | amf0decNull 71 | amf0decNumber 72 | amf0decObject 73 | amf0decRef 74 | amf0decSArray 75 | amf0decString 76 | amf0decTypedObj 77 | amf0decUndefined 78 | amf0decXmlDoc 79 | amf0encArray 80 | amf0encBool 81 | amf0encDate 82 | amf0encLongString 83 | amf0encNull 84 | amf0encNumber 85 | amf0encObject 86 | amf0encRef 87 | amf0encSArray 88 | amf0encString 89 | amf0encTypedObj 90 | amf0encUndefined 91 | amf0encXmlDoc 92 | amf3decArray 93 | amf3decByteArray 94 | amf3decDate 95 | amf3decDouble 96 | amf3decFalse 97 | amf3decInteger 98 | amf3decNull 99 | amf3decObject 100 | amf3decString 101 | amf3decTrue 102 | amf3decUI29 103 | amf3decUndefined 104 | amf3decXml 105 | amf3decXmlDoc 106 | amf3encArray 107 | amf3encByteArray 108 | amf3encDate 109 | amf3encDouble 110 | amf3encFalse 111 | amf3encInteger 112 | amf3encNull 113 | amf3encObject 114 | amf3encString 115 | amf3encTrue 116 | amf3encUI29 117 | amf3encUndefined 118 | amf3encXml 119 | amf3encXmlDoc 120 | -------------------------------------------------------------------------------- /amfUtils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by delian on 3/12/14. 3 | * This module provides encoding and decoding of the AMF0 and AMF3 format 4 | */ 5 | 6 | 7 | var amf3dRules = { 8 | 0x00: amf3decUndefined, 9 | 0x01: amf3decNull, 10 | 0x02: amf3decFalse, 11 | 0x03: amf3decTrue, 12 | 0x04: amf3decInteger, 13 | 0x05: amf3decDouble, 14 | 0x06: amf3decString, 15 | 0x07: amf3decXmlDoc, 16 | 0x08: amf3decDate, 17 | 0x09: amf3decArray, 18 | 0x0A: amf3decObject, 19 | 0x0B: amf3decXml, 20 | 0x0C: amf3decByteArray //, 21 | // 0x0D: amf3decVecInt, 22 | // 0x0E: amf3decVecUInt, 23 | // 0x0F: amf3decVecDouble, 24 | // 0x10: amf3decVecObject, 25 | // 0x11: amf3decDict // No dictionary support for the moment! 26 | }; 27 | 28 | var amf3eRules = { 29 | 'string': amf3encString, 30 | 'integer': amf3encInteger, 31 | 'double': amf3encDouble, 32 | 'xml': amf3encXmlDoc, 33 | 'object': amf3encObject, 34 | 'array': amf3encArray, 35 | 'sarray': amf3encArray, 36 | 'binary': amf3encByteArray, 37 | 'true': amf3encTrue, 38 | 'false': amf3encFalse, 39 | 'undefined': amf3encUndefined, 40 | 'null': amf3encNull 41 | }; 42 | 43 | var amf0dRules = { 44 | 0x00: amf0decNumber, 45 | 0x01: amf0decBool, 46 | 0x02: amf0decString, 47 | 0x03: amf0decObject, 48 | // 0x04: amf0decMovie, // Reserved 49 | 0x05: amf0decNull, 50 | 0x06: amf0decUndefined, 51 | 0x07: amf0decRef, 52 | 0x08: amf0decArray, 53 | // 0x09: amf0decObjEnd, // Should never happen normally 54 | 0x0A: amf0decSArray, 55 | 0x0B: amf0decDate, 56 | 0x0C: amf0decLongString, 57 | // 0x0D: amf0decUnsupported, // Has been never originally implemented by Adobe! 58 | // 0x0E: amf0decRecSet, // Has been never originally implemented by Adobe! 59 | 0x0F: amf0decXmlDoc, 60 | 0x10: amf0decTypedObj 61 | }; 62 | 63 | var amf0eRules = { 64 | 'string': amf0encString, 65 | 'integer': amf0encNumber, 66 | 'double': amf0encNumber, 67 | 'xml': amf0encXmlDoc, 68 | 'object': amf0encObject, 69 | 'array': amf0encArray, 70 | 'sarray': amf0encSArray, 71 | 'binary': amf0encString, 72 | 'true': amf0encBool, 73 | 'false': amf0encBool, 74 | 'undefined': amf0encUndefined, 75 | 'null': amf0encNull 76 | }; 77 | 78 | function amfType(o) { 79 | var jsType = typeof o; 80 | 81 | if (o === null) return 'null'; 82 | if (jsType == 'undefined') return 'undefined'; 83 | if (jsType == 'number') { 84 | if (parseInt(o) == o) return 'integer'; 85 | return 'double'; 86 | } 87 | if (jsType == 'boolean') return o ? 'true' : 'false'; 88 | if (jsType == 'string') return 'string'; 89 | if (jsType == 'object') { 90 | if (o instanceof Array) { 91 | if (o.sarray) return 'sarray'; 92 | return 'array'; 93 | } 94 | return 'object'; 95 | } 96 | throw new Error('Unsupported type!') 97 | } 98 | 99 | // AMF3 implementation 100 | 101 | /** 102 | * AMF3 Decode undefined value 103 | * @returns {{len: number, value: undefined}} 104 | */ 105 | function amf3decUndefined() { 106 | return { len: 1, value: undefined } 107 | } 108 | 109 | /** 110 | * AMF3 Encode undefined value 111 | * @returns {Buffer} 112 | */ 113 | function amf3encUndefined() { 114 | var buf = new Buffer(1); 115 | buf.writeUInt8(0x00); 116 | return buf; 117 | } 118 | 119 | /** 120 | * AMF3 Decode null 121 | * @returns {{len: number, value: null}} 122 | */ 123 | function amf3decNull() { 124 | return { len: 1, value: null } 125 | } 126 | 127 | /** 128 | * AMF3 Encode null 129 | * @returns {Buffer} 130 | */ 131 | function amf3encNull() { 132 | var buf = new Buffer(1); 133 | buf.writeUInt8(0x01); 134 | return buf; 135 | } 136 | 137 | /** 138 | * AMF3 Decode false 139 | * @returns {{len: number, value: boolean}} 140 | */ 141 | function amf3decFalse() { 142 | return { len: 1, value: false } 143 | } 144 | 145 | /** 146 | * AMF3 Encode false 147 | * @returns {Buffer} 148 | */ 149 | function amf3encFalse() { 150 | var buf = new Buffer(1); 151 | buf.writeUInt8(0x02); 152 | return buf; 153 | } 154 | 155 | /** 156 | * AMF3 Decode true 157 | * @returns {{len: number, value: boolean}} 158 | */ 159 | function amf3decTrue() { 160 | return { len: 1, value: true } 161 | } 162 | 163 | /** 164 | * AMF3 Encode true 165 | * @returns {Buffer} 166 | */ 167 | function amf3encTrue() { 168 | var buf = new Buffer(1); 169 | buf.writeUInt8(0x03); 170 | return buf; 171 | } 172 | 173 | /** 174 | * Generic decode of AMF3 UInt29 values 175 | * @param buf 176 | * @returns {{len: number, value: number}} 177 | */ 178 | function amf3decUI29(buf) { 179 | var val = 0; 180 | var len = 1; 181 | var b; 182 | 183 | do { 184 | b = buf.readUInt8(len++); 185 | val = (val << 7) + (b & 0x7F); 186 | } while (len < 5 || b > 0x7F); 187 | 188 | if (len == 5) val = val | b; // Preserve the major bit of the last byte 189 | 190 | return { len: len, value: val } 191 | } 192 | 193 | /** 194 | * Generic encode of AMF3 UInt29 value 195 | * @param num 196 | * @returns {Buffer} 197 | */ 198 | function amf3encUI29(num) { 199 | var len = 0; 200 | if (num < 0x80) len = 1; 201 | if (num < 0x4000) len = 2; 202 | if (num < 0x200000) len = 3; 203 | if (num >= 0x200000) len = 4; 204 | var buf = new Buffer(len); 205 | switch (len) { 206 | case 1: 207 | buf.writeUInt8(num, 0); 208 | break; 209 | case 2: 210 | buf.writeUInt8(num & 0x7F, 0); 211 | buf.writeUInt8((num >> 7) | 0x80, 1); 212 | break; 213 | case 3: 214 | buf.writeUInt8(num & 0x7F, 0); 215 | buf.writeUInt8((num >> 7) & 0x7F, 1); 216 | buf.writeUInt8((num >> 14) | 0x80, 2); 217 | break; 218 | case 4: 219 | buf.writeUInt8(num & 0xFF, 0); 220 | buf.writeUInt8((num >> 8) & 0x7F, 1); 221 | buf.writeUInt8((num >> 15) | 0x7F, 2); 222 | buf.writeUInt8((num >> 22) | 0x7F, 3); 223 | break; 224 | } 225 | return buf; 226 | } 227 | 228 | /** 229 | * AMF3 Decode an integer 230 | * @param buf 231 | * @returns {{len: number, value: number}} 232 | */ 233 | function amf3decInteger(buf) { // Invert the integer 234 | var resp = amf3decUI29(buf); 235 | if (resp.value > 0x0FFFFFFF) resp.value = (resp.value & 0x0FFFFFFF) - 0x10000000; 236 | return resp; 237 | } 238 | 239 | /** 240 | * AMF3 Encode an integer 241 | * @param num 242 | * @returns {Buffer} 243 | */ 244 | function amf3encInteger(num) { 245 | var buf = new Buffer(1); 246 | buf.writeUInt8(0x4, 0); 247 | return Buffer.concat([buf, amf3encUI29(num & 0x3FFFFFFF)]); // This AND will auto convert the sign bit! 248 | } 249 | 250 | /** 251 | * AMF3 Decode String 252 | * @param buf 253 | * @returns {{len: *, value: (*|String)}} 254 | */ 255 | function amf3decString(buf) { 256 | var sLen = amf3decUI29(buf); 257 | var s = sLen & 1; 258 | sLen = sLen >> 1; // The real length without the lowest bit 259 | if (s) return { len: sLen.value + 5, value: buf.slice(5, sLen.value + 5).toString('utf8') }; 260 | throw new Error("Error, we have a need to decode a String that is a Reference"); // TODO: Implement references! 261 | } 262 | 263 | /** 264 | * AMF3 Encode String 265 | * @param str 266 | * @returns {Buffer} 267 | */ 268 | function amf3encString(str) { 269 | var sLen = amf3encUI29(str.length << 1); 270 | var buf = new Buffer(1); 271 | buf.writeUInt8(0x6, 0); 272 | return Buffer.concat([buf, sLen, new Buffer(str, 'utf8')]); 273 | } 274 | 275 | /** 276 | * AMF3 Decode XMLDoc 277 | * @param buf 278 | * @returns {{len: *, value: (*|String)}} 279 | */ 280 | function amf3decXmlDoc(buf) { 281 | var sLen = amf3decUI29(buf); 282 | var s = sLen & 1; 283 | sLen = sLen >> 1; // The real length without the lowest bit 284 | if (s) return { len: sLen.value + 5, value: buf.slice(5, sLen.value + 5).toString('utf8') }; 285 | throw new Error("Error, we have a need to decode a String that is a Reference"); // TODO: Implement references! 286 | } 287 | 288 | /** 289 | * AMF3 Encode XMLDoc 290 | * @param str 291 | * @returns {Buffer} 292 | */ 293 | function amf3encXmlDoc(str) { 294 | var sLen = amf3encUI29(str.length << 1); 295 | var buf = new Buffer(1); 296 | buf.writeUInt8(0x7, 0); 297 | return Buffer.concat([buf, sLen, new Buffer(str, 'utf8')]); 298 | } 299 | 300 | /** 301 | * AMF3 Decode Generic XML 302 | * @param buf 303 | * @returns {{len: *, value: (*|String)}} 304 | */ 305 | function amf3decXml(buf) { 306 | var sLen = amf3decUI29(buf); 307 | var s = sLen & 1; 308 | sLen = sLen >> 1; // The real length without the lowest bit 309 | if (s) return { len: sLen.value + 5, value: buf.slice(5, sLen.value + 5).toString('utf8') }; 310 | throw new Error("Error, we have a need to decode a String that is a Reference"); // TODO: Implement references! 311 | } 312 | 313 | /** 314 | * AMF3 Encode Generic XML 315 | * @param str 316 | * @returns {Buffer} 317 | */ 318 | function amf3encXml(str) { 319 | var sLen = amf3encUI29(str.length << 1); 320 | var buf = new Buffer(1); 321 | buf.writeUInt8(0x0B, 0); 322 | return Buffer.concat([buf, sLen, new Buffer(str, 'utf8')]); 323 | } 324 | 325 | /** 326 | * AMF3 Decide Byte Array 327 | * @param buf 328 | * @returns {{len: *, value: (Array|string|*|Buffer|Blob)}} 329 | */ 330 | function amf3decByteArray(buf) { 331 | var sLen = amf3decUI29(buf); 332 | var s = sLen & 1; // TODO: Check if we follow the same rule! 333 | sLen = sLen >> 1; // The real length without the lowest bit 334 | if (s) return { len: sLen.value + 5, value: buf.slice(5, sLen.value + 5) }; 335 | throw new Error("Error, we have a need to decode a String that is a Reference"); // TODO: Implement references! 336 | } 337 | 338 | /** 339 | * AMF3 Encode Byte Array 340 | * @param str 341 | * @returns {Buffer} 342 | */ 343 | function amf3encByteArray(str) { 344 | var sLen = amf3encUI29(str.length << 1); 345 | var buf = new Buffer(1); 346 | buf.writeUInt8(0x0C, 0); 347 | return Buffer.concat([buf, sLen, (typeof str == 'string') ? new Buffer(str, 'binary') : str]); 348 | } 349 | 350 | /** 351 | * AMF3 Decode Double 352 | * @param buf 353 | * @returns {{len: number, value: (*|Number)}} 354 | */ 355 | function amf3decDouble(buf) { 356 | return { len: 9, value: buf.readDoubleBE(1) } 357 | } 358 | 359 | /** 360 | * AMF3 Encode Double 361 | * @param num 362 | * @returns {Buffer} 363 | */ 364 | function amf3encDouble(num) { 365 | var buf = new Buffer(9); 366 | buf.writeUInt8(0x05, 0); 367 | buf.writeDoubleBE(num, 1); 368 | return buf; 369 | } 370 | 371 | /** 372 | * AMF3 Decode Date 373 | * @param buf 374 | * @returns {{len: *, value: (*|Number)}} 375 | */ 376 | function amf3decDate(buf) { // The UI29 should be 1 377 | var uTz = amf3decUI29(buf); 378 | var ts = buf.readDoubleBE(uTz.len); 379 | return { len: uTz.len + 8, value: ts } 380 | } 381 | 382 | /** 383 | * AMF3 Encode Date 384 | * @param ts 385 | * @returns {Buffer} 386 | */ 387 | function amf3encDate(ts) { 388 | var buf = new Buffer(1); 389 | buf.writeUInt8(0x8, 0); 390 | var tsBuf = new Buffer(8); 391 | tsBuf.writeDoubleBE(ts, 0); 392 | return Buffer.concat([buf, amf3encUI29(1), tsBuf]); // We always do 1 393 | } 394 | 395 | /** 396 | * AMF3 Decode Array 397 | * @param buf 398 | * @returns {{len: *, value: *}} 399 | */ 400 | function amf3decArray(buf) { 401 | var count = amf3decUI29(buf.slice(1)); 402 | var obj = amf3decObject(buf.slice(count.len)); 403 | if (count.value % 2 == 1) throw new Error("This is a reference to another array, which currently we don't support!"); 404 | return { len: count.len + obj.len, value: obj.value } 405 | } 406 | 407 | /** 408 | * AMF3 Encode Array 409 | */ 410 | function amf3encArray() { 411 | throw new Error('Encoding arrays is not supported yet!'); // TODO: Implement encoding of arrays 412 | } 413 | 414 | /** 415 | * AMF3 Decode Object 416 | * @param buf 417 | */ 418 | function amf3decObject(buf) { 419 | 420 | } 421 | 422 | /** 423 | * AMF3 Encode Object 424 | * @param o 425 | */ 426 | function amf3encObject(o) { 427 | 428 | } 429 | 430 | // AMF0 Implementation 431 | 432 | /** 433 | * AMF0 Decode Number 434 | * @param buf 435 | * @returns {{len: number, value: (*|Number)}} 436 | */ 437 | function amf0decNumber(buf) { 438 | return { len: 9, value: buf.readDoubleBE(1) } 439 | } 440 | 441 | /** 442 | * AMF0 Encode Number 443 | * @param num 444 | * @returns {Buffer} 445 | */ 446 | function amf0encNumber(num) { 447 | var buf = new Buffer(9); 448 | buf.writeUInt8(0x00, 0); 449 | buf.writeDoubleBE(num, 1); 450 | return buf; 451 | } 452 | 453 | /** 454 | * AMF0 Decode Boolean 455 | * @param buf 456 | * @returns {{len: number, value: boolean}} 457 | */ 458 | function amf0decBool(buf) { 459 | return { len: 2, value: (buf.readUInt8(1) != 0) } 460 | } 461 | 462 | /** 463 | * AMF0 Encode Boolean 464 | * @param num 465 | * @returns {Buffer} 466 | */ 467 | function amf0encBool(num) { 468 | var buf = new Buffer(2); 469 | buf.writeUInt8(0x01, 0); 470 | buf.writeUInt8((num ? 1 : 0), 1); 471 | return buf; 472 | } 473 | 474 | /** 475 | * AMF0 Decode Null 476 | * @returns {{len: number, value: null}} 477 | */ 478 | function amf0decNull() { 479 | return { len: 1, value: null } 480 | } 481 | 482 | /** 483 | * AMF0 Encode Null 484 | * @returns {Buffer} 485 | */ 486 | function amf0encNull() { 487 | var buf = new Buffer(1); 488 | buf.writeUInt8(0x05, 0); 489 | return buf; 490 | } 491 | 492 | /** 493 | * AMF0 Decode Undefined 494 | * @returns {{len: number, value: undefined}} 495 | */ 496 | function amf0decUndefined() { 497 | return { len: 1, value: undefined } 498 | } 499 | 500 | /** 501 | * AMF0 Encode Undefined 502 | * @returns {Buffer} 503 | */ 504 | function amf0encUndefined() { 505 | var buf = new Buffer(1); 506 | buf.writeUInt8(0x06, 0); 507 | return buf; 508 | } 509 | 510 | /** 511 | * AMF0 Decode Date 512 | * @param buf 513 | * @returns {{len: number, value: (*|Number)}} 514 | */ 515 | function amf0decDate(buf) { 516 | // var s16 = buf.readInt16BE(1); 517 | var ts = buf.readDoubleBE(3); 518 | return { len: 11, value: ts } 519 | } 520 | 521 | /** 522 | * AMF0 Encode Date 523 | * @param ts 524 | * @returns {Buffer} 525 | */ 526 | function amf0encDate(ts) { 527 | var buf = new Buffer(11); 528 | buf.writeUInt8(0x0B, 0); 529 | buf.writeInt16BE(0, 1); 530 | buf.writeDoubleBE(ts, 3); 531 | return buf; 532 | } 533 | 534 | /** 535 | * AMF0 Decode Object 536 | * @param buf 537 | * @returns {{len: number, value: {}}} 538 | */ 539 | function amf0decObject(buf) { // TODO: Implement references! 540 | var obj = {}; 541 | var iBuf = buf.slice(1); 542 | var len = 1; 543 | // console.log('ODec',iBuf.readUInt8(0)); 544 | while (iBuf.readUInt8(0) != 0x09) { 545 | // console.log('Field', iBuf.readUInt8(0), iBuf); 546 | var prop = amf0decUString(iBuf); 547 | // console.log('Got field for property', prop); 548 | len += prop.len; 549 | if (iBuf.slice(prop.len).readUInt8(0) == 0x09) { 550 | len++; 551 | // console.log('Found the end property'); 552 | break; 553 | } // END Object as value, we shall leave 554 | if (prop.value == '') break; 555 | var val = amf0DecodeOne(iBuf.slice(prop.len)); 556 | // console.log('Got field for value', val); 557 | obj[prop.value] = val.value; 558 | len += val.len; 559 | iBuf = iBuf.slice(prop.len + val.len); 560 | } 561 | return { len: len, value: obj } 562 | } 563 | 564 | /** 565 | * AMF0 Encode Object 566 | */ 567 | function amf0encObject(o) { 568 | if (typeof o !== 'object') return; 569 | 570 | var data = new Buffer(1); 571 | data.writeUInt8(0x03,0); // Type object 572 | var k; 573 | for (k in o) { 574 | data = Buffer.concat([data,amf0encUString(k),amf0EncodeOne(o[k])]); 575 | } 576 | var termCode = new Buffer(1); 577 | termCode.writeUInt8(0x09,0); 578 | return Buffer.concat([data,amf0encUString(''),termCode]); 579 | } 580 | 581 | /** 582 | * AMF0 Decode Reference 583 | * @param buf 584 | * @returns {{len: number, value: string}} 585 | */ 586 | function amf0decRef(buf) { 587 | var index = buf.readUInt16BE(1); 588 | return { len: 3, value: 'ref' + index } 589 | } 590 | 591 | /** 592 | * AMF0 Encode Reference 593 | * @param index 594 | * @returns {Buffer} 595 | */ 596 | function amf0encRef(index) { 597 | var buf = new Buffer(3); 598 | buf.writeUInt8(0x07, 0); 599 | buf.writeUInt16BE(index, 1); 600 | return buf; 601 | } 602 | 603 | /** 604 | * AMF0 Decode String 605 | * @param buf 606 | * @returns {{len: *, value: (*|string|String)}} 607 | */ 608 | function amf0decString(buf) { 609 | var sLen = buf.readUInt16BE(1); 610 | return { len: 3 + sLen, value: buf.toString('utf8', 3, 3 + sLen) } 611 | } 612 | 613 | /** 614 | * AMF0 Decode Untyped (without the type byte) String 615 | * @param buf 616 | * @returns {{len: *, value: (*|string|String)}} 617 | */ 618 | function amf0decUString(buf) { 619 | var sLen = buf.readUInt16BE(0); 620 | return { len: 2 + sLen, value: buf.toString('utf8', 2, 2 + sLen) } 621 | } 622 | 623 | /** 624 | * Do AMD0 Encode of Untyped String 625 | * @param s 626 | * @returns {Buffer} 627 | */ 628 | function amf0encUString(s) { 629 | var data = new Buffer(s,'utf8'); 630 | var sLen = new Buffer(2); 631 | sLen.writeUInt16BE(data.length,0); 632 | return Buffer.concat([sLen,data]); 633 | } 634 | 635 | /** 636 | * AMF0 Encode String 637 | * @param str 638 | * @returns {Buffer} 639 | */ 640 | function amf0encString(str) { 641 | var buf = new Buffer(3); 642 | buf.writeUInt8(0x02, 0); 643 | buf.writeUInt16BE(str.length, 1); 644 | return Buffer.concat([buf, new Buffer(str, 'utf8')]); 645 | } 646 | 647 | /** 648 | * AMF0 Decode Long String 649 | * @param buf 650 | * @returns {{len: *, value: (*|string|String)}} 651 | */ 652 | function amf0decLongString(buf) { 653 | var sLen = buf.readUInt32BE(1); 654 | return { len: 5 + sLen, value: buf.toString('utf8', 5, 5 + sLen) } 655 | } 656 | 657 | /** 658 | * AMF0 Encode Long String 659 | * @param str 660 | * @returns {Buffer} 661 | */ 662 | function amf0encLongString(str) { 663 | var buf = new Buffer(5); 664 | buf.writeUInt8(0x0C, 0); 665 | buf.writeUInt32BE(str.length, 1); 666 | return Buffer.concat([buf, new Buffer(str, 'utf8')]); 667 | } 668 | 669 | /** 670 | * AMF0 Decode Array 671 | * @param buf 672 | * @returns {{len: *, value: ({}|*)}} 673 | */ 674 | function amf0decArray(buf) { 675 | // var count = buf.readUInt32BE(1); 676 | var obj = amf0decObject(buf.slice(4)); 677 | return { len: 5 + obj.len, value: obj.value } 678 | } 679 | 680 | /** 681 | * AMF0 Encode Array 682 | */ 683 | function amf0encArray(a) { 684 | var l = 0; 685 | if (a instanceof Array) l = a.length; else l = Object.keys(a).length; 686 | console.log('Array encode', l, a); 687 | var buf = new Buffer(5); 688 | buf.writeUInt8(8,0); 689 | buf.writeUInt32BE(l,1); 690 | var data = amf0encObject(a); 691 | return Buffer.concat([buf,data.slice(1)]); 692 | } 693 | 694 | /** 695 | * AMF0 Encode Binary Array into binary Object 696 | * @param aData 697 | * @returns {Buffer} 698 | */ 699 | function amf0cnvArray2Object(aData) { 700 | var buf = new Buffer(1); 701 | buf.writeUInt8(0x3,0); // Object id 702 | return Buffer.concat([buf,aData.slice(5)]); 703 | } 704 | 705 | /** 706 | * AMF0 Encode Binary Object into binary Array 707 | * @param oData 708 | * @returns {Buffer} 709 | */ 710 | function amf0cnvObject2Array(oData) { 711 | var buf = new Buffer(5); 712 | var o = amf0decObject(oData); 713 | var l = Object.keys(o).length; 714 | buf.writeUInt32BE(l,1); 715 | return Buffer.concat([buf,oData.slice(1)]); 716 | } 717 | 718 | /** 719 | * AMF0 Decode XMLDoc 720 | * @param buf 721 | * @returns {{len: *, value: (*|string|String)}} 722 | */ 723 | function amf0decXmlDoc(buf) { 724 | var sLen = buf.readUInt16BE(1); 725 | return { len: 3 + sLen, value: buf.toString('utf8', 3, 3 + sLen) } 726 | } 727 | 728 | /** 729 | * AMF0 Encode XMLDoc 730 | * @param str 731 | * @returns {Buffer} 732 | */ 733 | function amf0encXmlDoc(str) { // Essentially it is the same as string 734 | var buf = new Buffer(3); 735 | buf.writeUInt8(0x0F, 0); 736 | buf.writeUInt16BE(str.length, 1); 737 | return Buffer.concat([buf, new Buffer(str, 'utf8')]); 738 | } 739 | 740 | /** 741 | * AMF0 Decode Strict Array 742 | * @param buf 743 | * @returns {{len: number, value: Array}} 744 | */ 745 | function amf0decSArray(buf) { 746 | var a = []; 747 | var len = 5; 748 | var ret; 749 | for (var count = buf.readUInt32BE(1); count; count--) { 750 | ret = amf0DecodeOne(buf.slice(len)); 751 | a.push(ret.value); 752 | len += ret.len; 753 | } 754 | return { len: len, value: amf0markSArray(a) } 755 | } 756 | 757 | /** 758 | * AMF0 Encode Strict Array 759 | * @param a Array 760 | */ 761 | function amf0encSArray(a) { 762 | console.log('Do strict array!'); 763 | var buf = new Buffer(5); 764 | buf.writeUInt8(0x0A,0); 765 | buf.writeUInt32BE(a.length,1); 766 | var i; 767 | for (i=0;i< a.length;i++) { 768 | buf = Buffer.concat([buf,amf0EncodeOne(a[i])]); 769 | } 770 | return buf; 771 | } 772 | 773 | function amf0markSArray(a) { 774 | Object.defineProperty(a,'sarray', { value: true }); 775 | return a; 776 | } 777 | 778 | /** 779 | * AMF0 Decode Typed Object 780 | * @param buf 781 | * @returns {{len: number, value: ({}|*)}} 782 | */ 783 | function amf0decTypedObj(buf) { 784 | var className = amf0decString(buf); 785 | var obj = amf0decObject(buf.slice(className.len - 1)); 786 | obj.value.__className__ = className.value; 787 | return { len: className.len + obj.len - 1, value: obj.value } 788 | } 789 | 790 | /** 791 | * AMF0 Encode Typed Object 792 | */ 793 | function amf0encTypedObj() { 794 | throw new Error("Error: SArray encoding is not yet implemented!"); // TODO: Error 795 | } 796 | 797 | /** 798 | * Decode one value from the Buffer according to the applied rules 799 | * @param rules 800 | * @param buffer 801 | * @returns {*} 802 | */ 803 | function amfXDecodeOne(rules, buffer) { 804 | if (!rules[buffer.readUInt8(0)]) { 805 | console.log('Unknown field', buffer.readUInt8(0)); 806 | throw new Error("Error: Unknown field"); 807 | } 808 | return rules[buffer.readUInt8(0)](buffer); 809 | } 810 | 811 | /** 812 | * Decode one AMF0 value 813 | * @param buffer 814 | * @returns {*} 815 | */ 816 | function amf0DecodeOne(buffer) { 817 | return amfXDecodeOne(amf0dRules, buffer); 818 | } 819 | 820 | /** 821 | * Decode one AMF3 value 822 | * @param buffer 823 | * @returns {*} 824 | */ 825 | function amf3DecodeOne(buffer) { 826 | return amfXDecodeOne(amf3dRules, buffer); 827 | } 828 | 829 | /** 830 | * Decode a whole buffer of AMF values according to rules and return in array 831 | * @param rules 832 | * @param buffer 833 | * @returns {Array} 834 | */ 835 | function amfXDecode(rules, buffer) { 836 | // We shall receive clean buffer and will respond with an array of values 837 | var resp = []; 838 | var res; 839 | for (var i = 0; i < buffer.length;) { 840 | res = amfXDecodeOne(rules, buffer.slice(i)); 841 | i += res.len; 842 | resp.push(res.value); // Add the response 843 | } 844 | return resp; 845 | } 846 | 847 | /** 848 | * Decode a buffer of AMF3 values 849 | * @param buffer 850 | * @returns {Array} 851 | */ 852 | function amf3Decode(buffer) { 853 | return amfXDecode(amf3dRules, buffer); 854 | } 855 | 856 | /** 857 | * Decode a buffer of AMF0 values 858 | * @param buffer 859 | * @returns {Array} 860 | */ 861 | function amf0Decode(buffer) { 862 | return amfXDecode(amf0dRules, buffer); 863 | } 864 | 865 | /** 866 | * Encode one AMF value according to rules 867 | * @param rules 868 | * @param o 869 | * @returns {*} 870 | */ 871 | function amfXEncodeOne(rules, o) { 872 | // console.log('amfXEncodeOne type',o,amfType(o),rules[amfType(o)]); 873 | var f = rules[amfType(o)]; 874 | if (f) return f(o); 875 | throw new Error('Unsupported type for encoding!'); 876 | } 877 | 878 | /** 879 | * Encode one AMF0 value 880 | * @param o 881 | * @returns {*} 882 | */ 883 | function amf0EncodeOne(o) { 884 | return amfXEncodeOne(amf0eRules, o); 885 | } 886 | 887 | /** 888 | * Encode one AMF3 value 889 | * @param o 890 | * @returns {*} 891 | */ 892 | function amf3EncodeOne(o) { 893 | return amfXEncodeOne(amf3eRules, o); 894 | } 895 | 896 | /** 897 | * Encode an array of values into a buffer 898 | * @param a 899 | * @returns {Buffer} 900 | */ 901 | function amf3Encode(a) { 902 | var buf = new Buffer(0); 903 | a.forEach(function (o) { 904 | buf = Buffer.concat([buf, amf3EncodeOne(o)]); 905 | }); 906 | return buf; 907 | } 908 | 909 | /** 910 | * Encode an array of values into a buffer 911 | * @param a 912 | * @returns {Buffer} 913 | */ 914 | function amf0Encode(a) { 915 | var buf = new Buffer(0); 916 | a.forEach(function (o) { 917 | buf = Buffer.concat([buf, amf0EncodeOne(o)]); 918 | }); 919 | return buf; 920 | } 921 | 922 | 923 | var rtmpCmdDecode = { 924 | "_result": [ "transId","cmdObj","info" ], 925 | "_error": [ "transId","cmdObj","info","streamId" ], // Info / Streamid are optional 926 | "onStatus": [ "transId","cmdObj","info" ], 927 | "releaseStream": [ "transId", "cmdObj", "streamId" ], 928 | "getStreamLength": [ "transId", "cmdObj", "streamId" ], 929 | "getMovLen": [ "transId", "cmdObj", "streamId" ], 930 | "FCPublish": [ "transId", "cmdObj", "streamId" ], 931 | "onFCPublish": [ "transId", "cmdObj", "info" ], 932 | "connect": [ "transId","cmdObj","args" ], 933 | "call": [ "transId","cmdObj","args" ], 934 | "createStream": [ "transId","cmdObj"], 935 | "close": ["transId","cmdObj"], 936 | "play": [ "transId","cmdObj","streamName","start","duration","reset" ], 937 | "play2": [ "transId","cmdObj","params" ], 938 | "deleteStream": [ "transId","cmdObj","streamId" ], 939 | "receiveAudio": [ "transId","cmdObj","bool" ], 940 | "receiveVideo": [ "transId","cmdObj","bool" ], 941 | "publish": [ "transId","cmdObj","name","type" ], 942 | "seek": [ "transId","cmdObj","ms" ], 943 | "pause": [ "transId","cmdObj","pause","ms" ] 944 | }; 945 | 946 | /** 947 | * Decode a command! 948 | * @param dbuf 949 | * @returns {{cmd: (*|string|String|*), value: *}} 950 | */ 951 | function decodeAMF0Cmd(dbuf) { 952 | var buffer = dbuf; 953 | var resp = {}; 954 | 955 | var cmd = amf0DecodeOne(buffer); 956 | resp.cmd = cmd.value; 957 | buffer = buffer.slice(cmd.len); 958 | 959 | if (rtmpCmdDecode[cmd.value]) { 960 | rtmpCmdDecode[cmd.value].forEach(function (n) { 961 | if (buffer.length > 0) { 962 | var r = amf0DecodeOne(buffer); 963 | buffer = buffer.slice(r.len); 964 | resp[n] = r.value; 965 | } 966 | }); 967 | } else { 968 | console.log('Unknown command', resp); 969 | } 970 | return resp 971 | } 972 | 973 | /** 974 | * Encode AMF0 Command 975 | * @param opt 976 | * @returns {*} 977 | */ 978 | function encodeAMF0Cmd(opt) { 979 | var data = amf0EncodeOne(opt.cmd); 980 | 981 | if (rtmpCmdDecode[opt.cmd]) { 982 | rtmpCmdDecode[opt.cmd].forEach(function (n) { 983 | if (opt.hasOwnProperty(n)) 984 | data = Buffer.concat([data, amf0EncodeOne(opt[n])]); 985 | }); 986 | } else { 987 | console.log('Unknown command', opt); 988 | } 989 | // console.log('Encoded as',data.toString('hex')); 990 | return data 991 | } 992 | 993 | /** 994 | * 995 | * @param dbuf 996 | * @returns {{}} 997 | */ 998 | function decodeAMF3Cmd(dbuf) { 999 | var buffer = dbuf; 1000 | var resp = {}; 1001 | 1002 | var cmd = amf3DecodeOne(buffer); 1003 | resp.cmd = cmd.value; 1004 | buffer = buffer.slice(cmd.len); 1005 | 1006 | if (rtmpCmdDecode[cmd.value]) { 1007 | rtmpCmdDecode[cmd.value].forEach(function (n) { 1008 | if (buffer.length > 0) { 1009 | var r = amf3DecodeOne(buffer); 1010 | buffer = buffer.slice(r.len); 1011 | resp[n] = r.value; 1012 | } 1013 | }); 1014 | } else { 1015 | console.log('Unknown command', resp); 1016 | } 1017 | return resp 1018 | } 1019 | 1020 | /** 1021 | * Encode AMF3 Command 1022 | * @param opt 1023 | * @returns {*} 1024 | */ 1025 | function encodeAMF3Cmd(opt) { 1026 | var data = amf0EncodeOne(opt.cmd); 1027 | 1028 | if (rtmpCmdDecode[opt.cmd]) { 1029 | rtmpCmdDecode[opt.cmd].forEach(function (n) { 1030 | if (opt.hasOwnProperty(n)) 1031 | data = Buffer.concat([data, amf3EncodeOne(opt[n])]); 1032 | }); 1033 | } else { 1034 | console.log('Unknown command', opt); 1035 | } 1036 | return data 1037 | } 1038 | 1039 | module.exports = { 1040 | decodeAmf3Cmd: decodeAMF3Cmd, 1041 | encodeAmf3Cmd: encodeAMF3Cmd, 1042 | decodeAmf0Cmd: decodeAMF0Cmd, 1043 | encodeAmf0Cmd: encodeAMF0Cmd, 1044 | amfType: amfType, 1045 | amf0Encode: amf0Encode, 1046 | amf0EncodeOne: amf0EncodeOne, 1047 | amf0Decode: amf0Decode, 1048 | amf0DecodeOne: amf0DecodeOne, 1049 | amf3Encode: amf3Encode, 1050 | amf3EncodeOne: amf3EncodeOne, 1051 | amf3Decode: amf3Decode, 1052 | amf3DecodeOne: amf3DecodeOne, 1053 | amf0cnvA2O: amf0cnvArray2Object, 1054 | amf0cnvO2A: amf0cnvObject2Array, 1055 | amf0markSArray: amf0markSArray, 1056 | amf0decArray: amf0decArray, 1057 | amf0decBool: amf0decBool, 1058 | amf0decDate: amf0decDate, 1059 | amf0decLongString: amf0decLongString, 1060 | amf0decNull: amf0decNull, 1061 | amf0decNumber: amf0decNumber, 1062 | amf0decObject: amf0decObject, 1063 | amf0decRef: amf0decRef, 1064 | amf0decSArray: amf0decSArray, 1065 | amf0decString: amf0decString, 1066 | amf0decTypedObj: amf0decTypedObj, 1067 | amf0decUndefined: amf0decUndefined, 1068 | amf0decXmlDoc: amf0decXmlDoc, 1069 | amf0encArray: amf0encArray, 1070 | amf0encBool: amf0encBool, 1071 | amf0encDate: amf0encDate, 1072 | amf0encLongString: amf0encLongString, 1073 | amf0encNull: amf0encNull, 1074 | amf0encNumber: amf0encNumber, 1075 | amf0encObject: amf0encObject, 1076 | amf0encRef: amf0encRef, 1077 | amf0encSArray: amf0encSArray, 1078 | amf0encString: amf0encString, 1079 | amf0encTypedObj: amf0encTypedObj, 1080 | amf0encUndefined: amf0encUndefined, 1081 | amf0encXmlDoc: amf0encXmlDoc, 1082 | amf3decArray: amf3decArray, 1083 | amf3decByteArray: amf3decByteArray, 1084 | amf3decDate: amf3decDate, 1085 | amf3decDouble: amf3decDouble, 1086 | amf3decFalse: amf3decFalse, 1087 | amf3decInteger: amf3decInteger, 1088 | amf3decNull: amf3decNull, 1089 | amf3decObject: amf3decObject, 1090 | amf3decString: amf3decString, 1091 | amf3decTrue: amf3decTrue, 1092 | amf3decUI29: amf3decUI29, 1093 | amf3decUndefined: amf3decUndefined, 1094 | amf3decXml: amf3decXml, 1095 | amf3decXmlDoc: amf3decXmlDoc, 1096 | amf3encArray: amf3encArray, 1097 | amf3encByteArray: amf3encByteArray, 1098 | amf3encDate: amf3encDate, 1099 | amf3encDouble: amf3encDouble, 1100 | amf3encFalse: amf3encFalse, 1101 | amf3encInteger: amf3encInteger, 1102 | amf3encNull: amf3encNull, 1103 | amf3encObject: amf3encObject, 1104 | amf3encString: amf3encString, 1105 | amf3encTrue: amf3encTrue, 1106 | amf3encUI29: amf3encUI29, 1107 | amf3encUndefined: amf3encUndefined, 1108 | amf3encXml: amf3encXml, 1109 | amf3encXmlDoc: amf3encXmlDoc 1110 | }; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node-amfutils", 3 | "version": "0.0.1", 4 | "description": "Node.JS module that provides an API for encoding and decoding of AMF0 and AMF3 protocols", 5 | "main": "amfUtils.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/delian/node-amfutils.git" 12 | }, 13 | "keywords": [ 14 | "node", 15 | "amf", 16 | "adobe", 17 | "rtmp", 18 | "amd0", 19 | "amf3" 20 | ], 21 | "author": "Delian Delchev (http://delian.blogspot.com/)", 22 | "license": "GPLv2", 23 | "bugs": { 24 | "url": "https://github.com/delian/node-amfutils/issues" 25 | }, 26 | "homepage": "https://github.com/delian/node-amfutils" 27 | } 28 | --------------------------------------------------------------------------------