Skip to content
This repository has been archived by the owner on Nov 22, 2024. It is now read-only.

Commit

Permalink
feat(grpc-engine): introduce package
Browse files Browse the repository at this point in the history
  • Loading branch information
CaerusKaru committed Dec 23, 2018
1 parent 9b83bbe commit 03357fd
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 1 deletion.
73 changes: 73 additions & 0 deletions modules/grpc-engine/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
load("//tools:defaults.bzl", "ng_module", "ng_package", "ng_test_library", "jasmine_node_test")

package(default_visibility = ["//visibility:public"])

#load("@build_bazel_rules_typescript//:defs.bzl", "ts_proto_library")

#proto_library(
# name = "engine_proto",
# srcs = ["grpc-engine.proto"],
#)
#
#ts_proto_library(
# name = "engine_ts_proto",
# deps = [":engine_proto"],
#)

filegroup(
name = "temp_proto",
srcs = ["src/grpc-engine.proto"]
)

ng_module(
name = "grpc-engine",
srcs = glob([
"*.ts",
"src/**/*.ts",
]),
module_name = "@nguniversal/grpc-engine",
deps = [
"//modules/common/engine",
"@ngudeps//grpc"
# ":engine_ts_proto"
],
)

ng_package(
name = "npm_package",
srcs = [
":package.json",
":grpc-engine.proto",
],
entry_point = "modules/grpc-engine/index.js",
readme_md = ":README.md",
tags = ["release"],
deps = [
":grpc-engine",
"@ngudeps//grpc",
],
)

ng_test_library(
name = "unit_test_lib",
srcs = glob([
"spec/**/*.spec.ts",
]),
deps = [
":grpc-engine",
"@ngudeps//grpc",
"@ngudeps//domino",
"@ngudeps//xhr2",
"@ngudeps//zone.js",
"@angular//packages/platform-browser",
"@angular//packages/platform-server",
],
)

jasmine_node_test(
name = "unit_test",
srcs = [
":unit_test_lib",
":grpc-engine.proto",
],
)
7 changes: 7 additions & 0 deletions modules/grpc-engine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Angular gRPC Engine

This is a gRPC Engine for running Angular applications on the server for server side rendering

## Usage

To be added
8 changes: 8 additions & 0 deletions modules/grpc-engine/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export * from './public_api';
28 changes: 28 additions & 0 deletions modules/grpc-engine/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@nguniversal/grpc-engine",
"version": "0.0.0-PLACEHOLDER",
"description": "gRPC engine for running server Angular applications",
"license": "MIT",
"keywords": [
"grpc",
"ssr",
"universal"
],
"peerDependencies": {
"@angular/common": "NG_VERSION",
"@angular/core": "NG_VERSION",
"@angular/platform-server": "NG_VERSION",
"grpc": "^1.11.3"
},
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
},
"repository": {
"type": "git",
"url": "https://github.com/angular/universal"
},
"bugs": {
"url": "https://github.com/angular/universal/issues"
},
"homepage": "https://github.com/angular/universal"
}
8 changes: 8 additions & 0 deletions modules/grpc-engine/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
export { startGRPCEngine } from './src/main';
43 changes: 43 additions & 0 deletions modules/grpc-engine/spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ServerModule } from '@angular/platform-server';
import { NgModule, Component } from '@angular/core';
import 'zone.js';

import { BrowserModule } from '@angular/platform-browser';
import { startGRPCEngine } from '@nguniversal/grpc-engine';
import {load, credentials} from 'grpc';

function createClient() {
const engineProto = load('../grpc-engine.proto').GRPCEngine;
const client = engineProto.SSR('localhost:9090', credentials.createInsecure());
return client;
}

export function makeTestingModule(template: string, component?: any): any {
@Component({
selector: 'root',
template: template
})
class MockComponent {}
@NgModule({
imports: [ServerModule, BrowserModule.withServerTransition({appId: 'mock'})],
declarations: [component || MockComponent],
bootstrap: [component || MockComponent]
})
class MockServerModule {}
return MockServerModule;
}

describe('test runner', () => {
it('should render a basic template', async (done) => {
const template = `some template: ${new Date()}`;
const appModule = makeTestingModule(template);
const server = await startGRPCEngine(appModule);
const client = createClient();

client.render({id: 1, document: '<root></root>'}, async(_err: any, response: any) => {
expect(response.html).toContain(template);
await server.close();
done();
});
});
});
24 changes: 24 additions & 0 deletions modules/grpc-engine/src/grpc-engine.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package grpcengine;
syntax = "proto3";


service SSR {
rpc render(RenderOptions) returns (RenderResponse) {}
}

message RenderOptions {
int32 id = 1;
string document = 2;
string documentFilename = 3;
}

message RenderResponse {
int32 id = 1;
string html = 2;
optional Error error = 3;
}

message Error {
string message = 1;
string stack = 2;
}
59 changes: 59 additions & 0 deletions modules/grpc-engine/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {
ɵCommonEngine as CommonEngine,
ɵRenderOptions as RenderOptions,
} from '@nguniversal/common/engine';
import {NgModuleFactory, Type} from '@angular/core';
import * as grpc from 'grpc';

export interface GRPCEngineServer {
close: () => void;
}

export interface GRPCEngineRenderOptions extends RenderOptions {
id: number;
}

export interface GRPCEngineResponse {
id: number;
html: string;
}

export function startGRPCEngine(
moduleOrFactory: Type<{}> | NgModuleFactory<{}>,
host = 'localhost',
port = 9090
): Promise<GRPCEngineServer> {
// needs to be a directory up so it lines up with deployment
const protoDescriptor = grpc.load('./grpc-engine.proto');
return new Promise((resolve, _reject) => {
const engine = new CommonEngine(moduleOrFactory);

const server = new grpc.Server();
server.addProtoService(protoDescriptor.GRPCEngine.service, {
render: async (call: any, callback: any) => {
const renderOptions = call.request as GRPCEngineRenderOptions;
try {
const html = await engine.render(renderOptions);
callback(null, {id: renderOptions.id, html});
} catch (error) {
callback(null, {id: renderOptions.id, error});
}
}
});
// TODO(Toxicable): how to take credentials as input?
server.bind(`${host}:${port}`, grpc.ServerCredentials.createInsecure());
server.start();

resolve({
close: () => new Promise((res, _rej) => server.tryShutdown(() => res()))
});
});
}

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
"zone.js": "^0.8.26"
},
"dependencies": {
"yarn": "^1.10.1"
"yarn": "^1.10.1",
"@types/ws": "^5.1.1",
"grpc": "^1.12.2",
"ws": "^5.1.1"
}
}

0 comments on commit 03357fd

Please sign in to comment.