@pnp/common
Advanced tools
Comparing version 2.0.0-1 to 2.0.0-2
@@ -1,5 +0,2 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./src/common"), exports); | ||
export * from "./src/common"; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@pnp/common", | ||
"version": "2.0.0-1", | ||
"version": "2.0.0-2", | ||
"description": "pnp - provides shared functionality across all pnp libraries", | ||
@@ -5,0 +5,0 @@ "main": "./index.js", |
@@ -1,8 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
const netutil_1 = require("./netutil"); | ||
const util_1 = require("./util"); | ||
import * as tslib_1 from "tslib"; | ||
import { BearerTokenFetchClient } from "./netutil"; | ||
import { isUrlAbsolute } from "./util"; | ||
// @ts-ignore | ||
const adal = require("adal-angular/dist/adal.min.js"); | ||
import * as adal from "adal-angular/dist/adal.min.js"; | ||
/** | ||
@@ -15,5 +13,5 @@ * Parses out the root of the request url to use as the resource when getting the token | ||
function getResource(url) { | ||
const parser = document.createElement("a"); | ||
var parser = document.createElement("a"); | ||
parser.href = url; | ||
return `${parser.protocol}//${parser.hostname}`; | ||
return parser.protocol + "//" + parser.hostname; | ||
} | ||
@@ -23,3 +21,4 @@ /** | ||
*/ | ||
class AdalClient extends netutil_1.BearerTokenFetchClient { | ||
var AdalClient = /** @class */ (function (_super) { | ||
tslib_1.__extends(AdalClient, _super); | ||
/** | ||
@@ -31,9 +30,10 @@ * Creates a new instance of AdalClient | ||
*/ | ||
constructor(clientId, tenant, redirectUri) { | ||
super(null); | ||
this.clientId = clientId; | ||
this.tenant = tenant; | ||
this.redirectUri = redirectUri; | ||
this._displayCallback = null; | ||
this._loginPromise = null; | ||
function AdalClient(clientId, tenant, redirectUri) { | ||
var _this = _super.call(this, null) || this; | ||
_this.clientId = clientId; | ||
_this.tenant = tenant; | ||
_this.redirectUri = redirectUri; | ||
_this._displayCallback = null; | ||
_this._loginPromise = null; | ||
return _this; | ||
} | ||
@@ -47,5 +47,5 @@ /** | ||
*/ | ||
static fromSPFxContext(spfxContext) { | ||
AdalClient.fromSPFxContext = function (spfxContext) { | ||
return new SPFxAdalClient(spfxContext); | ||
} | ||
}; | ||
/** | ||
@@ -57,16 +57,20 @@ * Conducts the fetch opertation against the AAD secured resource | ||
*/ | ||
fetch(url, options) { | ||
const _super = Object.create(null, { | ||
fetch: { get: () => super.fetch } | ||
AdalClient.prototype.fetch = function (url, options) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var token; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!isUrlAbsolute(url)) { | ||
throw Error("You must supply absolute urls to AdalClient.fetch."); | ||
} | ||
return [4 /*yield*/, this.getToken(getResource(url))]; | ||
case 1: | ||
token = _a.sent(); | ||
this.token = token; | ||
return [2 /*return*/, _super.prototype.fetch.call(this, url, options)]; | ||
} | ||
}); | ||
}); | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
if (!util_1.isUrlAbsolute(url)) { | ||
throw Error("You must supply absolute urls to AdalClient.fetch."); | ||
} | ||
// the url we are calling is the resource | ||
const token = yield this.getToken(getResource(url)); | ||
this.token = token; | ||
return _super.fetch.call(this, url, options); | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -77,32 +81,42 @@ * Gets a token based on the current user | ||
*/ | ||
getToken(resource) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function* () { | ||
yield this.ensureAuthContext(); | ||
yield this.login(); | ||
let token = null; | ||
AdalClient._authContext.acquireToken(resource, (message, tok) => { | ||
if (message) { | ||
throw Error(message); | ||
AdalClient.prototype.getToken = function (resource) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var token; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.ensureAuthContext()]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, this.login()]; | ||
case 2: | ||
_a.sent(); | ||
token = null; | ||
AdalClient._authContext.acquireToken(resource, function (message, tok) { | ||
if (message) { | ||
throw Error(message); | ||
} | ||
token = tok; | ||
}); | ||
return [2 /*return*/, token]; | ||
} | ||
token = tok; | ||
}); | ||
return token; | ||
}); | ||
} | ||
}; | ||
/** | ||
* Ensures we have created and setup the adal AuthenticationContext instance | ||
*/ | ||
ensureAuthContext() { | ||
return new Promise(resolve => { | ||
AdalClient.prototype.ensureAuthContext = function () { | ||
var _this = this; | ||
return new Promise(function (resolve) { | ||
if (AdalClient._authContext === null) { | ||
AdalClient._authContext = adal.inject({ | ||
clientId: this.clientId, | ||
displayCall: (url) => { | ||
if (this._displayCallback) { | ||
this._displayCallback(url); | ||
clientId: _this.clientId, | ||
displayCall: function (url) { | ||
if (_this._displayCallback) { | ||
_this._displayCallback(url); | ||
} | ||
}, | ||
navigateToLoginRequestUrl: false, | ||
redirectUri: this.redirectUri, | ||
tenant: this.tenant, | ||
redirectUri: _this.redirectUri, | ||
tenant: _this.tenant, | ||
}); | ||
@@ -112,16 +126,17 @@ } | ||
}); | ||
} | ||
}; | ||
/** | ||
* Ensures the current user is logged in | ||
*/ | ||
login() { | ||
AdalClient.prototype.login = function () { | ||
var _this = this; | ||
if (this._loginPromise) { | ||
return this._loginPromise; | ||
} | ||
this._loginPromise = new Promise((resolve, reject) => { | ||
this._loginPromise = new Promise(function (resolve, reject) { | ||
if (AdalClient._authContext.getCachedUser()) { | ||
return resolve(); | ||
} | ||
this._displayCallback = (url) => { | ||
const popupWindow = window.open(url, "login", "width=483, height=600"); | ||
_this._displayCallback = function (url) { | ||
var popupWindow = window.open(url, "login", "width=483, height=600"); | ||
if (!popupWindow) { | ||
@@ -133,3 +148,3 @@ return reject(Error("Could not open pop-up window for auth. Likely pop-ups are blocked by the browser.")); | ||
} | ||
const pollTimer = window.setInterval(() => { | ||
var pollTimer = window.setInterval(function () { | ||
if (!popupWindow || popupWindow.closed || popupWindow.closed === undefined) { | ||
@@ -139,3 +154,3 @@ window.clearInterval(pollTimer); | ||
try { | ||
if (popupWindow.document.URL.indexOf(this.redirectUri) !== -1) { | ||
if (popupWindow.document.URL.indexOf(_this.redirectUri) !== -1) { | ||
window.clearInterval(pollTimer); | ||
@@ -153,20 +168,22 @@ AdalClient._authContext.handleWindowCallback(popupWindow.location.hash); | ||
// this triggers the login process | ||
this.ensureAuthContext().then(_ => { | ||
_this.ensureAuthContext().then(function (_) { | ||
AdalClient._authContext._loginInProgress = false; | ||
AdalClient._authContext.login(); | ||
this._displayCallback = null; | ||
_this._displayCallback = null; | ||
}); | ||
}); | ||
return this._loginPromise; | ||
} | ||
} | ||
}; | ||
/** | ||
* Our auth context | ||
*/ | ||
AdalClient._authContext = null; | ||
return AdalClient; | ||
}(BearerTokenFetchClient)); | ||
export { AdalClient }; | ||
/** | ||
* Our auth context | ||
*/ | ||
AdalClient._authContext = null; | ||
exports.AdalClient = AdalClient; | ||
/** | ||
* Client wrapping the aadTokenProvider available from SPFx >= 1.6 | ||
*/ | ||
class SPFxAdalClient extends netutil_1.BearerTokenFetchClient { | ||
var SPFxAdalClient = /** @class */ (function (_super) { | ||
tslib_1.__extends(SPFxAdalClient, _super); | ||
/** | ||
@@ -176,5 +193,6 @@ * | ||
*/ | ||
constructor(context) { | ||
super(null); | ||
this.context = context; | ||
function SPFxAdalClient(context) { | ||
var _this = _super.call(this, null) || this; | ||
_this.context = context; | ||
return _this; | ||
} | ||
@@ -187,8 +205,9 @@ /** | ||
*/ | ||
fetch(url, options) { | ||
return this.getToken(getResource(url)).then(token => { | ||
this.token = token; | ||
return super.fetch(url, options); | ||
SPFxAdalClient.prototype.fetch = function (url, options) { | ||
var _this = this; | ||
return this.getToken(getResource(url)).then(function (token) { | ||
_this.token = token; | ||
return _super.prototype.fetch.call(_this, url, options); | ||
}); | ||
} | ||
}; | ||
/** | ||
@@ -199,9 +218,10 @@ * Gets an AAD token for the provided resource using the SPFx AADTokenProvider | ||
*/ | ||
getToken(resource) { | ||
return this.context.aadTokenProviderFactory.getTokenProvider().then(provider => { | ||
SPFxAdalClient.prototype.getToken = function (resource) { | ||
return this.context.aadTokenProviderFactory.getTokenProvider().then(function (provider) { | ||
return provider.getToken(resource); | ||
}); | ||
} | ||
} | ||
exports.SPFxAdalClient = SPFxAdalClient; | ||
}; | ||
return SPFxAdalClient; | ||
}(BearerTokenFetchClient)); | ||
export { SPFxAdalClient }; | ||
//# sourceMappingURL=adalclient.js.map |
@@ -1,8 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("./util"); | ||
import { isFunc } from "./util"; | ||
/** | ||
* Used to calculate the object properties, with polyfill if needed | ||
*/ | ||
const objectEntries = util_1.isFunc(Object.entries) ? Object.entries : (o) => Object.keys(o).map((k) => [k, o[k]]); | ||
var objectEntries = isFunc(Object.entries) ? Object.entries : function (o) { return Object.keys(o).map(function (k) { return [k, o[k]]; }); }; | ||
/** | ||
@@ -13,3 +11,3 @@ * Converts the supplied object to a map | ||
*/ | ||
function objectToMap(o) { | ||
export function objectToMap(o) { | ||
if (o !== undefined && o !== null) { | ||
@@ -20,3 +18,2 @@ return new Map(objectEntries(o)); | ||
} | ||
exports.objectToMap = objectToMap; | ||
/** | ||
@@ -28,5 +25,9 @@ * Merges to Map instances together, overwriting values in target with matching keys, last in wins | ||
*/ | ||
function mergeMaps(target, ...maps) { | ||
for (let i = 0; i < maps.length; i++) { | ||
maps[i].forEach((v, k) => { | ||
export function mergeMaps(target) { | ||
var maps = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
maps[_i - 1] = arguments[_i]; | ||
} | ||
for (var i = 0; i < maps.length; i++) { | ||
maps[i].forEach(function (v, k) { | ||
target.set(k, v); | ||
@@ -37,3 +38,2 @@ }); | ||
} | ||
exports.mergeMaps = mergeMaps; | ||
//# sourceMappingURL=collections.js.map |
@@ -1,10 +0,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const tslib_1 = require("tslib"); | ||
tslib_1.__exportStar(require("./adalclient"), exports); | ||
tslib_1.__exportStar(require("./collections"), exports); | ||
tslib_1.__exportStar(require("./libconfig"), exports); | ||
tslib_1.__exportStar(require("./netutil"), exports); | ||
tslib_1.__exportStar(require("./storage"), exports); | ||
tslib_1.__exportStar(require("./util"), exports); | ||
export * from "./adalclient"; | ||
export * from "./collections"; | ||
export * from "./libconfig"; | ||
export * from "./netutil"; | ||
export * from "./storage"; | ||
export * from "./util"; | ||
//# sourceMappingURL=common.js.map |
@@ -1,10 +0,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const collections_1 = require("./collections"); | ||
function setup(config) { | ||
exports.RuntimeConfig.extend(config); | ||
import { mergeMaps, objectToMap } from "./collections"; | ||
export function setup(config) { | ||
RuntimeConfig.extend(config); | ||
} | ||
exports.setup = setup; | ||
// lable mapping for known config values | ||
const s = [ | ||
var s = [ | ||
"defaultCachingStore", | ||
@@ -18,4 +15,5 @@ "defaultCachingTimeoutSeconds", | ||
]; | ||
class RuntimeConfigImpl { | ||
constructor(_v = new Map()) { | ||
var RuntimeConfigImpl = /** @class */ (function () { | ||
function RuntimeConfigImpl(_v) { | ||
if (_v === void 0) { _v = new Map(); } | ||
this._v = _v; | ||
@@ -35,37 +33,66 @@ // setup defaults | ||
*/ | ||
extend(config) { | ||
this._v = collections_1.mergeMaps(this._v, collections_1.objectToMap(config)); | ||
} | ||
get(key) { | ||
RuntimeConfigImpl.prototype.extend = function (config) { | ||
this._v = mergeMaps(this._v, objectToMap(config)); | ||
}; | ||
RuntimeConfigImpl.prototype.get = function (key) { | ||
return this._v.get(key); | ||
} | ||
get defaultCachingStore() { | ||
return this.get(s[0]); | ||
} | ||
get defaultCachingTimeoutSeconds() { | ||
return this.get(s[1]); | ||
} | ||
get globalCacheDisable() { | ||
return this.get(s[2]); | ||
} | ||
get enableCacheExpiration() { | ||
return this.get(s[3]); | ||
} | ||
get cacheExpirationIntervalMilliseconds() { | ||
return this.get(s[4]); | ||
} | ||
get spfxContext() { | ||
return this.get(s[5]); | ||
} | ||
get ie11() { | ||
const v = this.get(s[6]); | ||
if (v) { | ||
console.warn("PnPjs is running in ie11 compat mode. Not all features may work as expected."); | ||
} | ||
return v; | ||
} | ||
} | ||
exports.RuntimeConfigImpl = RuntimeConfigImpl; | ||
const _runtimeConfig = new RuntimeConfigImpl(); | ||
exports.RuntimeConfig = _runtimeConfig; | ||
}; | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "defaultCachingStore", { | ||
get: function () { | ||
return this.get(s[0]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "defaultCachingTimeoutSeconds", { | ||
get: function () { | ||
return this.get(s[1]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "globalCacheDisable", { | ||
get: function () { | ||
return this.get(s[2]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "enableCacheExpiration", { | ||
get: function () { | ||
return this.get(s[3]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "cacheExpirationIntervalMilliseconds", { | ||
get: function () { | ||
return this.get(s[4]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "spfxContext", { | ||
get: function () { | ||
return this.get(s[5]); | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(RuntimeConfigImpl.prototype, "ie11", { | ||
get: function () { | ||
var v = this.get(s[6]); | ||
if (v) { | ||
console.warn("PnPjs is running in ie11 compat mode. Not all features may work as expected."); | ||
} | ||
return v; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
return RuntimeConfigImpl; | ||
}()); | ||
export { RuntimeConfigImpl }; | ||
var _runtimeConfig = new RuntimeConfigImpl(); | ||
export var RuntimeConfig = _runtimeConfig; | ||
//# sourceMappingURL=libconfig.js.map |
@@ -1,8 +0,7 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("./util"); | ||
function mergeHeaders(target, source) { | ||
import * as tslib_1 from "tslib"; | ||
import { extend, objectDefinedNotNull } from "./util"; | ||
export function mergeHeaders(target, source) { | ||
if (source !== undefined && source !== null) { | ||
const temp = new Request("", { headers: source }); | ||
temp.headers.forEach((value, name) => { | ||
var temp = new Request("", { headers: source }); | ||
temp.headers.forEach(function (value, name) { | ||
target.append(name, value); | ||
@@ -12,43 +11,52 @@ }); | ||
} | ||
exports.mergeHeaders = mergeHeaders; | ||
function mergeOptions(target, source) { | ||
if (util_1.objectDefinedNotNull(source)) { | ||
const headers = util_1.extend(target.headers || {}, source.headers); | ||
target = util_1.extend(target, source); | ||
export function mergeOptions(target, source) { | ||
if (objectDefinedNotNull(source)) { | ||
var headers = extend(target.headers || {}, source.headers); | ||
target = extend(target, source); | ||
target.headers = headers; | ||
} | ||
} | ||
exports.mergeOptions = mergeOptions; | ||
/** | ||
* Makes requests using the global/window fetch API | ||
*/ | ||
class FetchClient { | ||
fetch(url, options) { | ||
var FetchClient = /** @class */ (function () { | ||
function FetchClient() { | ||
} | ||
FetchClient.prototype.fetch = function (url, options) { | ||
return global.fetch(url, options); | ||
} | ||
} | ||
exports.FetchClient = FetchClient; | ||
}; | ||
return FetchClient; | ||
}()); | ||
export { FetchClient }; | ||
/** | ||
* Makes requests using the fetch API adding the supplied token to the Authorization header | ||
*/ | ||
class BearerTokenFetchClient extends FetchClient { | ||
constructor(_token) { | ||
super(); | ||
this._token = _token; | ||
var BearerTokenFetchClient = /** @class */ (function (_super) { | ||
tslib_1.__extends(BearerTokenFetchClient, _super); | ||
function BearerTokenFetchClient(_token) { | ||
var _this = _super.call(this) || this; | ||
_this._token = _token; | ||
return _this; | ||
} | ||
get token() { | ||
return this._token || ""; | ||
} | ||
set token(token) { | ||
this._token = token; | ||
} | ||
fetch(url, options = {}) { | ||
const headers = new Headers(); | ||
Object.defineProperty(BearerTokenFetchClient.prototype, "token", { | ||
get: function () { | ||
return this._token || ""; | ||
}, | ||
set: function (token) { | ||
this._token = token; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
BearerTokenFetchClient.prototype.fetch = function (url, options) { | ||
if (options === void 0) { options = {}; } | ||
var headers = new Headers(); | ||
mergeHeaders(headers, options.headers); | ||
headers.set("Authorization", `Bearer ${this._token}`); | ||
headers.set("Authorization", "Bearer " + this._token); | ||
options.headers = headers; | ||
return super.fetch(url, options); | ||
} | ||
} | ||
exports.BearerTokenFetchClient = BearerTokenFetchClient; | ||
return _super.prototype.fetch.call(this, url, options); | ||
}; | ||
return BearerTokenFetchClient; | ||
}(FetchClient)); | ||
export { BearerTokenFetchClient }; | ||
//# sourceMappingURL=netutil.js.map |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=spfxcontextinterface.js.map |
@@ -1,5 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const util_1 = require("./util"); | ||
const libconfig_1 = require("./libconfig"); | ||
import { dateAdd, getCtxCallback, jsS, objectDefinedNotNull } from "./util"; | ||
import { RuntimeConfig } from "./libconfig"; | ||
/** | ||
@@ -9,3 +7,3 @@ * A wrapper class to provide a consistent interface to browser based storage | ||
*/ | ||
class PnPClientStorageWrapper { | ||
var PnPClientStorageWrapper = /** @class */ (function () { | ||
/** | ||
@@ -16,3 +14,4 @@ * Creates a new instance of the PnPClientStorageWrapper class | ||
*/ | ||
constructor(store, defaultTimeoutMinutes = -1) { | ||
function PnPClientStorageWrapper(store, defaultTimeoutMinutes) { | ||
if (defaultTimeoutMinutes === void 0) { defaultTimeoutMinutes = -1; } | ||
this.store = store; | ||
@@ -23,3 +22,3 @@ this.defaultTimeoutMinutes = defaultTimeoutMinutes; | ||
// this will clear any expired items and set the timeout function | ||
if (libconfig_1.RuntimeConfig.enableCacheExpiration) { | ||
if (RuntimeConfig.enableCacheExpiration) { | ||
this.cacheExpirationHandler(); | ||
@@ -33,11 +32,11 @@ } | ||
*/ | ||
get(key) { | ||
PnPClientStorageWrapper.prototype.get = function (key) { | ||
if (!this.enabled) { | ||
return null; | ||
} | ||
const o = this.store.getItem(key); | ||
if (!util_1.objectDefinedNotNull(o)) { | ||
var o = this.store.getItem(key); | ||
if (!objectDefinedNotNull(o)) { | ||
return null; | ||
} | ||
const persistable = JSON.parse(o); | ||
var persistable = JSON.parse(o); | ||
if (new Date(persistable.expiration) <= new Date()) { | ||
@@ -50,3 +49,3 @@ this.delete(key); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -59,7 +58,7 @@ * Adds a value to the underlying storage | ||
*/ | ||
put(key, o, expire) { | ||
PnPClientStorageWrapper.prototype.put = function (key, o, expire) { | ||
if (this.enabled) { | ||
this.store.setItem(key, this.createPersistable(o, expire)); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -70,7 +69,7 @@ * Deletes a value from the underlying storage | ||
*/ | ||
delete(key) { | ||
PnPClientStorageWrapper.prototype.delete = function (key) { | ||
if (this.enabled) { | ||
this.store.removeItem(key); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -83,11 +82,12 @@ * Gets an item from the underlying storage, or adds it if it does not exist using the supplied getter function | ||
*/ | ||
getOrPut(key, getter, expire) { | ||
PnPClientStorageWrapper.prototype.getOrPut = function (key, getter, expire) { | ||
var _this = this; | ||
if (!this.enabled) { | ||
return getter(); | ||
} | ||
return new Promise((resolve) => { | ||
const o = this.get(key); | ||
return new Promise(function (resolve) { | ||
var o = _this.get(key); | ||
if (o == null) { | ||
getter().then((d) => { | ||
this.put(key, d, expire); | ||
getter().then(function (d) { | ||
_this.put(key, d, expire); | ||
resolve(d); | ||
@@ -100,19 +100,20 @@ }); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Deletes any expired items placed in the store by the pnp library, leaves other items untouched | ||
*/ | ||
deleteExpired() { | ||
return new Promise((resolve, reject) => { | ||
if (!this.enabled) { | ||
PnPClientStorageWrapper.prototype.deleteExpired = function () { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
if (!_this.enabled) { | ||
resolve(); | ||
} | ||
try { | ||
for (let i = 0; i < this.store.length; i++) { | ||
const key = this.store.key(i); | ||
for (var i = 0; i < _this.store.length; i++) { | ||
var key = _this.store.key(i); | ||
if (key !== null) { | ||
// test the stored item to see if we stored it | ||
if (/["|']?pnp["|']? ?: ?1/i.test(this.store.getItem(key))) { | ||
if (/["|']?pnp["|']? ?: ?1/i.test(_this.store.getItem(key))) { | ||
// get those items as get will delete from cache if they are expired | ||
this.get(key); | ||
_this.get(key); | ||
} | ||
@@ -127,8 +128,8 @@ } | ||
}); | ||
} | ||
}; | ||
/** | ||
* Used to determine if the wrapped storage is available currently | ||
*/ | ||
test() { | ||
const str = "t"; | ||
PnPClientStorageWrapper.prototype.test = function () { | ||
var str = "t"; | ||
try { | ||
@@ -142,60 +143,68 @@ this.store.setItem(str, str); | ||
} | ||
} | ||
}; | ||
/** | ||
* Creates the persistable to store | ||
*/ | ||
createPersistable(o, expire) { | ||
PnPClientStorageWrapper.prototype.createPersistable = function (o, expire) { | ||
if (expire === undefined) { | ||
// ensure we are by default inline with the global library setting | ||
let defaultTimeout = libconfig_1.RuntimeConfig.defaultCachingTimeoutSeconds; | ||
var defaultTimeout = RuntimeConfig.defaultCachingTimeoutSeconds; | ||
if (this.defaultTimeoutMinutes > 0) { | ||
defaultTimeout = this.defaultTimeoutMinutes * 60; | ||
} | ||
expire = util_1.dateAdd(new Date(), "second", defaultTimeout); | ||
expire = dateAdd(new Date(), "second", defaultTimeout); | ||
} | ||
return util_1.jsS({ pnp: 1, expiration: expire, value: o }); | ||
} | ||
return jsS({ pnp: 1, expiration: expire, value: o }); | ||
}; | ||
/** | ||
* Deletes expired items added by this library in this.store and sets a timeout to call itself | ||
*/ | ||
cacheExpirationHandler() { | ||
this.deleteExpired().then(_ => { | ||
PnPClientStorageWrapper.prototype.cacheExpirationHandler = function () { | ||
var _this = this; | ||
this.deleteExpired().then(function (_) { | ||
// call ourself in the future | ||
setTimeout(util_1.getCtxCallback(this, this.cacheExpirationHandler), libconfig_1.RuntimeConfig.cacheExpirationIntervalMilliseconds); | ||
}).catch(e => { | ||
setTimeout(getCtxCallback(_this, _this.cacheExpirationHandler), RuntimeConfig.cacheExpirationIntervalMilliseconds); | ||
}).catch(function (e) { | ||
console.error(e); | ||
}); | ||
} | ||
} | ||
exports.PnPClientStorageWrapper = PnPClientStorageWrapper; | ||
}; | ||
return PnPClientStorageWrapper; | ||
}()); | ||
export { PnPClientStorageWrapper }; | ||
/** | ||
* A thin implementation of in-memory storage for use in nodejs | ||
*/ | ||
class MemoryStorage { | ||
constructor(_store = new Map()) { | ||
var MemoryStorage = /** @class */ (function () { | ||
function MemoryStorage(_store) { | ||
if (_store === void 0) { _store = new Map(); } | ||
this._store = _store; | ||
} | ||
get length() { | ||
return this._store.size; | ||
} | ||
clear() { | ||
Object.defineProperty(MemoryStorage.prototype, "length", { | ||
get: function () { | ||
return this._store.size; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
MemoryStorage.prototype.clear = function () { | ||
this._store.clear(); | ||
} | ||
getItem(key) { | ||
}; | ||
MemoryStorage.prototype.getItem = function (key) { | ||
return this._store.get(key); | ||
} | ||
key(index) { | ||
}; | ||
MemoryStorage.prototype.key = function (index) { | ||
return Array.from(this._store)[index][0]; | ||
} | ||
removeItem(key) { | ||
}; | ||
MemoryStorage.prototype.removeItem = function (key) { | ||
this._store.delete(key); | ||
} | ||
setItem(key, data) { | ||
}; | ||
MemoryStorage.prototype.setItem = function (key, data) { | ||
this._store.set(key, data); | ||
} | ||
} | ||
}; | ||
return MemoryStorage; | ||
}()); | ||
/** | ||
* A class that will establish wrappers for both local and session storage | ||
*/ | ||
class PnPClientStorage { | ||
var PnPClientStorage = /** @class */ (function () { | ||
/** | ||
@@ -206,25 +215,35 @@ * Creates a new instance of the PnPClientStorage class | ||
*/ | ||
constructor(_local = null, _session = null) { | ||
function PnPClientStorage(_local, _session) { | ||
if (_local === void 0) { _local = null; } | ||
if (_session === void 0) { _session = null; } | ||
this._local = _local; | ||
this._session = _session; | ||
} | ||
/** | ||
* Provides access to the local storage of the browser | ||
*/ | ||
get local() { | ||
if (this._local === null) { | ||
this._local = this.getStore("local"); | ||
} | ||
return this._local; | ||
} | ||
/** | ||
* Provides access to the session storage of the browser | ||
*/ | ||
get session() { | ||
if (this._session === null) { | ||
this._session = this.getStore("session"); | ||
} | ||
return this._session; | ||
} | ||
getStore(name) { | ||
Object.defineProperty(PnPClientStorage.prototype, "local", { | ||
/** | ||
* Provides access to the local storage of the browser | ||
*/ | ||
get: function () { | ||
if (this._local === null) { | ||
this._local = this.getStore("local"); | ||
} | ||
return this._local; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Object.defineProperty(PnPClientStorage.prototype, "session", { | ||
/** | ||
* Provides access to the session storage of the browser | ||
*/ | ||
get: function () { | ||
if (this._session === null) { | ||
this._session = this.getStore("session"); | ||
} | ||
return this._session; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
PnPClientStorage.prototype.getStore = function (name) { | ||
if (name === "local") { | ||
@@ -234,5 +253,6 @@ return new PnPClientStorageWrapper(typeof (localStorage) === "undefined" ? new MemoryStorage() : localStorage); | ||
return new PnPClientStorageWrapper(typeof (sessionStorage) === "undefined" ? new MemoryStorage() : sessionStorage); | ||
} | ||
} | ||
exports.PnPClientStorage = PnPClientStorage; | ||
}; | ||
return PnPClientStorage; | ||
}()); | ||
export { PnPClientStorage }; | ||
//# sourceMappingURL=storage.js.map |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
@@ -11,3 +9,7 @@ * Gets a callback function which will maintain context across async calls. | ||
*/ | ||
function getCtxCallback(context, method, ...params) { | ||
export function getCtxCallback(context, method) { | ||
var params = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
params[_i - 2] = arguments[_i]; | ||
} | ||
return function () { | ||
@@ -17,3 +19,2 @@ method.apply(context, params); | ||
} | ||
exports.getCtxCallback = getCtxCallback; | ||
/** | ||
@@ -28,4 +29,4 @@ * Adds a value to a date | ||
*/ | ||
function dateAdd(date, interval, units) { | ||
let ret = new Date(date); // don't change original date | ||
export function dateAdd(date, interval, units) { | ||
var ret = new Date(date.toString()); // don't change original date | ||
switch (interval.toLowerCase()) { | ||
@@ -62,3 +63,2 @@ case "year": | ||
} | ||
exports.dateAdd = dateAdd; | ||
/** | ||
@@ -69,10 +69,13 @@ * Combines an arbitrary set of paths ensuring and normalizes the slashes | ||
*/ | ||
function combine(...paths) { | ||
export function combine() { | ||
var paths = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
paths[_i] = arguments[_i]; | ||
} | ||
return paths | ||
.filter(path => !stringIsNullOrEmpty(path)) | ||
.map(path => path.replace(/^[\\|\/]/, "").replace(/[\\|\/]$/, "")) | ||
.filter(function (path) { return !stringIsNullOrEmpty(path); }) | ||
.map(function (path) { return path.replace(/^[\\|\/]/, "").replace(/[\\|\/]$/, ""); }) | ||
.join("/") | ||
.replace(/\\/g, "/"); | ||
} | ||
exports.combine = combine; | ||
/** | ||
@@ -85,6 +88,6 @@ * Gets a random string of chars length | ||
*/ | ||
function getRandomString(chars) { | ||
const text = new Array(chars); | ||
const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
for (let i = 0; i < chars; i++) { | ||
export function getRandomString(chars) { | ||
var text = new Array(chars); | ||
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
for (var i = 0; i < chars; i++) { | ||
text[i] = possible.charAt(Math.floor(Math.random() * possible.length)); | ||
@@ -94,3 +97,2 @@ } | ||
} | ||
exports.getRandomString = getRandomString; | ||
/** | ||
@@ -103,9 +105,9 @@ * Gets a random GUID value | ||
/* tslint:disable no-bitwise */ | ||
function getGUID() { | ||
let d = Date.now(); | ||
export function getGUID() { | ||
var d = Date.now(); | ||
if (typeof performance !== "undefined" && typeof performance.now === "function") { | ||
d += performance.now(); // use high-precision timer if available | ||
} | ||
const guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { | ||
const r = (d + Math.random() * 16) % 16 | 0; | ||
var guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) { | ||
var r = (d + Math.random() * 16) % 16 | 0; | ||
d = Math.floor(d / 16); | ||
@@ -116,3 +118,2 @@ return (c === "x" ? r : (r & 0x3 | 0x8)).toString(16); | ||
} | ||
exports.getGUID = getGUID; | ||
/* tslint:enable */ | ||
@@ -124,6 +125,5 @@ /** | ||
*/ | ||
function isFunc(cf) { | ||
export function isFunc(cf) { | ||
return typeof cf === "function"; | ||
} | ||
exports.isFunc = isFunc; | ||
/** | ||
@@ -133,10 +133,9 @@ * Determines if an object is both defined and not null | ||
*/ | ||
function objectDefinedNotNull(obj) { | ||
export function objectDefinedNotNull(obj) { | ||
return typeof obj !== "undefined" && obj !== null; | ||
} | ||
exports.objectDefinedNotNull = objectDefinedNotNull; | ||
/** | ||
* @returns whether the provided parameter is a JavaScript Array or not. | ||
*/ | ||
function isArray(array) { | ||
export function isArray(array) { | ||
if (Array.isArray) { | ||
@@ -147,3 +146,2 @@ return Array.isArray(array); | ||
} | ||
exports.isArray = isArray; | ||
/** | ||
@@ -158,3 +156,5 @@ * Provides functionality to extend the given object by doing a shallow copy | ||
*/ | ||
function extend(target, source, noOverwrite = false, filter = () => true) { | ||
export function extend(target, source, noOverwrite, filter) { | ||
if (noOverwrite === void 0) { noOverwrite = false; } | ||
if (filter === void 0) { filter = function () { return true; }; } | ||
if (!objectDefinedNotNull(source)) { | ||
@@ -164,8 +164,8 @@ return target; | ||
// ensure we don't overwrite things we don't want overwritten | ||
const check = noOverwrite ? (o, i) => !(i in o) : () => true; | ||
var check = noOverwrite ? function (o, i) { return !(i in o); } : function () { return true; }; | ||
// final filter we will use | ||
const f = (v) => check(target, v) && filter(v); | ||
var f = function (v) { return check(target, v) && filter(v); }; | ||
return Object.getOwnPropertyNames(source) | ||
.filter(f) | ||
.reduce((t, v) => { | ||
.reduce(function (t, v) { | ||
t[v] = source[v]; | ||
@@ -175,3 +175,2 @@ return t; | ||
} | ||
exports.extend = extend; | ||
/** | ||
@@ -182,6 +181,5 @@ * Determines if a given url is absolute | ||
*/ | ||
function isUrlAbsolute(url) { | ||
export function isUrlAbsolute(url) { | ||
return /^https?:\/\/|^\/\//i.test(url); | ||
} | ||
exports.isUrlAbsolute = isUrlAbsolute; | ||
/** | ||
@@ -192,6 +190,5 @@ * Determines if a string is null or empty or undefined | ||
*/ | ||
function stringIsNullOrEmpty(s) { | ||
export function stringIsNullOrEmpty(s) { | ||
return s === undefined || s === null || s.length < 1; | ||
} | ||
exports.stringIsNullOrEmpty = stringIsNullOrEmpty; | ||
/** | ||
@@ -204,10 +201,9 @@ * Gets an attribute value from an html/xml string block. NOTE: if the input attribute value has | ||
*/ | ||
function getAttrValueFromString(html, attrName) { | ||
export function getAttrValueFromString(html, attrName) { | ||
// make the input safe for regex | ||
html = html.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); | ||
const reg = new RegExp(`${attrName}\\s*?=\\s*?("|')([^\\1]*?)\\1`, "i"); | ||
const match = reg.exec(html); | ||
var reg = new RegExp(attrName + "\\s*?=\\s*?(\"|')([^\\1]*?)\\1", "i"); | ||
var match = reg.exec(html); | ||
return match !== null && match.length > 0 ? match[2] : null; | ||
} | ||
exports.getAttrValueFromString = getAttrValueFromString; | ||
/** | ||
@@ -218,10 +214,9 @@ * Ensures guid values are represented consistently as "ea123463-137d-4ae3-89b8-cf3fc578ca05" | ||
*/ | ||
function sanitizeGuid(guid) { | ||
export function sanitizeGuid(guid) { | ||
if (stringIsNullOrEmpty(guid)) { | ||
return guid; | ||
} | ||
const matches = /([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})/i.exec(guid); | ||
var matches = /([0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12})/i.exec(guid); | ||
return matches === null ? guid : matches[1]; | ||
} | ||
exports.sanitizeGuid = sanitizeGuid; | ||
/** | ||
@@ -232,6 +227,5 @@ * Shorthand for JSON.stringify | ||
*/ | ||
function jsS(o) { | ||
export function jsS(o) { | ||
return JSON.stringify(o); | ||
} | ||
exports.jsS = jsS; | ||
/** | ||
@@ -243,6 +237,5 @@ * Shorthand for Object.hasOwnProperty | ||
*/ | ||
function hOP(o, p) { | ||
export function hOP(o, p) { | ||
return Object.hasOwnProperty.call(o, p); | ||
} | ||
exports.hOP = hOP; | ||
/** | ||
@@ -254,9 +247,9 @@ * Generates a ~unique hash code | ||
// tslint:disable:no-bitwise | ||
function getHashCode(s) { | ||
let hash = 0; | ||
export function getHashCode(s) { | ||
var hash = 0; | ||
if (s.length === 0) { | ||
return hash; | ||
} | ||
for (let i = 0; i < s.length; i++) { | ||
const chr = s.charCodeAt(i); | ||
for (var i = 0; i < s.length; i++) { | ||
var chr = s.charCodeAt(i); | ||
hash = ((hash << 5) - hash) + chr; | ||
@@ -267,4 +260,3 @@ hash |= 0; // Convert to 32bit integer | ||
} | ||
exports.getHashCode = getHashCode; | ||
// tslint:enable:no-bitwise | ||
//# sourceMappingURL=util.js.map |
52147
1353