@pi-r/chrome 0.2.11 → 0.2.12

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.
Files changed (3) hide show
  1. package/index.d.ts +6 -6
  2. package/index.js +57 -9
  3. package/package.json +5 -5
package/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import type { IFileManager } from '@e-mc/types/lib';
2
-
3
- import type { ChromeDocumentConstructor, DocumentAsset } from './types';
4
-
5
- declare const Chrome: ChromeDocumentConstructor<IFileManager<DocumentAsset>>;
6
-
1
+ import type { IFileManager } from '@e-mc/types/lib';
2
+
3
+ import type { ChromeDocumentConstructor, DocumentAsset } from './types';
4
+
5
+ declare const Chrome: ChromeDocumentConstructor<IFileManager<DocumentAsset>>;
6
+
7
7
  export = Chrome;
package/index.js CHANGED
@@ -11,6 +11,9 @@ const transform_1 = require("@e-mc/document/transform");
11
11
  const Document = require('@e-mc/document');
12
12
  const CACHE_DBRESULT = new Map();
13
13
  const CACHE_LOCALFILE = new Map();
14
+ const CACHE_ETAG = new Map();
15
+ const CACHE_CONTENT = new Map();
16
+ const CACHE_FORMAT = new Map();
14
17
  const CACHE_DATASET = {};
15
18
  const CACHE_ATRULES = {};
16
19
  let CACHE_TOTAL = 0;
@@ -931,6 +934,9 @@ class ChromeDocument extends Document {
931
934
  }
932
935
  CACHE_TOTAL = stored.length - result;
933
936
  }
937
+ CACHE_ETAG.clear();
938
+ CACHE_CONTENT.clear();
939
+ CACHE_FORMAT.clear();
934
940
  }
935
941
  return result + (parent && result > 0 ? await super.purgeMemory(typeof parent === 'number' && parent > 0 ? parent : percent, limit, true) : 0);
936
942
  }
@@ -958,12 +964,27 @@ class ChromeDocument extends Document {
958
964
  if (outHtml) {
959
965
  const { etag, initialValue } = htmlFile;
960
966
  let buffer;
961
- if (!initialValue || !(etag && initialValue.etag === etag && (buffer = initialValue.buffer)) || fetched.find(item => item.bundleReplace && (!(0, types_1.isArray)(item.from) || item.from.includes('style')))) {
967
+ if (!initialValue || !(etag && initialValue.etag === etag && (buffer = initialValue.buffer)) || fetched.find(item => item !== htmlFile && item.etag && CACHE_ETAG.get(item.localUri) !== item.etag && (item.inlineContent || item.bundleReplace && (!Array.isArray(item.from) || item.from.includes('style'))))) {
968
+ const isValid = (item) => !!item.localUri && item !== htmlFile && !item.invalid;
969
+ for (const item of fetched) {
970
+ if (item.etag && !item.content && isValid(item)) {
971
+ CACHE_ETAG.set(item.localUri, item.etag);
972
+ }
973
+ }
974
+ for (const item of instance.assets) {
975
+ if (isValid(item)) {
976
+ if (item.content) {
977
+ CACHE_CONTENT.set(item.localUri, item.content);
978
+ }
979
+ if (item.format) {
980
+ CACHE_FORMAT.set(item.localUri, item.format.toString());
981
+ }
982
+ }
983
+ }
962
984
  break;
963
985
  }
964
986
  try {
965
- const { localUri, encoding } = htmlFile;
966
- fs.writeFileSync(localUri, buffer, (0, types_1.getEncoding)(encoding));
987
+ fs.writeFileSync(htmlFile.localUri, buffer, (0, types_1.getEncoding)(htmlFile.encoding));
967
988
  htmlFile.buffer = buffer;
968
989
  htmlFile.sourceUTF8 = undefined;
969
990
  }
@@ -971,12 +992,39 @@ class ChromeDocument extends Document {
971
992
  break;
972
993
  }
973
994
  }
974
- const { jsFiles, cssFiles, svgFiles } = instance;
975
- const copiedAssets = this.copiedAssets;
976
- const wasModified = (item) => fetched.includes(item) || copiedAssets.includes(item) && (!(0, types_1.isEmpty)(item.bundleId) || !(0, types_1.isEmpty)(item.trailingContent));
977
- const js = jsFiles.filter(item => item.imports || wasModified(item));
978
- const css = cssFiles.filter(wasModified);
979
- const svg = svgFiles.filter(wasModified);
995
+ const wasModified = (item) => {
996
+ const localUri = item.localUri;
997
+ let result = fetched.includes(item) || Array.isArray(item.from) && item.from.includes('style');
998
+ if (localUri) {
999
+ if (item.etag && !item.content && !item.invalid) {
1000
+ CACHE_ETAG.set(localUri, item.etag);
1001
+ }
1002
+ else {
1003
+ CACHE_ETAG.delete(localUri);
1004
+ }
1005
+ if (!item.content || item.invalid) {
1006
+ CACHE_CONTENT.delete(localUri);
1007
+ }
1008
+ else if (CACHE_CONTENT.get(localUri) !== item.content || !item.inlineContent && !Document.isPath(localUri)) {
1009
+ CACHE_CONTENT.set(localUri, item.content);
1010
+ result = true;
1011
+ }
1012
+ if (!item.format || item.invalid) {
1013
+ CACHE_FORMAT.delete(localUri);
1014
+ }
1015
+ else {
1016
+ const format = item.format.toString();
1017
+ if (CACHE_FORMAT.get(localUri) !== format || !item.inlineContent && !Document.isPath(localUri)) {
1018
+ CACHE_FORMAT.set(localUri, format);
1019
+ result = true;
1020
+ }
1021
+ }
1022
+ }
1023
+ return result || this.copiedAssets.includes(item) && (!(0, types_1.isEmpty)(item.bundleId) || !(0, types_1.isEmpty)(item.trailingContent));
1024
+ };
1025
+ const js = instance.jsFiles.filter(item => wasModified(item) || !!item.imports);
1026
+ const css = instance.cssFiles.filter(wasModified);
1027
+ const svg = instance.svgFiles.filter(wasModified);
980
1028
  if (js.length || css.length || svg.length) {
981
1029
  instance.jsFiles = js;
982
1030
  instance.cssFiles = css;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Chrome document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "publishConfig": {
@@ -20,9 +20,9 @@
20
20
  "license": "MIT",
21
21
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
22
  "dependencies": {
23
- "@e-mc/cloud": "^0.5.6",
24
- "@e-mc/core": "^0.5.6",
25
- "@e-mc/document": "^0.5.6",
26
- "@e-mc/types": "^0.5.6"
23
+ "@e-mc/cloud": "^0.5.10",
24
+ "@e-mc/core": "^0.5.10",
25
+ "@e-mc/document": "^0.5.10",
26
+ "@e-mc/types": "^0.5.10"
27
27
  }
28
28
  }