Skip to content

Commit f6d8e3a

Browse files
committed
Remove LZString compression for data storage
1 parent c904083 commit f6d8e3a

5 files changed

Lines changed: 28 additions & 37 deletions

File tree

lib/models/note.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ module.exports = function (sequelize, DataTypes) {
124124
var body = fs.readFileSync(filePath, 'utf8');
125125
var contentLength = body.length;
126126
var title = Note.parseNoteTitle(body);
127-
body = LZString.compressToBase64(body);
128-
title = LZString.compressToBase64(title);
129127
if (fsModifiedTime.isAfter(dbModifiedTime) && note.content !== body) {
130128
note.update({
131129
title: title,
@@ -135,14 +133,14 @@ module.exports = function (sequelize, DataTypes) {
135133
sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
136134
if (err) return _callback(err, null);
137135
// update authorship on after making revision of docs
138-
var patch = dmp.patch_fromText(LZString.decompressFromBase64(revision.patch));
136+
var patch = dmp.patch_fromText(revision.patch);
139137
var operations = Note.transformPatchToOperations(patch, contentLength);
140-
var authorship = note.authorship ? JSON.parse(LZString.decompressFromBase64(note.authorship)) : [];
138+
var authorship = note.authorship;
141139
for (var i = 0; i < operations.length; i++) {
142140
authorship = Note.updateAuthorshipByOperation(operations[i], null, authorship);
143141
}
144142
note.update({
145-
authorship: LZString.compressToBase64(JSON.stringify(authorship))
143+
authorship: JSON.stringify(authorship)
146144
}).then(function (note) {
147145
return callback(null, note.id);
148146
}).catch(function (err) {
@@ -264,10 +262,7 @@ module.exports = function (sequelize, DataTypes) {
264262
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ');
265263
},
266264
decodeTitle: function (title) {
267-
var decodedTitle = LZString.decompressFromBase64(title);
268-
if (decodedTitle) title = decodedTitle;
269-
else title = 'Untitled';
270-
return title;
265+
return title ? title : 'Untitled';
271266
},
272267
generateWebTitle: function (title) {
273268
title = !title || title == "Untitled" ? "HackMD - Collaborative markdown notes" : title + " - HackMD";
@@ -496,8 +491,8 @@ module.exports = function (sequelize, DataTypes) {
496491
if (Note.checkFileExist(filePath)) {
497492
var fsCreatedTime = moment(fs.statSync(filePath).ctime);
498493
body = fs.readFileSync(filePath, 'utf8');
499-
note.title = LZString.compressToBase64(Note.parseNoteTitle(body));
500-
note.content = LZString.compressToBase64(body);
494+
note.title = Note.parseNoteTitle(body);
495+
note.content = body;
501496
if (filePath !== config.defaultnotepath) {
502497
note.createdAt = fsCreatedTime;
503498
}

lib/models/revision.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
// external modules
44
var Sequelize = require("sequelize");
5-
var LZString = require('lz-string');
65
var async = require('async');
76
var moment = require('moment');
87
var childProcess = require('child_process');
@@ -214,7 +213,7 @@ module.exports = function (sequelize, DataTypes) {
214213
Revision.create({
215214
noteId: note.id,
216215
lastContent: note.content,
217-
length: LZString.decompressFromBase64(note.content).length,
216+
length: note.content.length,
218217
authorship: note.authorship
219218
}).then(function (revision) {
220219
Revision.finishSaveNoteRevision(note, revision, callback);
@@ -223,8 +222,8 @@ module.exports = function (sequelize, DataTypes) {
223222
});
224223
} else {
225224
var latestRevision = revisions[0];
226-
var lastContent = LZString.decompressFromBase64(latestRevision.content || latestRevision.lastContent);
227-
var content = LZString.decompressFromBase64(note.content);
225+
var lastContent = latestRevision.content || latestRevision.lastContent;
226+
var content = note.content;
228227
sendDmpWorker({
229228
msg: 'create patch',
230229
lastDoc: lastContent,
@@ -244,9 +243,9 @@ module.exports = function (sequelize, DataTypes) {
244243
} else {
245244
Revision.create({
246245
noteId: note.id,
247-
patch: LZString.compressToBase64(patch),
246+
patch: patch,
248247
content: note.content,
249-
length: LZString.decompressFromBase64(note.content).length,
248+
length: note.content.length,
250249
authorship: note.authorship
251250
}).then(function (revision) {
252251
// clear last revision content to reduce db size

lib/realtime.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,10 @@ function finishUpdateNote(note, _note, callback) {
152152
if (!note || !note.server) return callback(null, null);
153153
var body = note.server.document;
154154
var title = note.title = models.Note.parseNoteTitle(body);
155-
title = LZString.compressToBase64(title);
156-
body = LZString.compressToBase64(body);
157155
var values = {
158156
title: title,
159157
content: body,
160-
authorship: LZString.compressToBase64(JSON.stringify(note.authorship)),
158+
authorship: note.authorship,
161159
lastchangeuserId: note.lastchangeuser,
162160
lastchangeAt: Date.now()
163161
};
@@ -459,7 +457,7 @@ function startConnection(socket) {
459457
var lastchangeuser = note.lastchangeuserId;
460458
var lastchangeuserprofile = note.lastchangeuser ? models.User.getProfile(note.lastchangeuser) : null;
461459

462-
var body = LZString.decompressFromBase64(note.content);
460+
var body = note.content;
463461
var createtime = note.createdAt;
464462
var updatetime = note.lastchangeAt;
465463
var server = new ot.EditorSocketIOServer(body, [], noteId, ifMayEdit, operationCallback);
@@ -479,7 +477,7 @@ function startConnection(socket) {
479477
notes[noteId] = {
480478
id: noteId,
481479
alias: note.alias,
482-
title: LZString.decompressFromBase64(note.title),
480+
title: note.title,
483481
owner: owner,
484482
ownerprofile: ownerprofile,
485483
permission: note.permission,
@@ -491,7 +489,7 @@ function startConnection(socket) {
491489
updatetime: moment(updatetime).valueOf(),
492490
server: server,
493491
authors: authors,
494-
authorship: note.authorship ? JSON.parse(LZString.decompressFromBase64(note.authorship)) : []
492+
authorship: note.authorship
495493
};
496494

497495
return finishConnection(socket, notes[noteId], users[socket.id]);

lib/response.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function showIndex(req, res, next) {
7575
}
7676

7777
function responseHackMD(res, note) {
78-
var body = LZString.decompressFromBase64(note.content);
78+
var body = note.content;
7979
var meta = null;
8080
try {
8181
meta = models.Note.parseMeta(metaMarked(body).meta);
@@ -191,7 +191,7 @@ function showPublishNote(req, res, next) {
191191
if (!note) {
192192
return response.errorNotFound(res);
193193
}
194-
var body = LZString.decompressFromBase64(note.content);
194+
var body = note.content;
195195
var meta = null;
196196
var markdown = null;
197197
try {
@@ -248,7 +248,7 @@ function actionSlide(req, res, note) {
248248
}
249249

250250
function actionDownload(req, res, note) {
251-
var body = LZString.decompressFromBase64(note.content);
251+
var body = note.content;
252252
var title = models.Note.decodeTitle(note.title);
253253
var filename = title;
254254
filename = encodeURIComponent(filename);
@@ -265,7 +265,7 @@ function actionDownload(req, res, note) {
265265
}
266266

267267
function actionInfo(req, res, note) {
268-
var body = LZString.decompressFromBase64(note.content);
268+
var body = note.content;
269269
var meta = null;
270270
var markdown = null;
271271
try {
@@ -297,7 +297,7 @@ function actionInfo(req, res, note) {
297297
}
298298

299299
function actionPDF(req, res, note) {
300-
var body = LZString.decompressFromBase64(note.content);
300+
var body = note.content;
301301
try {
302302
body = metaMarked(body).markdown;
303303
} catch(err) {
@@ -479,7 +479,7 @@ function githubActionGist(req, res, note) {
479479
if (!error && httpResponse.statusCode == 200) {
480480
var access_token = body.access_token;
481481
if (access_token) {
482-
var content = LZString.decompressFromBase64(note.content);
482+
var content = note.content;
483483
var title = models.Note.decodeTitle(note.title);
484484
var filename = title.replace('/', ' ') + '.md';
485485
var gist = {
@@ -579,7 +579,7 @@ function showPublishSlide(req, res, next) {
579579
if (!note) {
580580
return response.errorNotFound(res);
581581
}
582-
var body = LZString.decompressFromBase64(note.content);
582+
var body = note.content;
583583
var meta = null;
584584
var markdown = null;
585585
try {

lib/workers/dmpWorker.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// external modules
2-
var LZString = require('lz-string');
32
var DiffMatchPatch = require('diff-match-patch');
43
var dmp = new DiffMatchPatch();
54

@@ -80,10 +79,10 @@ function getRevision(revisions, count) {
8079
for (var i = 0; i < count; i++) {
8180
var revision = revisions[i];
8281
if (i == 0) {
83-
startContent = LZString.decompressFromBase64(revision.content || revision.lastContent);
82+
startContent = revision.content || revision.lastContent;
8483
}
8584
if (i != count - 1) {
86-
var patch = dmp.patch_fromText(LZString.decompressFromBase64(revision.patch));
85+
var patch = dmp.patch_fromText(revision.patch);
8786
applyPatches = applyPatches.concat(patch);
8887
}
8988
lastPatch = revision.patch;
@@ -105,11 +104,11 @@ function getRevision(revisions, count) {
105104
for (var i = l; i >= count - 1; i--) {
106105
var revision = revisions[i];
107106
if (i == l) {
108-
startContent = LZString.decompressFromBase64(revision.lastContent);
107+
startContent = revision.lastContent;
109108
authorship = revision.authorship;
110109
}
111110
if (revision.patch) {
112-
var patch = dmp.patch_fromText(LZString.decompressFromBase64(revision.patch));
111+
var patch = dmp.patch_fromText(revision.patch);
113112
applyPatches = applyPatches.concat(patch);
114113
}
115114
lastPatch = revision.patch;
@@ -123,8 +122,8 @@ function getRevision(revisions, count) {
123122
}
124123
var data = {
125124
content: finalContent,
126-
patch: dmp.patch_fromText(LZString.decompressFromBase64(lastPatch)),
127-
authorship: authorship ? JSON.parse(LZString.decompressFromBase64(authorship)) : null
125+
patch: dmp.patch_fromText(lastPatch),
126+
authorship: authorship
128127
};
129128
var ms_end = (new Date()).getTime();
130129
if (config.debug) {

0 commit comments

Comments
 (0)