@@ -19,14 +19,14 @@ const checkURL = url => {
1919 }
2020} ;
2121
22- const dependency = { } ;
22+ const external = { } ;
2323
2424/**
2525 * @param {string } url
2626 * @template T
2727 * @returns {Promise<T> }
2828 */
29- dependency . import = url => {
29+ external . import = url => {
3030 checkURL ( url ) ;
3131 // Need to specify webpackIgnore so that webpack compiles this directly to a call to import()
3232 // instead of trying making it try to use the webpack import system.
@@ -37,9 +37,37 @@ dependency.import = url => {
3737 * @param {string } url
3838 * @returns {Promise<Response> }
3939 */
40- dependency . fetch = url => {
40+ external . fetch = async url => {
4141 checkURL ( url ) ;
42- return fetch ( url ) ;
42+ const res = await fetch ( url ) ;
43+ if ( ! res . ok ) {
44+ throw new Error ( `HTTP ${ res . status } fetching ${ url } ` ) ;
45+ }
46+ return res ;
47+ } ;
48+
49+ /**
50+ * @param {string } url
51+ * @returns {Promise<string> }
52+ */
53+ external . dataURL = async url => {
54+ const res = await external . fetch ( url ) ;
55+ const blob = await res . blob ( ) ;
56+ return new Promise ( ( resolve , reject ) => {
57+ const fr = new FileReader ( ) ;
58+ fr . onload = ( ) => resolve ( fr . result ) ;
59+ fr . onerror = ( ) => reject ( fr . error ) ;
60+ fr . readAsDataURL ( blob ) ;
61+ } ) ;
62+ } ;
63+
64+ /**
65+ * @param {string } url
66+ * @returns {Promise<Blob> }
67+ */
68+ external . blobURL = async url => {
69+ const res = await external . fetch ( url ) ;
70+ return res . blob ( ) ;
4371} ;
4472
4573/**
@@ -48,18 +76,12 @@ dependency.fetch = url => {
4876 * @template T
4977 * @returns {Promise<T> }
5078 */
51- dependency . evalAndReturn = async ( url , returnExpression ) => {
52- checkURL ( url ) ;
53-
54- const res = await fetch ( url ) ;
55- if ( ! res . ok ) {
56- throw new Error ( `HTTP ${ res . status } fetching ${ url } ` ) ;
57- }
58-
79+ external . evalAndReturn = async ( url , returnExpression ) => {
80+ const res = await external . fetch ( url ) ;
5981 const text = await res . text ( ) ;
6082 const js = `${ text } ;return ${ returnExpression } ` ;
6183 const fn = new Function ( js ) ;
6284 return fn ( ) ;
6385} ;
6486
65- module . exports = dependency ;
87+ module . exports = external ;
0 commit comments