Skip to content

Commit

Permalink
Merge branch 'master' into grpc-js_priority_load_balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
murgatroid99 committed May 21, 2020
2 parents 7aa9177 + 075a75b commit 8ad1f82
Show file tree
Hide file tree
Showing 384 changed files with 1,302 additions and 30,033 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/grpc-tools-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: grpc-tools Build

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
linux_build:
name: Linux grpc-tools Build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build
run: |
docker build -t kokoro-native-image tools/release/native
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $GITHUB_WORKSPACE:$GITHUB_WORKSPACE kokoro-native-image $GITHUB_WORKSPACE/packages/grpc-tools/build_binaries.sh
- uses: actions/upload-artifact@v2
with:
name: grpc-tools_linux
path: artifacts/
macos_build:
name: Macos grpc-tools Build
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build
run: packages/grpc-tools/build_binaries.sh
- uses: actions/upload-artifact@v2
with:
name: grpc-tools_macos
path: artifacts/
windows_build:
name: Windows grpc-tools Build
runs-on: windows-latest
strategy:
matrix:
arch: [ia32, x64]
env:
ARCH: ${{matrix.arch}}
steps:
- uses: actions/checkout@v2
with:
submodules: recursive
- name: Build
run: powershell -File ./packages/grpc-tools/build_binaries.ps1
shell: cmd
- uses: actions/upload-artifact@v1
with:
name: grpc-tools_windows_${{matrix.arch}}
path: artifacts/
combine_artifacts:
name: Combine grpc-tools artifacts
runs-on: ubuntu-latest
needs: [linux_build, macos_build, windows_build]
steps:
- uses: actions/download-artifact@v2
- name: Copy
run: |
mkdir artifacts
cp -r ./**/* artifacts/
- uses: actions/upload-artifact@v2
with:
name: combined-artifacts
path: artifacts/
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ yarn.lock
\#*\#
.\#*

packages/grpc-native-core/src/node/

.nyc_output/
reports/

Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "packages/grpc-native-core/deps/grpc"]
path = packages/grpc-native-core/deps/grpc
url = https://github.com/grpc/grpc.git
[submodule "packages/grpc-tools/deps/protobuf"]
path = packages/grpc-tools/deps/protobuf
url = https://github.com/protocolbuffers/protobuf
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ For a comparison of the features available in these two libraries, see [this doc

### C-based Client and Server

Directory: [`packages/grpc-native-core`](https://github.com/grpc/grpc-node/tree/master/packages/grpc-native-core) (see here for installation information)
Directory: [`packages/grpc-native-core`](https://github.com/grpc/grpc-node/tree/[email protected]/packages/grpc-native-core) (lives in the `[email protected]` branch) (see here for installation information)

npm package: [grpc](https://www.npmjs.com/package/grpc).

Expand Down
31 changes: 8 additions & 23 deletions gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,40 @@
import * as gulp from 'gulp';
import * as healthCheck from './packages/grpc-health-check/gulpfile';
import * as jsCore from './packages/grpc-js/gulpfile';
import * as nativeCore from './packages/grpc-native-core/gulpfile';
import * as protobuf from './packages/proto-loader/gulpfile';
import * as internalTest from './test/gulpfile';

const root = __dirname;
const installAll = gulp.series(jsCore.install, healthCheck.install, protobuf.install, internalTest.install);

const installAll = gulp.series(jsCore.install, nativeCore.install, healthCheck.install, protobuf.install, internalTest.install);
const lint = gulp.parallel(jsCore.lint);

const installAllWindows = gulp.series(jsCore.install, nativeCore.installWindows, healthCheck.install, protobuf.install, internalTest.install);
const build = gulp.series(jsCore.compile, protobuf.compile);

const lint = gulp.parallel(jsCore.lint, nativeCore.lint);

const build = gulp.series(jsCore.compile, nativeCore.build, protobuf.compile);

const link = gulp.series(healthCheck.linkAdd);

const setup = gulp.series(installAll, link);

const setupWindows = gulp.series(installAllWindows, link);
const setup = gulp.series(installAll);

const setupPureJSInterop = gulp.series(jsCore.install, protobuf.install, internalTest.install);

const clean = gulp.series(jsCore.clean, nativeCore.clean, protobuf.clean);
const clean = gulp.series(jsCore.clean, protobuf.clean);

const cleanAll = gulp.series(jsCore.cleanAll, nativeCore.cleanAll, healthCheck.cleanAll, internalTest.cleanAll, protobuf.cleanAll);
const cleanAll = gulp.series(jsCore.cleanAll, internalTest.cleanAll, protobuf.cleanAll);

const nativeTestOnly = gulp.parallel(nativeCore.test, healthCheck.test);
const nativeTestOnly = gulp.parallel(healthCheck.test);

const nativeTest = gulp.series(build, nativeTestOnly);

const testOnly = gulp.parallel(jsCore.test, nativeTestOnly, protobuf.test);

const test = gulp.series(build, testOnly, internalTest.test);

const docGen = gulp.series(nativeCore.docGen);

export {
installAll,
installAllWindows,
lint,
build,
link,
setup,
setupWindows,
setupPureJSInterop,
clean,
cleanAll,
nativeTestOnly,
nativeTest,
test,
docGen
test
};
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,13 @@
"include": [
"packages/grpc-health-check/health.js",
"packages/grpc-js/build/src/*",
"packages/grpc-native-core/index.js",
"packages/grpc-native-core/src/*.js",
"packages/proto-loader/build/src/*"
],
"cache": true,
"all": true
},
"scripts": {
"test": "nyc gulp test && GRPC_DNS_RESOLVER=ares nyc gulp nativeTestOnly",
"test": "nyc gulp test",
"coverage": "nyc report --reporter=text-lcov | coveralls"
}
}
14 changes: 2 additions & 12 deletions packages/grpc-health-check/gulpfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,15 @@ const healthCheckDir = __dirname;
const baseDir = path.resolve(healthCheckDir, '..', '..');
const testDir = path.resolve(healthCheckDir, 'test');

const cleanLinks = () => del(path.resolve(healthCheckDir, 'node_modules/grpc'));

const cleanAll = gulp.parallel(cleanLinks);

const runInstall = () => execa('npm', ['install', '--unsafe-perm'], {cwd: healthCheckDir, stdio: 'inherit'});

const install = gulp.series(cleanLinks, runInstall);
const runRebuild = () => execa('npm', ['rebuild', '--unsafe-perm'], {cwd: healthCheckDir, stdio: 'inherit'});

const linkAdd = (callback) => {
linkSync(healthCheckDir, './node_modules/grpc', '../grpc-native-core');
callback();
}
const install = gulp.series(runInstall, runRebuild);

const test = () => gulp.src(`${testDir}/*.js`).pipe(mocha({reporter: 'mocha-jenkins-reporter'}));

export {
cleanLinks,
cleanAll,
install,
linkAdd,
test
}
36 changes: 18 additions & 18 deletions packages/grpc-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,31 @@ npm install @grpc/grpc-js

## Features

- Clients
- Automatic reconnection
- Servers
- Streaming
- Metadata
- Partial compression support: clients can decompress response messages
- Pick first and round robin load balancing policies
- Client Interceptors
- Connection Keepalives
- HTTP Connect support (proxies)
- Clients
- Automatic reconnection
- Servers
- Streaming
- Metadata
- Partial compression support: clients can decompress response messages
- Pick first and round robin load balancing policies
- Client Interceptors
- Connection Keepalives
- HTTP Connect support (proxies)

This library does not directly handle `.proto` files. To use `.proto` files with this library we recommend using the `@grpc/proto-loader` package.

## Migrating from [`grpc`](https://www.npmjs.com/package/grpc)
## Migrating from [`grpc`](https://www.npmjs.com/package/grpc)

`@grpc/grpc-js` is almost a drop-in replacement for `grpc`, but you may need to make a few code changes to use it:
`@grpc/grpc-js` is almost a drop-in replacement for `grpc`, but you may need to make a few code changes to use it:

- If you are currently loading `.proto` files using `grpc.load`, that function is not available in this library. You should instead load your `.proto` files using `@grpc/proto-loader` and load the resulting package definition objects into `@grpc/grpc-js` using `grpc.loadPackageDefinition`.
- If you are currently loading packages generated by `grpc-tools`, you should instead generate your files using the `--generate_package_definitions` option in `grpc-tools`, then load the object exported by the generated file into `@grpc/grpc-js` using `grpc.loadPackageDefinition`.
- If you have a server and you are using `Server#bind` to bind ports, you will need to use `Server#bindAsync` instead.
- If you are currently loading `.proto` files using `grpc.load`, that function is not available in this library. You should instead load your `.proto` files using `@grpc/proto-loader` and load the resulting package definition objects into `@grpc/grpc-js` using `grpc.loadPackageDefinition`.
- If you are currently loading packages generated by `grpc-tools`, you should instead generate your files using the `--generate_package_definitions` option in `grpc-tools`, then load the object exported by the generated file into `@grpc/grpc-js` using `grpc.loadPackageDefinition`.
- If you have a server and you are using `Server#bind` to bind ports, you will need to use `Server#bindAsync` instead.

## Some Notes on API Guarantees

The public API of this library follows semantic versioning, with some caveats:

- Some methods are prefixed with an underscore. These methods are internal and should not be considered part of the public API.
- The class `Call` is only exposed due to limitations of TypeScript. It should not be considered part of the public API.
- In general, any API that is exposed by this library but is not exposed by the `grpc` library is likely an error and should not be considered part of the public API.
- Some methods are prefixed with an underscore. These methods are internal and should not be considered part of the public API.
- The class `Call` is only exposed due to limitations of TypeScript. It should not be considered part of the public API.
- In general, any API that is exposed by this library but is not exposed by the `grpc` library is likely an error and should not be considered part of the public API.
6 changes: 4 additions & 2 deletions packages/grpc-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@grpc/grpc-js",
"version": "1.0.3",
"version": "1.0.4",
"description": "gRPC Library for Node - pure JS implementation",
"homepage": "https://grpc.io/",
"repository": "https://github.com/grpc/grpc-node/tree/master/packages/grpc-js",
Expand Down Expand Up @@ -33,6 +33,7 @@
"mocha-jenkins-reporter": "^0.4.1",
"ncp": "^2.0.0",
"pify": "^4.0.1",
"rimraf": "^3.0.2",
"ts-node": "^8.3.0",
"typescript": "^3.7.2"
},
Expand All @@ -43,7 +44,7 @@
],
"scripts": {
"build": "npm run compile",
"clean": "gts clean",
"clean": "node -e 'require(\"rimraf\")(\"./build\", () => {})'",
"compile": "tsc -p .",
"format": "clang-format -i -style=\"{Language: JavaScript, BasedOnStyle: Google, ColumnLimit: 80}\" src/*.ts test/*.ts",
"lint": "npm run check",
Expand All @@ -55,6 +56,7 @@
"posttest": "npm run check"
},
"dependencies": {
"google-auth-library": "^6.0.0",
"semver": "^6.2.0"
},
"files": [
Expand Down
70 changes: 70 additions & 0 deletions packages/grpc-js/src/call-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ export type CallMetadataGenerator = (
cb: (err: Error | null, metadata?: Metadata) => void
) => void;

// google-auth-library pre-v2.0.0 does not have getRequestHeaders
// but has getRequestMetadata, which is deprecated in v2.0.0
export interface OldOAuth2Client {
getRequestMetadata: (
url: string,
callback: (
err: Error | null,
headers?: {
[index: string]: string;
}
) => void
) => void;
}

export interface CurrentOAuth2Client {
getRequestHeaders: (url?: string) => Promise<{ [index: string]: string }>;
}

export type OAuth2Client = OldOAuth2Client | CurrentOAuth2Client;

function isCurrentOauth2Client(
client: OAuth2Client
): client is CurrentOAuth2Client {
return (
'getRequestHeaders' in client &&
typeof client.getRequestHeaders === 'function'
);
}

/**
* A class that represents a generic method of adding authentication-related
* metadata on a per-request basis.
Expand Down Expand Up @@ -65,6 +94,47 @@ export abstract class CallCredentials {
return new SingleCallCredentials(metadataGenerator);
}

/**
* Create a gRPC credential from a Google credential object.
* @param googleCredentials The authentication client to use.
* @return The resulting CallCredentials object.
*/
static createFromGoogleCredential(
googleCredentials: OAuth2Client
): CallCredentials {
return CallCredentials.createFromMetadataGenerator((options, callback) => {
let getHeaders: Promise<{ [index: string]: string }>;
if (isCurrentOauth2Client(googleCredentials)) {
getHeaders = googleCredentials.getRequestHeaders(options.service_url);
} else {
getHeaders = new Promise((resolve, reject) => {
googleCredentials.getRequestMetadata(
options.service_url,
(err, headers) => {
if (err) {
reject(err);
return;
}
resolve(headers);
}
);
});
}
getHeaders.then(
(headers) => {
const metadata = new Metadata();
for (const key of Object.keys(headers)) {
metadata.add(key, headers[key]);
}
callback(null, metadata);
},
(err) => {
callback(err);
}
);
});
}

static createEmpty(): CallCredentials {
return new EmptyCallCredentials();
}
Expand Down
11 changes: 11 additions & 0 deletions packages/grpc-js/src/channel-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ConnectionOptions, createSecureContext, PeerCertificate } from 'tls';

import { CallCredentials } from './call-credentials';
import { CIPHER_SUITES, getDefaultRootsData } from './tls-helpers';
import { GoogleAuth as GoogleAuthType } from 'google-auth-library';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function verifyIsBufferOrNull(obj: any, friendlyName: string): void {
Expand Down Expand Up @@ -278,3 +279,13 @@ class ComposedChannelCredentialsImpl extends ChannelCredentials {
}
}
}

export function createGoogleDefaultCredentials(): ChannelCredentials {
const GoogleAuth = require('google-auth-library')
.GoogleAuth as typeof GoogleAuthType;
const sslCreds = ChannelCredentials.createSsl();
const googleAuthCreds = CallCredentials.createFromGoogleCredential(
new GoogleAuth()
);
return sslCreds.compose(googleAuthCreds);
}
Loading

0 comments on commit 8ad1f82

Please sign in to comment.