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
Fabian Wiles committed May 20, 2018
1 parent 5ca79bb commit a708168
Show file tree
Hide file tree
Showing 10 changed files with 739 additions and 796 deletions.
47 changes: 47 additions & 0 deletions modules/grpc-engine/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
load("//tools:defaults.bzl", "ts_library", "ng_module", "ng_package")

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

load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test")

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

],
)

ng_package(
name = "npm_package",
srcs = [
":package.json",
],
entry_point = "modules/socket-engine/index.js",
readme_md = ":README.md",
deps = [
":socket-engine",
"//modules/common",
],
)

ts_library(
name = "unit_test_lib",
testonly = True,
srcs = glob([
"spec/**/*.spec.ts",
]),
deps = [
":socket-engine",
],
)

jasmine_node_test(
name = "unit_test",
srcs = [":unit_test_lib"],
)
Empty file added modules/grpc-engine/README.md
Empty file.
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';
16 changes: 16 additions & 0 deletions modules/grpc-engine/interface.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
syntax = "proto3";


service GRPCEngine {
rpc render(RenderOptions) returns (string) {}
}

message RenderOptions {
string document
string documentFilename
}

message RenderOptions {
string document
string documentFilename
}
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 Apps",
"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';
41 changes: 41 additions & 0 deletions modules/grpc-engine/spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

import { ServerModule } from '@angular/platform-server';
import { NgModule, Component } from '@angular/core';
import 'zone.js';

import { BrowserModule } from '@angular/platform-browser';
import { startSocketEngine, SocketEngineResponse } from '@nguniversal/socket-engine';
import * as WebSocket from 'ws';

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 startSocketEngine(appModule);

const ws = new WebSocket('ws://localhost:9090');

ws.on('open', () => ws.send(JSON.stringify({id: 1, url: '/path', document: '<root></root>'})));
ws.on('message', (res: SocketEngineResponse) => {
expect(res.id).toEqual(1);
expect(res.html).toEqual(template);
server.close();
done();
});
});
});
53 changes: 53 additions & 0 deletions modules/grpc-engine/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @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';
import { NgModuleFactory, Type } from '@angular/core';
import * as grpc from 'grpc';

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

export interface GRPCEngineRenderOptions extends RenderOptions {
}

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

export function startGRPCEngine(
moduleOrFactory: Type<{}> | NgModuleFactory<{}>,
host = 'localhost',
port = 9090
): Promise<GRPCEngineServer> {
// TODO: how will this work with deployment?
const protoDescriptor = grpc.load('../interface.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 options = call.request as GRPCEngineRenderOptions;
const html = await engine.render(renderOptions);
// TODO: how to send errors?
callback(null, html);
}
});
// TODO: 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: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@
"typescript": "~2.7.2",
"uglify-js": "^2.8.14",
"zone.js": "^0.8.12"
},
"dependencies": {
"@types/ws": "^5.1.1",
"grpc": "^1.11.3",
"ws": "^5.1.1"
}
}
Loading

0 comments on commit a708168

Please sign in to comment.