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

asset-sync-manager

v0.2.1

Published

Sync remote resource to local with version control

Downloads

73

Readme

Asset Sync Manager

Asset Sync Manager 用于从远程服务器同步资源到本地的node库,并且能够管理本地资源版本。

安装

使用 npm/yarn 安装:

npm install asset-sync-manager
yarn add asset-sync-manager

如果需要使用包中提供的 MongodbVersionController 实现,则需要额外安装 mongodb 与对应的 mongodb 库。

npm install asset-sync-manager mongodb
yarn add asset-sync-manager mongodb

使用

例子

asset-sync-manager 使用插件式的方式进行初始化。初始化时需要提供VersionController,PathController与可选的IntegrityChecker。

  1. VersionController 用于管理本地资源的版本。包中提供了mongodb实现。
  2. PathController 用于提供资源id(assetId)与远程资源路径、本地资源路径的映射关系。
  3. IntegrityChecker 为可选参数,用于检查下载至本地资源的完整性。

example/index.ts 中提供了示例:

import {
  SyncManager,
  PathController,
  VersionController,
  utils,
  IntegrityChecker,
} from "../dist";

const FfmpegIntegrityChecker = IntegrityChecker.FfmpegIntegrityChecker;
const MongodbVersionController = VersionController.MongodbVersionController;
const {
  constructDefaultGetRemotePath,
  constructDefaultGetLocalPath,
  // constructDefaultVersionedGetLocalPath // 文件存储路径与 version 相关的简单实现
} = PathController;

async function main() {
  const versionController = new MongodbVersionController({
    uri: "mongodb://127.0.0.1:27017",
    dbName: "test",
    collectionName: "assets",
  });
  await versionController.init();
  const pathController = {
    // 提供资源的baseUrl
    getRemotePath: constructDefaultGetRemotePath("https://static.example.com"),
    // 提供资源放置的本地目录
    getLocalPath: constructDefaultGetLocalPath("./assets"),
    // 资源下载使用的临时目录
    getTmpLocalPath: constructDefaultGetLocalPath("./tmp"),
  };
  const syncManager = new SyncManager(
    versionController,
    pathController,
    // 可选参数,用于检查下载至本地的图片/视频的完整性,为空时不进行完整性检验
    // 需要安装 ffmpeg
    new FfmpegIntegrityChecker(), 
    {
      logger: console, // 可选参数,日志输出的方式,为空时会输出至 "error.log" 与 "conbined.log" 文件中
    }
  );

  // 需要提供的 assetId 与 version 索引
  const assetEntry = {
    "video/1.mp4": 1,
    "img/114514.png": 2,
    "json/88888.json": 3,
    "json/secret/1919810.json": 5,
  };

  await syncManager.syncAll(assetEntry, {
    assetIdFilters: {
      assetIdIncludeFilters: [utils.constructPrefixFilter(["json/"])],
      assetIdExcludeFilters: [utils.constructPrefixFilter(["json/secret"])],
    }, // 从assetEntry中筛选出需要同步的asset。二者均为空时同步所有资源。否则筛选出满足前者且不满足后者的资源
    concurrency: 16, // 下载资源时的并行数
    removeLegacy: false, // 是否删除旧版本文件(若版本不同时,文件路径不变,则此选项无影响)
  });
}

main()
  .then(() => {
    console.log("finished");
    process.exit(0);
  })
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

实现插件

src/types 中包含了插件接口的定义。用户可以根据需求实现插件。

VersionController

export interface IVersionController<T> {
  init(): Promise<void>;
  shouldSync(assetId: string, remoteVersion: T): Promise<boolean>;
  upsertLocalVersion(assetId: string, version: T): Promise<void>;
  getLocalVersion?(assetId: string): Promise<T | null>;
  isRemoteVersionNewer?(remoteVersion: T, localVersion: T): boolean;
}

接口实现可以参考包中 MongodbVersionController

PathController

// src/types/path-controller.ts
export type GetPath<T> = (assetId: string, version: T) => string;
export type ResolvePath<T> = (path: string) => {
  assetId: string;
  version: T;
};
export interface IPathController<T> {
  getRemotePath: GetPath<T>;
  getLocalPath: GetPath<T>;
  getTmpLocalPath: GetPath<T>;
  resolveRemotePath?: ResolvePath<T>; // no use now
  resolveLocalPath?: ResolvePath<T>; // no use now
}

IntegrityChecker

export interface IIntegrityChecker {
  shouldCheckIntegrity: (assetId: string) => boolean;
  checkIntegrity: (localPath: string) => Promise<void>; // throw an error if not integrity
}