@e-mc/document 0.7.1 → 0.8.1

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/index.js CHANGED
@@ -11,6 +11,7 @@ const db_1 = require("../db");
11
11
  const util_1 = require("./util");
12
12
  const transform_1 = require("./transform");
13
13
  const parse_1 = require("./parse");
14
+ const PLATFORM_WIN32 = process.platform === 'win32';
14
15
  const CACHE_PACKAGE = {};
15
16
  const CACHE_REQUIRE = {};
16
17
  const CACHE_ETAG = {};
@@ -34,7 +35,7 @@ function deleteTransform(map, key, timeout) {
34
35
  --CACHE_TOTAL;
35
36
  }
36
37
  function getSourceMappingURL(value, css) {
37
- if (value.indexOf(' ') !== -1) {
38
+ if (value.includes(' ')) {
38
39
  value = encodeURIComponent(value);
39
40
  }
40
41
  return css ? `\n/*# sourceMappingURL=${value} */\n` : `\n//# sourceMappingURL=${value}\n`;
@@ -329,14 +330,9 @@ class Document extends core_1.Client {
329
330
  if (config) {
330
331
  let baseUrl;
331
332
  ({ baseUrl, ignoreModules, ignoreExtensions } = config);
332
- let username, users;
333
- if (username = this.host?.username) {
334
- if (!(0, types_1.isPlainObject)(users = this.settings.users?.[username])) {
335
- users = undefined;
336
- }
337
- else if (Array.isArray(users.extensions)) {
338
- this._extensions = users.extensions.slice(0);
339
- }
333
+ const users = this.getUserSettings();
334
+ if (users && Array.isArray(users.extensions)) {
335
+ this._extensions = users.extensions.slice(0);
340
336
  }
341
337
  if (baseUrl) {
342
338
  let pages = this.settings.pages;
@@ -445,11 +441,14 @@ class Document extends core_1.Client {
445
441
  }
446
442
  return this;
447
443
  }
448
- abort(name) {
449
- if (this.aborted || name && this.settingsOf(name, 'abort') !== true) {
444
+ abort(name, reason) {
445
+ if (name instanceof Error) {
446
+ reason || (reason = name);
447
+ }
448
+ if (this.aborted || (0, types_1.isString)(name) && this.settingsOf(name, 'abort') !== true) {
450
449
  return;
451
450
  }
452
- super.abort();
451
+ super.abort(reason);
453
452
  }
454
453
  customize(options) {
455
454
  const transform = options.transform;
@@ -686,26 +685,98 @@ class Document extends core_1.Client {
686
685
  }
687
686
  return result || fallback;
688
687
  }
688
+ findSourceScope(uri, imports) {
689
+ const scopes = imports.scopes;
690
+ if ((0, types_1.isPlainObject)(scopes)) {
691
+ const qualified = [];
692
+ for (const qualifier in scopes) {
693
+ const item = scopes[qualifier];
694
+ if (uri.includes(qualifier) && (0, types_1.isPlainObject)(item)) {
695
+ qualified.push([qualifier, item]);
696
+ }
697
+ }
698
+ return qualified.sort((a, b) => b[0].length - a[0].length).map(item => item[1]);
699
+ }
700
+ return [];
701
+ }
689
702
  findSourceRoot(uri, imports = this.imports) {
690
- if (imports) {
691
- const normalizeDir = (value) => {
692
- const sep = value.includes('\\') && !value.includes('/') ? '\\' : '/';
693
- return value + (value[value.length - 1] !== sep ? sep : '');
694
- };
695
- for (const url in imports) {
696
- if (uri === url || normalizeDir(uri) === normalizeDir(url)) {
697
- return imports[url];
703
+ if (!(0, types_1.isPlainObject)(imports)) {
704
+ return;
705
+ }
706
+ const toPosix = (value) => value.replace(/(?:^\\|\\+)/g, '/');
707
+ const normalizeDir = (value, check) => {
708
+ const sep = !check && value.includes('\\') && !value.includes('/') ? '\\' : '/';
709
+ if (check) {
710
+ value = toPosix(value).toLowerCase();
711
+ }
712
+ return value.endsWith(sep) ? value : value + sep;
713
+ };
714
+ const importsStrict = this.getUserSettings()?.imports_strict ?? this.settings.imports_strict;
715
+ const scopes = (importsStrict ? this.findSourceScope(uri, imports) : []).concat([imports]);
716
+ const isDir = /[\\/]$/;
717
+ let result;
718
+ for (const scope of scopes) {
719
+ for (const url in scope) {
720
+ if (uri === url && (0, types_1.isString)(result = scope[url])) {
721
+ if (importsStrict) {
722
+ if (isDir.test(url) || !core_1.Client.isPath(result)) {
723
+ continue;
724
+ }
725
+ return toPosix(result);
726
+ }
727
+ return result;
698
728
  }
699
729
  }
700
- const found = [];
701
- for (const hostname in imports) {
702
- if (uri.startsWith(normalizeDir(hostname)) && (0, types_1.isString)(imports[hostname])) {
703
- found.push(hostname);
730
+ }
731
+ const remote = toPosix(uri);
732
+ const found = [];
733
+ if (importsStrict) {
734
+ const protocol = core_1.Client.isURL(remote);
735
+ for (const scope of scopes) {
736
+ for (const url in scope) {
737
+ const local = protocol ? url : toPosix(url);
738
+ if (isDir.test(local) && remote.startsWith(local) && (result = scope[url]) && isDir.test(result)) {
739
+ if (protocol) {
740
+ found.push([url, result]);
741
+ }
742
+ else if (core_1.Client.isPath(result = path.join(result, remote.substring(local.length)))) {
743
+ return PLATFORM_WIN32 ? toPosix(result) : result;
744
+ }
745
+ }
704
746
  }
705
747
  }
706
- if (found.length) {
707
- const hostname = found.sort((a, b) => b.length - a.length)[0];
708
- return core_1.Client.toPosix(path.resolve(imports[hostname], uri.substring(normalizeDir(hostname).length).split('?')[0]));
748
+ }
749
+ else {
750
+ for (const scope of scopes) {
751
+ for (const url in scope) {
752
+ const local = normalizeDir(url);
753
+ if ((normalizeDir(uri) === local || PLATFORM_WIN32 && normalizeDir(uri, true) === local.toLowerCase()) && (0, types_1.isString)(result = scope[url])) {
754
+ return result;
755
+ }
756
+ }
757
+ for (const url in scope) {
758
+ if (uri.startsWith(normalizeDir(url)) && (0, types_1.isString)(result = scope[url])) {
759
+ found.push([url, result]);
760
+ }
761
+ }
762
+ }
763
+ }
764
+ if (found.length) {
765
+ found.sort((a, b) => b[0].length - a[0].length);
766
+ const toPath = (hostname) => path.resolve(hostname[1], uri.substring(normalizeDir(hostname[0]).length).split('?')[0]);
767
+ if (importsStrict) {
768
+ for (const url of found) {
769
+ if (core_1.Client.isPath(result = toPath(url))) {
770
+ break;
771
+ }
772
+ result = '';
773
+ }
774
+ }
775
+ else {
776
+ result = toPath(found[0]);
777
+ }
778
+ if (result) {
779
+ return PLATFORM_WIN32 ? toPosix(result) : result;
709
780
  }
710
781
  }
711
782
  }
@@ -1212,7 +1283,7 @@ class Document extends core_1.Client {
1212
1283
  let pathname;
1213
1284
  if (typeof cacheType === 'string' && core_1.Client.createDir(pathname = path.join(cacheType, 'transform', type))) {
1214
1285
  try {
1215
- fs.writeFileSync(pathname = path.join(pathname, (0, types_1.generateUUID)()), JSON.stringify(formatData));
1286
+ fs.writeFileSync(pathname = path.join(pathname, (0, types_1.incrementUUID)()), JSON.stringify(formatData));
1216
1287
  return pathname;
1217
1288
  }
1218
1289
  catch {
@@ -1298,8 +1369,7 @@ class Document extends core_1.Client {
1298
1369
  this._imports = Object.assign(imports, value);
1299
1370
  }
1300
1371
  else {
1301
- const username = this.host?.username;
1302
- imports = username && this.settings.users?.[username]?.imports || this.module.imports;
1372
+ imports = this.getUserSettings()?.imports || this.module.imports;
1303
1373
  this._imports = Object.assign(this._imports || {}, (0, types_1.isPlainObject)(imports) ? imports : undefined, value);
1304
1374
  }
1305
1375
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e-mc/document",
3
- "version": "0.7.1",
3
+ "version": "0.8.1",
4
4
  "description": "Document constructor for E-mc.",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,9 +20,9 @@
20
20
  "license": "BSD 3-Clause",
21
21
  "homepage": "https://github.com/anpham6/e-mc#readme",
22
22
  "dependencies": {
23
- "@e-mc/core": "0.7.1",
24
- "@e-mc/db": "0.7.1",
25
- "@e-mc/types": "0.7.1",
23
+ "@e-mc/core": "0.8.1",
24
+ "@e-mc/db": "0.8.1",
25
+ "@e-mc/types": "0.8.1",
26
26
  "chalk": "4.1.2",
27
27
  "htmlparser2": "^9.0.0",
28
28
  "js-yaml": "^4.1.0",
package/parse/dom.js CHANGED
@@ -30,7 +30,7 @@ class DomWriter extends index_1.XmlWriter {
30
30
  if (ignoreTagGroup) {
31
31
  for (let i = 0, length = ignoreTagGroup.length; i < length; i += 2) {
32
32
  const start = ignoreTagGroup[i];
33
- if (start[0] === '<') {
33
+ if (start.startsWith('<')) {
34
34
  tagGroup.push(new RegExp('^' + (0, types_1.escapePattern)(start)));
35
35
  }
36
36
  const end = ignoreTagGroup[i + 1];
package/parse/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ /* eslint @typescript-eslint/prefer-includes: off */
2
3
  Object.defineProperty(exports, "__esModule", { value: true });
3
4
  exports.XmlElement = exports.XmlWriter = exports.IGNORE_FLAG = void 0;
4
5
  const htmlparser2 = require("htmlparser2");
@@ -910,11 +911,11 @@ class XmlWriter {
910
911
  }
911
912
  let tagName = this.ignoreTagName || '';
912
913
  if (tagName) {
913
- if (value[0] !== '|' && tagName[tagName.length - 1] !== '|') {
914
+ if (!value.startsWith('|') && !tagName.endsWith('|')) {
914
915
  tagName += '|';
915
916
  }
916
917
  }
917
- else if (value[0] === '|') {
918
+ else if (value.startsWith('|')) {
918
919
  value = value.substring(1);
919
920
  }
920
921
  this.ignoreTagName = tagName + value;
@@ -16,7 +16,7 @@ const kUsername = Symbol('username');
16
16
  const kProductionRelease = Symbol('productionRelease');
17
17
  function parseMap(data, value) {
18
18
  let [mimeType, encoding] = data.split(';');
19
- if (!encoding && mimeType.indexOf('/') === -1) {
19
+ if (!encoding && !mimeType.includes('/')) {
20
20
  encoding = mimeType;
21
21
  }
22
22
  return encoding.includes('base64') ? Buffer.from(value, 'base64').toString() : value;
package/util.js CHANGED
@@ -50,7 +50,7 @@ function replaceAll(source, valueOf, opening = '{{', closing = '}}') {
50
50
  exports.replaceAll = replaceAll;
51
51
  function concatString(values, newline) {
52
52
  if ((0, types_1.isArray)(values)) {
53
- newline || (newline = values.find(item => item.indexOf('\r') !== -1) ? '\r\n' : '\n');
53
+ newline || (newline = values.find(item => item.indexOf('\r') !== -1) ? '\r\n' : '\n'); // eslint-disable-line @typescript-eslint/prefer-includes
54
54
  return values.reduce((a, b) => a + newline + b, '');
55
55
  }
56
56
  return typeof values === 'string' ? values : '';