Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@shoutem/assets-sdk

Package Overview
Dependencies
Maintainers
4
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@shoutem/assets-sdk - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2-beta.1

build/asset-uri-transformer/index.js

90

build/asset-manager/AssetManager.js

@@ -7,6 +7,6 @@ 'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _lodash = require('lodash');

@@ -40,2 +40,3 @@

var mandatoryProperties = ['assetPolicyHost', 'scopeType', 'scopeId'];
_lodash2.default.forEach(mandatoryProperties, function (property) {

@@ -48,24 +49,2 @@ if (!_lodash2.default.has(config, property)) {

function generatePolicyEndpoint(assetPolicyHost) {
return new _urijs2.default(assetPolicyHost).segment(['v1', 'asset-policies']).toString();
}
function generateHeaders() {
var headers = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return _extends({
Accept: 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json'
}, headers);
}
function createBody(attributes) {
return JSON.stringify({
data: {
type: 'shoutem.core.asset-policies',
attributes: attributes
}
});
}
var AssetManager = function (_AbstractAssetManager) {

@@ -80,3 +59,4 @@ _inherits(AssetManager, _AbstractAssetManager);

validateConfig(config);
_this.policyEndpoint = generatePolicyEndpoint(_this.config.assetPolicyHost);
_this.policyEndpoint = new _urijs2.default(config.assetPolicyHost).segment(['v1', 'asset-policies']).toString();
return _this;

@@ -87,3 +67,3 @@ }

key: 'signRequest',
value: function signRequest(path, body, reject) {
value: function signRequest(path, body) {
var _config = this.config,

@@ -97,8 +77,16 @@ scopeType = _config.scopeType,

method: 'POST',
headers: generateHeaders(headers),
body: createBody(_extends({
scopeType: scopeType,
scopeId: scopeId,
path: path
}, body))
headers: _extends({
Accept: 'application/vnd.api+json',
'Content-Type': 'application/vnd.api+json'
}, headers),
body: JSON.stringify({
data: {
type: 'shoutem.core.asset-policies',
attributes: _extends({
scopeType: scopeType,
scopeId: scopeId,
path: path
}, body)
}
})
};

@@ -108,5 +96,5 @@

return res.json();
}, reject).then(function (json) {
return Promise.resolve(json.data.attributes.signedRequest);
}, reject);
}).then(function (json) {
return _lodash2.default.get(json, 'data.attributes.signedRequest');
});
}

@@ -116,14 +104,10 @@ }, {

value: function uploadFile(path, file) {
var _this2 = this;
return this.signRequest(path, { action: 'upload', contentType: file.type }).then(function (signedRequest) {
var endpoint = signedRequest.endpoint,
formData = signedRequest.formData;
return new Promise(function (resolve, reject) {
_this2.signRequest(path, { action: 'upload', contentType: file.type }, reject).then(function (signedRequest) {
var endpoint = signedRequest.endpoint,
formData = signedRequest.formData;
services.uploadFile(file, endpoint, formData).then(function () {
var filePath = new _urijs2.default(endpoint).segment(formData.key).toString();
return resolve(filePath);
}, reject);
}, reject);
return services.uploadFile(file, endpoint, formData).then(function () {
return new _urijs2.default(endpoint).segment(formData.key).toString();
});
});

@@ -134,8 +118,4 @@ }

value: function listFolder(path) {
var _this3 = this;
return new Promise(function (resolve, reject) {
_this3.signRequest(path, { action: 'list' }, reject).then(function (signedRequest) {
return resolve(services.listFolder(signedRequest.url));
}, reject);
return this.signRequest(path, { action: 'list' }).then(function (signedRequest) {
return services.listFolder(signedRequest.url);
});

@@ -146,8 +126,4 @@ }

value: function deleteFile(path) {
var _this4 = this;
return new Promise(function (resolve, reject) {
_this4.signRequest(path, { action: 'delete' }, reject).then(function (signedRequest) {
return resolve(services.deleteFile(signedRequest.url));
}, reject);
return this.signRequest(path, { action: 'delete' }).then(function (signedRequest) {
return services.deleteFile(signedRequest.url);
});

@@ -154,0 +130,0 @@ }

@@ -20,2 +20,4 @@ 'use strict';

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function parseFolderFiles(endpoint, result) {

@@ -29,12 +31,11 @@ var files = [];

var contents = _lodash2.default.get(result, 'ListBucketResult.Contents', []);
if (!_lodash2.default.isArray(contents)) {
contents = [contents];
}
var parsedContent = _lodash2.default.get(result, 'ListBucketResult.Contents', []);
var contents = [].concat(_toConsumableArray(parsedContent));
contents.forEach(function (content) {
var size = parseInt(content.Size, 10);
var size = Number(content.Size);
if (size > 0) {
var fileRelativePath = _lodash2.default.head(content.Key);
var fileUrl = new _urijs2.default(endpoint).segment(fileRelativePath).toString();
files.push(fileUrl);

@@ -52,15 +53,15 @@ }

fetch(signedEndpoint, { method: 'GET' }).then(function (res) {
res.text().then(function (xml) {
// parseString transforms xml response to json
(0, _xml2js.parseString)(xml, function (error, result) {
if (error) {
reject(error);
return;
}
var files = parseFolderFiles(endpoint, result);
resolve(files);
});
}, reject);
}, reject);
return res.text();
}).then(function (xml) {
// parseString transforms xml response to json
(0, _xml2js.parseString)(xml, function (error, result) {
if (error) {
return reject(error);
}
var files = parseFolderFiles(endpoint, result);
return resolve(files);
});
});
});
}

@@ -37,11 +37,9 @@ 'use strict';

return new Promise(function (resolve, reject) {
fetch(endpoint, { method: 'POST', body: formData }).then(function (res) {
if (res.status !== 204) {
reject(res);
} else {
resolve(res);
}
}, reject);
return fetch(endpoint, { method: 'POST', body: formData }).then(function (response) {
if (response.status !== 204) {
throw new Error('Upload request failed');
}
return response;
});
}

@@ -24,3 +24,3 @@ 'use strict';

return new _AssetUriTransformer2.default();
}, 'Cannot create AssetUriTransformer. Parameter \'remoteHost\' has to be defined');
}, 'Parameter \'remoteHost\' is missing');
});

@@ -31,3 +31,3 @@

return new _AssetUriTransformer2.default('cdn.shoutem.com');
}, 'Cannot create AssetUriTransformer. Parameter \'remotePathPrefix\' has to be defined');
}, 'Parameter \'remotePathPrefix\' is missing');
});

@@ -38,7 +38,7 @@

return new _AssetUriTransformer2.default('cdn.shoutem.com', 'bucket');
}, 'Cannot create AssetUriTransformer. Parameter \'scopeContext\' has to be defined');
}, 'Parameter \'scopeContext\' is missing');
});
});
var defaultRemoteHost = 'cnd.shoutem.com';
var defaultRemoteHost = 'cdn.shoutem.com';
var defaultRemotePathPrefix = 'bucket';

@@ -57,7 +57,7 @@ var defaultScopeType = 'application';

var uri = 'http://www.imagehosting.com/image.jpg';
var remoteHost = 'cnd.shoutem.com';
var remoteHost = 'cdn.shoutem.com';
var assetUriTransformer = createAssetUriTransformer();
_chai.assert.throws(function () {
return assetUriTransformer.createAssetUri(uri);
}, 'Cannot create asset URI. Host in ' + uri + ' is not valid. Expected to match ' + remoteHost);
}, uri + ' is not valid. Host mismatch: ' + remoteHost + ' www.imagehosting.com');
});

@@ -70,3 +70,3 @@

return assetUriTransformer.createAssetUri(uri);
}, 'Cannot create asset URI. Path in ' + uri + ' does not start with ' + defaultRemotePathPrefix);
}, uri + ' is not valid. Path does not start with \'' + defaultRemotePathPrefix + '\'');
});

@@ -80,7 +80,7 @@

return assetUriTransformer.createAssetUri(uri);
}, 'Cannot create asset URI. Scope context does not contain value for \'' + scopeType + '\'');
}, uri + ' is not valid. Scope type is not valid: \'' + scopeType + '\'');
});
it('validates URI scope ID', function () {
var uriScopeId = 666;
var uriScopeId = '666';
var uri = 'http://' + defaultRemoteHost + '/' + defaultRemotePathPrefix + '/application/' + uriScopeId + '/image.jpg';

@@ -92,3 +92,3 @@ var assetUriTransformer = createAssetUriTransformer();

// eslint-disable-next-line max-len
'Cannot create asset URI. Scope ID ' + defaultScopeId + ' is not equal to URI scope ID \'' + uriScopeId + '\'');
uri + ' is not valid. Scope id is not valid: \'' + uriScopeId + '\'');
});

@@ -112,3 +112,3 @@

var expectedAssetPath = new _urijs2.default().protocol(_const.assetSchema).host(defaultScopeType).path(path.replace(defaultScopeTypeIdPrefix, '')).toString();
var expectedAssetPath = new _urijs2.default().protocol(_const.ASSET_SCHEMA).host(defaultScopeType).path(path.replace(defaultScopeTypeIdPrefix, '')).toString();

@@ -140,3 +140,3 @@ _chai.assert.strictEqual(actualAssetPath, expectedAssetPath);

return assetUriTransformer.resolveAssetUri(uri);
}, 'Cannot resolve asset URI. ' + uri + ' is not a valid asset URI');
}, uri + ' is not valid. ');
});

@@ -146,6 +146,7 @@

var scopeType = 'unknown';
var uri = 'asset://' + scopeType + '/image.jpg';
var assetUriTransformer = createAssetUriTransformer();
_chai.assert.throws(function () {
return assetUriTransformer.resolveAssetUri('asset://' + scopeType + '/image.jpg');
}, 'Cannot resolve asset URI. Scope context does not contain value for \'' + scopeType + '\'');
return assetUriTransformer.resolveAssetUri(uri);
}, uri + ' is not valid. Scope type is not valid: \'' + scopeType + '\'');
});

@@ -165,3 +166,3 @@

var assetUri = new _urijs2.default(path).protocol(_const.assetSchema).host(defaultScopeType).toString();
var assetUri = new _urijs2.default(path).protocol(_const.ASSET_SCHEMA).host(defaultScopeType).toString();

@@ -168,0 +169,0 @@ var actualAbsolutePath = assetUriTransformer.resolveAssetUri(assetUri);

@@ -9,4 +9,2 @@ 'use strict';

exports.isValidAssetUri = isValidAssetUri;
var _lodash = require('lodash');

@@ -22,2 +20,6 @@

var _services = require('./services');
var _errors = require('../errors');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -27,19 +29,2 @@

function isValidAssetUri(uri) {
var uriObj = new _urijs2.default(uri);
return uriObj.protocol() === _const.assetSchema && uriObj.host() && uriObj.path();
}
function createConstructorParameterNotDefinedError(parameter) {
return new Error('Cannot create AssetUriTransformer. Parameter \'' + parameter + '\' has to be defined');
}
function createAssetUriCreationError(message) {
return new Error('Cannot create asset URI. ' + message);
}
function createAssetUriResolutionError(message) {
return new Error('Cannot resolve asset URI. ' + message);
}
var AssetUriTransformer = function () {

@@ -50,9 +35,9 @@ function AssetUriTransformer(remoteHost, remotePathPrefix, scopeContext) {

if (!remoteHost) {
throw createConstructorParameterNotDefinedError('remoteHost');
throw new _errors.ParameterNotDefinedError('remoteHost');
}
if (!remotePathPrefix) {
throw createConstructorParameterNotDefinedError('remotePathPrefix');
throw new _errors.ParameterNotDefinedError('remotePathPrefix');
}
if (!scopeContext) {
throw createConstructorParameterNotDefinedError('scopeContext');
throw new _errors.ParameterNotDefinedError('scopeContext');
}

@@ -62,4 +47,5 @@

this.remotePathPrefix = remotePathPrefix;
this.scopeContext = scopeContext;
this.remotePathPrefixSegments = new _urijs2.default().path(remotePathPrefix).segment();
this.scopeContext = scopeContext;
}

@@ -70,45 +56,74 @@

value: function createAssetUri(uri) {
var remoteHost = this.remoteHost,
remotePathPrefix = this.remotePathPrefix,
remotePathPrefixSegments = this.remotePathPrefixSegments,
scopeContext = this.scopeContext;
var assetUri = new _urijs2.default(uri);
var remotePathPrefixSegmentLength = remotePathPrefixSegments.length;
this.validateHost(assetUri);
this.validatePathPrefix(assetUri);
var uriObj = new _urijs2.default(uri);
var _resolveUriParts = this.resolveUriParts(assetUri),
scopeType = _resolveUriParts.scopeType,
segmentsWithoutScope = _resolveUriParts.segmentsWithoutScope;
var uriHost = uriObj.host();
if (uriHost !== remoteHost) {
throw createAssetUriCreationError('Host in ' + uri + ' is not valid. Expected to match ' + remoteHost);
return new _urijs2.default().protocol(_const.ASSET_SCHEMA).host(scopeType).segment(segmentsWithoutScope).normalize().toString();
}
}, {
key: 'validateHost',
value: function validateHost(assetUri) {
var remoteHost = this.remoteHost;
var assetUriHost = assetUri.host();
if (assetUriHost !== remoteHost) {
throw new _errors.InvalidUriError(assetUri, 'Host mismatch: ' + remoteHost + ' ' + assetUriHost);
}
}
}, {
key: 'validatePathPrefix',
value: function validatePathPrefix(assetUri) {
var remotePathPrefix = this.remotePathPrefix,
remotePathPrefixSegments = this.remotePathPrefixSegments;
var uriSegments = uriObj.segment();
var scopeTypeIdSegments = uriSegments.slice(0, remotePathPrefixSegmentLength);
var scopeTypeIdSegments = _lodash2.default.take(assetUri.segment(), remotePathPrefixSegments.length);
if (!_lodash2.default.isEqual(scopeTypeIdSegments, remotePathPrefixSegments)) {
throw createAssetUriCreationError('Path in ' + uri + ' does not start with ' + remotePathPrefix);
throw new _errors.InvalidUriError(assetUri, 'Path does not start with \'' + remotePathPrefix + '\'');
}
}
}, {
key: 'resolveUriParts',
value: function resolveUriParts(assetUri) {
var scopeContext = this.scopeContext,
remotePathPrefixSegments = this.remotePathPrefixSegments;
var uriScopeType = uriSegments[remotePathPrefixSegmentLength];
var scopeId = scopeContext[uriScopeType];
if (scopeId === undefined) {
throw createAssetUriCreationError('Scope context does not contain value for \'' + uriScopeType + '\'');
var assetUriSegments = assetUri.segment();
// removes prefix part from asset uri
var assetUriBase = _lodash2.default.slice(assetUriSegments, remotePathPrefixSegments.length);
var scopeType = _lodash2.default.get(assetUriBase, '[0]');
var scopeId = _lodash2.default.get(assetUriBase, '[1]');
// relative path is located after scope type and scope ID
var segmentsWithoutScope = _lodash2.default.slice(assetUriBase, 2);
var isMatchingScopeType = _lodash2.default.has(scopeContext, scopeType);
if (!isMatchingScopeType) {
throw new _errors.InvalidUriError(assetUri, 'Scope type is not valid: \'' + scopeType + '\'');
}
// scope ID is located after scope type
var uriScopeIdSegmentIndex = remotePathPrefixSegmentLength + 1;
var uriScopeId = uriSegments[uriScopeIdSegmentIndex];
var scopeIdStr = '' + scopeId;
if (scopeIdStr !== uriScopeId) {
throw createAssetUriCreationError('Scope ID ' + scopeId + ' is not equal to URI scope ID \'' + uriScopeId + '\'');
var isMatchingScopeId = scopeContext[scopeType] === scopeId;
if (!isMatchingScopeId) {
throw new _errors.InvalidUriError(assetUri, 'Scope id is not valid: \'' + scopeId + '\'');
}
// relative path is located after scope type and scope ID
var uriRelativePathSegmentStartIndex = remotePathPrefixSegmentLength + 2;
var segmentsWithoutScopeTypeId = uriSegments.slice(uriRelativePathSegmentStartIndex);
return new _urijs2.default().protocol(_const.assetSchema).host(uriScopeType).segment(segmentsWithoutScopeTypeId).normalize().toString();
return {
scopeType: scopeType,
scopeId: scopeId,
segmentsWithoutScope: segmentsWithoutScope
};
}
}, {
key: 'resolveAssetUri',
value: function resolveAssetUri(assetUri) {
value: function resolveAssetUri(uri) {
var remoteHost = this.remoteHost,

@@ -119,17 +134,18 @@ remotePathPrefix = this.remotePathPrefix,

if (!isValidAssetUri(assetUri)) {
throw createAssetUriResolutionError(assetUri + ' is not a valid asset URI');
if (!(0, _services.isValidAssetUri)(uri)) {
throw new _errors.InvalidUriError(uri);
}
var scopeType = new _urijs2.default(assetUri).host();
var assetUri = new _urijs2.default(uri);
var scopeType = assetUri.host();
var scopeId = scopeContext[scopeType];
if (scopeId === undefined) {
throw createAssetUriResolutionError('Scope context does not contain value for \'' + scopeType + '\'');
if (!scopeId) {
throw new _errors.InvalidUriError(assetUri, 'Scope type is not valid: \'' + scopeType + '\'');
}
var path = new _urijs2.default(assetUri).path();
var scopeIdStr = '' + scopeId;
return new _urijs2.default().host(remoteHost).segment([remotePathPrefix, scopeType, scopeIdStr, path]).normalize().toString();
return new _urijs2.default().host(remoteHost).segment([remotePathPrefix, scopeType, scopeId, path]).normalize().toString();
}

@@ -136,0 +152,0 @@ }]);

@@ -6,2 +6,2 @@ 'use strict';

});
var assetSchema = exports.assetSchema = 'asset';
var ASSET_SCHEMA = exports.ASSET_SCHEMA = 'asset';
{
"name": "@shoutem/assets-sdk",
"version": "0.0.1",
"version": "0.0.2-beta.1",
"files": [

@@ -31,2 +31,3 @@ "build"

"babel-cli": "6.5.1",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-plugin-transform-object-rest-spread": "6.6.5",

@@ -33,0 +34,0 @@ "babel-preset-es2015": "^6.18.0",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc