[Pkg-javascript-commits] [node-base64id] 01/15: Imported Upstream version 0.1.0~git20130507
Mike Gabriel
sunweaver at debian.org
Tue Aug 19 17:50:44 UTC 2014
This is an automated email from the git hooks/post-receive script.
sunweaver pushed a commit to branch master
in repository node-base64id.
commit b4cfa723f14f7f5ddeed9c31148deac9eed6ef45
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date: Tue May 7 23:38:48 2013 +0200
Imported Upstream version 0.1.0~git20130507
---
.gitignore | 10 ++++++
.npmignore | 3 ++
LICENSE | 22 ++++++++++++
README.md | 18 ++++++++++
lib/base64id.js | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 12 +++++++
6 files changed, 168 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..a70d306
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,10 @@
+.DS_Store
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+benchmarks/*.png
+node_modules
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..39e9864
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,3 @@
+support
+test
+examples
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..3ebf4a7
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+(The MIT License)
+
+Copyright (c) 2012 Kristian Faeldt <faeldt_kristian at cyberagent.co.jp>
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..17689e6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,18 @@
+base64id
+========
+
+Node.js module that generates a base64 id.
+
+Uses crypto.randomBytes when available, falls back to unsafe methods for node.js <= 0.4.
+
+To increase performance, random bytes are buffered to minimize the number of synchronous calls to crypto.randomBytes.
+
+## Installation
+
+ $ npm install base64id
+
+## Usage
+
+ var base64id = require('base64id');
+
+ var id = base64id.generateId();
diff --git a/lib/base64id.js b/lib/base64id.js
new file mode 100644
index 0000000..f688159
--- /dev/null
+++ b/lib/base64id.js
@@ -0,0 +1,103 @@
+/*!
+ * base64id v0.1.0
+ */
+
+/**
+ * Module dependencies
+ */
+
+var crypto = require('crypto');
+
+/**
+ * Constructor
+ */
+
+var Base64Id = function() { };
+
+/**
+ * Get random bytes
+ *
+ * Uses a buffer if available, falls back to crypto.randomBytes
+ */
+
+Base64Id.prototype.getRandomBytes = function(bytes) {
+
+ var BUFFER_SIZE = 4096
+ var self = this;
+
+ bytes = bytes || 12;
+
+ if (bytes > BUFFER_SIZE) {
+ return crypto.randomBytes(bytes);
+ }
+
+ var bytesInBuffer = parseInt(BUFFER_SIZE/bytes);
+ var threshold = parseInt(bytesInBuffer*0.85);
+
+ if (!threshold) {
+ return crypto.randomBytes(bytes);
+ }
+
+ if (this.bytesBufferIndex == null) {
+ this.bytesBufferIndex = -1;
+ }
+
+ if (this.bytesBufferIndex == bytesInBuffer) {
+ this.bytesBuffer = null;
+ this.bytesBufferIndex = -1;
+ }
+
+ // No buffered bytes available or index above threshold
+ if (this.bytesBufferIndex == -1 || this.bytesBufferIndex > threshold) {
+
+ if (!this.isGeneratingBytes) {
+ this.isGeneratingBytes = true;
+ crypto.randomBytes(BUFFER_SIZE, function(err, bytes) {
+ self.bytesBuffer = bytes;
+ self.bytesBufferIndex = 0;
+ self.isGeneratingBytes = false;
+ });
+ }
+
+ // Fall back to sync call when no buffered bytes are available
+ if (this.bytesBufferIndex == -1) {
+ return crypto.randomBytes(bytes);
+ }
+ }
+
+ var result = this.bytesBuffer.slice(bytes*this.bytesBufferIndex, bytes*(this.bytesBufferIndex+1));
+ this.bytesBufferIndex++;
+
+ return result;
+}
+
+/**
+ * Generates a base64 id
+ *
+ * (Original version from socket.io <http://socket.io>)
+ */
+
+Base64Id.prototype.generateId = function () {
+ var rand = new Buffer(15); // multiple of 3 for base64
+ if (!rand.writeInt32BE) {
+ return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
+ + Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
+ }
+ this.sequenceNumber = (this.sequenceNumber + 1) | 0;
+ rand.writeInt32BE(this.sequenceNumber, 11);
+ if (crypto.randomBytes) {
+ this.getRandomBytes(12).copy(rand);
+ } else {
+ // not secure for node 0.4
+ [0, 4, 8].forEach(function(i) {
+ rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
+ });
+ }
+ return rand.toString('base64').replace(/\//g, '_').replace(/\+/g, '-');
+};
+
+/**
+ * Export
+ */
+
+exports = module.exports = new Base64Id();
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c1bc16b
--- /dev/null
+++ b/package.json
@@ -0,0 +1,12 @@
+{
+ "name": "base64id"
+ , "version": "0.1.0"
+ , "description": "Generates a base64 id"
+ , "author": "Kristian Faeldt <faeldt_kristian at cyberagent.co.jp>"
+ , "repository": {
+ "type": "git"
+ , "url": "https://github.com/faeldt/base64id.git"
+ }
+ , "main": "./lib/base64id.js"
+ , "engines": { "node": ">= 0.4.0" }
+}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-base64id.git
More information about the Pkg-javascript-commits
mailing list