shadow-cljs icon indicating copy to clipboard operation
shadow-cljs copied to clipboard

AWS amplify-js: AWSPinpointProvider - updateEndpoint failed TypeError: crypto_1.createHash is not a function

Open EduardoMRB opened this issue 1 year ago • 3 comments

I'm running into a problem when trying to use the Analytics module of amplify-js. The following error is thrown when an attempt to track the session is made:

ConsoleLogger.js:142 [ERROR] 09:34.510 AWSPinpointProvider - updateEndpoint failed TypeError: crypto_1.createHash is not a function
    at new Hash (index.js:10:106)
    at eval (getPayloadHash.js:21:26)
    at Generator.next (<anonymous>)
    at main.js:24:227
    at new Promise (<anonymous>)
    at $jscomp.asyncExecutePromiseGenerator (main.js:24:120)
    at $jscomp.asyncExecutePromiseGeneratorFunction (main.js:24:310)
    at exports.getPayloadHash (getPayloadHash.js:11:67)
    at eval (SignatureV4.js:94:52)
    at Generator.next (<anonymous>)

/js/cljs-runtime/node_modules/@aws-sdk/hash-node/dist/cjs/index.js

shadow$provide["module$node_modules$$aws_sdk$hash_node$dist$cjs$index"] = function(global,require,module,exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Hash = void 0;
const util_buffer_from_1 = require("@aws-sdk/util-buffer-from");
const buffer_1 = require("buffer");
const crypto_1 = require("crypto");
class Hash {
    constructor(algorithmIdentifier, secret) {
        this.hash = secret ? crypto_1.createHmac(algorithmIdentifier, castSourceData(secret)) : crypto_1.createHash(algorithmIdentifier);
    }
    update(toHash, encoding) {
        this.hash.update(castSourceData(toHash, encoding));
    }
    digest() {
        return Promise.resolve(this.hash.digest());
    }
}
exports.Hash = Hash;
function castSourceData(toCast, encoding) {
    if (buffer_1.Buffer.isBuffer(toCast)) {
        return toCast;
    }
    if (typeof toCast === "string") {
        return util_buffer_from_1.fromString(toCast, encoding);
    }
    if (ArrayBuffer.isView(toCast)) {
        return util_buffer_from_1.fromArrayBuffer(toCast.buffer, toCast.byteOffset, toCast.byteLength);
    }
    return util_buffer_from_1.fromArrayBuffer(toCast);
}
};

repro: https://github.com/EduardoMRB/amplify-shadow-cljs (unfortunately a Cognito user pool and Amazon Pinpoint application is needd to reproduce the issue) node version: v18.7.0

EduardoMRB avatar Aug 08 '22 17:08 EduardoMRB

This seems to be including some code meant for node? @aws-sdk/hash-node probably isn't meant to be used in a browser environment?

I don't know any of these packages. Are you sure they are meant to be browser compatible?

thheller avatar Aug 08 '22 18:08 thheller

I don't even know where to begin debugging this mess of a package that is aws-amplify. You are probably better off using webpack as described here.

thheller avatar Aug 08 '22 18:08 thheller

I think some cli code is contained in the package as well. Thanks for the tip, I'l give webpack a try.

EduardoMRB avatar Aug 08 '22 18:08 EduardoMRB