Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
/test/asset/sample.pdf
/test/asset/sample1.jpg
/test/asset/sample2.jpg
/.yarn/install-state.gz
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
9 changes: 5 additions & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default tseslint.config(
'@typescript-eslint': tseslint.plugin,
},

// rules: {
rules: {
// // TypeScript specific rules
// '@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
// '@typescript-eslint/explicit-function-return-type': 'warn',
Expand All @@ -50,8 +50,9 @@ export default tseslint.config(
// '@typescript-eslint/no-var-requires': 'error',
//
// // General ESLint rules
// 'no-console': 'warn',
// 'no-debugger': 'error',
'no-console': 'error',
'no-debugger': 'error',
'prefer-template': 'error',
// //'prefer-const': 'error',
// 'no-var': 'error',
//
Expand All @@ -62,7 +63,7 @@ export default tseslint.config(
// 'ignoreMemberSort': false,
// 'memberSyntaxSortOrder': ['none', 'all', 'multiple', 'single'],
// }],
// },
},
},

// Ignore patterns
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
],
"main": "dist/index.js",
"types": "dist/index.d.ts",
"bin": {
"node-hp-scan-to": "dist/index.js"
},
"bin": "dist/index.js",
"scripts": {
"prestart": "node getCommitId.js",
"start": "ts-node src/index.ts",
Expand Down Expand Up @@ -59,6 +57,7 @@
"@types/dateformat": "^5.0.3",
"@types/mocha": "^10.0.10",
"@types/node": "^24.3.0",
"@types/pino": "^7.0.5",
"@types/xml2js": "^0.4.14",
"@typescript-eslint/eslint-plugin": "^8.41.0",
"@typescript-eslint/parser": "^8.41.0",
Expand All @@ -72,12 +71,12 @@
"nock": "^14.0.10",
"nodemon": "^3.1.10",
"nyc": "^17.1.0",
"pino-pretty": "^13.1.1",
"prettier": "^3.6.2",
"ts-mockito": "^2.6.1",
"ts-node": "^10.9.2",
"typescript": "^5.9.2",
"typescript-eslint": "^8.41.0",
"yarn": "^1.22.22"
"typescript": "^5.9.3",
"typescript-eslint": "^8.41.0"
},
"dependencies": {
"@commander-js/extra-typings": "^14.0.0",
Expand All @@ -86,10 +85,12 @@
"commander": "^14.0.0",
"config": "^4.1.1",
"dateformat": "^5.0.3",
"form-data": "^4.0.4",
"jspdf": "^3.0.2",
"nanoid": "^5.1.6",
"pino": "^10.0.0",
"xml2js": "^0.6.2",
"zod": "^4.1.3"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
"packageManager": "yarn@4.10.3"
}
42 changes: 19 additions & 23 deletions src/HPApi.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

import { promisify } from "util";
import fs from "fs";
import { promisify } from "node:util";
import fs from "node:fs";
import axios, {
AxiosError,
AxiosRequestConfig,
AxiosResponse,
RawAxiosRequestHeaders,
} from "axios";
import * as stream from "stream";
import { Stream } from "stream";
import * as stream from "node:stream";
import { Stream } from "node:stream";
import EventTable, { EtagEventTable } from "./hpModels/EventTable";
import Job from "./hpModels/Job";
import ScanStatus from "./hpModels/ScanStatus";
Expand All @@ -26,40 +26,36 @@ import WalkupScanManifest from "./hpModels/WalkupScanManifest";
import ScanJobManifest from "./hpModels/ScanJobManifest";
import ScanCaps from "./hpModels/ScanCaps";
import { delay } from "./delay";
import * as net from "net";
import * as net from "node:net";
import EsclScanJobManifest from "./hpModels/EsclManifest";
import EsclScanCaps from "./hpModels/EsclScanCaps";
import EsclScanStatus from "./hpModels/EsclScanStatus";
import { IScanJobSettings } from "./hpModels/IScanJobSettings";
import EsclScanImageInfo from "./hpModels/EsclScanImageInfo";
import PathHelper from "./PathHelper";
import logger, { getLoggerForFile } from "./logger";

let printerIP = "192.168.1.11";
let debug = false;
let callCount = 0;

export default class HPApi {
private static logger = getLoggerForFile(__filename);
static setDeviceIP(ip: string): void {
printerIP = ip;
}

static setDebug(dbg: boolean): void {
debug = dbg;
}
static isDebug(): boolean {
return debug;
}

private static logDebug(
callId: number,
isRequest: boolean,
msg: object | string,
): void {
if (debug) {
const id = String(callId).padStart(4, "0");
const content = typeof msg === "string" ? msg : JSON.stringify(msg);
console.log(id + (isRequest ? " -> " : " <- ") + content);
}
if (!logger.isLevelEnabled("debug")) return;

const id = String(callId).padStart(4, "0");
const prefix = id + (isRequest ? " -> " : " <- ");
const content = typeof msg === "string" ? msg : JSON.stringify(msg);

this.logger.debug({ callId, isRequest, msg }, prefix + content);
}

private static async callAxios(
Expand Down Expand Up @@ -117,15 +113,15 @@ export default class HPApi {
let first = true;
while (!(await HPApi.isAlive())) {
if (first) {
console.log(
logger.info(
`Device ip: ${printerIP} is down! [${new Date().toISOString()}]`,
);
}
first = false;
await delay(deviceUpPollingInterval);
}
if (!first) {
console.log(
logger.info(
`Device ip: ${printerIP} is up again! [${new Date().toISOString()}]`,
);
}
Expand Down Expand Up @@ -440,7 +436,7 @@ export default class HPApi {
timeout = 1200;
}
if (timeout > 0) {
url += "?timeout=" + timeout;
url += `?timeout=${timeout}`;
}
return url;
}
Expand Down Expand Up @@ -607,7 +603,7 @@ export default class HPApi {
destination: string,
): Promise<string> {
return await HPApi.downloadPage(
jobUri + "/NextDocument",
`${jobUri}/NextDocument`,
destination,
60_000,
);
Expand All @@ -618,7 +614,7 @@ export default class HPApi {
): Promise<EsclScanImageInfo> {
const response = await HPApi.callAxios({
baseURL: `http://${printerIP}`,
url: jobUri + "/ScanImageInfo",
url: `${jobUri}/ScanImageInfo`,
method: "GET",
responseType: "text",
});
Expand Down
41 changes: 17 additions & 24 deletions src/JpegUtil.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
let debug = false;
import { getLoggerForFile } from "./logger";

const logger = getLoggerForFile(__filename);

const start_of_Frame_0 = "FFC0";
const define_number_of_lines = "FFDC";

export default class JpegUtil {
static setDebug(dbg: boolean) {
debug = dbg;
}

private static logDebug(msg: string | object) {
if (debug) {
console.log(msg);
}
}

private static numToHex(s: number) {
return s.toString(16).padStart(2, "0").toUpperCase();
}
Expand Down Expand Up @@ -116,12 +108,12 @@ export default class JpegUtil {
});

if (numberOfLine == null) {
this.logDebug("DNL marker not found impossible to fix height");
logger.debug("DNL marker not found impossible to fix height");
return null;
}

if (startOfStartOfFrame == null || lengthOfStartOfFrame == null) {
this.logDebug(
logger.debug(
"Start of frame 0 not found, either jpeg parsing is broken either the stream is corrupted",
);
return null;
Expand Down Expand Up @@ -182,15 +174,15 @@ export default class JpegUtil {
let i: number = 0;

if (!this.isSOIHeader(i, buffer)) {
this.logDebug("Not a valid SOI header");
logger.debug("Not a valid SOI header");
return false;
}

i += 4;

// Check for valid JPEG header (null terminated JFIF)
if (!this.isValidJpegHeader(i, buffer)) {
this.logDebug("Not a valid JFIF string");
logger.debug("Not a valid JFIF string");
return false;
}

Expand Down Expand Up @@ -239,7 +231,7 @@ export default class JpegUtil {
// read the new block length
const blockLength = buffer[i + 2] * 256 + buffer[i + 3];

this.logDebug(`block size for ${marker} is ${blockLength}`);
logger.debug(`block size for ${marker} is ${blockLength}`);
return blockLength;
}
}
Expand All @@ -259,7 +251,7 @@ export default class JpegUtil {
return j;
}
} else {
this.logDebug(
logger.debug(
`Premature end of stream reach while searching for the block size inside marker ${current_marker}`,
);
return null;
Expand All @@ -285,28 +277,29 @@ export default class JpegUtil {
i += blockLength;
while (i < buffer.length) {
if (buffer[i] != 0xff) {
this.logDebug(
"We should be at the begining of the next block, but got: " +
buffer[i],
logger.debug(
`We should be at the begining of the next block, but got: ${
buffer[i]
}`,
);
return false;
}

if (i + 1 >= buffer.length) {
this.logDebug("End of stream prematurely found in marker: " + marker);
logger.debug(`End of stream prematurely found in marker: ${marker}`);
return false;
}

if (buffer[i + 1] == 0x00) {
this.logDebug(`Bad marker at ${i} 0x00 just after marker ${marker}`);
logger.debug(`Bad marker at ${i} 0x00 just after marker ${marker}`);
return false;
}

marker = this.numToHex(buffer[i]) + this.numToHex(buffer[i + 1]);

const foundBlockLength = this.getBlockLength(buffer, i, marker);
if (foundBlockLength == null) {
this.logDebug(
logger.debug(
`Was not able to determine block size for marker ${marker}`,
);
return false;
Expand All @@ -321,7 +314,7 @@ export default class JpegUtil {
i = i + 2 + blockLength;
}

this.logDebug("End of payload reached");
logger.debug("End of payload reached");
return true;
}
}
11 changes: 7 additions & 4 deletions src/PathHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import os from "node:os";
import fs, { promises as Fs } from "node:fs";
import dateformat from "dateformat";
import { customAlphabet } from "nanoid";
import { getLoggerForFile } from "./logger";

const logger = getLoggerForFile(__filename);

const nanoid = customAlphabet(
"23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ",
Expand Down Expand Up @@ -50,8 +53,8 @@ export default class PathHelper {
files.some((x) => x.startsWith(currentScanCountProbe)) &&
files.some(
(x) =>
x.startsWith(currentScanCountProbe + "_") ||
files.some((x) => x.startsWith(currentScanCountProbe + ".")),
x.startsWith(`${currentScanCountProbe}_`) ||
files.some((x) => x.startsWith(`${currentScanCountProbe}.`)),
)
)
) {
Expand Down Expand Up @@ -153,14 +156,14 @@ export default class PathHelper {

static async getTargetFolder(directory: string | undefined) {
const folder = await PathHelper.getOutputFolder(directory);
console.log(`Output folder: ${folder}`);
logger.info(`Output folder: ${folder}`);
await this.checkIfFolderIsWritable(folder);
return folder;
}

static async getTempFolder(directory: string | undefined) {
const tempFolder = await PathHelper.getOutputFolder(directory);
console.log(`Temporary folder: ${tempFolder}`);
logger.info(`Temporary folder: ${tempFolder}`);
await this.checkIfFolderIsWritable(tempFolder);
return tempFolder;
}
Expand Down
Loading
Loading