Skip to content

Commit

Permalink
guide changed and new test cases added
Browse files Browse the repository at this point in the history
  • Loading branch information
Emre-Kul committed Sep 17, 2019
1 parent a57e8ce commit c2d9452
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 36 deletions.
38 changes: 30 additions & 8 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Chunked fragments are sent after the initial chunk. Like the example above `<div

##### ShouldWait Fragments

ShoudWait fragments are the fragments that should be waited and injected into initial chunk. Let's assume a page with 2 fragments. One is shouldWait and the other is chunked.
ShouldWait fragments are the fragments that should be waited and injected into initial chunk. Let's assume a page with 2 fragments. One is shouldWait and the other is chunked.
```html
0xf3
<html>
Expand Down Expand Up @@ -156,7 +156,9 @@ An example of starting storefront with simple configuration.
```js
const Storefront = require('@puzzle-js/core');
const storefront = new Storefront({
port: 4444,
serverOptions: {
port: 4444
},
gateways: [{
name: 'Gateway',
url: 'https://127.0.0.1:4448/',
Expand All @@ -178,10 +180,9 @@ You can provide configuration as object or you can use [Configurator](#configura

| Property | Type | Required | Description |
|-|-|-|-|
| port | number | True | Port to listen |
| serverOptions | ServerOptions | True | server options |
| gateways | gateway | True | Gateway Configuration |
| pages | page | True | Page Configuration |
| spdy | spdy | False | http2 and spdy configuration |
| dependencies | dependency[] | True | Shared dependencies (React, angular, etc.) or [] |

#### Gateway Definition
Expand Down Expand Up @@ -365,7 +366,9 @@ configurator.config({
api: [],
name: 'Gateway',
url: 'http://gateway.com/',
port: 32,
serverOptions: {
port: 32
},
fragments: [
{
versions: {
Expand Down Expand Up @@ -431,7 +434,9 @@ new Gateway({
fragmentsFolder: '',
name: 'Gateway',
url: 'http://gateway.com',
port: 32,
serverOptions: {
port: 32
},
fragments: []
})
```
Expand Down Expand Up @@ -465,7 +470,9 @@ configurator.config({
],
name: 'Gateway',
url: 'http://gateway.com/',
port: 32,
serverOptions: {
port: 32
},
fragments: []
});

Expand All @@ -475,7 +482,7 @@ With this feature, you can easily manage your configuration on a separate file.

| Name | Description |
| - | - |
| Middleware | Used for adding express middlewares |
| Middleware | Used for adding express middleware |
| Handler | Used for custom handlers, read [Handler](#handler) |
| Custom | Can be used for anything |

Expand Down Expand Up @@ -549,3 +556,18 @@ PuzzleJs has some inner configurations you can't change using any Storefront or
| DEBUG_INFORMATION | false | It enables debug information globally |
| NO_COMPRESS_QUERY_NAME | '__noCompress' | It disables compression for that request |

## Configuration Models

### Server Options
** HTTP/2 Does not supported yet
| Property | Type | Required | Description |
|-|-|-|-|
| port | number | True | Port for server to listen |
| http2 | boolean | False | HTTP2 option (Not Supported Yet)|
| https | serverHttpsOptions | False | HTTPS options |

#### Server Https Options
| Property | Type | Required | Description |
|-|-|-|-|
| cert | string | True | Certificate for HTTPS |
| key | string | True | Key for HTTPS |
8 changes: 6 additions & 2 deletions docs/quick.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ const gateway = new PuzzleJs.Gateway({
}
}
],
port: 4444,
serverOptions: {
port: 4444
},
url: 'http://localhost:4444',
fragmentsFolder: path.join(__dirname, "./src/fragments")
});
Expand Down Expand Up @@ -179,7 +181,9 @@ const path = require('path');
const fs = require('fs');

const storefront = new PuzzleJs.Storefront({
port: 4445,
serverOptions: {
port: 4445
},
gateways: [
{
name: 'Browsing',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@puzzle-js/core",
"version": "1.0.0-beta.21",
"version": "1.1.0-beta.1",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"logo": "https://image.ibb.co/jM29on/puzzlelogo.png",
Expand Down
21 changes: 5 additions & 16 deletions src/network/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import https from "https";
import http from "http";
import http2 from "http2";

import { IServerOptions } from "./server-options";
import { ExpressHandler } from "./express-handler";

// type Handler = (req: http.IncomingMessage | http2.Http2ServerRequest, res: http.OutgoingMessage | http2.Http2ServerResponse) => void;

export class Server {
instance: http.Server | https.Server | http2.Http2SecureServer | http2.Http2Server;
instance: http.Server | https.Server;
handler;

private options: IServerOptions;
Expand Down Expand Up @@ -38,25 +37,15 @@ export class Server {
}

private create() {
if(this.options.http2) {
console.warn(`Falling back to http1.1 as express doesn't support it yet`);
}
if (this.options.https) {
const httpsSettings = {
key: this.options.https.key,
cert: this.options.https.cert,
};
if (this.options.http2) {
/*
return http2.createSecureServer({
...httpsSettings,
allowHTTP1: typeof this.options.https.allowHTTP1 === "boolean" ? this.options.https.allowHTTP1 : true
}, this.handler.getApp());
*/
console.warn(`Falling back to http1.1 as express doesn't support it yet`);
return https.createServer(httpsSettings, this.handler.getApp());
} else {
return https.createServer(httpsSettings, this.handler.getApp());
}
} else if (this.options.http2) {
return http2.createServer(this.handler.getApp());
return https.createServer(httpsSettings, this.handler.getApp());
} else {
return http.createServer(this.handler.getApp());
}
Expand Down
42 changes: 33 additions & 9 deletions tests/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from "path";
import {EVENTS, HTTP_METHODS, TRANSFER_PROTOCOLS} from "../src/enums";
import * as fs from "fs";
import {pubsub} from "../src/util";
import {TLS_CERT_SET, TLS_PASS} from "./core.settings";
import {TLS_CERT_SET, TLS_KEY, TLS_CERT} from "./core.settings";
import faker from "faker";
import {NO_COMPRESS_QUERY_NAME} from "../src/config";
import {ICustomHeader} from "../src/types";
Expand Down Expand Up @@ -171,27 +171,51 @@ describe('Server', () => {

});

/*
xit('should use h2 protocol when h2 configuration provided', (done) => {
const http2Server = new Server({

it('should use h1 protocol when h2 configuration provided', (done) => {
const httpServer = new Server({
port: TEST_CONFIG.TEST_PORT,
http2: true
});
const response = faker.random.words();

http2Server.handler.addRoute('/', HTTP_METHODS.GET, (req, res) => {
httpServer.handler.addRoute('/', HTTP_METHODS.GET, (req, res) => {
res.end(response);
});

httpServer.listen(() => {
request(TEST_CONFIG.TEST_URL).get('/').expect(200).end((err, res: Response) => {
httpServer.close(done);
expect((res as any).res.httpVersionMajor).to.eq(1);
expect(res.text).to.eq(response);
});
});
});

it('should use secure h1 protocol when h2 secure configuration provided', (done) => {
const httpsServer = new Server({
port: TEST_CONFIG.TEST_PORT,
http2: true,
https: {
cert: TLS_CERT,
key: TLS_KEY
}
} as any);
const response = faker.random.words();

httpsServer.handler.addRoute('/', HTTP_METHODS.GET, (req, res) => {
res.end(response);
});

http2Server.listen(() => {
httpsServer.listen(() => {
request(TEST_CONFIG.TEST_URL.replace('http', 'https')).get('/').expect(200).end((err, res: Response) => {
http2Server.close();
httpsServer.close(done);
expect((res as any).res.httpVersionMajor).to.eq(1);
expect(res.text).to.eq(response);
done(err);
});
});
});
*/


it('should use https when key cert provided', (done) => {
const server: Server = new Server(SERVER_OPTIONS);
Expand Down

0 comments on commit c2d9452

Please sign in to comment.