asn1.js icon indicating copy to clipboard operation
asn1.js copied to clipboard

Hidden cyclical dependency in base

Open Vanuan opened this issue 7 years ago • 3 comments
trafficstars

There's this base file which contains a hidden cyclical dependency. The problem is buffer depends on base while base depends on buffer.

https://github.com/indutny/asn1.js/blob/bbf14e0732b7d8a5addedb800c91da7bc2a17e83/lib/asn1/base/index.js#L1-L8

https://github.com/indutny/asn1.js/blob/master/lib/asn1/base/buffer.js#L4 It works in node (and probably in webpack), but it causes problems when deployed using https://unpkg.com

Vanuan avatar Dec 26 '17 20:12 Vanuan

To resolve this dependency cycle, inner modules should depend on inner modules directly, without index.js as intermediate:

// index.js
const buffer = require('./buffer');
const reporter = require('./reporter');
const Node = require('./node');

module.exports = {
  Reporter: reporter.Reporter,
  DecodeBuffer: buffer.DecodeBuffer,
  EncoderBuffer: buffer.EncoderBuffer,
  Node,
};
// buffer.js
const Reporter = require('./reporter').Reporter;

Are there any reasons this wasn't done this way?

Vanuan avatar Dec 26 '17 20:12 Vanuan

I agree, this cause cycle requires in some environments which could potentially be error-prone

balthazar avatar Oct 30 '18 15:10 balthazar

Rollup is another example environment where this breaks.

lordvlad avatar Nov 10 '18 21:11 lordvlad