Comparing version 2.3.2 to 3.0.0
110
lib/index.js
'use strict'; | ||
var arraybufferXmlParser = require('arraybuffer-xml-parser'); | ||
var pako = require('pako'); | ||
var mlSpectraProcessing = require('ml-spectra-processing'); | ||
var uint8Base64 = require('uint8-base64'); | ||
var camelCase = require('camelcase'); | ||
function decodeBase64(base64, options = {}) { | ||
async function inflate(zlibCompressedData) { | ||
const inputStream = new ReadableStream({ | ||
start(controller) { | ||
controller.enqueue(zlibCompressedData); | ||
controller.close(); | ||
}, | ||
}); | ||
const decompressedStream = inputStream.pipeThrough( | ||
new DecompressionStream('deflate'), | ||
); | ||
const reader = decompressedStream.getReader(); | ||
const chunks = []; | ||
let totalLength = 0; | ||
while (true) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const { value, done } = await reader.read(); | ||
if (done) break; | ||
chunks.push(value); | ||
totalLength += value.length; | ||
} | ||
// Combine chunks into a single Uint8Array | ||
const decompressedData = new Uint8Array(totalLength); | ||
let offset = 0; | ||
for (const chunk of chunks) { | ||
decompressedData.set(chunk, offset); | ||
offset += chunk.length; | ||
} | ||
return decompressedData; | ||
} | ||
async function decodeBase64(base64, options = {}) { | ||
let { | ||
@@ -35,3 +70,3 @@ endian = 'little', | ||
case 'zlib': | ||
uint8Array = pako.inflate(uint8Array); | ||
uint8Array = await inflate(uint8Array); | ||
break; | ||
@@ -175,3 +210,10 @@ case '': | ||
function parseMzData(arrayBuffer) { | ||
/** | ||
* | ||
* @param {*} arrayBuffer | ||
* @param {import('../Options.js').Options} [options={}] | ||
* @returns | ||
*/ | ||
async function parseMzData(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -188,10 +230,16 @@ metadata: {}, | ||
let parsed = arraybufferXmlParser.parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
if (node.tagName !== 'data') return decoder$3.decode(value); | ||
return decodeBase64(node.value, node.attributes); | ||
const promise = decodeBase64(node.bytes, node.attributes); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
await mlSpectraProcessing.recursiveResolve(parsed); | ||
processMetadata(parsed.mzData, result.metadata); | ||
@@ -276,3 +324,4 @@ processSpectrumList$2(parsed.mzData, result.times, result.series.ms.data); | ||
// CV = Controlled vocabulary | ||
function parseMzML(arrayBuffer) { | ||
async function parseMzML(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -289,4 +338,4 @@ metadata: {}, | ||
let parsed = arraybufferXmlParser.parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
@@ -297,6 +346,13 @@ if (node.tagName !== 'binary') return decoder$2.decode(value); | ||
); | ||
return decodeBase64(node.value, { ontologies }); | ||
const promise = decodeBase64(node.bytes, { ontologies }); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
// parsed file still contains promises | ||
await mlSpectraProcessing.recursiveResolve(parsed); | ||
@@ -340,3 +396,10 @@ const mzML = parsed.mzML || parsed.indexedmzML.mzML; | ||
function parseMzXML(arrayBuffer) { | ||
/** | ||
* | ||
* @param {*} arrayBuffer | ||
* @param {import('../Options.js').Options} [options] | ||
* @returns | ||
*/ | ||
async function parseMzXML(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -352,7 +415,8 @@ metadata: {}, | ||
let parsed = arraybufferXmlParser.parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
if (node.tagName !== 'peaks') return decoder$1.decode(value); | ||
return decodeBase64(node.value, { | ||
const promise = decodeBase64(node.bytes, { | ||
precision: node.attributes.precision, | ||
@@ -362,4 +426,11 @@ endian: node.attributes.byteOrder, | ||
}); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
await mlSpectraProcessing.recursiveResolve(parsed); | ||
@@ -376,5 +447,6 @@ processSpectrumList(parsed.mzXML, result.times, result.series.ms); | ||
* @param {ArrayBuffer|string} xml - ArrayBuffer or String or any Typed Array (including Node.js' Buffer from v4) with the data | ||
* @return {{times: Array<number>, series: { ms: { data:Array<Array<number>>}}}} | ||
* @param {import('./Options.js').Options} [options={}] | ||
* @return Promise<{{times: Array<number>, series: { ms: { data:Array<Array<number>>}}}}> | ||
*/ | ||
function parseMZ(xml) { | ||
async function parseMZ(xml, options = {}) { | ||
if (typeof xml === 'string') { | ||
@@ -394,7 +466,7 @@ const encoder = new TextEncoder(); | ||
if (header.includes('mzData')) { | ||
return parseMzData(xml); | ||
return parseMzData(xml, options); | ||
} else if (header.includes('mzML')) { | ||
return parseMzML(xml); | ||
return parseMzML(xml, options); | ||
} else if (header.includes('mzXML')) { | ||
return parseMzXML(xml); | ||
return parseMzXML(xml, options); | ||
} else { | ||
@@ -401,0 +473,0 @@ throw new Error(`MZ parser: unknown format`); |
{ | ||
"name": "mzdata", | ||
"version": "2.3.2", | ||
"version": "3.0.0", | ||
"description": "Read and explore mzData v1.05 files", | ||
@@ -34,3 +34,3 @@ "main": "lib/index.js", | ||
"@babel/plugin-transform-modules-commonjs": "^7.25.9", | ||
"@vitest/coverage-v8": "2.1.7", | ||
"@vitest/coverage-v8": "2.1.8", | ||
"cheminfo-build": "^1.2.0", | ||
@@ -42,11 +42,11 @@ "eslint": "^9.16.0", | ||
"rollup": "^4.28.0", | ||
"vitest": "^2.1.7" | ||
"vitest": "^2.1.8" | ||
}, | ||
"dependencies": { | ||
"arraybuffer-xml-parser": "^0.6.1", | ||
"base64-arraybuffer": "^1.0.2", | ||
"arraybuffer-xml-parser": "^1.0.0", | ||
"camelcase": "^6.3.0", | ||
"pako": "^2.1.0", | ||
"cheminfo-types": "^1.8.1", | ||
"ml-spectra-processing": "^14.8.0", | ||
"uint8-base64": "^0.1.1" | ||
} | ||
} |
@@ -48,3 +48,3 @@ # History of formats | ||
const mzDataFile = readFileSync(__dirname + '/tiny.mzData.xml'); | ||
var response = parseMZ(mzDataFile); | ||
var response = await parseMZ(mzDataFile); | ||
``` | ||
@@ -62,4 +62,2 @@ | ||
## [API Documentation](https://cheminfo-js.github.io/mzData/) | ||
## License | ||
@@ -66,0 +64,0 @@ |
@@ -10,5 +10,6 @@ import { parseMzData } from './mzdata/parseMzData'; | ||
* @param {ArrayBuffer|string} xml - ArrayBuffer or String or any Typed Array (including Node.js' Buffer from v4) with the data | ||
* @return {{times: Array<number>, series: { ms: { data:Array<Array<number>>}}}} | ||
* @param {import('./Options.js').Options} [options={}] | ||
* @return Promise<{{times: Array<number>, series: { ms: { data:Array<Array<number>>}}}}> | ||
*/ | ||
export function parseMZ(xml) { | ||
export async function parseMZ(xml, options = {}) { | ||
if (typeof xml === 'string') { | ||
@@ -28,7 +29,7 @@ const encoder = new TextEncoder(); | ||
if (header.includes('mzData')) { | ||
return parseMzData(xml); | ||
return parseMzData(xml, options); | ||
} else if (header.includes('mzML')) { | ||
return parseMzML(xml); | ||
return parseMzML(xml, options); | ||
} else if (header.includes('mzXML')) { | ||
return parseMzXML(xml); | ||
return parseMzXML(xml, options); | ||
} else { | ||
@@ -35,0 +36,0 @@ throw new Error(`MZ parser: unknown format`); |
import { parse } from 'arraybuffer-xml-parser'; | ||
import { recursiveResolve } from 'ml-spectra-processing'; | ||
@@ -10,3 +11,10 @@ import { decodeBase64 } from '../util/decodeBase64'; | ||
export function parseMzData(arrayBuffer) { | ||
/** | ||
* | ||
* @param {*} arrayBuffer | ||
* @param {import('../Options.js').Options} [options={}] | ||
* @returns | ||
*/ | ||
export async function parseMzData(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -23,10 +31,16 @@ metadata: {}, | ||
let parsed = parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
if (node.tagName !== 'data') return decoder.decode(value); | ||
return decodeBase64(node.value, node.attributes); | ||
const promise = decodeBase64(node.bytes, node.attributes); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
await recursiveResolve(parsed); | ||
processMetadata(parsed.mzData, result.metadata); | ||
@@ -33,0 +47,0 @@ processSpectrumList(parsed.mzData, result.times, result.series.ms.data); |
import { parse } from 'arraybuffer-xml-parser'; | ||
import { recursiveResolve } from 'ml-spectra-processing'; | ||
@@ -11,3 +12,4 @@ import { decodeBase64 } from '../util/decodeBase64'; | ||
// CV = Controlled vocabulary | ||
export function parseMzML(arrayBuffer) { | ||
export async function parseMzML(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -24,4 +26,4 @@ metadata: {}, | ||
let parsed = parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
@@ -32,6 +34,13 @@ if (node.tagName !== 'binary') return decoder.decode(value); | ||
); | ||
return decodeBase64(node.value, { ontologies }); | ||
const promise = decodeBase64(node.bytes, { ontologies }); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
// parsed file still contains promises | ||
await recursiveResolve(parsed); | ||
@@ -38,0 +47,0 @@ const mzML = parsed.mzML || parsed.indexedmzML.mzML; |
@@ -1,5 +0,6 @@ | ||
import { decode } from 'base64-arraybuffer'; | ||
import { inflate } from 'pako'; | ||
import { decode } from 'uint8-base64'; | ||
export function decoder(base64Encoded, options = {}) { | ||
import { inflate } from '../util/inflate.js'; | ||
export async function decoder(base64Encoded, options = {}) { | ||
const { compressionAlgorithm } = options; | ||
@@ -9,3 +10,3 @@ let decoded; | ||
case 'zlib': | ||
decoded = inflate(decode(base64Encoded)); | ||
decoded = await inflate(decode(base64Encoded)); | ||
break; | ||
@@ -21,3 +22,3 @@ case undefined: | ||
} | ||
if (!decoded.byteLength % 8) { | ||
if (decoded.length % 8 !== 0) { | ||
throw new Error('decode to Float64Array not the right length'); | ||
@@ -24,0 +25,0 @@ } |
import { parse } from 'arraybuffer-xml-parser'; | ||
import { recursiveResolve } from 'ml-spectra-processing'; | ||
@@ -9,3 +10,10 @@ import { decodeBase64 } from '../util/decodeBase64'; | ||
export function parseMzXML(arrayBuffer) { | ||
/** | ||
* | ||
* @param {*} arrayBuffer | ||
* @param {import('../Options.js').Options} [options] | ||
* @returns | ||
*/ | ||
export async function parseMzXML(arrayBuffer, options = {}) { | ||
const { logger = console } = options; | ||
const result = { | ||
@@ -21,7 +29,8 @@ metadata: {}, | ||
let parsed = parse(arrayBuffer, { | ||
attributeNamePrefix: '', | ||
attributesNodeName: 'attributes', | ||
attributeNameProcessor: (attributeName) => attributeName, | ||
tagValueProcessor: (value, node) => { | ||
if (node.tagName !== 'peaks') return decoder.decode(value); | ||
return decodeBase64(node.value, { | ||
const promise = decodeBase64(node.bytes, { | ||
precision: node.attributes.precision, | ||
@@ -31,4 +40,11 @@ endian: node.attributes.byteOrder, | ||
}); | ||
// avoid unhandled promise rejection and swallow the error | ||
promise.catch((error) => { | ||
logger.error('error decoding base64', error); | ||
return []; | ||
}); | ||
return promise; | ||
}, | ||
}); | ||
await recursiveResolve(parsed); | ||
@@ -35,0 +51,0 @@ processSpectrumList(parsed.mzXML, result.times, result.series.ms); |
@@ -1,5 +0,6 @@ | ||
import { decode } from 'base64-arraybuffer'; | ||
import { inflate } from 'pako'; | ||
import { decode } from 'uint8-base64'; | ||
export function decoder(base64Encoded, options = {}) { | ||
import { inflate } from '../util/inflate.js'; | ||
export async function decoder(base64Encoded, options = {}) { | ||
const { compressionAlgorithm } = options; | ||
@@ -9,3 +10,3 @@ let decoded; | ||
case 'zlib': | ||
decoded = inflate(decode(base64Encoded)); | ||
decoded = await inflate(decode(base64Encoded)); | ||
break; | ||
@@ -21,3 +22,3 @@ case undefined: | ||
} | ||
if (!decoded.byteLength % 8) { | ||
if (decoded.byteLength % 8 !== 0) { | ||
throw new Error('decode to Float64Array not the right length'); | ||
@@ -24,0 +25,0 @@ } |
@@ -1,5 +0,6 @@ | ||
import { inflate } from 'pako'; | ||
import { decode } from 'uint8-base64'; | ||
export function decodeBase64(base64, options = {}) { | ||
import { inflate } from './inflate.js'; | ||
export async function decodeBase64(base64, options = {}) { | ||
let { | ||
@@ -31,3 +32,3 @@ endian = 'little', | ||
case 'zlib': | ||
uint8Array = inflate(uint8Array); | ||
uint8Array = await inflate(uint8Array); | ||
break; | ||
@@ -34,0 +35,0 @@ case '': |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7823423
22
977
73
+ Addedcheminfo-types@^1.8.1
+ Addedarraybuffer-xml-parser@1.0.0(transitive)
+ Addedbinary-search@1.3.6(transitive)
+ Addedcheminfo-types@1.8.1(transitive)
+ Addedd3-array@0.7.1(transitive)
+ Addedfft.js@4.0.4(transitive)
+ Addedis-any-array@2.0.1(transitive)
+ Addedml-array-max@1.2.4(transitive)
+ Addedml-array-min@1.2.3(transitive)
+ Addedml-array-rescale@1.3.7(transitive)
+ Addedml-matrix@6.12.0(transitive)
+ Addedml-spectra-processing@14.9.1(transitive)
+ Addedml-xsadd@3.0.1(transitive)
+ Addedspline-interpolator@1.0.0(transitive)
- Removedbase64-arraybuffer@^1.0.2
- Removedpako@^2.1.0
- Removedarraybuffer-xml-parser@0.6.1(transitive)
- Removedbase64-arraybuffer@1.0.2(transitive)
- Removedpako@2.1.0(transitive)