async-preloader
Advanced tools
Comparing version 3.4.0 to 4.0.0
@@ -1,1 +0,1 @@ | ||
"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var LoaderKey,tslib_1=require("tslib"),FontFaceObserver=_interopDefault(require("fontfaceobserver-es")),get=_interopDefault(require("lodash-es/get"));!function(e){e.Json="Json",e.ArrayBuffer="ArrayBuffer",e.Blob="Blob",e.FormData="FormData",e.Text="Text",e.Image="Image",e.Video="Video",e.Audio="Audio",e.Xml="Xml",e.Font="Font"}(LoaderKey||(LoaderKey={}));var AsyncPreloader=function(){function s(){var e=this;this.items=new Map,this.defaultBodyMethod="blob",this.defaultLoader=LoaderKey.Text,this.loadItems=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,Promise.all(t.map(this.loadItem))];case 1:return[2,e.sent()]}})})},this.loadItem=function(i){return tslib_1.__awaiter(e,void 0,void 0,function(){var t,r,n,o;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=s.getFileExtension(i.src),"Xml"!==(r=i.loader||s.getLoaderKey(t))||i.mimeType||(n=s.loaders.get(LoaderKey.Xml).mimeType[t]||s.loaders.get(LoaderKey.Xml).mimeType.svg,i=tslib_1.__assign({},i,{mimeType:n})),[4,this["load"+r](i)];case 1:return o=e.sent(),this.items.set(i.id||i.src,o),[2,o]}})})},this.loadManifest=function(n,o){return void 0===o&&(o="items"),tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,this.loadJson({src:n})];case 1:return t=e.sent(),r=get(t,o),[4,this.loadItems(r)];case 2:return[2,e.sent()]}})})},this.loadText=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().text()];case 2:return[2,e.sent()]}})})},this.loadJson=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().json()];case 2:return[2,e.sent()]}})})},this.loadArrayBuffer=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().arrayBuffer()];case 2:return[2,e.sent()]}})})},this.loadBlob=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().blob()];case 2:return[2,e.sent()]}})})},this.loadFormData=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().formData()];case 2:return[2,e.sent()]}})})},this.loadImage=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){var r,n;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return r=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,r]:(n=new Image,[4,new Promise(function(e,t){n.addEventListener("load",function(){return e(n)},!1),n.addEventListener("error",t,!1),n.src=URL.createObjectURL(r)})]);case 3:return[2,e.sent()]}})})},this.loadVideo=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){var r,n;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return r=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,r]:(n=document.createElement("video"),[4,new Promise(function(e,t){n.addEventListener("canplaythrough",function(){return e(n)},!1),n.addEventListener("error",t,!1),n.src=URL.createObjectURL(r)})]);case 3:return[2,e.sent()]}})})},this.loadAudio=function(n){return tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(n)];case 1:return[4,e.sent()[n.body||this.defaultBodyMethod]()];case 2:return t=e.sent(),n.body&&n.body!==this.defaultBodyMethod?[2,t]:((r=document.createElement("audio")).autoplay=!1,r.preload="auto",r.src=URL.createObjectURL(t),[4,new Promise(function(e,t){r.addEventListener("canplaythrough",function(){return e(r)},!1),r.addEventListener("error",t,!1)})]);case 3:return[2,e.sent()]}})})},this.loadXml=function(r){return tslib_1.__awaiter(e,void 0,void 0,function(){var t;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(r)];case 1:return[4,e.sent().text()];case 2:return t=e.sent(),[2,s.domParser.parseFromString(t,r.mimeType)]}})})},this.loadFont=function(r){return tslib_1.__awaiter(e,void 0,void 0,function(){var t;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=r.id||s.getFileName(r.src),[4,new FontFaceObserver(t,r.options||{}).load()];case 1:return e.sent(),[2,t]}})})}}return s.fetchItem=function(e){return fetch(e.src,e.options||{})},s.getFileExtension=function(e){return(e.match(/[^\\\/]\.([^.\\\/]+)$/)||[null]).pop()},s.getFileName=function(e){return e.replace(/^.*[\\\/]/,"").split(".").shift()},s.getLoaderKey=function(t){var e=Array.from(s.loaders).find(function(e){return e[1].extensions.includes(t)});return e?e[0]:LoaderKey.Text},s.loaders=(new Map).set(LoaderKey.Text,{extensions:["txt"]}).set(LoaderKey.Json,{extensions:["json"]}).set(LoaderKey.Image,{extensions:["jpeg","jpg","gif","png","webp"]}).set(LoaderKey.Video,{extensions:["webm","ogg","mp4"]}).set(LoaderKey.Audio,{extensions:["webm","ogg","mp3","wav","flac"]}).set(LoaderKey.Xml,{extensions:["xml","svg","html"],mimeType:{xml:"application/xml",svg:"image/svg+xml",html:"text/html"}}).set(LoaderKey.Font,{extensions:["woff2","woff","ttf","otf","eot"]}),s.domParser=new DOMParser,s}(),AsyncPreloaderInstance=new AsyncPreloader;exports.AsyncPreloader=AsyncPreloader,exports.default=AsyncPreloaderInstance; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var LoaderKey,tslib_1=require("tslib"),FontFaceObserver=require("fontfaceobserver-es");!function(e){e.Json="Json",e.ArrayBuffer="ArrayBuffer",e.Blob="Blob",e.FormData="FormData",e.Text="Text",e.Image="Image",e.Video="Video",e.Audio="Audio",e.Xml="Xml",e.Font="Font"}(LoaderKey||(LoaderKey={}));var AsyncPreloader=function(){function i(){var e=this;this.items=new Map,this.defaultBodyMethod="blob",this.defaultLoader=LoaderKey.Text,this.loadItems=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,Promise.all(t.map(this.loadItem))];case 1:return[2,e.sent()]}})})},this.loadItem=function(n){return tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=i.getFileExtension(n.src),[4,this["load"+(n.loader||i.getLoaderKey(t))](n)];case 1:return r=e.sent(),this.items.set(n.id||n.src,r),[2,r]}})})},this.loadManifest=function(n,o){return void 0===o&&(o="items"),tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,this.loadJson({src:n})];case 1:return t=e.sent(),r=i.getProp(t,o),[4,this.loadItems(r)];case 2:return[2,e.sent()]}})})},this.loadText=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().text()];case 2:return[2,e.sent()]}})})},this.loadJson=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().json()];case 2:return[2,e.sent()]}})})},this.loadArrayBuffer=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().arrayBuffer()];case 2:return[2,e.sent()]}})})},this.loadBlob=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().blob()];case 2:return[2,e.sent()]}})})},this.loadFormData=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().formData()];case 2:return[2,e.sent()]}})})},this.loadImage=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){var r,n;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return r=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,r]:(n=new Image,[4,new Promise(function(e,t){n.addEventListener("load",function(){return e(n)},!1),n.addEventListener("error",t,!1),n.src=URL.createObjectURL(r)})]);case 3:return[2,e.sent()]}})})},this.loadVideo=function(t){return tslib_1.__awaiter(e,void 0,void 0,function(){var r,n;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return r=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,r]:(n=document.createElement("video"),[4,new Promise(function(e,t){n.addEventListener("canplaythrough",function(){return e(n)},!1),n.addEventListener("error",t,!1),n.src=URL.createObjectURL(r)})]);case 3:return[2,e.sent()]}})})},this.loadAudio=function(n){return tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(n)];case 1:return[4,e.sent()[n.body||this.defaultBodyMethod]()];case 2:return t=e.sent(),n.body&&n.body!==this.defaultBodyMethod?[2,t]:((r=document.createElement("audio")).autoplay=!1,r.preload="auto",r.src=URL.createObjectURL(t),[4,new Promise(function(e,t){r.addEventListener("canplaythrough",function(){return e(r)},!1),r.addEventListener("error",t,!1)})]);case 3:return[2,e.sent()]}})})},this.loadXml=function(n){return tslib_1.__awaiter(e,void 0,void 0,function(){var t,r;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return n.mimeType||(t=i.getFileExtension(n.src),n=tslib_1.__assign({},n,{mimeType:i.getMimeType(LoaderKey.Xml,t)})),[4,i.fetchItem(n)];case 1:return[4,e.sent().text()];case 2:return r=e.sent(),[2,i.domParser.parseFromString(r,n.mimeType)]}})})},this.loadFont=function(r){return tslib_1.__awaiter(e,void 0,void 0,function(){var t;return tslib_1.__generator(this,function(e){switch(e.label){case 0:return t=r.id,[4,new FontFaceObserver(t,r.options||{}).load()];case 1:return e.sent(),[2,t]}})})}}return i.fetchItem=function(e){return fetch(e.src,e.options||{})},i.getProp=function(e,t){var r=Array.isArray(t)?t:t.split(".").filter(function(e){return e.length});return r.length?i.getProp(e[r.shift()],r):e},i.getFileExtension=function(e){return(e.match(/[^\\\/]\.([^.\\\/]+)$/)||[null]).pop()},i.getLoaderKey=function(t){var e=Array.from(i.loaders).find(function(e){return e[1].extensions.includes(t)});return e?e[0]:LoaderKey.Text},i.getMimeType=function(e,t){var r=i.loaders.get(e);return r.mimeType[t]||r.defaultMimeType},i.loaders=(new Map).set(LoaderKey.Text,{extensions:["txt"]}).set(LoaderKey.Json,{extensions:["json"]}).set(LoaderKey.Image,{extensions:["jpeg","jpg","gif","png","webp"]}).set(LoaderKey.Video,{extensions:["webm","ogg","mp4"]}).set(LoaderKey.Audio,{extensions:["webm","ogg","mp3","wav","flac"]}).set(LoaderKey.Xml,{extensions:["xml","svg","html"],mimeType:{xml:"application/xml",svg:"image/svg+xml",html:"text/html"},defaultMimeType:"xml"}).set(LoaderKey.Font,{extensions:["woff2","woff","ttf","otf","eot"]}),i.domParser=new DOMParser,i}(),AsyncPreloaderInstance=new AsyncPreloader;exports.AsyncPreloader=AsyncPreloader,exports.default=AsyncPreloaderInstance; |
import { __awaiter, __generator, __assign } from 'tslib'; | ||
import FontFaceObserver from 'fontfaceobserver-es'; | ||
import get from 'lodash-es/get'; | ||
import * as FontFaceObserver from 'fontfaceobserver-es'; | ||
@@ -81,3 +80,3 @@ /** | ||
this.loadItem = function (item) { return __awaiter(_this, void 0, void 0, function () { | ||
var extension, loaderKey, mimeType, loadedItem; | ||
var extension, loaderKey, loadedItem; | ||
return __generator(this, function (_a) { | ||
@@ -88,7 +87,2 @@ switch (_a.label) { | ||
loaderKey = item.loader || AsyncPreloader.getLoaderKey(extension); | ||
if (loaderKey === "Xml" && !item.mimeType) { | ||
mimeType = AsyncPreloader.loaders.get(LoaderKey.Xml).mimeType[extension] || | ||
AsyncPreloader.loaders.get(LoaderKey.Xml).mimeType["svg"]; | ||
item = __assign({}, item, { mimeType: mimeType }); | ||
} | ||
return [4 /*yield*/, this["load" + loaderKey](item)]; | ||
@@ -107,3 +101,3 @@ case 1: | ||
* @param {string} src Manifest src url | ||
* @param {string} [key="items"] Manifest key in the JSON object containing the array of LoadItem. Used by [lodash.get](https://lodash.com/docs/4.17.5#get). | ||
* @param {string} [key="items"] Manifest key in the JSON object containing the array of LoadItem. | ||
* @returns {Promise<LoadedValue[]>} | ||
@@ -122,3 +116,3 @@ */ | ||
loadedManifest = _a.sent(); | ||
items = get(loadedManifest, key); | ||
items = AsyncPreloader.getProp(loadedManifest, key); | ||
return [4 /*yield*/, this.loadItems(items)]; | ||
@@ -330,6 +324,11 @@ case 2: return [2 /*return*/, _a.sent()]; | ||
this.loadXml = function (item) { return __awaiter(_this, void 0, void 0, function () { | ||
var response, data; | ||
var extension, response, data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, AsyncPreloader.fetchItem(item)]; | ||
case 0: | ||
if (!item.mimeType) { | ||
extension = AsyncPreloader.getFileExtension(item.src); | ||
item = __assign({}, item, { mimeType: AsyncPreloader.getMimeType(LoaderKey.Xml, extension) }); | ||
} | ||
return [4 /*yield*/, AsyncPreloader.fetchItem(item)]; | ||
case 1: | ||
@@ -344,2 +343,8 @@ response = _a.sent(); | ||
}); }; | ||
/** | ||
* Load a font via a FontFaceObserver instance | ||
* | ||
* @param {LoadItem} item Item to load (id correspond to the fontName). | ||
* @returns {Promise<string>} Fulfilled value with fontName initial id. | ||
*/ | ||
this.loadFont = function (item) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -350,3 +355,3 @@ var fontName, font; | ||
case 0: | ||
fontName = item.id || AsyncPreloader.getFileName(item.src); | ||
fontName = item.id; | ||
font = new FontFaceObserver(fontName, item.options || {}); | ||
@@ -372,2 +377,18 @@ return [4 /*yield*/, font.load()]; | ||
/** | ||
* Get an object property by its path in the form 'a[0].b.c' or ['a', '0', 'b', 'c']. | ||
* Similar to [lodash.get](https://lodash.com/docs/4.17.5#get). | ||
* | ||
* @param {any} object Object with nested properties | ||
* @param {(string | string[])} path Path to the desired property | ||
* @returns {any} The returned object property | ||
*/ | ||
AsyncPreloader.getProp = function (object, path) { | ||
var p = Array.isArray(path) | ||
? path | ||
: path.split(".").filter(function (index) { return index.length; }); | ||
if (!p.length) | ||
return object; | ||
return AsyncPreloader.getProp(object[p.shift()], p); | ||
}; | ||
/** | ||
* Get file extension from path | ||
@@ -382,14 +403,2 @@ * | ||
/** | ||
* Get file name from path | ||
* | ||
* @param {any} path | ||
* @returns {string} | ||
*/ | ||
AsyncPreloader.getFileName = function (path) { | ||
return path | ||
.replace(/^.*[\\\/]/, "") | ||
.split(".") | ||
.shift(); | ||
}; | ||
/** | ||
* Retrieve loader key from extension (when the loader option isn't specified in the LoadItem) | ||
@@ -407,2 +416,13 @@ * | ||
/** | ||
* Retrieve mime type from extension | ||
* | ||
* @param {LoaderKey} loaderKey | ||
* @param {string} extension | ||
* @returns {string} | ||
*/ | ||
AsyncPreloader.getMimeType = function (loaderKey, extension) { | ||
var loader = AsyncPreloader.loaders.get(loaderKey); | ||
return loader.mimeType[extension] || loader.defaultMimeType; | ||
}; | ||
/** | ||
* Loader types and the extensions they handle | ||
@@ -424,3 +444,4 @@ * | ||
html: "text/html" | ||
} | ||
}, | ||
defaultMimeType: "xml" | ||
}) | ||
@@ -427,0 +448,0 @@ .set(LoaderKey.Font, { |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tslib"),require("fontfaceobserver-es"),require("lodash-es/get")):"function"==typeof define&&define.amd?define(["exports","tslib","fontfaceobserver-es","lodash-es/get"],t):t(e.AsyncPreloader={},e.tslib,e.FontFaceObserver,e._.get)}(this,function(e,a,r,i){"use strict";var u,t;r=r&&r.hasOwnProperty("default")?r.default:r,i=i&&i.hasOwnProperty("default")?i.default:i,(t=u||(u={})).Json="Json",t.ArrayBuffer="ArrayBuffer",t.Blob="Blob",t.FormData="FormData",t.Text="Text",t.Image="Image",t.Video="Video",t.Audio="Audio",t.Xml="Xml",t.Font="Font";var n=function(){function s(){var e=this;this.items=new Map,this.defaultBodyMethod="blob",this.defaultLoader=u.Text,this.loadItems=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,Promise.all(t.map(this.loadItem))];case 1:return[2,e.sent()]}})})},this.loadItem=function(i){return a.__awaiter(e,void 0,void 0,function(){var t,n,r,o;return a.__generator(this,function(e){switch(e.label){case 0:return t=s.getFileExtension(i.src),"Xml"!==(n=i.loader||s.getLoaderKey(t))||i.mimeType||(r=s.loaders.get(u.Xml).mimeType[t]||s.loaders.get(u.Xml).mimeType.svg,i=a.__assign({},i,{mimeType:r})),[4,this["load"+n](i)];case 1:return o=e.sent(),this.items.set(i.id||i.src,o),[2,o]}})})},this.loadManifest=function(r,o){return void 0===o&&(o="items"),a.__awaiter(e,void 0,void 0,function(){var t,n;return a.__generator(this,function(e){switch(e.label){case 0:return[4,this.loadJson({src:r})];case 1:return t=e.sent(),n=i(t,o),[4,this.loadItems(n)];case 2:return[2,e.sent()]}})})},this.loadText=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().text()];case 2:return[2,e.sent()]}})})},this.loadJson=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().json()];case 2:return[2,e.sent()]}})})},this.loadArrayBuffer=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().arrayBuffer()];case 2:return[2,e.sent()]}})})},this.loadBlob=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().blob()];case 2:return[2,e.sent()]}})})},this.loadFormData=function(t){return a.__awaiter(e,void 0,void 0,function(){return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent().formData()];case 2:return[2,e.sent()]}})})},this.loadImage=function(t){return a.__awaiter(e,void 0,void 0,function(){var n,r;return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return n=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,n]:(r=new Image,[4,new Promise(function(e,t){r.addEventListener("load",function(){return e(r)},!1),r.addEventListener("error",t,!1),r.src=URL.createObjectURL(n)})]);case 3:return[2,e.sent()]}})})},this.loadVideo=function(t){return a.__awaiter(e,void 0,void 0,function(){var n,r;return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return n=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,n]:(r=document.createElement("video"),[4,new Promise(function(e,t){r.addEventListener("canplaythrough",function(){return e(r)},!1),r.addEventListener("error",t,!1),r.src=URL.createObjectURL(n)})]);case 3:return[2,e.sent()]}})})},this.loadAudio=function(r){return a.__awaiter(e,void 0,void 0,function(){var t,n;return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(r)];case 1:return[4,e.sent()[r.body||this.defaultBodyMethod]()];case 2:return t=e.sent(),r.body&&r.body!==this.defaultBodyMethod?[2,t]:((n=document.createElement("audio")).autoplay=!1,n.preload="auto",n.src=URL.createObjectURL(t),[4,new Promise(function(e,t){n.addEventListener("canplaythrough",function(){return e(n)},!1),n.addEventListener("error",t,!1)})]);case 3:return[2,e.sent()]}})})},this.loadXml=function(n){return a.__awaiter(e,void 0,void 0,function(){var t;return a.__generator(this,function(e){switch(e.label){case 0:return[4,s.fetchItem(n)];case 1:return[4,e.sent().text()];case 2:return t=e.sent(),[2,s.domParser.parseFromString(t,n.mimeType)]}})})},this.loadFont=function(n){return a.__awaiter(e,void 0,void 0,function(){var t;return a.__generator(this,function(e){switch(e.label){case 0:return t=n.id||s.getFileName(n.src),[4,new r(t,n.options||{}).load()];case 1:return e.sent(),[2,t]}})})}}return s.fetchItem=function(e){return fetch(e.src,e.options||{})},s.getFileExtension=function(e){return(e.match(/[^\\\/]\.([^.\\\/]+)$/)||[null]).pop()},s.getFileName=function(e){return e.replace(/^.*[\\\/]/,"").split(".").shift()},s.getLoaderKey=function(t){var e=Array.from(s.loaders).find(function(e){return e[1].extensions.includes(t)});return e?e[0]:u.Text},s.loaders=(new Map).set(u.Text,{extensions:["txt"]}).set(u.Json,{extensions:["json"]}).set(u.Image,{extensions:["jpeg","jpg","gif","png","webp"]}).set(u.Video,{extensions:["webm","ogg","mp4"]}).set(u.Audio,{extensions:["webm","ogg","mp3","wav","flac"]}).set(u.Xml,{extensions:["xml","svg","html"],mimeType:{xml:"application/xml",svg:"image/svg+xml",html:"text/html"}}).set(u.Font,{extensions:["woff2","woff","ttf","otf","eot"]}),s.domParser=new DOMParser,s}(),o=new n;e.AsyncPreloader=n,e.default=o,Object.defineProperty(e,"__esModule",{value:!0})}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("tslib"),require("fontfaceobserver-es")):"function"==typeof define&&define.amd?define(["exports","tslib","fontfaceobserver-es"],t):t(e.AsyncPreloader={},e.tslib,e.FontFaceObserver)}(this,function(e,s,r){"use strict";var o,t;(t=o||(o={})).Json="Json",t.ArrayBuffer="ArrayBuffer",t.Blob="Blob",t.FormData="FormData",t.Text="Text",t.Image="Image",t.Video="Video",t.Audio="Audio",t.Xml="Xml",t.Font="Font";var n=function(){function i(){var e=this;this.items=new Map,this.defaultBodyMethod="blob",this.defaultLoader=o.Text,this.loadItems=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,Promise.all(t.map(this.loadItem))];case 1:return[2,e.sent()]}})})},this.loadItem=function(r){return s.__awaiter(e,void 0,void 0,function(){var t,n;return s.__generator(this,function(e){switch(e.label){case 0:return t=i.getFileExtension(r.src),[4,this["load"+(r.loader||i.getLoaderKey(t))](r)];case 1:return n=e.sent(),this.items.set(r.id||r.src,n),[2,n]}})})},this.loadManifest=function(r,o){return void 0===o&&(o="items"),s.__awaiter(e,void 0,void 0,function(){var t,n;return s.__generator(this,function(e){switch(e.label){case 0:return[4,this.loadJson({src:r})];case 1:return t=e.sent(),n=i.getProp(t,o),[4,this.loadItems(n)];case 2:return[2,e.sent()]}})})},this.loadText=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().text()];case 2:return[2,e.sent()]}})})},this.loadJson=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().json()];case 2:return[2,e.sent()]}})})},this.loadArrayBuffer=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().arrayBuffer()];case 2:return[2,e.sent()]}})})},this.loadBlob=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().blob()];case 2:return[2,e.sent()]}})})},this.loadFormData=function(t){return s.__awaiter(e,void 0,void 0,function(){return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent().formData()];case 2:return[2,e.sent()]}})})},this.loadImage=function(t){return s.__awaiter(e,void 0,void 0,function(){var n,r;return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return n=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,n]:(r=new Image,[4,new Promise(function(e,t){r.addEventListener("load",function(){return e(r)},!1),r.addEventListener("error",t,!1),r.src=URL.createObjectURL(n)})]);case 3:return[2,e.sent()]}})})},this.loadVideo=function(t){return s.__awaiter(e,void 0,void 0,function(){var n,r;return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(t)];case 1:return[4,e.sent()[t.body||this.defaultBodyMethod]()];case 2:return n=e.sent(),t.body&&t.body!==this.defaultBodyMethod?[2,n]:(r=document.createElement("video"),[4,new Promise(function(e,t){r.addEventListener("canplaythrough",function(){return e(r)},!1),r.addEventListener("error",t,!1),r.src=URL.createObjectURL(n)})]);case 3:return[2,e.sent()]}})})},this.loadAudio=function(r){return s.__awaiter(e,void 0,void 0,function(){var t,n;return s.__generator(this,function(e){switch(e.label){case 0:return[4,i.fetchItem(r)];case 1:return[4,e.sent()[r.body||this.defaultBodyMethod]()];case 2:return t=e.sent(),r.body&&r.body!==this.defaultBodyMethod?[2,t]:((n=document.createElement("audio")).autoplay=!1,n.preload="auto",n.src=URL.createObjectURL(t),[4,new Promise(function(e,t){n.addEventListener("canplaythrough",function(){return e(n)},!1),n.addEventListener("error",t,!1)})]);case 3:return[2,e.sent()]}})})},this.loadXml=function(r){return s.__awaiter(e,void 0,void 0,function(){var t,n;return s.__generator(this,function(e){switch(e.label){case 0:return r.mimeType||(t=i.getFileExtension(r.src),r=s.__assign({},r,{mimeType:i.getMimeType(o.Xml,t)})),[4,i.fetchItem(r)];case 1:return[4,e.sent().text()];case 2:return n=e.sent(),[2,i.domParser.parseFromString(n,r.mimeType)]}})})},this.loadFont=function(n){return s.__awaiter(e,void 0,void 0,function(){var t;return s.__generator(this,function(e){switch(e.label){case 0:return t=n.id,[4,new r(t,n.options||{}).load()];case 1:return e.sent(),[2,t]}})})}}return i.fetchItem=function(e){return fetch(e.src,e.options||{})},i.getProp=function(e,t){var n=Array.isArray(t)?t:t.split(".").filter(function(e){return e.length});return n.length?i.getProp(e[n.shift()],n):e},i.getFileExtension=function(e){return(e.match(/[^\\\/]\.([^.\\\/]+)$/)||[null]).pop()},i.getLoaderKey=function(t){var e=Array.from(i.loaders).find(function(e){return e[1].extensions.includes(t)});return e?e[0]:o.Text},i.getMimeType=function(e,t){var n=i.loaders.get(e);return n.mimeType[t]||n.defaultMimeType},i.loaders=(new Map).set(o.Text,{extensions:["txt"]}).set(o.Json,{extensions:["json"]}).set(o.Image,{extensions:["jpeg","jpg","gif","png","webp"]}).set(o.Video,{extensions:["webm","ogg","mp4"]}).set(o.Audio,{extensions:["webm","ogg","mp3","wav","flac"]}).set(o.Xml,{extensions:["xml","svg","html"],mimeType:{xml:"application/xml",svg:"image/svg+xml",html:"text/html"},defaultMimeType:"xml"}).set(o.Font,{extensions:["woff2","woff","ttf","otf","eot"]}),i.domParser=new DOMParser,i}(),i=new n;e.AsyncPreloader=n,e.default=i,Object.defineProperty(e,"__esModule",{value:!0})}); |
@@ -64,3 +64,3 @@ import { BodyMethod, LoadItem, LoadedValue, LoadedXMLValue, LoaderKey } from "./types"; | ||
* @param {string} src Manifest src url | ||
* @param {string} [key="items"] Manifest key in the JSON object containing the array of LoadItem. Used by [lodash.get](https://lodash.com/docs/4.17.5#get). | ||
* @param {string} [key="items"] Manifest key in the JSON object containing the array of LoadItem. | ||
* @returns {Promise<LoadedValue[]>} | ||
@@ -144,2 +144,8 @@ */ | ||
loadXml: (item: LoadItem) => Promise<LoadedXMLValue>; | ||
/** | ||
* Load a font via a FontFaceObserver instance | ||
* | ||
* @param {LoadItem} item Item to load (id correspond to the fontName). | ||
* @returns {Promise<string>} Fulfilled value with fontName initial id. | ||
*/ | ||
loadFont: (item: LoadItem) => Promise<string>; | ||
@@ -152,17 +158,19 @@ /** | ||
*/ | ||
private static fetchItem(item); | ||
private static fetchItem; | ||
/** | ||
* Get file extension from path | ||
* Get an object property by its path in the form 'a[0].b.c' or ['a', '0', 'b', 'c']. | ||
* Similar to [lodash.get](https://lodash.com/docs/4.17.5#get). | ||
* | ||
* @param {(RequestInfo | USVString)} path | ||
* @returns {string} | ||
* @param {any} object Object with nested properties | ||
* @param {(string | string[])} path Path to the desired property | ||
* @returns {any} The returned object property | ||
*/ | ||
private static getFileExtension(path); | ||
private static getProp; | ||
/** | ||
* Get file name from path | ||
* Get file extension from path | ||
* | ||
* @param {any} path | ||
* @param {(RequestInfo | USVString)} path | ||
* @returns {string} | ||
*/ | ||
private static getFileName(path); | ||
private static getFileExtension; | ||
/** | ||
@@ -174,3 +182,11 @@ * Retrieve loader key from extension (when the loader option isn't specified in the LoadItem) | ||
*/ | ||
private static getLoaderKey(extension); | ||
private static getLoaderKey; | ||
/** | ||
* Retrieve mime type from extension | ||
* | ||
* @param {LoaderKey} loaderKey | ||
* @param {string} extension | ||
* @returns {string} | ||
*/ | ||
private static getMimeType; | ||
} | ||
@@ -177,0 +193,0 @@ export { AsyncPreloader }; |
@@ -70,3 +70,3 @@ /** | ||
Xml = "Xml", | ||
Font = "Font", | ||
Font = "Font" | ||
} | ||
@@ -89,2 +89,8 @@ /** | ||
}; | ||
/** | ||
* Optional defaultMimeType used to handle the Response. | ||
* | ||
* Note: Only used to parse the document in the Xml Loader. | ||
*/ | ||
defaultMimeType?: string; | ||
} |
{ | ||
"name": "async-preloader", | ||
"version": "3.4.0", | ||
"version": "4.0.0", | ||
"description": "Assets preloader using ES2017 async/await and fetch.", | ||
@@ -17,3 +17,3 @@ "main": "lib/async-preloader.cjs.js", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=8.0.0" | ||
}, | ||
@@ -24,2 +24,3 @@ "keywords": [ | ||
"async", | ||
"await", | ||
"typescript", | ||
@@ -30,7 +31,7 @@ "ES2017" | ||
"dev": "rollup -c -w -m inline --environment NODE_ENV:development", | ||
"build": "rollup -c --environment NODE_ENV:production", | ||
"test": "jest test/", | ||
"jest": "jest test/ --watch --coverage --no-cache", | ||
"docs": "typedoc --out docs --mode modules --excludeExternals --includeDeclarations --module commonjs --theme default src && touch docs/.nojekyll", | ||
"clean": "rm -rf lib && rm -rf docs", | ||
"build": "rollup -c --environment NODE_ENV:production", | ||
"docs": "typedoc --out docs --mode modules --excludeExternals --includeDeclarations --module commonjs --theme default src && touch docs/.nojekyll", | ||
"test": "jest test/", | ||
"preversion": "npm run clean && npm run test", | ||
@@ -48,2 +49,5 @@ "version": "npm run build && npm run docs && git add -A", | ||
], | ||
"testEnvironmentOptions": { | ||
"resources": "usable" | ||
}, | ||
"transform": { | ||
@@ -53,6 +57,7 @@ "^.+\\.(t|j)s$": "ts-jest" | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!(lodash-es)/)" | ||
"node_modules/(?!(fontfaceobserver-es)/)" | ||
], | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|js)x?$", | ||
"moduleFileExtensions": [ | ||
"json", | ||
"ts", | ||
@@ -65,27 +70,23 @@ "tsx", | ||
"devDependencies": { | ||
"@types/jest": "^22.1.3", | ||
"@types/node": "^9.4.6", | ||
"fetch-mock": "^6.0.0-beta.10", | ||
"isomorphic-fetch": "^2.2.1", | ||
"jest": "^21.2.1", | ||
"jest-environment-jsdom-latest": "^20.0.4", | ||
"jsdom": "^11.6.2", | ||
"node-fetch": "^2.0.0", | ||
"prettier": "^1.10.2", | ||
"rollup": "^0.56.3", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"rollup-plugin-node-resolve": "^3.0.3", | ||
"rollup-plugin-typescript2": "^0.11.1", | ||
"rollup-plugin-uglify": "^4.0.0", | ||
"rollup-watch": "^4.3.1", | ||
"ts-jest": "^22.4.0", | ||
"tslint": "^5.9.1", | ||
"typedoc": "^0.10.0", | ||
"typescript": "^2.7.2" | ||
"@types/jest": "^23.3.2", | ||
"@types/node": "^10.9.4", | ||
"jest": "^23.6.0", | ||
"jsdom": "^12.0.0", | ||
"node-fetch": "^2.2.0", | ||
"prettier": "^1.14.2", | ||
"rollup": "^0.65.2", | ||
"rollup-plugin-commonjs": "^9.1.6", | ||
"rollup-plugin-node-resolve": "^3.4.0", | ||
"rollup-plugin-typescript2": "^0.17.0", | ||
"rollup-plugin-uglify": "^5.0.2", | ||
"ts-jest": "^23.1.4", | ||
"tslint": "^5.11.0", | ||
"typedoc": "^0.12.0", | ||
"typescript": "^3.0.3", | ||
"xmldom": "^0.1.27" | ||
}, | ||
"dependencies": { | ||
"fontfaceobserver-es": "^3.2.0", | ||
"lodash-es": "^4.17.10", | ||
"tslib": "^1.9.0" | ||
"tslib": "^1.9.3" | ||
} | ||
} |
@@ -23,3 +23,3 @@ # async-preloader | ||
This section covers the basic usage of `AsyncPreloader`. For more informations about async/await, see [Async functions - making promises friendly](https://developers.google.com/web/fundamentals/primers/async-functions). | ||
This section covers the basic usage of `AsyncPreloader`. For more informations about async/await, see [Async functions - making promises friendly](https://developers.google.com/web/fundamentals/primers/async-functions). Usage in Node.js environment is limited to its capacity to handle `fetch` requests. Polyfills like [`node-fetch`](https://www.npmjs.com/package/node-fetch) and [`xmldom`](https://www.npmjs.com/package/xmldom) might come handy. | ||
@@ -32,12 +32,13 @@ ### Preload items and retrieve them | ||
const items = [ | ||
{ "id": "myDefaultFile", "src": "assets/default" }, | ||
{ "id": "myTextFile", "src": "assets/text.txt" }, | ||
{ "id": "myJsonFile", "src": "assets/json.json" }, | ||
{ "id": "myImageFile", "src": "assets/image.jpg" }, | ||
{ "id": "myVideoFile", "src": "assets/video.mp4" }, | ||
{ "id": "myAudioFile", "src": "assets/audio.mp3" }, | ||
{ "id": "myXmlFile", "src": "assets/xml.xml" }, | ||
{ "id": "mySvgFile", "src": "assets/xml.svg" }, | ||
{ "id": "myHtmlFile", "src": "assets/xml.html" }, | ||
{ "id": "myFont", "src": "Open Sans Regular", "loader": "Font" }, | ||
{ "id": "myDefaultFile", "src": "assets/default" }, | ||
{ "id": "myTextFile", "src": "assets/text.txt" }, | ||
{ "id": "myJsonFile", "src": "assets/json.json" }, | ||
{ "id": "myImageFile", "src": "assets/image.jpg" }, | ||
{ "id": "myVideoFile", "src": "assets/video.mp4" }, | ||
{ "id": "myAudioFile", "src": "assets/audio.mp3" }, | ||
{ "id": "myXmlFile", "src": "assets/xml.xml" }, | ||
{ "id": "mySvgFile", "src": "assets/xml.svg" }, | ||
{ "id": "myHtmlFile", "src": "assets/xml.html" }, | ||
{ "id": "myDefaultXmlFile", "src": "assets/xml", "loader": "Xml" }, | ||
{ "id": "myFont", "loader": "Font" }, | ||
{ "src": "assets/fileWithoutId" } // Can be retrieved with the src property eg. AsyncPreloader.items.get("assets/fileWithoutId") | ||
@@ -44,0 +45,0 @@ ]; |
@@ -21,3 +21,3 @@ import resolve from "rollup-plugin-node-resolve"; | ||
].filter(Boolean), | ||
external: ["tslib", "lodash-es/get", "fontfaceobserver-es"], | ||
external: ["tslib", "fontfaceobserver-es"], | ||
output: { | ||
@@ -30,3 +30,2 @@ name: "AsyncPreloader", | ||
tslib: "tslib", | ||
"lodash-es/get": "_.get", | ||
"fontfaceobserver-es": "FontFaceObserver" | ||
@@ -46,3 +45,3 @@ } | ||
].filter(Boolean), | ||
external: ["tslib", "lodash-es/get", "fontfaceobserver-es"], | ||
external: ["tslib", "fontfaceobserver-es"], | ||
output: { | ||
@@ -62,3 +61,3 @@ format: "cjs", | ||
], | ||
external: ["tslib", "lodash-es/get", "fontfaceobserver-es"], | ||
external: ["tslib", "fontfaceobserver-es"], | ||
output: { | ||
@@ -65,0 +64,0 @@ format: "es", |
{ | ||
"items": [ | ||
{ "id": "myDefaultFile", "src": "assets/default" }, | ||
{ "id": "myTextFile", "src": "assets/text.txt" }, | ||
{ "id": "myJsonFile", "src": "assets/json.json" }, | ||
{ "id": "myXmlFile", "src": "assets/xml.xml" }, | ||
{ "id": "mySvgFile", "src": "assets/xml.svg" }, | ||
{ "id": "myHtmlFile", "src": "assets/xml.html" }, | ||
{ "id": "myFont", "src": "Open Sans Regular", "loader": "Font" } | ||
{ | ||
"id": "myDefaultFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/default" | ||
}, | ||
{ | ||
"id": "myTextFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/text.txt" | ||
}, | ||
{ | ||
"id": "myJsonFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/json.json" | ||
}, | ||
{ | ||
"id": "myXmlFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.xml" | ||
}, | ||
{ | ||
"id": "mySvgFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.svg" | ||
}, | ||
{ | ||
"id": "myHtmlFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.html" | ||
}, | ||
{ | ||
"id": "myXmlDefaulFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml", | ||
"loader": "Xml" | ||
} | ||
], | ||
"custom": { | ||
"path": [ | ||
{ "id": "myDefaultFile", "src": "assets/default" }, | ||
{ "id": "myTextFile", "src": "assets/text.txt" }, | ||
{ "id": "myJsonFile", "src": "assets/json.json" }, | ||
{ "id": "myXmlFile", "src": "assets/xml.xml" }, | ||
{ "id": "mySvgFile", "src": "assets/xml.svg" }, | ||
{ "id": "myHtmlFile", "src": "assets/xml.html" }, | ||
{ "id": "myFont", "src": "Open Sans Regular", "loader": "Font" } | ||
{ | ||
"id": "myDefaultFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/default" | ||
}, | ||
{ | ||
"id": "myTextFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/text.txt" | ||
}, | ||
{ | ||
"id": "myJsonFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/json.json" | ||
}, | ||
{ | ||
"id": "myXmlFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.xml" | ||
}, | ||
{ | ||
"id": "mySvgFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.svg" | ||
}, | ||
{ | ||
"id": "myHtmlFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml.html" | ||
}, | ||
{ | ||
"id": "myXmlDefaulFile", | ||
"src": "https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/xml", | ||
"loader": "Xml" | ||
} | ||
] | ||
} | ||
} |
136
test/data.js
@@ -0,18 +1,130 @@ | ||
const rootUrl = | ||
"https://raw.githubusercontent.com/dmnsgn/async-preloader/master/test/assets/"; | ||
export const items = new Map() | ||
.set("default", { id: "myDefaultFile", src: "assets/default" }) | ||
.set("txt", { id: "myTextFile", src: "assets/text.txt" }) | ||
.set("json", { id: "myJsonFile", src: "assets/json.json" }) | ||
.set("jpg", { id: "myImageFile", src: "assets/image.jpg" }) | ||
.set("mp4", { id: "myVideoFile", src: "assets/video.mp4" }) | ||
.set("mp3", { id: "myAudioFile", src: "assets/audio.mp3" }) | ||
.set("xml", { id: "myXmlFile", src: "assets/xml.xml" }) | ||
.set("svg", { id: "mySvgFile", src: "assets/xml.svg" }) | ||
.set("html", { id: "myHtmlFile", src: "assets/xml.html" }) | ||
.set("font", { id: "myFont", src: "Open Sans Regular", loader: "Font" }); | ||
.set("default", { id: "myDefaultFile", src: `${rootUrl}default` }) | ||
.set("txt", { id: "myTextFile", src: `${rootUrl}text.txt` }) | ||
.set("json", { id: "myJsonFile", src: `${rootUrl}json.json` }) | ||
.set("jpg", { id: "myImageFile", src: `${rootUrl}image.jpg` }) | ||
.set("mp4", { id: "myVideoFile", src: `${rootUrl}video.mp4` }) | ||
.set("mp3", { id: "myAudioFile", src: `${rootUrl}audio.mp3` }) | ||
.set("xml", { id: "myXmlFile", src: `${rootUrl}xml.xml` }) | ||
.set("svg", { id: "mySvgFile", src: `${rootUrl}xml.svg` }) | ||
.set("html", { id: "myHtmlFile", src: `${rootUrl}xml.html` }) | ||
.set("defaultXml", { | ||
id: "myXmlDefaultFile", | ||
src: `${rootUrl}xml`, | ||
loader: "Xml" | ||
}); | ||
export const expected = new Map() | ||
.set("string", `test string\n`) | ||
.set("font", "myFont") | ||
.set( | ||
"html", | ||
new DOMParser().parseFromString( | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title></title> | ||
</head> | ||
<body> | ||
</body> | ||
</html> | ||
\n`, | ||
"text/html" | ||
) | ||
) | ||
.set( | ||
"svg", | ||
new DOMParser().parseFromString( | ||
`<svg xmlns="http://www.w3.org/2000/svg" width="150" height="100" viewBox="0 0 3 2"> | ||
<rect width="1" height="2" x="0" fill="#008d46" /> | ||
<rect width="1" height="2" x="1" fill="#ffffff" /> | ||
<rect width="1" height="2" x="2" fill="#d2232c" /> | ||
</svg> | ||
\n`, | ||
"image/svg+xml" | ||
) | ||
) | ||
.set( | ||
"xml", | ||
new DOMParser().parseFromString( | ||
`<?xml version="1.0"?> | ||
<planet> | ||
<ocean> | ||
<name>Arctic</name> | ||
<area>13,000</area> | ||
<depth>1,200</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Atlantic</name> | ||
<area>87,000</area> | ||
<depth>3,900</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Pacific</name> | ||
<area>180,000</area> | ||
<depth>4,000</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Indian</name> | ||
<area>75,000</area> | ||
<depth>3,900</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Southern</name> | ||
<area>20,000</area> | ||
<depth>4,500</depth> | ||
</ocean> | ||
</planet> | ||
\n`, | ||
"application/xml" | ||
) | ||
) | ||
.set( | ||
"defaultXml", | ||
new DOMParser().parseFromString( | ||
`<?xml version="1.0"?> | ||
<planet> | ||
<ocean> | ||
<name>Arctic</name> | ||
<area>13,000</area> | ||
<depth>1,200</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Atlantic</name> | ||
<area>87,000</area> | ||
<depth>3,900</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Pacific</name> | ||
<area>180,000</area> | ||
<depth>4,000</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Indian</name> | ||
<area>75,000</area> | ||
<depth>3,900</depth> | ||
</ocean> | ||
<ocean> | ||
<name>Southern</name> | ||
<area>20,000</area> | ||
<depth>4,500</depth> | ||
</ocean> | ||
</planet> | ||
\n`, | ||
"application/xml" | ||
) | ||
) | ||
.set("json", { test: "json" }); | ||
export const manifestSrc = "assets/manifest.json"; | ||
export const fontItem = { | ||
id: "myFont", | ||
src: "Open Sans Regular", | ||
loader: "Font" | ||
}; | ||
export const manifestSrc = `${rootUrl}manifest.json`; |
@@ -1,7 +0,3 @@ | ||
/** | ||
* @jest-environment jsdom-latest | ||
*/ | ||
import Preloader, { AsyncPreloader } from "../src/"; | ||
import { items, expected, manifestSrc } from "./data"; | ||
import { mockFetch, unmockFetch } from "./mock"; | ||
import { items, expected, fontItem, manifestSrc } from "./data"; | ||
@@ -16,3 +12,3 @@ const excludes = ["jpg", "mp4", "mp3"]; | ||
beforeEach(() => { | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 2000; | ||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 4000; | ||
}); | ||
@@ -22,3 +18,2 @@ | ||
Preloader.items.clear(); | ||
unmockFetch(); | ||
}); | ||
@@ -31,9 +26,9 @@ | ||
let itemsToLoad = Array.from(items.values()).filter( | ||
item => !excludes.includes(AsyncPreloader.getFileExtension(item.src)) | ||
item => | ||
AsyncPreloader.getFileExtension(item.src) === null || | ||
!excludes.includes(AsyncPreloader.getFileExtension(item.src)) | ||
); | ||
itemsToLoad | ||
.filter(item => item.loader !== "Font") | ||
.forEach(item => mockFetch(item.src)); | ||
const data = await Preloader.loadItems(itemsToLoad); | ||
expect(data).toBeInstanceOf(Array); | ||
@@ -49,11 +44,2 @@ expect(data).toHaveLength(items.size - excludes.length); // TEMP: fix blob tests | ||
mockFetch(manifestSrc); | ||
Array.from(items.values()) | ||
.filter( | ||
item => | ||
!excludes.includes(AsyncPreloader.getFileExtension(item.src)) | ||
) | ||
.filter(item => item.loader !== "Font") | ||
.forEach(item => mockFetch(item.src)); | ||
const data = await Preloader.loadManifest(manifestSrc); | ||
@@ -71,12 +57,4 @@ | ||
mockFetch(manifestSrc); | ||
Array.from(items.values()) | ||
.filter( | ||
item => | ||
!excludes.includes(AsyncPreloader.getFileExtension(item.src)) | ||
) | ||
.filter(item => item.loader !== "Font") | ||
.forEach(item => mockFetch(item.src)); | ||
const data = await Preloader.loadManifest(manifestSrc, "custom.path"); | ||
const data = await Preloader.loadManifest(manifestSrc, "custom.path"); | ||
expect(data).toBeInstanceOf(Array); | ||
@@ -95,3 +73,2 @@ expect(data).toHaveLength(items.size - excludes.length); // TEMP: fix blob tests | ||
const item = items.get("json"); | ||
mockFetch(item.src); | ||
@@ -106,3 +83,2 @@ const data = await Preloader.loadJson(item); | ||
const item = items.get("txt"); | ||
mockFetch(item.src); | ||
@@ -117,3 +93,2 @@ const data = await Preloader.loadText(item); | ||
const item = items.get("mp3"); | ||
mockFetch(item.src, "arrayBuffer"); | ||
@@ -128,8 +103,28 @@ const data = await Preloader.loadArrayBuffer(item); | ||
const item = items.get("jpg"); | ||
mockFetch(item.src, "blob"); | ||
const data = await Preloader.loadBlob(item); | ||
expect(data.constructor.name).toEqual(Blob.name); | ||
// TODO: expect(data).toBeInstanceOf(Blob); | ||
// node-fetch doesn't have a real Blob | ||
// expect(data).toBeInstanceOf(Blob); | ||
}); | ||
// it("should load a LoadItem with FormData loader and return instanceof FormData", async () => { | ||
// expect.assertions(1); | ||
// const formData = new FormData(); | ||
// formData.append("a", "1"); | ||
// const item = { | ||
// src: "https://httpbin.org/post", | ||
// options: { | ||
// method: "POST", | ||
// body: formData | ||
// } | ||
// }; | ||
// const data = await Preloader.loadFormData(item); | ||
// expect(data.constructor.name).toEqual(FormData.name); | ||
// }); | ||
}); | ||
@@ -142,3 +137,2 @@ | ||
const item = items.get("default"); | ||
mockFetch(item.src); | ||
@@ -153,3 +147,2 @@ const data = await Preloader.loadItem(item); | ||
const item = items.get("json"); | ||
mockFetch(item.src); | ||
@@ -164,5 +157,5 @@ const data = await Preloader.loadItem(item); | ||
// const item = items.get("jpg"); | ||
// mockFetch(item.src, "blob"); | ||
// const data = await Preloader.loadItem(item); | ||
// // console.log(data.constructor) | ||
// expect(data).toBeInstanceOf(HTMLMediaElement); | ||
@@ -175,3 +168,2 @@ // }); | ||
// const item = items.get("mp4"); | ||
// mockFetch(item.src, "blob"); | ||
@@ -186,3 +178,2 @@ // const data = await Preloader.loadItem(item); | ||
// const item = items.get("mp3"); | ||
// mockFetch(item.src, "blob"); | ||
@@ -197,3 +188,2 @@ // const data = await Preloader.loadItem(item); | ||
const item = items.get("mp3"); | ||
mockFetch(item.src); | ||
item.body = "arrayBuffer"; | ||
@@ -205,12 +195,42 @@ | ||
it("should load a LoadItem with Xml Loader and return HTMLDocument", async () => { | ||
expect.assertions(1); | ||
it("should load a LoadItem (xml) with Xml Loader and return Document", async () => { | ||
expect.assertions(2); | ||
const item = items.get("xml"); | ||
mockFetch(item.src); | ||
const data = await Preloader.loadItem(item); | ||
expect(data).toBeInstanceOf(HTMLDocument); | ||
expect(data.constructor.name).toEqual(Document.name); | ||
expect(data).toEqual(expected.get("xml")); | ||
}); | ||
it("should load a LoadItem (html) with Xml Loader and return Document", async () => { | ||
expect.assertions(2); | ||
const item = items.get("html"); | ||
const data = await Preloader.loadItem(item); | ||
expect(data.constructor.name).toEqual(Document.name); | ||
expect(data).toEqual(expected.get("html")); | ||
}); | ||
it("should load a LoadItem (svg) with Xml Loader and return Document", async () => { | ||
expect.assertions(2); | ||
const item = items.get("svg"); | ||
const data = await Preloader.loadItem(item); | ||
expect(data.constructor.name).toEqual(Document.name); | ||
expect(data).toEqual(expected.get("svg")); | ||
}); | ||
it("should load a LoadItem (svg file with a special extension) with Xml Loader and return Document", async () => { | ||
expect.assertions(2); | ||
const item = items.get("defaultXml"); | ||
const data = await Preloader.loadItem(item); | ||
expect(data.constructor.name).toEqual(Document.name); | ||
expect(data).toEqual(expected.get("xml")); | ||
}); | ||
it("should load a LoadItem with Text Loader and return String", async () => { | ||
@@ -220,3 +240,2 @@ expect.assertions(1); | ||
const item = items.get("txt"); | ||
mockFetch(item.src); | ||
@@ -227,8 +246,11 @@ const data = await Preloader.loadItem(item); | ||
it("should check the font is loaded with FontFaceObserver", async () => { | ||
it("should check the font with id, src, and loader, fails to load as it is not supported in Node", async () => { | ||
expect.assertions(1); | ||
const item = items.get("font"); | ||
const data = await Preloader.loadItem(item); | ||
expect(data).toBe(expected.get("font")); | ||
await expect(Preloader.loadItem(fontItem)).rejects.toEqual({ | ||
family: "myFont", | ||
stretch: "normal", | ||
style: "normal", | ||
weight: "normal" | ||
}); | ||
}); | ||
@@ -242,3 +264,2 @@ }); | ||
const item = items.get("default"); | ||
mockFetch(item.src); | ||
@@ -255,5 +276,3 @@ const data = await Preloader.loadItem({ src: item.src }); | ||
); | ||
itemsToLoad | ||
.filter(item => item.loader !== "Font") | ||
.forEach(item => mockFetch(item.src)); | ||
itemsToLoad.filter(item => item.loader !== "Font"); | ||
@@ -287,7 +306,2 @@ let loadedCount = 0; | ||
}); | ||
it("should return file name from path", () => { | ||
const data = AsyncPreloader.getFileName("/rwrgw/Open Sans Regular"); | ||
expect(data).toBe("Open Sans Regular"); | ||
}); | ||
}); | ||
@@ -294,0 +308,0 @@ |
@@ -1,5 +0,9 @@ | ||
import "isomorphic-fetch"; | ||
import fetch from "node-fetch"; | ||
import { DOMParser } from "xmldom"; | ||
import { ArrayBufferToData } from "./utils"; | ||
global.DOMParser = DOMParser; | ||
global.fetch = fetch; | ||
URL.createObjectURL = blob => { | ||
@@ -6,0 +10,0 @@ let string; |
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
8652426
2
16
24
1299
156
2
6
- Removedlodash-es@^4.17.10
- Removedlodash-es@4.17.21(transitive)
Updatedtslib@^1.9.3