Skip to content

Commit

Permalink
Merge pull request #27 from Sparticuz/feature/replace-input
Browse files Browse the repository at this point in the history
  • Loading branch information
Sparticuz committed Jan 6, 2023
2 parents 5ae292f + 11305bd commit 4e93b97
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 700 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// eslint-disable-next-line node/no-unpublished-require
require("@rushstack/eslint-patch/modern-module-resolution");

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Install QPDF
run: |
sudo apt-get install wget unzip -y
wget -O /tmp/qpdf.zip https://github.com/qpdf/qpdf/releases/download/v11.1.0/qpdf-11.1.0-bin-linux-x86_64.zip
wget -O /tmp/qpdf.zip https://github.com/qpdf/qpdf/releases/download/v11.2.0/qpdf-11.2.0-bin-linux-x86_64.zip
sudo unzip -d / /tmp/qpdf.zip
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 14
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm run build
Expand Down
1,326 changes: 640 additions & 686 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build": "tsc -p tsconfig.build.json"
},
"engines": {
"node": ">=14"
"node": ">=14.18.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -48,16 +48,16 @@
"homepage": "https://github.com/Sparticuz/node-qpdf2",
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@sparticuz/eslint-config": "^7.1.8",
"@sparticuz/eslint-config": "^7.1.11",
"@tsconfig/node14": "^1.0.3",
"@types/node": "^14.18.29",
"@typescript-eslint/eslint-plugin": "^5.37.0",
"@typescript-eslint/parser": "^5.37.0",
"ava": "^4.3.3",
"@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0",
"ava": "^5.1.0",
"c8": "^7.12.0",
"eslint": "^8.23.1",
"prettier": "^2.7.1",
"eslint": "^8.31.0",
"prettier": "^2.8.1",
"ts-node": "^10.9.1",
"typescript": "^4.8.3"
"typescript": "^4.9.4"
}
}
7 changes: 6 additions & 1 deletion src/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ export const encrypt = async (userPayload: EncryptOptions): Promise<Buffer> => {
callArguments.push("--", payload.input);

if (payload.output) {
callArguments.push(payload.output);
// If the input and output locations are the same, and overwrite is true, replace the input file
if (payload.input === payload.output && payload.overwrite) {
callArguments.push("--replace-input");
} else {
callArguments.push(payload.output);
}
} else {
// Print PDF on stdout
callArguments.push("-");
Expand Down
2 changes: 1 addition & 1 deletion src/spawn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Ignore errors about unsafe-arguments, this is because data is unknown
/* eslint-disable @typescript-eslint/no-unsafe-argument */
import { spawn } from "child_process";
import { spawn } from "node:child_process";

export default (callArguments: string[]): Promise<Buffer> => {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from "fs";
import { existsSync } from "node:fs";

/**
* hyphenate will take any value that is dromedary case (camelCase with a initial lowercase letter)
Expand Down
15 changes: 15 additions & 0 deletions test/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import test from "ava";
import { copyFile } from "node:fs/promises";
import { encrypt } from "../src/encrypt";

const input = "test/example.pdf";
Expand Down Expand Up @@ -112,3 +113,17 @@ test("should throw if restrictions are wrong", async (t) => {
);
t.is(error?.message, "Invalid Restrictions");
});

test("Should encrypt and overwrite the file", async (t) => {
await t.notThrowsAsync(async () => {
// First, copy example.pdf to output/overwrite.pdf
// eslint-disable-next-line sonarjs/no-duplicate-string
await copyFile("test/example.pdf", "test/output/overwrite.pdf");
await encrypt({
input: "test/output/overwrite.pdf",
output: "test/output/overwrite.pdf",
overwrite: true,
password: "1234",
});
});
});

0 comments on commit 4e93b97

Please sign in to comment.