@drupflare/untarl

中文文档
0.1.1发布于 2026/11/13MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2361

Dependency-free, workerd-safe tar and tar.gz extraction. No Node APIs.

原始使用文档
文档目录10 节
  1. 📋 Table of Contents
  2. 🎯 Why
  3. 📥 Install
  4. 🚀 Usage
  5. 🔧 API
  6. 🛡 Untrusted Input
  7. 🚫 Out of Scope
  8. 🧪 Testing
  9. 🔗 Related Repositories
  10. 📄 License

📦 untarl

Tar and tar.gz extraction with no dependencies and no Node APIs

Build Prettier codecov License

Reads a .tar or .tar.gz on workerd, in the browser, in Bun and in Node, with the same code and zero dependencies. Gzip is handled by the platform's own DecompressionStream('gzip') rather than a bundled inflater, which is what keeps the dependency count at zero and the bundle cost at nothing.


📋 Table of Contents


🎯 Why

The original problem was installing a Drupal contrib module at runtime, which means reading a drupal.org .tar.gz from inside a Cloudflare Worker. Two obvious answers were both closed:

  1. Do it in PHP. The wasm build has neither ext-zip nor ext-phar, and adding either is a multi-hour Docker rebuild of the interpreter.
  2. Use a tar library. Every popular one reaches for node:zlib, node:fs or Buffer. workerd has none of those.

What workerd does have is DecompressionStream('gzip'), natively. And tar is nothing but 512-byte blocks with an octal header. So the whole job fits in ~320 lines of platform-only JavaScript, out where await is legal and no build flag has to change.


📥 Install

bun add @drupflare/untarl

One entry point. The exports map declares . and ./package.json and nothing else, so every symbol arrives from @drupflare/untarl and @drupflare/untarl/src/untar.ts is refused by the resolver. A second name for the only module would be two ways to import the same thing with no way to say which is canonical; the sibling cartridge splits by subpath because its modules have different dependency costs, and here there are none to differ.


🚀 Usage

The common case is two calls: inflate and parse, then flatten to the files you want to write.

import { tarEntryTree, untarGzip } from '@drupflare/untarl';

const response = await fetch('https://ftp.drupal.org/files/projects/token-8.x-1.15.tar.gz');
const entries = await untarGzip(response.body!);

// strip = 1 drops the `token/` wrapper every drupal.org tarball has
for (const [path, bytes] of tarEntryTree(entries, 1)) {
  // write `bytes` to `path` here
}

If you already hold the bytes, or the archive is not gzipped, skip straight to the parser:

import { parseTar } from '@drupflare/untarl';

const entries = parseTar(new Uint8Array(await file.arrayBuffer()));
const names = entries.filter((e) => e.type === 'file').map((e) => e.name);

🔧 API

ExportSignatureWhat it does
untarGzip(stream)(ReadableStream<Uint8Array>) => Promise<TarEntry[]>gunzips through DecompressionStream, then parses
parseTar(bytes)(Uint8Array) => TarEntry[]parses a POSIX/ustar archive faithfully; refuses nothing on path grounds
tarEntryTree(entries, strip= 1)(TarEntry[], number) => Map<string, Uint8Array>flattens to the files to write, refusing paths that escape
TarEntry{ name, size, type, mode, bytes }one member; bytes is an owned copy, so the archive can be released
TarEntryType'file' | 'directory'the several typeflags that mean the same thing, folded together
TarParseErrorError & { offset?: number }malformed, truncated or out-of-range archive
TarPathErrorError & { path: string }a member path that would escape the target directory

The split between the two error types is the design. parseTar() is faithful to the bytes and will happily hand back an entry whose name climbs out of the target directory with .. segments. tarEntryTree() decides what is safe to write and is the only layer that throws TarPathError. Separating them lets you inspect a hostile archive without a parser that lies about its contents.


🛡 Untrusted Input

A tarball off the network is untrusted, so every read is bounds-checked and every failure is a named error rather than a read past the end of the buffer, an infinite loop, or a plausible empty result.

RefusedError
a header cut short by a truncated downloadTarParseError, with the byte offset
a size field that overruns the bufferTarParseError
a non-octal size or modeTarParseError
a nameless entryTarParseError
an absolute member pathTarPathError, from tarEntryTree()
a member path containing ..TarPathError, from tarEntryTree()
a hostile directory name, even though droppedTarPathError: checked before the type filter
a negative or non-integer stripRangeError

The header checksum at offset 148 is not enforced. Writers disagree on whether that field sums the bytes as signed or unsigned, so enforcing it rejects real archives; the octal and bounds checks already stop a malformed header from being read. That is a decision, not an omission.


🚫 Out of Scope

  • It does not stream the parse. The archive is buffered whole, because a tar header carries no back-pointer: an entry cannot be found without walking every block before it. For a contrib module that is a few megabytes, which is the size this was built for.
  • It does not follow links. Symlinks, hard links and device nodes are consumed and dropped. Nothing in a module install should follow one.
  • It does not write files. tarEntryTree() hands you a Map; where those bytes land is yours.
  • It does not create archives. Extraction only.

It does handle the shapes real drupal.org tarballs contain: the ustar prefix split, GNU L long names, pax x/g headers (skipped, though an x header's path= record renames the entry it describes), and the v7 habit of writing a directory as typeflag 0 with a trailing slash.


🧪 Testing

bun run typecheck
bun run test # 89 assertions across 2 specs
bun run test:coverage

89 passing, at 98.68% statements, over archives built in the spec rather than checked-in fixtures, so every refusal above has a case that trips it.

Two lines are uncovered. src/untar.ts:75 is the byteAt() bounds guard. Every header read sits inside a block the loop has already checked, so it cannot fire from parseTar(). src/untar.ts:214 is the "does not advance" guard, which is provably unreachable: next is headerAt + 512 + ceil(size / 512) * 512, so next <= headerAt cannot hold while a header is a whole block. It stays because the failure it guards against is a hang rather than an error.


RepositoryWhat it is
drupflare/workerthe consumer: Drupal 11 on Cloudflare Workers, which reads module tarballs
drupflare/cartridgethe host side of running a blocking interpreter in a Durable Object
drupflare/durabledbthe measured limits of Durable Object SQLite, plus the codec that survives them

📄 License

MIT (c) Gregory Mitchell 2026. See LICENSE.

版本与发布记录

@drupflare/untarl 共发布 2 个版本,首次发布于 2026/08/14;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.1latest2026/11/13今天
0.1.02026/08/142 个月前

关于 @drupflare/untarl 的常见问题

@drupflare/untarl 是什么?

Dependency-free, workerd-safe tar and tar.gz extraction. No Node APIs.

如何安装 @drupflare/untarl?

在项目目录运行 npm install @drupflare/untarl,或使用 pnpm add / yarn add / bun add。

@drupflare/untarl 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@drupflare/untarl 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@drupflare/untarl 可以商用吗?

@drupflare/untarl 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@drupflare/untarl 的下载量有多少?

根据 npm 官方下载统计,@drupflare/untarl 在 2026/10/13 – 2026/11/11 这 30 天内被下载 2,361 次,日均约 79 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@drupflare/untarl 还在维护吗?

最新版本 0.1.1 发布于 2026/11/13(今天);至今共发布 2 个版本,其中过去 12 个月发布了 2 个;首次发布于 2026/08/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。