Skip to content

Commit

Permalink
fix: Cors headers (#119)
Browse files Browse the repository at this point in the history
* fix: Cors header by default '*'

* feat: Pass CLI option for cors headers

* refactor: Change description of cors option
  • Loading branch information
rkostrzewski authored and developit committed Oct 12, 2017
1 parent 97cdfd6 commit 9e2dd15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export default asyncCommand({
description: 'Port to start a server on.',
defaultDescription: 'PORT || 8080',
alias: 'p'
},
cors: {
description: 'Set allowed origins',
defaultDescription: 'https://localhost:${PORT}'
}
},

Expand Down Expand Up @@ -79,12 +83,15 @@ async function serve(options) {
configFile = await tmpFile({ postfix: '.json' });
await fs.writeFile(configFile, JSON.stringify(config));

let port = options.port || process.env.PORT || 8080;

await serveHttp2({
options,
config: configFile,
configObj: config,
server: options.server,
port: options.port || process.env.PORT || 8080,
cors: options.cors || `https://localhost:${port}`,
port,
dir,
cwd: path.resolve(__dirname, '../resources')
});
Expand Down Expand Up @@ -184,19 +191,20 @@ const SERVERS = {
options.cwd = persistencePath('preact-cli');
process.stderr.write(`Falling back to shared directory + simplehttp2server.\n(dir: ${options.cwd})\n`);
}

return [
simplehttp2server,
'-cors', '*',
'-cors', options.cors,
'-config', options.config,
'-listen', `:${options.port || 8080}`
'-listen', `:${options.port}`
];
},
superstatic(options) {
return [
'superstatic',
path.relative(options.cwd, options.dir),
'--gzip',
'-p', options.port || 8080,
'-p', options.port,
'-c', JSON.stringify({ ...options.configObj, public: undefined })
];
},
Expand Down
4 changes: 0 additions & 4 deletions src/resources/static-app.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
"key": "Cache-Control",
"value": "public, max-age=3600, no-cache"
},
{
"key": "Access-Control-Allow-Origin",
"value": "*"
},
{
"key": "Access-Control-Max-Age",
"value": "600"
Expand Down

0 comments on commit 9e2dd15

Please sign in to comment.