Skip to content

Commit 6bb2e7f

Browse files
refactor(sio): internalize base64id dependency
This dependency is a one-file package used to generate Base64 identifiers. Repository: https://github.com/faeldt/base64id
1 parent a80711a commit 6bb2e7f

4 files changed

Lines changed: 112 additions & 12 deletions

File tree

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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();

packages/socket.io/lib/socket.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import type {
2525
Session,
2626
SocketId,
2727
} from "socket.io-adapter";
28-
import base64id from "base64id";
28+
import { base64id } from "./contrib/base64id";
2929
import { BroadcastOperator } from "./broadcast-operator";
3030
import {
3131
DisconnectReason,

packages/socket.io/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"types": "./dist/index.d.ts",
3131
"import": "./wrapper.mjs",
3232
"require": "./dist/index.js"
33-
},
33+
},
3434
"./package.json": "./package.json"
3535
},
3636
"types": "./dist/index.d.ts",
@@ -54,7 +54,6 @@
5454
},
5555
"dependencies": {
5656
"accepts": "~1.3.4",
57-
"base64id": "~2.0.0",
5857
"cors": "~2.8.5",
5958
"debug": "~4.4.1",
6059
"engine.io": "~6.6.0",

0 commit comments

Comments
 (0)