npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@genshin-toolkit/parser

v1.0.1

Published

A small library to parse and validate Genshin Impact game data.

Downloads

48

Readme

Parser module of Genshin Toolkit

This module serves as a core component of all tools built in the Genshin Toolkit chain. This module depends on zod for data validation and parsing.

Usage

The current version has support for extracting game data from a HAR file captured from your Battle Chronicle. You need to be signed-in to obtain your game data

If you aren't sure on how to capture the HAR, follow instructions from this Guide by Microsoft for the browser you intend to work on:

Importing modules

Use core imports for node and web for web environments

// For node environment
import { DataProvider } from "@genshin-toolkit/parser";

// For web environments
import { DataProvider } from "@genshin-toolkit/parser/web";

Create a new DataProvider instance.

There are data provider classes for specific use cases, for Node and for Web:

Cross-environment

  • BufferDataProvider If you have the HAR Buffer at hand, you can use this DataProvider instead.
  • StringDataProvider Reads data from raw String.

Node only

  • FileDataProvider This is the provider you'll be using if you want to skip the hassle of reading the file yourself and passing in the buffer to the DataProvider.

Web only

  • WebFileDataProvider Alternate DataProvider for FileDataProvider to be used in browsers.

    import {
      BufferDataProvider,
      FileDataProvider,
      loadFromHar,
    } from "@genshin-toolkit/parser";
    
    const fileDataProvider = new FileDataProvider("..path-to.har");
    // or
    const bufferDataProvider = new BufferDataProvider(buffer);
    // or
    const stringDataProvider = new StringDataProvider("{...string_data...}");
  1. Call loadFromHar to extract and parse game data from a HAR file.

    const gameData = await loadFromHar(provider);
       
    // Tabular display (example)
    console.table(gameData.avatars.map(avatar => ({
        character: avatar.name,
        level: avatar.level,
        weapon: `${avatar.weapon?.type_name}/${avatar.weapon?.name} - lv${avatar.weapon?.level}`,
        friendship: `lv${avatar.fetter}`
    })));

Examples

Below are the examples of loading data from HAR for two different use-cases (Use async function wrapper if using await):

Parsing data from a local file

import { FileDataProvider, loadFromHar } from "@genshin-toolkit/parser";

const provider = new FileDataProvider("/home/path/to.har");
const gameDataFactory = await loadFromHar(provider);

Parsing game data from a game data file

If you want to parse the game data (data parsed from HAR with loadFromHar and saved on disk or a GameData schema compatible json file)

import { FileDataProvider, loadFromFile } from "@genshin-toolkit/parser";

const provider = new FileDataProvider("/home/path/gamedata.json");
const gameDataFactory = await loadFromFile(provider);

Parsing on the web

Use WebFileDataProvider to directly work with the File provided by the provided by the browser on picking a file. Or use cross-env DataProviders like BufferDataProvider if you already have the file Buffer in memory.

If using TypeScript, your module resolution should be the newer Node16 or NodeNext when trying to import parser/web.

import { WebFileDataProvider } from '@genshin-toolkit/parser/web';

document.getElementById('fileInput').addEventListener('change', async (event) => {
    const input = event.target as HTMLInputElement;
    if (input.files && input.files.length > 0) {
        const file = input.files[0];
        const provider = new WebFileDataProvider(file);

        try {
            const data = await provider.load();
            // Parse with loadFromHar or loadFromFile here.
            console.log('File data loaded:', data);
        } catch (error) {
            console.error('Error loading file:', error);
        }
    }
});