This repository has been archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(grpc-engine): introduce package
- Loading branch information
Fabian Wiles
committed
May 20, 2018
1 parent
5ca79bb
commit a708168
Showing
10 changed files
with
739 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())) | ||
}); | ||
}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.