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

Commit

Permalink
mirror socket-engine from angular/universal/issues/1000.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbortt committed Aug 6, 2018
1 parent fe5b14a commit 2b8f92c
Show file tree
Hide file tree
Showing 30 changed files with 2,640 additions and 2,171 deletions.
4,163 changes: 2,072 additions & 2,091 deletions core-server/authorization-server/src/main/webapp/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@angular/platform-browser-dynamic": "^6.0.3",
"@angular/platform-server": "^6.0.3",
"@angular/router": "^6.0.3",
"@nguniversal/express-engine": "^6.0.0",
"@nguniversal/module-map-ngfactory-loader": "^6.0.0",
"core-js": "^2.5.4",
"rxjs": "^6.0.0",
Expand Down
42 changes: 0 additions & 42 deletions core-server/authorization-server/src/main/webapp/server.old.js

This file was deleted.

42 changes: 6 additions & 36 deletions core-server/authorization-server/src/main/webapp/server.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,9 @@
import 'zone.js/dist/zone-node';
import 'reflect-metadata';

import * as express from 'express';
import {join} from 'path';
import {ngExpressEngine} from '@nguniversal/express-engine';
import {provideModuleMap} from '@nguniversal/module-map-ngfactory-loader';

const app = express();

const PORT = process.env.PORT || 4000;
const DIST_FOLDER = join(process.cwd(), 'dist');
// TODO: Install the latest stable version as soon as the pull-request is merged
// --> https://github.com/angular/universal/pull/999
import * as socketEngine from './vendor/socket-engine';
// const socketEngine = require('@nguniversal/socket-engine');

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {AppServerModuleNgFactory, LAZY_MODULE_MAP} = require('./dist/server/main');

app.engine('html', ngExpressEngine({
bootstrap: AppServerModuleNgFactory,
providers: [
provideModuleMap(LAZY_MODULE_MAP)
]
}));

app.set('view engine', 'html');
app.set('views', join(DIST_FOLDER, 'browser'));

// TODO: proxy backend requests
app.get('/api/*', (req, res) => {
res.status(404).send('data requests are not supported');
});

app.get('*.*', express.static(join(DIST_FOLDER, 'browser')));

app.get('*', (req, res) => {
res.render('index', {req});
});

app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
socketEngine.startSocketEngine(AppServerModuleNgFactory);
11 changes: 11 additions & 0 deletions core-server/authorization-server/src/main/webapp/vendor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# angular / universal / socket-engine

This vendor packages mirror the repository at [commit ea6f7b13b70c40b55ce72bd549e2dfb56ca146db](https://github.com/angular/universal/commit/ea6f7b13b70c40b55ce72bd549e2dfb56ca146db). Local paths (imports) were adapted accordingly.

## Bugs and replacement

The engine is still experimental and contains some bugs. The [major bug]([https://github.com/angular/universal/pull/1003]) depends on (bazel)[https://github.com/bazelbuild/rules_typescript/issues/211]. Once the PR is merged you could install those libraries via NPM.

## Future installation

Once the module is merged you can install it as following: `npm install @nguniversal/socket-engine @nguniversal/common --save`
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Angular Universal Common Module

This is the common Angular Universal module that is common across server-side rendering app
irrespective of the rendering engine.

The package can be installed using:

`npm install @nguniversal/common --save`

## TransferHttpCacheModule

Moved to [/docs/transfer-http.md](https://github.com/angular/universal/tree/master/docs/transfer-http.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package(default_visibility = ["//visibility:public"])

load("//tools:defaults.bzl", "ng_module")

ng_module(
name = "engine",
srcs = glob([
"src/*.ts",
"*.ts",
]),
module_name = "@nguniversal/common/engine",
)
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';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @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 { FileLoader as ɵFileLoader, CommonEngine as ɵCommonEngine,
RenderOptions as ɵRenderOptions } from './src/index';
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 './private_api';
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @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 {ResourceLoader} from '@angular/compiler';
import {Compiler, Type, NgModuleFactory, CompilerFactory, StaticProvider} from '@angular/core';
import {INITIAL_CONFIG, renderModuleFactory, platformDynamicServer} from '@angular/platform-server';
import * as fs from 'fs';

import {FileLoader} from './file-loader';
import {RenderOptions} from './interfaces';

/**
* A common rendering engine utility. This abstracts the logic
* for handling the platformServer compiler, the module cache, and
* the document loader
*/
export class CommonEngine {

/** Return an instance of the platformServer compiler */
getCompiler(): Compiler {
const compilerFactory: CompilerFactory = platformDynamicServer().injector.get(CompilerFactory);
return compilerFactory.createCompiler([
{providers: [{provide: ResourceLoader, useClass: FileLoader, deps: []}]}
]);
}

private factoryCacheMap = new Map<Type<{}>, NgModuleFactory<{}>>();
private templateCache: {[key: string]: string} = {};

constructor(private moduleOrFactory: Type<{}> | NgModuleFactory<{}>,
private providers: StaticProvider[] = []) {}

/**
* Render an HTML document for a specific URL with specified
* render options
*/
async render(opts: RenderOptions): Promise<string> {
// if opts.document dosen't exist then opts.documentFilePath must
const doc = opts.document || await this.getDocument(opts!.documentFilePath as string);
const extraProviders = [
...(opts.providers || []),
...(this.providers || []),
{
provide: INITIAL_CONFIG,
useValue: {
document: doc,
url: opts.url
}
}
];

const factory = await this.getFactory();
return renderModuleFactory(factory, {extraProviders});
}

/** Return the factory for a given engine instance */
getFactory(): Promise<NgModuleFactory<{}>> {
// If module has been compiled AoT
const moduleOrFactory = this.moduleOrFactory;
if (moduleOrFactory instanceof NgModuleFactory) {
return Promise.resolve(moduleOrFactory);
} else {
// we're in JIT mode
let moduleFactory = this.factoryCacheMap.get(moduleOrFactory);

// If module factory is cached
if (moduleFactory) {
return Promise.resolve(moduleFactory);
}

// Compile the module and cache it
return this.getCompiler().compileModuleAsync(moduleOrFactory)
.then((factory) => {
this.factoryCacheMap.set(moduleOrFactory, factory);
return factory;
});
}
}

/** Retrieve the document from the cache or the filesystem */
private getDocument(filePath: string): Promise<string> {
const doc = this.templateCache[filePath] = this.templateCache[filePath] ||
fs.readFileSync(filePath).toString();

// As promise so we can change the API later without breaking
return Promise.resolve(doc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @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 * as fs from 'fs';
import { ResourceLoader } from '@angular/compiler';

/** ResourceLoader implementation for loading files */
export class FileLoader implements ResourceLoader {
get(url: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(url, (err: NodeJS.ErrnoException, buffer: Buffer) => {
if (err) {
return reject(err);
}

resolve(buffer.toString());
});
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @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 './interfaces';
export * from './file-loader';
export * from './engine';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @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 {NgModuleFactory, StaticProvider, Type} from '@angular/core';

/** These are the allowed options for the render */
export interface RenderOptions {
bootstrap: Type<{}> | NgModuleFactory<{}>;
providers?: StaticProvider[];
url?: string;
document?: string;
documentFilePath?: string;
}
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';
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 { TransferHttpCacheInterceptor as ɵTransferHttpCacheInterceptor } from './src/transfer_http';
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @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 { TransferHttpCacheModule } from './src/transfer_http';
export { StateTransferInitializerModule } from './src/state-transfer-initializer/module';
export * from './private_api';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# State Transfer Initializer
Delays the app bootstrap process to ensure that the DOM content is loaded before state transfer initilization

Simply import the module into your project and you will no longer need to wrap your component bootstrap function in an `DOMContentLoaded` callback

```
import { StateTransferInitializerModule } from '@nguniversal/common'
@NgModule({
imports: [ StateTransferInitializerModule ]
})
export class AppModule{}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @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 { NgModule, APP_INITIALIZER } from '@angular/core';
import { DOCUMENT } from '@angular/common';

export function domContentLoadedFactory(doc: Document) {
return () => new Promise ((resolve, _reject) => {
const contentLoaded = () => {
doc.removeEventListener('DOMContentLoaded', contentLoaded);
resolve();
};
doc.addEventListener('DOMContentLoaded', contentLoaded);
});
}


@NgModule({
providers: [
{provide: APP_INITIALIZER, multi: true, useFactory: domContentLoadedFactory, deps: [DOCUMENT]},
]
})
export class StateTransferInitializerModule {}
Loading

0 comments on commit 2b8f92c

Please sign in to comment.