enhanced-printer 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +1 -2
- package/lib/index.js +2 -3
- package/lib/job-name-mapper.d.ts +24 -0
- package/lib/job-name-mapper.js +84 -0
- package/lib/job-tracker.d.ts +7 -2
- package/lib/job-tracker.js +34 -14
- package/lib/print-manager.d.ts +4 -9
- package/lib/print-manager.js +19 -17
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -10,6 +10,5 @@
|
|
|
10
10
|
* - Automatic use of printer defaults when settings not specified
|
|
11
11
|
* - Query job status and printer information
|
|
12
12
|
*/
|
|
13
|
-
export { print, getPrinters, getDefaultPrinter,
|
|
14
|
-
cancelJob } from './print-manager';
|
|
13
|
+
export { print, getPrinters, getDefaultPrinter, getJobInfo, getJobs, cancelJob } from './print-manager';
|
|
15
14
|
export { PrintOptions, PrintResult, PrinterInfo, JobInfo } from './types';
|
package/lib/index.js
CHANGED
|
@@ -12,12 +12,11 @@
|
|
|
12
12
|
* - Query job status and printer information
|
|
13
13
|
*/
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.cancelJob = exports.getJobs = exports.
|
|
15
|
+
exports.cancelJob = exports.getJobs = exports.getJobInfo = exports.getDefaultPrinter = exports.getPrinters = exports.print = void 0;
|
|
16
16
|
var print_manager_1 = require("./print-manager");
|
|
17
17
|
Object.defineProperty(exports, "print", { enumerable: true, get: function () { return print_manager_1.print; } });
|
|
18
18
|
Object.defineProperty(exports, "getPrinters", { enumerable: true, get: function () { return print_manager_1.getPrinters; } });
|
|
19
19
|
Object.defineProperty(exports, "getDefaultPrinter", { enumerable: true, get: function () { return print_manager_1.getDefaultPrinter; } });
|
|
20
|
-
Object.defineProperty(exports, "
|
|
21
|
-
Object.defineProperty(exports, "getPrinterJobs", { enumerable: true, get: function () { return print_manager_1.getPrinterJobs; } });
|
|
20
|
+
Object.defineProperty(exports, "getJobInfo", { enumerable: true, get: function () { return print_manager_1.getJobInfo; } });
|
|
22
21
|
Object.defineProperty(exports, "getJobs", { enumerable: true, get: function () { return print_manager_1.getJobs; } });
|
|
23
22
|
Object.defineProperty(exports, "cancelJob", { enumerable: true, get: function () { return print_manager_1.cancelJob; } });
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Job name mapping to track custom names
|
|
3
|
+
* Maps job ID to custom job name provided by user
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Store custom job name mapping
|
|
7
|
+
*/
|
|
8
|
+
export declare function storeJobName(printerName: string, jobId: number, customName: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Get custom job name for a job
|
|
11
|
+
*/
|
|
12
|
+
export declare function getCustomJobName(printerName: string, jobId: number): string | null;
|
|
13
|
+
/**
|
|
14
|
+
* Get system job ID from a custom job name (reverse lookup)
|
|
15
|
+
* Returns the jobId and printerName if found, null otherwise
|
|
16
|
+
*/
|
|
17
|
+
export declare function getSystemJobId(customJobName: string, printerName?: string): {
|
|
18
|
+
jobId: number;
|
|
19
|
+
printerName: string;
|
|
20
|
+
} | null;
|
|
21
|
+
/**
|
|
22
|
+
* Clear all job name mappings
|
|
23
|
+
*/
|
|
24
|
+
export declare function clearJobNames(): void;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Job name mapping to track custom names
|
|
4
|
+
* Maps job ID to custom job name provided by user
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.storeJobName = storeJobName;
|
|
8
|
+
exports.getCustomJobName = getCustomJobName;
|
|
9
|
+
exports.getSystemJobId = getSystemJobId;
|
|
10
|
+
exports.clearJobNames = clearJobNames;
|
|
11
|
+
// In-memory storage for job name mappings
|
|
12
|
+
const jobNameMap = new Map();
|
|
13
|
+
// Max age for mappings (24 hours)
|
|
14
|
+
const MAX_MAPPING_AGE = 24 * 60 * 60 * 1000;
|
|
15
|
+
/**
|
|
16
|
+
* Store custom job name mapping
|
|
17
|
+
*/
|
|
18
|
+
function storeJobName(printerName, jobId, customName) {
|
|
19
|
+
const key = `${printerName}_${jobId}`;
|
|
20
|
+
jobNameMap.set(key, {
|
|
21
|
+
jobId,
|
|
22
|
+
customName,
|
|
23
|
+
printerName,
|
|
24
|
+
timestamp: Date.now()
|
|
25
|
+
});
|
|
26
|
+
// Clean old entries
|
|
27
|
+
cleanOldMappings();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get custom job name for a job
|
|
31
|
+
*/
|
|
32
|
+
function getCustomJobName(printerName, jobId) {
|
|
33
|
+
const key = `${printerName}_${jobId}`;
|
|
34
|
+
const mapping = jobNameMap.get(key);
|
|
35
|
+
if (!mapping) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
// Check if mapping is too old
|
|
39
|
+
if (Date.now() - mapping.timestamp > MAX_MAPPING_AGE) {
|
|
40
|
+
jobNameMap.delete(key);
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return mapping.customName;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get system job ID from a custom job name (reverse lookup)
|
|
47
|
+
* Returns the jobId and printerName if found, null otherwise
|
|
48
|
+
*/
|
|
49
|
+
function getSystemJobId(customJobName, printerName) {
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
for (const [key, mapping] of jobNameMap.entries()) {
|
|
52
|
+
// Skip expired mappings
|
|
53
|
+
if (now - mapping.timestamp > MAX_MAPPING_AGE) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (mapping.customName === customJobName) {
|
|
57
|
+
// If printerName is provided, match it too
|
|
58
|
+
if (printerName && mapping.printerName !== printerName) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
return { jobId: mapping.jobId, printerName: mapping.printerName };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Clean mappings older than MAX_MAPPING_AGE
|
|
68
|
+
*/
|
|
69
|
+
function cleanOldMappings() {
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
const keysToDelete = [];
|
|
72
|
+
jobNameMap.forEach((mapping, key) => {
|
|
73
|
+
if (now - mapping.timestamp > MAX_MAPPING_AGE) {
|
|
74
|
+
keysToDelete.push(key);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
keysToDelete.forEach(key => jobNameMap.delete(key));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Clear all job name mappings
|
|
81
|
+
*/
|
|
82
|
+
function clearJobNames() {
|
|
83
|
+
jobNameMap.clear();
|
|
84
|
+
}
|
package/lib/job-tracker.d.ts
CHANGED
|
@@ -4,9 +4,14 @@ import { JobInfo } from './types';
|
|
|
4
4
|
*/
|
|
5
5
|
export declare function getJobIdByName(printerName: string, jobName: string, maxRetries?: number, retryDelay?: number): Promise<number | null>;
|
|
6
6
|
/**
|
|
7
|
-
* Gets detailed information about a print job
|
|
7
|
+
* Gets detailed information about a print job using system job ID
|
|
8
8
|
*/
|
|
9
|
-
export declare function
|
|
9
|
+
export declare function getJobInfoBySystemId(printerName: string, jobId: number): Promise<JobInfo | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Gets detailed information about a print job using custom job name
|
|
12
|
+
* Resolves the custom job name to the system job ID, then queries the spooler
|
|
13
|
+
*/
|
|
14
|
+
export declare function getJobInfo(customJobName: string, printerName?: string): Promise<JobInfo | null>;
|
|
10
15
|
/**
|
|
11
16
|
* Gets all print jobs for a printer with enhanced details
|
|
12
17
|
*/
|
package/lib/job-tracker.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getJobIdByName = getJobIdByName;
|
|
4
|
+
exports.getJobInfoBySystemId = getJobInfoBySystemId;
|
|
4
5
|
exports.getJobInfo = getJobInfo;
|
|
5
6
|
exports.getAllJobs = getAllJobs;
|
|
6
7
|
const child_process_1 = require("child_process");
|
|
8
|
+
const job_name_mapper_1 = require("./job-name-mapper");
|
|
7
9
|
/**
|
|
8
10
|
* Retrieves job ID by document name using PowerShell
|
|
9
11
|
*/
|
|
@@ -21,9 +23,9 @@ async function getJobIdByName(printerName, jobName, maxRetries = 3, retryDelay =
|
|
|
21
23
|
return null;
|
|
22
24
|
}
|
|
23
25
|
/**
|
|
24
|
-
* Gets detailed information about a print job
|
|
26
|
+
* Gets detailed information about a print job using system job ID
|
|
25
27
|
*/
|
|
26
|
-
async function
|
|
28
|
+
async function getJobInfoBySystemId(printerName, jobId) {
|
|
27
29
|
const script = `
|
|
28
30
|
Get-PrintJob -PrinterName "${escapePowerShellString(printerName)}" |
|
|
29
31
|
Where-Object {$_.Id -eq ${jobId}} |
|
|
@@ -36,10 +38,12 @@ async function getJobInfo(printerName, jobId) {
|
|
|
36
38
|
if (!result || !result.Id) {
|
|
37
39
|
return null;
|
|
38
40
|
}
|
|
41
|
+
// Get custom job name from mapper
|
|
42
|
+
const customName = (0, job_name_mapper_1.getCustomJobName)(printerName, result.Id);
|
|
39
43
|
return {
|
|
40
44
|
jobId: result.Id,
|
|
41
45
|
jobName: result.DocumentName || '',
|
|
42
|
-
customJobName: result.DocumentName || '',
|
|
46
|
+
customJobName: customName || result.DocumentName || '',
|
|
43
47
|
printerName: printerName,
|
|
44
48
|
status: result.JobStatus || 'Unknown',
|
|
45
49
|
pages: result.TotalPages || 0,
|
|
@@ -52,6 +56,18 @@ async function getJobInfo(printerName, jobId) {
|
|
|
52
56
|
return null;
|
|
53
57
|
}
|
|
54
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Gets detailed information about a print job using custom job name
|
|
61
|
+
* Resolves the custom job name to the system job ID, then queries the spooler
|
|
62
|
+
*/
|
|
63
|
+
async function getJobInfo(customJobName, printerName) {
|
|
64
|
+
// Resolve custom job name to system job ID
|
|
65
|
+
const mapping = (0, job_name_mapper_1.getSystemJobId)(customJobName, printerName);
|
|
66
|
+
if (!mapping) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
return getJobInfoBySystemId(mapping.printerName, mapping.jobId);
|
|
70
|
+
}
|
|
55
71
|
/**
|
|
56
72
|
* Gets all print jobs for a printer with enhanced details
|
|
57
73
|
*/
|
|
@@ -66,17 +82,21 @@ async function getAllJobs(printerName) {
|
|
|
66
82
|
const result = JSON.parse(output);
|
|
67
83
|
// Handle single result vs array
|
|
68
84
|
const jobs = Array.isArray(result) ? result : (result ? [result] : []);
|
|
69
|
-
return jobs.map((job) =>
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
85
|
+
return jobs.map((job) => {
|
|
86
|
+
// Get custom job name from mapper
|
|
87
|
+
const customName = (0, job_name_mapper_1.getCustomJobName)(printerName, job.Id);
|
|
88
|
+
return {
|
|
89
|
+
jobId: job.Id,
|
|
90
|
+
jobName: job.DocumentName || '',
|
|
91
|
+
customJobName: customName || job.DocumentName || '', // Use custom name if available
|
|
92
|
+
printerName: printerName,
|
|
93
|
+
status: job.JobStatus || 'Unknown',
|
|
94
|
+
pages: job.TotalPages || 0,
|
|
95
|
+
size: job.Size,
|
|
96
|
+
userName: job.UserName,
|
|
97
|
+
submittedTime: job.SubmittedTime
|
|
98
|
+
};
|
|
99
|
+
});
|
|
80
100
|
}
|
|
81
101
|
catch (error) {
|
|
82
102
|
return [];
|
package/lib/print-manager.d.ts
CHANGED
|
@@ -12,19 +12,14 @@ export declare function getPrinters(): Promise<PrinterInfo[]>;
|
|
|
12
12
|
*/
|
|
13
13
|
export declare function getDefaultPrinter(): Promise<string | null>;
|
|
14
14
|
/**
|
|
15
|
-
* Gets information about a
|
|
15
|
+
* Gets detailed information about a print job using custom job name
|
|
16
16
|
*/
|
|
17
|
-
export declare function
|
|
17
|
+
export declare function getJobInfo(customJobName: string, printerName?: string): Promise<JobInfo | null>;
|
|
18
18
|
/**
|
|
19
19
|
* Gets all print jobs for a specific printer
|
|
20
20
|
*/
|
|
21
|
-
export declare function getPrinterJobs(printerName: string): Promise<JobInfo[]>;
|
|
22
|
-
/**
|
|
23
|
-
* Gets all print jobs with custom job names for a specific printer
|
|
24
|
-
* This is an alias for getPrinterJobs with clearer intent
|
|
25
|
-
*/
|
|
26
21
|
export declare function getJobs(printerName: string): Promise<JobInfo[]>;
|
|
27
22
|
/**
|
|
28
|
-
* Cancels a print job
|
|
23
|
+
* Cancels a print job using custom job name
|
|
29
24
|
*/
|
|
30
|
-
export declare function cancelJob(
|
|
25
|
+
export declare function cancelJob(customJobName: string, printerName?: string): Promise<boolean>;
|
package/lib/print-manager.js
CHANGED
|
@@ -3,14 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.print = print;
|
|
4
4
|
exports.getPrinters = getPrinters;
|
|
5
5
|
exports.getDefaultPrinter = getDefaultPrinter;
|
|
6
|
-
exports.
|
|
7
|
-
exports.getPrinterJobs = getPrinterJobs;
|
|
6
|
+
exports.getJobInfo = getJobInfo;
|
|
8
7
|
exports.getJobs = getJobs;
|
|
9
8
|
exports.cancelJob = cancelJob;
|
|
10
9
|
const pdf_to_printer_1 = require("pdf-to-printer");
|
|
11
10
|
const uuid_1 = require("uuid");
|
|
12
11
|
const child_process_1 = require("child_process");
|
|
13
12
|
const job_tracker_1 = require("./job-tracker");
|
|
13
|
+
const job_name_mapper_1 = require("./job-name-mapper");
|
|
14
14
|
/**
|
|
15
15
|
* Prints a PDF file with the specified options
|
|
16
16
|
*/
|
|
@@ -64,11 +64,15 @@ async function print(options) {
|
|
|
64
64
|
printOptions.subset = options.subset;
|
|
65
65
|
}
|
|
66
66
|
// Set the document name for job tracking
|
|
67
|
-
printOptions.win32 = ['-print-settings', `${
|
|
67
|
+
printOptions.win32 = ['-print-settings', `${options.filePath}`];
|
|
68
68
|
// Print the document
|
|
69
69
|
await (0, pdf_to_printer_1.print)(options.filePath, printOptions);
|
|
70
70
|
// Try to retrieve job ID
|
|
71
|
-
const jobId = await (0, job_tracker_1.getJobIdByName)(options.printerName,
|
|
71
|
+
const jobId = await (0, job_tracker_1.getJobIdByName)(options.printerName, options.filePath);
|
|
72
|
+
// Store custom job name mapping if we got a job ID
|
|
73
|
+
if (jobId) {
|
|
74
|
+
(0, job_name_mapper_1.storeJobName)(options.printerName, jobId, jobName);
|
|
75
|
+
}
|
|
72
76
|
return {
|
|
73
77
|
success: true,
|
|
74
78
|
jobId: jobId || undefined,
|
|
@@ -115,30 +119,28 @@ async function getDefaultPrinter() {
|
|
|
115
119
|
return defaultPrinter ? defaultPrinter.name : null;
|
|
116
120
|
}
|
|
117
121
|
/**
|
|
118
|
-
* Gets information about a
|
|
122
|
+
* Gets detailed information about a print job using custom job name
|
|
119
123
|
*/
|
|
120
|
-
async function
|
|
121
|
-
return (0, job_tracker_1.getJobInfo)(
|
|
124
|
+
async function getJobInfo(customJobName, printerName) {
|
|
125
|
+
return (0, job_tracker_1.getJobInfo)(customJobName, printerName);
|
|
122
126
|
}
|
|
123
127
|
/**
|
|
124
128
|
* Gets all print jobs for a specific printer
|
|
125
129
|
*/
|
|
126
|
-
async function getPrinterJobs(printerName) {
|
|
127
|
-
return (0, job_tracker_1.getAllJobs)(printerName);
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Gets all print jobs with custom job names for a specific printer
|
|
131
|
-
* This is an alias for getPrinterJobs with clearer intent
|
|
132
|
-
*/
|
|
133
130
|
async function getJobs(printerName) {
|
|
134
131
|
return (0, job_tracker_1.getAllJobs)(printerName);
|
|
135
132
|
}
|
|
136
133
|
/**
|
|
137
|
-
* Cancels a print job
|
|
134
|
+
* Cancels a print job using custom job name
|
|
138
135
|
*/
|
|
139
|
-
async function cancelJob(
|
|
136
|
+
async function cancelJob(customJobName, printerName) {
|
|
137
|
+
// Resolve custom job name to system job ID
|
|
138
|
+
const mapping = (0, job_name_mapper_1.getSystemJobId)(customJobName, printerName);
|
|
139
|
+
if (!mapping) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
140
142
|
const script = `
|
|
141
|
-
Remove-PrintJob -PrinterName "${escapePowerShellString(printerName)}" -ID ${jobId}
|
|
143
|
+
Remove-PrintJob -PrinterName "${escapePowerShellString(mapping.printerName)}" -ID ${mapping.jobId}
|
|
142
144
|
`;
|
|
143
145
|
try {
|
|
144
146
|
await executePowerShell(script);
|
package/package.json
CHANGED