Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jaridmargolin committed Jun 7, 2019
0 parents commit 0ecf87c
Show file tree
Hide file tree
Showing 12 changed files with 4,592 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";

/* -----------------------------------------------------------------------------
* eslint config
* -------------------------------------------------------------------------- */

module.exports = {
root: true,
extends: "standard",
parserOptions: { ecmaVersion: 10 },
overrides: [
{
files: ["**/*.test.js"],
env: { mocha: true }
}
]
};
25 changes: 25 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.DS_Store
.grunt

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

npm-debug.log
sauce_connect.log

/.nyc_output
/node_modules
/bower_components
/coverage
/dist
/docs
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sudo: required
dist: trusty

language: node_js
node_js:
- "10"

script: npm run coverage

after_success:
- npm run coveralls
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<h1 align="center">with-fileserver</h1>
<div align="center">
<p>FileServer context manager</p>
<div>
<a href="https://travis-ci.org/jaridmargolin/with-fileserver"><img src="https://travis-ci.org/jaridmargolin/with-fileserver.svg?branch=master" alt="Build Status"></a>
<a href="https://coveralls.io/github/jaridmargolin/with-fileserver?branch=master"><img src="https://coveralls.io/repos/github/jaridmargolin/with-fileserver/badge.svg?branch=master" alt="Coverage Status"></a>
<a href="http://standardjs.com/"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg" alt="Standard - JavaScript Style Guide"></a>
</div>
<div>
<a href="https://npmjs.org/package/with-fileserver"><img src="https://img.shields.io/npm/v/with-fileserver.svg" alt="NPM with-fileserver package"></a>
<a href="https://david-dm.org/jaridmargolin/with-fileserver"><img src="https://david-dm.org/jaridmargolin/with-fileserver.svg" alt="Dependency Status"></a>
<a href="https://david-dm.org/jaridmargolin/with-fileserver#info=devDependencies"><img src="https://david-dm.org/jaridmargolin/with-fileserver/dev-status.svg" alt="devDependency Status"></a>
</div>
</div>
<br>

## Example Usage

```js
const withFileServer = require("with-fileserver");
const request = require("request-promise-native");

const html = withFileServer({ port: 8080, public: process.cwd() })(url =>
request(url)
);
```

## License

The MIT License (MIT) Copyright (c) 2019 Jarid Margolin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

/* -----------------------------------------------------------------------------
* dependencies
* -------------------------------------------------------------------------- */

// lib
const http = require('http')

// 3rd party
const handler = require('serve-handler')

/* -----------------------------------------------------------------------------
* withFileServer
* -------------------------------------------------------------------------- */

// TODO: Pull this out into standalone pkg
const contextManager = ({ before, after }) => options => async action => {
const context = {}
const beforeResult = await before(context, options)

try {
const actionResult = await action(beforeResult)
await after(context, options)
return actionResult
} catch (e) {
await after(context, options)
throw e
}
}

const withFileServer = contextManager({
before: (ctx, { port = 3000, ...opts }) =>
new Promise(resolve => {
ctx.server = http.createServer((req, res) => handler(req, res, opts))
return ctx.server.listen(port, () => resolve(`http://localhost:${port}`))
}),
after: ({ server }) =>
new Promise(resolve => {
return server.close(() => resolve())
})
})

module.exports = withFileServer
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "with-fileserver",
"description": "FileServer context manager",
"author": "Jarid Margolin <jaridmargolin@gmail.com>",
"version": "0.0.1",
"homepage": "https://github.com/jaridmargolin/with-fileserver",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/jaridmargolin/with-fileserver"
},
"bugs": {
"url": "https://github.com/jaridmargolin/with-fileserver/issues"
},
"main": "lib/index.js",
"scripts": {
"coverage": "nyc --reporter=lcov --reporter=text npm test",
"coveralls": "cat ./coverage/lcov.info | coveralls",
"format": "prettier-eslint --write \"**/*.js\"",
"lint": "eslint \"**/*.js\"",
"test": "npm run lint && mocha"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test"
}
},
"lint-staged": {
"*.js": [
"npm run format",
"git add"
]
},
"dependencies": {
"serve-handler": "^6.0.1"
},
"devDependencies": {
"chai": "4.2.0",
"coveralls": "3.0.4",
"eslint": "5.8.0",
"eslint-config-standard": "12.0.0",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-node": "8.0.1",
"eslint-plugin-promise": "4.0.1",
"eslint-plugin-standard": "4.0.0",
"husky": "2.4.0",
"lint-staged": "8.2.0",
"mocha": "6.1.4",
"nyc": "14.1.1",
"prettier-eslint-cli": "4.7.1",
"request": "2.88.0",
"request-promise-native": "1.0.7"
}
}
7 changes: 7 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

/* -----------------------------------------------------------------------------
* prettier config
* -------------------------------------------------------------------------- */

module.exports = {}
5 changes: 5 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"config:base"
]
}
8 changes: 8 additions & 0 deletions test/fixtures/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>with-fileserver</title>
</head>
<body></body>
</html>
60 changes: 60 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* eslint-env mocha */
'use strict'

/* -----------------------------------------------------------------------------
* dependencies
* -------------------------------------------------------------------------- */

// core
const path = require('path')

// 3rd party
const request = require('request-promise-native')
const assert = require('chai').assert

// lib
const withFileServer = require('../lib')

/* -----------------------------------------------------------------------------
* test
* -------------------------------------------------------------------------- */

const fixturesDir = path.join(__dirname, 'fixtures')
const serverOpts = { port: 5005, public: fixturesDir }
const knownUrl = `http://localhost:${serverOpts.port}`

describe('with-fileserver', function () {
it('should start and stop file server', async () => {
await withFileServer(serverOpts)(async url => {
assert.equal(url, knownUrl)
await request(url)
})

try {
await request(knownUrl)
} catch (e) {
return
}

throw new Error('Server not stopped')
})

it('should resolve with fn result', async () => {
assert.equal(await withFileServer(serverOpts)(__ => 'result'), 'result')
})

it('should reject with fn error', async () => {
let err

try {
await withFileServer(serverOpts)(__ => {
err = new Error('error')
throw err
})
} catch (e) {
return assert.equal(e, err)
}

throw new Error('Error not rejected')
})
})
Loading

0 comments on commit 0ecf87c

Please sign in to comment.