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 🙏

© 2026 – Pkg Stats / Ryan Hefner

tsx-safe-eval

v1.0.0

Published

Safe evaluation of TSX code strings using ts-morph

Downloads

137

Readme

tsx-safe-eval

TSXコードの文字列を安全に(evalを使わずに)動的実行するためのランタイムのライブラリです。 eval()new Function() を使用しないため、比較的安全です。 ts-morph を使用して抽象構文木(AST)を解析し、独自のインタプリタで実行します。

機能

  • コーディングスタイルを完全に保持するソースファイルのJSONデータ構造 (AST)
  • ソースファイルのASTコード文字列とJSONの変換(コンパイル)
  • JSONで定義されたソースファイルのスクリプト実行

インストール

npm install tsx-safe-eval

使い方

import { evalSyntaxList, compileSourceFileToJSON } from 'tsx-safe-eval';
import { Project } from 'ts-morph';

const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("test.ts", "export const a = 1 + 2;");
const sourceFileJson = compileSourceFileToJSON(sourceFile);

const variables = [{}]; // スコープ
const modules = {};    // インポート可能なモジュール

const result = evalSyntaxList(sourceFileJson.syntaxList, variables, modules);
console.log(result.exports.object.a); // 3

プロジェクトの読み込み・保存 (Project API)

本ライブラリは、スタンドアロン(Node.js環境)および非スタンドアロン(ブラウザ環境)の両方でディレクトリからのプロジェクトの読み込みや保存をサポートします。環境に合わせて path モジュールと ts-morphProjectOptions を渡すことができます。

スタンドアロン環境 (Node.js) での使い方

Node.jsの標準モジュールである path をコンテキストとして渡すことで、実際のファイルシステムを対象に読み書きします。ファイルシステム操作自体は ts-morph 側の FileSystemHost を利用して自動的に処理されます。

import { loadProject, ProjectContext } from 'tsx-safe-eval';
import path from 'path';

const context: ProjectContext = {
  path: path,
  // 必要な場合は ts-morph の ProjectOptions を渡すことができます
  // projectOptions: { compilerOptions: { ... } }
};

async function main() {
  const projectData = await loadProject('./project/tsconfig.json', context);
  console.log(projectData);
}

ブラウザ環境での使い方

ブラウザ環境ではネイティブのファイルシステム操作ができないため、path-browserify などのパス操作モジュールと、ts-morph のインメモリファイルシステム(またはカスタムファイルシステム)を組み合わせて使用します。

import { loadProject, ProjectContext } from 'tsx-safe-eval';
import path from 'path-browserify';

const context: ProjectContext = {
  path: path,
  projectOptions: {
    useInMemoryFileSystem: true // メモリ上でファイルシステムをエミュレートする
  }
};

async function main() {
  const projectData = await loadProject('/project/tsconfig.json', context);
  console.log(projectData);
}