-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
378 lines (345 loc) · 10.1 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*!
* node tDoc API wrapper
* (c) 2014-2018 Lapo Luchini <[email protected]>
*/
'use strict';
const
crypto = require('crypto'),
Q = require('./lib/promise'), // we're currently using Bluebird, but Q is a shorter name
req = require('superagent'),
reProto = /^(https?):/,
reEtag = /^"([0-9A-F]+)[-"]/;
function TDoc (address, username, password) {
this.address = address.replace(/\/?$/, '/'); // check that it includes the trailing slash
this.username = username;
this.password = password;
const proto = reProto.exec(address);
if (!proto)
throw new Error('Unsupported protocol.');
this.agent = new (require(proto[1])).Agent({
keepAlive: true, // keep alive connections for reuse
keepAliveMsecs: 5000, // for up to 5 seconds
maxSockets: 4, // do not use more than 4 parallel connections
});
}
TDoc.Promise = Q;
TDoc.Error = function (method, err) {
// inspired by: http://stackoverflow.com/a/8460753/166524
if ('captureStackTrace' in Error)
Error.captureStackTrace(this, this.constructor);
this.name = 'TDoc.Error';
this.method = method;
this.status = 0 | err.status;
try {
//TODO: does this actually happen?
if ('code' in err.response.body)
err = err.response.body;
} catch (e) {
// ignore
}
this.code = 0 | err.code;
this.message = err.message;
this.additional = err.additional || [];
};
TDoc.Error.prototype = Object.create(Error.prototype);
TDoc.Error.prototype.constructor = TDoc.Error;
TDoc.longStack = function (val) {
if (!val)
console.log('WARNING: long stack traces are always enabled since version 0.2.0');
};
function forceNumber (n) {
return +n;
}
function nameValue2Object (arr) {
const o = {};
arr.forEach(function (e) {
o[e.name] = e.value;
});
return o;
}
function massageDoc (doc) {
if (Array.isArray(doc.metadata)) { // old format, used up to tDoc r13584
doc.lotto = +doc.lotto;
doc.metadata = nameValue2Object(doc.metadata);
}
return doc;
}
function massageDoctype (doctypes) {
doctypes.forEach(function (dt) {
if (typeof dt.custom == 'string') // tDoc r14171 returns it as a string, but will change in the future
dt.custom = JSON.parse(dt.custom);
});
return doctypes;
}
function GET (me, method, data) {
return Q.resolve(req
.get(me.address + method)
.agent(me.agent)
.auth(me.username, me.password)
.query(data)
).catch(function (err) {
throw new TDoc.Error(method, err);
}).then(function (resp) {
const data = resp.body;
if (typeof data == 'object' && 'message' in data)
throw new TDoc.Error(method, resp);
if (resp.status >= 400)
throw new TDoc.Error(method, resp);
return data;
});
}
function GETbuffer (me, method, data) {
return Q.resolve(req
.get(me.address + method)
.agent(me.agent)
.auth(me.username, me.password)
.buffer(true).parse(req.parse.image) // necessary to have resp.body as a Buffer
.query(data)
).catch(function (err) {
throw new TDoc.Error(method, err);
}).then(function (resp) {
if ('etag' in resp.header) {
const m = reEtag.exec(resp.header.etag);
if (m) {
const declared = m[1];
const algo = declared.length < 64 ? 'sha1' : 'sha256';
const calc = crypto.createHash(algo).update(resp.body).digest('hex').toUpperCase();
if (calc != declared)
throw new Error('Hash value mismatch.');
}
}
return resp.body;
});
}
function POST (me, method, data) {
return Q.resolve(req
.post(me.address + method)
.agent(me.agent)
.auth(me.username, me.password)
.type('form')
.send(data)
).catch(function (err) {
throw new TDoc.Error(method, err);
}).then(function (resp) {
const data = resp.body;
if (typeof data == 'object' && 'message' in data)
throw new TDoc.Error(method, data.code, data.message);
return data;
});
}
function documentPOST (me, method, data, document) {
const r = req
.post(me.address + method)
.agent(me.agent)
.auth(me.username, me.password)
.field(data);
if (document)
r.attach('document', document);
return Q.resolve(r
).catch(function (err) {
throw new TDoc.Error(method, err);
}).then(function (resp) {
const data = resp.body;
if (typeof data == 'object' && 'message' in data)
throw new TDoc.Error(method, data.code, data.message);
if (typeof data == 'object' && 'document' in data) {
if ('warning' in data)
data.document.warning = { message: data.warning.shift(), extra: data.warning };
return massageDoc(data.document);
}
throw new Error('Unexpected return value: ' + JSON.stringify(data));
});
}
function parcelPOST (me, method, data) {
return POST(me, method, data).then(function (data) {
if (typeof data == 'object' && 'parcel' in data)
return data.parcel;
throw new Error('Unexpected return value: ' + JSON.stringify(data));
});
}
function commonUploadParams (p) {
const s = {};
if (p.mimetype)
s.mimetype = p.mimetype;
if (p.user)
s.user = p.user;
if (p.company)
s.company = p.company;
if (p.period)
s.period = forceNumber(p.period);
if (p.parcel) // upload only
s.parcel = p.parcel;
if (p.pages)
s.pages = forceNumber(p.pages);
if (p.meta)
s.meta = JSON.stringify(p.meta);
//if (p.alias && p.pin) {
// s.alias = p.alias;
// s.pin = p.pin;
//}
if (p.alias)
s.alias = p.alias;
if (p.pin)
s.pin = p.pin;
if (p.overwrite) // upload only
s.overwrite = 0 | p.overwrite;
if (p.id) // update only
s.id = 0 | p.id;
if ('ready' in p) // as missing value is truthy
s.ready = p.ready ? 1 : 0;
return s;
}
function upload (me, p) {
if (!p.doctype)
return Q.reject(new Error('you need to specify ‘doctype’'));
if (!p.period)
return Q.reject(new Error('you need to specify ‘period’'));
if (!p.meta && p.ready)
return Q.reject(new Error('if the document is ‘ready’ it must contain ‘meta’'));
if (p.ready && (!p.file && !p.data))
return Q.reject(new Error('if the document is ‘ready’ it must have a content as either ‘file’ or ‘data’'));
const s = commonUploadParams(p);
s.doctype = p.doctype;
return documentPOST(me, 'docs/upload', s, p.file || p.data);
}
function update (me, p) {
const s = commonUploadParams(p);
return documentPOST(me, 'docs/update', s, p.file || p.data);
}
function updateMeta (me, p) {
const data = {
meta: p.meta,
value: p.value,
};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return POST(me, 'docs/' + (0 | p.id) + '/meta/update', data).then(massageDoc);
}
function search (me, p) {
const data = {
doctype: p.doctype,
meta: JSON.stringify(p.meta),
};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
if (p.period) data.period = forceNumber(p.period);
if (p.limit) data.limit = forceNumber(p.limit);
if (p.complete) data.complete = 1;
return POST(me, 'docs/search', data).then(function (data) {
if (typeof data == 'object' && 'documents' in data)
return data.documents;
throw new Error('malformed response');
});
}
function document (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GETbuffer(me, 'docs/' + (0 | p.id), data);
}
function documentMeta (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GET(me, 'docs/' + (0 | p.id) + '/meta', data).then(massageDoc);
}
function documentLink (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GET(me, 'docs/' + (0 | p.id) + '/link', data);
}
function documentDelete (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GET(me, 'docs/' + (0 | p.id) + '/delete', data);
}
function searchOne (me, p) {
p.limit = 2; // we need 1 but limit to 2 to know if search was not unique
p.complete = 1; // download each metadata directly to avoid one round-trip
return search(me, p).then(function (data) {
if (data.length != 1)
throw new Error('Search result was not a single document');
return data[0];
});
}
function parcelCreate (me, p) {
const data = {
company: p.company,
doctype: p.doctype,
filename: p.filename,
};
if (p.user) data.user = p.user;
return parcelPOST(me, 'docs/parcel/create', data);
}
function parcelClose (me, p) {
const data = {
parcel: p.id,
};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
if (p.extra) data.extra = p.extra;
return parcelPOST(me, 'docs/parcel/close', data);
}
function parcelDelete (me, p) {
const data = {
parcel: p.id,
};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
if (p.error) data.error = p.error;
if (p.extra) data.extra = p.extra;
return parcelPOST(me, 'docs/parcel/delete', data);
}
function parcelXML (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GETbuffer(me, 'docs/parcel/' + p.id + '.xml', data);
}
function companyList (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GET(me, 'company/list', data);
}
function doctypeList (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
return GET(me, 'doctype/list', data);
}
function doctypeInfo (me, p) {
const data = {};
if (p.user) data.user = p.user;
if (p.company) data.company = p.company;
if (p.doctype) data.doctype = p.doctype;
return GET(me, 'doctype', data).then(massageDoctype);
}
// register nodeified versions in the prototype
[
companyList,
doctypeInfo,
doctypeList,
document,
documentDelete,
documentLink,
documentMeta,
parcelClose,
parcelCreate,
parcelDelete,
parcelXML,
search,
searchOne,
update,
updateMeta,
upload,
].forEach(function (f) {
TDoc.prototype[f.name] = function (p) {
if (typeof p != 'object')
throw new Error('The parameter must be an object.');
return f(this, p).nodeify(p.callback);
};
});
module.exports = TDoc;