@jsm-mit/sultana-assets-rust-package
v0.4.0
Published
Wrapper TypeScript package for the sultana-assets-rust canister: chunked and single-shot uploads, salon images and transcoding reservations, the worker upload path, reads and asset URLs.
Readme
@jsm-mit/sultana-assets-rust-package
TypeScript wrapper for the sultana-assets-rust canister — Sultana's
asset storage on the Internet Computer (salon images, videos, music tracks, posters) — plus
the integration tests that prove it.
import { AssetsRustSalonActor, AssetsRustUploadsActor, AssetsRustAssetsActor } from "@jsm-mit/sultana-assets-rust-package";
// A salon owner puts an image into the salon's library. The canister asks
// sultana-core-motoko first (owner? quota?), makes a thumbnail, stores both and registers
// them in core — or traps and stores nothing.
const salon = new AssetsRustSalonActor(assetsCanisterId, ownerIdentity);
const imageId = await salon.uploadImageAsyncUnsafe({ salonId, name: "front.jpg", contentType: "image/jpeg", data });
// A video goes to a transcoding worker, not to the canister: reserve an id, then upload to
// one of the returned worker URLs (sultana-core-motoko-package's VideoUploadClient does it).
const { id, uploadUrls } = await salon.reserveTranscodingUploadAsyncUnsafe({ salonId, name: "promo" });
// Any file, any size, not tied to a salon: a chunked session.
const fileId = await new AssetsRustUploadsActor(assetsCanisterId, identity).uploadBytesAsyncUnsafe("notes.pdf", "application/pdf", bytes);
// Reads are anonymous.
const assets = new AssetsRustAssetsActor(assetsCanisterId);
const info = await assets.getAssetInfoAsyncUnsafe(imageId);
const url = assets.assetUrl(imageId); // https://<canister>.raw.icp0.io/asset/<id>One class per part of the canister:
| Class | Calls | Who |
|---|---|---|
| AssetsRustAdminActor | core link (claim-once), worker list, delete_asset, the reservation sweep | the core canister (via core's superadmin proxies); wiring scripts |
| AssetsRustUploadsActor | chunked session, single-shot upload, uploadBytesAsyncUnsafe | any signed-in caller |
| AssetsRustSalonActor | upload_image, delete_salon_asset, transcoding and render reservations | the salon's owner — core decides |
| AssetsRustWorkerActor | system upload of a transcoded result under a reserved id | listed transcoding workers |
| AssetsRustAssetsActor | get_asset_info, list_assets, http_request, assetUrl | anybody; list_assets only the canister's controllers and the core seat |
| AssetsRustAvatarActor | upload_my_avatar, delete_my_avatar (own avatar only, profile required — core isUser —, no quota; 64 KB / 512 px, re-encoded as JPEG), get_avatar, /avatar/<principal>, avatarUrl | writes: the caller for themselves; reads: anybody |
Every method is …AsyncUnsafe and throws one of three errors, like the other wrapper
packages:
CanisterError— the canister trapped. sultana-assets-rust has no error variants: it returns the value or traps, socauseis{ errorKey: "Trap", errorMessage: <trap text>, rawError }— e.g."upload not authorized: core canUploadAsset rejected: …".CallRefusedAtInspectionStage— refused before execution (kept for the shared vocabulary; the canister has noinspect_messagetoday).CriticalCanisterError— anything else: transport, an unreachable canister, no cycles.
Methods the canister answers with false for a refusal (setCoreCanister, setWorkers,
deleteAsset, abortUpload, failSystemUpload) return that boolean. Views turn
Principal into text, opt into optional fields, variants into string unions and sizes
into number; timestamps stay bigint nanoseconds. ASSETS_RUST_LIMITS carries the
canister's limits (chunk size, render settings) for checks before a call. The raw candid
types are exported too, and ./declarations/* gives the generated files.
src/ is browser-safe — the same package serves the biz panel, the playground and the
transcoding worker.
Scripts: build, typecheck, sync-declarations, test-suite, test-smoke, test-upgrade,
redeploy, sandbox
(an interactive console: npm run sandbox -- <canister-id>, caller from IDENTITY_PEM in
.env.sandbox or anonymous), stamp-canister, publish-public. See CLAUDE.md for who runs what.
Integration tests
All tests run on the LOCAL replica — no cycles, no shared slot. npm run test-suite
reinstalls the current ../sultana-assets-rust build into a local canister and runs
tests/test-suite.ts there. No
.env, no PEMs: every identity is generated per run. A fresh canister has no core canister,
so the suite claims that claim-once seat for a generated identity and plays core itself.
npm run test-smoke does the same with tests/test-smoke.ts: setup, then one write and one
read per domain (worker list, a few-byte single-shot upload read back over HTTP, a worker
result, a salon upload refused by the stand-in core), one replication wait, ≈ 30 s.
publish-public runs it too.
npm run test-upgrade seeds the canister, upgrades it in the middle of the run and checks
that the stored data reads back identical. scripts/local-canister.sh
builds ../sultana-assets-rust with cargo and reinstalls it into a canister of the dfx project
in local/ (its id stays under local/.dfx); the upgrade test upgrades that canister. The
local replica must run (dfx start --background); the scripts never start or stop it.
What the suite covers: the core seat and the worker list; chunked and single-shot uploads
with every owner and size check; reads, HTTP 200/206/404, certification header and
streaming; the worker's system upload with a poster; delete_asset taking the thumb along;
the reserve_render input checks. What it leaves to sultana-command-line-playground: every
path where core says yes — the canister's calls out to core go to the generated identity and
fail, and the suite asserts exactly that trap. Rabbit is reached only after core agreed, so
the suite never touches it.
Publishing
Bump the version by hand, commit, push, then npm run publish-public. It refuses unless this
repo and ../sultana-assets-rust are on a clean, pushed master and the version is new on
npm; it reinstalls the canister's master into the canister on the LOCAL replica
(scripts/local-canister.sh; the replica must run), checks that its
candid:service equals declarations/sultana-assets-rust/sultana-assets-rust.did as dfx
rewrites it (the hand-written .did is normalized by a local dfx build --check first), runs
the smoke test (tests/test-smoke.ts) on that same deploy, stamps
sultanaAssets.{repo, repoUrl, commit} into package.json, publishes and commits Version bump
to <version>.
