node-opcua-xml2json
Advanced tools
Comparing version 2.142.0 to 2.143.0
@@ -1,2 +0,2 @@ | ||
import { Callback, SimpleCallback, Xml2Json } from "../xml2json"; | ||
import { Xml2Json } from "../xml2json"; | ||
export declare class Xml2JsonFs extends Xml2Json { | ||
@@ -7,3 +7,2 @@ /** | ||
parse(xmlFile: string): Promise<any>; | ||
parse(xmlFile: string, callback: Callback<any> | SimpleCallback): void; | ||
} |
@@ -10,34 +10,16 @@ "use strict"; | ||
class Xml2JsonFs extends xml2json_1.Xml2Json { | ||
parse(xmlFile, callback) { | ||
if (!callback) { | ||
throw new Error("internal error"); | ||
/** | ||
* @param xmlFile - the name of the xml file to parse. | ||
*/ | ||
async parse(xmlFile) { | ||
// slightly faster but require more memory .. | ||
let data = await fs_1.default.promises.readFile(xmlFile); | ||
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { | ||
data = data.subarray(3); | ||
} | ||
const readWholeFile = true; | ||
if (readWholeFile) { | ||
// slightly faster but require more memory .. | ||
fs_1.default.readFile(xmlFile, (err, data) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { | ||
data = data.subarray(3); | ||
} | ||
const dataAsString = data.toString(); | ||
const parser = this._prepareParser(callback); | ||
parser.write(dataAsString); | ||
parser.end(); | ||
}); | ||
} | ||
else { | ||
const Bomstrip = require("bomstrip"); | ||
const parser = this._prepareParser(callback); | ||
fs_1.default.createReadStream(xmlFile, { autoClose: true, encoding: "utf8" }).pipe(new Bomstrip()).pipe(parser); | ||
} | ||
const dataAsString = data.toString(); | ||
return this.__parseInternal(dataAsString); | ||
} | ||
} | ||
exports.Xml2JsonFs = Xml2JsonFs; | ||
// tslint:disable:no-var-requires | ||
const thenify_ex_1 = require("thenify-ex"); | ||
const opts = { multiArgs: false }; | ||
Xml2JsonFs.prototype.parse = (0, thenify_ex_1.withCallback)(Xml2JsonFs.prototype.parse, opts); | ||
//# sourceMappingURL=xml2json_fs.js.map |
@@ -5,13 +5,4 @@ /** | ||
*/ | ||
declare const LtxParser: any; | ||
export type SimpleCallback = (err?: Error) => void; | ||
export type Callback<T> = (err?: Error | null, result?: T) => void; | ||
declare interface LtxParser { | ||
write(str: string): void; | ||
end(): void; | ||
on(eventName: "startElement", eventHandler: (name: string, attrs: XmlAttributes) => void): void; | ||
on(eventName: "endElement", eventHandler: (name: string) => void): void; | ||
on(eventName: "text", eventHandler: (name: string) => void): void; | ||
on(eventName: "close", eventHandler: () => void): void; | ||
} | ||
export interface Parser { | ||
@@ -137,9 +128,4 @@ [key: string]: ReaderState; | ||
constructor(options: ReaderStateParser); | ||
parseStringSync(xml_text: string): Record<string, unknown>; | ||
parseString(xml_text: string): Record<string, unknown>; | ||
/** | ||
* @deprecated | ||
*/ | ||
parseString(xml_text: string): Promise<any>; | ||
parseString(xml_text: string, callback: Callback<any> | SimpleCallback): void; | ||
/** | ||
* @private | ||
@@ -158,4 +144,3 @@ * @internal | ||
*/ | ||
protected _prepareParser(callback: Callback<any> | SimpleCallback): LtxParser; | ||
protected __parseInternal(data: string): Record<string, unknown>; | ||
} | ||
export {}; |
@@ -8,7 +8,4 @@ "use strict"; | ||
exports.Xml2Json = exports.ReaderState = exports.ReaderStateBase = void 0; | ||
// tslint:disable:max-classes-per-file | ||
// tslint:disable:no-var-requires | ||
// tslint:disable:unified-signatures | ||
const node_opcua_assert_1 = require("node-opcua-assert"); | ||
const LtxParser = require("ltx/lib/parsers/ltx.js"); | ||
const lts_1 = require("./thirdparties/parser/lts"); | ||
/** | ||
@@ -196,14 +193,5 @@ * @static | ||
} | ||
parseStringSync(xml_text) { | ||
let retValue = {}; | ||
const parser = this._prepareParser((err, r) => (retValue = r)); | ||
parser.write(xml_text); | ||
parser.end(); | ||
return retValue; | ||
parseString(xml_text) { | ||
return this.__parseInternal(xml_text); | ||
} | ||
parseString(xml_text, callback) { | ||
const parser = this._prepareParser(callback); | ||
parser.write(xml_text); | ||
parser.end(); | ||
} | ||
/** | ||
@@ -239,5 +227,4 @@ * @private | ||
*/ | ||
_prepareParser(callback) { | ||
(0, node_opcua_assert_1.assert)(typeof callback === "function"); | ||
const parser = new LtxParser(); | ||
__parseInternal(data) { | ||
const parser = new lts_1.SaxLtx(); | ||
this.currentLevel = 0; | ||
@@ -270,15 +257,16 @@ parser.on("startElement", (name, attrs) => { | ||
}); | ||
parser.once("close", () => { | ||
if (callback) { | ||
callback(null, this.current_state._pojo); | ||
} | ||
}); | ||
return parser; | ||
parser.write(data); | ||
parser.end(""); | ||
return this.current_state._pojo; | ||
/* | ||
return await new Promise((resolve) => { | ||
parser.once("close", () => { | ||
resolve((this.current_state! as any)._pojo); | ||
}); | ||
//parser.write(data); | ||
parser.end(data); | ||
})*/ | ||
} | ||
} | ||
exports.Xml2Json = Xml2Json; | ||
// tslint:disable:no-var-requires | ||
const thenify_ex_1 = require("thenify-ex"); | ||
const opts = { multiArgs: false }; | ||
Xml2Json.prototype.parseString = (0, thenify_ex_1.withCallback)(Xml2Json.prototype.parseString, opts); | ||
//# sourceMappingURL=xml2json.js.map |
{ | ||
"name": "node-opcua-xml2json", | ||
"version": "2.142.0", | ||
"version": "2.143.0", | ||
"description": "pure nodejs OPCUA SDK - module xml2json", | ||
@@ -15,6 +15,4 @@ "main": "dist/source/index.js", | ||
"dependencies": { | ||
"ltx": "^3.1.2", | ||
"node-opcua-assert": "2.139.0", | ||
"node-opcua-utils": "2.139.0", | ||
"thenify-ex": "4.2.0", | ||
"xml-writer": "^1.7.0" | ||
@@ -41,3 +39,3 @@ }, | ||
"homepage": "http://node-opcua.github.io/", | ||
"gitHead": "50cfa07779d4c07b299176ac9c27527fcd97d079", | ||
"gitHead": "3bd85d4036f219f6f7e401d54e60afb3592ff1ba", | ||
"files": [ | ||
@@ -44,0 +42,0 @@ "dist", |
@@ -8,35 +8,11 @@ import fs from "fs"; | ||
*/ | ||
public async parse(xmlFile: string): Promise<any>; | ||
public parse(xmlFile: string, callback: Callback<any> | SimpleCallback): void; | ||
public parse(xmlFile: string, callback?: Callback<any> | SimpleCallback): any { | ||
if (!callback) { | ||
throw new Error("internal error"); | ||
public async parse(xmlFile: string): Promise<any> { | ||
// slightly faster but require more memory .. | ||
let data = await fs.promises.readFile(xmlFile); | ||
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { | ||
data = data.subarray(3); | ||
} | ||
const readWholeFile = true; | ||
if (readWholeFile) { | ||
// slightly faster but require more memory .. | ||
fs.readFile(xmlFile, (err: Error | null, data: Buffer) => { | ||
if (err) { | ||
return callback(err); | ||
} | ||
if (data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf) { | ||
data = data.subarray(3); | ||
} | ||
const dataAsString = data.toString(); | ||
const parser = this._prepareParser(callback); | ||
parser.write(dataAsString); | ||
parser.end(); | ||
}); | ||
} else { | ||
const Bomstrip = require("bomstrip"); | ||
const parser = this._prepareParser(callback); | ||
fs.createReadStream(xmlFile, { autoClose: true, encoding: "utf8" }).pipe(new Bomstrip()).pipe(parser); | ||
} | ||
const dataAsString = data.toString(); | ||
return this.__parseInternal(dataAsString); | ||
} | ||
} | ||
// tslint:disable:no-var-requires | ||
import { withCallback } from "thenify-ex"; | ||
const opts = { multiArgs: false }; | ||
Xml2JsonFs.prototype.parse = withCallback(Xml2JsonFs.prototype.parse, opts); |
@@ -6,8 +6,5 @@ /** | ||
// tslint:disable:max-classes-per-file | ||
// tslint:disable:no-var-requires | ||
// tslint:disable:unified-signatures | ||
import { assert } from "node-opcua-assert"; | ||
const LtxParser = require("ltx/lib/parsers/ltx.js"); | ||
import { SaxLtx } from "./thirdparties/parser/lts"; | ||
@@ -17,16 +14,3 @@ export type SimpleCallback = (err?: Error) => void; | ||
declare interface LtxParser { | ||
write(str: string): void; | ||
end(): void; | ||
on(eventName: "startElement", eventHandler: (name: string, attrs: XmlAttributes) => void): void; | ||
on(eventName: "endElement", eventHandler: (name: string) => void): void; | ||
on(eventName: "text", eventHandler: (name: string) => void): void; | ||
on(eventName: "close", eventHandler: () => void): void; | ||
} | ||
export interface Parser { | ||
@@ -93,4 +77,4 @@ [key: string]: ReaderState; | ||
export class ReaderStateBase {} | ||
export interface ReaderStateBase extends IReaderState {} | ||
export class ReaderStateBase { } | ||
export interface ReaderStateBase extends IReaderState { } | ||
/** | ||
@@ -287,20 +271,6 @@ * @private | ||
public parseStringSync(xml_text: string): Record<string, unknown> { | ||
let retValue: Record<string, unknown> = {}; | ||
const parser = this._prepareParser((err: Error | null | undefined, r: Record<string, unknown>) => (retValue = r)); | ||
parser.write(xml_text); | ||
parser.end(); | ||
return retValue; | ||
public parseString(xml_text: string): Record<string, unknown> { | ||
return this.__parseInternal(xml_text); | ||
} | ||
/** | ||
* @deprecated | ||
*/ | ||
public parseString(xml_text: string): Promise<any>; | ||
public parseString(xml_text: string, callback: Callback<any> | SimpleCallback): void; | ||
public parseString(xml_text: string, callback?: Callback<any> | SimpleCallback): any { | ||
const parser = this._prepareParser(callback!); | ||
parser.write(xml_text); | ||
parser.end(); | ||
} | ||
/** | ||
* @private | ||
@@ -338,5 +308,4 @@ * @internal | ||
*/ | ||
protected _prepareParser(callback: Callback<any> | SimpleCallback): LtxParser { | ||
assert(typeof callback === "function"); | ||
const parser = new LtxParser(); | ||
protected __parseInternal(data: string): Record<string, unknown> { | ||
const parser = new SaxLtx(); | ||
this.currentLevel = 0; | ||
@@ -369,14 +338,14 @@ parser.on("startElement", (name: string, attrs: XmlAttributes) => { | ||
}); | ||
parser.once("close", () => { | ||
if (callback) { | ||
(callback as any)(null, (this.current_state! as any)._pojo); | ||
} | ||
}); | ||
return parser; | ||
parser.write(data); | ||
parser.end(""); | ||
return (this.current_state! as any)._pojo; | ||
/* | ||
return await new Promise((resolve) => { | ||
parser.once("close", () => { | ||
resolve((this.current_state! as any)._pojo); | ||
}); | ||
//parser.write(data); | ||
parser.end(data); | ||
})*/ | ||
} | ||
} | ||
// tslint:disable:no-var-requires | ||
import { withCallback } from "thenify-ex"; | ||
const opts = { multiArgs: false }; | ||
Xml2Json.prototype.parseString = withCallback(Xml2Json.prototype.parseString, opts); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
146770
3
43
2065
- Removedltx@^3.1.2
- Removedthenify-ex@4.2.0
- Removed@types/assert@1.5.11(transitive)
- Removedany-promise@1.3.0(transitive)
- Removedassert@2.1.0(transitive)
- Removedavailable-typed-arrays@1.0.7(transitive)
- Removedcall-bind@1.0.8(transitive)
- Removedcall-bind-apply-helpers@1.0.1(transitive)
- Removedcall-bound@1.0.3(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddunder-proto@1.0.1(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes-object-atoms@1.1.1(transitive)
- Removedfor-each@0.3.5(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.7(transitive)
- Removedget-proto@1.0.1(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-symbols@1.1.0(transitive)
- Removedhas-tostringtag@1.0.2(transitive)
- Removedhasown@2.0.2(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-arguments@1.2.0(transitive)
- Removedis-callable@1.2.7(transitive)
- Removedis-generator-function@1.1.0(transitive)
- Removedis-nan@1.3.2(transitive)
- Removedis-regex@1.2.1(transitive)
- Removedis-typed-array@1.1.15(transitive)
- Removedltx@3.1.2(transitive)
- Removedmath-intrinsics@1.1.0(transitive)
- Removedobject-is@1.1.6(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedobject.assign@4.1.7(transitive)
- Removedpossible-typed-array-names@1.1.0(transitive)
- Removedsafe-regex-test@1.1.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedthenify-ex@4.2.0(transitive)
- Removedutil@0.12.5(transitive)
- Removedwhich-typed-array@1.1.18(transitive)