hyperledger / identus-edge-agent-sdk-ts

Apache License 2.0
23 stars 11 forks source link

esbuild bundling #189

Closed mixmix closed 5 months ago

mixmix commented 7 months ago

Is this a regression?

No

Description

We bundle our node code to keep size of installers down, and speed startup in our electron app. We found some recent change meant that we were seeing a fatal startup error associated with the (new?) anoncreds stuff?

Please provide the exception or error you saw

Error looks like this:

TypeError: getObject(...).require is not a function
    at module2.exports.__wbg_require_900d5c3984fe7703 (/tmp/.mount_Ahau_LehboFR/resources/app.asar/main.bundle.js:341529:35)
    at wasm://wasm/00a48e8a:wasm-function[471]:0x13e70e
    at wasm://wasm/00a48e8a:wasm-function[1296]:0x1dd1c8
    at wasm://wasm/00a48e8a:wasm-function[731]:0x16baff
    at wasm://wasm/00a48e8a:wasm-function[769]:0x16f6a2
    at wasm://wasm/00a48e8a:wasm-function[785]:0x171833
    at module2.exports.proverCreateLinkSecret (/tmp/.mount_Ahau_LehboFR/resources/app.asar/main.bundle.js:338970:14)
    at _AnoncredsLoader.createLinksecret (/tmp/.mount_Ahau_LehboFR/resources/app.asar/main.bundle.js:348742:26)
    at _Agent.start (/tmp/.mount_Ahau_LehboFR/resources/app.asar/main.bundle.js:351206:52)

Please provide the environment you discovered this bug in

- Production
- electron
  - node v18

Anything else?

Here's the esbuild setup we're running:

import * as esbuild from 'esbuild'
import dirnameFix from 'esbuild-plugin-fileloc'
import fs from 'node:fs'

const result = await esbuild.build({
  entryPoints: ['main.js'],
  bundle: true,
  platform: 'node',
  target: 'node18.6.1',
  external: [
    'electron' // shouldn't bundle
  ],
  plugins: [
    dirnameFix.filelocPlugin()
    // fixes __dirname refs used by node-gyp-build
    // see also: https://github.com/evanw/esbuild/issues/859

    // NOTE this plugin has been patched (see /patches/esbuild-plugin-fileloc.diff)
    // It was installing absolute paths (encoding the directories of the building computer!)
  ],
  outfile: 'main.bundle.js',
  metafile: true
})

fs.writeFileSync('meta.json', JSON.stringify(result.metafile))
// check it out: https://esbuild.github.io/analyze/
mixmix commented 7 months ago

:heavy_check_mark: HACK that fixes this. I :heart: patch-package

diff --git a/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-00869b44.js b/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-00869b44.js
index 5d3724e..b81a4a2 100644
--- a/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-00869b44.js
+++ b/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-00869b44.js
@@ -2817,7 +2817,12 @@ module.exports.__wbg_static_accessor_MODULE_ef3aa2eb251158a5 = function() {
     return addHeapObject(ret);
 };
 module.exports.__wbg_require_900d5c3984fe7703 = function(arg0, arg1, arg2) {
-    const ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));
+    const val = getStringFromWasm0(arg1, arg2);
+    if (val === 'crypto') {
+      return addHeapObject(require('crypto'));
+    }
+
+    const ret = getObject(arg0).require(val);
     return addHeapObject(ret);
 };
 module.exports.__wbg_getRandomValues_307049345d0bd88c = function(arg0) {
diff --git a/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-4c3caa55.js b/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-4c3caa55.js
index c79edd0..45dcf5a 100644
--- a/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-4c3caa55.js
+++ b/node_modules/@atala/prism-wallet-sdk/build/node/anoncreds-4c3caa55.js
@@ -2815,7 +2815,12 @@ module.exports.__wbg_static_accessor_MODULE_ef3aa2eb251158a5 = function() {
     return addHeapObject(ret);
 };
 module.exports.__wbg_require_900d5c3984fe7703 = function(arg0, arg1, arg2) {
-    const ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));
+    const val = getStringFromWasm0(arg1, arg2);
+    if (val === 'crypto') {
+      return addHeapObject(require('crypto'));
+    }
+
+    const ret = getObject(arg0).require(val);
     return addHeapObject(ret);
 };
 module.exports.__wbg_getRandomValues_307049345d0bd88c = function(arg0) {
diff --git a/node_modules/@atala/prism-wallet-sdk/build/node/index.cjs b/node_modules/@atala/prism-wallet-sdk/build/node/index.cjs
index 8c11f81..06f35d5 100644
--- a/node_modules/@atala/prism-wallet-sdk/build/node/index.cjs
+++ b/node_modules/@atala/prism-wallet-sdk/build/node/index.cjs
@@ -6608,7 +6608,7 @@ class AnoncredsLoader {
     async load() {
         /*START.NODE_ONLY*/
         if (!this.loaded) {
-            this.pkg = await Promise.resolve().then(function () { return require('./anoncreds-00869b44.js'); });
+            this.pkg = require('./anoncreds-00869b44.js');
             this.loaded = true;
         }
         /*END.NODE_ONLY*/
@@ -6621,6 +6621,7 @@ class AnoncredsLoader {
     }
     createLinksecret() {
         return this.wasm.proverCreateLinkSecret();
+
     }
     createCredentialRequest(credentialOffer, credentialDefinition, linkSecret, linkSecretId) {
         const result = this.wasm.proverCreateCredentialRequest(credentialOffer, credentialDefinition, linkSecret, linkSecretId);
diff --git a/node_modules/@atala/prism-wallet-sdk/index.cjs b/node_modules/@atala/prism-wallet-sdk/index.cjs
index 9bfb79c..4875fb1 100644
--- a/node_modules/@atala/prism-wallet-sdk/index.cjs
+++ b/node_modules/@atala/prism-wallet-sdk/index.cjs
@@ -1,5 +1 @@
-module.exports =
-    (typeof window !== 'undefined') ?
-        require('./build/browser/index.cjs')
-        :
-        require('./build/node/index.cjs')
+module.exports = require('./build/node/index.cjs')

NOTES:

  1. there are 2 anoncreds files? Why? don't know
  2. I mutated the index.cjs because I don't want the browser bundle in there aye!
  3. in electron-builder config, I ensure all the needed files are there (and no more!):
  files: [
    // NOTE that we have to include ! (not) rules in files, otherwise
    // electron-builder auto-adds **/*  (add everything!)
    '!build',
    '!node_modules',    // <<<<< THIS
    '!*.env*',

    /* main process */
    'main.bundle.js',

    // native module bindings for main process
    'node_modules/node-gyp-build/*.js',

    'node_modules/sodium-native/index.js',
    'node_modules/sodium-native/prebuilds/${platform}-${arch}/*',

    'node_modules/leveldown/*.js',
    'node_modules/leveldown/prebuilds/${platform}-${arch}/*',

    'node_modules/utp-native/index.js',
    'node_modules/utp-native/prebuilds/${platform}-${arch}/*',

    'node_modules/@atala/prism-wallet-sdk/build/node-wasm/*.wasm',  // <<<<<< THIS

    /* UI files (referenced by main.bundle.js) */
    'dist',
    '!dist/installers',
  ],
mkbreuningIOHK commented 6 months ago

@mixmix , is this still an issue to follow? Was there a PR that fixed it? Please link to it if any. cc @elribonazo

elribonazo commented 5 months ago

This has been fixed in v5.2.0, closing the issue.