Skip to content

Commit 5825052

Browse files
authored
Gzip compression (#167)
1 parent 5cd6650 commit 5825052

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

docs/api/woqlClient.js.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ Common request dispatch function
644644
| apiUrl | <code>string</code> | the server call endpoint |
645645
| [payload] | <code>object</code> | the post body |
646646
| [getDataVersion] | <code>boolean</code> | If true return response with data version |
647+
| [compress] | <code>boolean</code> | If true, compress the data if it is bigger than 1024 bytes |
647648
648649
649650
## generateCommitInfo

lib/dispatchRequest.js

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable camelcase */
2+
const pako = require('pako');
23
const axiosInstance = require('./axiosInstance');
34
const UTILS = require('./utils');
45
const CONST = require('./const');
@@ -38,9 +39,10 @@ function getResultWithDataVersion(response) {
3839
* @param {object} payload data to be transmitted to endpoint
3940
* @param {string} key optional basic auth string to be passed
4041
* @param {object} customHeaders the unique reqID
42+
* @param {boolean} compress If true, compress the data with gzip if its size is bigger than 1024
4143
*/
4244
// eslint-disable-next-line max-len
43-
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, customHeaders = null, getDataVersion = false) {
45+
function DispatchRequest(url, action, payload, local_auth, remote_auth = null, customHeaders = null, getDataVersion = false, compress = false) {
4446
/*
4547
*CORS is only required when trying to fetch data from a browser,
4648
*as browsers by default will block requests to different origins
@@ -102,7 +104,7 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
102104
}
103105

104106
switch (action) {
105-
case CONST.DELETE:
107+
case CONST.DELETE: {
106108
if (payload) {
107109
options.headers = options.headers ? options.headers : {};
108110
options.headers['Content-Type'] = 'application/json; charset=utf-8';
@@ -116,7 +118,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
116118
if (err.response && err.response.data) e.data = err.response.data;
117119
throw e;
118120
});
119-
case CONST.HEAD:
121+
}
122+
case CONST.HEAD: {
120123
return axiosInstance
121124
.head(url, options)
122125
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
@@ -127,7 +130,8 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
127130
}
128131
throw e;
129132
});
130-
case CONST.GET:
133+
}
134+
case CONST.GET: {
131135
if (payload) {
132136
const ext = UTILS.URIEncodePayload(payload);
133137
// eslint-disable-next-line no-param-reassign
@@ -143,8 +147,9 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
143147
}
144148
throw e;
145149
});
150+
}
146151
case CONST.ADD_CSV:
147-
case CONST.INSERT_TRIPLES:
152+
case CONST.INSERT_TRIPLES: {
148153
options.headers = options.headers ? options.headers : {};
149154
options.headers['Content-Type'] = 'application/form-data; charset=utf-8';
150155
return axiosInstance
@@ -155,32 +160,49 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
155160
if (err.response && err.response.data) e.data = err.response.data;
156161
throw e;
157162
});
158-
case CONST.PUT:
163+
}
164+
case CONST.PUT: {
159165
options.headers = options.headers ? options.headers : {};
160166
options.headers['Content-Type'] = 'application/json; charset=utf-8';
167+
let compressedContent = null;
168+
const jsonString = JSON.stringify(payload);
169+
170+
if (jsonString.length > 1024 && compress) {
171+
options.headers['Content-Encoding'] = 'gzip';
172+
compressedContent = pako.gzip(jsonString);
173+
}
161174
return axiosInstance
162-
.put(url, payload, options)
175+
.put(url, compressedContent || payload, options)
163176
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
164177
.catch((err) => {
165178
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
166179
if (err.response && err.response.data) e.data = err.response.data;
167180
throw e;
168181
});
169-
case CONST.QUERY_DOCUMENT:
182+
}
183+
case CONST.QUERY_DOCUMENT: {
170184
options.headers = options.headers ? options.headers : {};
171185
options.headers['X-HTTP-Method-Override'] = 'GET';
172-
// eslint-disable-next-line no-fallthrough
173-
default:
186+
// eslint-disable-next-line no-fallthrough
187+
}
188+
default: {
189+
let compressedContentPost = null;
174190
options.headers = options.headers ? options.headers : {};
175191
options.headers['Content-Type'] = 'application/json; charset=utf-8';
192+
const jsonStringPost = JSON.stringify(payload);
193+
if (jsonStringPost.length > 1024 && compress) {
194+
options.headers['Content-Encoding'] = 'gzip';
195+
compressedContentPost = pako.gzip(jsonStringPost);
196+
}
176197
return axiosInstance
177-
.post(url, payload, options)
198+
.post(url, compressedContentPost || payload, options)
178199
.then((response) => (getDataVersion ? getResultWithDataVersion(response) : response.data))
179200
.catch((err) => {
180201
const e = new Error(ErrorMessage.getAPIErrorMessage(url, options, err));
181202
if (err.response && err.response.data) e.data = err.response.data;
182203
throw e;
183204
});
205+
}
184206
}
185207
}
186208

lib/woqlClient.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,16 @@ WOQLClient.prototype.clonedb = function (cloneSource, newDbId, orgId) {
842842
* @property {string} apiUrl - the server call endpoint
843843
* @property {object} [payload] - the post body
844844
* @property {boolean} [getDataVersion] - If true return response with data version
845+
* @property {boolean} [compress] - If true, compress the data if it is bigger than 1024 bytes
845846
* @returns {Promise} A promise that returns the call response object, or an Error if rejected.
846847
*/
847-
WOQLClient.prototype.dispatch = function (action, apiUrl, payload, getDataVersion) {
848+
WOQLClient.prototype.dispatch = function (
849+
action,
850+
apiUrl,
851+
payload,
852+
getDataVersion,
853+
compress = false,
854+
) {
848855
if (!apiUrl) {
849856
return Promise.reject(
850857
new Error(
@@ -881,6 +888,7 @@ WOQLClient.prototype.dispatch = function (action, apiUrl, payload, getDataVersio
881888
this.remoteAuth(),
882889
this.customHeaders(),
883890
getDataVersion,
891+
compress,
884892
);
885893
// }
886894
};
@@ -983,15 +991,14 @@ WOQLClient.prototype.updateDatabase = function (dbDoc) {
983991
* )
984992
*/
985993

986-
WOQLClient.prototype.addDocument = function (json, params, dbId, message = 'add a new document', lastDataVersion = '', getDataVersion = false) {
994+
WOQLClient.prototype.addDocument = function (json, params, dbId, message = 'add a new document', lastDataVersion = '', getDataVersion = false, compress = false) {
987995
if (dbId) {
988996
this.db(dbId);
989997
}
990998

991999
if (typeof lastDataVersion === 'string' && lastDataVersion !== '') {
9921000
this.customHeaders({ 'TerminusDB-Data-Version': lastDataVersion });
9931001
}
994-
9951002
const docParams = params || {};
9961003
docParams.author = this.author();
9971004
docParams.message = message;
@@ -1000,6 +1007,7 @@ WOQLClient.prototype.addDocument = function (json, params, dbId, message = 'add
10001007
this.connectionConfig.documentURL(docParams),
10011008
json,
10021009
getDataVersion,
1010+
compress,
10031011
);
10041012
};
10051013

@@ -1202,7 +1210,7 @@ WOQLClient.prototype.getDocument = function (params, dbId, branch, lastDataVersi
12021210
);
12031211
*/
12041212

1205-
WOQLClient.prototype.updateDocument = function (json, params, dbId, message = 'update document', lastDataVersion = '', getDataVersion = false) {
1213+
WOQLClient.prototype.updateDocument = function (json, params, dbId, message = 'update document', lastDataVersion = '', getDataVersion = false, compress = false) {
12061214
const docParams = params || {};
12071215
docParams.author = this.author();
12081216
docParams.message = message;
@@ -1217,6 +1225,7 @@ WOQLClient.prototype.updateDocument = function (json, params, dbId, message = 'u
12171225
this.connectionConfig.documentURL(docParams),
12181226
json,
12191227
getDataVersion,
1228+
compress,
12201229
);
12211230
};
12221231

package-lock.json

Lines changed: 13 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"follow-redirects": ">=1.14.8",
2727
"form-data": "^4.0.0",
2828
"node-forge": ">=1.0.0",
29+
"pako": "^2.0.4",
2930
"pathval": ">=1.1.1",
3031
"underscore": ">=1.13.2"
3132
},

0 commit comments

Comments
 (0)