Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: --replace-input #27

Merged
merged 4 commits into from
Jan 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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",
});
});
});