@magic-sdk/provider
Advanced tools
Comparing version 2.7.0 to 2.8.0
@@ -15,2 +15,8 @@ ## Upcoming Changes | ||
## `2.8.0` - 09/24/2020 | ||
#### Added | ||
- Adds a Magic SDK extensions runtime compatibility check, ensuring you're version of Magic SDK is designed for the extensions you have in use. | ||
## `2.7.0` - 09/15/2020 | ||
@@ -17,0 +23,0 @@ |
@@ -24,2 +24,6 @@ import localForage from 'localforage'; | ||
export declare function createSDK<SDK extends SDKBase>(SDKBaseCtor: ConstructorOf<SDK>, environment: SDKEnvironment): WithExtensions<SDK>; | ||
export declare const envNameToNpmName: { | ||
'magic-sdk': "magic-sdk"; | ||
'magic-sdk-rn': "@magic-sdk/react-native"; | ||
}; | ||
export {}; |
@@ -9,2 +9,6 @@ "use strict"; | ||
exports.createSDK = createSDK; | ||
exports.envNameToNpmName = { | ||
'magic-sdk': 'magic-sdk', | ||
'magic-sdk-rn': '@magic-sdk/react-native', | ||
}; | ||
//# sourceMappingURL=sdk-environment.js.map |
@@ -72,2 +72,3 @@ import { JsonRpcError, RPCErrorCode, SDKErrorCode, SDKWarningCode } from '@magic-sdk/types'; | ||
export declare function createWebAuthCreateCredentialError(message: string): MagicSDKError; | ||
export declare function createIncompatibleExtensionsError(extensions: Extension<string>[]): MagicSDKError; | ||
export declare function createInvalidArgumentError(options: { | ||
@@ -74,0 +75,0 @@ procedure: string; |
@@ -141,2 +141,22 @@ "use strict"; | ||
exports.createWebAuthCreateCredentialError = createWebAuthCreateCredentialError; | ||
function createIncompatibleExtensionsError(extensions) { | ||
var npmName = sdk_environment_1.envNameToNpmName[sdk_environment_1.SDKEnvironment.sdkName]; | ||
var msg = "Some extensions are incompatible with `" + npmName + "@" + sdk_environment_1.SDKEnvironment.version + "`:"; | ||
extensions | ||
.filter(function (ext) { return typeof ext.compat !== 'undefined' && ext.compat !== null; }) | ||
.forEach(function (ext) { | ||
var compat = ext.compat[npmName]; | ||
/* istanbul ignore else */ | ||
if (typeof compat === 'string') { | ||
msg += "\n - Extension `" + ext.name + "` supports version(s) `" + compat + "`"; | ||
} | ||
else if (!compat) { | ||
msg += "\n - Extension `" + ext.name + "` does not support " + sdk_environment_1.SDKEnvironment.target + " environments."; | ||
} | ||
// Else case is irrelevant here here | ||
// (we filter out extensions with missing `compat` field) | ||
}); | ||
return new MagicSDKError(types_1.SDKErrorCode.IncompatibleExtensions, msg); | ||
} | ||
exports.createIncompatibleExtensionsError = createIncompatibleExtensionsError; | ||
function createInvalidArgumentError(options) { | ||
@@ -179,7 +199,3 @@ /** | ||
var method = options.method, removalVersions = options.removalVersions, useInstead = options.useInstead; | ||
var envNameToNpmName = { | ||
'magic-sdk': 'magic-sdk', | ||
'magic-sdk-rn': '@magic-sdk/react-native', | ||
}; | ||
var npmName = envNameToNpmName[sdk_environment_1.SDKEnvironment.sdkName]; | ||
var npmName = sdk_environment_1.envNameToNpmName[sdk_environment_1.SDKEnvironment.sdkName]; | ||
var removalVersion = removalVersions[npmName]; | ||
@@ -186,0 +202,0 @@ var useInsteadSuffix = useInstead ? " Use `" + useInstead + "` instead." : ''; |
@@ -39,3 +39,7 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var satisfies_1 = __importDefault(require("semver/functions/satisfies")); | ||
var base64_json_1 = require("../util/base64-json"); | ||
@@ -50,2 +54,73 @@ var sdk_exceptions_1 = require("./sdk-exceptions"); | ||
var sdk_environment_1 = require("./sdk-environment"); | ||
/** | ||
* Checks if the given `ext` is compatible with the platform & version of Magic | ||
* SDK currently in use. | ||
*/ | ||
function checkExtensionCompat(ext) { | ||
if (ext.compat) { | ||
// Check web compatibility | ||
if (sdk_environment_1.SDKEnvironment.sdkName === 'magic-sdk') { | ||
return typeof ext.compat['magic-sdk'] === 'string' | ||
? satisfies_1.default(sdk_environment_1.SDKEnvironment.version, ext.compat['magic-sdk']) | ||
: !!ext.compat['magic-sdk']; | ||
} | ||
// Check React Native compatibility | ||
/* istanbul ignore else */ | ||
if (sdk_environment_1.SDKEnvironment.sdkName === 'magic-sdk-rn') { | ||
return typeof ext.compat['@magic-sdk/react-native'] === 'string' | ||
? satisfies_1.default(sdk_environment_1.SDKEnvironment.version, ext.compat['@magic-sdk/react-native']) | ||
: !!ext.compat['@magic-sdk/react-native']; | ||
} | ||
// Else case should be impossible here... | ||
} | ||
// To gracefully support older extensions, we assume | ||
// compatibility when the `compat` field is missing. | ||
return true; | ||
} | ||
/** | ||
* Initializes SDK extensions, checks for platform/version compatiblity issues, | ||
* then consolidates any global configurations provided by those extensions. | ||
*/ | ||
function prepareExtensions(options) { | ||
var _this = this; | ||
var _a; | ||
var extensions = (_a = options === null || options === void 0 ? void 0 : options.extensions) !== null && _a !== void 0 ? _a : []; | ||
var extConfig = {}; | ||
var incompatibleExtensions = []; | ||
if (Array.isArray(extensions)) { | ||
extensions.forEach(function (ext) { | ||
if (checkExtensionCompat(ext)) { | ||
ext.init(_this); | ||
_this[ext.name] = ext; | ||
if (ext instanceof base_extension_1.Extension.Internal) { | ||
if (!type_guards_1.isEmpty(ext.config)) | ||
extConfig[ext.name] = ext.config; | ||
} | ||
} | ||
else { | ||
incompatibleExtensions.push(ext); | ||
} | ||
}); | ||
} | ||
else { | ||
Object.keys(extensions).forEach(function (name) { | ||
if (checkExtensionCompat(extensions[name])) { | ||
extensions[name].init(_this); | ||
var ext = extensions[name]; | ||
_this[name] = ext; | ||
if (ext instanceof base_extension_1.Extension.Internal) { | ||
if (!type_guards_1.isEmpty(ext.config)) | ||
extConfig[extensions[name].name] = ext.config; | ||
} | ||
} | ||
else { | ||
incompatibleExtensions.push(extensions[name]); | ||
} | ||
}); | ||
} | ||
if (incompatibleExtensions.length) { | ||
throw sdk_exceptions_1.createIncompatibleExtensionsError(incompatibleExtensions); | ||
} | ||
return extConfig; | ||
} | ||
var SDKBase = /** @class */ (function () { | ||
@@ -56,4 +131,3 @@ /** | ||
function SDKBase(apiKey, options) { | ||
var _this = this; | ||
var _a, _b; | ||
var _a; | ||
this.apiKey = apiKey; | ||
@@ -72,25 +146,3 @@ if (!apiKey) | ||
// Prepare Extensions | ||
var extensions = (_b = options === null || options === void 0 ? void 0 : options.extensions) !== null && _b !== void 0 ? _b : []; | ||
var extConfig = {}; | ||
if (Array.isArray(extensions)) { | ||
extensions.forEach(function (ext) { | ||
ext.init(_this); | ||
_this[ext.name] = ext; | ||
if (ext instanceof base_extension_1.Extension.Internal) { | ||
if (!type_guards_1.isEmpty(ext.config)) | ||
extConfig[ext.name] = ext.config; | ||
} | ||
}); | ||
} | ||
else { | ||
Object.keys(extensions).forEach(function (name) { | ||
extensions[name].init(_this); | ||
var ext = extensions[name]; | ||
_this[name] = ext; | ||
if (ext instanceof base_extension_1.Extension.Internal) { | ||
if (!type_guards_1.isEmpty(ext.config)) | ||
extConfig[extensions[name].name] = ext.config; | ||
} | ||
}); | ||
} | ||
var extConfig = prepareExtensions.call(this, options); | ||
// Build query params for the current `ViewController` | ||
@@ -97,0 +149,0 @@ this.encodedQueryParams = base64_json_1.encodeJSON({ |
@@ -6,2 +6,8 @@ import { createJsonRpcRequestPayload, standardizeJsonRpcRequestPayload } from '../core/json-rpc'; | ||
import { createPromiEvent, encodeJSON, decodeJSON, encodeQueryParameters, decodeQueryParameters, storage, isPromiEvent } from '../util'; | ||
interface BaseExtension<TName extends string> extends BaseModule { | ||
compat?: { | ||
'magic-sdk': boolean | string; | ||
'@magic-sdk/react-native': boolean | string; | ||
}; | ||
} | ||
declare abstract class BaseExtension<TName extends string> extends BaseModule { | ||
@@ -74,3 +80,3 @@ abstract readonly name: TName; | ||
*/ | ||
declare type HiddenExtensionFields = 'name' | 'init' | 'config'; | ||
declare type HiddenExtensionFields = 'name' | 'init' | 'config' | 'compat'; | ||
/** | ||
@@ -77,0 +83,0 @@ * Gets the type contained in an array type. |
@@ -37,3 +37,3 @@ "use strict"; | ||
var sdkAccessFields = ['request', 'transport', 'overlay', 'sdk']; | ||
// Dissallow SDK access before initialization. | ||
// Disallow SDK access before initialization. | ||
return new Proxy(_this, { | ||
@@ -40,0 +40,0 @@ get: function (target, prop, receiver) { |
@@ -24,2 +24,6 @@ import localForage from 'localforage'; | ||
export declare function createSDK<SDK extends SDKBase>(SDKBaseCtor: ConstructorOf<SDK>, environment: SDKEnvironment): WithExtensions<SDK>; | ||
export declare const envNameToNpmName: { | ||
'magic-sdk': "magic-sdk"; | ||
'magic-sdk-rn': "@magic-sdk/react-native"; | ||
}; | ||
export {}; |
@@ -6,2 +6,6 @@ export var SDKEnvironment = {}; | ||
} | ||
export var envNameToNpmName = { | ||
'magic-sdk': 'magic-sdk', | ||
'magic-sdk-rn': '@magic-sdk/react-native', | ||
}; | ||
//# sourceMappingURL=sdk-environment.js.map |
@@ -72,2 +72,3 @@ import { JsonRpcError, RPCErrorCode, SDKErrorCode, SDKWarningCode } from '@magic-sdk/types'; | ||
export declare function createWebAuthCreateCredentialError(message: string): MagicSDKError; | ||
export declare function createIncompatibleExtensionsError(extensions: Extension<string>[]): MagicSDKError; | ||
export declare function createInvalidArgumentError(options: { | ||
@@ -74,0 +75,0 @@ procedure: string; |
@@ -16,3 +16,3 @@ var __extends = (this && this.__extends) || (function () { | ||
import { isJsonRpcErrorCode } from '../util/type-guards'; | ||
import { SDKEnvironment } from './sdk-environment'; | ||
import { SDKEnvironment, envNameToNpmName } from './sdk-environment'; | ||
// --- Error/warning classes | ||
@@ -134,2 +134,21 @@ /** | ||
} | ||
export function createIncompatibleExtensionsError(extensions) { | ||
var npmName = envNameToNpmName[SDKEnvironment.sdkName]; | ||
var msg = "Some extensions are incompatible with `" + npmName + "@" + SDKEnvironment.version + "`:"; | ||
extensions | ||
.filter(function (ext) { return typeof ext.compat !== 'undefined' && ext.compat !== null; }) | ||
.forEach(function (ext) { | ||
var compat = ext.compat[npmName]; | ||
/* istanbul ignore else */ | ||
if (typeof compat === 'string') { | ||
msg += "\n - Extension `" + ext.name + "` supports version(s) `" + compat + "`"; | ||
} | ||
else if (!compat) { | ||
msg += "\n - Extension `" + ext.name + "` does not support " + SDKEnvironment.target + " environments."; | ||
} | ||
// Else case is irrelevant here here | ||
// (we filter out extensions with missing `compat` field) | ||
}); | ||
return new MagicSDKError(SDKErrorCode.IncompatibleExtensions, msg); | ||
} | ||
export function createInvalidArgumentError(options) { | ||
@@ -168,6 +187,2 @@ /** | ||
var method = options.method, removalVersions = options.removalVersions, useInstead = options.useInstead; | ||
var envNameToNpmName = { | ||
'magic-sdk': 'magic-sdk', | ||
'magic-sdk-rn': '@magic-sdk/react-native', | ||
}; | ||
var npmName = envNameToNpmName[SDKEnvironment.sdkName]; | ||
@@ -174,0 +189,0 @@ var removalVersion = removalVersions[npmName]; |
@@ -38,4 +38,5 @@ /* eslint-disable no-underscore-dangle, no-param-reassign */ | ||
}; | ||
import semverSatisfies from 'semver/functions/satisfies'; | ||
import { encodeJSON } from '../util/base64-json'; | ||
import { createMissingApiKeyError, createReactNativeEndpointConfigurationWarning } from './sdk-exceptions'; | ||
import { createMissingApiKeyError, createReactNativeEndpointConfigurationWarning, createIncompatibleExtensionsError, } from './sdk-exceptions'; | ||
import { AuthModule } from '../modules/auth'; | ||
@@ -48,2 +49,73 @@ import { UserModule } from '../modules/user'; | ||
import { SDKEnvironment } from './sdk-environment'; | ||
/** | ||
* Checks if the given `ext` is compatible with the platform & version of Magic | ||
* SDK currently in use. | ||
*/ | ||
function checkExtensionCompat(ext) { | ||
if (ext.compat) { | ||
// Check web compatibility | ||
if (SDKEnvironment.sdkName === 'magic-sdk') { | ||
return typeof ext.compat['magic-sdk'] === 'string' | ||
? semverSatisfies(SDKEnvironment.version, ext.compat['magic-sdk']) | ||
: !!ext.compat['magic-sdk']; | ||
} | ||
// Check React Native compatibility | ||
/* istanbul ignore else */ | ||
if (SDKEnvironment.sdkName === 'magic-sdk-rn') { | ||
return typeof ext.compat['@magic-sdk/react-native'] === 'string' | ||
? semverSatisfies(SDKEnvironment.version, ext.compat['@magic-sdk/react-native']) | ||
: !!ext.compat['@magic-sdk/react-native']; | ||
} | ||
// Else case should be impossible here... | ||
} | ||
// To gracefully support older extensions, we assume | ||
// compatibility when the `compat` field is missing. | ||
return true; | ||
} | ||
/** | ||
* Initializes SDK extensions, checks for platform/version compatiblity issues, | ||
* then consolidates any global configurations provided by those extensions. | ||
*/ | ||
function prepareExtensions(options) { | ||
var _this = this; | ||
var _a; | ||
var extensions = (_a = options === null || options === void 0 ? void 0 : options.extensions) !== null && _a !== void 0 ? _a : []; | ||
var extConfig = {}; | ||
var incompatibleExtensions = []; | ||
if (Array.isArray(extensions)) { | ||
extensions.forEach(function (ext) { | ||
if (checkExtensionCompat(ext)) { | ||
ext.init(_this); | ||
_this[ext.name] = ext; | ||
if (ext instanceof Extension.Internal) { | ||
if (!isEmpty(ext.config)) | ||
extConfig[ext.name] = ext.config; | ||
} | ||
} | ||
else { | ||
incompatibleExtensions.push(ext); | ||
} | ||
}); | ||
} | ||
else { | ||
Object.keys(extensions).forEach(function (name) { | ||
if (checkExtensionCompat(extensions[name])) { | ||
extensions[name].init(_this); | ||
var ext = extensions[name]; | ||
_this[name] = ext; | ||
if (ext instanceof Extension.Internal) { | ||
if (!isEmpty(ext.config)) | ||
extConfig[extensions[name].name] = ext.config; | ||
} | ||
} | ||
else { | ||
incompatibleExtensions.push(extensions[name]); | ||
} | ||
}); | ||
} | ||
if (incompatibleExtensions.length) { | ||
throw createIncompatibleExtensionsError(incompatibleExtensions); | ||
} | ||
return extConfig; | ||
} | ||
var SDKBase = /** @class */ (function () { | ||
@@ -54,4 +126,3 @@ /** | ||
function SDKBase(apiKey, options) { | ||
var _this = this; | ||
var _a, _b; | ||
var _a; | ||
this.apiKey = apiKey; | ||
@@ -70,25 +141,3 @@ if (!apiKey) | ||
// Prepare Extensions | ||
var extensions = (_b = options === null || options === void 0 ? void 0 : options.extensions) !== null && _b !== void 0 ? _b : []; | ||
var extConfig = {}; | ||
if (Array.isArray(extensions)) { | ||
extensions.forEach(function (ext) { | ||
ext.init(_this); | ||
_this[ext.name] = ext; | ||
if (ext instanceof Extension.Internal) { | ||
if (!isEmpty(ext.config)) | ||
extConfig[ext.name] = ext.config; | ||
} | ||
}); | ||
} | ||
else { | ||
Object.keys(extensions).forEach(function (name) { | ||
extensions[name].init(_this); | ||
var ext = extensions[name]; | ||
_this[name] = ext; | ||
if (ext instanceof Extension.Internal) { | ||
if (!isEmpty(ext.config)) | ||
extConfig[extensions[name].name] = ext.config; | ||
} | ||
}); | ||
} | ||
var extConfig = prepareExtensions.call(this, options); | ||
// Build query params for the current `ViewController` | ||
@@ -95,0 +144,0 @@ this.encodedQueryParams = encodeJSON({ |
@@ -6,2 +6,8 @@ import { createJsonRpcRequestPayload, standardizeJsonRpcRequestPayload } from '../core/json-rpc'; | ||
import { createPromiEvent, encodeJSON, decodeJSON, encodeQueryParameters, decodeQueryParameters, storage, isPromiEvent } from '../util'; | ||
interface BaseExtension<TName extends string> extends BaseModule { | ||
compat?: { | ||
'magic-sdk': boolean | string; | ||
'@magic-sdk/react-native': boolean | string; | ||
}; | ||
} | ||
declare abstract class BaseExtension<TName extends string> extends BaseModule { | ||
@@ -74,3 +80,3 @@ abstract readonly name: TName; | ||
*/ | ||
declare type HiddenExtensionFields = 'name' | 'init' | 'config'; | ||
declare type HiddenExtensionFields = 'name' | 'init' | 'config' | 'compat'; | ||
/** | ||
@@ -77,0 +83,0 @@ * Gets the type contained in an array type. |
@@ -35,3 +35,3 @@ var __extends = (this && this.__extends) || (function () { | ||
var sdkAccessFields = ['request', 'transport', 'overlay', 'sdk']; | ||
// Dissallow SDK access before initialization. | ||
// Disallow SDK access before initialization. | ||
return new Proxy(_this, { | ||
@@ -38,0 +38,0 @@ get: function (target, prop, receiver) { |
{ | ||
"name": "@magic-sdk/provider", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "Core business logic for Magic SDK packages.", | ||
@@ -23,4 +23,5 @@ "author": "Fortmatic <team@fortmatic.com> (https://fortmatic.com/)", | ||
"dependencies": { | ||
"@magic-sdk/types": "^1.5.0", | ||
"eventemitter3": "^4.0.4" | ||
"@magic-sdk/types": "^1.6.0", | ||
"eventemitter3": "^4.0.4", | ||
"semver": "^7.3.2" | ||
}, | ||
@@ -32,2 +33,3 @@ "peerDependencies": { | ||
"@babel/plugin-transform-runtime": "^7.9.6", | ||
"@types/semver": "^7.3.4", | ||
"localforage": "^1.7.4", | ||
@@ -63,3 +65,3 @@ "localforage-driver-memory": "^1.0.5" | ||
}, | ||
"gitHead": "b4239a21694ce4eb4b6a3cad407b68720b459fd7" | ||
"gitHead": "0ae99071e9a8bf8aacc1504942b51db7774f24fc" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1001188
5185
4
4
+ Addedsemver@^7.3.2
+ Addedsemver@7.6.3(transitive)
Updated@magic-sdk/types@^1.6.0