Skip to content

Commit 5b885cf

Browse files
add encryption func
1 parent f9249c0 commit 5b885cf

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

lib/utils/zip-stream.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const events = require('events');
22
const JSZip = require('jszip');
3+
const officeCrypto = require('officecrypto-tool');
34

45
const StreamBuf = require('./stream-buf');
56
const {stringToBuffer} = require('./browser-buffer-encode');
@@ -35,8 +36,12 @@ class ZipWriter extends events.EventEmitter {
3536
}
3637
}
3738

38-
async finalize() {
39-
const content = await this.zip.generateAsync(this.options);
39+
async finalize(options) {
40+
const totalOptions = {...this.options, ...options};
41+
let content = await this.zip.generateAsync(totalOptions);
42+
if (totalOptions.password) {
43+
content = officeCrypto.encrypt(content, {password: totalOptions.password});
44+
}
4045
this.stream.end(content);
4146
this.emit('finish');
4247
}

lib/xlsx/xlsx.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -588,13 +588,13 @@ class XLSX {
588588
});
589589
}
590590

591-
_finalize(zip) {
591+
_finalize(zip, options) {
592592
return new Promise((resolve, reject) => {
593593
zip.on('finish', () => {
594594
resolve(this);
595595
});
596596
zip.on('error', reject);
597-
zip.finalize();
597+
zip.finalize(options);
598598
});
599599
}
600600

@@ -666,7 +666,7 @@ class XLSX {
666666
await this.addMedia(zip, model);
667667
await Promise.all([this.addApp(zip, model), this.addCore(zip, model)]);
668668
await this.addWorkbook(zip, model);
669-
return this._finalize(zip);
669+
return this._finalize(zip, options);
670670
}
671671

672672
writeFile(filename, options) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const {PassThrough} = require('readable-stream');
2+
const express = require('express');
3+
const got = require('got');
4+
const testutils = require('../utils/index');
5+
6+
const Excel = verquire('exceljs');
7+
8+
const password = '123456';
9+
10+
describe('Express test workbook.xlsx.write encryption', () => {
11+
let server;
12+
before(() => {
13+
const app = express();
14+
app.get('/workbook', (req, res) => {
15+
const wb = testutils.createTestBook(new Excel.Workbook(), 'xlsx');
16+
res.setHeader(
17+
'Content-Type',
18+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
19+
);
20+
res.setHeader('Content-Disposition', 'attachment; filename=Report.xlsx');
21+
wb.xlsx.write(res, {password}).then(() => {
22+
res.end();
23+
});
24+
});
25+
server = app.listen(3003);
26+
});
27+
28+
after(() => {
29+
server.close();
30+
});
31+
32+
it('downloads a workbook, workbook.xlsx.write encryption successful', async function() {
33+
this.timeout(5000);
34+
const res = got.stream('http://127.0.0.1:3003/workbook', {
35+
decompress: false,
36+
});
37+
const wb2 = new Excel.Workbook();
38+
// TODO: Remove passThrough with got 10+ (requires node v10+)
39+
await wb2.xlsx.read(res.pipe(new PassThrough()), {password});
40+
testutils.checkTestBook(wb2, 'xlsx');
41+
});
42+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const ExcelJS = verquire('exceljs');
2+
3+
const test = './spec/integration/data/new-issue-2-test-encryption.xlsx';
4+
5+
describe('github issues', () => {
6+
describe('github issues encrypted xlsx ', () => {
7+
it('workbook.xlsx.writeFile, ecma376_agile encryption successful', async () => {
8+
const password = '123456';
9+
const writeWorkbook = new ExcelJS.Workbook();
10+
writeWorkbook.addWorksheet('Sheet1');
11+
await writeWorkbook.xlsx.writeFile(test, {password});
12+
13+
const workbook = new ExcelJS.Workbook();
14+
await workbook.xlsx.readFile(test, {
15+
password,
16+
});
17+
const sheetName = workbook.getWorksheet(1).name;
18+
expect(sheetName).to.equal('Sheet1');
19+
}).timeout(10000);
20+
21+
it('workbook.xlsx.writeBuffer, ecma376_agile encryption successful', async () => {
22+
const password = '123456';
23+
const writeWorkbook = new ExcelJS.Workbook();
24+
writeWorkbook.addWorksheet('Sheet1');
25+
const encryptBuffer = await writeWorkbook.xlsx.writeBuffer({password});
26+
27+
const workbook = new ExcelJS.Workbook();
28+
await workbook.xlsx.load(encryptBuffer, {
29+
password,
30+
});
31+
const sheetName = workbook.getWorksheet(1).name;
32+
expect(sheetName).to.equal('Sheet1');
33+
}).timeout(10000);
34+
});
35+
});

0 commit comments

Comments
 (0)