bare-module-traverse 2.4.2 → 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 +76 -39
  2. package/lib/errors.js +6 -3
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -332,6 +332,8 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
332
332
 
333
333
  if (typeof imports !== 'object' || imports === null) return false
334
334
 
335
+ const type = moduleType(url, {}, null, opts)
336
+
335
337
  for (const [specifier, entry] of Object.entries(imports)) {
336
338
  const stack = [{ entry, asset: false }]
337
339
 
@@ -352,7 +354,8 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
352
354
  yield {
353
355
  children: exports.module(url, null, {}, artifacts, visited, {
354
356
  ...opts,
355
- asset: true
357
+ asset: true,
358
+ referrerType: type
356
359
  }),
357
360
  deferred: true
358
361
  }
@@ -362,7 +365,10 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
362
365
  url.protocol !== deferredProtocol
363
366
  ) {
364
367
  yield {
365
- children: exports.module(url, null, {}, artifacts, visited, opts),
368
+ children: exports.module(url, null, {}, artifacts, visited, {
369
+ ...opts,
370
+ referrerType: type
371
+ }),
366
372
  deferred: false
367
373
  }
368
374
  }
@@ -374,8 +380,6 @@ exports.preresolved = function* (url, source, resolutions, artifacts, visited, o
374
380
  }
375
381
  }
376
382
 
377
- const type = moduleType(url, {}, null, opts)
378
-
379
383
  const lexer = { imports: [], exports: [] }
380
384
 
381
385
  if (type === constants.SCRIPT || type === constants.MODULE) {
@@ -684,9 +688,8 @@ exports.patternMatches = function* patternMatches(pattern, parentURL, matches, o
684
688
  if (patternNegate) removeURL(matches, url)
685
689
  else addURL(matches, url)
686
690
  } else if (patternTrailer === '' || url.href.endsWith(patternTrailer)) {
687
- addURL(matches, url)
688
- } else if (patternNegate) {
689
- removeURL(matches, url)
691
+ if (patternNegate) removeURL(matches, url)
692
+ else addURL(matches, url)
690
693
  }
691
694
  }
692
695
  } else if (Array.isArray(pattern)) {
@@ -722,41 +725,14 @@ function* postresolve(url) {
722
725
  }
723
726
 
724
727
  function moduleType(url, attributes, info, opts = {}) {
725
- const { defaultType = constants.SCRIPT, referrerType, aliases = null } = opts
726
-
727
- if (typeof attributes.type === 'string') {
728
- switch (attributes.type) {
729
- case 'script':
730
- return constants.SCRIPT
731
- case 'module':
732
- return constants.MODULE
733
- case 'json':
734
- return constants.JSON
735
- case 'bundle':
736
- return constants.BUNDLE
737
- case 'addon':
738
- return constants.ADDON
739
- case 'binary':
740
- return constants.BINARY
741
- case 'text':
742
- return constants.TEXT
743
- }
744
-
745
- return 0
746
- }
728
+ const { defaultType = constants.SCRIPT, aliases = null } = opts
747
729
 
748
730
  if (url.protocol === 'data:') {
749
- const { mime } = parseDataURL(url)
750
-
751
- if (mime === null || mime.subtype === 'javascript') {
752
- if (referrerType) return referrerType
753
-
754
- return defaultType === constants.MODULE ? constants.MODULE : constants.SCRIPT
755
- }
756
-
757
- if (mime.subtype === 'json') return constants.JSON
731
+ return dataURLModuleType(url, attributes, opts)
732
+ }
758
733
 
759
- return defaultType
734
+ if (typeof attributes.type === 'string') {
735
+ return typeForAttribute(attributes.type)
760
736
  }
761
737
 
762
738
  const match = url.pathname.match(/\.[a-z]+$/)
@@ -795,6 +771,67 @@ function moduleType(url, attributes, info, opts = {}) {
795
771
  return defaultType
796
772
  }
797
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
+
798
835
  function parseDataURL(url) {
799
836
  const { pathname } = url
800
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.2",
3
+ "version": "2.4.4",
4
4
  "description": "Low-level module graph traversal for Bare",
5
5
  "exports": {
6
6
  ".": {