Skip to content

Commit

Permalink
2016-01-30 v0.0.0 Release
Browse files Browse the repository at this point in the history
express’s prototype injection, made generic with a usable API.
  • Loading branch information
Fishrock123 committed Jan 30, 2016
0 parents commit 9c35aa4
Show file tree
Hide file tree
Showing 8 changed files with 479 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# OS X
.DS_Store*
Icon?
._*

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux
.directory
*~


# npm
node_modules
*.log
*.gz


# Coveralls
coverage
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: node_js
node_js:
- "4"
- "5"
matrix:
allow_failures:
- node_js: "5"
fast_finish: true
script: "npm run-script test-travis"
after_script: "test $TRAVIS_NODE_VERSION = '0.10' && npm install [email protected] && cat ./coverage/lcov.info | coveralls"
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
0.0.0 / 2016-01-27
==================

* Initial release.
* Prototype extension.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2016 Jeremiah Senkpiel <[email protected]>

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.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# extend-proto

[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Node.js Version][node-version-image]][node-version-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]


```js
var http = require('http')
var proto = require('extend-proto')

var proto = Proto({
req: http.IncomingMessage,
res: http.ServerResponse
})
proto.req.defineProperty('kittens', { value: 'the best' })

http.createServer(function(req, res) {
proto(req, res)

req.kittens // the best
})

```


## [MIT Licensed](LICENSE)

[npm-image]: https://img.shields.io/npm/v/extend-proto.svg?style=flat
[npm-url]: https://npmjs.org/package/extend-proto
[node-version-image]: https://img.shields.io/node/v/extend-proto.svg?style=flat
[node-version-url]: http://nodejs.org/download/
[travis-image]: https://img.shields.io/travis/jshttp/extend-proto.svg?style=flat
[travis-url]: https://travis-ci.org/jshttp/extend-proto
[coveralls-image]: https://img.shields.io/coveralls/jshttp/extend-proto.svg?style=flat
[coveralls-url]: https://coveralls.io/r/jshttp/extend-proto?branch=master
[downloads-image]: https://img.shields.io/npm/dm/extend-proto.svg?style=flat
[downloads-url]: https://npmjs.org/package/extend-proto
111 changes: 111 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*!
* extend-proto
* Copyright (c) 2016 Jeremiah Senkpiel
* MIT Licensed
*/


/**
* Expose `Proto`.
*/
module.exports = Proto


/**
* Initialize a new `Proto` with the given `options`.
*
* @param {object} options
* @return {middleware} for prototype extension
* @public
*/
function Proto(protos, options) {
var opts = options || {}

if (!protos) {
throw new TypeError('argument `protos` is required')
}
if (typeof protos !== 'object' || Array.isArray(protos)) {
throw new TypeError('argument `protos` must be an object')
}
var _protos = Object.keys(protos)

// default `configurable` to `true` so that properties may be overwriten
opts.configurable = undefined === opts.configurable ? true : opts.configurable

// default `enumerable` to `true` so that prototypes may be properly inspected
opts.enumerable = undefined === opts.enumerable ? true : opts.enumerable

/**
* prototype extension middlware
*/
var middleware = function setProto() {
var i = arguments.length
while (i--) {
arguments[i].__proto__ = middleware[_protos[i]].proto
}
}

// setup prototypes
var i = _protos.length
while (i--) {
var name = _protos[i]
var prop = middleware[name] = {
proto: { __proto__: protos[name].prototype }
, _opts: opts
}
prop.defineProperty = defineProperty.bind(prop)
prop.defineProperties = defineProperties.bind(prop)
}

return middleware
}


/**
* define a property onto the __proto__
*/
function defineProperty(name, descriptor) {
if (!name) {
throw new TypeError('argument `name` is required')
}

if (typeof name !== 'string') {
throw new TypeError('argument `name` must be a string')
}

if (!descriptor) {
throw new TypeError('argument `descriptor` is required')
}

if (typeof descriptor !== 'object' || Array.isArray(descriptor)) {
throw new TypeError('argument `descriptor` must be an object')
}

// set user-defined options
descriptor.configurable = this._opts.configurable
descriptor.enumerable = this._opts.enumerable

Object.defineProperty(this.proto, name, descriptor)
}


/**
* define properties on __proto__ via an object map
*/
function defineProperties(props) {
if (!props) {
throw new TypeError('argument `props` is required')
}

if (typeof props !== 'object' || Array.isArray(props)) {
throw new TypeError('argument `props` must be an object')
}

// set user-defined options
for (var key in props) {
props[key].configurable = this._opts.configurable
props[key].enumerable = this._opts.enumerable
}

Object.defineProperties(this.proto, props)
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "extend-proto",
"description": "generic __proto__ injection utility",
"version": "0.0.0",
"contributors": [
"Jeremiah Senkpiel <[email protected]>"
],
"license": "MIT",
"repository": "pillarjs/extend-proto",
"keywords": [
"proto",
"__proto__",
"prototype",
"extend",
"inject"
],
"dependencies": {
},
"devDependencies": {
"mocha": "^2.0.1",
"istanbul": "0.3.2"
},
"engines": {
"node": ">= 4"
},
"files": [
"LICENSE",
"HISTORY.md",
"README.md",
"index.js"
],
"scripts": {
"test": "mocha --reporter spec --check-leaks",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks"
}
}
Loading

0 comments on commit 9c35aa4

Please sign in to comment.