@esslassi/electron-printer 0.0.3 → 0.0.6
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/README.md +222 -53
- package/binding.gyp +51 -33
- package/lib/electronPrinter.d.ts +33 -0
- package/lib/electronPrinter.js +38 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +17 -1
- package/package.json +47 -33
- package/src/linux_printer.cpp +171 -0
- package/src/linux_printer.h +21 -0
- package/src/mac_printer.cpp +166 -0
- package/src/mac_printer.h +22 -0
- package/src/main.cpp +22 -0
- package/src/print.cpp +272 -0
- package/src/printer_factory.cpp +20 -0
- package/src/printer_factory.h +13 -0
- package/src/printer_interface.h +29 -0
- package/src/windows_printer.cpp +210 -0
- package/src/windows_printer.h +35 -0
- package/.gitattributes +0 -2
- package/Gruntfile.js +0 -80
- package/LICENSE +0 -21
- package/binding.js +0 -244
- package/index.js +0 -1
- package/lib/binding.js +0 -244
- package/printer.js +0 -1
- package/src/hello_world.cc +0 -8
- package/src/macros.hh +0 -53
- package/src/node_printer.cc +0 -31
- package/src/node_printer.hpp +0 -77
- package/src/node_printer_win.cc +0 -586
- package/test/getDefaultPrinterName.test.js +0 -18
- package/test/getPrinters.test.js +0 -26
- package/test/incompleteFunctions.js +0 -56
- package/test/printDirect.test.js +0 -34
- package/test/sayMyName.test.js +0 -21
- package/tools/buildElectronLinux.sh +0 -10
- package/tools/buildElectronWindows.ps1 +0 -20
- package/tools/buildWindows.ps1 +0 -23
- package/tools/generateReleaseBuildsLinux.sh +0 -59
- package/tools/generateReleaseBuildsWindows.ps1 +0 -63
- package/tools/getSourceFiles.py +0 -12
- package/tools/remove_directory.py +0 -27
- package/tsconfig.json +0 -22
- package/types/index.d.ts +0 -56
package/lib/binding.js
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
const path = require('path');
|
|
2
|
-
const os = require('os');
|
|
3
|
-
let addon = {}, binary_path;
|
|
4
|
-
switch (os.platform()) {
|
|
5
|
-
case 'win32':
|
|
6
|
-
if (os.arch() === 'ia32') {
|
|
7
|
-
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
8
|
-
} else if (os.arch() === 'x64') {
|
|
9
|
-
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
10
|
-
}
|
|
11
|
-
addon = require(binary_path);
|
|
12
|
-
break;
|
|
13
|
-
case 'darwin':
|
|
14
|
-
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
15
|
-
addon = require(binary_path);
|
|
16
|
-
break;
|
|
17
|
-
case 'linux':
|
|
18
|
-
if (os.arch() === 'ia32') {
|
|
19
|
-
addon = path.join(__dirname, 'electron-printer.node');
|
|
20
|
-
} else if (os.arch() === 'x64') {
|
|
21
|
-
addon = path.join(__dirname, 'electron-printer.node');
|
|
22
|
-
}
|
|
23
|
-
break;
|
|
24
|
-
default:
|
|
25
|
-
binary_path = path.join(__dirname, 'electron-printer.node');
|
|
26
|
-
addon = require(binary_path);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
module.exports.sayMyName = addon.SayMyName
|
|
30
|
-
module.exports.getPrinters = addon.getPrinters
|
|
31
|
-
module.exports.printDirect = printDirect
|
|
32
|
-
module.exports.getDefaultPrinterName = addon.getDefaultPrinterName
|
|
33
|
-
module.exports.getPrinter = getPrinter;
|
|
34
|
-
/// send file to printer
|
|
35
|
-
module.exports.printFile = printFile;
|
|
36
|
-
/** Get supported print format for printDirect
|
|
37
|
-
*/
|
|
38
|
-
module.exports.getSupportedPrintFormats = addon.getSupportedPrintFormats;
|
|
39
|
-
/*
|
|
40
|
-
print raw data. This function is intend to be asynchronous
|
|
41
|
-
|
|
42
|
-
parameters:
|
|
43
|
-
parameters - Object, parameters objects with the following structure:
|
|
44
|
-
data - String, mandatory, data to printer
|
|
45
|
-
printer - String, optional, name of the printer, if missing, will try to print to default printer
|
|
46
|
-
docname - String, optional, name of document showed in printer status
|
|
47
|
-
type - String, optional, only for wind32, data type, one of the RAW, TEXT
|
|
48
|
-
options - JS object with CUPS options, optional
|
|
49
|
-
success - Function, optional, callback function
|
|
50
|
-
error - Function, optional, callback function if exists any error
|
|
51
|
-
|
|
52
|
-
or
|
|
53
|
-
|
|
54
|
-
data - String, mandatory, data to printer
|
|
55
|
-
printer - String, optional, name of the printer, if missing, will try to print to default printer
|
|
56
|
-
docname - String, optional, name of document showed in printer status
|
|
57
|
-
type - String, optional, data type, one of the RAW, TEXT
|
|
58
|
-
options - JS object with CUPS options, optional
|
|
59
|
-
success - Function, optional, callback function with first argument job_id
|
|
60
|
-
error - Function, optional, callback function if exists any error
|
|
61
|
-
*/
|
|
62
|
-
function printDirect(parameters){
|
|
63
|
-
var data = parameters
|
|
64
|
-
, printer
|
|
65
|
-
, docname
|
|
66
|
-
, type
|
|
67
|
-
, options
|
|
68
|
-
, success
|
|
69
|
-
, error;
|
|
70
|
-
|
|
71
|
-
if(arguments.length==1){
|
|
72
|
-
//TODO: check parameters type
|
|
73
|
-
//if (typeof parameters )
|
|
74
|
-
data = parameters.data;
|
|
75
|
-
printer = parameters.printer;
|
|
76
|
-
docname = parameters.docname;
|
|
77
|
-
type = parameters.type;
|
|
78
|
-
options = parameters.options||{};
|
|
79
|
-
success = parameters.success;
|
|
80
|
-
error = parameters.error;
|
|
81
|
-
}else{
|
|
82
|
-
printer = arguments[1];
|
|
83
|
-
type = arguments[2];
|
|
84
|
-
docname = arguments[3];
|
|
85
|
-
options = arguments[4];
|
|
86
|
-
success = arguments[5];
|
|
87
|
-
error = arguments[6];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if(!type){
|
|
91
|
-
type = "RAW";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Set default printer name
|
|
95
|
-
if(!printer) {
|
|
96
|
-
printer = addon.getDefaultPrinterName();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
type = type.toUpperCase();
|
|
100
|
-
|
|
101
|
-
if(!docname){
|
|
102
|
-
docname = "node print job";
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!options){
|
|
106
|
-
options = {};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
//TODO: check parameters type
|
|
110
|
-
if(addon.printDirect){// call C++ binding
|
|
111
|
-
try{
|
|
112
|
-
var res = addon.printDirect(data, printer, docname, type, options);
|
|
113
|
-
if(res){
|
|
114
|
-
success(res);
|
|
115
|
-
}else{
|
|
116
|
-
error(Error("Something wrong in printDirect"));
|
|
117
|
-
}
|
|
118
|
-
}catch (e){
|
|
119
|
-
error(e);
|
|
120
|
-
}
|
|
121
|
-
}else{
|
|
122
|
-
error("Not supported");
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Get printer info with jobs
|
|
127
|
-
* @param printerName printer name to extract the info
|
|
128
|
-
* @return printer object info:
|
|
129
|
-
* TODO: to enum all possible attributes
|
|
130
|
-
*/
|
|
131
|
-
function getPrinter(printerName)
|
|
132
|
-
{
|
|
133
|
-
if(!printerName) {
|
|
134
|
-
printerName = addon.getDefaultPrinterName();
|
|
135
|
-
}
|
|
136
|
-
var printer = addon.getPrinter(printerName);
|
|
137
|
-
correctPrinterinfo(printer);
|
|
138
|
-
return printer;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
function correctPrinterinfo(printer) {
|
|
143
|
-
if(printer.status || !printer.options || !printer.options['printer-state']){
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
var status = printer.options['printer-state'];
|
|
148
|
-
// Add posix status
|
|
149
|
-
if(status == '3'){
|
|
150
|
-
status = 'IDLE'
|
|
151
|
-
}
|
|
152
|
-
else if(status == '4'){
|
|
153
|
-
status = 'PRINTING'
|
|
154
|
-
}
|
|
155
|
-
else if(status == '5'){
|
|
156
|
-
status = 'STOPPED'
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// correct date type
|
|
160
|
-
var k;
|
|
161
|
-
for(k in printer.options) {
|
|
162
|
-
if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
|
|
163
|
-
printer.options[k] = new Date(printer.options[k] * 1000);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
printer.status = status;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
parameters:
|
|
172
|
-
parameters - Object, parameters objects with the following structure:
|
|
173
|
-
filename - String, mandatory, data to printer
|
|
174
|
-
docname - String, optional, name of document showed in printer status
|
|
175
|
-
printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
|
|
176
|
-
success - Function, optional, callback function
|
|
177
|
-
error - Function, optional, callback function if exists any error
|
|
178
|
-
*/
|
|
179
|
-
function printFile(parameters){
|
|
180
|
-
var filename,
|
|
181
|
-
docname,
|
|
182
|
-
printer,
|
|
183
|
-
options,
|
|
184
|
-
success,
|
|
185
|
-
error;
|
|
186
|
-
|
|
187
|
-
if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
|
|
188
|
-
throw new Error('must provide arguments object');
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
filename = parameters.filename;
|
|
192
|
-
docname = parameters.docname;
|
|
193
|
-
printer = parameters.printer;
|
|
194
|
-
options = parameters.options || {};
|
|
195
|
-
success = parameters.success;
|
|
196
|
-
error = parameters.error;
|
|
197
|
-
|
|
198
|
-
if(!success){
|
|
199
|
-
success = function(){};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
if(!error){
|
|
203
|
-
error = function(err){
|
|
204
|
-
throw err;
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
if(!filename){
|
|
209
|
-
var err = new Error('must provide at least a filename');
|
|
210
|
-
return error(err);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// try to define default printer name
|
|
214
|
-
if(!printer) {
|
|
215
|
-
printer = addon.getDefaultPrinterName();
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if(!printer) {
|
|
219
|
-
return error(new Error('Printer parameter of default printer is not defined'));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
// set filename if docname is missing
|
|
223
|
-
if(!docname){
|
|
224
|
-
docname = filename;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
//TODO: check parameters type
|
|
228
|
-
if(addon.printFile){// call C++ binding
|
|
229
|
-
try{
|
|
230
|
-
// TODO: proper success/error callbacks from the extension
|
|
231
|
-
var res = addon.printFile(filename, docname, printer, options);
|
|
232
|
-
|
|
233
|
-
if(!isNaN(parseInt(res))) {
|
|
234
|
-
success(res);
|
|
235
|
-
} else {
|
|
236
|
-
error(Error(res));
|
|
237
|
-
}
|
|
238
|
-
} catch (e) {
|
|
239
|
-
error(e);
|
|
240
|
-
}
|
|
241
|
-
} else {
|
|
242
|
-
error("Not supported");
|
|
243
|
-
}
|
|
244
|
-
}
|
package/printer.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('./lib/binding');
|
package/src/hello_world.cc
DELETED
package/src/macros.hh
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
#ifndef NODE_PRINTER_SRC_MACROS_H
|
|
2
|
-
#define NODE_PRINTER_SRC_MACROS_H
|
|
3
|
-
#include <napi.h>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
#define MY_NODE_MODULE_ENV_DECL Napi::Env env = info.Env();
|
|
7
|
-
#define MY_NODE_MODULE_ENV env
|
|
8
|
-
#define MY_NODE_MODULE_HANDLESCOPE MY_NODE_MODULE_ENV_DECL Napi::HandleScope scope(env);
|
|
9
|
-
|
|
10
|
-
#define MY_MODULE_SET_METHOD(exports, name, method) \
|
|
11
|
-
(exports).Set(Napi::String::New(env, name), Napi::Function::New(env, method))
|
|
12
|
-
|
|
13
|
-
#define MY_NODE_MODULE_CALLBACK(name) Napi::Value name(const Napi::CallbackInfo& info)
|
|
14
|
-
|
|
15
|
-
#define MY_NODE_MODULE_RETURN_VALUE(value) \
|
|
16
|
-
return value;
|
|
17
|
-
|
|
18
|
-
#define NAPI_STRING_NEW_UTF8(env, value) Napi::String::New(env, value)
|
|
19
|
-
|
|
20
|
-
#define ADD_NAPI_NUMBER_PROPERTY(obj, name, value) \
|
|
21
|
-
obj.Set(Napi::String::New(obj.Env(), name), Napi::Number::New(obj.Env(), value))
|
|
22
|
-
|
|
23
|
-
#define NAPI_STRING_NEW_2BYTES(env, value) Napi::String::New(env, (char16_t*)value)
|
|
24
|
-
|
|
25
|
-
#define ADD_NAPI_STRING_PROPERTY(obj, name, key) \
|
|
26
|
-
if ((job->key != NULL) && (*job->key != L'\0')) { \
|
|
27
|
-
obj.Set(Napi::String::New(obj.Env(), name), Napi::String::New(obj.Env(), (char16_t*)job->key)); \
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
#define RETURN_EXCEPTION(env, msg) \
|
|
31
|
-
Napi::Error::New(env, msg).ThrowAsJavaScriptException(); \
|
|
32
|
-
return env.Null(); // Ensure a Napi::Value is returned
|
|
33
|
-
|
|
34
|
-
#define RETURN_EXCEPTION_STR(env, msg) \
|
|
35
|
-
RETURN_EXCEPTION(env, msg)
|
|
36
|
-
|
|
37
|
-
#define REQUIRE_ARGUMENTS(env, args, n) \
|
|
38
|
-
if (args.Length() < (n)) { \
|
|
39
|
-
RETURN_EXCEPTION_STR(env, "Expected " #n " arguments"); \
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
#define ARG_CHECK_STRING(env, args, i) \
|
|
43
|
-
if ((args).Length() <= (i) || !(args)[(i)].IsString()) { \
|
|
44
|
-
RETURN_EXCEPTION_STR(env, "Argument " #i " must be a string"); \
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
#define REQUIRE_ARGUMENT_STRINGW(env, args, i, var) \
|
|
48
|
-
ARG_CHECK_STRING(env, args, i); \
|
|
49
|
-
std::wstring var = (args)[(i)].As<Napi::String>().Utf16Value();
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
#endif
|
|
53
|
-
|
package/src/node_printer.cc
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
#include "node_printer.hpp"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
|
6
|
-
MY_MODULE_SET_METHOD(exports, "SayMyName", SayMyName);
|
|
7
|
-
MY_MODULE_SET_METHOD(exports, "getPrinters", getPrinters);
|
|
8
|
-
MY_MODULE_SET_METHOD(exports, "getDefaultPrinterName", getDefaultPrinterName);
|
|
9
|
-
MY_MODULE_SET_METHOD(exports, "printDirect", printDirect);
|
|
10
|
-
MY_MODULE_SET_METHOD(exports, "getPrinter", getPrinter);
|
|
11
|
-
MY_MODULE_SET_METHOD(exports, "printFile", printFile);
|
|
12
|
-
MY_MODULE_SET_METHOD(exports, "getSupportedPrintFormats", getSupportedPrintFormats);
|
|
13
|
-
return exports;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
NODE_API_MODULE(addon, Init)
|
|
17
|
-
|
|
18
|
-
// Helpers
|
|
19
|
-
|
|
20
|
-
bool getStringOrBufferFromNapiValue(const Napi::Value& value, std::string& oData) {
|
|
21
|
-
if (value.IsString()) {
|
|
22
|
-
oData = value.As<Napi::String>().Utf8Value();
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
if (value.IsBuffer()) {
|
|
26
|
-
Napi::Buffer<char> buffer = value.As<Napi::Buffer<char>>();
|
|
27
|
-
oData.assign(buffer.Data(), buffer.Length());
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return false;
|
|
31
|
-
}
|
package/src/node_printer.hpp
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
#ifndef NODE_PRINTER_HPP
|
|
2
|
-
#define NODE_PRINTER_HPP
|
|
3
|
-
#include "macros.hh"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
MY_NODE_MODULE_CALLBACK(SayMyName);
|
|
7
|
-
MY_NODE_MODULE_CALLBACK(getPrinters);
|
|
8
|
-
MY_NODE_MODULE_CALLBACK(getDefaultPrinterName);
|
|
9
|
-
MY_NODE_MODULE_CALLBACK(printDirect);
|
|
10
|
-
MY_NODE_MODULE_CALLBACK(getPrinter);
|
|
11
|
-
MY_NODE_MODULE_CALLBACK(printFile);
|
|
12
|
-
MY_NODE_MODULE_CALLBACK(getSupportedPrintFormats);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @brief A base class template for managing a pointer to a value of type Type.
|
|
18
|
-
*
|
|
19
|
-
* This class provides basic functionality for managing a pointer to a value,
|
|
20
|
-
* including construction, destruction, and access methods.
|
|
21
|
-
*
|
|
22
|
-
* @tparam Type The type of the value being managed.
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
template<typename Type>
|
|
26
|
-
class MemValueBase
|
|
27
|
-
{
|
|
28
|
-
public:
|
|
29
|
-
/**
|
|
30
|
-
* @brief Default constructor. Initializes the pointer to NULL.
|
|
31
|
-
*/
|
|
32
|
-
MemValueBase(): _value(NULL) {}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* @brief Virtual destructor. The allocated memory will be deallocated.
|
|
36
|
-
*/
|
|
37
|
-
virtual ~MemValueBase() {}
|
|
38
|
-
/**
|
|
39
|
-
* @brief Gets the pointer to the value.
|
|
40
|
-
*
|
|
41
|
-
* @return A pointer to the value of type Type.
|
|
42
|
-
*/
|
|
43
|
-
Type * get() {return _value; }
|
|
44
|
-
/**
|
|
45
|
-
* @brief Overloaded arrow operator to access the value.
|
|
46
|
-
*
|
|
47
|
-
* @return A pointer to the value of type Type.
|
|
48
|
-
*/
|
|
49
|
-
Type * operator ->() { return &_value; }
|
|
50
|
-
/**
|
|
51
|
-
* @brief Conversion operator to check if the pointer is not NULL.
|
|
52
|
-
*
|
|
53
|
-
* @return True if the pointer is not NULL, false otherwise.
|
|
54
|
-
*/
|
|
55
|
-
operator bool() const { return (_value != NULL); }
|
|
56
|
-
protected:
|
|
57
|
-
Type *_value; /**< Pointer to the value of type Type. */
|
|
58
|
-
/**
|
|
59
|
-
* @brief Virtual method to free the allocated memory.
|
|
60
|
-
*
|
|
61
|
-
* This method can be overridden by derived classes to provide custom
|
|
62
|
-
* memory deallocation logic.
|
|
63
|
-
*/
|
|
64
|
-
virtual void free() {};
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Try to extract a string or buffer from a N-API value.
|
|
69
|
-
* @param value - source N-API value
|
|
70
|
-
* @param oData - destination data
|
|
71
|
-
* @return TRUE if value is a string or buffer, FALSE otherwise
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
bool getStringOrBufferFromNapiValue(const Napi::Value& value, std::string& oData);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
#endif
|