bare-module-traverse 2.4.3 → 2.4.4

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.js +68 -35
  2. package/lib/errors.js +6 -3
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -688,9 +688,8 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
688
688
  if (patternNegate) removeURL(matches, url)
689
689
  else addURL(matches, url)
690
690
  } else if (patternTrailer === '' || url.href.endsWith(patternTrailer)) {
691
- addURL(matches, url)
692
- } else if (patternNegate) {
693
- removeURL(matches, url)
691
+ if (patternNegate) removeURL(matches, url)
692
+ else addURL(matches, url)
694
693
  }
695
694
  }
696
695
  } else if (Array.isArray(pattern)) {
@@ -726,41 +725,14 @@ function* postresolve(url) {
726
725
  }
727
726
 
728
727
  function moduleType(url, attributes, info, opts = {}) {
729
- const { defaultType = constants.SCRIPT, referrerType, aliases = null } = opts
730
-
731
- if (typeof attributes.type === 'string') {
732
- switch (attributes.type) {
733
- case 'script':
734
- return constants.SCRIPT
735
- case 'module':
736
- return constants.MODULE
737
- case 'json':
738
- return constants.JSON
739
- case 'bundle':
740
- return constants.BUNDLE
741
- case 'addon':
742
- return constants.ADDON
743
- case 'binary':
744
- return constants.BINARY
745
- case 'text':
746
- return constants.TEXT
747
- }
748
-
749
- return 0
750
- }
728
+ const { defaultType = constants.SCRIPT, aliases = null } = opts
751
729
 
752
730
  if (url.protocol === 'data:') {
753
- const { mime } = parseDataURL(url)
754
-
755
- if (mime === null || mime.subtype === 'javascript') {
756
- if (referrerType) return referrerType
757
-
758
- return defaultType === constants.MODULE ? constants.MODULE : constants.SCRIPT
759
- }
760
-
761
- if (mime.subtype === 'json') return constants.JSON
731
+ return dataURLModuleType(url, attributes, opts)
732
+ }
762
733
 
763
- return defaultType
734
+ if (typeof attributes.type === 'string') {
735
+ return typeForAttribute(attributes.type)
764
736
  }
765
737
 
766
738
  const match = url.pathname.match(/\.[a-z]+$/)
@@ -799,6 +771,67 @@ function moduleType(url, attributes, info, opts = {}) {
799
771
  return defaultType
800
772
  }
801
773
 
774
+ function typeForAttribute(type) {
775
+ switch (type) {
776
+ case 'script':
777
+ return constants.SCRIPT
778
+ case 'module':
779
+ return constants.MODULE
780
+ case 'json':
781
+ return constants.JSON
782
+ case 'bundle':
783
+ return constants.BUNDLE
784
+ case 'addon':
785
+ return constants.ADDON
786
+ case 'binary':
787
+ return constants.BINARY
788
+ case 'text':
789
+ return constants.TEXT
790
+ }
791
+
792
+ return 0
793
+ }
794
+
795
+ function dataURLModuleType(url, attributes, opts = {}) {
796
+ const { defaultType = constants.SCRIPT, referrerType } = opts
797
+
798
+ const { mime } = parseDataURL(url)
799
+
800
+ const asserted = typeof attributes.type === 'string' ? typeForAttribute(attributes.type) : null
801
+
802
+ if (mime === null || mime.subtype === 'javascript') {
803
+ if (asserted === constants.SCRIPT || asserted === constants.MODULE) return asserted
804
+
805
+ if (asserted !== null) {
806
+ throw errors.TYPE_INCOMPATIBLE(`Module '${url.href}' is not of type '${attributes.type}'`)
807
+ }
808
+
809
+ if (referrerType) return referrerType
810
+
811
+ return defaultType === constants.MODULE ? constants.MODULE : constants.SCRIPT
812
+ }
813
+
814
+ let type
815
+
816
+ if (mime.subtype === 'json') {
817
+ type = constants.JSON
818
+ } else if (mime.type === 'text') {
819
+ type = constants.TEXT
820
+ } else if (mime.subtype === 'octet-stream') {
821
+ type = constants.BINARY
822
+ } else {
823
+ throw errors.TYPE_INCOMPATIBLE(
824
+ `Media type '${mime.type}/${mime.subtype}' of '${url.href}' is not supported`
825
+ )
826
+ }
827
+
828
+ if (asserted !== null && asserted !== type) {
829
+ throw errors.TYPE_INCOMPATIBLE(`Module '${url.href}' is not of type '${attributes.type}'`)
830
+ }
831
+
832
+ return type
833
+ }
834
+
802
835
  function parseDataURL(url) {
803
836
  const { pathname } = url
804
837
 
package/lib/errors.js CHANGED
@@ -1,11 +1,10 @@
1
1
  module.exports = class ModuleTraverseError extends Error {
2
2
  constructor(msg, fn = ModuleTraverseError, code = fn.name) {
3
3
  super(`${code}: ${msg}`)
4
+
4
5
  this.code = code
5
6
 
6
- if (Error.captureStackTrace) {
7
- Error.captureStackTrace(this, fn)
8
- }
7
+ if (Error.captureStackTrace) Error.captureStackTrace(this, fn)
9
8
  }
10
9
 
11
10
  get name() {
@@ -46,6 +45,10 @@ module.exports = class ModuleTraverseError extends Error {
46
45
  return new ModuleTraverseError(msg, ModuleTraverseError.INVALID_IMPORTS_MAP)
47
46
  }
48
47
 
48
+ static TYPE_INCOMPATIBLE(msg) {
49
+ return new ModuleTraverseError(msg, ModuleTraverseError.TYPE_INCOMPATIBLE)
50
+ }
51
+
49
52
  static UNKNOWN_DATA_URL_CHARSET(msg, charset) {
50
53
  const err = new ModuleTraverseError(msg, ModuleTraverseError.UNKNOWN_DATA_URL_CHARSET)
51
54
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module-traverse",
3
- "version": "2.4.3",
3
+ "version": "2.4.4",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {