Skip to content

Commit d8399f7

Browse files
committed
Convert to JavaScript
1 parent c49ba13 commit d8399f7

15 files changed

+710
-976
lines changed

.editorconfig

Lines changed: 0 additions & 3 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
bin
2-
dist
3-
node_modules
4-
test/test.js
5-
test/generated
1+
bin/
2+
node_modules/
3+
test/generated/

.npmignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
bin
2-
src
3-
test
4-
.editorconfig
5-
.gitignore
6-
tsconfig.json
1+
bin/
2+
test/

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 Elias Skogevall
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,29 @@
11
# Protoc Tools gRPC Web Plugin
2-
**Protoc gRPC web generator plugin for @accility/protoc-tools ([NPM](https://npmjs.com/package/accility/protoc-tools) | [GitHub](https://github.com/accility/protoc-tools))**
2+
**This [accility/protoc-tools](https://github.com/accility/protoc-tools) plugin uses [protoc-gen-grpc-web](https://github.com/grpc/grpc-web/releases) to generate the gRPC Web files.** The protoc-gen-grpc-web binary can be downloaded and used as-is, however this have some implications, that mainly being: adding the binary to path and installation. This plugin solves both of theese issues.
33

4-
This package downloads and utilizes the [gRPC Web](https://github.com/grpc/grpc-web) ([protoc-gen-grpc-web](https://github.com/grpc/grpc-web/releases)) plugin from the [gRPC](https://github.com/grpc) project on post-install.
4+
For a more in-depth documentation, visit https://github.com/grpc/grpc-web.
55

6-
[protoc-gen-grpc-web](https://github.com/grpc/grpc-web/releases) is a plugin and can be used with [@accility/protoc-tools](https://github.com/accility/protoc-tools) directly. But protoc-gen-grpc-web is a binary and needs to be installed for respectively operating system, which this plugin does on post-install **and** needs to be executed in PATH, which this plugin adds the binary directory to the process enviroment PATH variable.
7-
8-
## Usage
6+
```
7+
npm install --save-dev @accility/protoc-tools protoc-tools-grpc-web-plugin
8+
```
99

1010
```javascript
1111
import * as tools from '@accility/protoc-tools'
12-
import * as gRPCWeb from '../dist/plugin'
13-
14-
// OR
15-
16-
const tools = require('@accility/protoc-tools')
17-
const gRPCWeb = require('../dist/plugin')
12+
import * as grpcweb from 'protoc-tools-grpc-web-plugin'
1813

1914
tools.protoc({
2015
...
2116
outOptions: [
2217
tools.generators.js({
2318
outOptions: 'import_style=commonjs'
2419
}),
25-
gRPCWeb({
26-
module: 'commonjs', // 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'
27-
// mode: 'grpcwebtext' | 'grpcweb'
20+
grpcweb({
21+
// required: 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'
22+
module: 'commonjs',
23+
24+
// optional: 'grpcwebtext' | 'grpcweb'
25+
mode: 'grpcwebtext'
2826
})
2927
]
3028
})
3129
```
32-
33-
### Module
34-
Import style (https://github.com/grpc/grpc-web#import-style)
35-
36-
- `closure` - the default generated code has Closure `goog.require()` import style.
37-
- `commonjs` - the CommonJS style `require()` is also supported.
38-
- `commonjs+dts` - (experimental) in addition to above, a .d.ts typings file will also be generated for the protobufssages and service stub.
39-
- `typescript` - (experimental) the service stub will be generat
40-
41-
### Mode
42-
Wire format mode (https://github.com/grpc/grpc-web#wire-format-mode)
43-
44-
`grpcwebtext`: The default generated code sends the payload in the grpc-web-text format.
45-
- `Content-type: application/grpc-web-text`
46-
- Payload are base64-encoded.
47-
- Both unary and server streaming calls are supported.
48-
49-
`grpcweb`: A binary protobuf format is also supported.
50-
- `Content-type: application/grpc-web+proto`
51-
- Payload are in the binary protobuf format.
52-
- Only unary calls are supported for now.

index.d.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { OutputOptions } from '@accility/protoc-tools'
2+
3+
export interface Options {
4+
/**
5+
* Import style (https://github.com/grpc/grpc-web#import-style)
6+
*
7+
* - `closure` - the default generated code has Closure `goog.require()` import style.
8+
* - `commonjs` - the CommonJS style `require()` is also supported.
9+
* - `commonjs+dts` - (experimental) in addition to above, a .d.ts typings file will also be generated for the protobuf messages and service stub.
10+
* - `typescript` - (experimental) the service stub will be generated in TypeScript. See TypeScript Support below for information on how to generate TypeScript files.
11+
*/
12+
module: 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'
13+
14+
/**
15+
* Wire format mode (https://github.com/grpc/grpc-web#wire-format-mode)
16+
*
17+
* `mode=grpcwebtext`: The default generated code sends the payload in the grpc-web-text format.
18+
* - `Content-type: application/grpc-web-text`
19+
* - Payload are base64-encoded.
20+
* - Both unary and server streaming calls are supported.
21+
*
22+
* `mode=grpcweb`: A binary protobuf format is also supported.
23+
* - `Content-type: application/grpc-web+proto`
24+
* - Payload are in the binary protobuf format.
25+
* - Only unary calls are supported for now.
26+
*
27+
* @default 'grpcwebtext'
28+
*/
29+
mode?: 'grpcwebtext' | 'grpcweb'
30+
}
31+
32+
export default function plugin(options: Options): OutputOptions

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const path = require('path')
2+
3+
function appendPathEnv(path) {
4+
const name = process.platform === 'win32' ? 'Path' : 'PATH'
5+
const sep = process.platform === 'win32' ? ';' : ':'
6+
const paths = process.env[name].split(sep).filter(v => !!v)
7+
8+
if (!paths.includes(path))
9+
process.env[name] = paths.concat(path).join(sep)
10+
}
11+
12+
function plugin(options) {
13+
appendPathEnv(path.join(__dirname, 'bin'))
14+
15+
return {
16+
name: 'grpc-web',
17+
outOptions: `import_style=${options.module},mode=${options.mode || 'grpcwebtext'}`,
18+
}
19+
}
20+
21+
module.exports = plugin
22+
module.exports.default = plugin

0 commit comments

Comments
 (0)