@pi-r/chrome 0.7.1 → 0.7.3

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 (2) hide show
  1. package/index.js +74 -25
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -13,6 +13,9 @@ const CACHE_DBRESULT = new Map();
13
13
  const CACHE_LOCALFILE = new Map();
14
14
  const CACHE_QUERY = new Map();
15
15
  const CACHE_SELECTOR = new Map();
16
+ const CACHE_ETAG = new Map();
17
+ const CACHE_CONTENT = new Map();
18
+ const CACHE_FORMAT = new Map();
16
19
  const CACHE_DATASET = {};
17
20
  const CACHE_ATRULES = {};
18
21
  let CACHE_TOTAL = 0;
@@ -898,6 +901,7 @@ const getEndIndex = (match) => match.index + match[0].length;
898
901
  const getRelativeURL = (file) => joinPath(file.moveTo, file.pathname, file.inlineFilename || file.filename) + (!file.static ? restoreSearchParams(file.url?.search) : '');
899
902
  const errorHtml = ({ tagName, tagIndex = -1 }) => (0, types_1.errorMessage)('html', 'Location did not resolve', tagName.toLowerCase() + (tagIndex >= 0 ? ': ' + tagIndex : ''));
900
903
  const errorDataSource = (value, hint) => (0, types_1.errorMessage)('data-source', value, hint);
904
+ const isHtmlOnly = (assets, items, htmlFile) => !items.find(item => item !== htmlFile && assets.includes(item));
901
905
  class ChromeDocument extends Document {
902
906
  static INTERNAL_ASSIGNUUID = "__assign__";
903
907
  static INTERNAL_SERVERROOT = "__serverroot__";
@@ -931,38 +935,56 @@ class ChromeDocument extends Document {
931
935
  }
932
936
  CACHE_QUERY.clear();
933
937
  CACHE_SELECTOR.clear();
938
+ CACHE_ETAG.clear();
939
+ CACHE_CONTENT.clear();
940
+ CACHE_FORMAT.clear();
934
941
  }
935
942
  return result + (parent && result > 0 ? await super.purgeMemory(typeof parent === 'number' && parent > 0 ? parent : percent, limit, true) : 0);
936
943
  }
937
944
  static async finalize(instance) {
938
945
  if (instance.aborted) {
939
- return Promise.reject((0, types_1.createAbortError)());
946
+ return (0, types_1.createAbortError)(true);
940
947
  }
941
- const productionRelease = instance.productionRelease;
942
- const processing = { host: this, startTime: process.hrtime(), hashed: new Set(), contentMap: {}, bufferMap: {}, cacheMiss: [], productionRelease: productionRelease ? true : undefined };
943
- const { Db: db, htmlFile, assets } = instance;
944
- const outHtml = htmlFile && !isIgnored(htmlFile, true);
945
- if (db && await db.commit()) {
946
- db.writeTimeElapsed('DB', ["Transactions were committed", db.moduleName], processing.startTime, { ...Document.LOG_STYLE_SUCCESS, type: 65536 });
948
+ const startTime = process.hrtime();
949
+ if (instance.Db && await instance.Db.commit()) {
950
+ instance.Db.writeTimeElapsed('DB', ["Transactions were committed", instance.Db.moduleName], startTime, { ...Document.LOG_STYLE_SUCCESS, type: 65536 });
947
951
  }
952
+ const productionRelease = !!instance.productionRelease;
953
+ const processing = { host: this, startTime, hashed: new Set(), contentMap: {}, bufferMap: {}, cacheMiss: [], productionRelease: productionRelease ? true : undefined };
954
+ const htmlFile = instance.htmlFile;
955
+ const outHtml = !!htmlFile && !isIgnored(htmlFile, true);
948
956
  if (instance.dataSource.length === 0 && outHtml) {
949
957
  const incremental = htmlFile.incremental ?? this.incremental;
950
958
  if (incremental === 'etag' || incremental === 'exists') {
951
959
  const fetched = this.fetchedAssets;
952
- const isHtmlOnly = () => !fetched.find(item => item !== htmlFile && assets.includes(item));
953
- if (!productionRelease || instance.productionIncremental && (isHtmlOnly() || !fetched.find(item => item.hash))) {
960
+ if (!productionRelease || instance.productionIncremental && (isHtmlOnly(instance.assets, fetched, htmlFile) || !fetched.find(item => item.hash))) {
954
961
  let hint = '';
955
962
  switch (incremental) {
956
963
  case 'etag': {
957
964
  if (outHtml) {
958
965
  const { etag, initialValue } = htmlFile;
959
966
  let buffer;
960
- 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 && (!(0, types_1.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
+ }
961
984
  break;
962
985
  }
963
986
  try {
964
- const { localUri, encoding } = htmlFile;
965
- fs.writeFileSync(localUri, buffer, (0, types_1.getEncoding)(encoding));
987
+ fs.writeFileSync(htmlFile.localUri, buffer, (0, types_1.getEncoding)(htmlFile.encoding));
966
988
  htmlFile.buffer = buffer;
967
989
  htmlFile.sourceUTF8 = undefined;
968
990
  }
@@ -970,22 +992,49 @@ class ChromeDocument extends Document {
970
992
  break;
971
993
  }
972
994
  }
973
- const { jsFiles, cssFiles, svgFiles } = instance;
974
- const copiedAssets = this.copiedAssets;
975
- const wasModified = (item) => fetched.includes(item) || copiedAssets.includes(item) && (!(0, types_1.isEmpty)(item.bundleId) || !(0, types_1.isEmpty)(item.trailingContent));
976
- const js = jsFiles.filter(item => item.imports || wasModified(item));
977
- const css = cssFiles.filter(wasModified);
978
- 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);
979
1028
  if (js.length || css.length || svg.length) {
980
1029
  instance.jsFiles = js;
981
1030
  instance.cssFiles = css;
982
1031
  instance.svgFiles = svg;
983
1032
  await instance.applyTransforms(processing);
984
1033
  }
985
- hint = assets.filter(item => item.etag).length + ' assets';
1034
+ hint = instance.assets.filter(item => item.etag).length + ' assets';
986
1035
  }
987
1036
  case 'exists':
988
- if (hint || isHtmlOnly()) {
1037
+ if (hint || isHtmlOnly(instance.assets, fetched, htmlFile)) {
989
1038
  this.formatMessage(4, 'HTML', ['No changes detected', 'incremental: ' + incremental], hint, { ...Document.LOG_STYLE_NOTICE, messageBgColor: 'bgWhite', messageColor: 'black' });
990
1039
  return super.finalize.call(this, instance);
991
1040
  }
@@ -998,7 +1047,7 @@ class ChromeDocument extends Document {
998
1047
  if (instance.editing.length) {
999
1048
  await instance.applyTransforms(processing);
1000
1049
  if (instance.aborted) {
1001
- return Promise.reject((0, types_1.createAbortError)());
1050
+ return (0, types_1.createAbortError)(true);
1002
1051
  }
1003
1052
  }
1004
1053
  if (productionRelease) {
@@ -1011,7 +1060,7 @@ class ChromeDocument extends Document {
1011
1060
  }
1012
1061
  static async cleanup(instance) {
1013
1062
  if (instance.aborted) {
1014
- return Promise.reject((0, types_1.createAbortError)());
1063
+ return (0, types_1.createAbortError)(true);
1015
1064
  }
1016
1065
  if (this.Watch) {
1017
1066
  const related = instance.related;
@@ -1314,7 +1363,7 @@ class ChromeDocument extends Document {
1314
1363
  }
1315
1364
  async using(data) {
1316
1365
  if (this.aborted) {
1317
- return Promise.reject((0, types_1.createAbortError)());
1366
+ return (0, types_1.createAbortError)(true);
1318
1367
  }
1319
1368
  const file = data.file;
1320
1369
  if (file.editing) {
@@ -1735,7 +1784,7 @@ class ChromeDocument extends Document {
1735
1784
  }
1736
1785
  async cloudFinalize(state) {
1737
1786
  if (this.aborted) {
1738
- return Promise.reject((0, types_1.createAbortError)());
1787
+ return (0, types_1.createAbortError)(true);
1739
1788
  }
1740
1789
  const { host, instance, localStorage } = state;
1741
1790
  const htmlFile = this.htmlFile;
@@ -1773,7 +1822,7 @@ class ChromeDocument extends Document {
1773
1822
  }
1774
1823
  }
1775
1824
  if (this.aborted) {
1776
- return Promise.reject((0, types_1.createAbortError)());
1825
+ return (0, types_1.createAbortError)(true);
1777
1826
  }
1778
1827
  }
1779
1828
  if (htmlFile) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/chrome",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
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.9.2",
24
- "@e-mc/core": "^0.9.2",
25
- "@e-mc/document": "^0.9.2",
26
- "@e-mc/types": "^0.9.2"
23
+ "@e-mc/cloud": "^0.9.6",
24
+ "@e-mc/core": "^0.9.6",
25
+ "@e-mc/document": "^0.9.6",
26
+ "@e-mc/types": "^0.9.6"
27
27
  }
28
28
  }