diff --git a/.babelrc b/.babelrc index 7273d21244..b1ff51d47d 100644 --- a/.babelrc +++ b/.babelrc @@ -4,6 +4,11 @@ ], "presets": [ "es2015", - "stage-0" + "stage-3", + ["env", { + "targets": { + "node": "4.6" + } + }] ] } diff --git a/package.json b/package.json index b921fb2802..32cac6d6e8 100644 --- a/package.json +++ b/package.json @@ -49,8 +49,9 @@ "babel-eslint": "^7.1.1", "babel-plugin-syntax-flow": "6.18.0", "babel-plugin-transform-flow-strip-types": "6.22.0", + "babel-preset-env": "1.6.0", "babel-preset-es2015": "6.24.1", - "babel-preset-stage-0": "6.24.1", + "babel-preset-stage-3": "6.24.1", "babel-register": "6.26.0", "bcrypt-nodejs": "0.0.3", "cross-env": "5.0.2", diff --git a/spec/CLI.spec.js b/spec/CLI.spec.js index 06312fc602..efe9ee1fb2 100644 --- a/spec/CLI.spec.js +++ b/spec/CLI.spec.js @@ -157,6 +157,12 @@ describe('definitions', () => { } } }); + + it('should throw when using deprecated facebookAppIds', () => { + expect(() => { + definitions.facebookAppIds.action() + }).toThrow(); + }); }); describe('LiveQuery definitions', () => { diff --git a/spec/ParseFile.spec.js b/spec/ParseFile.spec.js index 600c4bae59..9ec2c680f3 100644 --- a/spec/ParseFile.spec.js +++ b/spec/ParseFile.spec.js @@ -595,4 +595,290 @@ describe('Parse.File testing', () => { done(); }); }); + + it('fails to upload an empty file', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: '', + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toBe(400); + expect(body).toEqual('{"code":130,"error":"Invalid file upload."}'); + done(); + }); + }); + + it('fails to upload without a file name', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/', + body: 'yolo', + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toBe(400); + expect(body).toEqual('{"code":122,"error":"Filename not provided."}'); + done(); + }); + }); + + it('fails to upload without a file name', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/', + body: 'yolo', + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toBe(400); + expect(body).toEqual('{"code":122,"error":"Filename not provided."}'); + done(); + }); + }); + + it('fails to delete an unkown file', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'X-Parse-Master-Key': 'test' + }; + request.delete({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toBe(400); + expect(body).toEqual('{"code":153,"error":"Could not delete file."}'); + done(); + }); + }); + + describe_only_db('mongo')('Gridstore Range tests', () => { + it('supports range requests', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=0-5' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle '); + done(); + }); + }); + }); + + it('supports small range requests', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=0-2' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('arg'); + done(); + }); + }); + }); + + // See specs https://www.greenbytes.de/tech/webdav/draft-ietf-httpbis-p5-range-latest.html#byte.ranges + it('supports getting one byte', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=2-2' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('g'); + done(); + }); + }); + }); + + it('supports getting last n bytes', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'something different', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=-4' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body.length).toBe(4); + expect(body).toEqual('rent'); + done(); + }); + }); + }); + + it('supports getting first n bytes', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'something different', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=10-' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('different'); + done(); + }); + }); + }); + + function repeat(string, count) { + var s = string; + while (count > 0) { + s += string; + count--; + } + return s; + } + + it('supports large range requests', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: repeat('argle bargle', 100) + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=13-240' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body.length).toEqual(228); + expect(body.indexOf('rgle barglea')).toBe(0); + done(); + }); + }); + }); + + it('fails to stream unknown file', done => { + request.get({ url: 'http://localhost:8378/1/files/test/file.txt', headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=13-240' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(response.statusCode).toBe(404); + expect(body).toEqual('File not found.'); + done(); + }); + }); + }); + + // Because GridStore is not loaded on PG, those are perfect + // for fallback tests + describe_only_db('postgres')('Default Range tests', () => { + it('fallback to regular request', done => { + var headers = { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + request.post({ + headers: headers, + url: 'http://localhost:8378/1/files/file.txt', + body: 'argle bargle', + }, (error, response, body) => { + expect(error).toBe(null); + var b = JSON.parse(body); + request.get({ url: b.url, headers: { + 'Content-Type': 'application/octet-stream', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest', + 'Range': 'bytes=0-5' + } }, (error, response, body) => { + expect(error).toBe(null); + expect(body).toEqual('argle bargle'); + done(); + }); + }); + }); + }); }); diff --git a/spec/parsers.spec.js b/spec/parsers.spec.js index 9f4ba0e24b..bd52a89368 100644 --- a/spec/parsers.spec.js +++ b/spec/parsers.spec.js @@ -2,6 +2,10 @@ import { numberParser, numberOrBoolParser, booleanParser, + objectParser, + arrayParser, + moduleOrObjectParser, + nullParser, } from '../src/cli/utils/parsers'; describe('parsers', () => { @@ -31,4 +35,33 @@ describe('parsers', () => { expect(parser(1)).toEqual(true); expect(parser(2)).toEqual(false); }); + + it('parses correctly with objectParser', () => { + const parser = objectParser; + expect(parser({hello: 'world'})).toEqual({hello: 'world'}); + expect(parser('{"hello": "world"}')).toEqual({hello: 'world'}); + expect(() => {parser('string')}).toThrow(); + }); + + it('parses correctly with moduleOrObjectParser', () => { + const parser = moduleOrObjectParser; + expect(parser({hello: 'world'})).toEqual({hello: 'world'}); + expect(parser('{"hello": "world"}')).toEqual({hello: 'world'}); + expect(parser('string')).toEqual('string'); + }); + + it('parses correctly with arrayParser', () => { + const parser = arrayParser; + expect(parser([1,2,3])).toEqual([1,2,3]); + expect(parser('{"hello": "world"}')).toEqual(['{"hello": "world"}']); + expect(parser('1,2,3')).toEqual(['1','2','3']); + expect(() => {parser(1)}).toThrow(); + }); + + it('parses correctly with nullParser', () => { + const parser = nullParser; + expect(parser('null')).toEqual(null); + expect(parser(1)).toEqual(1); + expect(parser('blabla')).toEqual('blabla'); + }); }); diff --git a/src/Adapters/Storage/Mongo/MongoCollection.js b/src/Adapters/Storage/Mongo/MongoCollection.js index 09ab7bac79..ad1b458d25 100644 --- a/src/Adapters/Storage/Mongo/MongoCollection.js +++ b/src/Adapters/Storage/Mongo/MongoCollection.js @@ -79,10 +79,6 @@ export default class MongoCollection { return this._mongoCollection.updateMany(query, update); } - deleteOne(query) { - return this._mongoCollection.deleteOne(query); - } - deleteMany(query) { return this._mongoCollection.deleteMany(query); } diff --git a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js index 32e3a8db8a..3142712a3a 100644 --- a/src/Adapters/Storage/Mongo/MongoStorageAdapter.js +++ b/src/Adapters/Storage/Mongo/MongoStorageAdapter.js @@ -278,8 +278,15 @@ export class MongoStorageAdapter { .then(collection => collection.insertOne(mongoObject)) .catch(error => { if (error.code === 11000) { // Duplicate value - throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, - 'A duplicate value for a field with unique values was provided'); + const err = new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'A duplicate value for a field with unique values was provided'); + err.underlyingError = error; + if (error.message) { + const matches = error.message.match(/index:[\sa-zA-Z0-9_\-\.]+\$?([a-zA-Z_-]+)_1/); + if (matches && Array.isArray(matches)) { + err.userInfo = { duplicated_field: matches[1] }; + } + } + throw err; } throw error; }); @@ -375,9 +382,8 @@ export class MongoStorageAdapter { .catch(error => { if (error.code === 11000) { throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Tried to ensure field uniqueness for a class that already has duplicates.'); - } else { - throw error; } + throw error; }); } diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index e9fd7b1b7c..bce54a4c95 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -945,7 +945,15 @@ export class PostgresStorageAdapter { .then(() => ({ ops: [object] })) .catch(error => { if (error.code === PostgresUniqueIndexViolationError) { - throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'A duplicate value for a field with unique values was provided'); + const err = new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'A duplicate value for a field with unique values was provided'); + err.underlyingError = error; + if (error.constraint) { + const matches = error.constraint.match(/unique_([a-zA-Z]+)/); + if (matches && Array.isArray(matches)) { + err.userInfo = { duplicated_field: matches[1] }; + } + } + throw err; } else { throw error; } diff --git a/src/Push/PushWorker.js b/src/Push/PushWorker.js index b6d23955cb..52b6d08451 100644 --- a/src/Push/PushWorker.js +++ b/src/Push/PushWorker.js @@ -10,8 +10,6 @@ import * as utils from './utils'; import { ParseMessageQueue } from '../ParseMessageQueue'; import { PushQueue } from './PushQueue'; -const UNSUPPORTED_BADGE_KEY = "unsupported"; - function groupByBadge(installations) { return installations.reduce((map, installation) => { const badge = installation.badge + ''; @@ -93,11 +91,7 @@ export class PushWorker { // Map the on the badges count and return the send result const promises = Object.keys(badgeInstallationsMap).map((badge) => { const payload = deepcopy(body); - if (badge == UNSUPPORTED_BADGE_KEY) { - delete payload.data.badge; - } else { - payload.data.badge = parseInt(badge); - } + payload.data.badge = parseInt(badge); const installations = badgeInstallationsMap[badge]; return this.sendToAdapter(payload, installations, pushStatus, config); }); diff --git a/src/RestWrite.js b/src/RestWrite.js index 686437bc03..44a77672e1 100644 --- a/src/RestWrite.js +++ b/src/RestWrite.js @@ -1011,9 +1011,19 @@ RestWrite.prototype.runDatabaseOperation = function() { if (this.className !== '_User' || error.code !== Parse.Error.DUPLICATE_VALUE) { throw error; } + + // Quick check, if we were able to infer the duplicated field name + if (error && error.userInfo && error.userInfo.duplicated_field === 'username') { + throw new Parse.Error(Parse.Error.USERNAME_TAKEN, 'Account already exists for this username.'); + } + + if (error && error.userInfo && error.userInfo.duplicated_field === 'email') { + throw new Parse.Error(Parse.Error.EMAIL_TAKEN, 'Account already exists for this email address.'); + } + // If this was a failed user creation due to username or email already taken, we need to // check whether it was username or email and return the appropriate error. - + // Fallback to the original method // TODO: See if we can later do this without additional queries by using named indexes. return this.config.database.find( this.className, diff --git a/src/Routers/AudiencesRouter.js b/src/Routers/AudiencesRouter.js index 347abc1128..3dbb94cf85 100644 --- a/src/Routers/AudiencesRouter.js +++ b/src/Routers/AudiencesRouter.js @@ -3,25 +3,14 @@ import rest from '../rest'; import * as middleware from '../middlewares'; export class AudiencesRouter extends ClassesRouter { + + className() { + return '_Audience'; + } + handleFind(req) { const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query)); - var options = {}; - - if (body.skip) { - options.skip = Number(body.skip); - } - if (body.limit || body.limit === 0) { - options.limit = Number(body.limit); - } - if (body.order) { - options.order = String(body.order); - } - if (body.count) { - options.count = true; - } - if (body.include) { - options.include = String(body.include); - } + const options = ClassesRouter.optionsFromBody(body); return rest.find(req.config, req.auth, '_Audience', body.where, options, req.info.clientSDK) .then((response) => { @@ -35,7 +24,6 @@ export class AudiencesRouter extends ClassesRouter { } handleGet(req) { - req.params.className = '_Audience'; return super.handleGet(req) .then((data) => { data.response.query = JSON.parse(data.response.query); @@ -44,21 +32,6 @@ export class AudiencesRouter extends ClassesRouter { }); } - handleCreate(req) { - req.params.className = '_Audience'; - return super.handleCreate(req); - } - - handleUpdate(req) { - req.params.className = '_Audience'; - return super.handleUpdate(req); - } - - handleDelete(req) { - req.params.className = '_Audience'; - return super.handleDelete(req); - } - mountRoutes() { this.route('GET','/push_audiences', middleware.promiseEnforceMasterKeyAccess, req => { return this.handleFind(req); }); this.route('GET','/push_audiences/:objectId', middleware.promiseEnforceMasterKeyAccess, req => { return this.handleGet(req); }); diff --git a/src/Routers/ClassesRouter.js b/src/Routers/ClassesRouter.js index 6cfb8eb6c8..5793445a60 100644 --- a/src/Routers/ClassesRouter.js +++ b/src/Routers/ClassesRouter.js @@ -8,45 +8,20 @@ const ALLOWED_GET_QUERY_KEYS = ['keys', 'include']; export class ClassesRouter extends PromiseRouter { + className(req) { + return req.params.className; + } + handleFind(req) { const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query)); - const options = {}; - const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys', - 'include', 'redirectClassNameForKey', 'where']; - - for (const key of Object.keys(body)) { - if (allowConstraints.indexOf(key) === -1) { - throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid parameter for query: ${key}`); - } - } - - if (body.skip) { - options.skip = Number(body.skip); - } - if (body.limit || body.limit === 0) { - options.limit = Number(body.limit); - } else { - options.limit = Number(100); - } - if (body.order) { - options.order = String(body.order); - } - if (body.count) { - options.count = true; - } - if (typeof body.keys == 'string') { - options.keys = body.keys; - } - if (body.include) { - options.include = String(body.include); - } + const options = ClassesRouter.optionsFromBody(body); if (body.redirectClassNameForKey) { options.redirectClassNameForKey = String(body.redirectClassNameForKey); } if (typeof body.where === 'string') { body.where = JSON.parse(body.where); } - return rest.find(req.config, req.auth, req.params.className, body.where, options, req.info.clientSDK) + return rest.find(req.config, req.auth, this.className(req), body.where, options, req.info.clientSDK) .then((response) => { if (response && response.results) { for (const result of response.results) { @@ -77,13 +52,13 @@ export class ClassesRouter extends PromiseRouter { options.include = String(body.include); } - return rest.get(req.config, req.auth, req.params.className, req.params.objectId, options, req.info.clientSDK) + return rest.get(req.config, req.auth, this.className(req), req.params.objectId, options, req.info.clientSDK) .then((response) => { if (!response.results || response.results.length == 0) { throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.'); } - if (req.params.className === "_User") { + if (this.className(req) === "_User") { delete response.results[0].sessionToken; @@ -99,16 +74,16 @@ export class ClassesRouter extends PromiseRouter { } handleCreate(req) { - return rest.create(req.config, req.auth, req.params.className, req.body, req.info.clientSDK); + return rest.create(req.config, req.auth, this.className(req), req.body, req.info.clientSDK); } handleUpdate(req) { const where = { objectId: req.params.objectId } - return rest.update(req.config, req.auth, req.params.className, where, req.body, req.info.clientSDK); + return rest.update(req.config, req.auth, this.className(req), where, req.body, req.info.clientSDK); } handleDelete(req) { - return rest.del(req.config, req.auth, req.params.className, req.params.objectId, req.info.clientSDK) + return rest.del(req.config, req.auth, this.className(req), req.params.objectId, req.info.clientSDK) .then(() => { return {response: {}}; }); @@ -126,6 +101,39 @@ export class ClassesRouter extends PromiseRouter { return json } + static optionsFromBody(body) { + const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys', + 'include', 'redirectClassNameForKey', 'where']; + + for (const key of Object.keys(body)) { + if (allowConstraints.indexOf(key) === -1) { + throw new Parse.Error(Parse.Error.INVALID_QUERY, `Invalid parameter for query: ${key}`); + } + } + const options = {}; + if (body.skip) { + options.skip = Number(body.skip); + } + if (body.limit || body.limit === 0) { + options.limit = Number(body.limit); + } else { + options.limit = Number(100); + } + if (body.order) { + options.order = String(body.order); + } + if (body.count) { + options.count = true; + } + if (typeof body.keys == 'string') { + options.keys = body.keys; + } + if (body.include) { + options.include = String(body.include); + } + return options; + } + mountRoutes() { this.route('GET', '/classes/:className', (req) => { return this.handleFind(req); }); this.route('GET', '/classes/:className/:objectId', (req) => { return this.handleGet(req); }); diff --git a/src/Routers/FilesRouter.js b/src/Routers/FilesRouter.js index 4f788d679e..dfd951fbb0 100644 --- a/src/Routers/FilesRouter.js +++ b/src/Routers/FilesRouter.js @@ -8,7 +8,7 @@ import logger from '../logger'; export class FilesRouter { - expressRouter(options = {}) { + expressRouter({ maxUploadSize = '20Mb' } = {}) { var router = express.Router(); router.get('/files/:appId/:filename', this.getHandler); @@ -19,7 +19,7 @@ export class FilesRouter { router.post('/files/:filename', Middlewares.allowCrossDomain, - BodyParser.raw({type: () => { return true; }, limit: options.maxUploadSize || '20mb'}), // Allow uploads without Content-Type, or with any Content-Type. + BodyParser.raw({type: () => { return true; }, limit: maxUploadSize }), // Allow uploads without Content-Type, or with any Content-Type. Middlewares.handleParseHeaders, this.createHandler ); @@ -108,87 +108,74 @@ export class FilesRouter { } function isFileStreamable(req, filesController){ - if (req.get('Range')) { - if (!(typeof filesController.adapter.getFileStream === 'function')) { - return false; - } - if (typeof filesController.adapter.constructor.name !== 'undefined') { - if (filesController.adapter.constructor.name == 'GridStoreAdapter') { - return true; - } - } - } - return false; + return req.get('Range') && typeof filesController.adapter.getFileStream === 'function'; +} + +function getRange(req) { + const parts = req.get('Range').replace(/bytes=/, "").split("-"); + return { start: parseInt(parts[0], 10), end: parseInt(parts[1], 10) }; } // handleFileStream is licenced under Creative Commons Attribution 4.0 International License (https://creativecommons.org/licenses/by/4.0/). // Author: LEROIB at weightingformypizza (https://weightingformypizza.wordpress.com/2015/06/24/stream-html5-media-content-like-video-audio-from-mongodb-using-express-and-gridstore/). function handleFileStream(stream, req, res, contentType) { - var buffer_size = 1024 * 1024;//1024Kb + const buffer_size = 1024 * 1024; //1024Kb // Range request, partiall stream the file - var parts = req.get('Range').replace(/bytes=/, "").split("-"); - var partialstart = parts[0]; - var partialend = parts[1]; - var start = partialstart ? parseInt(partialstart, 10) : 0; - var end = partialend ? parseInt(partialend, 10) : stream.length - 1; - var chunksize = (end - start) + 1; - - if (chunksize == 1) { - start = 0; - partialend = false; + let { + start, end + } = getRange(req); + + const notEnded = (!end && end !== 0); + const notStarted = (!start && start !== 0); + // No end provided, we want all bytes + if (notEnded) { + end = stream.length - 1; } - - if (!partialend) { - if (((stream.length - 1) - start) < (buffer_size)) { - end = stream.length - 1; - }else{ - end = start + (buffer_size); - } - chunksize = (end - start) + 1; + // No start provided, we're reading backwards + if (notStarted) { + start = stream.length - end; + end = start + end - 1; } - if (start == 0 && end == 2) { - chunksize = 1; + // Data exceeds the buffer_size, cap + if (end - start >= buffer_size) { + end = start + buffer_size - 1; } + const contentLength = (end - start) + 1; + res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + stream.length, 'Accept-Ranges': 'bytes', - 'Content-Length': chunksize, + 'Content-Length': contentLength, 'Content-Type': contentType, }); stream.seek(start, function () { // get gridFile stream - var gridFileStream = stream.stream(true); - var bufferAvail = 0; - var range = (end - start) + 1; - var totalbyteswanted = (end - start) + 1; - var totalbyteswritten = 0; + const gridFileStream = stream.stream(true); + let bufferAvail = 0; + let remainingBytesToWrite = contentLength; + let totalBytesWritten = 0; // write to response - gridFileStream.on('data', function (buff) { - bufferAvail += buff.length; - //Ok check if we have enough to cover our range - if (bufferAvail < range) { - //Not enough bytes to satisfy our full range - if (bufferAvail > 0) { - //Write full buffer - res.write(buff); - totalbyteswritten += buff.length; - range -= buff.length; - bufferAvail -= buff.length; - } - } else { - //Enough bytes to satisfy our full range! - if (bufferAvail > 0) { - const buffer = buff.slice(0,range); - res.write(buffer); - totalbyteswritten += buffer.length; - bufferAvail -= range; - } + gridFileStream.on('data', function (data) { + bufferAvail += data.length; + if (bufferAvail > 0) { + // slice returns the same buffer if overflowing + // safe to call in any case + const buffer = data.slice(0, remainingBytesToWrite); + // write the buffer + res.write(buffer); + // increment total + totalBytesWritten += buffer.length; + // decrement remaining + remainingBytesToWrite -= data.length; + // decrement the avaialbe buffer + bufferAvail -= buffer.length; } - if (totalbyteswritten >= totalbyteswanted) { - //totalbytes = 0; + // in case of small slices, all values will be good at that point + // we've written enough, end... + if (totalBytesWritten >= contentLength) { stream.close(); res.end(); this.destroy(); diff --git a/src/Routers/InstallationsRouter.js b/src/Routers/InstallationsRouter.js index d54b1138e7..90ab113eb6 100644 --- a/src/Routers/InstallationsRouter.js +++ b/src/Routers/InstallationsRouter.js @@ -4,26 +4,13 @@ import ClassesRouter from './ClassesRouter'; import rest from '../rest'; export class InstallationsRouter extends ClassesRouter { + className() { + return '_Installation'; + } + handleFind(req) { const body = Object.assign(req.body, ClassesRouter.JSONFromQuery(req.query)); - var options = {}; - - if (body.skip) { - options.skip = Number(body.skip); - } - if (body.limit || body.limit === 0) { - options.limit = Number(body.limit); - } - if (body.order) { - options.order = String(body.order); - } - if (body.count) { - options.count = true; - } - if (body.include) { - options.include = String(body.include); - } - + const options = ClassesRouter.optionsFromBody(body); return rest.find(req.config, req.auth, '_Installation', body.where, options, req.info.clientSDK) .then((response) => { @@ -31,26 +18,6 @@ export class InstallationsRouter extends ClassesRouter { }); } - handleGet(req) { - req.params.className = '_Installation'; - return super.handleGet(req); - } - - handleCreate(req) { - req.params.className = '_Installation'; - return super.handleCreate(req); - } - - handleUpdate(req) { - req.params.className = '_Installation'; - return super.handleUpdate(req); - } - - handleDelete(req) { - req.params.className = '_Installation'; - return super.handleDelete(req); - } - mountRoutes() { this.route('GET','/installations', req => { return this.handleFind(req); }); this.route('GET','/installations/:objectId', req => { return this.handleGet(req); }); diff --git a/src/Routers/RolesRouter.js b/src/Routers/RolesRouter.js index a09c424bb0..67fea13817 100644 --- a/src/Routers/RolesRouter.js +++ b/src/Routers/RolesRouter.js @@ -2,29 +2,8 @@ import ClassesRouter from './ClassesRouter'; export class RolesRouter extends ClassesRouter { - handleFind(req) { - req.params.className = '_Role'; - return super.handleFind(req); - } - - handleGet(req) { - req.params.className = '_Role'; - return super.handleGet(req); - } - - handleCreate(req) { - req.params.className = '_Role'; - return super.handleCreate(req); - } - - handleUpdate(req) { - req.params.className = '_Role'; - return super.handleUpdate(req); - } - - handleDelete(req) { - req.params.className = '_Role'; - return super.handleDelete(req); + className() { + return '_Role'; } mountRoutes() { diff --git a/src/Routers/SessionsRouter.js b/src/Routers/SessionsRouter.js index 74ce21834b..ed9b3830f7 100644 --- a/src/Routers/SessionsRouter.js +++ b/src/Routers/SessionsRouter.js @@ -7,29 +7,9 @@ import RestWrite from '../RestWrite'; import { newToken } from '../cryptoUtils'; export class SessionsRouter extends ClassesRouter { - handleFind(req) { - req.params.className = '_Session'; - return super.handleFind(req); - } - - handleGet(req) { - req.params.className = '_Session'; - return super.handleGet(req); - } - - handleCreate(req) { - req.params.className = '_Session'; - return super.handleCreate(req); - } - - handleUpdate(req) { - req.params.className = '_Session'; - return super.handleUpdate(req); - } - handleDelete(req) { - req.params.className = '_Session'; - return super.handleDelete(req); + className() { + return '_Session'; } handleMe(req) { diff --git a/src/Routers/UsersRouter.js b/src/Routers/UsersRouter.js index 0ab0e9e6f8..0a0910c442 100644 --- a/src/Routers/UsersRouter.js +++ b/src/Routers/UsersRouter.js @@ -1,6 +1,5 @@ // These methods handle the User-related routes. -import deepcopy from 'deepcopy'; import Parse from 'parse/node'; import Config from '../Config'; import AccountLockout from '../AccountLockout'; @@ -12,32 +11,9 @@ import RestWrite from '../RestWrite'; const cryptoUtils = require('../cryptoUtils'); export class UsersRouter extends ClassesRouter { - handleFind(req) { - req.params.className = '_User'; - return super.handleFind(req); - } - - handleGet(req) { - req.params.className = '_User'; - return super.handleGet(req); - } - - handleCreate(req) { - const data = deepcopy(req.body); - req.body = data; - req.params.className = '_User'; - - return super.handleCreate(req); - } - - handleUpdate(req) { - req.params.className = '_User'; - return super.handleUpdate(req); - } - handleDelete(req) { - req.params.className = '_User'; - return super.handleDelete(req); + className() { + return '_User'; } handleMe(req) { diff --git a/src/triggers.js b/src/triggers.js index 21b8c3a01d..b202e6134b 100644 --- a/src/triggers.js +++ b/src/triggers.js @@ -54,25 +54,11 @@ export function removeFunction(functionName, applicationId) { delete _triggerStore[applicationId].Functions[functionName] } -export function removeJob(jobName, applicationId) { - applicationId = applicationId || Parse.applicationId; - delete _triggerStore[applicationId].Jobs[jobName] -} - export function removeTrigger(type, className, applicationId) { applicationId = applicationId || Parse.applicationId; delete _triggerStore[applicationId].Triggers[type][className] } -export function _unregister(appId,category,className,type) { - if (type) { - removeTrigger(className,type,appId); - delete _triggerStore[appId][category][className][type]; - } else { - delete _triggerStore[appId][category][className]; - } -} - export function _unregisterAll() { Object.keys(_triggerStore).forEach(appId => delete _triggerStore[appId]); }