Skip to content

Commit 0cbf3c3

Browse files
author
Robert Jackson
committed
General cleanup; ES<latest>ification.
There are no intentional semantic changes here, just cleanup of syntax while reviewing.
1 parent 96eabc8 commit 0cbf3c3

8 files changed

Lines changed: 90 additions & 105 deletions

File tree

.eslintrc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
},
77
'extends': ['eslint:recommended', 'plugin:node/recommended'],
88
'parserOptions': {
9-
'ecmaVersion': 6
9+
'ecmaVersion': 2017
1010
},
1111
'rules': {
1212
'no-console': ['error', { 'allow': ['warn', 'error']}],
@@ -19,4 +19,4 @@ module.exports = {
1919
'always'
2020
]
2121
}
22-
};
22+
};

src/ember-app.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ class EmberApp {
7171
* @param {Object} [sandboxGlobals={}] any additional variables to expose in the sandbox or override existing in the sandbox
7272
*/
7373
buildSandbox(distPath, sandboxClass, sandboxGlobals) {
74+
const { config, appName } = this;
75+
7476
let sandboxRequire = this.buildWhitelistedRequire(this.moduleWhitelist, distPath);
75-
const config = this.config;
76-
const appName = this.appName;
77+
7778
function fastbootConfig(key) {
7879
if (!key) {
7980
// default to app key
@@ -93,6 +94,7 @@ class EmberApp {
9394
FastBoot: {
9495
require: sandboxRequire,
9596
config: fastbootConfig,
97+
9698
get distPath() {
9799
return distPath;
98100
}
@@ -422,12 +424,12 @@ class EmberApp {
422424
}
423425

424426
debug("reading array of app file paths from manifest");
425-
var appFiles = manifest.appFiles.map(function(appFile) {
427+
let appFiles = manifest.appFiles.map(function(appFile) {
426428
return path.join(distPath, appFile);
427429
});
428430

429431
debug("reading array of vendor file paths from manifest");
430-
var vendorFiles = manifest.vendorFiles.map(function(vendorFile) {
432+
let vendorFiles = manifest.vendorFiles.map(function(vendorFile) {
431433
return path.join(distPath, vendorFile);
432434
});
433435

src/fastboot-headers.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class FastBootHeaders {
66
headers = headers || {};
77
this.headers = {};
88

9-
for (var header in headers) {
9+
for (let header in headers) {
1010
let value = headers[header];
1111

1212
// Express gives us either a string
@@ -36,11 +36,11 @@ class FastBootHeaders {
3636
}
3737

3838
entries() {
39-
var entries = [];
39+
let entries = [];
4040

41-
for(var key in this.headers) {
42-
var values = this.headers[key];
43-
for(var index = 0; index < values.length; ++index ) {
41+
for(let key in this.headers) {
42+
let values = this.headers[key];
43+
for(let index = 0; index < values.length; ++index ) {
4444
entries.push([key, values[index]]);
4545
}
4646
}
@@ -61,11 +61,11 @@ class FastBootHeaders {
6161
}
6262

6363
keys() {
64-
var entries = [];
64+
let entries = [];
6565

66-
for(var key in this.headers) {
67-
var values = this.headers[key];
68-
for(var index = 0; index < values.length; ++index ) {
66+
for(let key in this.headers) {
67+
let values = this.headers[key];
68+
for(let index = 0; index < values.length; ++index ) {
6969
entries.push(key);
7070
}
7171
}
@@ -79,11 +79,11 @@ class FastBootHeaders {
7979
}
8080

8181
values() {
82-
var entries = [];
82+
let entries = [];
8383

84-
for(var key in this.headers) {
85-
var values = this.headers[key];
86-
for(var index = 0; index < values.length; ++index ) {
84+
for(let key in this.headers) {
85+
let values = this.headers[key];
86+
for(let index = 0; index < values.length; ++index ) {
8787
entries.push(values[index]);
8888
}
8989
}

src/fastboot-info.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const RSVP = require('rsvp');
43
const FastBootRequest = require('./fastboot-request');
54
const FastBootResponse = require('./fastboot-response');
65

@@ -15,11 +14,11 @@ const FastBootResponse = require('./fastboot-response');
1514
* @param {Array} [options.hostWhitelist] expected hosts in your application
1615
* @param {Object} [options.metaData] per request meta data
1716
*/
18-
class FastBootInfo {
17+
module.exports = class FastBootInfo {
1918
constructor(request, response, options) {
20-
this.deferredPromise = RSVP.resolve();
21-
let hostWhitelist = options.hostWhitelist;
22-
let metadata = options.metadata;
19+
this.deferredPromise = Promise.resolve();
20+
let { hostWhitelist, metadata } = options;
21+
2322
if (request) {
2423
this.request = new FastBootRequest(request, hostWhitelist);
2524
}
@@ -29,9 +28,7 @@ class FastBootInfo {
2928
}
3029

3130
deferRendering(promise) {
32-
this.deferredPromise = this.deferredPromise.then(function() {
33-
return promise;
34-
});
31+
this.deferredPromise = Promise.all(this.deferredPromise, promise);
3532
}
3633

3734
/*
@@ -43,7 +40,4 @@ class FastBootInfo {
4340
instance.register('info:-fastboot', this, { instantiate: false });
4441
instance.inject('service:fastboot', '_fastbootInfo', 'info:-fastboot');
4542
}
46-
}
47-
48-
49-
module.exports = FastBootInfo;
43+
};

src/index.js

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ class FastBoot {
4141
* @param {Sandbox} [options.sandbox=VMSandbox] the sandbox to use
4242
* @param {Object} [options.sandboxGlobals={}] any additional sandbox variables that an app server wants to override and/or add in the sandbox
4343
*/
44-
constructor(options) {
45-
options = options || {};
44+
constructor(options = {}) {
45+
let { distPath, sandbox, sandboxGlobals } = options;
4646

47-
this.distPath = options.distPath;
48-
this.sandbox = options.sandbox || require('./vm-sandbox');
49-
this.sandboxGlobals = options.sandboxGlobals || {};
50-
this.resilient = !!options.resilient || false;
47+
this.resilient = 'resilient' in options ? Boolean(options.resilient) : false;
48+
49+
this.distPath = distPath;
50+
this.sandbox = sandbox || require('./vm-sandbox');
51+
this.sandboxGlobals = sandboxGlobals || {};
5152

5253
this._buildEmberApp(this.distPath, this.sandbox, this.sandboxGlobals);
5354
}
@@ -67,39 +68,27 @@ class FastBoot {
6768
* @param {Integer} [options.destroyAppInstanceInMs] whether to destroy the instance in the given number of ms. This is a failure mechanism to not wedge the Node process (See: https://github.com/ember-fastboot/fastboot/issues/90)
6869
* @returns {Promise<Result>} result
6970
*/
70-
visit(path, options) {
71-
options = options || {};
71+
async visit(path, options = {}) {
72+
let resilient = 'resilient' in options ? options.resilient : this.resilient;
7273

73-
let resilient = options.resilient;
74+
let result = await this._app.visit(path, options);
7475

75-
if (resilient === undefined) {
76-
resilient = this.resilient;
76+
if (!resilient && result.error) {
77+
throw result.error;
78+
} else {
79+
return result;
7780
}
78-
79-
return this._app.visit(path, options)
80-
.then(result => {
81-
if (!resilient && result.error) {
82-
throw result.error;
83-
} else {
84-
return result;
85-
}
86-
});
8781
}
8882

89-
reload(options) {
83+
reload({ distPath }) {
9084
if (this._app) {
9185
this._app.destroy();
9286
}
9387

94-
options = options || {};
95-
this._buildEmberApp(options.distPath || null);
88+
this._buildEmberApp(distPath);
9689
}
9790

98-
_buildEmberApp(distPath, sandbox, sandboxGlobals) {
99-
distPath = distPath || this.distPath;
100-
sandbox = sandbox || this.sandbox;
101-
sandboxGlobals = sandboxGlobals || this.sandboxGlobals;
102-
91+
_buildEmberApp(distPath = this.distPath, sandbox = this.sandbox, sandboxGlobals = this.sandboxGlobals) {
10392
if (!distPath) {
10493
throw new Error('You must instantiate FastBoot with a distPath ' +
10594
'option that contains a path to a dist directory ' +
@@ -111,10 +100,11 @@ class FastBoot {
111100
}
112101

113102
this.distPath = distPath;
103+
114104
this._app = new EmberApp({
115-
distPath: distPath,
116-
sandbox: sandbox,
117-
sandboxGlobals: sandboxGlobals
105+
distPath,
106+
sandbox,
107+
sandboxGlobals,
118108
});
119109
}
120110

src/install-source-map-support.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const sourceMapSupport = require('source-map-support');
22

33
function prepareStackTrace(error, stack) {
4-
return error + stack.map(function(frame) {
5-
return '\n at ' + sourceMapSupport.wrapCallSite(frame);
6-
}).join('');
4+
return error + stack.map((frame) => '\n at ' + sourceMapSupport.wrapCallSite(frame)).join('');
75
}
86

97
function install(errorClass) {
@@ -18,5 +16,5 @@ function install(errorClass) {
1816
}
1917

2018
module.exports = {
21-
install: install
19+
install
2220
};

src/result.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ const HTML_HEAD_REGEX = /^([\s\S]*<\/head>)([\s\S]*)/;
1212
* method.
1313
*/
1414
class Result {
15-
constructor(options) {
15+
constructor(options = {}) {
16+
let { doc, html, fastbootInfo } = options;
17+
1618
this._instanceDestroyed = false;
17-
this._doc = options.doc;
18-
this._html = options.html;
19-
this._fastbootInfo = options.fastbootInfo;
19+
20+
this._doc = doc;
21+
this._html = html;
22+
this._fastbootInfo = fastbootInfo;
2023
}
2124

2225
/**
@@ -66,22 +69,19 @@ class Result {
6669
return [html];
6770
}
6871

69-
let head = docParts[1];
70-
let body = docParts[2];
72+
let [, head, body] = docParts;
7173

7274
if (!head || !body) {
7375
throw new Error('Could not idenfity head and body of the document! Make sure the document is well formed.');
7476
}
7577

76-
let chunks = [head];
77-
let bodyParts = body.split(SHOEBOX_TAG_PATTERN);
78-
let plainBody = bodyParts[0];
79-
chunks.push(plainBody);
78+
let [ plainBody, ...shoeboxes ] = body.split(SHOEBOX_TAG_PATTERN);
8079

81-
let shoeboxes = bodyParts.splice(1);
82-
shoeboxes.forEach((shoebox) => {
83-
chunks.push(`${SHOEBOX_TAG_PATTERN}${shoebox}`);
84-
});
80+
let chunks = [
81+
head,
82+
plainBody
83+
]
84+
.concat(shoeboxes.map(shoebox => `${SHOEBOX_TAG_PATTERN}${shoebox}`));
8585

8686
return chunks;
8787
});
@@ -114,7 +114,7 @@ class Result {
114114

115115
// Grab some metadata from the sandboxed application instance
116116
// and copy it to this Result object.
117-
let instance = this.instance;
117+
let { instance } = this;
118118
if (instance) {
119119
this._finalizeMetadata(instance);
120120
}
@@ -130,7 +130,7 @@ class Result {
130130
this.url = instance.getURL();
131131
}
132132

133-
let response = this._fastbootInfo.response;
133+
let { response } = this._fastbootInfo;
134134

135135
if (response) {
136136
this.headers = response.headers;
@@ -142,8 +142,10 @@ class Result {
142142
if (this.instance && !this._instanceDestroyed) {
143143
this._instanceDestroyed = true;
144144
this.instance.destroy();
145+
145146
return true;
146147
}
148+
147149
return false;
148150
}
149151

@@ -178,10 +180,10 @@ class Result {
178180
}
179181

180182
function missingTag(tag) {
181-
return Promise.reject(new Error(`Fastboot was not able to find ${tag} in base HTML. It could not replace the contents.`));
183+
throw new Error(`Fastboot was not able to find ${tag} in base HTML. It could not replace the contents.`);
182184
}
183185

184-
function insertIntoIndexHTML(html, htmlAttributes, head, body, bodyAttributes) {
186+
async function insertIntoIndexHTML(html, htmlAttributes, head, body, bodyAttributes) {
185187
if (!html) { return Promise.resolve(html); }
186188
let isBodyReplaced = false;
187189
let isHeadReplaced = false;
@@ -210,13 +212,13 @@ function insertIntoIndexHTML(html, htmlAttributes, head, body, bodyAttributes) {
210212
}
211213

212214
if (head && !isHeadReplaced) {
213-
return missingTag('<!--EMBER_CLI_FASTBOOT_HEAD-->');
215+
missingTag('<!--EMBER_CLI_FASTBOOT_HEAD-->');
214216
}
215217
if (body && !isBodyReplaced) {
216-
return missingTag('<!--EMBER_CLI_FASTBOOT_BODY-->');
218+
missingTag('<!--EMBER_CLI_FASTBOOT_BODY-->');
217219
}
218220

219-
return Promise.resolve(html);
221+
return html;
220222
}
221223

222224
module.exports = Result;

0 commit comments

Comments
 (0)