Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
xlsx-wasm-parser
Advanced tools
A wasm layer of the rust Calamine crate in order to parse XLSX-files fast.
XLSX-Wasm-Parser
A WebAssembly wrapper over the Rust Calamine crate, bringing Blazingly Fast (🔥) XLSX deserialization for Excel-files to both the browser and NodeJS.
This project is designed for deserializing XLSX files in the browser and Node. It takes the bytes of a file and returns either a 2D array of cells, or a list of objects based on a passed schema, which can be validated through built-in integration with Zod.
This project is still in it's early stages, and only implements the bare essentials for deserializing XLSX files. If all you need is to either get all rows from a file, or getting rows based on a schema, you can use this library as I won't be making breaking changes to the current public interface. However if you need full support for all XLSX features today, please consider using read-excel-file instead. If you have any feature requests, please open an issue. If you would like to contribute, please open a PR.
npm install xlsx-wasm-parser
We need to enable experimental WASM support in Webpack. To do this, add the following to your Webpack config:
// webpack.config.js
module.exports = {
//...
experiments: {
...,
asyncWebAssembly: true,
topLevelAwait: true,
},
};
For NextJS we need to enable WASM-support in the Webpack-bundler, which can be done by adding the following to your next.config.js
-file
const config = {
...
webpack: (config) => {
// enable webassembly
config.experiments = { ...config.experiments, asyncWebAssembly: true, topLevelAwait: true };
return config;
},
};
Vite requires the vite-plugin-wasm to run WebAssembly, which in turn requires the vite-plugin-top-level-await plugin. To use this library with Vite, you must install both of these plugins and add them to your Vite config.
npm i -D vite-plugin-wasm vite-plugin-top-level-await
// vite.config.ts
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
export default defineConfig({
plugins: [wasm(), topLevelAwait()],
});
Since the code might run on both the server and the client, we need to dynamically import the library based on the environment. This can be done by using the following code:
const { getAllRows } =
typeof window === "undefined"
? await import("xlsx-wasm-parser/node")
: await import("xlsx-wasm-parser");
import { getAllRows } from "xlsx-wasm-parser";
const input = document.getElementById("input");
input.addEventListener("change", () => {
// Currently only supports sync operations and therefore requires inputs to be of type ArrayBuffer or a Uint8Array
input.files?.[0]?
.arrayBuffer()
.then(getAllRows)
.then((rows) => {
// Rows is a 2d array of cells
});
});
// Fetch response
fetch("https://example.com/file.xlsx")
.then((response) => response.arrayBuffer())
.then(getAllRows)
.then((rows) => {
// Rows is a 2d array of cells
});
import { getParsedRows } from "xlsx-wasm-parser";
const xlsxSchema = [
[0, "name"],
[1, "age"],
[2, "address"]
] as [number, string][];
const input = document.getElementById("input");
input.addEventListener("change", () => {
// Currently only supports sync operations and therefore requires inputs to be of type ArrayBuffer or a Uint8Array
input.files?.[0]?
.arrayBuffer()
.then((buffer) => getRows(buffer, xlsxSchema))
.then((rows) => {
// Rows is a list of objects based on the schema
});
});
To use the validation feature, import the getParsedRowsWithZodSchema
function from the validation
entry-point.
import { getParsedRowsWithZodSchema} from "xlsx-wasm-parser/validation";
import z from "zod";
const xlsxSchema = [
[0, "name"],
[1, "age"],
[2, "address"]
] as [number, string][];
const zodSchema = z.object({
name: z.string(),
age: z.number(),
address: z.string(),
});
const input = document.getElementById("input");
input.addEventListener("change", () => {
// Currently only supports sync operations and therefore requires inputs to be of type ArrayBuffer or a Uint8Array
input.files?.[0]?
.arrayBuffer()
.then((buffer) => getRowsWithZodSchema(buffer, xlsxSchema, zodSchema))
.then((rows) => {
// Rows is a list of the objects that passed validation
});
});
To use the Node version of this package, import it from the Node entry-point.
import { getAllRows } from "xlsx-wasm-parser/node";
import fs from "fs";
const rows = getAllRows(fs.readFileSync("file.xlsx"));
// Rows is a 2d array of cells
import { getParsedRows } from "xlsx-wasm-parser/node";
import fs from "fs";
const xlsxSchema = [
[0, "name"],
[1, "age"],
[2, "address"],
] as [number, string][];
const rows = getParsedRows(fs.readFileSync("file.xlsx"), xlsxSchema);
// Rows is a list of objects based on the schema
To use the validation feature for Node, import the getParsedRowsWithZodSchema
function from the node/validation
entry-point.
import { getParsedRowsWithZodSchema } from "xlsx-wasm-parser/node/validation";
import fs from "fs";
const xlsxSchema = [
[0, "name"],
[1, "age"],
[2, "address"],
] as [number, string][];
const zodSchema = z.object({
name: z.string(),
age: z.number(),
address: z.string(),
});
const rows = getParsedRowsWithZodSchema(
fs.readFileSync("file.xlsx"),
xlsxSchema,
zodSchema
);
// Rows is a list of the objects that passed validation
Coming soon! I need to write fair implementations of the other libraries first. However from what I've been able to measure so far I can tell you it's fast.
Currently there is a lot to do so all contributions are appreciated!
FAQs
A wasm layer of the rust Calamine crate in order to parse XLSX-files fast.
The npm package xlsx-wasm-parser receives a total of 43 weekly downloads. As such, xlsx-wasm-parser popularity was classified as not popular.
We found that xlsx-wasm-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.