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 🙏

© 2025 – Pkg Stats / Ryan Hefner

koishi-plugin-basedata

v0.2.0

Published

A Koishi plugin for download & read files

Readme

koishi-plugin-basedata

npm

为 Koishi 设计的一款强大而灵活的文件下载服务插件。

它允许其他插件按需下载文件,并将其作为 Base64 Data URL 读取,同时内置了可靠的下载重试和作用域隔离机制。

✨ 特性

  • 按需下载: 仅在文件不存在时才执行下载,节省带宽和存储空间。
  • Base64 读取: 方便地将任何文件(图片、字体等)直接以内联形式嵌入到 HTML 或 CSS 中。
  • 自动重试: 内置可靠的 fetch 重试机制,提高下载成功率。
  • 作用域隔离: 通过 scope API,不同插件可以拥有独立的存储空间,有效避免文件名冲突。
  • 动态 MIME 类型: 自动识别文件类型,生成正确的 Base64 Data URL。

📖 API

该插件向 Koishi 的上下文中注入了 ctx.basedata 服务,提供以下核心方法:

  • read(fileName: string, downloadsurl?: string): Promise<string>

    • 缓存优先。会优先尝试读取本地文件。如果文件不存在,则会自动下载并存入本地,然后返回其 Base64 Data URL。
  • download(fileName: string, downloadsurl?: string): Promise<string>

    • 强制刷新。会直接从远端下载文件,覆盖本地已有的同名文件,然后返回其 Base64 Data URL。当你需要确保文件是最新版本时,应使用此方法。
  • scope(scope: string): ScopedDownloadsURL

    • 获取一个带作用域的 API 实例,其下的 readdownload 方法与上述相同,但文件会被存储在隔离的子目录中,以避免插件间的文件名冲突。

示例 1: 读取文件 (缓存优先)

这是最常用的方法。插件会自动处理下载逻辑。

import { Context } from 'koishi'

export function apply(ctx: Context) {
  ctx.command('show-image', '显示一张网络图片').action(async () => {
    try {
      const imageUrl = 'https://koishi.chat/logo.png'
      // 如果 logo.png 已存在,则直接读取;否则,先下载再读取
      const imageBase64 = await ctx.basedata.read('logo.png', imageUrl)
      return <img src={imageBase64} />
    } catch (error) {
      ctx.logger('my-plugin').error('图片加载失败', error)
      return '图片加载失败。'
    }
  })
}

示例 2: 强制刷新文件并使用作用域

当你需要确保获取的是最新版本的文件时,应使用 download 方法。同时,使用 scope 来隔离文件是一种最佳实践。

import { Context } from 'koishi'

export const name = 'my-awesome-plugin'

export function apply(ctx: Context) {
  const logger = ctx.logger(name)

  // 使用你的插件名作为 scope
  const downloader = ctx.basedata.scope(name)

  ctx.command('update-asset', '更新并显示资源').action(async () => {
    try {
      const assetUrl = 'https://example.com/my-plugin-asset.jpg'
      // 总是从远端下载最新版本,并覆盖本地文件
      const imageBase64 = await downloader.download('asset.jpg', assetUrl)
      return <img src={imageBase64} />
    } catch (error) {
      logger.error('资源更新失败', error)
      return '资源更新失败。'
    }
  })
}

获得完整的类型提示

为了在你的插件中获得 ctx.basedata 服务的完整 TypeScript 类型提示,你可以从本插件导入 BaseData 服务类。

import { Context } from 'koishi'
import type { BaseData } from 'koishi-plugin-basedata'

declare module 'koishi' {
  interface Context {
    basedata: BaseData
  }
}

export function apply(ctx: Context) {
  // 现在 ctx.basedata 将拥有完整的类型提示
}