From a7f75b4627f5a0420a47f9156cffa47ecd471234 Mon Sep 17 00:00:00 2001 From: Jon Ursenbach Date: Fri, 13 Jan 2023 16:45:37 -0800 Subject: [PATCH] fix: moving off `fs/promises` to `fs` in client-side code --- packages/api/.eslintrc | 9 ++++++ packages/api/src/core/prepareParams.ts | 43 +++++++++++++------------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/packages/api/.eslintrc b/packages/api/.eslintrc index b6035f4a..2c85b305 100644 --- a/packages/api/.eslintrc +++ b/packages/api/.eslintrc @@ -33,6 +33,15 @@ "import/no-unresolved": "off" } }, + { + "files": ["src/core/**/*.ts"], + "rules": { + "no-restricted-imports": ["error", { + "name": "fs/promises", + "message": "Please use `fs` instead as some client frameworks don't polyfill `fs/promises`." + }] + } + }, { "files": ["example.js"], "rules": { diff --git a/packages/api/src/core/prepareParams.ts b/packages/api/src/core/prepareParams.ts index 8cd7970c..364d5670 100644 --- a/packages/api/src/core/prepareParams.ts +++ b/packages/api/src/core/prepareParams.ts @@ -2,7 +2,7 @@ import type { ReadStream } from 'fs'; import type { Operation } from 'oas'; import type { ParameterObject, SchemaObject } from 'oas/dist/rmoas.types'; -import fs from 'fs/promises'; +import fs from 'fs'; import path from 'path'; import stream from 'stream'; @@ -81,33 +81,34 @@ function processFile( // In order to support relative pathed files, we need to attempt to resolve them. const resolvedFile = path.resolve(file); - return fs - .stat(resolvedFile) - .then(() => datauri(resolvedFile)) - .then(fileMetadata => { + return new Promise((resolve, reject) => { + fs.stat(resolvedFile, async err => { + if (err) { + if (err.code === 'ENOENT') { + // It's less than ideal for us to handle files that don't exist like this but because + // `file` is a string it might actually be the full text contents of the file and not + // actually a path. + // + // We also can't really regex to see if `file` *looks*` like a path because one should be + // able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that + // doesn't *look* like a path, it is one that should still work. + return resolve(undefined); + } + + return reject(err); + } + + const fileMetadata = await datauri(resolvedFile); const payloadFilename = encodeURIComponent(path.basename(resolvedFile)); - return { + return resolve({ paramName, base64: fileMetadata.content.replace(';base64', `;name=${payloadFilename};base64`), filename: payloadFilename, buffer: fileMetadata.buffer, - }; - }) - .catch(err => { - if (err.code === 'ENOENT') { - // It's less than ideal for us to handle files that don't exist like this but because - // `file` is a string it might actually be the full text contents of the file and not - // actually a path. - // - // We also can't really regex to see if `file` *looks*` like a path because one should be - // able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that - // doesn't *look* like a path, it is one that should still work. - return undefined; - } - - throw err; + }); }); + }); } else if (file instanceof stream.Readable) { return getStream.buffer(file).then(buffer => { const filePath = file.path as string;