Skip to content

Commit

Permalink
Convert to JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
tscpp committed Jan 16, 2022
1 parent c49ba13 commit d8399f7
Show file tree
Hide file tree
Showing 15 changed files with 710 additions and 976 deletions.
3 changes: 0 additions & 3 deletions .editorconfig

This file was deleted.

8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
bin
dist
node_modules
test/test.js
test/generated
bin/
node_modules/
test/generated/
8 changes: 2 additions & 6 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
bin
src
test
.editorconfig
.gitignore
tsconfig.json
bin/
test/
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Elias Skogevall

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 12 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
# Protoc Tools gRPC Web Plugin
**Protoc gRPC web generator plugin for @accility/protoc-tools ([NPM](https://npmjs.com/package/accility/protoc-tools) | [GitHub](https://github.com/accility/protoc-tools))**
**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.

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.
For a more in-depth documentation, visit https://github.com/grpc/grpc-web.

[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.

## Usage
```
npm install --save-dev @accility/protoc-tools protoc-tools-grpc-web-plugin
```

```javascript
import * as tools from '@accility/protoc-tools'
import * as gRPCWeb from '../dist/plugin'

// OR

const tools = require('@accility/protoc-tools')
const gRPCWeb = require('../dist/plugin')
import * as grpcweb from 'protoc-tools-grpc-web-plugin'

tools.protoc({
...
outOptions: [
tools.generators.js({
outOptions: 'import_style=commonjs'
}),
gRPCWeb({
module: 'commonjs', // 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'
// mode: 'grpcwebtext' | 'grpcweb'
grpcweb({
// required: 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'
module: 'commonjs',

// optional: 'grpcwebtext' | 'grpcweb'
mode: 'grpcwebtext'
})
]
})
```

### Module
Import style (https://github.com/grpc/grpc-web#import-style)

- `closure` - the default generated code has Closure `goog.require()` import style.
- `commonjs` - the CommonJS style `require()` is also supported.
- `commonjs+dts` - (experimental) in addition to above, a .d.ts typings file will also be generated for the protobufssages and service stub.
- `typescript` - (experimental) the service stub will be generat

### Mode
Wire format mode (https://github.com/grpc/grpc-web#wire-format-mode)

`grpcwebtext`: The default generated code sends the payload in the grpc-web-text format.
- `Content-type: application/grpc-web-text`
- Payload are base64-encoded.
- Both unary and server streaming calls are supported.

`grpcweb`: A binary protobuf format is also supported.
- `Content-type: application/grpc-web+proto`
- Payload are in the binary protobuf format.
- Only unary calls are supported for now.
32 changes: 32 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { OutputOptions } from '@accility/protoc-tools'

export interface Options {
/**
* Import style (https://github.com/grpc/grpc-web#import-style)
*
* - `closure` - the default generated code has Closure `goog.require()` import style.
* - `commonjs` - the CommonJS style `require()` is also supported.
* - `commonjs+dts` - (experimental) in addition to above, a .d.ts typings file will also be generated for the protobuf messages and service stub.
* - `typescript` - (experimental) the service stub will be generated in TypeScript. See TypeScript Support below for information on how to generate TypeScript files.
*/
module: 'closure' | 'commonjs' | 'commonjs+dts' | 'typescript'

/**
* Wire format mode (https://github.com/grpc/grpc-web#wire-format-mode)
*
* `mode=grpcwebtext`: The default generated code sends the payload in the grpc-web-text format.
* - `Content-type: application/grpc-web-text`
* - Payload are base64-encoded.
* - Both unary and server streaming calls are supported.
*
* `mode=grpcweb`: A binary protobuf format is also supported.
* - `Content-type: application/grpc-web+proto`
* - Payload are in the binary protobuf format.
* - Only unary calls are supported for now.
*
* @default 'grpcwebtext'
*/
mode?: 'grpcwebtext' | 'grpcweb'
}

export default function plugin(options: Options): OutputOptions
22 changes: 22 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require('path')

function appendPathEnv(path) {
const name = process.platform === 'win32' ? 'Path' : 'PATH'
const sep = process.platform === 'win32' ? ';' : ':'
const paths = process.env[name].split(sep).filter(v => !!v)

if (!paths.includes(path))
process.env[name] = paths.concat(path).join(sep)
}

function plugin(options) {
appendPathEnv(path.join(__dirname, 'bin'))

return {
name: 'grpc-web',
outOptions: `import_style=${options.module},mode=${options.mode || 'grpcwebtext'}`,
}
}

module.exports = plugin
module.exports.default = plugin
Loading

0 comments on commit d8399f7

Please sign in to comment.