konsumer / rawproto

Guess structure of protobuf binary from raw data
https://konsumer.js.org/rawproto/
35 stars 9 forks source link

SyntaxError: Cannot use import statement outside a module #10

Closed fashioncj closed 4 months ago

fashioncj commented 1 year ago

I find error both CLI and NPX in version 0.7.13

$ npx rawproto
/Users/x/.npm/_npx/05c2c072889f8a67/node_modules/rawproto/dist/rawproto.cjs:1
import e from"protobufjs";const{Reader:s}=e,t=e=>Array(e).join("  "),r=(e,s="MessageRoot",a=1)=>{const o=[],n=[],u=e.map(e=>{const u=Object.keys(e).pop();switch(Array.isArray(e[u])?"array":typeof e[u]){case"object":return`${t(a+1)}bytes field${u} = ${u}; // could be a repeated-value, string, bytes, or malformed sub-message`;case"string":return`${t(a+1)}string field${u} = ${u}; // could be a repeated-value, string, bytes, or malformed sub-message`;case"number":return(e=>Number(e)===e&&e%1!=0)(e[u])?`${t(a+1)}float field${u} = ${u}; // could be a fixed64, sfixed64, double, fixed32, sfixed32, or float`:`${t(a+1)}int32 field${u} = ${u}; // could be a int32, int64, uint32, bool, enum, etc, or even a float of some kind`;case"array":if(-1===o.indexOf(u))return o.push(u),`\n${r(e[u],s,a+1)}\n${t(a+1)}\n${t(a+1)}Message${u} subMessage${u} = ${u};`;n.push(u)}}).filter(e=>e),i=[];return n.forEach(e=>{u.forEach((s,t)=>{-1!==s.indexOf(`subMessage${e}`)&&-1===i.indexOf(e)&&(u[t]=s.replace(`Message${e} subMessage${e}`,`repeated Message${e} subMessage${e}`),i.push(e))})}),`${t(a)}message ${s} {\n${u.join("\n")}\n${t(a)}}`};function a(e,t,r="auto",o=""){const n=s.create(e),u=[];for(;n.pos<n.len;){const e=n.uint64(),s=7&e,i=o+(e>>>3).toString();switch(s){case 0:u.push({[i]:n.uint32()});break;case 1:u.push({[i]:n.fixed64()});break;case 2:const e=n.bytes();try{const s=a(e,t,r,o);u.push({[i]:s})}catch(s){if("binary"===r)u.push({[i]:e});else if("string"===r)u.push({[i]:e.toString()});else{let s=!1;e.forEach(e=>{e<32&&(s=!0)}),u.push(s?{[i]:e}:{[i]:e.toString()})}}break;case 5:u.push({[i]:n.float()});break;default:n.skipType(s)}}return t&&t.decode(e),u}function o(e,s,t="MessageRoot",o="auto"){const n=a(e,s,o);let u='syntax = "proto3";\n\n';return u+=r(n,t),u}export{a as getData,o as getProto};
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1094:15)
    at Module._compile (node:internal/modules/cjs/loader:1129:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)
    at Module.load (node:internal/modules/cjs/loader:1043:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1067:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/jairochen/.npm/_npx/05c2c072889f8a67/node_modules/rawproto/rawproto.cjs:3:18)
    at Module._compile (node:internal/modules/cjs/loader:1165:14)
konsumer commented 5 months ago

Hmm, there may be some issue with how I was packaging it (microbuindle config.) I am currently in the process of adding a ton of new features and re-packaging (see #12) so hopefully it will all be resolved after.

In the meantime, try using an older version: npm i rawproto@0.7.4

konsumer commented 4 months ago

I am going to close this, as I am deprecating currently published version. As I said, you can use old verison, or migrate to new stuff (which is nicer, in my opinion, but might take a bit of code-change to use.)