skypackjs / skypack-cdn

An issue tracker for the CDN
107 stars 5 forks source link

[bson] Skypack fails to resolve `Buffer` type declarations #237

Open BastiDood opened 2 years ago

BastiDood commented 2 years ago

The Problem

I believe the following output from deno info should be sufficient in explaining the problem:

$ deno info https://cdn.skypack.dev/bson@4.5.4?dts
Download https://cdn.skypack.dev/bson@4.5.4?dts
Download https://cdn.skypack.dev/-/bson@v4.5.4-NmUFQh2CHRJ7WgSMuCi0/dist=es2019,mode=imports/optimized/bson.js
Download https://cdn.skypack.dev/-/bson@v4.5.4-NmUFQh2CHRJ7WgSMuCi0/dist=es2019,mode=types/bson.d.ts
Download https://cdn.skypack.dev/error/buffer?from=bson

Shortly after this, Deno aborts type-checking. From the snippet above, we can see that the associated X-TypeScript-Types header leads Deno to attempt importing from /error/buffer?from=bson. Notably, this URL is the user-facing error that Skypack returns when it "detects a problem with a package ahead-of-time". The error reports that there is a "malformed URL", but I'm not exactly sure why (and where) this is the case.

Some Considerations

We know that bson depends on buffer. The buffer package is supposed to be a browser-compatible polyfill for Node.js' Buffer API. It is important to note that both bson and buffer provide appropriate type declarations. Indeed, the Skypack registry detects these declarations—at least according to the check mark (✔) found beside the "TypeScript Types" section of the package scores.

However, the problem occurs when we import bson?dts on its own. Strangely, the X-TypeScript-Types redirects to valid type declarations except for the first line:

// File: bson.d.ts
import { Buffer } from '/error/buffer?from=bson';

// --snip--
// Rest of bson's type declarations here...

Here, we see that the Skypack's bson type declarations fail to resolve to that of buffer?dts. This is unexpected because we know for certain that importing buffer?dts yields no errors. To prove that this is indeed the case, note the following output from deno info:

$ deno info https://cdn.skypack.dev/buffer@6.0.3?dts
Download https://cdn.skypack.dev/buffer?dts
Download https://cdn.skypack.dev/-/buffer@v6.0.3-9TXtXoOPyENPVOx2wqZk/dist=es2019,mode=imports/optimized/buffer.js
Download https://cdn.skypack.dev/-/buffer@v6.0.3-9TXtXoOPyENPVOx2wqZk/dist=es2019,mode=types/index.d.ts
Download https://cdn.skypack.dev/-/base64-js@v1.5.1-W9sqY0mF5INkRg7HAvxn/dist=es2019,mode=imports/optimized/base64-js.js
Download https://cdn.skypack.dev/-/ieee754@v1.2.1-wxdRuKvQQOTpW1dpWzFI/dist=es2019,mode=imports/optimized/ieee754.js

Thus, there are no issues when resolving buffer?dts on its own. The error only occurs when importing buffer?dts via bson?dts. Could it be the case that Skypack mistakenly polyfills Buffer instead of resolving to the buffer package?

Additional Context

This issue comes as a result of denodrivers/deno_mongo#290. It was found that CI was broken due to Deno failing to resolve the /error/buffer?from=bson import.