UNPKG

2.1 kBJavaScriptView Raw
1// Copyright (c) 2016 Kinvey Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4// in compliance with the License. You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software distributed under the License
9// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10// or implied. See the License for the specific language governing permissions and limitations under
11// the License.
12
13process.env.NODE_ENV = process.env.NODE_ENV || 'development';
14
15module.exports = (() => {
16 let receiver;
17
18 function start(options, taskReceivedCallback, receiverStartedCallback) {
19 if (!taskReceivedCallback || !receiverStartedCallback) {
20 const missingArgumentsError = new Error('Missing Arguments');
21 missingArgumentsError.description = 'Cannot start Task Receiver - missing arguments';
22 missingArgumentsError.debug = 'Missing arguments - initializing a task receiver requires a taskReceived callback '
23 + 'function and a receiver started callback function.';
24 throw missingArgumentsError;
25 }
26
27 if (typeof taskReceivedCallback !== 'function' || typeof receiverStartedCallback !== 'function') {
28 const invalidArgumentTypeError = new Error('Invalid Argument Type');
29 invalidArgumentTypeError.description = 'Cannot start Task Receiver - invalid arguments';
30 invalidArgumentTypeError.debug = 'Invalid arguments - taskReceivedCallback and receiverStartedCallback '
31 + 'must be functions.';
32 throw invalidArgumentTypeError;
33 }
34
35 if (options && options.type === 'http') {
36 receiver = require('./receivers/httpReceiver');
37 } else {
38 receiver = require('./receivers/tcpReceiver');
39 }
40 receiver.startServer(taskReceivedCallback, receiverStartedCallback, options);
41 }
42
43 function stop() {
44 receiver.stop();
45 }
46
47 function _getApp() {
48 return receiver._app || null;
49 }
50
51 return {
52 start,
53 stop,
54 _getApp
55 };
56})();