Skip to content

Introduce async/await #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Introduce async/await
  • Loading branch information
alubbe committed Aug 21, 2019
commit f66531dc276bf4cf3b1615478603a77440c82c0a
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prefer-rest-params": ["off"],
"quotes": ["error", "single"],
"semi": ["error", "always"],
"space-before-function-paren": ["error", "never"],
"space-before-function-paren": ["error", {"anonymous": "never", "named": "never", "asyncArrow": "always"}],
"strict": ["off"],
"class-methods-use-this": ["off"],
"no-return-assign": ["off"],
Expand Down
3 changes: 2 additions & 1 deletion .prettier
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"bracketSpacing": false,
"printWidth": 160,
"trailingComma": "all"
"trailingComma": "all",
"bracketSpacing": false
}
28 changes: 11 additions & 17 deletions lib/csv/csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,16 @@ const SpecialValues = {
/* eslint-ensable quote-props */

CSV.prototype = {
readFile(filename, options) {
async readFile(filename, options) {
const self = this;
options = options || {};
let stream;
return utils.fs
.exists(filename)
.then(exists => {
if (!exists) {
throw new Error(`File not found: ${filename}`);
}
stream = fs.createReadStream(filename);
return self.read(stream, options);
})
.then(worksheet => {
stream.close();
return worksheet;
});
if (!(await utils.fs.exists(filename))) {
throw new Error(`File not found: ${filename}`);
}
const stream = fs.createReadStream(filename);
const worksheet = await self.read(stream, options);
stream.close();
return worksheet;
},
read(stream, options) {
options = options || {};
Expand Down Expand Up @@ -163,9 +156,10 @@ CSV.prototype = {

return this.write(stream, options);
},
writeBuffer(options) {
async writeBuffer(options) {
const self = this;
const stream = new StreamBuf();
return self.write(stream, options).then(() => stream.read());
await self.write(stream, options);
return stream.read();
},
};
7 changes: 4 additions & 3 deletions lib/stream/xlsx/sheet-rels-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ SheetRelsWriter.prototype = {
// ================================================================================
_writeOpen() {
this.stream.write(
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">`
);
},
_writeRelationship(relationship) {
Expand All @@ -88,14 +89,14 @@ SheetRelsWriter.prototype = {

if (relationship.TargetMode) {
this.stream.write(
`${'<Relationship' + ' Id="'}${rId}"` +
`<Relationship Id="${rId}"` +
` Type="${relationship.Type}"` +
` Target="${utils.xmlEncode(relationship.Target)}"` +
` TargetMode="${relationship.TargetMode}"` +
'/>'
);
} else {
this.stream.write(`${'<Relationship' + ' Id="'}${rId}"` + ` Type="${relationship.Type}"` + ` Target="${relationship.Target}"` + '/>');
this.stream.write(`<Relationship Id="${rId}" Type="${relationship.Type}" Target="${relationship.Target}"/>`);
}

return rId;
Expand Down
14 changes: 6 additions & 8 deletions lib/stream/xlsx/workbook-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ WorkbookWriter.prototype = {
return PromiseLib.Promise.resolve();
},

commit() {
async commit() {
// commit all worksheets, then add suplimentary files
return this.promise
.then(() => this._commitWorksheets())
.then(() =>
PromiseLib.Promise.all([this.addContentTypes(), this.addApp(), this.addCore(), this.addSharedStrings(), this.addStyles(), this.addWorkbookRels()])
)
.then(() => this.addWorkbook())
.then(() => this._finalize());
await this.promise;
await this._commitWorksheets();
await PromiseLib.Promise.all([this.addContentTypes(), this.addApp(), this.addCore(), this.addSharedStrings(), this.addStyles(), this.addWorkbookRels()]);
await this.addWorkbook();
return this._finalize();
},
get nextId() {
// find the next unique spot to add worksheet
Expand Down
33 changes: 15 additions & 18 deletions lib/utils/flow-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FlowControl extends events.EventEmitter {

// determine timeout for flow control delays
if (options.gc) {
const {gc} = options;
const { gc } = options;
if (gc.getTimeout) {
this.getTimeout = gc.getTimeout;
} else {
Expand Down Expand Up @@ -77,7 +77,7 @@ class FlowControl extends events.EventEmitter {
});
}

_pipe(chunk) {
async _pipe(chunk) {
// Write chunk to all pipes. A chunk with no data is the end
const promises = [];
this.pipes.forEach(pipe => {
Expand All @@ -94,13 +94,12 @@ class FlowControl extends events.EventEmitter {
if (!promises.length) {
promises.push(PromiseLib.Promise.resolve());
}
return PromiseLib.Promise.all(promises).then(() => {
try {
chunk.callback();
} catch (e) {
// quietly ignore
}
});
await PromiseLib.Promise.all(promises);
try {
chunk.callback();
} catch (e) {
// quietly ignore
}
}

_animate() {
Expand All @@ -127,19 +126,17 @@ class FlowControl extends events.EventEmitter {
return PromiseLib.Promise.resolve();
}

_flush() {
async _flush() {
// If/while not corked and we have buffers to send, send them
if (this.queue && !this.flushing && !this.corked) {
if (this.queue.length) {
this.flushing = true;
this._delay()
.then(() => this._pipe(this.queue.shift()))
.then(() => {
setImmediate(() => {
this.flushing = false;
this._flush();
});
});
await this._delay();
await this._pipe(this.queue.shift());
setImmediate(() => {
this.flushing = false;
this._flush();
});
}

if (!this.stem) {
Expand Down
10 changes: 5 additions & 5 deletions lib/utils/stream-buf.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,15 @@ utils.inherits(StreamBuf, Stream.Duplex, {
return buf;
},

_pipe(chunk) {
async _pipe(chunk) {
const write = function(pipe) {
return new PromiseLib.Promise(resolve => {
pipe.write(chunk.toBuffer(), () => {
resolve();
});
});
};
const promises = this.pipes.map(write);
return promises.length ? PromiseLib.Promise.all(promises).then(utils.nop) : PromiseLib.Promise.resolve();
await Promise.all(this.pipes.map(write));
},
_writeToBuffers(chunk) {
let inPos = 0;
Expand All @@ -223,7 +222,7 @@ utils.inherits(StreamBuf, Stream.Duplex, {
inPos += buffer.write(chunk, inPos, inLen - inPos);
}
},
write(data, encoding, callback) {
async write(data, encoding, callback) {
if (encoding instanceof Function) {
callback = encoding;
encoding = 'utf8';
Expand All @@ -249,7 +248,8 @@ utils.inherits(StreamBuf, Stream.Duplex, {
this._pipe(this.buffers.shift());
}
} else if (!this.corked) {
this._pipe(chunk).then(callback);
await this._pipe(chunk);
callback();
} else {
this._writeToBuffers(chunk);
process.nextTick(callback);
Expand Down
68 changes: 31 additions & 37 deletions lib/utils/zip-stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const events = require('events');
const JSZip = require('jszip');
const PromiseLib = require('./promise');

const StreamBuf = require('./stream-buf');

Expand All @@ -27,44 +26,40 @@ class ZipReader extends events.EventEmitter {

_finished() {
if (!--this.count) {
PromiseLib.Promise.resolve().then(() => {
process.nextTick(() => {
this.emit('finished');
});
}
}

_process() {
const content = this.stream.read();
this.jsZip
.loadAsync(content)
.then(zip => {
zip.forEach((path, entry) => {
if (!entry.dir) {
this.count++;
entry
.async(this.getEntryType(path))
.then(data => {
const entryStream = new StreamBuf();
entryStream.path = path;
entryStream.write(data);
entryStream.autodrain = () => {
this._finished();
};
entryStream.on('finish', () => {
this._finished();
});

this.emit('entry', entryStream);
})
.catch(error => {
this.emit('error', error);
});
async _process() {
try {
const content = this.stream.read();
const zip = await this.jsZip.loadAsync(content);
zip.forEach(async (path, entry) => {
if (!entry.dir) {
this.count++;
try {
const data = await entry.async(this.getEntryType(path));
const entryStream = new StreamBuf();
entryStream.path = path;
entryStream.write(data);
entryStream.autodrain = () => {
this._finished();
};
entryStream.on('finish', () => {
this._finished();
});

this.emit('entry', entryStream);
} catch (error) {
this.emit('error', error);
}
});
})
.catch(error => {
this.emit('error', error);
}
});
} catch (error) {
this.emit('error', error);
}
}

// ==========================================================================
Expand Down Expand Up @@ -121,11 +116,10 @@ class ZipWriter extends events.EventEmitter {
}
}

finalize() {
return this.zip.generateAsync(this.options).then(content => {
this.stream.end(content);
this.emit('finish');
});
async finalize() {
const content = await this.zip.generateAsync(this.options);
this.stream.end(content);
this.emit('finish');
}

// ==========================================================================
Expand Down
5 changes: 1 addition & 4 deletions lib/xlsx/xform/base-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ class BaseXform {

mergeModel(obj) {
// set obj's props to this.model
this.model = Object.assign(
this.model || {},
obj
);
this.model = Object.assign(this.model || {}, obj);
}

parse(parser, stream) {
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy