calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
714 stars 228 forks source link

Problem with instanceof check #183

Closed wirk closed 1 year ago

wirk commented 1 year ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch shpjs@4.0.3 for the project I'm working on.

I ran into an issue when switching from jest to vitest. After some debugging, I figured out that my ArrayBuffer seems to be created in a different context and does not pass the instanceof check. I have adopted the check from https://github.com/fengyuanchen/is-array-buffer/blob/main/index.ts and it works again for me.

Here is the diff that solved my problem:

diff --git a/node_modules/shpjs/lib/index.js b/node_modules/shpjs/lib/index.js
index 7f8cda8..64d1419 100644
--- a/node_modules/shpjs/lib/index.js
+++ b/node_modules/shpjs/lib/index.js
@@ -23,7 +23,7 @@ function toBuffer (b) {
   if (Buffer.isBuffer(b)) {
     return b;
   }
-  if (b instanceof global.ArrayBuffer) {
+  if (b instanceof global.ArrayBuffer || Object.prototype.toString.call(b) === '[object ArrayBuffer]') {
     return Buffer.from(b);
   }
   if (b.buffer instanceof global.ArrayBuffer) {

This issue body was partially generated by patch-package.

calvinmetcalf commented 1 year ago

do you want to open a pull request ?

wirk commented 1 year ago

Thanks for your reply, I just created a PR

calvinmetcalf commented 1 year ago

v4.0.4