moltin / js-sdk

JavaScript & Node.js SDKs for the Elastic Path Commerce Cloud eCommerce API
http://documentation.elasticpath.com/
MIT License
173 stars 77 forks source link

localStorage module error on AWS Lambda #131

Closed acorer closed 6 years ago

acorer commented 6 years ago

Steps to reproduce:

  1. Create a lambda function
  2. Import moltin sdk const MoltinGateway = require('@moltin/sdk').gateway;

And optionally

const Moltin = MoltinGateway({
  client_id:process.env.moltin_client_id,
  client_secret: process.env.moltin_secret
});

// some operations like Authenticate

This gives an error when the function is invoked START RequestId: 7cb9c41f.... Version: $LATEST module initialization error: Error at Error (native) at Object.fs.mkdirSync (fs.js:923:18) at LocalStorage._init (/var/task/node_modules/node-localstorage/LocalStorage.js:151:12) at new LocalStorage (/var/task/node_modules/node-localstorage/LocalStorage.js:121:12) at new StorageFactory (/var/task/node_modules/@moltin/sdk/dist/moltin.cjs.js:1:854) at cartIdentifier (/var/task/node_modules/@moltin/sdk/dist/moltin.cjs.js:1:2009) at new Moltin (/var/task/node_modules/@moltin/sdk/dist/moltin.cjs.js:1:11644) at gateway (/var/task/node_modules/@moltin/sdk/dist/moltin.cjs.js:1:12252) at Object. (/var/task/index.js:6:16) at Module._compile (module.js:570:32) END RequestId: 7cb9c41f-1e56-11e8-bc01-115ee78d5e05 REPORT RequestId: 7cb9c41f-1e56-11e8-bc01-115ee78d5e05 Duration: 250.42 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 25 MB
module initialization error Error

notrab commented 6 years ago

Hi @acorer

I'm not sure of the best way to move forward with this issue right now but we're open for suggestions on getting the SDK working with Lambda functions.

Once the SDK supports custom storage as requested in #106 it would be a matter of creating a storage engine that is compatible with lambda functions.

acorer commented 6 years ago

For now, what we could do is to provide a config item for localStorage path, so that the user can at least select the path he'd like the localStorage folder be created in.

In my case, I had to change the file "src/factories/storage.js" manually to replace the static string which initializes localStorage.

Present code:

// src/factories/storage.js
class StorageFactory {
  constructor() {
    if (typeof localStorage === 'undefined' || localStorage === null) {
      const LocalStorage = require('node-localstorage').LocalStorage;

      this.localStorage = new LocalStorage('./localStorage');
      console.log(this.localStorage);
    } else {
      this.localStorage = window.localStorage;
    }
  }

So instead of using const string "./localStorage", there needs to be a way where we can use it via config file. I changed './localStorage' to '/tmp/localStorage' to make sure it worked on AWS Lambda, but as you see editing this file is not at all a good practice.

notrab commented 6 years ago

Resolved in 3.14.0