Skip to content
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

Replace callbacks with promises #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

SemenchenkoVitaliy
Copy link
Member

@metarhia/globalstorage changed its API from callbacks to promises in metarhia/globalstorage#416. In order to also update FsProvider, @metarhia/filestorage should also be updated.

lib/fs.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
lib/utils.js Show resolved Hide resolved
lib/filestorage.js Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
@SemenchenkoVitaliy SemenchenkoVitaliy force-pushed the replace-callbacks-with-promises branch from 713cb9f to 1bb1c97 Compare May 13, 2019 11:27
test/utils.js Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
test/utils.js Outdated Show resolved Hide resolved
@SemenchenkoVitaliy
Copy link
Member Author

Blocked until metarhia/common#290.

@SemenchenkoVitaliy SemenchenkoVitaliy added the blocked PRs that are blocked by other issues/PRs. label May 20, 2019
}

const stats = await fs.stat(file);
if (stats.size <= minCompressSize) return false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but:

Now that I think of it, wouldn't this lead for the user of Filestorage to a need to keep a map of compressed/uncompressed files? Because without such map if someone calls compress(file, 100, 'xz') and the file size is less than 100 then the file will not be compressed and if the user then calls read(file, { compression: 'xz' }) this would lead to an error as the file was not compressed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was that metadata including file name, hash sum, deduplication hash, compression type etc. will be stored in the DB. I don't see a problem with the need for such a map since you already have to store files compression types somewhere.

Copy link
Member

@lundibundi lundibundi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM after we use rmRecursive from common.

package.json Outdated Show resolved Hide resolved
test/filestorage.js Outdated Show resolved Hide resolved
test/utils.js Outdated Show resolved Hide resolved
@SemenchenkoVitaliy SemenchenkoVitaliy force-pushed the replace-callbacks-with-promises branch from 59bceb0 to 7d19dc7 Compare June 18, 2019 07:56
@SemenchenkoVitaliy SemenchenkoVitaliy force-pushed the replace-callbacks-with-promises branch from 7d19dc7 to e4ccff5 Compare June 18, 2019 08:19
@SemenchenkoVitaliy SemenchenkoVitaliy removed the blocked PRs that are blocked by other issues/PRs. label Jun 19, 2019
lib/utils.js Outdated Show resolved Hide resolved
lib/utils.js Outdated Show resolved Hide resolved
lib/fs.js Outdated
'readdir',
];

if (process.version.slice(1).split('.')[0] >= 10) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (process.version.slice(1).split('.')[0] >= 10) {
if (fs.promises) {

Copy link
Member

@lundibundi lundibundi Jun 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should even check for Node.js '11.14.0' (nodejs/node#26581) in here to avoid having the warning in the console every time this is run.

// checksum - <string>, data checksum
// dedupHash - <string>, second data checksum
// size - <number>, data size
// Returns: <Object>, stats
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns a Promise, since it is an AsyncFunction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we need to somehow specify what this promise resolves.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SemenchenkoVitaliy we don't have a special syntax in metadoc for that, at the moment most of the projects just specify the return value as Promise without providing any info on what value it resolves with (except for the function's description above), so I think you can either add some syntax for that in metadoc, or leave out the details on what this promise resolves with. But you still have to specify the correct return type, which is Promise, not Object.

lib/utils.js Outdated
throw new Error(`Unknown compression type ${opts.compression} specified`);
}

return new Promise((res, rej) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming is not very obvious here:

Suggested change
return new Promise((res, rej) => {
return new Promise((resolve, reject) => {

.once('error', rej)
.once('finish', () => {
if (opts.encoding) res(buffers.join(''));
else res(Buffer.concat(buffers));
})
.on('entry', (header, stream, next) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this will work with gzip stream, since it doesn't emit 'entry' event:

Note: tar, tgz and zip have the same uncompressing streaming API as above: it's a writable stream, and entries will be emitted while uncompressing one after one another, while that of gzip is slightly different: gzip.UncompressStream is a transform stream, so no entry event will be emitted and you can just pipe to another stream

Also, I think, you should just use the newly added common.MemoryWritable here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants