1 | /**
|
2 | * @license
|
3 | * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
4 | * This code may only be used under the BSD style license found at
|
5 | * http://polymer.github.io/LICENSE.txt
|
6 | * The complete set of authors may be found at
|
7 | * http://polymer.github.io/AUTHORS.txt
|
8 | * The complete set of contributors may be found at
|
9 | * http://polymer.github.io/CONTRIBUTORS.txt
|
10 | * Code distributed by Google as part of the polymer project is also
|
11 | * subject to an additional IP rights grant found at
|
12 | * http://polymer.github.io/PATENTS.txt
|
13 | */
|
14 | /// <reference types="node" />
|
15 | import * as express from 'express';
|
16 | import * as http from 'spdy';
|
17 | import * as url from 'url';
|
18 | export interface ServerOptions {
|
19 | /** The root directory to serve **/
|
20 | root?: string;
|
21 | /**
|
22 | * The path on disk of the entry point HTML file that will be served for
|
23 | * app-shell style projects. Must be contained by `root`. Defaults to
|
24 | * `index.html`.
|
25 | */
|
26 | entrypoint?: string;
|
27 | /** Whether or not to compile JavaScript **/
|
28 | compile?: 'always' | 'never' | 'auto';
|
29 | /** Resolution algorithm to use for rewriting module specifiers */
|
30 | moduleResolution?: 'none' | 'node';
|
31 | /** The port to serve from */
|
32 | port?: number;
|
33 | /** The hostname to serve from */
|
34 | hostname?: string;
|
35 | /** Headers to send with every response */
|
36 | headers?: {
|
37 | [name: string]: string;
|
38 | };
|
39 | /** Whether to open the browser when run **/
|
40 | open?: boolean;
|
41 | /** The browser(s) to open when run with open argument **/
|
42 | browser?: string[];
|
43 | /** The URL path to open in each browser **/
|
44 | openPath?: string;
|
45 | /** The component directory to use **/
|
46 | componentDir?: string;
|
47 | /** The component url to serve **/
|
48 | componentUrl?: string;
|
49 | /** The package name to use for the root directory **/
|
50 | packageName?: string;
|
51 | /**
|
52 | * Sets npm mode: component directory is 'node_modules' and the package name
|
53 | * is read from package.json.
|
54 | */
|
55 | npm?: boolean;
|
56 | /** The HTTP protocol to use */
|
57 | protocol?: string;
|
58 | /** Path to TLS service key for HTTPS */
|
59 | keyPath?: string;
|
60 | /** Path to TLS certificate for HTTPS */
|
61 | certPath?: string;
|
62 | /** Path to H2 push-manifest file */
|
63 | pushManifestPath?: string;
|
64 | /** Proxy to redirect for all matching `path` to `target` */
|
65 | proxy?: {
|
66 | path: string;
|
67 | target: string;
|
68 | };
|
69 | /** Sets the value of the Access-Control-Allow-Origin header */
|
70 | allowOrigin?: string;
|
71 | /**
|
72 | * An optional list of routes & route handlers to attach to the polyserve
|
73 | * app, to be handled before all others
|
74 | */
|
75 | additionalRoutes?: Map<string, express.RequestHandler>;
|
76 | }
|
77 | export declare type ExpressAppMapper = (app: express.Express, options: ServerOptions) => Promise<express.Express>;
|
78 | /**
|
79 | * @param {ServerOptions} options used to configure the generated polyserve app
|
80 | * and server.
|
81 | * @param {ExpressAppMapper} appMapper optional mapper function which is called
|
82 | * with the generated polyserve app and the options used to generate
|
83 | * it and returns an optional substitution Express app. This is usually one
|
84 | * that mounts the original app, to add routes or middleware in advance of
|
85 | * polyserve's catch-all routes.
|
86 | * @return {Promise} A Promise that completes when the server has started.
|
87 | * @deprecated Please use `startServers` instead. This function will be removed
|
88 | * in a future release.
|
89 | */
|
90 | export declare function startServer(options: ServerOptions, appMapper?: ExpressAppMapper): Promise<http.Server>;
|
91 | export declare type ServerInfo = MainlineServer | VariantServer | ControlServer;
|
92 | export interface PolyserveServer {
|
93 | kind: 'control' | 'mainline' | 'variant';
|
94 | server: http.Server;
|
95 | app: express.Application;
|
96 | options: ServerOptions;
|
97 | }
|
98 | /**
|
99 | * The `default` or `primary` server. If only one ServerInfo is returned from
|
100 | * startServers it must be a MainlineServer. This is the server that's running
|
101 | * with the default configuration and not running a variant configuration.
|
102 | */
|
103 | export interface MainlineServer extends PolyserveServer {
|
104 | kind: 'mainline';
|
105 | }
|
106 | /**
|
107 | * These are servers which are running some named variant configuration. For
|
108 | * multiple variant dependency directories are detected/configured, there will
|
109 | * be one MainlineServer that serves out the default dependency directory, and
|
110 | * one VariantServer for each other dependency directory.
|
111 | */
|
112 | export interface VariantServer extends PolyserveServer {
|
113 | kind: 'variant';
|
114 | variantName: string;
|
115 | dependencyDir: string;
|
116 | }
|
117 | /**
|
118 | * If more than one server is started by startServers, the main port will serve
|
119 | * out a control server. This server serves out an HTML interface that
|
120 | * describes the other servers which have been started, and provides convenience
|
121 | * links to them.
|
122 | */
|
123 | export interface ControlServer extends PolyserveServer {
|
124 | kind: 'control';
|
125 | }
|
126 | export interface MultipleServersInfo {
|
127 | kind: 'MultipleServers';
|
128 | mainline: MainlineServer;
|
129 | variants: VariantServer[];
|
130 | control: ControlServer;
|
131 | servers: PolyserveServer[];
|
132 | }
|
133 | export declare type StartServerResult = MainlineServer | MultipleServersInfo;
|
134 | /**
|
135 | * Starts one or more web servers, based on the given options and
|
136 | * variant bower_components directories that are found in the root dir.
|
137 | */
|
138 | export declare function startServers(options: ServerOptions, appMapper?: ExpressAppMapper): Promise<StartServerResult>;
|
139 | export declare function startControlServer(options: ServerOptions, mainlineInfo: MainlineServer, variantInfos: VariantServer[]): Promise<ControlServer>;
|
140 | export declare function getApp(options: ServerOptions): express.Express;
|
141 | /**
|
142 | * Gets the URLs for the main and component pages
|
143 | * @param {ServerOptions} options
|
144 | * @returns {{serverUrl: {protocol: string, hostname: string, port: string},
|
145 | * componentUrl: url.Url}}
|
146 | */
|
147 | export declare function getServerUrls(options: ServerOptions, server: http.Server): {
|
148 | serverUrl: url.Url;
|
149 | componentUrl: url.Url;
|
150 | };
|
151 | /**
|
152 | * Starts an HTTP(S) server serving the given app.
|
153 | */
|
154 | export declare function startWithApp(options: ServerOptions, app: express.Application): Promise<http.Server>;
|
155 | export declare function assertNotString<T>(value: string | T): T;
|