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

Issues with esbuild-script.mjs #201

Closed
tbigueres opened this issue Jun 20, 2024 · 3 comments
Closed

Issues with esbuild-script.mjs #201

tbigueres opened this issue Jun 20, 2024 · 3 comments

Comments

@tbigueres
Copy link

tbigueres commented Jun 20, 2024

Hello,

I have some issues as soon as I try to install and use aws package.
Indeed when I run the node --no-warnings esbuild-script.mjs (in npm run build).

npm run build

> [email protected] build
> npm run clean && npm run lint && tsc && node esbuild-script.mjs


> [email protected] clean
> rimraf dist


> [email protected] lint
> eslint src/

(node:79545) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
✘ [ERROR] Could not resolve "./auth/httpAuthSchemeProvider"

    node_modules/@aws-sdk/client-lambda/dist-cjs/index.js:223:44:
      223 │ var import_httpAuthSchemeProvider = require("./auth/httpAuthSchemeProvider");
          ╵                                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "././runtimeConfig"

    node_modules/@aws-sdk/client-lambda/dist-cjs/index.js:242:35:
      242 │ var import_runtimeConfig = require("././runtimeConfig");
          ╵                                    ~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./middleware-http-auth-scheme"

    node_modules/@smithy/core/dist-es/index.js:1:14:
      1 │ export * from "./middleware-http-auth-scheme";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./middleware-http-signing"

    node_modules/@smithy/core/dist-es/index.js:2:14:
      2 │ export * from "./middleware-http-signing";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./submodules/client/index"

    node_modules/@aws-sdk/core/dist-es/index.js:1:14:
      1 │ export * from "./submodules/client/index";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./util-identity-and-auth"

    node_modules/@smithy/core/dist-es/index.js:3:14:
      3 │ export * from "./util-identity-and-auth";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./adaptors/getEndpointFromConfig"

    node_modules/@smithy/middleware-endpoint/dist-cjs/index.js:101:43:
      101 │ var import_getEndpointFromConfig = require("./adaptors/getEndpointFromConfig");
          ╵                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./submodules/httpAuthSchemes/index"

    node_modules/@aws-sdk/core/dist-es/index.js:2:14:
      2 │ export * from "./submodules/httpAuthSchemes/index";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./getSmithyContext"

    node_modules/@smithy/core/dist-es/index.js:4:14:
      4 │ export * from "./getSmithyContext";
        ╵               ~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./submodules/protocols/index"

    node_modules/@aws-sdk/core/dist-es/index.js:3:14:
      3 │ export * from "./submodules/protocols/index";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./normalizeProvider"

    node_modules/@smithy/core/dist-es/index.js:5:14:
      5 │ export * from "./normalizeProvider";
        ╵               ~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./isStreamingPayload/isStreamingPayload"

    node_modules/@smithy/middleware-retry/dist-cjs/index.js:299:40:
      299 │ var import_isStreamingPayload = require("./isStreamingPayload/isStreamingPayload");
          ╵                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./protocols/requestBuilder"

    node_modules/@smithy/core/dist-es/index.js:6:14:
      6 │ export * from "./protocols/requestBuilder";
        ╵               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "./pagination/createPaginator"

    node_modules/@smithy/core/dist-es/index.js:7:32:
      7 │ export { createPaginator } from "./pagination/createPaginator";
        ╵                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "././getAwsChunkedEncodingStream"

    node_modules/@smithy/util-stream/dist-cjs/index.js:80:32:
      80 │ __reExport(src_exports, require("././getAwsChunkedEncodingStream"), module.exports);
         ╵                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "././sdk-stream-mixin"

    node_modules/@smithy/util-stream/dist-cjs/index.js:81:32:
      81 │ __reExport(src_exports, require("././sdk-stream-mixin"), module.exports);
         ╵                                 ~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "././fromBase64"

    node_modules/@smithy/util-base64/dist-cjs/index.js:19:32:
      19 │ __reExport(src_exports, require("././fromBase64"), module.exports);
         ╵                                 ~~~~~~~~~~~~~~~~

✘ [ERROR] Could not resolve "././toBase64"

    node_modules/@smithy/util-base64/dist-cjs/index.js:20:32:
      20 │ __reExport(src_exports, require("././toBase64"), module.exports);
         ╵                                 ~~~~~~~~~~~~~~

Here's my package.json

{
  "name": "medplum-demo-bots",
  "version": "3.1.5",
  "description": "Medplum Demo Bots",
  "license": "Apache-2.0",
  "author": "Medplum <[email protected]>",
  "type": "module",
  "scripts": {
    "build": "npm run clean && npm run lint && tsc && node --no-warnings esbuild-script.mjs",
    "clean": "rimraf dist",
    "lint": "eslint src/",
    "prettier": "prettier --write .",
    "test": "vitest run",
    "test:coverage": "vitest run --coverage",
    "test:ui": "vitest --ui"
  },
  "prettier": {
    "printWidth": 120,
    "singleQuote": true,
    "trailingComma": "es5"
  },
  "eslintConfig": {
    "parserOptions": {
      "project": "./tsconfig.json"
    },
    "extends": [
      "@medplum/eslint-config"
    ],
    "root": true
  },
  "devDependencies": {
    "@medplum/bot-layer": "3.1.8",
    "@medplum/cli": "3.1.8",
    "@medplum/core": "3.1.8",
    "@medplum/eslint-config": "3.1.8",
    "@medplum/fhirtypes": "3.1.8",
    "@medplum/mock": "3.1.8",
    "@types/node": "20.14.6",
    "@types/node-fetch": "2.6.11",
    "@types/ssh2-sftp-client": "9.0.3",
    "@vitest/coverage-v8": "1.6.0",
    "@vitest/ui": "1.6.0",
    "esbuild": "0.21.5",
    "form-data": "4.0.0",
    "glob": "^10.4.2",
    "node-fetch": "3.3.2",
    "pdfmake": "0.2.10",
    "rimraf": "5.0.7",
    "ssh2-sftp-client": "10.0.3",
    "stripe": "15.12.0",
    "typescript": "5.4.5",
    "vitest": "^1.6.0"
  },
  "dependencies": {
    "@aws-sdk/client-lambda": "^3.598.0",
    "@aws-sdk/client-s3": "^3.598.0",
    "@aws-sdk/credential-provider-http": "^3.598.0",
    "@aws-sdk/credential-provider-ini": "^3.598.0",
    "@aws-sdk/credential-provider-web-identity": "^3.598.0",
    "@aws-sdk/s3-request-presigner": "^3.598.0",
    "@smithy/middleware-endpoint": "^3.0.3",
    "@smithy/node-http-handler": "^3.1.0",
    "aws-sdk": "^2.1645.0"
  }
}

And here is a simple bot I've created that shows the issue

import { InvokeCommand, InvokeCommandInput, LambdaClient } from '@aws-sdk/client-lambda';
import { BotEvent, MedplumClient, parseReference } from '@medplum/core';
import { Device, DocumentReference, Reference } from '@medplum/fhirtypes';

export const CALIBRATION_SUCCESS = 'SUCCESS';
export const CALIBRATION_FAILED = 'FAILED';
export const CALIBRATION_ERROR = 'ERROR';

type CalibrationBotEvent = BotEvent & {
    input: {
        device: Reference<Device>;
        documentReference: Reference<DocumentReference>;
    };
};


/*
 * This bot is responsible for running the calibration process.
 * - Run the lambda that handles the calibration verification 
 * - Create an Observation based on the Lambda's return
 * https://apneal.atlassian.net/wiki/spaces/ADA/pages/476545035/Code+Calibration
 */
export async function handler(medplum: MedplumClient, event: CalibrationBotEvent): Promise<any> {

    const lambdaClient = new LambdaClient();
    const invokeParams:InvokeCommandInput = {
        FunctionName: 'arn:aws:lambda:eu-west-3:XXXXXX:function:Calibration',
        InvocationType: "RequestResponse",    
        Payload: JSON.stringify('s3:https://bucket/file.tar.gz'),
    };
    const response = await lambdaClient.send(new InvokeCommand(invokeParams));
    let observationValue;
    if(response.StatusCode !== 200){
        observationValue = CALIBRATION_ERROR;
    } else {
        const decoder  = new TextDecoder();
        const result = decoder.decode(response.Payload);
        console.log(result);
        if(result === "true") {
            observationValue = CALIBRATION_SUCCESS;
        } else {
            observationValue = CALIBRATION_FAILED;
        }   
    }   

    await medplum.createResource({
        resourceType: 'Observation',
        code: {
            coding: [
                {
                    system: 'https://fhir.apneal.ai/code-system',
                    code: 'calibration',
                    display: 'Calibration status',
                }
            ]
        },
        status: 'final',
        valueString: observationValue,
        subject: event.input.device,
        issued: new Date().toISOString(),
    });

    console.log(event.input.documentReference)
    const document = await medplum.readResource('DocumentReference', parseReference(event.input.documentReference)[1]);
    if(document === undefined){
        throw new Error('DocumentReference not found');
    }

    await medplum.updateResource({
        ...document,
        docStatus: 'final'
    });
    
    return response.Payload;

}

I you have any idea of what is going on here I'd be glad to have your input as it is blocking for me to deploy my bots !

@tbigueres
Copy link
Author

After digging a bit, I've managed to build the application by removing "bundle:true" and "external: botLayerDeps,"
This is a hack and gives me other issues.
For examples I have a bot that uses the package "@aws-sdk/s3-request-presigner": "^3.598.0",
When testing the bot in my bot repository, it works all fine and I can build the bot that is not an issue neither.
But after deploying the bot I have an issue as the package is not bundled with the bot.

So I have the error : package not found, which makes sense.
What I did for the moment in order to have the possibility to continue working is that I've added the package in the dependency of the medplum server and rerun the server.
This is hacky and definetely not what I hope for the long term, but it allows me to keep working for the moment !

If you have any idea how I can handle this I'd be more than happy !

@tbigueres
Copy link
Author

I've opened an issue on aws-sdk
aws/aws-sdk-js-v3#6223

@tbigueres
Copy link
Author

Easy fix :

resolveExtensions: ['.ts'],
to

// resolveExtensions: ['.ts'],
or

resolveExtensions: ['.ts', '.js'],

I guess this could be added to the esbuild ! for future compatibility :)

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