FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
parse-server/src/ParseServer.js at master · ultimateprogramer/parse-server · GitHub
ultimateprogramer
/
parse-server
Public
forked from
parse-community/parse-server
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
parse-server
/
src
/
ParseServer.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
348 lines (308 loc) · 13.3 KB
Breadcrumbs
parse-server
/
src
/
ParseServer.js
Copy path
File metadata and controls
348 lines (308 loc) · 13.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// ParseServer - open-source compatible API Server for Parse apps
var
batch
=
require
(
'./batch'
)
,
bodyParser
=
require
(
'body-parser'
)
,
DatabaseAdapter
=
require
(
'./DatabaseAdapter'
)
,
express
=
require
(
'express'
)
,
middlewares
=
require
(
'./middlewares'
)
,
multer
=
require
(
'multer'
)
,
Parse
=
require
(
'parse/node'
)
.
Parse
,
path
=
require
(
'path'
)
,
authDataManager
=
require
(
'./authDataManager'
)
;
if
(
!
global
.
_babelPolyfill
)
{
require
(
'babel-polyfill'
)
;
}
import
{
logger
,
configureLogger
}
from
'./logger'
;
import
AppCache
from
'./cache'
;
import
Config
from
'./Config'
;
import
parseServerPackage
from
'../package.json'
;
import
PromiseRouter
from
'./PromiseRouter'
;
import
requiredParameter
from
'./requiredParameter'
;
import
{
AnalyticsRouter
}
from
'./Routers/AnalyticsRouter'
;
import
{
ClassesRouter
}
from
'./Routers/ClassesRouter'
;
import
{
FeaturesRouter
}
from
'./Routers/FeaturesRouter'
;
import
{
InMemoryCacheAdapter
}
from
'./Adapters/Cache/InMemoryCacheAdapter'
;
import
{
CacheController
}
from
'./Controllers/CacheController'
;
import
{
FileLoggerAdapter
}
from
'./Adapters/Logger/FileLoggerAdapter'
;
import
{
FilesController
}
from
'./Controllers/FilesController'
;
import
{
FilesRouter
}
from
'./Routers/FilesRouter'
;
import
{
FunctionsRouter
}
from
'./Routers/FunctionsRouter'
;
import
{
GlobalConfigRouter
}
from
'./Routers/GlobalConfigRouter'
;
import
{
GridStoreAdapter
}
from
'./Adapters/Files/GridStoreAdapter'
;
import
{
HooksController
}
from
'./Controllers/HooksController'
;
import
{
HooksRouter
}
from
'./Routers/HooksRouter'
;
import
{
IAPValidationRouter
}
from
'./Routers/IAPValidationRouter'
;
import
{
InstallationsRouter
}
from
'./Routers/InstallationsRouter'
;
import
{
loadAdapter
}
from
'./Adapters/AdapterLoader'
;
import
{
LiveQueryController
}
from
'./Controllers/LiveQueryController'
;
import
{
LoggerController
}
from
'./Controllers/LoggerController'
;
import
{
LogsRouter
}
from
'./Routers/LogsRouter'
;
import
{
ParseLiveQueryServer
}
from
'./LiveQuery/ParseLiveQueryServer'
;
import
{
PublicAPIRouter
}
from
'./Routers/PublicAPIRouter'
;
import
{
PushController
}
from
'./Controllers/PushController'
;
import
{
PushRouter
}
from
'./Routers/PushRouter'
;
import
{
randomString
}
from
'./cryptoUtils'
;
import
{
RolesRouter
}
from
'./Routers/RolesRouter'
;
import
{
SchemasRouter
}
from
'./Routers/SchemasRouter'
;
import
{
SessionsRouter
}
from
'./Routers/SessionsRouter'
;
import
{
UserController
}
from
'./Controllers/UserController'
;
import
{
UsersRouter
}
from
'./Routers/UsersRouter'
;
import
{
PurgeRouter
}
from
'./Routers/PurgeRouter'
;
import
DatabaseController
from
'./Controllers/DatabaseController'
;
const
SchemaController
=
require
(
'./Controllers/SchemaController'
)
;
import
ParsePushAdapter
from
'parse-server-push-adapter'
;
import
MongoStorageAdapter
from
'./Adapters/Storage/Mongo/MongoStorageAdapter'
;
// Mutate the Parse object to add the Cloud Code handlers
addParseCloud
(
)
;
const
requiredUserFields
=
{
fields
:
{
...
SchemaController
.
defaultColumns
.
_Default
,
...
SchemaController
.
defaultColumns
.
_User
}
}
;
// ParseServer works like a constructor of an express app.
// The args that we understand are:
// "filesAdapter": a class like GridStoreAdapter providing create, get,
// and delete
// "loggerAdapter": a class like FileLoggerAdapter providing info, error,
// and query
// "databaseURI": a uri like mongodb://localhost:27017/dbname to tell us
// what database this Parse API connects to.
// "cloud": relative location to cloud code to require, or a function
// that is given an instance of Parse as a parameter. Use this instance of Parse
// to register your cloud code hooks and functions.
// "appId": the application id to host
// "masterKey": the master key for requests to this app
// "facebookAppIds": an array of valid Facebook Application IDs, required
// if using Facebook login
// "collectionPrefix": optional prefix for database collection names
// "fileKey": optional key from Parse dashboard for supporting older files
// hosted by Parse
// "clientKey": optional key from Parse dashboard
// "dotNetKey": optional key from Parse dashboard
// "restAPIKey": optional key from Parse dashboard
// "webhookKey": optional key from Parse dashboard
// "javascriptKey": optional key from Parse dashboard
// "push": optional key from configure push
// "sessionLength": optional length in seconds for how long Sessions should be valid for
class
ParseServer
{
constructor
(
{
appId
=
requiredParameter
(
'You must provide an appId!'
)
,
masterKey
=
requiredParameter
(
'You must provide a masterKey!'
)
,
appName
,
filesAdapter
,
push
,
loggerAdapter
,
logsFolder
,
databaseURI
,
databaseOptions
,
databaseAdapter
,
cloud
,
collectionPrefix
=
''
,
clientKey
,
javascriptKey
,
dotNetKey
,
restAPIKey
,
webhookKey
,
fileKey
=
'invalid-file-key'
,
facebookAppIds
=
[
]
,
enableAnonymousUsers
=
true
,
allowClientClassCreation
=
true
,
oauth
=
{
}
,
serverURL
=
requiredParameter
(
'You must provide a serverURL!'
)
,
maxUploadSize
=
'20mb'
,
verifyUserEmails
=
false
,
preventLoginWithUnverifiedEmail
=
false
,
cacheAdapter
,
emailAdapter
,
publicServerURL
,
customPages
=
{
invalidLink
:
undefined
,
verifyEmailSuccess
:
undefined
,
choosePassword
:
undefined
,
passwordResetSuccess
:
undefined
}
,
liveQuery
=
{
}
,
sessionLength
=
31536000
,
// 1 Year in seconds
expireInactiveSessions
=
true
,
verbose
=
false
,
revokeSessionOnPasswordReset
=
true
,
__indexBuildCompletionCallbackForTests
=
(
)
=>
{
}
,
}
)
{
// Initialize the node client SDK automatically
Parse
.
initialize
(
appId
,
javascriptKey
||
'unused'
,
masterKey
)
;
Parse
.
serverURL
=
serverURL
;
if
(
(
databaseOptions
||
databaseURI
||
collectionPrefix
!==
''
)
&&
databaseAdapter
)
{
throw
'You cannot specify both a databaseAdapter and a databaseURI/databaseOptions/connectionPrefix.'
;
}
else
if
(
!
databaseAdapter
)
{
databaseAdapter
=
new
MongoStorageAdapter
(
{
uri
:
databaseURI
,
collectionPrefix
,
mongoOptions
:
databaseOptions
,
}
)
;
}
else
{
databaseAdapter
=
loadAdapter
(
databaseAdapter
)
}
if
(
!
filesAdapter
&&
!
databaseURI
)
{
throw
'When using an explicit database adapter, you must also use and explicit filesAdapter.'
;
}
if
(
logsFolder
)
{
configureLogger
(
{
logsFolder
}
)
}
if
(
cloud
)
{
addParseCloud
(
)
;
if
(
typeof
cloud
===
'function'
)
{
cloud
(
Parse
)
}
else
if
(
typeof
cloud
===
'string'
)
{
require
(
path
.
resolve
(
process
.
cwd
(
)
,
cloud
)
)
;
}
else
{
throw
"argument 'cloud' must either be a string or a function"
;
}
}
if
(
verbose
||
process
.
env
.
VERBOSE
||
process
.
env
.
VERBOSE_PARSE_SERVER
)
{
configureLogger
(
{
level
:
'silly'
}
)
;
}
const
filesControllerAdapter
=
loadAdapter
(
filesAdapter
,
(
)
=>
{
return
new
GridStoreAdapter
(
databaseURI
)
;
}
)
;
// Pass the push options too as it works with the default
const
pushControllerAdapter
=
loadAdapter
(
push
&&
push
.
adapter
,
ParsePushAdapter
,
push
||
{
}
)
;
const
loggerControllerAdapter
=
loadAdapter
(
loggerAdapter
,
FileLoggerAdapter
)
;
const
emailControllerAdapter
=
loadAdapter
(
emailAdapter
)
;
const
cacheControllerAdapter
=
loadAdapter
(
cacheAdapter
,
InMemoryCacheAdapter
,
{
appId
:
appId
}
)
;
// We pass the options and the base class for the adatper,
// Note that passing an instance would work too
const
filesController
=
new
FilesController
(
filesControllerAdapter
,
appId
)
;
const
pushController
=
new
PushController
(
pushControllerAdapter
,
appId
,
push
)
;
const
loggerController
=
new
LoggerController
(
loggerControllerAdapter
,
appId
)
;
const
userController
=
new
UserController
(
emailControllerAdapter
,
appId
,
{
verifyUserEmails
}
)
;
const
liveQueryController
=
new
LiveQueryController
(
liveQuery
)
;
const
cacheController
=
new
CacheController
(
cacheControllerAdapter
,
appId
)
;
const
databaseController
=
new
DatabaseController
(
databaseAdapter
)
;
const
hooksController
=
new
HooksController
(
appId
,
databaseController
,
webhookKey
)
;
// TODO: create indexes on first creation of a _User object. Otherwise it's impossible to
// have a Parse app without it having a _User collection.
let
userClassPromise
=
databaseController
.
loadSchema
(
)
.
then
(
schema
=>
schema
.
enforceClassExists
(
'_User'
)
)
let
usernameUniqueness
=
userClassPromise
.
then
(
(
)
=>
databaseController
.
adapter
.
ensureUniqueness
(
'_User'
,
requiredUserFields
,
[
'username'
]
)
)
.
catch
(
error
=>
{
logger
.
warn
(
'Unable to ensure uniqueness for usernames: '
,
error
)
;
return
Promise
.
reject
(
error
)
;
}
)
;
let
emailUniqueness
=
userClassPromise
.
then
(
(
)
=>
databaseController
.
adapter
.
ensureUniqueness
(
'_User'
,
requiredUserFields
,
[
'email'
]
)
)
.
catch
(
error
=>
{
logger
.
warn
(
'Unabled to ensure uniqueness for user email addresses: '
,
error
)
;
return
Promise
.
reject
(
error
)
;
}
)
AppCache
.
put
(
appId
,
{
masterKey
:
masterKey
,
serverURL
:
serverURL
,
collectionPrefix
:
collectionPrefix
,
clientKey
:
clientKey
,
javascriptKey
:
javascriptKey
,
dotNetKey
:
dotNetKey
,
restAPIKey
:
restAPIKey
,
webhookKey
:
webhookKey
,
fileKey
:
fileKey
,
facebookAppIds
:
facebookAppIds
,
cacheController
:
cacheController
,
filesController
:
filesController
,
pushController
:
pushController
,
loggerController
:
loggerController
,
hooksController
:
hooksController
,
userController
:
userController
,
verifyUserEmails
:
verifyUserEmails
,
preventLoginWithUnverifiedEmail
:
preventLoginWithUnverifiedEmail
,
allowClientClassCreation
:
allowClientClassCreation
,
authDataManager
:
authDataManager
(
oauth
,
enableAnonymousUsers
)
,
appName
:
appName
,
publicServerURL
:
publicServerURL
,
customPages
:
customPages
,
maxUploadSize
:
maxUploadSize
,
liveQueryController
:
liveQueryController
,
sessionLength
:
Number
(
sessionLength
)
,
expireInactiveSessions
:
expireInactiveSessions
,
revokeSessionOnPasswordReset
,
databaseController
,
}
)
;
// To maintain compatibility. TODO: Remove in some version that breaks backwards compatability
if
(
process
.
env
.
FACEBOOK_APP_ID
)
{
AppCache
.
get
(
appId
)
[
'facebookAppIds'
]
.
push
(
process
.
env
.
FACEBOOK_APP_ID
)
;
}
Config
.
validate
(
AppCache
.
get
(
appId
)
)
;
this
.
config
=
AppCache
.
get
(
appId
)
;
hooksController
.
load
(
)
;
// Note: Tests will start to fail if any validation happens after this is called.
if
(
process
.
env
.
TESTING
)
{
__indexBuildCompletionCallbackForTests
(
Promise
.
all
(
[
usernameUniqueness
,
emailUniqueness
]
)
)
;
}
}
get
app
(
)
{
return
ParseServer
.
app
(
this
.
config
)
;
}
static
app
(
{
maxUploadSize
=
'20mb'
}
)
{
// This app serves the Parse API directly.
// It's the equivalent of https://api.parse.com/1 in the hosted Parse API.
var
api
=
express
(
)
;
//api.use("/apps", express.static(__dirname + "/public"));
// File handling needs to be before default middlewares are applied
api
.
use
(
'/'
,
middlewares
.
allowCrossDomain
,
new
FilesRouter
(
)
.
getExpressRouter
(
{
maxUploadSize
:
maxUploadSize
}
)
)
;
api
.
use
(
'/'
,
bodyParser
.
urlencoded
(
{
extended
:
false
}
)
,
new
PublicAPIRouter
(
)
.
expressApp
(
)
)
;
// TODO: separate this from the regular ParseServer object
if
(
process
.
env
.
TESTING
==
1
)
{
api
.
use
(
'/'
,
require
(
'./testing-routes'
)
.
router
)
;
}
api
.
use
(
bodyParser
.
json
(
{
'type'
:
'*/*'
,
limit
:
maxUploadSize
}
)
)
;
api
.
use
(
middlewares
.
allowCrossDomain
)
;
api
.
use
(
middlewares
.
allowMethodOverride
)
;
api
.
use
(
middlewares
.
handleParseHeaders
)
;
let
routers
=
[
new
ClassesRouter
(
)
,
new
UsersRouter
(
)
,
new
SessionsRouter
(
)
,
new
RolesRouter
(
)
,
new
AnalyticsRouter
(
)
,
new
InstallationsRouter
(
)
,
new
FunctionsRouter
(
)
,
new
SchemasRouter
(
)
,
new
PushRouter
(
)
,
new
LogsRouter
(
)
,
new
IAPValidationRouter
(
)
,
new
FeaturesRouter
(
)
,
new
GlobalConfigRouter
(
)
,
new
PurgeRouter
(
)
,
]
;
if
(
process
.
env
.
PARSE_EXPERIMENTAL_HOOKS_ENABLED
||
process
.
env
.
TESTING
)
{
routers
.
push
(
new
HooksRouter
(
)
)
;
}
let
routes
=
routers
.
reduce
(
(
memo
,
router
)
=>
{
return
memo
.
concat
(
router
.
routes
)
;
}
,
[
]
)
;
let
appRouter
=
new
PromiseRouter
(
routes
)
;
batch
.
mountOnto
(
appRouter
)
;
api
.
use
(
appRouter
.
expressApp
(
)
)
;
api
.
use
(
middlewares
.
handleParseErrors
)
;
//This causes tests to spew some useless warnings, so disable in test
if
(
!
process
.
env
.
TESTING
)
{
process
.
on
(
'uncaughtException'
,
(
err
)
=>
{
if
(
err
.
code
===
"EADDRINUSE"
)
{
// user-friendly message for this common error
console
.
error
(
`Unable to listen on port
${
err
.
port
}
. The port is already in use.`
)
;
process
.
exit
(
0
)
;
}
else
{
throw
err
;
}
}
)
;
}
return
api
;
}
static
createLiveQueryServer
(
httpServer
,
config
)
{
return
new
ParseLiveQueryServer
(
httpServer
,
config
)
;
}
}
function
addParseCloud
(
)
{
const
ParseCloud
=
require
(
"./cloud-code/Parse.Cloud"
)
;
Object
.
assign
(
Parse
.
Cloud
,
ParseCloud
)
;
global
.
Parse
=
Parse
;
}
export
default
ParseServer
;
Back
|
FazBrowse Home
|
New Git URL