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

Extensions! #146

Open
wants to merge 4 commits into
base: v3.0.0
Choose a base branch
from
Open

Extensions! #146

wants to merge 4 commits into from

Conversation

JbIPS
Copy link
Collaborator

@JbIPS JbIPS commented May 23, 2018

Create a basic system of extensions on read and write action. This will be adaptable to be able to cover a lot of hook points in the future.

@JbIPS JbIPS requested review from clemos and lexoyo May 23, 2018 21:44
@JbIPS JbIPS changed the title Extensions 🎉 Extensions! May 23, 2018
lib/index.js Outdated
* @param {Plugin} plugin - A plugin with at least one hook implemented
*/
ext(plugin) {
Object.keys(HOOKS).map((key) => HOOKS[key]).forEach((hook) => {
Copy link

Choose a reason for hiding this comment

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

Object.values(HOOKS) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Better, I was looking for this but got stuck with forof loop

test/index.js Outdated
onWrite: (input) => input.replace('b', 'd')
});
return unifile.writeFile({}, 'memory', '', 'ab')
.then(() => expect(inMemoryFile).to.equal('db'))
Copy link

Choose a reason for hiding this comment

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

shouldn't it be... dd (ab => first onWrite (a to b) => bb => second onWrite (b to d) => dd)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I though that to but the replace is not global, so only the first match is replaced.

Copy link

Choose a reason for hiding this comment

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

haha wtf
then maybe you should put a more obvious transformation in your test, because the test is pretty tricky to read (maybe upper case / lower case, prefixing / unprefixing, ...)
also I was wondering: is input just a chunk, or is it garanteed to be the full content ? if it's a chunk, then maybe you should think about implementing the whole stream API

test/index.js Outdated
.should.eventually.equal('aa');
});

it('follow a chain of action in order', function() {
Copy link

Choose a reason for hiding this comment

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

IMHO it shouldn't be possible to add several codecs, as the spec is very confusing:
For instance, if you use 2 extension, say cipher / decipher and then zip, on write it's all fine (cipher then zip), but on read, it would certainly break (decipher then unzip => boom)
I think you should either clarify the spec (and likely fix your implementation :S) or allow only 1 ext / hook.
I think allowing only 1 ext is better anyway, since you can still compose your onRead / onWrite hooks like onWrite: compose( cipher , zip ), onRead: compose( unzip, decipher )

const Unifile = require('../lib/');
const unifile = new Unifile();

const cipher = crypto.createCipher('aes192', 'a password');
Copy link

Choose a reason for hiding this comment

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

duplicate => constants :D const PASSWORD = 'a password'

@JbIPS
Copy link
Collaborator Author

JbIPS commented May 27, 2018

@clemos Fixed as you requested

Bonus, I added plugins for stream 😃

clemos
clemos previously approved these changes May 28, 2018
Copy link
Member

@lexoyo lexoyo left a comment

Choose a reason for hiding this comment

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

I like how simple this is to create a plugin (impressive the "encrypt" plugin!

Maybe the plugins should have a ON_BEFORE_* and be able to change all the input data?

And will it be a limitation that we can only have 1 set of plugin? I mean what if Silex calls unifile.ext and CE did too before it? Or what if I have an object which is the "encrypt" plugin and another which is the "change extensions" plugin, and I want to activate one or the other depending on my project settings...

@@ -258,7 +302,8 @@ class Unifile {
* @return {external:Promise<string>} a promise of the content of the file
*/
readFile(session, connectorName, path) {
return this.callMethod(connectorName, session, 'readFile', path);
return this.callMethod(connectorName, session, 'readFile', path)
.then((content) => applyPlugin(this.plugins[HOOKS.ON_READ], content));
Copy link
Member

Choose a reason for hiding this comment

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

here maybe the plugins should have a ON_BEFORE_READ in order to change the paths and possibly the session and connectorName

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.

None yet

3 participants