@xiaoerge/node-printer 0.0.0-stage → 1.0.2

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/Gruntfile.js ADDED
@@ -0,0 +1,75 @@
1
+ module.exports = function(grunt) {
2
+ grunt.initConfig({
3
+ gyp: {
4
+ ia32: {
5
+ command: 'rebuild',
6
+ options: {
7
+ arch: 'ia32'
8
+ }
9
+ },
10
+ x64: {
11
+ command: 'rebuild',
12
+ options: {
13
+ arch: 'x64'
14
+ }
15
+ }
16
+ },
17
+ "nw-gyp": {
18
+ ia32: {
19
+ command: 'rebuild',
20
+ options: {
21
+ arch: 'ia32'
22
+ }
23
+ },
24
+ x64: {
25
+ command: 'rebuild',
26
+ options: {
27
+ arch: 'x64'
28
+ }
29
+ }
30
+ },
31
+ copy: {
32
+ ia32: {
33
+ files: [{src: 'build/Release/node_printer.node', dest: 'lib/node_printer_' + process.platform + '_ia32.node'}]
34
+ },
35
+ x64: {
36
+ files: [{src: 'build/Release/node_printer.node', dest: 'lib/node_printer_' + process.platform + '_x64.node'}]
37
+ }
38
+ }
39
+ });
40
+
41
+ grunt.loadNpmTasks('grunt-contrib-jshint');
42
+ grunt.loadNpmTasks('grunt-node-gyp');
43
+ grunt.loadNpmTasks('grunt-nw-gyp');
44
+ grunt.loadNpmTasks('grunt-contrib-copy');
45
+
46
+ grunt.registerTask('build-nw-ia32', [
47
+ 'nw-gyp:ia32',
48
+ 'copy:ia32'
49
+ ]);
50
+
51
+ grunt.registerTask('build-ia32', [
52
+ 'gyp:ia32',
53
+ 'copy:ia32'
54
+ ]);
55
+
56
+ grunt.registerTask('build-x64', [
57
+ 'gyp:x64',
58
+ 'copy:x64'
59
+ ]);
60
+
61
+ grunt.registerTask('build-nw-x64', [
62
+ 'nw-gyp:x64',
63
+ 'copy:x64'
64
+ ]);
65
+
66
+ grunt.registerTask('build', [
67
+ 'build-ia32',
68
+ 'build-x64'
69
+ ]);
70
+
71
+ grunt.registerTask('build-nw', [
72
+ 'build-nw-ia32',
73
+ 'build-nw-x64'
74
+ ]);
75
+ }
package/README.md CHANGED
@@ -1,3 +1,97 @@
1
- # Temporary Holding Version
1
+ # @xiaoerge/node-printer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Native printing bindings for Node.js and Electron. Wraps the Windows print APIs
4
+ and the POSIX ones via [CUPS](http://cups.org/).
5
+
6
+ **This is a temporary fork of
7
+ [niick555/node-printer](https://github.com/niick555/node-printer)**, published so
8
+ that an Electron 45 prebuild is available while the upstream repository does not
9
+ yet ship one. See [Why this fork exists](#why-this-fork-exists).
10
+
11
+ ## Installing
12
+
13
+ ```
14
+ npm install @xiaoerge/node-printer
15
+ ```
16
+
17
+ ## Why this fork exists
18
+
19
+ Native addons are compiled against a specific **ABI**, and one binary works with
20
+ exactly one of them. Electron 45 uses ABI 150:
21
+
22
+ | Electron | ABI |
23
+ | --- | --- |
24
+ | 34 | 132 |
25
+ | 44 | 149 |
26
+ | 45 | 150 |
27
+
28
+ Upstream publishes prebuilds per ABI, and its newest is `electron-v132`
29
+ (Electron 34), because its build matrix pins `node-abi` to a version that does
30
+ not know anything newer. On Electron 45 that means no prebuild is found, the
31
+ install falls back to compiling from source, and the source no longer compiles:
32
+ `nan` 2.22 predates the V8 changes Electron 45 ships.
33
+
34
+ This fork carries the two bumps that fix that — `nan` to `^2.30.0` and
35
+ `node-abi` to `^3.104.0` — and publishes the resulting prebuilds under its own
36
+ release. The intent is to delete this fork once the same change lands upstream;
37
+ the equivalent pull request is open there.
38
+
39
+ **At the next Electron bump this cycle repeats**: `node-abi` has to know the new
40
+ version, and a prebuild for the new ABI has to be published. The workflow in
41
+ `.github/workflows/prebuild.yml` does the second part on every push to `main`.
42
+
43
+ The 32-bit Windows build is bounded deliberately. `--all` compiles for every
44
+ ABI `node-abi` knows, and Electron stopped publishing 32-bit binaries at 44, so
45
+ an unbounded sweep would try to build ia32 for versions that cannot have it.
46
+ The ia32 leg therefore builds an explicit list of targets, all at or below 43,
47
+ while x64 uses `--all`.
48
+
49
+ ## Prebuilds
50
+
51
+ Installing downloads a prebuilt binary from this repository's releases. When one
52
+ exists for the target platform, architecture and ABI, nothing is compiled; the
53
+ source build is only a fallback.
54
+
55
+ Assets are named from the package name with the scope stripped, so scope or
56
+ rename changes do not affect them:
57
+
58
+ ```
59
+ node-printer-v1.0.2-electron-v150-win32-x64.tar.gz
60
+ ```
61
+
62
+ ## API
63
+
64
+ - `getPrinters()` — enumerate installed printers with their jobs and statuses
65
+ - `getPrinter(printerName)` — info for a specific or default printer
66
+ - `getPrinterDriverOptions(printerName)` — POSIX only
67
+ - `getSelectedPaperSize(printerName)` — POSIX only
68
+ - `getDefaultPrinterName()`
69
+ - `printDirect(options)` — send a job to a specific or default printer, with
70
+ [CUPS options](http://www.cups.org/documentation.php/options.html) passed as a
71
+ JS object
72
+ - `printFile(options)` — POSIX only
73
+ - `getSupportedPrintFormats()` — depends on the OS; `RAW` and `TEXT` are
74
+ supported everywhere
75
+ - `getJob(printerName, jobId)`
76
+ - `setJob(printerName, jobId, command)` — e.g. `'CANCEL'`
77
+ - `getSupportedJobCommands()` — depends on the OS; `'CANCEL'` is supported
78
+ everywhere
79
+
80
+ ## Examples
81
+
82
+ See [examples](https://github.com/aacgn/node-printer/tree/main/examples).
83
+
84
+ ## Authors and contributors
85
+
86
+ - Ion Lupascu, ionlupascu@gmail.com
87
+ - Nicolas Alves, @niick555
88
+ - Timo Kunze, @niick555
89
+ - Thiago Lugli, @thiagoelg
90
+ - Eko Eryanto, @ekoeryanto
91
+
92
+ ## License
93
+
94
+ Inherited from upstream and **unresolved**: `package.json` declares BSD, while
95
+ the upstream README declared MIT, and upstream carries no `LICENSE` file. Both
96
+ are permissive and the discrepancy is not this fork's to settle — it is noted
97
+ here rather than silently resolved in either direction.
package/binding.gyp ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "variables": {
3
+ "module_name%": "node_printer",
4
+ "module_path%": "lib",
5
+ "openssl_fips": ""
6
+ },
7
+ 'targets': [
8
+ {
9
+ "target_name": "action_after_build",
10
+ "type": "none",
11
+ "dependencies": [ "<(module_name)" ],
12
+ "copies": [
13
+ {
14
+ "files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
15
+ "destination": "<(module_path)"
16
+ }
17
+ ]
18
+ },
19
+ {
20
+ 'target_name': 'node_printer',
21
+ 'sources': [
22
+ # is like "ls -1 src/*.cc", but gyp does not support direct patterns on
23
+ # sources
24
+ '<!@(["python3", "tools/getSourceFiles.py", "src", "cc"])'
25
+ ],
26
+ 'include_dirs' : [
27
+ "<!(node -e \"require('nan')\")"
28
+ ],
29
+ 'cflags_cc+': [
30
+ "-Wno-deprecated-declarations"
31
+ ],
32
+ 'conditions': [
33
+ # common exclusions
34
+ ['OS!="linux"', {'sources/': [['exclude', '_linux\\.cc$']]}],
35
+ ['OS!="mac"', {'sources/': [['exclude', '_mac\\.cc|mm?$']]}],
36
+ ['OS!="win"', {
37
+ 'sources/': [['exclude', '_win\\.cc$']]}, {
38
+ # else if OS==win, exclude also posix files
39
+ 'sources/': [['exclude', '_posix\\.cc$']]
40
+ }],
41
+ # specific settings
42
+ ['OS!="win"', {
43
+ 'cflags':[
44
+ '<!(cups-config --cflags)'
45
+ ],
46
+ 'ldflags':[
47
+ '<!(cups-config --libs)'
48
+ #'-lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lz -lpthread -lm -lcrypt -lz'
49
+ ],
50
+ 'libraries':[
51
+ '<!(cups-config --libs)'
52
+ #'-lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lz -lpthread -lm -lcrypt -lz'
53
+ ],
54
+ 'link_settings': {
55
+ 'libraries': [
56
+ '<!(cups-config --libs)'
57
+ ]
58
+ }
59
+ }],
60
+ ['OS=="win"', {
61
+ "defines": [
62
+ "NOMINMAX" # allow std::min/max to work
63
+ ],
64
+ "cflags": [
65
+ "-O2"
66
+ ],
67
+ "msvs_settings": {
68
+ "VCCLCompilerTool": {
69
+ "AdditionalOptions": [ "-std:c++20", ],
70
+ },
71
+ },
72
+ }],
73
+ ['OS=="mac"', {
74
+ 'cflags':[
75
+ "-stdlib=libc++"
76
+ ],
77
+ 'xcode_settings': {
78
+ "OTHER_CPLUSPLUSFLAGS":["-std=c++20", "-stdlib=libc++"],
79
+ "OTHER_LDFLAGS": ["-stdlib=libc++"],
80
+ "MACOSX_DEPLOYMENT_TARGET": "14.0",
81
+ },
82
+ }],
83
+ ['OS=="linux"', {
84
+ 'cflags_cc':[
85
+ "-std=c++20"
86
+ ]
87
+ }],
88
+ ]
89
+ }
90
+ ]
91
+ }
package/lib/index.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./printer');
package/lib/printer.js ADDED
@@ -0,0 +1,330 @@
1
+ var printer_helper = {},
2
+ fs = require("fs"),
3
+ child_process = require("child_process"),
4
+ os = require("os"),
5
+ path = require("path"),
6
+ binding_path = path.resolve(__dirname, './node_printer.node'),
7
+ printer_helper;
8
+
9
+ if(fs.existsSync(binding_path)) {
10
+ printer_helper = require(binding_path);
11
+ } else {
12
+ printer_helper = require('./node_printer_'+process.platform+'_'+process.arch+'.node');
13
+ }
14
+
15
+ /** Return all installed printers including active jobs
16
+ */
17
+ module.exports.getPrinters = getPrinters;
18
+
19
+ /** send data to printer
20
+ */
21
+ module.exports.printDirect = printDirect;
22
+
23
+ /// send file to printer
24
+ module.exports.printFile = printFile;
25
+
26
+ /** Get supported print format for printDirect
27
+ */
28
+ module.exports.getSupportedPrintFormats = printer_helper.getSupportedPrintFormats;
29
+
30
+ /**
31
+ * Get possible job command for setJob. It depends on os.
32
+ * @return Array of string. e.g.: DELETE, PAUSE, RESUME
33
+ */
34
+ module.exports.getSupportedJobCommands = printer_helper.getSupportedJobCommands;
35
+
36
+ /** get printer info object. It includes all active jobs
37
+ */
38
+ module.exports.getPrinter = getPrinter;
39
+ module.exports.getSelectedPaperSize = getSelectedPaperSize;
40
+ module.exports.getPrinterDriverOptions = getPrinterDriverOptions;
41
+
42
+ /// Return default printer name
43
+ module.exports.getDefaultPrinterName = getDefaultPrinterName;
44
+
45
+ /** get printer job info object
46
+ */
47
+ module.exports.getJob = getJob;
48
+ module.exports.setJob = setJob;
49
+
50
+ /**
51
+ * return user defined printer, according to https://www.cups.org/documentation.php/doc-2.0/api-cups.html#cupsGetDefault2 :
52
+ * "Applications should use the cupsGetDests and cupsGetDest functions to get the user-defined default printer,
53
+ * as this function does not support the lpoptions-defined default printer"
54
+ */
55
+ function getDefaultPrinterName() {
56
+ var printerName = printer_helper.getDefaultPrinterName();
57
+ if(printerName) {
58
+ return printerName;
59
+ }
60
+
61
+ // seems correct posix behaviour
62
+ var printers= getPrinters();
63
+ if(printers && printers.length){
64
+ var i = printers.length;
65
+ for(i in printers) {
66
+ var printer = printers[i];
67
+ if(printer.isDefault === true) {
68
+ return printer.name;
69
+ }
70
+ }
71
+ }
72
+
73
+ // printer not found, return nothing(undefined)
74
+ }
75
+
76
+ /** Get printer info with jobs
77
+ * @param printerName printer name to extract the info
78
+ * @return printer object info:
79
+ * TODO: to enum all possible attributes
80
+ */
81
+ function getPrinter(printerName)
82
+ {
83
+ if(!printerName) {
84
+ printerName = getDefaultPrinterName();
85
+ }
86
+ var printer = printer_helper.getPrinter(printerName);
87
+ correctPrinterinfo(printer);
88
+ return printer;
89
+ }
90
+
91
+ /** Get printer driver options includes advanced options like supported paper size
92
+ * @param printerName printer name to extract the info (default printer used if printer is not provided)
93
+ * @return printer driver info:
94
+ */
95
+ function getPrinterDriverOptions(printerName)
96
+ {
97
+ if(!printerName) {
98
+ printerName = getDefaultPrinterName();
99
+ }
100
+
101
+ return printer_helper.getPrinterDriverOptions(printerName);
102
+ }
103
+
104
+ /** Finds selected paper size pertaining to the specific printer out of all supported ones in driver_options
105
+ * @param printerName printer name to extract the info (default printer used if printer is not provided)
106
+ * @return selected paper size
107
+ */
108
+ function getSelectedPaperSize(printerName){
109
+ var driver_options = getPrinterDriverOptions(printerName);
110
+ var selectedSize = "";
111
+ if (driver_options && driver_options.PageSize) {
112
+ Object.keys(driver_options.PageSize).forEach(function(key){
113
+ if (driver_options.PageSize[key])
114
+ selectedSize = key;
115
+ });
116
+ }
117
+ return selectedSize;
118
+ }
119
+
120
+ function getJob(printerName, jobId)
121
+ {
122
+ return printer_helper.getJob(printerName, jobId);
123
+ }
124
+
125
+ function setJob(printerName, jobId, command)
126
+ {
127
+ return printer_helper.setJob(printerName, jobId, command);
128
+ }
129
+
130
+ function getPrinters(){
131
+ var printers = printer_helper.getPrinters();
132
+ if(printers && printers.length){
133
+ var i = printers.length;
134
+ for(i in printers){
135
+ correctPrinterinfo(printers[i]);
136
+ }
137
+ }
138
+ return printers;
139
+ }
140
+
141
+ function correctPrinterinfo(printer) {
142
+ if(printer.status || !printer.options || !printer.options['printer-state']){
143
+ return;
144
+ }
145
+
146
+ var status = printer.options['printer-state'];
147
+ // Add posix status
148
+ if(status == '3'){
149
+ status = 'IDLE'
150
+ }
151
+ else if(status == '4'){
152
+ status = 'PRINTING'
153
+ }
154
+ else if(status == '5'){
155
+ status = 'STOPPED'
156
+ }
157
+
158
+ // correct date type
159
+ var k;
160
+ for(k in printer.options) {
161
+ if(/time$/.test(k) && printer.options[k] && !(printer.options[k] instanceof Date)) {
162
+ printer.options[k] = new Date(printer.options[k] * 1000);
163
+ }
164
+ }
165
+
166
+ printer.status = status;
167
+ }
168
+
169
+ /*
170
+ print raw data. This function is intend to be asynchronous
171
+
172
+ parameters:
173
+ parameters - Object, parameters objects with the following structure:
174
+ data - String, mandatory, data to printer
175
+ printer - String, optional, name of the printer, if missing, will try to print to default printer
176
+ docname - String, optional, name of document showed in printer status
177
+ type - String, optional, only for wind32, data type, one of the RAW, TEXT
178
+ options - JS object with CUPS options, optional
179
+ success - Function, optional, callback function
180
+ error - Function, optional, callback function if exists any error
181
+
182
+ or
183
+
184
+ data - String, mandatory, data to printer
185
+ printer - String, optional, name of the printer, if missing, will try to print to default printer
186
+ docname - String, optional, name of document showed in printer status
187
+ type - String, optional, data type, one of the RAW, TEXT
188
+ options - JS object with CUPS options, optional
189
+ success - Function, optional, callback function with first argument job_id
190
+ error - Function, optional, callback function if exists any error
191
+ */
192
+ function printDirect(parameters){
193
+ var data = parameters
194
+ , printer
195
+ , docname
196
+ , type
197
+ , options
198
+ , success
199
+ , error;
200
+
201
+ if(arguments.length==1){
202
+ //TODO: check parameters type
203
+ //if (typeof parameters )
204
+ data = parameters.data;
205
+ printer = parameters.printer;
206
+ docname = parameters.docname;
207
+ type = parameters.type;
208
+ options = parameters.options||{};
209
+ success = parameters.success;
210
+ error = parameters.error;
211
+ }else{
212
+ printer = arguments[1];
213
+ type = arguments[2];
214
+ docname = arguments[3];
215
+ options = arguments[4];
216
+ success = arguments[5];
217
+ error = arguments[6];
218
+ }
219
+
220
+ if(!type){
221
+ type = "RAW";
222
+ }
223
+
224
+ // Set default printer name
225
+ if(!printer) {
226
+ printer = getDefaultPrinterName();
227
+ }
228
+
229
+ type = type.toUpperCase();
230
+
231
+ if(!docname){
232
+ docname = "node print job";
233
+ }
234
+
235
+ if (!options){
236
+ options = {};
237
+ }
238
+
239
+ //TODO: check parameters type
240
+ if(printer_helper.printDirect){// call C++ binding
241
+ try{
242
+ var res = printer_helper.printDirect(data, printer, docname, type, options);
243
+ if(res){
244
+ success(res);
245
+ }else{
246
+ error(Error("Something wrong in printDirect"));
247
+ }
248
+ }catch (e){
249
+ error(e);
250
+ }
251
+ }else{
252
+ error("Not supported");
253
+ }
254
+ }
255
+
256
+ /**
257
+ parameters:
258
+ parameters - Object, parameters objects with the following structure:
259
+ filename - String, mandatory, data to printer
260
+ docname - String, optional, name of document showed in printer status
261
+ printer - String, optional, mane of the printer, if missed, will try to retrieve the default printer name
262
+ success - Function, optional, callback function
263
+ error - Function, optional, callback function if exists any error
264
+ */
265
+ function printFile(parameters){
266
+ var filename,
267
+ docname,
268
+ printer,
269
+ options,
270
+ success,
271
+ error;
272
+
273
+ if((arguments.length !== 1) || (typeof(parameters) !== 'object')){
274
+ throw new Error('must provide arguments object');
275
+ }
276
+
277
+ filename = parameters.filename;
278
+ docname = parameters.docname;
279
+ printer = parameters.printer;
280
+ options = parameters.options || {};
281
+ success = parameters.success;
282
+ error = parameters.error;
283
+
284
+ if(!success){
285
+ success = function(){};
286
+ }
287
+
288
+ if(!error){
289
+ error = function(err){
290
+ throw err;
291
+ };
292
+ }
293
+
294
+ if(!filename){
295
+ var err = new Error('must provide at least a filename');
296
+ return error(err);
297
+ }
298
+
299
+ // try to define default printer name
300
+ if(!printer) {
301
+ printer = getDefaultPrinterName();
302
+ }
303
+
304
+ if(!printer) {
305
+ return error(new Error('Printer parameter of default printer is not defined'));
306
+ }
307
+
308
+ // set filename if docname is missing
309
+ if(!docname){
310
+ docname = filename;
311
+ }
312
+
313
+ //TODO: check parameters type
314
+ if(printer_helper.printFile){// call C++ binding
315
+ try{
316
+ // TODO: proper success/error callbacks from the extension
317
+ var res = printer_helper.printFile(filename, docname, printer, options);
318
+
319
+ if(!isNaN(parseInt(res))) {
320
+ success(res);
321
+ } else {
322
+ error(Error(res));
323
+ }
324
+ } catch (e) {
325
+ error(e);
326
+ }
327
+ } else {
328
+ error("Not supported");
329
+ }
330
+ }
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
- "name": "@xiaoerge/node-printer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@xiaoerge/node-printer",
3
+ "description": "Node.js printer bindings",
4
+ "version": "1.0.2",
5
+ "homepage": "https://github.com/aacgn/node-printer",
6
+ "author": {
7
+ "name": "Ion Lupascu",
8
+ "url": "http://program-support.co.uk/",
9
+ "email": "ionlupascu@gmail.com"
10
+ },
11
+ "contributors": [
12
+ "Nicolas Alves <logusbol@hotmail.com>",
13
+ "Timo Kunze <tkunze@compeso.com",
14
+ "Thiago Lugli <thiagoelg@gmail.com>",
15
+ "Eko Eryanto <ekoeryanto@gmail.com>"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git://github.com/aacgn/node-printer.git"
20
+ },
21
+ "engines": {
22
+ "node": ">= 14.0.0"
23
+ },
24
+ "scripts": {
25
+ "install": "prebuild-install || node-gyp rebuild",
26
+ "apply-patches": "patch-package",
27
+ "prebuild-all": "prebuild --all --force --strip --verbose --openssl_fips=X",
28
+ "prebuild:ci": "prebuild",
29
+ "rebuild": "node-gyp rebuild"
30
+ },
31
+ "binary": {
32
+ "module_name": "node_printer",
33
+ "module_path": "./lib/",
34
+ "host": "https://github.com/aacgn/node-printer/releases/download/",
35
+ "remote_path": "v{version}"
36
+ },
37
+ "licenses": [
38
+ {
39
+ "type": "BSD"
40
+ }
41
+ ],
42
+ "main": "./lib/printer",
43
+ "types": "types/index.d.ts",
44
+ "devDependencies": {
45
+ "node-abi": "^3.104.0",
46
+ "node-gyp": "^13.1.0",
47
+ "patch-package": "^8.0.0",
48
+ "prebuild": "^13.0.1",
49
+ "release-it": "^18.1.2"
50
+ },
51
+ "dependencies": {
52
+ "nan": "^2.30.0",
53
+ "prebuild-install": "^7.1.2"
54
+ },
55
+ "overrides": {
56
+ "node-gyp": "^13.1.0"
57
+ }
58
+ }
package/printer.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('./lib/printer');