UNPKG

4.2 kBJavaScriptView Raw
1var fs = require('fs');
2
3var iconv = null;
4try {
5 iconv = require('iconv-lite');
6} catch (err) {
7 // If module iconv-lite is not present then use only utf-8 encoding.
8 iconv = null;
9}
10
11var bufferWrapper = require('./bufferwrapper');
12
13/*
14 * The constructor of JSON writer.
15 */
16function MarcJsonWriter(options) {
17 if (!(this instanceof MarcJsonWriter)) {
18 return new MarcJsonWriter(options);
19 }
20
21 // File with records in JSON format.
22 this.recordsFile = null;
23 // Flag is true when write() can be performed.
24 this.readyToWrite = false;
25 // Position in file.
26 this.position = null;
27
28 // File options.
29 options = options || {};
30 this.options = {
31 // MARC format variation (MARC21, UNIMARC).
32 format: (options.format || 'UNIMARC').toUpperCase(),
33 // Output data encoding.
34 encoding: options.encoding || null
35 }
36}
37
38/*
39 * Opens records file by descriptor.
40 */
41MarcJsonWriter.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 * Opens records file by name.
65 */
66MarcJsonWriter.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 * Opens records file by name (sync version).
82 */
83MarcJsonWriter.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 * Closes records file.
92 */
93MarcJsonWriter.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 * Closes records file (sync version).
107 */
108MarcJsonWriter.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 * Writes record to the file.
119 */
120MarcJsonWriter.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 * Writes record to the file (sync version).
140 */
141MarcJsonWriter.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 * Returns current position in file.
158 */
159MarcJsonWriter.prototype.getPosition = function() {
160 return this.position;
161}
162
163module.exports = {
164 MarcJsonWriter: MarcJsonWriter
165};