-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpg-lo.js
258 lines (202 loc) · 6.84 KB
/
pg-lo.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* A custom implementation of pg-large-object
* @author Joris van der Wel <[email protected]>
* @contributor Peter Sirka <[email protected]>
*/
const NOOP = function(){};
const Stream = require('stream');
const BUFFERSIZE = 16384;
const WRITESTREAM = { highWaterMark: BUFFERSIZE, decodeStrings: true, objectMode: false };
const READSTREAM = { highWaterMark: BUFFERSIZE, encoding: null, objectMode: false };
var LargeObject = function(client, oid, fd) {
this._client = client;
this.oid = oid;
this._fd = fd;
};
LargeObject.SEEK_SET = 0;
LargeObject.SEEK_CUR = 1;
LargeObject.SEEK_END = 2;
LargeObject.prototype.close = function(callback) {
var self = this;
self._client.query({ name: 'npg_lo_close', text: 'SELECT lo_close($1) as ok', values: [self._fd] }, callback);
return self;
};
LargeObject.prototype.read = function(length, callback) {
var self = this;
self._client.query({ name: 'npg_loread', text:'SELECT loread($1, $2) as data', values: [self._fd, length] }, function(err, response) {
if (err)
return callback(err);
var data = response.rows[0].data;
callback(null, data);
});
return self;
};
LargeObject.prototype.write = function(buffer, callback) {
var self = this;
self._client.query({ name: 'npg_lowrite', text:'SELECT lowrite($1, $2)', values: [self._fd, buffer] }, callback);
return self;
};
LargeObject.prototype.seek = function(position, ref, callback) {
var self = this;
self._client.query({ name: 'npg_lo_lseek64', text: 'SELECT lo_lseek' + self.plusql + '($1, $2, $3) as location', values: [self._fd, position, ref] }, function(err, response) {
if (err)
return callback(err);
var location = response.rows[0].location;
callback(null, location);
});
return self;
};
LargeObject.prototype.tell = function(callback) {
var self = this;
self._client.query({ name: 'npg_lo_tell64', text: 'SELECT lo_tell' + self.plusql + '($1) as location', values: [self._fd] }, function(err, response) {
if (err)
return callback(err);
var location = response.rows[0].location;
callback(null, location);
});
return self;
};
LargeObject.prototype.size = function(callback) {
var self = this;
self._client.query({ name: 'npg_size', text: 'SELECT lo_lseek' + self.plusql + '($1, location, 0), seek.size FROM (SELECT lo_lseek' + self.plusql + '($1, 0, 2) AS SIZE, tell.location FROM (SELECT lo_tell' + self.plusql + '($1) AS location) tell) seek', values: [self._fd] }, function(err, response) {
if (err)
return callback(err);
var size = response.rows[0].size;
callback(null, size);
});
return self;
};
LargeObject.prototype.truncate = function(length, callback) {
var self = this;
self._client.query({ name: 'npg_lo_truncate' + self.plusql, text:'SELECT lo_truncate' + self.plusql + '($1, $2)', values: [self._fd, length]}, callback);
return self;
};
LargeObject.prototype.getReadableStream = function(bufferSize) {
return new ReadStream(this, bufferSize);
};
LargeObject.prototype.getWritableStream = function(bufferSize) {
return new WriteStream(this, bufferSize);
};
var LargeObjectManager = function(client) {
this._client = client;
};
LargeObjectManager.WRITE = 0x00020000;
LargeObjectManager.READ = 0x00040000;
LargeObjectManager.READWRITE = 0x00020000 | 0x00040000;
LargeObjectManager.prototype.open = function(oid, mode, callback) {
if (!oid)
throw 'Illegal Argument';
var self = this;
self._client.query({ name: 'npg_lo_open', text:'SELECT lo_open($1, $2) AS fd, current_setting(\'server_version_num\') as version', values: [oid, mode]}, function(err, response) {
if (err)
return callback(err);
var lo = new LargeObject(self._client, oid, response.rows[0].fd);
lo.oldversion = response.rows[0].version < 90300;
lo.plusql = lo.oldversion ? '' : '64';
callback(null, lo);
});
return self;
};
LargeObjectManager.prototype.create = function(callback) {
this._client.query({ name: 'npg_lo_creat', text:'SELECT lo_creat($1) AS oid', values: [LargeObjectManager.READWRITE]}, function(err, response) {
if (err)
return callback(err);
var oid = response.rows[0].oid;
callback(null, oid);
});
return this;
};
LargeObjectManager.prototype.unlink = function(oid, callback) {
if (!oid)
throw 'Illegal Argument';
this._client.query({ name: 'npg_lo_unlink', text:'SELECT lo_unlink($1) as ok', values: [oid]}, callback);
return this;
};
LargeObjectManager.prototype.readStream = function(oid, bufferSize, callback) {
if (typeof(bufferSize) === 'function') {
callback = bufferSize;
bufferSize = undefined;
}
var self = this;
self.open(oid, LargeObjectManager.READ, function(err, obj) {
if (err)
return callback(err);
obj.size(function(err, size) {
if (err)
return callback(err);
if (size === '0')
return callback(new Error('Stream is empty.'), size, null);
var stream = obj.getReadableStream(bufferSize);
stream.on('error', function() {
obj.close(NOOP);
});
stream.on('end', function() {
obj.close(NOOP);
});
callback(null, size, stream);
});
});
return self;
};
LargeObjectManager.prototype.writeStream = function(bufferSize, callback) {
if (typeof(bufferSize) === 'function') {
callback = bufferSize;
bufferSize = undefined;
}
var self = this;
self.create(function(err, oid) {
if (err)
return callback(err);
self.open(oid, LargeObjectManager.WRITE, function(err, obj) {
if (err)
return callback(err);
var stream = obj.getWritableStream(bufferSize);
stream.on('error', function() {
obj.close(NOOP);
});
stream.on('finish', function() {
obj.close(NOOP);
});
callback(null, oid, stream);
});
});
return self;
};
var WriteStream = function(largeObject, bufferSize) {
if (bufferSize && bufferSize !== BUFFERSIZE)
WRITESTREAM.bufferSize = bufferSize;
else if (WRITESTREAM.bufferSize !== BUFFERSIZE)
WRITESTREAM.bufferSize = BUFFERSIZE;
Stream.Writable.call(this, WRITESTREAM);
this._largeObject = largeObject;
};
WriteStream.prototype = Object.create(Stream.Writable.prototype);
WriteStream.prototype._write = function(chunk, encoding, callback) {
if (!Buffer.isBuffer(chunk))
throw 'Illegal Argument';
this._largeObject.write(chunk, callback);
};
var ReadStream = function(largeObject, bufferSize) {
if (bufferSize && bufferSize !== BUFFERSIZE)
READSTREAM.bufferSize = bufferSize;
else if (READSTREAM.bufferSize !== BUFFERSIZE)
READSTREAM.bufferSize = BUFFERSIZE;
Stream.Readable.call(this, READSTREAM);
this._largeObject = largeObject;
};
ReadStream.prototype = Object.create(Stream.Readable.prototype);
ReadStream.prototype._read = function(length) {
if (length <= 0)
throw 'Illegal Argument';
var self = this;
self._largeObject.read(length, function(error, data) {
if (error)
return self.emit('error', error);
self.push(data);
if (data.length < length)
self.push(null); // the large object has no more data left
});
};
exports.create = function(client) {
return new LargeObjectManager(client);
};