Skip to content

Commit 7101ed3

Browse files
authored
chore(index): add throw jsdoc tags; add type guards to tests (#491)
1 parent 8b9a6c7 commit 7101ed3

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

API.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
<dd></dd>
66
</dl>
77

8+
## Constants
9+
10+
<dl>
11+
<dt><a href="#ERROR_MSGS">ERROR_MSGS</a> : <code>Record.&lt;string, string&gt;</code></dt>
12+
<dd></dd>
13+
</dl>
14+
815
## Typedefs
916

1017
<dl>
@@ -33,6 +40,10 @@
3340
<a name="new_UnRTF_new"></a>
3441

3542
### new UnRTF([binPath])
43+
**Throws**:
44+
45+
- <code>Error</code> If UnRTF binary cannot be found or version cannot be determined.
46+
3647

3748
| Param | Type | Description |
3849
| --- | --- | --- |
@@ -61,6 +72,10 @@ UnRTF will use the directory of the original file to store embedded pictures.
6172

6273
**Kind**: instance method of [<code>UnRTF</code>](#UnRTF)
6374
**Returns**: <code>Promise.&lt;string&gt;</code> - A promise that resolves with a stdout string, or rejects with an `Error` object.
75+
**Throws**:
76+
77+
- <code>Error</code> If the file is missing, not an RTF file, or if UnRTF returns an error.
78+
6479
**Author**: Frazer Smith
6580

6681
| Param | Type | Description |
@@ -72,6 +87,10 @@ UnRTF will use the directory of the original file to store embedded pictures.
7287

7388
### UnRTF.UnRTF : <code>string</code> \| <code>undefined</code>
7489
**Kind**: static property of [<code>UnRTF</code>](#UnRTF)
90+
<a name="ERROR_MSGS"></a>
91+
92+
## ERROR\_MSGS : <code>Record.&lt;string, string&gt;</code>
93+
**Kind**: global constant
7594
<a name="OptionDetails"></a>
7695

7796
## OptionDetails : <code>object</code>

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { normalize, resolve: pathResolve } = require("node:path");
66
const { platform } = require("node:process");
77
const { gt, lt } = require("semver");
88

9+
/** @type {Record<string, string>} */
910
const ERROR_MSGS = {
1011
3221225477: "Segmentation fault",
1112
};
@@ -53,10 +54,10 @@ const UNRTF_VERSION_REG = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
5354
* version of binary.
5455
* @ignore
5556
* @param {UnRTFAcceptedOptions} acceptedOptions - Object containing accepted options.
56-
* @param {Record<string, any>} options - Object containing options to pass to binary.
57+
* @param {UnRTFOptions} options - Object containing options to pass to binary.
5758
* @param {string} version - Semantic version of binary.
5859
* @returns {string[]} Array of CLI arguments.
59-
* @throws If invalid arguments provided.
60+
* @throws {Error} If invalid arguments provided.
6061
*/
6162
function parseOptions(acceptedOptions, options, version) {
6263
/** @type {string[]} */
@@ -167,6 +168,7 @@ class UnRTF {
167168
*
168169
* For `win32`, a binary is bundled with the package and will be used
169170
* if a local installation is not found.
171+
* @throws {Error} If UnRTF binary cannot be found or version cannot be determined.
170172
*/
171173
constructor(binPath) {
172174
this.#unrtfPath = "";
@@ -241,6 +243,7 @@ class UnRTF {
241243
* @param {string} file - Filepath of the RTF file to read.
242244
* @param {UnRTFOptions} [options] - Options to pass to UnRTF binary.
243245
* @returns {Promise<string>} A promise that resolves with a stdout string, or rejects with an `Error` object.
246+
* @throws {Error} If the file is missing, not an RTF file, or if UnRTF returns an error.
244247
*/
245248
async convert(file, options = {}) {
246249
let normalizedFile;
@@ -303,8 +306,7 @@ class UnRTF {
303306
} else if (stdErr === "") {
304307
reject(
305308
new Error(
306-
// @ts-ignore: Second operand used if code is not in ERROR_MSGS
307-
ERROR_MSGS[code] ||
309+
ERROR_MSGS[code ?? -1] ||
308310
`unrtf ${args.join(
309311
" "
310312
)} exited with code ${code}`

src/index.test.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function getTestBinaryPath() {
4747
unrtfPath = join(__dirname, "lib", "win32", "unrtf-0.19.3", "bin");
4848
}
4949

50+
if (!unrtfPath) {
51+
throw new Error(`Unable to find ${platform} UnRTF binaries.`);
52+
}
53+
5054
return normalize(unrtfPath);
5155
}
5256

@@ -90,9 +94,11 @@ describe("Node-UnRTF module", () => {
9094
// eslint-disable-next-line no-unused-vars -- This is intentional
9195
const unRtf = new UnRTFMock();
9296
} catch (err) {
93-
expect(err.message).toBe(
94-
`Unable to find ${mockPlatform} UnRTF binaries, please pass the installation directory as a parameter to the UnRTF instance.`
95-
);
97+
if (err instanceof Error) {
98+
expect(err.message).toBe(
99+
`Unable to find ${mockPlatform} UnRTF binaries, please pass the installation directory as a parameter to the UnRTF instance.`
100+
);
101+
}
96102
}
97103
});
98104

@@ -115,22 +121,32 @@ describe("Node-UnRTF module", () => {
115121
// eslint-disable-next-line no-unused-vars -- This is intentional
116122
const unRtf = new UnRTFMock();
117123
} catch (err) {
118-
expect(err.message).toBe("Unable to determine UnRTF version.");
124+
if (err instanceof Error) {
125+
expect(err.message).toBe(
126+
"Unable to determine UnRTF version."
127+
);
128+
}
119129
}
120130
});
121131
});
122132

123133
const unRtf = new UnRTF(testBinaryPath);
124134

125135
describe("Convert function", () => {
136+
/** @type {string} */
126137
let version;
127138

128139
beforeAll(async () => {
129140
const { stderr } = await execFileAsync(
130141
join(testBinaryPath, "unrtf"),
131142
["--version"]
132143
);
133-
version = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
144+
145+
const match = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr);
146+
if (!match?.[1]) {
147+
throw new Error("Unable to parse UnRTF version from stderr");
148+
}
149+
version = match[1];
134150
});
135151

136152
it("Converts RTF if any valid options are set", async () => {
@@ -302,6 +318,7 @@ describe("Node-UnRTF module", () => {
302318
"Rejects with an Error object if $testName",
303319
async ({ filePath, options, expError }) => {
304320
await expect(
321+
// @ts-expect-error: Testing invalid parameters being passed
305322
unRtf.convert(filePath, {
306323
noPictures: true,
307324
...options,

0 commit comments

Comments
 (0)