-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
142 lines (114 loc) · 3.14 KB
/
index.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const stream = require('stream');
const timers = require('timers');
function MemoryBlobStore() {
if (!(this instanceof MemoryBlobStore)) {
return new MemoryBlobStore();
}
this.store = {};
this.writing = {};
this.readers = {};
return this;
}
function createWriteStream(opts, cb) {
const self = this;
cb = cb || function noop() {};
let key = opts;
if (typeof opts === 'object') {
key = key.name || key.key;
}
if (typeof key !== 'string') {
return timers.setImmediate(cb, 'Must include a key as a string');
}
const ws = new stream.Writable();
if (self.writing[key]) {
self._closeReaders(key);
}
self.readers[key] = [];
self.writing[key] = true;
self.store[key] = Buffer(0);
ws._write = function write(chunk, encoding, done) {
if (!self.store[key]) {
return timers.setImmediate(done, new Error('Blob does not exist'));
}
const buffer = Buffer.from(chunk);
self.store[key] = Buffer.concat([self.store[key], buffer]);
self._writeToReaders(key, buffer);
return timers.setImmediate(done);
};
ws.on('finish', () => {
delete self.writing[key];
self._closeReaders(key);
return timers.setImmediate(cb, null, { key });
});
return ws;
}
MemoryBlobStore.prototype.createWriteStream = createWriteStream;
function _writeToReaders(key, buffer) {
if(!this.readers[key]) {
return null;
}
for (let i = 0; i < this.readers[key].length; i += 1) {
this.readers[key][i].push(buffer);
}
}
MemoryBlobStore.prototype._writeToReaders = _writeToReaders;
function _closeReaders(key) {
if(!this.readers[key]) {
return null;
}
for (let i = 0; i < this.readers[key].length; i += 1) {
this.readers[key][i].push(null);
}
delete this.readers[key];
}
MemoryBlobStore.prototype._closeReaders = _closeReaders;
function remove(opts, cb) {
cb = cb || function noop() {};
let key = opts;
if (typeof key === 'object') {
key = key.key;
}
if (typeof key !== 'string') {
return timers.setImmediate(cb, 'Must include a key as a string');
}
delete this.store[key];
return timers.setImmediate(cb);
}
MemoryBlobStore.prototype.remove = remove;
function createReadStream(opts) {
const self = this;
let key = opts;
if (typeof key === 'object') {
key = key.key;
}
if (typeof key !== 'string') {
return new Error('Must include a key as a string');
}
const rs = new stream.Readable();
rs.push(self.store[key]);
if (!self.writing[key]) {
rs.push(null);
delete self.readers[key];
} else {
self.readers[key].push(rs);
}
if (!self.store[key]) {
timers.setImmediate(rs.emit.bind(rs), 'error', new Error('Blob does not exist.'));
}
rs._read = function noop() {};
return rs;
}
MemoryBlobStore.prototype.createReadStream = createReadStream;
function exists(opts, cb) {
cb = cb || function noop() {};
let key = opts;
if (typeof key === 'object') {
key = key.key;
}
if (typeof key !== 'string') {
return timers.setImmediate(cb, new Error('key must be provided'));
}
return timers.setImmediate(cb, null, this.store[key] !== undefined);
}
MemoryBlobStore.prototype.exists = exists;
module.exports = MemoryBlobStore;