Skip to content

Commit

Permalink
Improves loading of Push Adapter, fix loading of S3Adapter
Browse files Browse the repository at this point in the history
- Adds environment variables to configure S3Adapter
  • Loading branch information
flovilmart committed Mar 4, 2016
1 parent e3d26e2 commit 2fc67a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
7 changes: 0 additions & 7 deletions src/Adapters/AdapterLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ export function loadAdapter(adapter, defaultAdapter, options) {
return loadAdapter(adapter.class, undefined, adapter.options);
} else if (adapter.adapter) {
return loadAdapter(adapter.adapter, undefined, adapter.options);
} else {
// Try to load the defaultAdapter with the options
// The default adapter should throw if the options are
// incompatible
try {
return loadAdapter(defaultAdapter, undefined, adapter);
} catch (e) {};
}
// return the adapter as is as it's unusable otherwise
return adapter;
Expand Down
35 changes: 18 additions & 17 deletions src/Adapters/Files/S3Adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@ import requiredParameter from '../../requiredParameter';

const DEFAULT_S3_REGION = "us-east-1";

function parseS3AdapterOptions(...options) {
if (options.length === 1 && typeof options[0] == "object") {
return options;
function requiredOrFromEnvironment(env, name) {
let environmentVariable = process.env[env];
if (!environmentVariable) {
requiredParameter(`S3Adapter requires an ${name}`);
}

const additionalOptions = options[3] || {};

return {
accessKey: options[0],
secretKey: options[1],
bucket: options[2],
region: additionalOptions.region
return environmentVariable;
}

function fromEnvironmentOrDefault(env, defaultValue) {
let environmentVariable = process.env[env];
if (environmentVariable) {
return environmentVariable;
}
return defaultValue;
}

export class S3Adapter extends FilesAdapter {
// Creates an S3 session.
// Providing AWS access and secret keys is mandatory
// Region and bucket will use sane defaults if omitted
constructor(
accessKey = requiredParameter('S3Adapter requires an accessKey'),
secretKey = requiredParameter('S3Adapter requires a secretKey'),
bucket,
{ region = DEFAULT_S3_REGION,
bucketPrefix = '',
directAccess = false } = {}) {
accessKey = requiredOrFromEnvironment('S3_ACCESS_KEY', 'accessKey'),
secretKey = requiredOrFromEnvironment('S3_SECRET_KEY', 'secretKey'),
bucket = fromEnvironmentOrDefault('S3_BUCKET', undefined),
{ region = fromEnvironmentOrDefault('S3_REGION', DEFAULT_S3_REGION),
bucketPrefix = fromEnvironmentOrDefault('S3_BUCKET_PREFIX', ''),
directAccess = fromEnvironmentOrDefault('S3_DIRECT_ACCESS', false) } = {}) {
super();

this._region = region;
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ function ParseServer({
const filesControllerAdapter = loadAdapter(filesAdapter, () => {
return new GridStoreAdapter(databaseURI);
});
const pushControllerAdapter = loadAdapter(push, ParsePushAdapter);
// 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);
// We pass the options and the base class for the adatper,
Expand Down

0 comments on commit 2fc67a5

Please sign in to comment.