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

@suphark/files-client

v0.6.0

Published

Pi Files SDK: presigned/multipart upload, content proxy for Next.js route handlers, React upload hook, file references

Readme

@suphark/files-client

SDK ของ Pi Files (files.suphark.com) สำหรับ Next.js/React — dependency เดียวคือ zod; component สำเร็จรูป (/ui) ใช้ @suphark/ui ของแอป

pnpm add @suphark/files-client

เริ่มเร็วที่สุด: route เดียว + component (0.4.0)

// app/api/pi-files/[...path]/route.ts
export const { GET, POST, DELETE } = piFilesNext.route({
  authorize: async (request, action) => {
    const user = await getCurrentUser();
    if (!user) return new Response(null, { status: 401 });
    return (await canAccess(user, action)) && { userId: user.id, name: user.name };
  },
});
import { PiAttachments, PiImageField, usePiDraft } from "@suphark/files-client/ui"; // ต้องมี @suphark/ui ในแอป
<PiAttachments module="vendor.attachments" entity={{ type: "vendor", id }} />
<PiImageField module="user.avatar" entity={{ type: "user", id }} />

ฉบับร่าง → ระเบียนจริง references.moveEntity(draft, to) · ลบระเบียน references.detachEntity(entity) — ดู docs/guides/quick-start.md

0.6.0: useUploadQueue().enqueueReplace(fileId, file) อัปโหลดเวอร์ชันใหม่ผ่านคิว · Google Drive ส่งเป็นช่วง 8 MiB เน็ตหลุดแล้วส่งต่อจากเดิม (useUpload ต่อได้แม้ refresh หน้า)

0.5.0: references.counts(entityType, ids) (จำนวนไฟล์ต่อระเบียน ครั้งเดียวต่อหน้า) · migration.adoptMany(items, { dryRun }) รับไฟล์เดิมจาก URL ทีละชุด

1. server client (@suphark/files-client)

import { createPiFiles } from "@suphark/files-client";

export const piFiles = createPiFiles({
  baseUrl: process.env.PI_FILES_URL!, // https://files.suphark.com
  appKey: process.env.PI_FILES_APP_KEY!, // pfk_… (ownerType APP)
});

// อัปโหลดจาก server (Buffer/Blob) ครบวงจร
const file = await piFiles.upload(buffer, {
  filename: "รายงาน.pdf",
  mimeType: "application/pdf",
  sizeBytes: buffer.byteLength,
  featureKey: "REPORT",
  reference: { featureKey: "REPORT", entityType: "Project", entityId: "p1", role: "main" },
});

await piFiles.references.attach(file.id, {
  featureKey: "REPORT",
  entityType: "Project",
  entityId: "p2",
  role: "copy",
});
const mine = await piFiles.references.byEntity("Project", "p1");
const { url } = await piFiles.files.contentUrl(file.id, { download: true }); // presigned อายุสั้น
await piFiles.files.delete(file.id); // 409 FILE_REFERENCED ถ้ายังมี reference

piFiles.withUser(accessToken) → client เดียวกันแต่ใช้ Pi Auth JWT ของผู้ใช้ (ไฟล์ ownerType USER)

linked source (โฟลเดอร์ Google Drive เดิม) — 0.3.0

const src = await piFiles.sources.link({ folder: "https://drive.google.com/drive/folders/<id>" });
const tree = await piFiles.folders.contents(src.folderId, { recursive: true }); // relativePath, md5, modifiedTime, providerFileId
const set = await piFiles.folders.ensurePath(src.folderId, ["01_Docs", "ชุด A"]); // สร้างใน Drive จริง
const access = await piFiles.files.access(fileId); // ใครเปิดได้ใน Drive + ทำลิงก์สาธารณะได้ไหม

webhook file.created|updated|moved|deleted|missing, folder.* มี source.ancestorFolderIds — ดู docs/guides/connect-app.md §6.1

2. Next.js route handlers (@suphark/files-client/next)

// src/lib/pi-files.ts
import { createPiFiles, createPiFilesNext } from "@suphark/files-client/next";
export const piFiles = createPiFiles({ baseUrl, appKey });
export const piFilesNext = createPiFilesNext({ client: piFiles });

// app/api/files/uploads/[[...path]]/route.ts — proxy อัปโหลด (browser ไม่ถือ key)
export const { POST, DELETE } = piFilesNext.handlers.uploads({
  authorize: async (request, action) => {
    const user = await piAuth.getCurrentUser();
    if (!user) return null;                       // 401
    if (action.kind !== "create") return {};
    return { input: { featureKey: "REPORT", onBehalfOfUserId: user.id, reference: {...} } };
  },
});

// app/api/files/[id]/content/route.ts — URL same-origin ที่ browser ใช้
export const GET = piFilesNext.handlers.content({
  strategy: "redirect",                           // หรือ "stream" (Range passthrough)
  resolve: async (request, params) => {
    const user = await piAuth.getCurrentUser();
    if (!user) return new Response(null, { status: 401 });
    if (!(await canView(user, params.id))) return null; // 404
    return { fileId: String(params.id), download: request.url.includes("download=1") };
  },
});

3. React (@suphark/files-client/react)

"use client";
import { useUpload, useUploadQueue } from "@suphark/files-client/react";

const { upload, cancel, status, progress, error } = useUpload({ endpoint: "/api/files/uploads" });
await upload(file); // single PUT หรือ multipart (8 MiB/part, 3 ขนาน, resume หลัง refresh)

const { items, enqueue, enqueueReplace, cancel } = useUploadQueue({
  endpoint: "/api/files/uploads",
  parallel: 2,
});
enqueue(files);
enqueueReplace(existingFileId, file); // 0.6.0: เวอร์ชันใหม่ทับไฟล์เดิมในคิวเดียวกัน (เช่นผู้ใช้เลือก "แทนที่" เมื่อชื่อซ้ำ) — items[].replaceFileId

endpoint มีรูปแบบเดียวกับ /api/v1/uploads ของ Pi Files: POST (ticket), POST /:id/parts, POST /:id/complete, DELETE /:id

Errors

ทุก method โยน PiFilesError { status, code, message, problem } — code เช่น UNAUTHENTICATED, PERMISSION_DENIED, NOT_FOUND, QUOTA_EXCEEDED, UNSUPPORTED_MEDIA_TYPE, PAYLOAD_TOO_LARGE, FILE_REFERENCED, SIZE_MISMATCH, UPLOAD_INCOMPLETE, ABORTED