Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dynamic loader for buffer #36

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
Loading