Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[request error] this[D].init is not a function at new Sign ? Nuxt3 Nitro Server engine #217

Open
dosstx opened this issue May 4, 2022 · 0 comments

Comments

@dosstx
Copy link

dosstx commented May 4, 2022

When trying to generate headers using node-rsa, I get the following error:

[nuxt] [request error] this[D].init is not a function at new Sign (https://nuxt-starter-jzgdht.w.staticblitz.com/blitz.331c80ffab288536319518a60349d00207075dad.js:6:1135208) at Object.createSign (https://nuxt-starter-jzgdht.w.staticblitz.com/blitz.331c80ffab288536319518a60349d00207075dad.js:6:808197) at Scheme.sign (./node_modules/node-rsa/src/schemes/pkcs1.js:152:32)
at RSAKey.module.exports.Key.RSAKey.sign (./node_modules/node-rsa/src/libs/rsa.js:264:40) at NodeRSA.module.exports.NodeRSA.sign (./node_modules/node-rsa/src/NodeRSA.js:318:32) at generateWalmartHeaders (./.nuxt/dev/index.mjs:446:28) at eval (./.nuxt/dev/index.mjs:458:14) at eval (./node_modules/h3/dist/index.mjs:364:14) at Object.eval [as handler] (./node_modules/h3/dist/index.mjs:564:12) at eval (./node_modules/h3/dist/index.mjs:475:31) [nuxt] [request error] this[D].init is not a function at new Sign (https://nuxt-starter-jzgdht.w.staticblitz.com/blitz.331c80ffab288536319518a60349d00207075dad.js:6:1135208) at Object.createSign (https://nuxt-starter-jzgdht.w.staticblitz.com/blitz.331c80ffab288536319518a60349d00207075dad.js:6:808197) at Scheme.sign (./node_modules/node-rsa/src/schemes/pkcs1.js:152:32)
at RSAKey.module.exports.Key.RSAKey.sign (./node_modules/node-rsa/src/libs/rsa.js:264:40) at NodeRSA.module.exports.NodeRSA.sign (./node_modules/node-rsa/src/NodeRSA.js:318:32) at generateWalmartHeaders (./.nuxt/dev/index.mjs:446:28) at eval (./.nuxt/dev/index.mjs:458:14) at eval (./node_modules/h3/dist/index.mjs:364:14) at Object.eval [as handler] (./node_modules/h3/dist/index.mjs:564:12) at eval (./node_modules/h3/dist/index.mjs:475:31)

My code implemention is:

import NodeRSA from 'node-rsa';

const keyData = {
  consumerId: '<consumer id removed for this post>',
  privateKey: `-----BEGIN RSA PRIVATE KEY-----
  MIIEowIBAAKCAQEAiDffvTlnBcHfDhPjYlJhSk+atPFE6HpFaf4mm/aYBXmOn89A
  MMulkkmpu+RGj0SaPrpX/ockoSeMuuEPPd3AQ5uZAnJw9TBnE2/kJrPsHaKyVFGV
  hZneksIK/KdP/kpGSuVAkocPdit5zKwliMnc6/GVLpDFvU8K2QPnvYu2Jp8XBDwg
  gTu2tzHQkrm0cdCYZklKKqN9NdVLJy+6AL50+vX19nHwDdCYCvnPtH/hXxMwRnIJ
  3qibO8owPWh+q/xMld2K2OoUNWpbsxBj/r9Jxu60d429+XcIUu1hyPHG1lDKiK/n
  Z4L+7WE4Ez8sEVD0YrE9hRRbLl5Dsvi4XZrG9QIDAQABAoIBAQCFNN5+1JuCbcwK
  oDhD9fteB+pp92ZDUQ6AUCDBc6vF7tEiRjGnhf4ryA1LyDeX8qZDoUZbiRyw21Qr
  i9qzzR4u/wHp+q+rleG2iDy7/EZx1KA/BGkLdSTKrya/W35GVavXocg7gggErw80
  r0MlOQHpWR1hpAE61wjsn30HRpChwow1YZE/6cMIQN2nCJ+JGXuZDoGazYk4HwFD
  Fmrtag/FjShYUVgr4QlrPzYzcTCKX1UKQkZ9AED9Q2prKSvoD8ZxOJSaVF0FP1ty
  /i9L1I/eJEusSBPXc9v5xJjs7q8RRKZhn3TuvBHos3LDuaQKwPBe+w0vHr8ZVw5t
  tuZPiq8BAoGBAOYILkVnnrskg2qzqewHukiKnZHLi+WkJQPQzTHvVMMCEUaxF+6p
  Y58tLlaEd0uNH1ntDPya5s2y01/1eZ/8n1U1/SlqKoQ5apkC77eNBklMxixJ0xGP
  zu1bj5COba6pXdY+YcW2z7W0ubmPD3YCSD/VUD/IKXwTEAHm2J11ffM9AoGBAJeY
  hRbiSQW6GXWHu41qqIYsWfdJpy/A0qhpXjmySq1XFH/ThHDuFmn5RkZVvn7D/Pql
  GfO8E55QWjK+SLO7LBRazKP0GNmrKinVMKyo7WUgwrZy3fwEY5wcNaNFB/YL9J4M
  OSRp6eV3pnUwQI2NhzTzuHAyJgd/r+I6zMeSTn4ZAoGADGhejpHTRwbmK8g7Hycf
  jjAj5axUBHQBJx6JIutk6AvhgK2mu9HZNMnMGRCWGrYm/cPCkpGMZ4YAzsk/4ThQ
  I9mAqU43suAh9tTotz7dGvEQM21b/DOEltr8eHCmS+iIzjiZL3/33jY8Wlz0GYpv
  +Tl5VadnTXD9yQx5nKysuYUCgYA6PNy8Kth0u8a2ERvrOxNc4EL7ri7tOH11N218
  atMnfnGgnciefcjck2f880nId1CDldO/f/xlcGcGYXWanohTlYJSZh752DjNc1pM
  qmTw2cITx1MiUylVOr0caROi4XrrELUPGSVDA1FOaegSuVE89XhgmdNkRBh0p7Qt
  4zYGWQKBgFGuZgbPl76K3fj5POY5OlPusXdCCqJMHKPug+4e5mG82T1KvLFCj1PQ
  Fm0y9PoS3A8SXf+aEeLhRqVrpaU4w5RK8PqeF6IB2hpDrHJ8b+3ERU3J7/KuU8Vw
  pmwDIbjWypH4dNJgRMti+RKDb9llup6xP5Q4PQRzvUQdklSCp3D8
  -----END RSA PRIVATE KEY-----`,
  keyVer: 1,
};

const generateWalmartHeaders = () => {
  const { privateKey, consumerId, keyVer } = keyData;
  const hashList = {
    'WM_CONSUMER.ID': consumerId,
    'WM_CONSUMER.INTIMESTAMP': Date.now().toString(),
    'WM_SEC.KEY_VERSION': keyVer,
  };

  const sortedHashString = `${hashList['WM_CONSUMER.ID']}\n${hashList['WM_CONSUMER.INTIMESTAMP']}\n${hashList['WM_SEC.KEY_VERSION']}\n`;
  const signer = new NodeRSA(privateKey, 'pkcs1');
  const signature = signer.sign(sortedHashString);
  const signatureEnc = signature.toString('base64');

  return {
    'WM_SEC.AUTH_SIGNATURE': signatureEnc,
    'WM_CONSUMER.INTIMESTAMP': hashList['WM_CONSUMER.INTIMESTAMP'],
    'WM_CONSUMER.ID': hashList['WM_CONSUMER.ID'],
    'WM_SEC.KEY_VERSION': hashList['WM_SEC.KEY_VERSION'],
  };
};

export default defineEventHandler(() => {
  const options = {
    method: 'GET',
    headers: generateWalmartHeaders(), // <--- commenting this out will cause no errors
  };

  return {
    api: 'works',
    options: options,
  };
});

Anyone have any idea how I can fix this?
Stackoverflow post: https://stackoverflow.com/questions/72111954/error-exporting-a-function-when-using-nuxt3-nitro-engine

@dosstx dosstx changed the title [request error] this[D].init is not a function at new Sign ? [request error] this[D].init is not a function at new Sign ? Nuxt3 Nitro Server engine May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant