-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs-polyfill.js
More file actions
43 lines (37 loc) · 1.02 KB
/
Copy pathfs-polyfill.js
File metadata and controls
43 lines (37 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// fs-polyfill.js
// goal is to add webtorrent as a cdn media service but with nextjs get this error `Module not found: Can't resolve 'dgram'` not mvp enough to worry about yet
const { Readable } = require('stream');
class FsReadStream extends Readable {
constructor(file, options) {
super(options);
this.file = file;
this.position = 0;
}
_read(size) {
if (this.position >= this.file.length) {
this.push(null);
return;
}
const chunk = this.file.slice(this.position, this.position + size);
this.push(chunk);
this.position += chunk.length;
}
}
module.exports = {
createReadStream: (file, options) => new FsReadStream(file, options),
readFile: (file, options, callback) => {
if (typeof options === 'function') {
callback = options;
options = {};
}
if (typeof callback !== 'function') {
throw new Error('callback must be a function');
}
try {
const data = file;
callback(null, data);
} catch (err) {
callback(err);
}
},
};