Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/api/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
43 changes: 22 additions & 21 deletions packages/api/src/core/prepareParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down