1 | /*
|
2 | * OS.js - JavaScript Cloud/Web Desktop Platform
|
3 | *
|
4 | * Copyright (c) 2011-2020, Anders Evenrud <andersevenrud@gmail.com>
|
5 | * All rights reserved.
|
6 | *
|
7 | * Redistribution and use in source and binary forms, with or without
|
8 | * modification, are permitted provided that the following conditions are met:
|
9 | *
|
10 | * 1. Redistributions of source code must retain the above copyright notice, this
|
11 | * list of conditions and the following disclaimer
|
12 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
13 | * this list of conditions and the following disclaimer in the documentation
|
14 | * and/or other materials provided with the distribution
|
15 | *
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
20 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
26 | *
|
27 | * @author Anders Evenrud <andersevenrud@gmail.com>
|
28 | * @licence Simplified BSD License
|
29 | */
|
30 |
|
31 | const nullAdapter = require('./adapters/settings/null');
|
32 | const fsAdapter = require('./adapters/settings/fs');
|
33 |
|
34 | /**
|
35 | * OS.js Settings Manager
|
36 | */
|
37 | class Settings {
|
38 |
|
39 | /**
|
40 | * Create new instance
|
41 | * @param {Core} core Core reference
|
42 | * @param {object} [options] Instance options
|
43 | */
|
44 | constructor(core, options = {}) {
|
45 | this.core = core;
|
46 | this.options = {
|
47 | adapter: nullAdapter,
|
48 | ...options
|
49 | };
|
50 |
|
51 | if (this.options.adapter === 'fs') {
|
52 | this.options.adapter = fsAdapter;
|
53 | }
|
54 |
|
55 | try {
|
56 | this.adapter = this.options.adapter(core, this.options.config);
|
57 | } catch (e) {
|
58 | this.core.logger.warn(e);
|
59 | this.adapter = nullAdapter(core, this.options.config);
|
60 | }
|
61 | }
|
62 |
|
63 | /**
|
64 | * Destroy instance
|
65 | */
|
66 | destroy() {
|
67 | if (this.adapter.destroy) {
|
68 | this.adapter.destroy();
|
69 | }
|
70 | }
|
71 |
|
72 | /**
|
73 | * Initializes adapter
|
74 | */
|
75 | async init() {
|
76 | if (this.adapter.init) {
|
77 | return this.adapter.init();
|
78 | }
|
79 |
|
80 | return true;
|
81 | }
|
82 |
|
83 | /**
|
84 | * Sends save request to adapter
|
85 | * @param {Request} req Express request
|
86 | * @param {Response} res Express response
|
87 | */
|
88 | async save(req, res) {
|
89 | const result = await this.adapter.save(req, res);
|
90 | res.json(result);
|
91 | }
|
92 |
|
93 | /**
|
94 | * Sends load request to adapter
|
95 | * @param {Request} req Express request
|
96 | * @param {Response} res Express response
|
97 | */
|
98 | async load(req, res) {
|
99 | const result = await this.adapter.load(req, res);
|
100 | res.json(result);
|
101 | }
|
102 | }
|
103 |
|
104 |
|
105 | module.exports = Settings;
|