From 0de1dedd2ece832517cfcb01e12d1e2a439f68a2 Mon Sep 17 00:00:00 2001 From: Benny Joo Date: Thu, 21 Dec 2023 07:55:57 +0000 Subject: [PATCH 1/2] fix: INT-2334 ensured form-data has buffers when sending requests to CS --- package.json | 4 ++-- src/apis.ts | 14 ++++++++++++++ src/handlePublishCliCommand.ts | 14 +++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 33dd648..fe6357b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "intuita", - "version": "0.5.3", + "version": "0.6.0", "description": "A codemod engine for Node.js libraries (jscodeshift, ts-morph, etc.)", "type": "module", "exports": null, @@ -82,7 +82,7 @@ }, "packageManager": "pnpm@8.6.7", "scripts": { - "build": "esbuild ./src/index.ts --define:__INTUITA_CLI_VERSION__=\\\"0.5.3\\\" --bundle --platform=node --target=node16 --minify --format=cjs --legal-comments=inline --outfile=./dist/index.cjs", + "build": "esbuild ./src/index.ts --define:__INTUITA_CLI_VERSION__=\\\"0.6.0\\\" --bundle --platform=node --target=node16 --minify --format=cjs --legal-comments=inline --outfile=./dist/index.cjs", "lint:eslint": "eslint src --fix --ext ts", "lint:prettier": "prettier --write .", "package": "pkg --compress GZip .", diff --git a/src/apis.ts b/src/apis.ts index 719bba4..8a80475 100644 --- a/src/apis.ts +++ b/src/apis.ts @@ -1,5 +1,6 @@ import Axios from 'axios'; import { type Input, parse, object, string, nullable } from 'valibot'; +import type FormData from 'form-data'; const X_INTUITA_ACCESS_TOKEN = 'X-Intuita-Access-Token'.toLocaleLowerCase(); @@ -25,3 +26,16 @@ export const validateAccessToken = async ( return parse(dataSchema, response.data); }; + +export const publish = async ( + accessToken: string, + formData: FormData, +): Promise => { + await Axios.post('https://telemetry.intuita.io/publish', formData, { + headers: { + [X_INTUITA_ACCESS_TOKEN]: accessToken, + 'Content-Type': 'multipart/form-data', + }, + timeout: 10000, + }); +}; diff --git a/src/handlePublishCliCommand.ts b/src/handlePublishCliCommand.ts index 2f0bc3c..e8fc4c8 100644 --- a/src/handlePublishCliCommand.ts +++ b/src/handlePublishCliCommand.ts @@ -2,11 +2,10 @@ import * as fs from 'fs'; import type { PrinterBlueprint } from './printer.js'; import { homedir } from 'node:os'; import { join } from 'node:path'; -import { validateAccessToken } from './apis.js'; -import Axios from 'axios'; - import { object, string, parse } from 'valibot'; +import { publish, validateAccessToken } from './apis.js'; import { CodemodDownloader } from './downloadCodemod.js'; +import FormData from 'form-data'; const packageJsonSchema = object({ main: string(), @@ -81,17 +80,14 @@ export const handlePublishCliCommand = async ( } const formData = new FormData(); - formData.append('package.json', packageJsonData); - formData.append('index.cjs', indexCjsData); - formData.append('config.json', configJsonData); + formData.append('index.cjs', Buffer.from(indexCjsData)); + formData.append('config.json', Buffer.from(configJsonData)); if (descriptionMdData) { formData.append('description.md', descriptionMdData); } - await Axios.post('https://telemetry.intuita.io/publish', formData, { - timeout: 10000, - }); + await publish(token, formData); try { await codemodDownloader.download(pkg.name); From f85046df53665ed10cabdf39124c63c59aa01cc4 Mon Sep 17 00:00:00 2001 From: Greg Pabian <35925521+grzpab@users.noreply.github.com> Date: Thu, 21 Dec 2023 14:58:09 +0100 Subject: [PATCH 2/2] wip --- src/handlePublishCliCommand.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/handlePublishCliCommand.ts b/src/handlePublishCliCommand.ts index e8fc4c8..b13dc12 100644 --- a/src/handlePublishCliCommand.ts +++ b/src/handlePublishCliCommand.ts @@ -87,7 +87,21 @@ export const handlePublishCliCommand = async ( formData.append('description.md', descriptionMdData); } - await publish(token, formData); + try { + await publish(token, formData); + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + + printer.printConsoleMessage( + 'error', + `Could not publish the "${pkg.name}" package: ${message}`, + ); + } + + printer.printConsoleMessage( + 'info', + `Published the "${pkg.name}" package successfully`, + ); try { await codemodDownloader.download(pkg.name);