UNPKG

3.02 kBJavaScriptView Raw
1// Copyright IBM Corp. 2013,2014. All Rights Reserved.
2// Node module: loopback-component-storage
3// This file is licensed under the Artistic License 2.0.
4// License text available at https://opensource.org/licenses/Artistic-2.0
5'use strict';
6
7var pkgcloud = require('pkgcloud');
8
9/*!
10 * Patch the prototype for a given subclass of Container or File
11 * @param {Function} cls The subclass
12 */
13function patchBaseClass(cls) {
14 var proto = cls.prototype;
15 var found = false;
16 // Find the prototype that owns the _setProperties method
17 while (proto &&
18 proto.constructor !== pkgcloud.storage.Container &&
19 proto.constructor !== pkgcloud.storage.File) {
20 if (proto.hasOwnProperty('_setProperties')) {
21 found = true;
22 break;
23 } else {
24 proto = Object.getPrototypeOf(proto);
25 }
26 }
27 if (!found) {
28 proto = cls.prototype;
29 }
30 var m1 = proto._setProperties;
31 proto._setProperties = function(details) {
32 // Use an empty object to receive the calculated properties from details
33 var receiver = {};
34 // Pass in some context as non-enumerable properties
35 Object.defineProperties(receiver, {
36 client: {value: this.client},
37 files: {value: this.files},
38 });
39 m1.call(receiver, details);
40 // Apply the calculated properties to this
41 for (var p in receiver) {
42 this[p] = receiver[p];
43 }
44 // Keep references to raw and the calculated properties
45 this._rawMetadata = details;
46 this._metadata = receiver; // Use _metadata to avoid conflicts
47 };
48
49 proto.toJSON = function() {
50 return this._metadata;
51 };
52
53 proto.getMetadata = function() {
54 return this._metadata;
55 };
56
57 proto.getRawMetadata = function() {
58 return this._rawMetadata;
59 };
60}
61/*!
62 * Patch the pkgcloud Container/File classes so that the metadata are separately
63 * stored for JSON serialization
64 *
65 * @param {String} provider The name of the storage provider
66 */
67function patchContainerAndFileClass(provider) {
68 var storageProvider = getProvider(provider).storage;
69
70 patchBaseClass(storageProvider.Container);
71 patchBaseClass(storageProvider.File);
72}
73/**
74 * Create a client instance based on the options
75 * @param options
76 * @returns {*}
77 */
78function createClient(options) {
79 options = options || {};
80 var provider = options.provider || 'filesystem';
81 var handler;
82
83 try {
84 // Try to load the provider from providers folder
85 handler = require('./providers/' + provider);
86 } catch (err) {
87 // Fall back to pkgcloud
88 handler = require('pkgcloud').storage;
89 }
90 patchContainerAndFileClass(provider);
91 return handler.createClient(options);
92}
93
94/**
95 * Look up a provider by name
96 * @param provider
97 * @returns {*}
98 */
99function getProvider(provider) {
100 try {
101 // Try to load the provider from providers folder
102 return require('./providers/' + provider);
103 } catch (err) {
104 // Fall back to pkgcloud
105 return require('pkgcloud').providers[provider];
106 }
107}
108
109module.exports.createClient = createClient;
110module.exports.getProvider = getProvider;