single-file-core 1.2.6 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.2.6",
3
+ "version": "1.2.8",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -69,12 +69,26 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
69
69
  options.password = prompt("Please enter the password to view the page");
70
70
  }
71
71
  name = entry.filename.match(/^([0-9_]+\/)?(.*)$/)[2];
72
+ let mimeType;
72
73
  if (entry.filename.match(/index\.html$/) || entry.filename.match(/stylesheet_[0-9]+\.css/) || entry.filename.match(/scripts\/[0-9]+\.js/)) {
73
74
  dataWriter = new zip.TextWriter();
74
75
  textContent = await entry.getData(dataWriter, options);
76
+ if (entry.filename.match(/index\.html$/)) {
77
+ mimeType = "text/html;charset=utf-8";
78
+ } else {
79
+ if (entry.filename.match(/stylesheet_[0-9]+\.css/)) {
80
+ mimeType = "text/css;charset=utf-8";
81
+ } else if (entry.filename.match(/scripts\/[0-9]+\.js/)) {
82
+ mimeType = "text/javascript;charset=utf-8";
83
+ }
84
+ if (textContent !== undefined) {
85
+ content = noBlobURL ? await getDataURI(textContent, mimeType) : URL.createObjectURL(new Blob([textContent], { type: mimeType }));
86
+ } else {
87
+ content = "data:text/plain,";
88
+ }
89
+ }
75
90
  } else {
76
91
  const extension = entry.filename.match(/\.([^.]+)/);
77
- let mimeType;
78
92
  if (extension && extension[1] && KNOWN_MIMETYPES[extension[1]]) {
79
93
  mimeType = KNOWN_MIMETYPES[extension[1]];
80
94
  } else {
@@ -87,7 +101,7 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
87
101
  content = URL.createObjectURL(blob);
88
102
  }
89
103
  }
90
- resources.push({ filename: entry.filename, name, url: entry.comment, content, blob, textContent, parentResources: [] });
104
+ resources.push({ filename: entry.filename, name, url: entry.comment, content, mimeType, blob, textContent, parentResources: [] });
91
105
  }));
92
106
  await zipReader.close();
93
107
  let docContent, origDocContent, url;
@@ -105,7 +119,7 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
105
119
  }
106
120
  const isScript = resource.filename.match(/scripts\/[0-9]+\.js/);
107
121
  if (!isScript) {
108
- resources.forEach(innerResource => {
122
+ await Promise.all(resources.map(async innerResource => {
109
123
  if (innerResource.filename.startsWith(prefixPath) && innerResource.filename != resource.filename) {
110
124
  const filename = innerResource.filename.substring(prefixPath.length);
111
125
  if (!filename.match(/manifest\.json$/)) {
@@ -117,15 +131,10 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
117
131
  }
118
132
  }
119
133
  }
120
- });
134
+ }));
135
+ resource.content = await getDataURI(resource.textContent, resource.mimeType);
121
136
  }
122
- let mimeType;
123
- if (resource.filename.match(/stylesheet_[0-9]+\.css/)) {
124
- mimeType = "text/css";
125
- } else if (isScript) {
126
- mimeType = "text/javascript";
127
- } else if (resource.filename.match(/index\.html$/)) {
128
- mimeType = "text/html";
137
+ if (resource.filename.match(/index\.html$/)) {
129
138
  if (shadowRootScriptURL) {
130
139
  resource.textContent = resource.textContent.replace(/<script data-template-shadow-root.*<\/script>/g, "<script data-template-shadow-root src=" + shadowRootScriptURL + "></" + "script>");
131
140
  }
@@ -133,19 +142,17 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
133
142
  if (resource.filename.match(/^([0-9_]+\/)?index\.html$/)) {
134
143
  docContent = resource.textContent;
135
144
  url = resource.url;
136
- } else {
137
- const reader = new FileReader();
138
- if (resource.textContent) {
139
- reader.readAsDataURL(new Blob([resource.textContent], { type: mimeType + ";charset=utf-8" }));
140
- resource.content = await new Promise((resolve, reject) => {
141
- reader.addEventListener("load", () => resolve(reader.result), false);
142
- reader.addEventListener("error", reject, false);
143
- });
144
- } else {
145
- resource.content = "data:text/plain,";
146
- }
147
145
  }
148
146
  }
149
147
  }
150
148
  return { docContent, origDocContent, resources, url };
149
+
150
+ async function getDataURI(textContent, mimeType) {
151
+ const reader = new FileReader();
152
+ reader.readAsDataURL(new Blob([textContent], { type: mimeType }));
153
+ return new Promise((resolve, reject) => {
154
+ reader.addEventListener("load", () => resolve(reader.result), false);
155
+ reader.addEventListener("error", reject, false);
156
+ });
157
+ }
151
158
  }