Skip to content

Commit 5d88b87

Browse files
authored
Merge pull request #648 from share/inherits
♻️ Try loading `'inherits'` library if `'util'` is missing
2 parents 5259d0e + bf8e735 commit 5d88b87

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

lib/op-stream.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var inherits = require('util').inherits;
21
var Readable = require('stream').Readable;
32
var util = require('./util');
43

@@ -10,7 +9,7 @@ function OpStream() {
109
}
1110
module.exports = OpStream;
1211

13-
inherits(OpStream, Readable);
12+
util.inherits(OpStream, Readable);
1413

1514
// This function is for notifying us that the stream is empty and needs data.
1615
// For now, we'll just ignore the signal and assume the reader reads as fast

lib/stream-socket.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var Duplex = require('stream').Duplex;
2-
var inherits = require('util').inherits;
32
var logger = require('./logger');
43
var util = require('./util');
54

@@ -47,7 +46,7 @@ function ServerStream(socket) {
4746
socket.close('stopped');
4847
});
4948
}
50-
inherits(ServerStream, Duplex);
49+
util.inherits(ServerStream, Duplex);
5150

5251
ServerStream.prototype.isServer = true;
5352

lib/util.js

+12
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,15 @@ Object.getOwnPropertyNames(Object.prototype).forEach(function(prop) {
113113
exports.isDangerousProperty = function(propName) {
114114
return propName === '__proto__' || objectProtoPropNames[propName];
115115
};
116+
117+
try {
118+
var util = require('util');
119+
if (typeof util.inherits !== 'function') throw new Error('Could not find util.inherits()');
120+
exports.inherits = util.inherits;
121+
} catch (e) {
122+
try {
123+
exports.inherits = require('inherits');
124+
} catch (e) {
125+
throw new Error('If running sharedb in a browser, please install the "inherits" or "util" package');
126+
}
127+
}

0 commit comments

Comments
 (0)