Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #68, added utility functions, more descriptive error messages #71

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 3.0.1 (Next)

* [#71](https://github.com/alexa-js/alexa-app-server/pull/71): Fixed [#68](https://github.com/alexa-js/alexa-app-server/issues/68), added utility functions, more descriptive error messages - [@tejashah88](https://github.com/tejashah88).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's important in changelog is to describe the actual fix to be helpful for the person reading it. So for example:

* [#71](https://github.com/alexa-js/alexa-app-server/pull/71), [#68](https://github.com/alexa-js/alexa-app-server/issues/68): Fixed log output containing multiple slashes - [@tejashah88](https://github.com/tejashah88).

* [#72](https://github.com/alexa-js/alexa-app-server/pull/72): Use `path.join` for constructing relative paths - [@dblock](https://github.com/dblock).
* [#74](https://github.com/alexa-js/alexa-app-server/pull/74): Added locale selector to test page - [@siedi](https://github.com/siedi).
* Your contribution here.
Expand Down
18 changes: 9 additions & 9 deletions RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ Modify the "Stable Release" section in [README.md](README.md). Change the text t
```
## Stable Release

You're reading the documentation for the stable release of alexa-app-server, 2.4.0.
You're reading the documentation for the stable release of alexa-app-server, 3.0.1.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are examples I never change them, but no big deal.

```

Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.

```
### 2.4.0 (Feb 4, 2017)
### 3.0.1 (Feb 7, 2017)
```

Remove the line with "Your contribution here.", since there will be no more contributions to this release.
Expand All @@ -42,13 +42,13 @@ Commit your changes.

```
git add README.md CHANGELOG.md
git commit -m "Preparing for release, 2.4.0."
git commit -m "Preparing for release, 3.0.1."
```

Tag the release.

```
git tag v2.4.0
git tag v3.0.1
```

Release.
Expand All @@ -70,14 +70,14 @@ Modify the "Stable Release" section in [README.md](README.md). Change the text t
```
## Stable Release

You're reading the documentation for the next release of alexa-app-server, which should be 2.4.1.
The current stable release is 2.4.0.
You're reading the documentation for the next release of alexa-app-server, which should be 3.0.2.
The current stable release is 3.0.1.
```

Add the next release to [CHANGELOG.md](CHANGELOG.md).

```
#### 2.4.1 (Next)
#### 3.0.2 (Next)

* Your contribution here.
```
Expand All @@ -88,6 +88,6 @@ Commit your changes.

```
git add CHANGELOG.md README.md package.json
git commit -m "Preparing for next development iteration, 2.4.1."
git commit -m "Preparing for next development iteration, 3.0.2."
git push origin master
```
```
4 changes: 2 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This is the first release out of the [alexa-js Github org](https://github.com/al

#### Alexa-app minimum version

A minimum version 2.5.0 of `alexa-app` is now required.
A minimum version 3.0.0 of `alexa-app` is now required.

See [alexa-app CHANGELOG](https://github.com/alexa-js/alexa-app/blob/master/CHANGELOG.md) and [UPGRADING](https://github.com/alexa-js/alexa-app/blob/master/UPGRADING.md) for more information.

Expand All @@ -16,4 +16,4 @@ The `.urlencoded` body-parser has been removed. If your application requires it,

The `.json` body-parser is only used for Alexa applications when `alexa-app-server` is started with `verify: false`, because `verifier-middleware`, which is now mounted inside `alexa-app` acts as a body-parser as well.

See [#35](https://github.com/alexa-js/alexa-app-server/issues/35) and [#64](https://github.com/alexa-js/alexa-app-server/pull/64) for more information.
See [#35](https://github.com/alexa-js/alexa-app-server/issues/35) and [#64](https://github.com/alexa-js/alexa-app-server/pull/64) for more information.
52 changes: 27 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var hotswap = require('hotswap');
var fs = require('fs');
var path = path = require('path');
var path = require('path');
var http = require('http');
var https = require('https');
var express = require('express');
var alexa = require('alexa-app');
var Promise = require('bluebird');
var defaults = require('lodash.defaults');
var defaults = require("lodash.defaults");
var utils = require("./utils");

var appServer = function(config) {
var self = {};
Expand All @@ -32,7 +33,7 @@ var appServer = function(config) {
throw new Error("invalid configuration: the verify and debug options cannot be both enabled");
}

if (self.config.httpEnabled == false && self.config.httpsEnabled == false) {
if (!self.config.httpEnabled && !self.config.httpsEnabled) {
throw new Error("invalid configuration: either http or https must be enabled");
}

Expand Down Expand Up @@ -69,30 +70,30 @@ var appServer = function(config) {
// set up a router to hang all alexa apps off of
var alexaRouter = express.Router();

var normalizedRoot = root.indexOf('/') === 0 ? root : '/' + root;
var normalizedRoot = utils.normalizeApiPath(root);
self.express.use(normalizedRoot, alexaRouter);

var app_directories = function(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
return utils.isValidDirectory(path.join(srcpath, file));
});
};

app_directories(app_dir).forEach(function(dir) {
var package_json = path.join(app_dir, dir, "/package.json");
if (!fs.existsSync(package_json) || !fs.statSync(package_json).isFile()) {
var package_json = path.join(app_dir, dir, "package.json");
if (!utils.isValidFile(package_json)) {
self.error(" package.json not found in directory " + dir);
return;
}

var pkg = JSON.parse(fs.readFileSync(package_json, 'utf8'));
var pkg = utils.readJsonFile(package_json);
if (!pkg || !pkg.main || !pkg.name) {
self.error(" failed to load " + package_json);
return;
}

var main = fs.realpathSync(path.join(app_dir, dir, pkg.main));
if (!fs.existsSync(main) || !fs.statSync(main).isFile()) {
if (!utils.isValidFile(main)) {
self.error(" main file not found for app [" + pkg.name + "]: " + main);
return;
}
Expand Down Expand Up @@ -127,7 +128,8 @@ var appServer = function(config) {
postRequest: self.config.postRequest
});

self.log(" loaded app [" + pkg.name + "] at endpoint: " + normalizedRoot + "/" + pkg.name);
var endpoint = path.posix.join(normalizedRoot, pkg.name);
self.log(" loaded app [" + pkg.name + "] at endpoint: " + endpoint);
});

return self.apps;
Expand All @@ -137,7 +139,7 @@ var appServer = function(config) {
self.load_server_modules = function(server_dir) {
var server_files = function(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isFile();
return utils.isValidFile(path.join(srcpath, file));
});
};
server_files(server_dir).forEach(function(file) {
Expand All @@ -164,7 +166,7 @@ var appServer = function(config) {

// serve static content
var static_dir = path.join(self.config.server_root, self.config.public_html);
if (fs.existsSync(static_dir) && fs.statSync(static_dir).isDirectory()) {
if (utils.isValidDirectory(static_dir)) {
self.log("serving static content from: " + static_dir);
self.express.use(express.static(static_dir));
} else {
Expand All @@ -173,7 +175,7 @@ var appServer = function(config) {

// find any server-side processing modules and let them hook in
var server_dir = path.join(self.config.server_root, self.config.server_dir);
if (fs.existsSync(server_dir) && fs.statSync(server_dir).isDirectory()) {
if (utils.isValidDirectory(server_dir)) {
self.log("loading server-side modules from: " + server_dir);
self.load_server_modules(server_dir);
} else {
Expand All @@ -182,14 +184,14 @@ var appServer = function(config) {

// find and load alexa-app modules
var app_dir = path.join(self.config.server_root, self.config.app_dir);
if (fs.existsSync(app_dir) && fs.statSync(app_dir).isDirectory()) {
if (utils.isValidDirectory(app_dir)) {
self.log("loading apps from: " + app_dir);
self.load_apps(app_dir, self.config.app_root);
} else {
self.log("apps not loaded because directory [" + app_dir + "] does not exist");
}

if (self.config.httpsEnabled == true) {
if (self.config.httpsEnabled) {
self.log("enabling https");

if (self.config.privateKey != undefined && self.config.certificate != undefined && self.config.httpsPort != undefined) {
Expand All @@ -198,21 +200,21 @@ var appServer = function(config) {
var certificateFile = path.join(sslCertRoot, self.config.certificate);
var chainFile = (self.config.chain != undefined) ? path.join(sslCertRoot, self.config.chain) : undefined;

if (fs.existsSync(privateKeyFile) && fs.existsSync(certificateFile)) {
var privateKey = fs.readFileSync(privateKeyFile, 'utf8');
var certificate = fs.readFileSync(certificateFile, 'utf8');
if (utils.isValidFile(privateKeyFile) && utils.isValidFile(certificateFile)) {
var privateKey = utils.readFile(privateKeyFile);
var certificate = utils.readFile(certificateFile);

var chain = undefined;
if (chainFile != undefined) {
if (fs.existsSync(chainFile)) {
chain = fs.readFileSync(chainFile, 'utf8');
if (utils.isValidFile(chainFile)) {
chain = utils.readFile(chainFile);
} else {
self.error("chain: '" + self.config.chain + "' does not exist in /sslcert");
self.error("chain: '" + self.config.chain + "' does not exist in " + sslCertRoot);
}
}

if (chain == undefined && chainFile != undefined) {
self.error("failed to load chain from /sslcert, https will not be enabled");
self.error("failed to load chain from " + sslCertRoot + ", https will not be enabled");
} else if (privateKey != undefined && certificate != undefined) {
var credentials = {
key: privateKey,
Expand All @@ -225,7 +227,7 @@ var appServer = function(config) {

if (chain != undefined) {
credentials.ca = chain;
self.log("using chain certificate from /sslcert");
self.log("using chain certificate from " + sslCertRoot);
}

try {
Expand All @@ -243,10 +245,10 @@ var appServer = function(config) {
self.error("failed to listen via https: " + error);
}
} else {
self.error("failed to load privateKey or certificate from /sslcert, https will not be enabled");
self.error("failed to load privateKey or certificate from " + sslCertRoot + ", https will not be enabled");
}
} else {
self.error("privateKey: '" + self.config.privateKey + "' or certificate: '" + self.config.certificate + "' do not exist in /sslcert, https will not be enabled");
self.error("privateKey: '" + self.config.privateKey + "' or certificate: '" + self.config.certificate + "' do not exist in " + sslCertRoot + ", https will not be enabled");
}
} else {
self.error("httpsPort, privateKey or certificate parameter is not set in config, https will not be enabled");
Expand Down
26 changes: 26 additions & 0 deletions invalid_examples/apps/hello_world/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var alexa = require('alexa-app');

// Allow this module to be reloaded by hotswap when changed
module.change_code = 1;

// Define an alexa-app
var app = new alexa.app('hello_world');
app.launch(function(req, res) {
res.say("Hello World!!");
});

app.intent('NameIntent', {
"slots": { "NAME": "LITERAL", "AGE": "NUMBER" },
"utterances": ["{My name is|my name's} {matt|bob|bill|jake|nancy|mary|jane|NAME} and I am {1-100|AGE}{ years old|}"]
}, function(req, res) {
res.say('Your name is ' + req.slot('NAME') + ' and you are ' + req.slot('AGE') + ' years old');
});

app.intent('AgeIntent', {
"slots": { "AGE": "NUMBER" },
"utterances": ["My age is {1-100|AGE}"]
}, function(req, res) {
res.say('Your age is ' + req.slot('AGE'));
});

module.exports = app;
14 changes: 14 additions & 0 deletions invalid_examples/apps/hello_world/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "hello_world",
"version": "1.0.0",
"description": "A sample Alexa app",
"main": "index.js",
"author": "Matt Kruse <github@mattkruse.com> (http://mattkruse.com/)",
"license": "ISC",
"alexa": {
"applicationId": "amzn1.echo-sdk-ams.app.999999-d0ed-9999-ad00-999999d00ebe"
},
"dependencies": {
"alexa-app": "^2.1.0"
}
}
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"license": "MIT",
"dependencies": {
"alexa-app": "^3.0.0",
"alexa-verifier-middleware": "^0.1.8",
"bluebird": "^3.4.7",
"body-parser": "~1.16.0",
"ejs": "~2.5.5",
Expand All @@ -29,12 +28,12 @@
"lodash.defaults": "^4.2.0"
},
"devDependencies": {
"mocha": "^2.3.4",
"chai": "^3.4.1",
"supertest": "^2.0.1",
"mocha": "^3.2.0",
Copy link
Collaborator

@dblock dblock Feb 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously downgraded some dependencies here to make things work with older versions of node. Surpried this works.

"chai": "^3.5.0",
"supertest": "^3.0.0",
"istanbul": "0.4.5",
"coveralls": "^2.11.15",
"danger": "0.11.2",
"danger": "0.11.4",
"tcp-port-used": "0.1.2"
}
}
3 changes: 3 additions & 0 deletions test/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"pangram": "The quick brown fox jumps over the lazy dog"
}
1 change: 1 addition & 0 deletions test/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The quick brown fox jumps over the lazy dog
3 changes: 2 additions & 1 deletion test/test-examples-server-https-support-extended.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ var request = require("supertest");
var alexaAppServer = require("../index");
var fs = require("fs");
var tcpPortUsed = require('tcp-port-used');
var utils = require("../utils");

describe("Alexa App Server with Examples & more HTTPS support", function() {
var testServer;
var sampleLaunchReq;

before(function() {
sampleLaunchReq = JSON.parse(fs.readFileSync("test/sample-launch-req.json", 'utf8'));
sampleLaunchReq = utils.readJsonFile("test/sample-launch-req.json");
});

afterEach(function() {
Expand Down
8 changes: 5 additions & 3 deletions test/test-examples-server-https-support.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,25 @@ chai.config.includeStack = true;
var request = require("supertest");
var alexaAppServer = require("../index");
var fs = require("fs");
var utils = require("../utils");

describe("Alexa App Server with Examples & HTTPS support", function() {
var testServer;
var sampleLaunchReq;

before(function() {
testServer = alexaAppServer.start({
port: 6000,
port: 3000,
server_root: 'examples',
https: true,
httpsEnabled: true,
httpsPort: 6000,
privateKey: 'private-key.pem',
certificate: 'cert.cer',
chain: 'cert.ca_bundle',
passphrase: "test123"
});

sampleLaunchReq = JSON.parse(fs.readFileSync("test/sample-launch-req.json", 'utf8'));
sampleLaunchReq = utils.readJsonFile("test/sample-launch-req.json");
});

after(function() {
Expand Down
3 changes: 2 additions & 1 deletion test/test-examples-server-pre-post-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ chai.config.includeStack = true;
var request = require("supertest");
var alexaAppServer = require("../index");
var fs = require("fs");
var utils = require("../utils");

describe("Alexa App Server with Examples & Pre/Post functions", function() {
var testServer, fired;
Expand All @@ -30,7 +31,7 @@ describe("Alexa App Server with Examples & Pre/Post functions", function() {
}
});

sampleLaunchReq = JSON.parse(fs.readFileSync("test/sample-launch-req.json", 'utf8'));
sampleLaunchReq = utils.readJsonFile("test/sample-launch-req.json");
});

after(function() {
Expand Down
Loading