Skip to content

Commit

Permalink
feat: add dynamic loader for buffer (#36)
Browse files Browse the repository at this point in the history
* feat: add dynamic loader for buffer

* chore: remove console.logs
  • Loading branch information
jclaessens97 authored Jan 19, 2024
1 parent b207ee6 commit 4dab5e1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
9 changes: 8 additions & 1 deletion hyperid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
const uuidv4 = require('./uuid-node')
const parser = require('uuid-parse')
const maxInt = Math.pow(2, 31) - 1
const Buffer = require('buffer').Buffer
const Buffer = loadBuffer()
function loadBuffer () {
const b = require('buffer')
// use third party module if no buffer module
return b && b.Buffer
? b.Buffer
: require('buffer/').Buffer
}
const base64Padding = Buffer.from('==', 'base64')

function hyperid (opts) {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"scripts": {
"typescript": "tsc --project ./test/tsconfig.json",
"test": "standard && tape test/test.js test/uniqueness.js | tap-dot && npm run typescript"
"test": "standard && tape test/test.js test/uniqueness.js test/buffer.ts | tap-dot && npm run typescript"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,11 +41,13 @@
"standard": "^16.0.3",
"tap-dot": "^2.0.0",
"tape": "^5.0.0",
"typescript": "^4.3.4"
"typescript": "^4.3.4",
"proxyquire": "^2.1.3"
},
"dependencies": {
"uuid": "^8.3.2",
"uuid-parse": "^1.1.0"
"uuid-parse": "^1.1.0",
"buffer": "^5.2.1"
},
"browser": {
"./uuid-node.js": "./uuid-browser.js"
Expand Down
16 changes: 16 additions & 0 deletions test/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const hyperid = require('..')
const test = require('tape')
const proxyquire = require('proxyquire')

test('require buffer', function (t) {
t.plan(2);

const instance = hyperid()
const id = instance()
t.ok(id)

proxyquire('../hyperid', { 'buffer': { Buffer: null } });
const instance2 = hyperid()
const id2 = instance2()
t.ok(id2)
})

0 comments on commit 4dab5e1

Please sign in to comment.