-
Hello, Whenever I try to use the What is it that I am doing wrong here ? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 4 replies
-
Can you please share your actual code including import statements? Perhaps erase your |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer, here is the code I'm using : import {
S3Client,
PutObjectCommand,
PutObjectCommandInput
} from "@aws-sdk/client-s3";
import { env } from "process";
export class CdnManager {
readonly cdnBaseUrl: string = env.S3_ENDPOINT;
readonly cdbBucketName: string = env.S3_BUCKET_NAME;
private getS3Client() : S3Client {
return new S3Client({region:"auto", endpoint:this.cdnBaseUrl, apiVersion: '2006-03-01', credentials: {accessKeyId:env.S3_ACCESS_KEY_ID, secretAccessKey:env.S3_SECRET_ACCESS_KEY} });
}
async upload(destinationFilePath: string, body: string, contentType: string = "text/plain; charset=utf-8") {
if (destinationFilePath.startsWith("/")) {
destinationFilePath = destinationFilePath.substring(1);
}
console.log(`cdn upload to`,destinationFilePath);
const uploadParams:PutObjectCommandInput = {
Bucket: this.cdbBucketName,
Key: destinationFilePath,
Body: Buffer.from(body),
ContentType: contentType
};
try {
const s3 = this.getS3Client();
const command = new PutObjectCommand(uploadParams);
const result = await s3.send(command);
console.log("Upload Success", result.$metadata);
} catch (error) {
console.log("Upload : Error was received for file at ", destinationFilePath, error);
}
}
} This class is within an internal library with this {
"name": "learning-app-data",
"version": "1.0.0",
"description": "Data used in the learning app project",
"main": "src/index.ts",
"scripts": {
"test": "node --inspect-brk node_modules/.bin/jest --runInBand"
},
"author": "Maxime Britto",
"license": "ISC",
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/node": "^18.7.13",
"jest": "^28.1.0",
"ts-jest": "^28.0.2"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.170.0",
"@directus/sdk": "^9.12.1",
"teachable-api": "file:/Users/mbritto/code/node_projects/teachable-api"
}
} This library is the only dependency of a Directus extension with this package : {
"name": "directus-extension-elements-hooks",
"version": "1.0.0",
"keywords": [
"directus",
"directus-extension",
"directus-custom-hook"
],
"directus:extension": {
"type": "hook",
"path": "dist/index.js",
"source": "src/index.ts",
"host": "^9.12.1"
},
"scripts": {
"build": "directus-extension build",
"dev": "directus-extension build -w --no-minify"
},
"dependencies": {
"learning-app-data": "file:../learning-app-data"
},
"devDependencies": {
"@directus/extensions-sdk": "9.12.1",
"@types/node": "^17.0.41",
"typescript": "^4.7.3"
}
} Which itself is used by the Directus server : {
"name": "teachable-app-webservice",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "npx nodemon --legacy-watch --exec NODE_OPTIONS=--inspect directus start",
"start": "npx directus start",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@directus/sdk": "^9.0.0",
"directus": "^9.0.1",
"firebase-admin": "^10.0.2",
"mysql": "^2.18.1",
"sharp": "^0.30.4"
}
} I've tried your recommandation and removed the After 3 hours of research here is the summary of my findings :
That is why I posted here, I could not find any more information on how to fix this issue. Thanks for your help! |
Beta Was this translation helpful? Give feedback.
-
@maxbritto - thanks for your succinct explanation. I was running into the same situation using vite and was able to work around this issue with a combination of rollup settings and a small polyfill borrowing some basic logic from the react-native-get-random-values polyfill. Changes to vite.config.ts: import { nodeResolve } from '@rollup/plugin-node-resolve'
export default defineConfig({
build: {
// ...
rollupOptions: {
output: {
globals: {crypto: 'crypto'},
},
external: ['crypto'],
plugins: [nodeResolve({ preferBuiltins: true })],
},
},
}) My understanding is that these settings will direct the build/bundle to use the nodejs crypto module instead of some polyfill which likely either shims or does not fully implement crypto. In my application code, I first import this polyfill so it's run before any transitive deps are imported: import './polyfills/crypto'
// rest of application code The polyfill: import crypto from 'crypto' // should have webcrypto.getRandomValues defined
if (typeof global.crypto !== 'object') {
global.crypto = crypto
}
if (typeof global.crypto.getRandomValues !== 'function') {
global.crypto.getRandomValues = getRandomValues
}
function getRandomValues(array) {
return crypto.webcrypto.getRandomValues(array)
} Hope this helps to diagnose and fix in your own project! |
Beta Was this translation helpful? Give feedback.
-
@au-z solution worked for me, at directus operator extension. Hope AWS will fix it in a better manner soon. |
Beta Was this translation helpful? Give feedback.
-
if you are using rollup, this may caused by a resolving issue. update the plugin config in your {
// ...other configs
plugins: [
// ...other plugins
nodeResolve({ exportConditions: ['node'] })
]
} which worked for me. see uuidjs/uuid#544 (comment) |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
-
What would you do in the instance where you're not using rollup? |
Beta Was this translation helpful? Give feedback.
-
I found an answer from a similar problem that another library had: jsdom/jsdom#1612 Basically add this so the dependency can find crypto:
|
Beta Was this translation helpful? Give feedback.
-
I was able to get passed this issue by installing 'react-native-get-random-values' and importing and then rebuilding it to install npm install react-native-get-random-values --save adding the import line in the file that's calling the aws import 'react-native-get-random-values'; |
Beta Was this translation helpful? Give feedback.
@maxbritto - thanks for your succinct explanation. I was running into the same situation using vite and was able to work around this issue with a combination of rollup settings and a small polyfill borrowing some basic logic from the react-native-get-random-values polyfill.
Changes to vite.config.ts:
My understanding is that these settings will direct the build/bundle to use the nodejs crypto module i…