-
Notifications
You must be signed in to change notification settings - Fork 1
/
hapi-engine.ts
180 lines (146 loc) · 4.96 KB
/
hapi-engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Thanks to bisubus <https://github.com/bisubus> for this
* Awaiting for https://github.com/angular/universal/pull/599 to be merged
*/
import { platformUniversalDynamic } from 'angular2-universal';
import { PrebootOptions } from 'preboot';
declare var Zone: any;
export interface IHapiEngineConfig {
asyncDestroy: boolean;
document?: string;
DOCUMENT?: string;
cancelHandler?: () => boolean;
CANCEL_HANDLER?: () => boolean;
req?: any;
REQ?: any;
res?: any;
RES?: any;
time?: boolean;
TIME?: boolean;
id?: string;
ID?: string;
ngModule?: any;
precompile?: boolean;
preboot?: PrebootOptions;
cancel?: boolean;
CANCEL?: boolean;
requestUrl?: string;
REQUEST_URL?: string;
originUrl?: string;
ORIGIN_URL?: string;
baseUrl?: string;
BASE_URL?: string;
cookie?: string;
COOKIE?: string;
}
export interface IHapiCompileOptions {
platform?: any,
providers?: any
};
export interface IHapiContext extends IHapiEngineConfig {
request?: any,
};
export type THapiEngineRenderer = (context: IHapiContext, runtimeOptions: any, callback: (err: any, renderedContent: any) => void) => void;
// @internal
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
export class HapiEngine {
private _defaultCompileOptions: IHapiCompileOptions = {
platform: (providers) => platformUniversalDynamic(providers),
providers: []
};
private _defaultConfig: IHapiEngineConfig = {
precompile: true,
time: false,
asyncDestroy: true,
id: s4(),
ngModule: null
};
compile = (template: string, compileOptions: IHapiCompileOptions & IHapiEngineConfig, next: (err: any, renderer: THapiEngineRenderer) => void): void => {
if (!next) {
throw new Error('Only async template compilation is supported');
}
const mergedCompileOptions: IHapiCompileOptions & IHapiEngineConfig = Object.assign(
{},
this._defaultCompileOptions,
this._defaultConfig,
compileOptions
);
const mergedConfig = Object.assign({}, mergedCompileOptions);
delete mergedConfig.platform;
delete mergedConfig.providers;
const { platform, providers, ngModule } = mergedCompileOptions;
const platformRef: any = platform(providers);
if (ngModule) {
platformRef.cacheModuleFactory(ngModule);
}
const rendererWrapper: THapiEngineRenderer = (context, runtimeOptions, callback) => {
// engine config specified in reply.view context
const mergedContext = Object.assign({}, mergedConfig, context);
// runtime variables passed from compile to renderer
const mergedRuntimeOptions = Object.assign({}, runtimeOptions, { platformRef, template });
this._renderer(mergedContext, mergedRuntimeOptions, callback);
};
next(null, rendererWrapper);
}
private _renderer: THapiEngineRenderer = (context, runtimeOptions, callback) => {
const { platformRef, template } = runtimeOptions;
if (!context.ngModule) {
throw new Error('Please provide your main module as ngModule as the context in reply.view("index", { ngModule: MainModule }) or in Vision configuration as { compileConfig: { ngModule: MainModule } }');
}
if (!context.request || !(context.req && context.res)) {
throw new Error('Please provide request as the context in reply.view("index", { request })');
}
const rendererConfig = Object.assign({}, context);
delete rendererConfig.request;
const { request } = context;
const req = rendererConfig.req || request.raw.req;
const res = rendererConfig.res || request.raw.res;
const requestUrl = rendererConfig.requestUrl || req.url;
const originUrl = rendererConfig.originUrl || req.headers.host;
const baseUrl = rendererConfig.baseUrl || '/';
const cookie = rendererConfig.cookie || req.headers.cookie;
const DOCUMENT: string = template;
// TODO(gdi2290): breaking change for context globals
// document = parseDocument(document);
const document = DOCUMENT;
const cancelHandler = () => Zone.current.get('cancel');
const config: IHapiEngineConfig = Object.assign({}, rendererConfig, {
cancel: false,
req,
res,
requestUrl,
originUrl,
baseUrl,
cookie,
DOCUMENT,
document,
cancelHandler
});
req.on('close', () => config.cancel = true);
const zone = Zone.current.fork({
name: 'UNIVERSAL request',
properties: config
});
// convert to string
zone.run(() => (config.precompile ?
platformRef.serializeModule(config.ngModule, config) :
platformRef.serializeModuleFactory(config.ngModule, config)
)
.then(html => {
if (typeof html !== 'string' || config.cancel) {
callback(null, DOCUMENT);
} else {
callback(null, html);
}
})
.catch(e => {
console.log(e.stack);
// if server fail then return client html
callback(null, DOCUMENT);
}));
}
}
const hapiEngine = new HapiEngine();
export default hapiEngine;