|
18 | 18 | 'secretAccessKey': process.env.secretAccessKey, // Avoid hardcoding credentials
|
19 | 19 | 'bucket': '...'
|
20 | 20 | }
|
21 |
| - const adapter = new AWSS3SyncAdapter(options) |
| 21 | + const adapter = new AWSS3SyncAdapter(options); |
22 | 22 | db = new Loki('somefile.json', {
|
23 | 23 | autoload: false,
|
24 | 24 | autosave: true,
|
|
63 | 63 | (function (root, factory) {
|
64 | 64 | if (typeof define === 'function' && define.amd) {
|
65 | 65 | // AMD
|
66 |
| - define([], factory) |
| 66 | + define([], factory); |
67 | 67 | } else if (typeof exports === 'object') {
|
68 | 68 | // CommonJS
|
69 |
| - module.exports = factory() |
| 69 | + module.exports = factory(); |
70 | 70 | } else {
|
71 | 71 | // Browser globals
|
72 |
| - root.lokiAWSS3SyncAdapter = factory() |
| 72 | + root.lokiAWSS3SyncAdapter = factory(); |
73 | 73 | }
|
74 | 74 | }(this, function () {
|
75 | 75 | return (function (options) {
|
76 |
| - 'use strict' |
| 76 | + 'use strict'; |
77 | 77 |
|
78 | 78 | function AWSS3SyncAdapterError (message) {
|
79 |
| - this.name = 'AWSS3SyncAdapterError' |
80 |
| - this.message = (message || '') |
| 79 | + this.name = 'AWSS3SyncAdapterError'; |
| 80 | + this.message = (message || ''); |
81 | 81 | }
|
82 | 82 |
|
83 |
| - AWSS3SyncAdapterError.prototype = Error.prototype |
| 83 | + AWSS3SyncAdapterError.prototype = Error.prototype; |
84 | 84 |
|
85 | 85 | /**
|
86 | 86 | * This adapter requires an object options is passed containing the following properties:
|
|
91 | 91 | */
|
92 | 92 |
|
93 | 93 | function AWSS3SyncAdapter (options) {
|
94 |
| - this.options = options |
| 94 | + this.options = options; |
95 | 95 |
|
96 | 96 | if (!options) {
|
97 |
| - throw new AWSS3SyncAdapterError('No options configured in AWSS3SyncAdapter') |
| 97 | + throw new AWSS3SyncAdapterError('No options configured in AWSS3SyncAdapter'); |
98 | 98 | }
|
99 | 99 |
|
100 | 100 | if (!options.AWS) {
|
101 |
| - throw new AWSS3SyncAdapterError('No AWS library specified in options') |
| 101 | + throw new AWSS3SyncAdapterError('No AWS library specified in options'); |
102 | 102 | }
|
103 | 103 |
|
104 | 104 | if (!options.accessKeyId) {
|
105 |
| - throw new AWSS3SyncAdapterError('No accessKeyId property specified in options') |
| 105 | + throw new AWSS3SyncAdapterError('No accessKeyId property specified in options'); |
106 | 106 | }
|
107 | 107 |
|
108 | 108 | if (!options.secretAccessKey) {
|
109 |
| - throw new AWSS3SyncAdapterError('No secretAccessKey property specified in options') |
| 109 | + throw new AWSS3SyncAdapterError('No secretAccessKey property specified in options'); |
110 | 110 | }
|
111 | 111 |
|
112 | 112 | if (!options.bucket) {
|
113 |
| - throw new AWSS3SyncAdapterError('No bucket property specified in options') |
| 113 | + throw new AWSS3SyncAdapterError('No bucket property specified in options'); |
114 | 114 | }
|
115 | 115 |
|
116 |
| - this.options.AWS.config.update({accessKeyId: options.accessKeyId, secretAccessKey: options.secretAccessKey}) |
| 116 | + this.options.AWS.config.update({accessKeyId: options.accessKeyId, secretAccessKey: options.secretAccessKey}); |
117 | 117 | }
|
118 | 118 |
|
119 | 119 | AWSS3SyncAdapter.prototype.saveDatabase = function (name, data, callback) {
|
120 |
| - console.log('AWSS3SyncAdapter.prototype.saveDatabase():', name) |
| 120 | + console.log('AWSS3SyncAdapter.prototype.saveDatabase():', name); |
121 | 121 |
|
122 |
| - const s3 = new this.options.AWS.S3() |
123 |
| - const base64str = Buffer.from(data).toString('base64') |
| 122 | + const s3 = new this.options.AWS.S3(); |
| 123 | + const base64str = Buffer.from(data).toString('base64'); |
124 | 124 |
|
125 | 125 | var params = {
|
126 | 126 | Body: base64str,
|
127 | 127 | Bucket: this.options.bucket,
|
128 | 128 | Key: name,
|
129 | 129 | ServerSideEncryption: 'AES256',
|
130 | 130 | Tagging: '' // key1=value1&key2=value2
|
131 |
| - } |
| 131 | + }; |
132 | 132 |
|
133 | 133 | s3.putObject(params, function (err, data) {
|
134 | 134 | if (err) {
|
135 |
| - console.log('AWSS3SyncAdapter.prototype.saveDatabase() Error:', err, err.stack) |
136 |
| - throw new AWSS3SyncAdapterError('Remote sync failed') |
| 135 | + console.log('AWSS3SyncAdapter.prototype.saveDatabase() Error:', err, err.stack); |
| 136 | + throw new AWSS3SyncAdapterError('Remote sync failed'); |
137 | 137 | } else {
|
138 |
| - return callback(null, data) |
| 138 | + return callback(null, data); |
139 | 139 | }
|
140 |
| - }) |
141 |
| - } |
| 140 | + }); |
| 141 | + }; |
142 | 142 |
|
143 | 143 | AWSS3SyncAdapter.prototype.loadDatabase = function (name, callback) {
|
144 |
| - console.log('AWSS3SyncAdapter.prototype.loadDatabase():', name) |
145 |
| - const s3 = new this.options.AWS.S3() |
| 144 | + console.log('AWSS3SyncAdapter.prototype.loadDatabase():', name); |
| 145 | + const s3 = new this.options.AWS.S3(); |
146 | 146 |
|
147 | 147 | var params = {
|
148 | 148 | Bucket: this.options.bucket,
|
149 | 149 | Key: name
|
150 |
| - } |
| 150 | + }; |
151 | 151 |
|
152 | 152 | s3.getObject(params, function (err, data) {
|
153 | 153 | if (err) {
|
154 |
| - console.log('AWSS3SyncAdapter.prototype.loadDatabase() Error:', err, err.stack) |
155 |
| - throw new AWSS3SyncAdapterError('Remote load failed') |
| 154 | + console.log('AWSS3SyncAdapter.prototype.loadDatabase() Error:', err, err.stack); |
| 155 | + throw new AWSS3SyncAdapterError('Remote load failed'); |
156 | 156 | } else {
|
157 | 157 | if (!data.Body) {
|
158 |
| - throw new AWSS3SyncAdapterError('Remote load failed, no data found') |
| 158 | + throw new AWSS3SyncAdapterError('Remote load failed, no data found'); |
159 | 159 | }
|
160 | 160 | try {
|
161 |
| - var base64data = data.Body.toString() |
162 |
| - var asciiData = Buffer.from(base64data, 'base64').toString() |
163 |
| - var lokiDBObj = JSON.parse(asciiData) // For json objects |
164 |
| - console.log('AWSS3SyncAdapter.prototype.loadDatabase() asciiData:', lokiDBObj) |
165 |
| - return callback(lokiDBObj) |
| 161 | + var base64data = data.Body.toString(); |
| 162 | + var asciiData = Buffer.from(base64data, 'base64').toString(); |
| 163 | + var lokiDBObj = JSON.parse(asciiData); // For json objects |
| 164 | + console.log('AWSS3SyncAdapter.prototype.loadDatabase() asciiData:', lokiDBObj); |
| 165 | + return callback(lokiDBObj); |
166 | 166 | } catch (e) {
|
167 |
| - throw new AWSS3SyncAdapterError('Remote load failed, invalid loki database') |
| 167 | + throw new AWSS3SyncAdapterError('Remote load failed, invalid loki database'); |
168 | 168 | }
|
169 | 169 | }
|
170 |
| - }) |
171 |
| - } |
| 170 | + }); |
| 171 | + }; |
172 | 172 |
|
173 |
| - return AWSS3SyncAdapter |
174 |
| - }()) |
175 |
| -})) |
| 173 | + return AWSS3SyncAdapter; |
| 174 | + }()); |
| 175 | +})); |
0 commit comments