Skip to content

Commit

Permalink
Merge pull request #24 from manzt/manzt/optional-buffer
Browse files Browse the repository at this point in the history
feat: make Buffer optional
  • Loading branch information
lotas authored Sep 5, 2022
2 parents ce3bf62 + d708e84 commit 1ee5a2e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
12 changes: 11 additions & 1 deletion .taskcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,28 @@ tasks:
environments:
- image: node:6
description: Run tests with node v6
command: npm test
- image: node:8
description: Run tests with node v8
command: npm test
- image: node:10
description: Run tests with node v10
command: npm test
- image: node:12
description: Run tests with node v12
command: npm test
- image: node:14
description: Run tests with node v14
command: npm test
- image: node:16
description: Run tests with node v16
command: npm test
- image: node:latest
description: Run tests with latest node
command: npm test
- image: node:latest
description: Run tests with latest node without Buffer
command: npm run test-nobuffer
in:
$if: tasks_for == "github-pull-request" && event["action"] in ["opened","reopened","synchronize"]
then:
Expand Down Expand Up @@ -57,4 +67,4 @@ tasks:
git config advice.detachedHead false &&
git checkout --no-progress ${head_rev} &&
npm install --no-progress . &&
npm test
${env.command}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "URL-safe base64 UUID encoder for generating 22 character slugs",
"license": "MIT",
"scripts": {
"test": "nodeunit slugid_test.js"
"test": "nodeunit slugid_test.js",
"test-nobuffer": "NO_BUFFER=1 npm test"
},
"repository": {
"type": "git",
Expand Down
28 changes: 22 additions & 6 deletions slugid.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,29 @@

var uuid = require('uuid');

/** @type {(bytes: Uint8Array) => string} */
var toBase64 = (() => {
if (typeof Buffer !== 'undefined') {
return (bytes) => Buffer.from(bytes).toString('base64');
}
return (bytes) => btoa(String.fromCharCode(...bytes));
})();

/** @type {(base64: string) => Uint8Array | Buffer} */
var fromBase64 = (() => {
if (typeof Buffer !== 'undefined') {
return (base64) => Buffer.from(base64, 'base64');
}
return (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
})();

/**
* Returns the given uuid as a 22 character slug. This can be a regular v4
* slug or a "nice" slug.
*/
exports.encode = function(uuid_) {
var bytes = uuid.parse(uuid_);
var base64 = Buffer.from(bytes).toString('base64');
var base64 = toBase64(bytes);
var slug = base64
.replace(/\+/g, '-') // Replace + with - (see RFC 4648, sec. 5)
.replace(/\//g, '_') // Replace / with _ (see RFC 4648, sec. 5)
Expand All @@ -44,15 +60,15 @@ exports.decode = function(slug) {
.replace(/-/g, '+')
.replace(/_/g, '/')
+ '==';
return uuid.stringify(Buffer.from(base64, 'base64'));
return uuid.stringify(fromBase64(base64));
};

/**
* Returns a randomly generated uuid v4 compliant slug
*/
exports.v4 = function() {
var bytes = uuid.v4(null, Buffer.alloc(16));
var base64 = bytes.toString('base64');
var bytes = uuid.v4(null, new Uint8Array(16));
var base64 = toBase64(bytes);
var slug = base64
.replace(/\+/g, '-') // Replace + with - (see RFC 4648, sec. 5)
.replace(/\//g, '_') // Replace / with _ (see RFC 4648, sec. 5)
Expand All @@ -72,9 +88,9 @@ exports.v4 = function() {
* restrict the range of potential uuids that may be generated.
*/
exports.nice = function() {
var bytes = uuid.v4(null, Buffer.alloc(16));
var bytes = uuid.v4(null, new Uint8Array(16));
bytes[0] = bytes[0] & 0x7f; // unset first bit to ensure [A-Za-f] first char
var base64 = bytes.toString('base64');
var base64 = toBase64(bytes);
var slug = base64
.replace(/\+/g, '-') // Replace + with - (see RFC 4648, sec. 5)
.replace(/\//g, '_') // Replace / with _ (see RFC 4648, sec. 5)
Expand Down
6 changes: 6 additions & 0 deletions slugid_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// Allows the tests to be run in environments with and without `Buffer`.
if (process.env.NO_BUFFER === "1") {
delete global.Buffer;
console.log('\x1b[33m' + 'Removed `Buffer` from globalThis.' + '\x1b[0m');
}

var slugid = require('./slugid');
var uuidv4 = require('uuid').v4;

Expand Down

0 comments on commit 1ee5a2e

Please sign in to comment.