Skip to content

Commit 1d9c87e

Browse files
authored
fix: moving off fs/promises to fs in client-side code (#605)
1 parent 21bd60a commit 1d9c87e

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

packages/api/.eslintrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
"import/no-unresolved": "off"
3434
}
3535
},
36+
{
37+
"files": ["src/core/**/*.ts"],
38+
"rules": {
39+
"no-restricted-imports": ["error", {
40+
"name": "fs/promises",
41+
"message": "Please use `fs` instead as some client frameworks don't polyfill `fs/promises`."
42+
}]
43+
}
44+
},
3645
{
3746
"files": ["example.js"],
3847
"rules": {

packages/api/src/core/prepareParams.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { ReadStream } from 'fs';
22
import type { Operation } from 'oas';
33
import type { ParameterObject, SchemaObject } from 'oas/dist/rmoas.types';
44

5-
import fs from 'fs/promises';
5+
import fs from 'fs';
66
import path from 'path';
77
import stream from 'stream';
88

@@ -81,33 +81,34 @@ function processFile(
8181
// In order to support relative pathed files, we need to attempt to resolve them.
8282
const resolvedFile = path.resolve(file);
8383

84-
return fs
85-
.stat(resolvedFile)
86-
.then(() => datauri(resolvedFile))
87-
.then(fileMetadata => {
84+
return new Promise((resolve, reject) => {
85+
fs.stat(resolvedFile, async err => {
86+
if (err) {
87+
if (err.code === 'ENOENT') {
88+
// It's less than ideal for us to handle files that don't exist like this but because
89+
// `file` is a string it might actually be the full text contents of the file and not
90+
// actually a path.
91+
//
92+
// We also can't really regex to see if `file` *looks*` like a path because one should be
93+
// able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that
94+
// doesn't *look* like a path, it is one that should still work.
95+
return resolve(undefined);
96+
}
97+
98+
return reject(err);
99+
}
100+
101+
const fileMetadata = await datauri(resolvedFile);
88102
const payloadFilename = encodeURIComponent(path.basename(resolvedFile));
89103

90-
return {
104+
return resolve({
91105
paramName,
92106
base64: fileMetadata.content.replace(';base64', `;name=${payloadFilename};base64`),
93107
filename: payloadFilename,
94108
buffer: fileMetadata.buffer,
95-
};
96-
})
97-
.catch(err => {
98-
if (err.code === 'ENOENT') {
99-
// It's less than ideal for us to handle files that don't exist like this but because
100-
// `file` is a string it might actually be the full text contents of the file and not
101-
// actually a path.
102-
//
103-
// We also can't really regex to see if `file` *looks*` like a path because one should be
104-
// able to pass in a relative `owlbert.png` (instead of `./owlbert.png`) and though that
105-
// doesn't *look* like a path, it is one that should still work.
106-
return undefined;
107-
}
108-
109-
throw err;
109+
});
110110
});
111+
});
111112
} else if (file instanceof stream.Readable) {
112113
return getStream.buffer(file).then(buffer => {
113114
const filePath = file.path as string;

0 commit comments

Comments
 (0)