diff --git a/.versions b/.versions index 1acb1920..d2cc1234 100644 --- a/.versions +++ b/.versions @@ -26,7 +26,7 @@ modules-runtime@0.6.5 mongo-id@1.0.5 observe-sequence@1.0.12 ostrio:cookies@2.0.4 -ostrio:files@1.6.3 +ostrio:files@1.6.4 promise@0.7.3 random@1.0.10 reactive-var@1.0.10 diff --git a/README.md b/README.md index 4ff0277b..c649cc63 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ ToC: - [Awards](https://github.com/VeliovGroup/Meteor-Files#awards) - [See *MF* in action](https://github.com/VeliovGroup/Meteor-Files#demo-application) - Demo application - [Why this package?](https://github.com/VeliovGroup/Meteor-Files#why-meteor-files) - - [Installation](https://github.com/VeliovGroup/Meteor-Files#install) + - [Installation](https://github.com/VeliovGroup/Meteor-Files#installation) + - [ES6 Import](https://github.com/VeliovGroup/Meteor-Files#es6-import) - [API](https://github.com/VeliovGroup/Meteor-Files#api-overview-full-api): * [Initialize Collection](https://github.com/VeliovGroup/Meteor-Files#new-filescollectionconfig-isomorphic) * [Upload file](https://github.com/VeliovGroup/Meteor-Files#insertsettings-autostart-client) @@ -73,12 +74,18 @@ The `cfs` is a well known package, but it's huge monster which combines everythi Easy-peasy kids, *yeah*? -Install: +Installation: ======== ```shell meteor add ostrio:files ``` +ES6 Import: +======== +```jsx +import { FilesCollection } from 'meteor/ostrio:files'; +``` + API overview (*[full API](https://github.com/VeliovGroup/Meteor-Files/wiki)*) ======== Note: When using any of `accounts` packages - package `accounts-base` must be explicitly added to `.meteor/packages` above `ostrio:files` @@ -286,6 +293,7 @@ Supporters: I would like to thank everyone who support this project. *Because of those guys this project can have 100% of our attention*. - [@themeteorchef](https://github.com/themeteorchef) - [@MeDBejoHok](https://github.com/medbejohok) + - [@martunta](https://github.com/martunta) ---- diff --git a/demo-simplest-streaming/.meteor/.gitignore b/demo-simplest-streaming/.meteor/.gitignore index 40830374..501f92e4 100644 --- a/demo-simplest-streaming/.meteor/.gitignore +++ b/demo-simplest-streaming/.meteor/.gitignore @@ -1 +1,2 @@ +dev_bundle local diff --git a/demo-simplest-streaming/.meteor/release b/demo-simplest-streaming/.meteor/release index f80cc1ce..e5b4dc18 100644 --- a/demo-simplest-streaming/.meteor/release +++ b/demo-simplest-streaming/.meteor/release @@ -1 +1 @@ -METEOR@1.3.4.1 +METEOR@1.3.4.4 diff --git a/demo-simplest-streaming/.meteor/versions b/demo-simplest-streaming/.meteor/versions index 1300f9b0..485e609d 100644 --- a/demo-simplest-streaming/.meteor/versions +++ b/demo-simplest-streaming/.meteor/versions @@ -1,6 +1,6 @@ allow-deny@1.0.5 -autoupdate@1.2.10 -babel-compiler@6.8.3 +autoupdate@1.2.11 +babel-compiler@6.8.4 babel-runtime@0.1.9_1 base64@1.0.9 binary-heap@1.0.9 @@ -8,49 +8,49 @@ blaze@2.1.8 blaze-html-templates@1.0.4 blaze-tools@1.0.9 boilerplate-generator@1.0.9 -caching-compiler@1.0.5_1 +caching-compiler@1.0.6 caching-html-compiler@1.0.6 callback-hook@1.0.9 check@1.2.3 -coffeescript@1.1.2_1 +coffeescript@1.1.3 ddp@1.2.5 -ddp-client@1.2.8_1 +ddp-client@1.2.9 ddp-common@1.2.6 -ddp-server@1.2.8_1 +ddp-server@1.2.9 deps@1.0.12 diff-sequence@1.0.6 -ecmascript@0.4.6_1 -ecmascript-runtime@0.2.11_1 +ecmascript@0.4.7 +ecmascript-runtime@0.2.12 ejson@1.0.12 -es5-shim@4.5.12_1 +es5-shim@4.5.13 fastclick@1.0.12 geojson-utils@1.0.9 hot-code-push@1.0.4 html-tools@1.0.10 htmljs@1.0.10 -http@1.1.7 +http@1.1.8 id-map@1.0.8 jquery@1.11.9 launch-screen@1.0.12 livedata@1.0.18 -logging@1.0.13_1 -meteor@1.1.15_1 +logging@1.0.14 +meteor@1.1.16 meteor-base@1.0.4 -minifier-css@1.1.12_1 -minifier-js@1.1.12_1 +minifier-css@1.1.13 +minifier-js@1.1.13 minimongo@1.0.17 mobile-experience@1.0.4 mobile-status-bar@1.0.12 -modules@0.6.4 -modules-runtime@0.6.4_1 +modules@0.6.5 +modules-runtime@0.6.5 mongo@1.1.9_1 mongo-id@1.0.5 -npm-mongo@1.4.44_1 +npm-mongo@1.4.45 observe-sequence@1.0.12 ordered-dict@1.0.8 ostrio:cookies@2.0.4 -ostrio:files@1.6.2 -promise@0.7.2_1 +ostrio:files@1.6.4 +promise@0.7.3 random@1.0.10 reactive-var@1.0.10 reload@1.1.10 @@ -58,13 +58,13 @@ retry@1.0.8 routepolicy@1.0.11 spacebars@1.0.12 spacebars-compiler@1.0.12 -standard-minifier-css@1.0.7_1 -standard-minifier-js@1.0.7_1 -templating@1.1.12_1 +standard-minifier-css@1.0.8 +standard-minifier-js@1.0.8 +templating@1.1.13 templating-tools@1.0.4 tracker@1.0.14 ui@1.0.11 underscore@1.0.9 url@1.0.10 -webapp@1.2.9_1 +webapp@1.2.10 webapp-hashing@1.0.9 diff --git a/demo-simplest-streaming/client/main.js b/demo-simplest-streaming/client/main.js index db7b454c..3b85d51c 100644 --- a/demo-simplest-streaming/client/main.js +++ b/demo-simplest-streaming/client/main.js @@ -1,5 +1,5 @@ import { Template } from 'meteor/templating'; - +import { Videos, Images } from '../lib/files.collections.js'; import './main.html'; Template.file.helpers({ diff --git a/demo-simplest-streaming/lib/files.collections.js b/demo-simplest-streaming/lib/files.collections.js index 81a8fe1f..c6cd84be 100644 --- a/demo-simplest-streaming/lib/files.collections.js +++ b/demo-simplest-streaming/lib/files.collections.js @@ -1,16 +1,18 @@ -this.Images = new Meteor.Files({ +import { FilesCollection } from 'meteor/ostrio:files'; + +let Images = new FilesCollection({ debug: true, collectionName: 'Images', - onBeforeUpload: function () { + onBeforeUpload() { // Disallow uploads from client return false; } }); -this.Videos = new Meteor.Files({ +let Videos = new FilesCollection({ debug: true, collectionName: 'Videos', - onBeforeUpload: function () { + onBeforeUpload() { // Disallow uploads from client return false; } @@ -21,7 +23,7 @@ if (Meteor.isServer) { Images.denyClient(); Videos.denyClient(); - Meteor.startup(function () { + Meteor.startup(() => { if (!Images.findOne()) { Images.load('https://raw.githubusercontent.com/VeliovGroup/Meteor-Files/master/logo.png', { fileName: 'logo.png' @@ -35,16 +37,13 @@ if (Meteor.isServer) { } }); - Meteor.publish('files.images.all', function () { - return Images.find().cursor; - }); - - Meteor.publish('files.videos.all', function () { - return Videos.find().cursor; - }); + Meteor.publish('files.images.all', () => Images.find().cursor); + Meteor.publish('files.videos.all', () => Videos.find().cursor); } else { Meteor.subscribe('files.images.all'); Meteor.subscribe('files.videos.all'); -} \ No newline at end of file +} + +export { Videos, Images } \ No newline at end of file diff --git a/demo/.gitignore b/demo/.gitignore new file mode 100644 index 00000000..14a1690b --- /dev/null +++ b/demo/.gitignore @@ -0,0 +1 @@ +junk/ \ No newline at end of file diff --git a/demo/.meteor/versions b/demo/.meteor/versions index 6344326a..101e5e0c 100644 --- a/demo/.meteor/versions +++ b/demo/.meteor/versions @@ -81,7 +81,7 @@ observe-sequence@1.0.12 ordered-dict@1.0.8 ostrio:cookies@2.0.4 ostrio:cstorage@2.0.5 -ostrio:files@1.6.3 +ostrio:files@1.6.4 ostrio:flow-router-extra@2.12.2 ostrio:flow-router-meta@1.1.1 ostrio:flow-router-title@2.1.1 diff --git a/docs/constructor.md b/docs/constructor.md index af1d665d..c5048d35 100644 --- a/docs/constructor.md +++ b/docs/constructor.md @@ -137,10 +137,10 @@ Allows to change default response headers - Default Function + Default Function - We recommend to keep original function structure, with your modifications, see example altering default headers + We recommend to keep original function structure, with your modifications, see example altering default headers diff --git a/event-emitter.js b/event-emitter.js deleted file mode 100644 index 97a3127a..00000000 --- a/event-emitter.js +++ /dev/null @@ -1,474 +0,0 @@ -/*! - * EventEmitter v4.2.11 - git.io/ee - * Unlicense - http://unlicense.org/ - * Oliver Caldwell - http://oli.me.uk/ - * @preserve - */ - -;(function () { - 'use strict'; - - /** - * Class for managing events. - * Can be extended to provide event functionality in other classes. - * - * @class EventEmitter Manages event registering and emitting. - */ - function EventEmitter() {} - - // Shortcuts to improve speed and size - var proto = EventEmitter.prototype; - var exports = this; - var originalGlobalValue = exports.EventEmitter; - - /** - * Finds the index of the listener for the event in its storage array. - * - * @param {Function[]} listeners Array of listeners to search through. - * @param {Function} listener Method to look for. - * @return {Number} Index of the specified listener, -1 if not found - * @api private - */ - function indexOfListener(listeners, listener) { - var i = listeners.length; - while (i--) { - if (listeners[i].listener === listener) { - return i; - } - } - - return -1; - } - - /** - * Alias a method while keeping the context correct, to allow for overwriting of target method. - * - * @param {String} name The name of the target method. - * @return {Function} The aliased method - * @api private - */ - function alias(name) { - return function aliasClosure() { - return this[name].apply(this, arguments); - }; - } - - /** - * Returns the listener array for the specified event. - * Will initialise the event object and listener arrays if required. - * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. - * Each property in the object response is an array of listener functions. - * - * @param {String|RegExp} evt Name of the event to return the listeners from. - * @return {Function[]|Object} All listener functions for the event. - */ - proto.getListeners = function getListeners(evt) { - var events = this._getEvents(); - var response; - var key; - - // Return a concatenated array of all matching events if - // the selector is a regular expression. - if (evt instanceof RegExp) { - response = {}; - for (key in events) { - if (events.hasOwnProperty(key) && evt.test(key)) { - response[key] = events[key]; - } - } - } - else { - response = events[evt] || (events[evt] = []); - } - - return response; - }; - - /** - * Takes a list of listener objects and flattens it into a list of listener functions. - * - * @param {Object[]} listeners Raw listener objects. - * @return {Function[]} Just the listener functions. - */ - proto.flattenListeners = function flattenListeners(listeners) { - var flatListeners = []; - var i; - - for (i = 0; i < listeners.length; i += 1) { - flatListeners.push(listeners[i].listener); - } - - return flatListeners; - }; - - /** - * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. - * - * @param {String|RegExp} evt Name of the event to return the listeners from. - * @return {Object} All listener functions for an event in an object. - */ - proto.getListenersAsObject = function getListenersAsObject(evt) { - var listeners = this.getListeners(evt); - var response; - - if (listeners instanceof Array) { - response = {}; - response[evt] = listeners; - } - - return response || listeners; - }; - - /** - * Adds a listener function to the specified event. - * The listener will not be added if it is a duplicate. - * If the listener returns true then it will be removed after it is called. - * If you pass a regular expression as the event name then the listener will be added to all events that match it. - * - * @param {String|RegExp} evt Name of the event to attach the listener to. - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addListener = function addListener(evt, listener) { - var listeners = this.getListenersAsObject(evt); - var listenerIsWrapped = typeof listener === 'object'; - var key; - - for (key in listeners) { - if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { - listeners[key].push(listenerIsWrapped ? listener : { - listener: listener, - once: false - }); - } - } - - return this; - }; - - /** - * Alias of addListener - */ - proto.on = alias('addListener'); - - /** - * Semi-alias of addListener. It will add a listener that will be - * automatically removed after its first execution. - * - * @param {String|RegExp} evt Name of the event to attach the listener to. - * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addOnceListener = function addOnceListener(evt, listener) { - return this.addListener(evt, { - listener: listener, - once: true - }); - }; - - /** - * Alias of addOnceListener. - */ - proto.once = alias('addOnceListener'); - - /** - * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. - * You need to tell it what event names should be matched by a regex. - * - * @param {String} evt Name of the event to create. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.defineEvent = function defineEvent(evt) { - this.getListeners(evt); - return this; - }; - - /** - * Uses defineEvent to define multiple events. - * - * @param {String[]} evts An array of event names to define. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.defineEvents = function defineEvents(evts) { - for (var i = 0; i < evts.length; i += 1) { - this.defineEvent(evts[i]); - } - return this; - }; - - /** - * Removes a listener function from the specified event. - * When passed a regular expression as the event name, it will remove the listener from all events that match it. - * - * @param {String|RegExp} evt Name of the event to remove the listener from. - * @param {Function} listener Method to remove from the event. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeListener = function removeListener(evt, listener) { - var listeners = this.getListenersAsObject(evt); - var index; - var key; - - for (key in listeners) { - if (listeners.hasOwnProperty(key)) { - index = indexOfListener(listeners[key], listener); - - if (index !== -1) { - listeners[key].splice(index, 1); - } - } - } - - return this; - }; - - /** - * Alias of removeListener - */ - proto.off = alias('removeListener'); - - /** - * Adds listeners in bulk using the manipulateListeners method. - * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. - * You can also pass it a regular expression to add the array of listeners to all events that match it. - * Yeah, this function does quite a bit. That's probably a bad thing. - * - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to add. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.addListeners = function addListeners(evt, listeners) { - // Pass through to manipulateListeners - return this.manipulateListeners(false, evt, listeners); - }; - - /** - * Removes listeners in bulk using the manipulateListeners method. - * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. - * You can also pass it an event name and an array of listeners to be removed. - * You can also pass it a regular expression to remove the listeners from all events that match it. - * - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to remove. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeListeners = function removeListeners(evt, listeners) { - // Pass through to manipulateListeners - return this.manipulateListeners(true, evt, listeners); - }; - - /** - * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. - * The first argument will determine if the listeners are removed (true) or added (false). - * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. - * You can also pass it an event name and an array of listeners to be added/removed. - * You can also pass it a regular expression to manipulate the listeners of all events that match it. - * - * @param {Boolean} remove True if you want to remove listeners, false if you want to add. - * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. - * @param {Function[]} [listeners] An optional array of listener functions to add/remove. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { - var i; - var value; - var single = remove ? this.removeListener : this.addListener; - var multiple = remove ? this.removeListeners : this.addListeners; - - // If evt is an object then pass each of its properties to this method - if (typeof evt === 'object' && !(evt instanceof RegExp)) { - for (i in evt) { - if (evt.hasOwnProperty(i) && (value = evt[i])) { - // Pass the single listener straight through to the singular method - if (typeof value === 'function') { - single.call(this, i, value); - } - else { - // Otherwise pass back to the multiple function - multiple.call(this, i, value); - } - } - } - } - else { - // So evt must be a string - // And listeners must be an array of listeners - // Loop over it and pass each one to the multiple method - i = listeners.length; - while (i--) { - single.call(this, evt, listeners[i]); - } - } - - return this; - }; - - /** - * Removes all listeners from a specified event. - * If you do not specify an event then all listeners will be removed. - * That means every event will be emptied. - * You can also pass a regex to remove all events that match it. - * - * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.removeEvent = function removeEvent(evt) { - var type = typeof evt; - var events = this._getEvents(); - var key; - - // Remove different things depending on the state of evt - if (type === 'string') { - // Remove all listeners for the specified event - delete events[evt]; - } - else if (evt instanceof RegExp) { - // Remove all events matching the regex. - for (key in events) { - if (events.hasOwnProperty(key) && evt.test(key)) { - delete events[key]; - } - } - } - else { - // Remove all listeners in all events - delete this._events; - } - - return this; - }; - - /** - * Alias of removeEvent. - * - * Added to mirror the node API. - */ - proto.removeAllListeners = alias('removeEvent'); - - /** - * Emits an event of your choice. - * When emitted, every listener attached to that event will be executed. - * If you pass the optional argument array then those arguments will be passed to every listener upon execution. - * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. - * So they will not arrive within the array on the other side, they will be separate. - * You can also pass a regular expression to emit to all events that match it. - * - * @param {String|RegExp} evt Name of the event to emit and execute listeners for. - * @param {Array} [args] Optional array of arguments to be passed to each listener. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.emitEvent = function emitEvent(evt, args) { - var listenersMap = this.getListenersAsObject(evt); - var listeners; - var listener; - var i; - var key; - var response; - - for (key in listenersMap) { - if (listenersMap.hasOwnProperty(key)) { - listeners = listenersMap[key].slice(0); - i = listeners.length; - - while (i--) { - // If the listener returns true then it shall be removed from the event - // The function is executed either with a basic call or an apply if there is an args array - listener = listeners[i]; - - if (listener.once === true) { - this.removeListener(evt, listener.listener); - } - - response = listener.listener.apply(this, args || []); - - if (response === this._getOnceReturnValue()) { - this.removeListener(evt, listener.listener); - } - } - } - } - - return this; - }; - - /** - * Alias of emitEvent - */ - proto.trigger = alias('emitEvent'); - - /** - * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. - * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. - * - * @param {String|RegExp} evt Name of the event to emit and execute listeners for. - * @param {...*} Optional additional arguments to be passed to each listener. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.emit = function emit(evt) { - var args = Array.prototype.slice.call(arguments, 1); - return this.emitEvent(evt, args); - }; - - /** - * Sets the current value to check against when executing listeners. If a - * listeners return value matches the one set here then it will be removed - * after execution. This value defaults to true. - * - * @param {*} value The new value to check for when executing listeners. - * @return {Object} Current instance of EventEmitter for chaining. - */ - proto.setOnceReturnValue = function setOnceReturnValue(value) { - this._onceReturnValue = value; - return this; - }; - - /** - * Fetches the current value to check against when executing listeners. If - * the listeners return value matches this one then it should be removed - * automatically. It will return true by default. - * - * @return {*|Boolean} The current value to check for or the default, true. - * @api private - */ - proto._getOnceReturnValue = function _getOnceReturnValue() { - if (this.hasOwnProperty('_onceReturnValue')) { - return this._onceReturnValue; - } - else { - return true; - } - }; - - /** - * Fetches the events object and creates one if required. - * - * @return {Object} The events storage object. - * @api private - */ - proto._getEvents = function _getEvents() { - return this._events || (this._events = {}); - }; - - /** - * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. - * - * @return {Function} Non conflicting EventEmitter class. - */ - EventEmitter.noConflict = function noConflict() { - exports.EventEmitter = originalGlobalValue; - return EventEmitter; - }; - - // Expose the class either via AMD, CommonJS or the global object - if (typeof define === 'function' && define.amd) { - define(function () { - return EventEmitter; - }); - } - else if (typeof module === 'object' && module.exports){ - module.exports = EventEmitter; - } - else { - exports.EventEmitter = EventEmitter; - } -}.call(this)); \ No newline at end of file diff --git a/event-emitter.jsx b/event-emitter.jsx new file mode 100644 index 00000000..fd0a3b84 --- /dev/null +++ b/event-emitter.jsx @@ -0,0 +1,462 @@ +/*! + * EventEmitter v4.2.11 - git.io/ee + * Unlicense - http://unlicense.org/ + * Oliver Caldwell - http://oli.me.uk/ + * @preserve + */ + +;(function () { + /** + * Class for managing events. + * Can be extended to provide event functionality in other classes. + * + * @class EventEmitter Manages event registering and emitting. + */ + function EventEmitter() {} + + // Shortcuts to improve speed and size + const proto = EventEmitter.prototype; + const exports = this; + const originalGlobalValue = exports.EventEmitter; + + /** + * Finds the index of the listener for the event in its storage array. + * + * @param {Function[]} listeners Array of listeners to search through. + * @param {Function} listener Method to look for. + * @return {Number} Index of the specified listener, -1 if not found + * @api private + */ + function indexOfListener(listeners, listener) { + let i = listeners.length; + while (i--) { + if (listeners[i].listener === listener) { + return i; + } + } + + return -1; + } + + /** + * Alias a method while keeping the context correct, to allow for overwriting of target method. + * + * @param {String} name The name of the target method. + * @return {Function} The aliased method + * @api private + */ + function alias(name) { + return function aliasClosure() { + return this[name].apply(this, arguments); + }; + } + + /** + * Returns the listener array for the specified event. + * Will initialise the event object and listener arrays if required. + * Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them. + * Each property in the object response is an array of listener functions. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Function[]|Object} All listener functions for the event. + */ + proto.getListeners = function getListeners(evt) { + const events = this._getEvents(); + let response; + let key; + + // Return a concatenated array of all matching events if + // the selector is a regular expression. + if (evt instanceof RegExp) { + response = {}; + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + response[key] = events[key]; + } + } + } + else { + response = events[evt] || (events[evt] = []); + } + + return response; + }; + + /** + * Takes a list of listener objects and flattens it into a list of listener functions. + * + * @param {Object[]} listeners Raw listener objects. + * @return {Function[]} Just the listener functions. + */ + proto.flattenListeners = function flattenListeners(listeners) { + const flatListeners = []; + let i; + + for (i = 0; i < listeners.length; i += 1) { + flatListeners.push(listeners[i].listener); + } + + return flatListeners; + }; + + /** + * Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful. + * + * @param {String|RegExp} evt Name of the event to return the listeners from. + * @return {Object} All listener functions for an event in an object. + */ + proto.getListenersAsObject = function getListenersAsObject(evt) { + const listeners = this.getListeners(evt); + let response; + + if (listeners instanceof Array) { + response = {}; + response[evt] = listeners; + } + + return response || listeners; + }; + + /** + * Adds a listener function to the specified event. + * The listener will not be added if it is a duplicate. + * If the listener returns true then it will be removed after it is called. + * If you pass a regular expression as the event name then the listener will be added to all events that match it. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListener = function addListener(evt, listener) { + const listeners = this.getListenersAsObject(evt); + const listenerIsWrapped = typeof listener === 'object'; + let key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key) && indexOfListener(listeners[key], listener) === -1) { + listeners[key].push(listenerIsWrapped ? listener : { + listener, + once: false + }); + } + } + + return this; + }; + + /** + * Alias of addListener + */ + proto.on = alias('addListener'); + + /** + * Semi-alias of addListener. It will add a listener that will be + * automatically removed after its first execution. + * + * @param {String|RegExp} evt Name of the event to attach the listener to. + * @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addOnceListener = function addOnceListener(evt, listener) { + return this.addListener(evt, { + listener, + once: true + }); + }; + + /** + * Alias of addOnceListener. + */ + proto.once = alias('addOnceListener'); + + /** + * Defines an event name. This is required if you want to use a regex to add a listener to multiple events at once. If you don't do this then how do you expect it to know what event to add to? Should it just add to every possible match for a regex? No. That is scary and bad. + * You need to tell it what event names should be matched by a regex. + * + * @param {String} evt Name of the event to create. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvent = function defineEvent(evt) { + this.getListeners(evt); + return this; + }; + + /** + * Uses defineEvent to define multiple events. + * + * @param {String[]} evts An array of event names to define. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.defineEvents = function defineEvents(evts) { + for (let i = 0; i < evts.length; i += 1) { + this.defineEvent(evts[i]); + } + return this; + }; + + /** + * Removes a listener function from the specified event. + * When passed a regular expression as the event name, it will remove the listener from all events that match it. + * + * @param {String|RegExp} evt Name of the event to remove the listener from. + * @param {Function} listener Method to remove from the event. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListener = function removeListener(evt, listener) { + const listeners = this.getListenersAsObject(evt); + let index; + let key; + + for (key in listeners) { + if (listeners.hasOwnProperty(key)) { + index = indexOfListener(listeners[key], listener); + + if (index !== -1) { + listeners[key].splice(index, 1); + } + } + } + + return this; + }; + + /** + * Alias of removeListener + */ + proto.off = alias('removeListener'); + + /** + * Adds listeners in bulk using the manipulateListeners method. + * If you pass an object as the second argument you can add to multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. You can also pass it an event name and an array of listeners to be added. + * You can also pass it a regular expression to add the array of listeners to all events that match it. + * Yeah, this function does quite a bit. That's probably a bad thing. + * + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add to multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to add. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.addListeners = function addListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(false, evt, listeners); + }; + + /** + * Removes listeners in bulk using the manipulateListeners method. + * If you pass an object as the second argument you can remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. + * You can also pass it an event name and an array of listeners to be removed. + * You can also pass it a regular expression to remove the listeners from all events that match it. + * + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to remove from multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeListeners = function removeListeners(evt, listeners) { + // Pass through to manipulateListeners + return this.manipulateListeners(true, evt, listeners); + }; + + /** + * Edits listeners in bulk. The addListeners and removeListeners methods both use this to do their job. You should really use those instead, this is a little lower level. + * The first argument will determine if the listeners are removed (true) or added (false). + * If you pass an object as the second argument you can add/remove from multiple events at once. The object should contain key value pairs of events and listeners or listener arrays. + * You can also pass it an event name and an array of listeners to be added/removed. + * You can also pass it a regular expression to manipulate the listeners of all events that match it. + * + * @param {Boolean} remove True if you want to remove listeners, false if you want to add. + * @param {String|Object|RegExp} evt An event name if you will pass an array of listeners next. An object if you wish to add/remove from multiple events at once. + * @param {Function[]} [listeners] An optional array of listener functions to add/remove. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.manipulateListeners = function manipulateListeners(remove, evt, listeners) { + let i; + let value; + const single = remove ? this.removeListener : this.addListener; + const multiple = remove ? this.removeListeners : this.addListeners; + + // If evt is an object then pass each of its properties to this method + if (typeof evt === 'object' && !(evt instanceof RegExp)) { + for (i in evt) { + if (evt.hasOwnProperty(i) && (value = evt[i])) { + // Pass the single listener straight through to the singular method + if (typeof value === 'function') { + single.call(this, i, value); + } + else { + // Otherwise pass back to the multiple function + multiple.call(this, i, value); + } + } + } + } + else { + // So evt must be a string + // And listeners must be an array of listeners + // Loop over it and pass each one to the multiple method + i = listeners.length; + while (i--) { + single.call(this, evt, listeners[i]); + } + } + + return this; + }; + + /** + * Removes all listeners from a specified event. + * If you do not specify an event then all listeners will be removed. + * That means every event will be emptied. + * You can also pass a regex to remove all events that match it. + * + * @param {String|RegExp} [evt] Optional name of the event to remove all listeners for. Will remove from every event if not passed. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.removeEvent = function removeEvent(evt) { + const type = typeof evt; + const events = this._getEvents(); + let key; + + // Remove different things depending on the state of evt + if (type === 'string') { + // Remove all listeners for the specified event + delete events[evt]; + } + else if (evt instanceof RegExp) { + // Remove all events matching the regex. + for (key in events) { + if (events.hasOwnProperty(key) && evt.test(key)) { + delete events[key]; + } + } + } + else { + // Remove all listeners in all events + delete this._events; + } + + return this; + }; + + /** + * Alias of removeEvent. + * + * Added to mirror the node API. + */ + proto.removeAllListeners = alias('removeEvent'); + + /** + * Emits an event of your choice. + * When emitted, every listener attached to that event will be executed. + * If you pass the optional argument array then those arguments will be passed to every listener upon execution. + * Because it uses `apply`, your array of arguments will be passed as if you wrote them out separately. + * So they will not arrive within the array on the other side, they will be separate. + * You can also pass a regular expression to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {Array} [args] Optional array of arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emitEvent = function emitEvent(evt, args) { + const listenersMap = this.getListenersAsObject(evt); + let listeners; + let listener; + let i; + let key; + let response; + + for (key in listenersMap) { + if (listenersMap.hasOwnProperty(key)) { + listeners = listenersMap[key].slice(0); + i = listeners.length; + + while (i--) { + // If the listener returns true then it shall be removed from the event + // The function is executed either with a basic call or an apply if there is an args array + listener = listeners[i]; + + if (listener.once === true) { + this.removeListener(evt, listener.listener); + } + + response = listener.listener.apply(this, args || []); + + if (response === this._getOnceReturnValue()) { + this.removeListener(evt, listener.listener); + } + } + } + } + + return this; + }; + + /** + * Alias of emitEvent + */ + proto.trigger = alias('emitEvent'); + + /** + * Subtly different from emitEvent in that it will pass its arguments on to the listeners, as opposed to taking a single array of arguments to pass on. + * As with emitEvent, you can pass a regex in place of the event name to emit to all events that match it. + * + * @param {String|RegExp} evt Name of the event to emit and execute listeners for. + * @param {...*} Optional additional arguments to be passed to each listener. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.emit = function emit(evt) { + const args = Array.prototype.slice.call(arguments, 1); + return this.emitEvent(evt, args); + }; + + /** + * Sets the current value to check against when executing listeners. If a + * listeners return value matches the one set here then it will be removed + * after execution. This value defaults to true. + * + * @param {*} value The new value to check for when executing listeners. + * @return {Object} Current instance of EventEmitter for chaining. + */ + proto.setOnceReturnValue = function setOnceReturnValue(value) { + this._onceReturnValue = value; + return this; + }; + + /** + * Fetches the current value to check against when executing listeners. If + * the listeners return value matches this one then it should be removed + * automatically. It will return true by default. + * + * @return {*|Boolean} The current value to check for or the default, true. + * @api private + */ + proto._getOnceReturnValue = function _getOnceReturnValue() { + if (this.hasOwnProperty('_onceReturnValue')) { + return this._onceReturnValue; + } + else { + return true; + } + }; + + /** + * Fetches the events object and creates one if required. + * + * @return {Object} The events storage object. + * @api private + */ + proto._getEvents = function _getEvents() { + return this._events || (this._events = {}); + }; + + /** + * Reverts the global {@link EventEmitter} to its previous value and returns a reference to this version. + * + * @return {Function} Non conflicting EventEmitter class. + */ + EventEmitter.noConflict = function noConflict() { + exports.EventEmitter = originalGlobalValue; + return EventEmitter; + }; + + // Expose the class + export { EventEmitter }; +}.call(this)); \ No newline at end of file diff --git a/files.coffee b/files.coffee index c2b5f62a..a85b0207 100755 --- a/files.coffee +++ b/files.coffee @@ -59,6 +59,7 @@ if Meteor.isServer self = @ Meteor.setTimeout -> self.write num, chunk + return , 25 return false @@ -78,8 +79,11 @@ if Meteor.isServer self = @ Meteor.setTimeout -> self.end callback + return , 25 return false +else + `import { EventEmitter } from './event-emitter.jsx'` ### @private @@ -104,7 +108,10 @@ class FileCursor ### remove: (callback) -> console.info '[FilesCollection] [FileCursor] [remove()]' if @_collection.debug - if @_fileRef then @_collection.remove(@_fileRef._id, callback) else callback new Meteor.Error 404, 'No such file' + if @_fileRef + @_collection.remove(@_fileRef._id, callback) + else + callback and callback new Meteor.Error 404, 'No such file' return @ ### @@ -284,7 +291,7 @@ class FilesCursor ### remove: (callback) -> console.info '[FilesCollection] [FilesCursor] [remove()]' if @_collection.debug - @_collection.remove @_selector, callback, @ + @_collection.remove @_selector, callback return @ ### @@ -593,8 +600,7 @@ class FilesCollection return @_createStream = (_id, path, opts) -> - self._currentUploads[_id] = new writeStream path, opts.fileLength, opts - return self._currentUploads[_id] + return self._currentUploads[_id] = new writeStream path, opts.fileLength, opts # This little function allows to continue upload # even after server is restarted (*not on dev-stage*) @@ -1161,6 +1167,8 @@ class FilesCollection else callback and callback null, result console.info "[FilesCollection] [write]: #{fileName} -> #{self.collectionName}" if self.debug + return + return return @ else undefined @@ -1304,8 +1312,10 @@ class FilesCollection else callback and callback null, result console.info "[FilesCollection] [addFile]: #{fileName} -> #{self.collectionName}" if self.debug + return else callback and callback new Meteor.Error 400, "[FilesCollection] [addFile(#{path})]: File does not exist" + return return @ else @@ -1321,7 +1331,7 @@ class FilesCollection @returns {FileCursor} Instance ### findOne: (selector = {}, options) -> - console.info "[FilesCollection] [findOne(#{JSON.stringify(selector)})]" if @debug + console.info "[FilesCollection] [findOne(#{JSON.stringify(selector)}, #{JSON.stringify(options)})]" if @debug check selector, Match.OneOf Object, String check options, Match.Optional Object doc = @collection.findOne selector, options @@ -1374,8 +1384,7 @@ class FilesCollection {Function} readAsDataURL - Current file as data URL, use to create image preview and etc. Be aware of big files, may lead to browser crash ### insert: if Meteor.isClient then (config, autoStart = true) -> - mName = if autoStart then 'start' else 'manual' - return (new @_UploadInstance(config, @))[mName]() + return (new @_UploadInstance(config, @))[if autoStart then 'start' else 'manual']() else undefined ### @@ -1812,7 +1821,9 @@ class FilesCollection files = @collection.find selector if files.count() > 0 self = @ - files.forEach (file) -> self.unlink file + files.forEach (file) -> + self.unlink file + return if @onAfterRemove self = @ @@ -2168,4 +2179,8 @@ if Meteor.isClient else return '' +### +Export the FilesCollection class +### +`export { FilesCollection }` Meteor.Files = FilesCollection \ No newline at end of file diff --git a/package.js b/package.js index a040cb10..22f2cfff 100755 --- a/package.js +++ b/package.js @@ -1,6 +1,6 @@ Package.describe({ name: 'ostrio:files', - version: '1.6.3', + version: '1.6.4', summary: 'Fast and robust file upload package, with support of FS, AWS, GridFS, DropBox or Google Drive', git: 'https://github.com/VeliovGroup/Meteor-Files', documentation: 'README.md' @@ -8,13 +8,12 @@ Package.describe({ Package.onUse(function(api) { api.versionsFrom('1.3.3.1'); - api.use('ostrio:cookies@2.0.4', ['server', 'client']); - api.addFiles('event-emitter.js', 'client'); - api.addAssets('worker.js', 'client'); - api.addFiles('files.coffee', ['server', 'client']); api.use('webapp', 'server'); api.use(['templating', 'reactive-var', 'tracker', 'http'], 'client'); - api.use(['underscore', 'check', 'random', 'coffeescript'], ['client', 'server']); + api.use(['underscore', 'check', 'random', 'coffeescript', 'ecmascript', 'ostrio:cookies@2.0.4'], ['client', 'server']); + api.addFiles('event-emitter.jsx', 'client'); + api.addAssets('worker.js', 'client'); + api.addFiles('files.coffee', ['server', 'client']); api.export('FilesCollection'); });