tars2node/README.md

66 lines
3.6 KiB
Markdown
Raw Normal View History

2018-07-27 12:33:14 +08:00
# Tars2Node
2020-02-06 21:51:00 +08:00
`tars2node` can convert the Tars IDL definition file to the version used by the JavaScript language, combined with the [@tars/stream](https://www.npmjs.com/package/@tars/stream) module to perform codec operations.
2018-07-27 12:33:14 +08:00
2020-02-06 21:51:00 +08:00
If you have Tars RPC requirements, you can call it in conjunction with the [@tars/rpc](https://www.npmjs.com/package/@tars/rpc) module.
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
## Usage
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
Just use the `tars2node` precompiler in the build directory (Linux platform).
2018-07-27 12:33:14 +08:00
2020-02-06 21:51:00 +08:00
```bash
2018-07-27 12:33:14 +08:00
tars2node [OPTIONS] tarsfile
2020-02-06 21:51:00 +08:00
```
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
## options
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
Options | Functions |
2018-07-27 15:55:07 +08:00
| ------------- | ------------- |
2020-02-06 21:51:00 +08:00
| --stream-path = [DIRECTORY] | Specify the codec module name, the default is @tars/stream. |
| --rpc-path = [DIRECTORY] | Specify the RPC module name. The default is @tars/rpc. |
2020-02-04 13:04:17 +08:00
| --allow-reserved-namespace | Whether to allow `tars` as a namespace (because this namespace is mainly used for tars file definitions for framework services). |
| --dir = [DIRECTORY] | Output directory for generated files. |
| --relative | Limits all `.tars` files to look in the current directory. |
| --tarsBase = [DIRECTORY] | Specify the search directory for `.tars` files. |
| --r | Convert nested `.tars` files. |
| --r-minimal | Reduce dependent files and remove unnecessary members. |
| --r-reserved | Members to keep when thinning dependent files. |
| --client | Generate the calling class code for the client. |
| --server | Generate server-side framework code. |
| --ts | Turning on this option will only generate TypeScript (.ts) code. |
| --dts | Append a TypeScript description file (.d.ts) when generating. |
2020-02-06 21:51:00 +08:00
| --long-type = [number | string | bigint] | Optionally use \ <Number \ | String \ | BigInt \> to express the \ <long \> type, with a default value of \<Number \>. |
| --string-binary-encoding | When you encounter character encoding problems or need to access the original data, turn on this option to use \<buffer \> to store \<string \>. |
| --enum-reverse-mappings | Output code \<enum \> supports reverse mapping of enum values to enum names. |
| --optimize = [0 \| s] | Optimize the output code size. The default is 0 (that is, not optimized). |
2020-02-04 13:04:17 +08:00
## Examples
2020-02-06 21:51:00 +08:00
```bash
2018-07-27 15:55:07 +08:00
tars2node Protocol.tars
2020-02-06 21:51:00 +08:00
```
2018-07-27 15:55:07 +08:00
2020-02-04 13:04:17 +08:00
The above command will convert the constants, enumerations, and structures defined in the Protocol.tars file to generate ProtocolTars.js for encoding and decoding.
2020-02-06 21:51:00 +08:00
For usage, please refer to the [@tars/stream](https://www.npmjs.com/package/@tars/stream) module documentation.
2018-07-27 15:55:07 +08:00
2020-02-06 21:51:00 +08:00
```bash
2018-07-27 15:55:07 +08:00
tars2node Protocol.tars --client
2020-02-06 21:51:00 +08:00
```
2018-07-27 15:55:07 +08:00
2020-02-04 13:04:17 +08:00
The above command will convert the data types such as `constant`,` enumeration value`, `structure` defined in the file, and convert the` interface` description section into a Tars RPC client interface file, and finally generate `ProtocolProxy.js` for Used by the caller.
2020-02-06 21:51:00 +08:00
For usage, please refer to [@tars/rpc](https://www.npmjs.com/package/@tars/rpc) module documentation.
2018-07-27 15:55:07 +08:00
2020-02-06 21:51:00 +08:00
```bash
2018-07-27 15:55:07 +08:00
tars2node Protocol.tars --server
2020-02-06 21:51:00 +08:00
```
2018-07-27 15:55:07 +08:00
2020-02-06 21:51:00 +08:00
The above command will convert data types such as `constant`,` enumeration value`, `struct` defined in the file, and convert the` interface` description section into a Tars RPC server interface file, and finally generate `Protocol.js` and `ProtocolImp.js` is used by the service provider.
2020-02-04 13:04:17 +08:00
Developers do not need to change `Protocol.js`, they only need to continue to improve the specific functions in the` ProtocolImp.js` implementation file to provide services as Tars RPC server.
2020-02-06 21:51:00 +08:00
For usage, please refer to [@tars/rpc](https://www.npmjs.com/package/@tars/rpc) module documentation.
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
## Compile from source
2018-07-27 12:33:14 +08:00
2020-02-04 13:04:17 +08:00
1. Install build-essential for the corresponding platform
2020-02-06 21:51:00 +08:00
2. Install [CMake](https://cmake.org/)
2020-02-04 13:04:17 +08:00
3. Execute `cmake ../ && make` in the source build directory