From 96b3c4f8b5a9bc5be0cc366ce5b0db8932c4bdf1 Mon Sep 17 00:00:00 2001 From: "oscargonzalezmoreno@gmail.com" Date: Thu, 26 Dec 2024 18:31:57 +0100 Subject: [PATCH] fix base64 issues --- package-lock.json | 125 +++++++++++++++++++++++++- src/rices/rices.service.ts | 37 +++++++- test/restclient/01_create_rice.http | 7 +- test/restclient/02_download_rice.http | 2 +- test/restclient/03_update_rice.http | 11 ++- test/restclient/04_delete_rice.http | 4 +- 6 files changed, 167 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3429435..1740457 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,8 +9,6 @@ "version": "0.0.1", "license": "UNLICENSED", "dependencies": { - "@nestjs/common": "^10.0.0", - "@nestjs/core": "^10.0.0", "@nestjs/platform-express": "^10.0.0", "@nestjs/swagger": "^8.1.0", "@octokit/rest": "^18.12.0", @@ -26,10 +24,13 @@ }, "devDependencies": { "@nestjs/cli": "^10.0.0", + "@nestjs/common": "^10.4.15", "@nestjs/config": "^3.3.0", + "@nestjs/core": "^10.4.15", "@nestjs/schematics": "^10.0.0", "@nestjs/testing": "^10.0.0", "@nestjs/throttler": "^6.3.0", + "@supabase/supabase-js": "^2.47.10", "@types/express": "^5.0.0", "@types/jest": "^29.5.14", "@types/node": "^20.3.1", @@ -2120,6 +2121,87 @@ "@sinonjs/commons": "^3.0.0" } }, + "node_modules/@supabase/auth-js": { + "version": "2.67.3", + "resolved": "https://registry.npmjs.org/@supabase/auth-js/-/auth-js-2.67.3.tgz", + "integrity": "sha512-NJDaW8yXs49xMvWVOkSIr8j46jf+tYHV0wHhrwOaLLMZSFO4g6kKAf+MfzQ2RaD06OCUkUHIzctLAxjTgEVpzw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/functions-js": { + "version": "2.4.4", + "resolved": "https://registry.npmjs.org/@supabase/functions-js/-/functions-js-2.4.4.tgz", + "integrity": "sha512-WL2p6r4AXNGwop7iwvul2BvOtuJ1YQy8EbOd0dhG1oN1q8el/BIRSFCFnWAMM/vJJlHWLi4ad22sKbKr9mvjoA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/node-fetch": { + "version": "2.6.15", + "resolved": "https://registry.npmjs.org/@supabase/node-fetch/-/node-fetch-2.6.15.tgz", + "integrity": "sha512-1ibVeYUacxWYi9i0cf5efil6adJ9WRyZBLivgjs+AUpewx1F3xPi7gLgaASI2SmIQxPoCEjAsLAzKPgMJVgOUQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + } + }, + "node_modules/@supabase/postgrest-js": { + "version": "1.17.7", + "resolved": "https://registry.npmjs.org/@supabase/postgrest-js/-/postgrest-js-1.17.7.tgz", + "integrity": "sha512-aOzOYaTADm/dVTNksyqv9KsbhVa1gHz1Hoxb2ZEF2Ed9H7qlWOfptECQWmkEmrrFjtNaiPrgiSaPECvzI/seDA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/realtime-js": { + "version": "2.11.2", + "resolved": "https://registry.npmjs.org/@supabase/realtime-js/-/realtime-js-2.11.2.tgz", + "integrity": "sha512-u/XeuL2Y0QEhXSoIPZZwR6wMXgB+RQbJzG9VErA3VghVt7uRfSVsjeqd7m5GhX3JR6dM/WRmLbVR8URpDWG4+w==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14", + "@types/phoenix": "^1.5.4", + "@types/ws": "^8.5.10", + "ws": "^8.18.0" + } + }, + "node_modules/@supabase/storage-js": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/@supabase/storage-js/-/storage-js-2.7.1.tgz", + "integrity": "sha512-asYHcyDR1fKqrMpytAS1zjyEfvxuOIp1CIXX7ji4lHHcJKqyk+sLl/Vxgm4sN6u8zvuUtae9e4kDxQP2qrwWBA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/node-fetch": "^2.6.14" + } + }, + "node_modules/@supabase/supabase-js": { + "version": "2.47.10", + "resolved": "https://registry.npmjs.org/@supabase/supabase-js/-/supabase-js-2.47.10.tgz", + "integrity": "sha512-vJfPF820Ho5WILYHfKiBykDQ1SB9odTHrRZ0JxHfuLMC8GRvv21YLkUZQK7/rSVCkLvD6/ZwMWaOAfdUd//guw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@supabase/auth-js": "2.67.3", + "@supabase/functions-js": "2.4.4", + "@supabase/node-fetch": "2.6.15", + "@supabase/postgrest-js": "1.17.7", + "@supabase/realtime-js": "2.11.2", + "@supabase/storage-js": "2.7.1" + } + }, "node_modules/@tsconfig/node10": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.11.tgz", @@ -2364,6 +2446,13 @@ "undici-types": "~6.19.2" } }, + "node_modules/@types/phoenix": { + "version": "1.6.6", + "resolved": "https://registry.npmjs.org/@types/phoenix/-/phoenix-1.6.6.tgz", + "integrity": "sha512-PIzZZlEppgrpoT2QgbnDU+MMzuR6BbCjllj0bM70lWoejMeNJAxCchxnv7J3XFkI8MpygtRpzXrIlmWUBclP5A==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/qs": { "version": "6.9.17", "resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.9.17.tgz", @@ -2441,6 +2530,16 @@ "integrity": "sha512-6SlHBzUW8Jhf3liqrGGXyTJSIFe4nqlJ5A5KaMZ2l/vbM3Wh3KSybots/wfWVzNLK4D1NZluDlSQIbIEPx6oyA==", "license": "MIT" }, + "node_modules/@types/ws": { + "version": "8.5.13", + "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.13.tgz", + "integrity": "sha512-osM/gWBTPKgHV8XkTunnegTRIsvF6owmf5w+JtAfOw472dptdm0dlGv4xCt6GwQRcC2XVOvvRE/0bAoQcL2QkA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/yargs": { "version": "17.0.33", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", @@ -9558,6 +9657,28 @@ "dev": true, "license": "ISC" }, + "node_modules/ws": { + "version": "8.18.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, "node_modules/xtend": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", diff --git a/src/rices/rices.service.ts b/src/rices/rices.service.ts index 2ed5dc6..b2ecdd2 100644 --- a/src/rices/rices.service.ts +++ b/src/rices/rices.service.ts @@ -34,6 +34,11 @@ export class RicesService { : uuidv4(); const token = uuidv4(); + + const encodedContent = Buffer.from( + JSON.stringify(createRiceDto.content), + ).toString('base64'); + const metadata = { id: uuidv4(), token, @@ -58,7 +63,7 @@ export class RicesService { const uploadedFilePath = `rices/${slug}/data.zenrice`; await this.gitHubService.createOrUpdateFile( uploadedFilePath, - createRiceDto.content, + encodedContent, `Add file createRiceDto.content to rice ${slug}`, ); } @@ -79,11 +84,31 @@ export class RicesService { throw new NotFoundException('Rice file not found in GitHub'); } - return { slug, fileContent }; + // Decode Base64 content + const contentPrev = Buffer.from(fileContent, 'base64').toString('utf-8'); + + // Remove unescaped double quotes at the beginning and end, if present + const content = contentPrev.replace(/^"|"$/g, ''); + + return { slug, content }; } async update(slug: string, token: string, updateRiceDto: UpdateRiceDto) { - const rice = await this.supabaseService.getRiceBySlug(slug); + /************* ✨ Codeium Command ⭐ *************/ + /** + * Updates the metadata and content of a rice entry identified by its slug. + * + * @param slug - The unique identifier for the rice entry. + * @param token - The authorization token to verify the request. + * @param updateRiceDto - Data Transfer Object containing fields to update. + * + * @returns A confirmation message indicating successful update. + * + * @throws NotFoundException - If the rice entry does not exist. + * @throws UnauthorizedException - If the provided token is invalid. + */ + /****** bf5f61f3-c1dc-40a0-85e6-288824144ead *******/ const rice = + await this.supabaseService.getRiceBySlug(slug); if (!rice) throw new NotFoundException('Rice not found'); if (rice.token !== token) throw new UnauthorizedException('Invalid token'); @@ -103,10 +128,14 @@ export class RicesService { ); if (updateRiceDto.content) { + const encodedContent = Buffer.from( + JSON.stringify(updateRiceDto.content), + ).toString('base64'); + const uploadedFilePath = `rices/${slug}/data.zenrice`; await this.gitHubService.createOrUpdateFile( uploadedFilePath, - updateRiceDto.content, + encodedContent, `Update file updateRiceDto.content in rice ${slug}`, ); } diff --git a/test/restclient/01_create_rice.http b/test/restclient/01_create_rice.http index 1a3e426..c59db4f 100644 --- a/test/restclient/01_create_rice.http +++ b/test/restclient/01_create_rice.http @@ -1,18 +1,17 @@ @baseUrl = http://localhost:3000 -# base64 of following json: ewogICAgImtleSI6ICJ2YWx1ZSIsCiAgICAiZGVzY3JpcHRpb24iOiAiRXhhbXBsZSBjb250ZW50Igp9 - # { # "key": "value", # "description": "Example content" # } +# {'key':'value','description':'Example content'} POST {{baseUrl}}/rices Content-Type: application/json { - "name": "cool-zenrice-aurora2", - "content": "ewogICAgImtleSI6ICJ2YWx1ZSIsCiAgICAiZGVzY3JpcHRpb24iOiAiRXhhbXBsZSBjb250ZW50Igp9" + "name": "cool-zenrice-aurora", + "content": "{'key':'value','description':'Example content'}" } diff --git a/test/restclient/02_download_rice.http b/test/restclient/02_download_rice.http index 6de7703..670225d 100644 --- a/test/restclient/02_download_rice.http +++ b/test/restclient/02_download_rice.http @@ -1,4 +1,4 @@ @baseUrl = http://localhost:3000 -@previous_slug = cool-zenrice-aurora-84154784-8dc4-4153-8bd5-fd9f55f03b93 +@previous_slug = cool-zenrice-aurora-265e3881-f9de-4bbf-bb22-b9c37dbf04bc GET {{baseUrl}}/rices/{{previous_slug}} \ No newline at end of file diff --git a/test/restclient/03_update_rice.http b/test/restclient/03_update_rice.http index df7ea37..ed10088 100644 --- a/test/restclient/03_update_rice.http +++ b/test/restclient/03_update_rice.http @@ -2,19 +2,18 @@ @previous_slug = cool-zenrice-aurora2-1d1e74b3-8d6d-40ea-bd4f-a6f4ad893f88 @previous_token = be4545f4-d92b-416c-8b3b-50cc9a49dee9 -# base64 of following json: ewogICAgImtleSI6ICJuZXdfdmFsdWUiLAogICAgImRlc2NyaXB0aW9uIjogIkV4YW1wbGUgY29udGVudCIKfQ - # { -# "key": "new_value", -# "description": "Example content" +# "key": "value", +# "description": "Example updated content" # } +# {'key':'value','description':'Example updated content'} + PUT {{baseUrl}}/rices/{{previous_slug}} Content-Type: application/json x-rices-token: {{previous_token}} { - - "content": "ewogICAgImtleSI6ICJuZXdfdmFsdWUiLAogICAgImRlc2NyaXB0aW9uIjogIkV4YW1wbGUgY29udGVudCIKfQ" + "content": "{'key':'value','description':'Example updated content'}" } diff --git a/test/restclient/04_delete_rice.http b/test/restclient/04_delete_rice.http index 172da1b..99a7d0c 100644 --- a/test/restclient/04_delete_rice.http +++ b/test/restclient/04_delete_rice.http @@ -1,6 +1,6 @@ @baseUrl = http://localhost:3000 -@previous_slug = my-first-zenrice-b7b94d24-ecb6-4495-93de-ba85be2e3052 -@previous_token = 6181664b-00e8-4eef-8e23-1f7fa0c64021 +@previous_slug = cool-zenrice-aurora-e8abd40d-8657-4ddd-99c5-912cced67497 +@previous_token = 2ce5c580-46e5-4a28-adff-30ce5662f29d DELETE {{baseUrl}}/rices/{{previous_slug}} x-rices-token: {{previous_token}}