1 | var fs = require('fs');
|
2 |
|
3 | var iconv = null;
|
4 | try {
|
5 | iconv = require('iconv-lite');
|
6 | } catch (err) {
|
7 |
|
8 | iconv = null;
|
9 | }
|
10 |
|
11 | var bufferWrapper = require('./bufferwrapper');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | function MarcJsonWriter(options) {
|
17 | if (!(this instanceof MarcJsonWriter)) {
|
18 | return new MarcJsonWriter(options);
|
19 | }
|
20 |
|
21 |
|
22 | this.recordsFile = null;
|
23 |
|
24 | this.readyToWrite = false;
|
25 |
|
26 | this.position = null;
|
27 |
|
28 |
|
29 | options = options || {};
|
30 | this.options = {
|
31 |
|
32 | format: (options.format || 'UNIMARC').toUpperCase(),
|
33 |
|
34 | encoding: options.encoding || null
|
35 | }
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | MarcJsonWriter.prototype.openFile = function(recordsFile, options) {
|
42 | this.recordsFile = recordsFile;
|
43 | this.readyToWrite = true;
|
44 | this.position = 0;
|
45 |
|
46 | options = options || {};
|
47 |
|
48 | if (options.hasOwnProperty('format')) {
|
49 | this.options.format = (options.format || 'UNIMARC').toUpperCase();
|
50 | }
|
51 |
|
52 | if (options.hasOwnProperty('encoding')) {
|
53 | if (options.encoding && options.encoding !== 'utf-8'
|
54 | && iconv && iconv.encodingExists(options.encoding))
|
55 | {
|
56 | this.options.encoding = options.encoding;
|
57 | } else {
|
58 | this.options.encoding = null;
|
59 | }
|
60 | }
|
61 | }
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | MarcJsonWriter.prototype.open = function(recordsFileName) {
|
67 | var self = this;
|
68 | var options = arguments.length === 3 ? arguments[1] : undefined;
|
69 | var callback = arguments.length === 3 ? arguments[2] : arguments[1];
|
70 |
|
71 | var flags = (options || {}).flags || 'w';
|
72 | var mode = (options || {}).mode || '0666';
|
73 | fs.open(recordsFileName, flags, mode, function(err, recordsFile) {
|
74 | if (err) { return callback(err); }
|
75 | self.openFile(recordsFile, options);
|
76 | callback(null);
|
77 | });
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | MarcJsonWriter.prototype.openSync = function(recordsFileName, options) {
|
84 | var flags = (options || {}).flags || 'w';
|
85 | var mode = (options || {}).mode || '0666';
|
86 | var recordsFile = fs.openSync(recordsFileName, flags, mode);
|
87 | this.openFile(recordsFile, options);
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | MarcJsonWriter.prototype.close = function(callback) {
|
94 | var self = this;
|
95 | if (self.recordsFile !== null) {
|
96 | fs.close(self.recordsFile, function(err) {
|
97 | self.readyToWrite = false;
|
98 | self.recordsFile = null;
|
99 | self.position = null;
|
100 | callback(err);
|
101 | });
|
102 | }
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | MarcJsonWriter.prototype.closeSync = function() {
|
109 | if (this.recordsFile !== null) {
|
110 | fs.closeSync(this.recordsFile);
|
111 | this.readyToWrite = false;
|
112 | this.recordsFile = null;
|
113 | this.position = null;
|
114 | }
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | MarcJsonWriter.prototype.write = function(record, callback) {
|
121 | var self = this;
|
122 | if (self.recordsFile === null) {
|
123 | return callback(new Error('records file must be opened'));
|
124 | }
|
125 |
|
126 | var marcJsonRecord = JSON.stringify(record, null, 2) + '\n';
|
127 | var buffer = self.options.encoding ?
|
128 | iconv.encode(marcJsonRecord, self.options.encoding, {addBOM: false})
|
129 | : bufferWrapper.from(marcJsonRecord, 'utf-8');
|
130 | fs.write(self.recordsFile, buffer, 0, buffer.length, null,
|
131 | function(err, written) {
|
132 | self.position += written;
|
133 | callback(err);
|
134 | }
|
135 | );
|
136 | }
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | MarcJsonWriter.prototype.writeSync = function(record) {
|
142 | if (this.recordsFile === null) {
|
143 | throw new Error('records file must be opened');
|
144 | }
|
145 |
|
146 | var marcJsonRecord = JSON.stringify(record, null, 2) + '\n';
|
147 | var buffer = this.options.encoding ?
|
148 | iconv.encode(marcJsonRecord, this.options.encoding, {addBOM: false})
|
149 | : bufferWrapper.from(marcJsonRecord, 'utf-8');
|
150 |
|
151 | var written = fs.writeSync(this.recordsFile, buffer, 0,
|
152 | buffer.length, null);
|
153 | this.position += written;
|
154 | }
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | MarcJsonWriter.prototype.getPosition = function() {
|
160 | return this.position;
|
161 | }
|
162 |
|
163 | module.exports = {
|
164 | MarcJsonWriter: MarcJsonWriter
|
165 | };
|