|
| 1 | +// imported from https://github.com/faeldt/base64id/blob/af13114809291393846a56eddbb1ac2035ae397c/lib/base64id.js |
| 2 | +/*! |
| 3 | + * base64id v0.1.0 |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * Module dependencies |
| 8 | + */ |
| 9 | + |
| 10 | +import crypto = require("crypto"); |
| 11 | + |
| 12 | +/** |
| 13 | + * Constructor |
| 14 | + */ |
| 15 | + |
| 16 | +var Base64Id = function () {}; |
| 17 | + |
| 18 | +/** |
| 19 | + * Get random bytes |
| 20 | + * |
| 21 | + * Uses a buffer if available, falls back to crypto.randomBytes |
| 22 | + */ |
| 23 | + |
| 24 | +Base64Id.prototype.getRandomBytes = function (bytes) { |
| 25 | + var BUFFER_SIZE = 4096; |
| 26 | + var self = this; |
| 27 | + |
| 28 | + bytes = bytes || 12; |
| 29 | + |
| 30 | + if (bytes > BUFFER_SIZE) { |
| 31 | + return crypto.randomBytes(bytes); |
| 32 | + } |
| 33 | + |
| 34 | + // var bytesInBuffer = parseInt(BUFFER_SIZE / bytes); |
| 35 | + // var threshold = parseInt(bytesInBuffer * 0.85); |
| 36 | + var bytesInBuffer = Math.floor(BUFFER_SIZE / bytes); |
| 37 | + var threshold = Math.floor(bytesInBuffer * 0.85); |
| 38 | + |
| 39 | + if (!threshold) { |
| 40 | + return crypto.randomBytes(bytes); |
| 41 | + } |
| 42 | + |
| 43 | + if (this.bytesBufferIndex == null) { |
| 44 | + this.bytesBufferIndex = -1; |
| 45 | + } |
| 46 | + |
| 47 | + if (this.bytesBufferIndex == bytesInBuffer) { |
| 48 | + this.bytesBuffer = null; |
| 49 | + this.bytesBufferIndex = -1; |
| 50 | + } |
| 51 | + |
| 52 | + // No buffered bytes available or index above threshold |
| 53 | + if (this.bytesBufferIndex == -1 || this.bytesBufferIndex > threshold) { |
| 54 | + if (!this.isGeneratingBytes) { |
| 55 | + this.isGeneratingBytes = true; |
| 56 | + crypto.randomBytes(BUFFER_SIZE, function (err, bytes) { |
| 57 | + self.bytesBuffer = bytes; |
| 58 | + self.bytesBufferIndex = 0; |
| 59 | + self.isGeneratingBytes = false; |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + // Fall back to sync call when no buffered bytes are available |
| 64 | + if (this.bytesBufferIndex == -1) { |
| 65 | + return crypto.randomBytes(bytes); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + var result = this.bytesBuffer.slice( |
| 70 | + bytes * this.bytesBufferIndex, |
| 71 | + bytes * (this.bytesBufferIndex + 1), |
| 72 | + ); |
| 73 | + this.bytesBufferIndex++; |
| 74 | + |
| 75 | + return result; |
| 76 | +}; |
| 77 | + |
| 78 | +/** |
| 79 | + * Generates a base64 id |
| 80 | + * |
| 81 | + * (Original version from socket.io <http://socket.io>) |
| 82 | + */ |
| 83 | + |
| 84 | +Base64Id.prototype.generateId = function () { |
| 85 | + var rand = Buffer.alloc(15); // multiple of 3 for base64 |
| 86 | + if (!rand.writeInt32BE) { |
| 87 | + return ( |
| 88 | + Math.abs((Math.random() * Math.random() * Date.now()) | 0).toString() + |
| 89 | + Math.abs((Math.random() * Math.random() * Date.now()) | 0).toString() |
| 90 | + ); |
| 91 | + } |
| 92 | + this.sequenceNumber = (this.sequenceNumber + 1) | 0; |
| 93 | + rand.writeInt32BE(this.sequenceNumber, 11); |
| 94 | + // @ts-expect-error |
| 95 | + if (crypto.randomBytes) { |
| 96 | + this.getRandomBytes(12).copy(rand); |
| 97 | + } else { |
| 98 | + // not secure for node 0.4 |
| 99 | + [0, 4, 8].forEach(function (i) { |
| 100 | + rand.writeInt32BE((Math.random() * Math.pow(2, 32)) | 0, i); |
| 101 | + }); |
| 102 | + } |
| 103 | + return rand.toString("base64").replace(/\//g, "_").replace(/\+/g, "-"); |
| 104 | +}; |
| 105 | + |
| 106 | +/** |
| 107 | + * Export |
| 108 | + */ |
| 109 | + |
| 110 | +export const base64id = new Base64Id(); |
0 commit comments