diff --git a/packages/utils/package.json b/packages/utils/package.json new file mode 100644 index 00000000..7773238b --- /dev/null +++ b/packages/utils/package.json @@ -0,0 +1,72 @@ +{ + "name": "@signpdf/utils", + "version": "3.0.0", + "description": "Utilities for the @signpdf packages.", + "repository": { + "type": "git", + "url": "https://github.com/vbuch/node-signpdf" + }, + "license": "MIT", + "keywords": [ + "sign", + "pdf", + "node", + "nodejs", + "esign", + "adobe", + "ppklite", + "sign detached", + "pkcs7", + "pkcs#7", + "pades", + "digital signature" + ], + "main": "dist/signpdf.js", + "files": [ + "dist", + "LICENSE", + "README.md" + ], + "engines": { + "node": ">=12" + }, + "scripts": { + "test:coverage": "./node_modules/.bin/jest --coverage", + "test": "./node_modules/.bin/jest", + "test:watch": "./node_modules/.bin/jest --watch", + "build": "rm -rf ./dist/* & ./node_modules/.bin/babel ./src -d ./dist --ignore \"**/*.test.js\" & tsc", + "build:watch": "./node_modules/.bin/babel --watch ./src -d ./dist --ignore \"**/*.test.js\"", + "lint": "./node_modules/.bin/eslint -c .eslintrc --ignore-path .eslintignore ./", + "prepare": "husky install" + }, + "peerDependencies": { + "node-forge": "^1.2.1" + }, + "devDependencies": { + "@signpdf/signpdf": "workspace:^", + "@signpdf/placeholder-pdfkit10": "workspace:^", + + "@babel/cli": "^7.0.0", + "@babel/core": "^7.4.0", + "@babel/eslint-parser": "^7.16.3", + "@babel/node": "^7.0.0", + "@babel/plugin-syntax-object-rest-spread": "^7.0.0", + "@babel/preset-env": "^7.4.2", + "@types/node": ">=12.0.0", + "@types/node-forge": "^1.2.1", + "assertion-error": "^1.1.0", + "babel-jest": "^27.3.1", + "babel-plugin-module-resolver": "^3.1.1", + "coveralls": "^3.0.2", + "eslint": "^8.2.0", + "eslint-config-airbnb-base": "^15.0.0", + "eslint-import-resolver-babel-module": "^5.3.1", + "eslint-plugin-import": "^2.25.3", + "eslint-plugin-jest": "^25.2.4", + "husky": "^7.0.4", + "jest": "^27.3.1", + "node-forge": "^1.2.1", + "pdfkit": "^0.10.0", + "typescript": "^5.2.2" + } +} diff --git a/packages/utils/src/SignPdfError.js b/packages/utils/src/SignPdfError.js new file mode 100644 index 00000000..0312c1b0 --- /dev/null +++ b/packages/utils/src/SignPdfError.js @@ -0,0 +1,19 @@ +export const ERROR_TYPE_UNKNOWN = 1; +export const ERROR_TYPE_INPUT = 2; +export const ERROR_TYPE_PARSE = 3; +export const ERROR_VERIFY_SIGNATURE = 4; + +class SignPdfError extends Error { + constructor(msg, type = ERROR_TYPE_UNKNOWN) { + super(msg); + this.type = type; + } +} + +// Shorthand +SignPdfError.TYPE_UNKNOWN = ERROR_TYPE_UNKNOWN; +SignPdfError.TYPE_INPUT = ERROR_TYPE_INPUT; +SignPdfError.TYPE_PARSE = ERROR_TYPE_PARSE; +SignPdfError.VERIFY_SIGNATURE = ERROR_VERIFY_SIGNATURE; + +export default SignPdfError; diff --git a/packages/utils/src/SignPdfError.test.js b/packages/utils/src/SignPdfError.test.js new file mode 100644 index 00000000..93cca72a --- /dev/null +++ b/packages/utils/src/SignPdfError.test.js @@ -0,0 +1,26 @@ +import SignPdfError, { + ERROR_TYPE_UNKNOWN, + ERROR_TYPE_INPUT, + ERROR_TYPE_PARSE, +} from './SignPdfError'; + +describe('SignPdfError', () => { + it('SignPdfError extends Error', () => { + const instance = new SignPdfError('Whatever message'); + expect(instance instanceof Error).toBe(true); + }); + it('type defaults to UNKNOWN', () => { + const instance = new SignPdfError('Whatever message'); + expect(instance.type).toBe(ERROR_TYPE_UNKNOWN); + }); + it('type can be specified', () => { + [ + ERROR_TYPE_UNKNOWN, + ERROR_TYPE_INPUT, + ERROR_TYPE_PARSE, + ].forEach((type) => { + const instance = new SignPdfError('Whatever message', type); + expect(instance.type).toBe(type); + }); + }); +});