Skip to content

Commit

Permalink
Merge pull request #2 from Susheer/other-interfaces
Browse files Browse the repository at this point in the history
Other non-functional interfaces
  • Loading branch information
Susheer authored Nov 12, 2024
2 parents 7dadbf2 + da38f05 commit 3cc2731
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 8 deletions.
130 changes: 127 additions & 3 deletions binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ module.exports.sayMyName = addon.SayMyName
module.exports.getPrinters = addon.getPrinters
module.exports.printDirect = printDirect
module.exports.getDefaultPrinterName = addon.getDefaultPrinterName


module.exports.getPrinter = getPrinter;
/// send file to printer
module.exports.printFile = printFile;
/** Get supported print format for printDirect
*/
module.exports.getSupportedPrintFormats = addon.getSupportedPrintFormats;
/*
print raw data. This function is intend to be asynchronous
Expand Down Expand Up @@ -117,4 +121,124 @@ module.exports.getDefaultPrinterName = addon.getDefaultPrinterName
}else{
error("Not supported");
}
}
}

/** Get printer info with jobs
* @param printerName printer name to extract the info
* @return printer object info:
* TODO: to enum all possible attributes
*/
function getPrinter(printerName)
{
if(!printerName) {
printerName = addon.getDefaultPrinterName();
}
var printer = addon.getPrinter(printerName);
correctPrinterinfo(printer);
return printer;
}


function correctPrinterinfo(printer) {
if(printer.status || !printer.options || !printer.options['printer-state']){
return;
}

var status = printer.options['printer-state'];
// Add posix status
if(status == '3'){
status = 'IDLE'
}
else if(status == '4'){
status = 'PRINTING'
}
else if(status == '5'){
status = 'STOPPED'
}

// correct date type
var k;
for(k in printer.options) {
if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
printer.options[k] = new Date(printer.options[k] * 1000);
}
}

printer.status = status;
}

/**
parameters:
parameters - Object, parameters objects with the following structure:
filename - String, mandatory, data to printer
docname - String, optional, name of document showed in printer status
printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
*/
function printFile(parameters){
var filename,
docname,
printer,
options,
success,
error;

if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
throw new Error('must provide arguments object');
}

filename = parameters.filename;
docname = parameters.docname;
printer = parameters.printer;
options = parameters.options || {};
success = parameters.success;
error = parameters.error;

if(!success){
success = function(){};
}

if(!error){
error = function(err){
throw err;
};
}

if(!filename){
var err = new Error('must provide at least a filename');
return error(err);
}

// try to define default printer name
if(!printer) {
printer = addon.getDefaultPrinterName();
}

if(!printer) {
return error(new Error('Printer parameter of default printer is not defined'));
}

// set filename if docname is missing
if(!docname){
docname = filename;
}

//TODO: check parameters type
if(addon.printFile){// call C++ binding
try{
// TODO: proper success/error callbacks from the extension
var res = addon.printFile(filename, docname, printer, options);

if(!isNaN(parseInt(res))) {
success(res);
} else {
error(Error(res));
}
} catch (e) {
error(e);
}
} else {
error("Not supported");
}
}
130 changes: 127 additions & 3 deletions lib/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ module.exports.sayMyName = addon.SayMyName
module.exports.getPrinters = addon.getPrinters
module.exports.printDirect = printDirect
module.exports.getDefaultPrinterName = addon.getDefaultPrinterName


module.exports.getPrinter = getPrinter;
/// send file to printer
module.exports.printFile = printFile;
/** Get supported print format for printDirect
*/
module.exports.getSupportedPrintFormats = addon.getSupportedPrintFormats;
/*
print raw data. This function is intend to be asynchronous
Expand Down Expand Up @@ -117,4 +121,124 @@ module.exports.getDefaultPrinterName = addon.getDefaultPrinterName
}else{
error("Not supported");
}
}
}

/** Get printer info with jobs
* @param printerName printer name to extract the info
* @return printer object info:
* TODO: to enum all possible attributes
*/
function getPrinter(printerName)
{
if(!printerName) {
printerName = addon.getDefaultPrinterName();
}
var printer = addon.getPrinter(printerName);
correctPrinterinfo(printer);
return printer;
}


function correctPrinterinfo(printer) {
if(printer.status || !printer.options || !printer.options['printer-state']){
return;
}

var status = printer.options['printer-state'];
// Add posix status
if(status == '3'){
status = 'IDLE'
}
else if(status == '4'){
status = 'PRINTING'
}
else if(status == '5'){
status = 'STOPPED'
}

// correct date type
var k;
for(k in printer.options) {
if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
printer.options[k] = new Date(printer.options[k] * 1000);
}
}

printer.status = status;
}

/**
parameters:
parameters - Object, parameters objects with the following structure:
filename - String, mandatory, data to printer
docname - String, optional, name of document showed in printer status
printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
success - Function, optional, callback function
error - Function, optional, callback function if exists any error
*/
function printFile(parameters){
var filename,
docname,
printer,
options,
success,
error;

if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
throw new Error('must provide arguments object');
}

filename = parameters.filename;
docname = parameters.docname;
printer = parameters.printer;
options = parameters.options || {};
success = parameters.success;
error = parameters.error;

if(!success){
success = function(){};
}

if(!error){
error = function(err){
throw err;
};
}

if(!filename){
var err = new Error('must provide at least a filename');
return error(err);
}

// try to define default printer name
if(!printer) {
printer = addon.getDefaultPrinterName();
}

if(!printer) {
return error(new Error('Printer parameter of default printer is not defined'));
}

// set filename if docname is missing
if(!docname){
docname = filename;
}

//TODO: check parameters type
if(addon.printFile){// call C++ binding
try{
// TODO: proper success/error callbacks from the extension
var res = addon.printFile(filename, docname, printer, options);

if(!isNaN(parseInt(res))) {
success(res);
} else {
error(Error(res));
}
} catch (e) {
error(e);
}
} else {
error("Not supported");
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@thesusheer/electron-printer",
"description": "Node API (N-API) supported electron.s || node.js printer bindings",
"homepage": "https://github.com/Susheer/electron-printer",
"version": "2.0.1",
"version": "2.0.2",
"main": "./lib/index.js",
"types": "types/index.d.ts",
"dependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/node_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) {
MY_MODULE_SET_METHOD(exports, "SayMyName", SayMyName);
MY_MODULE_SET_METHOD(exports, "getPrinters", getPrinters);
MY_MODULE_SET_METHOD(exports, "getDefaultPrinterName", getDefaultPrinterName);
MY_MODULE_SET_METHOD(exports, "printDirect", printDirect);
MY_MODULE_SET_METHOD(exports, "printDirect", printDirect);
MY_MODULE_SET_METHOD(exports, "getPrinter", getPrinter);
MY_MODULE_SET_METHOD(exports, "printFile", printFile);
MY_MODULE_SET_METHOD(exports, "getSupportedPrintFormats", getSupportedPrintFormats);
return exports;
}

Expand Down
4 changes: 4 additions & 0 deletions src/node_printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ MY_NODE_MODULE_CALLBACK(SayMyName);
MY_NODE_MODULE_CALLBACK(getPrinters);
MY_NODE_MODULE_CALLBACK(getDefaultPrinterName);
MY_NODE_MODULE_CALLBACK(printDirect);
MY_NODE_MODULE_CALLBACK(getPrinter);
MY_NODE_MODULE_CALLBACK(printFile);
MY_NODE_MODULE_CALLBACK(getSupportedPrintFormats);



/**
Expand Down
19 changes: 19 additions & 0 deletions src/node_printer_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,23 @@ MY_NODE_MODULE_CALLBACK(printDirect)
}

MY_NODE_MODULE_RETURN_VALUE(Napi::Number::New(env, dwJob));
}


MY_NODE_MODULE_CALLBACK(getPrinter)
{
MY_NODE_MODULE_HANDLESCOPE;
MY_NODE_MODULE_RETURN_VALUE(NAPI_STRING_NEW_UTF8(MY_NODE_MODULE_ENV, "getPrinter is not implemented yet, request for a PR if required"));
}

MY_NODE_MODULE_CALLBACK(printFile)
{
MY_NODE_MODULE_HANDLESCOPE;
RETURN_EXCEPTION(MY_NODE_MODULE_ENV, "Not yet implemented on Windows");
}

MY_NODE_MODULE_CALLBACK(getSupportedPrintFormats)
{
MY_NODE_MODULE_HANDLESCOPE;
RETURN_EXCEPTION(MY_NODE_MODULE_ENV, "Not yet implemented, Request for a PR");
}
56 changes: 56 additions & 0 deletions test/incompleteFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const assert = require('assert/strict');
const { test, beforeEach } = require('node:test');

let addon;

beforeEach(() => {
addon = require('../lib');
});

test('Ensure Non-functional apis do not lead any issues', (t) => {
assert.ok(addon, 'Non-functional apis');
// Add checks for specific functions if needed
assert.strictEqual(typeof addon.getPrinter, 'function', 'Addon should have getPrinter');
});

// Additional test cases
test('Test getPrinter behavior', (t) => {
const result = addon.getPrinter();
assert.strictEqual(result, 'getPrinter is not implemented yet, request for a PR if required', 'getPrinter is not implemented yet, request for a PR if required"');
});

test('Test printFile behavior', (t) => {
const filename = process.argv[2] || __filename; // use this file as an input
if( process.platform != 'win32') {
addon.printFile({filename:filename,
printer: process.env[3], // printer name, if missing then will print to default printer
success:function(jobID){
console.log("addon.printFile: Job ID: "+jobID);
},
error:function(err){
console.log(err);
}
});
} else {
// not yet implemented, use printDirect and text
var fs = require('fs');
addon.printDirect({data:fs.readFileSync(filename),
printer: addon.getDefaultPrinterName(), // printer name, if missing then will print to default printer
type: "RAW",
success:function(jobID){
assert.equal(jobID>0, true, 'addon.printFile');
},
error:function(err){
console.log(err);
assert.strictEqual(!err, true, 'addon.printFile'); // failed test case
}
});
}
});

// getSupportedPrintFormats
test('Test getSupportedPrintFormats behavior', (t) => {
assert.throws(() => {
addon.getSupportedPrintFormats();
}, /Error/, 'getSupportedPrintFormats should throw an error when no arguments are passed');
});

0 comments on commit 3cc2731

Please sign in to comment.