@napplet/nap
v0.21.0
Published
The active napplet NAP domains (the foundational shell, plus relay, storage, inc, keys, theme, media, notify, identity, config, resource, cvm, outbox, upload, intent, ble, webrtc, link, lists, serial, common) as layered subpath exports
Readme
@napplet/nap
Every active napplet NAP domain (relay, storage, inc, keys, theme, media, notify, identity, config, resource, cvm, outbox, upload, intent, ble, webrtc, link, lists, serial, common) as layered subpath exports. The package name remains
@napplet/napfor compatibility.
Install
pnpm add @napplet/nap@napplet/nap has no root export — consumers MUST import from a domain subpath. See Subpath Patterns for the available entry-point shapes per domain.
Quick Start
// Barrel — types + shim installer + sdk helper together
import { installRelayShim, relaySubscribe, RelaySubscribeMessage } from '@napplet/nap/relay';// Granular — types only (zero runtime cost)
import type { IncEventMessage } from '@napplet/nap/inc/types';Shells that only need to mount a NAP import the shim subpath directly:
// Granular — shim installer only (no SDK helpers bundled)
import { installStorageShim } from '@napplet/nap/storage/shim';Napplet authors that want a typed wrapper over window.napplet without the installer pull from the SDK subpath:
// Granular — SDK helpers only (no shim installer bundled)
import { notifySend } from '@napplet/nap/notify/sdk';End-to-end: a napplet subscribes to a relay stream using the SDK helper, while the shell mounts the matching installer on the napplet window:
// In the napplet (runs inside the sandboxed iframe)
import { relaySubscribe } from '@napplet/nap/relay/sdk';
const sub = relaySubscribe(
[{ kinds: [1], limit: 20 }],
{
onEvent: (event) => console.log('note', event),
onEose: () => console.log('caught up'),
},
);
// Later, tear down the subscription
sub.close();// In the shell (runs on the host page)
import { installRelayShim } from '@napplet/nap/relay/shim';
installRelayShim(nappletWindow, {
// shell-provided relay pool, ACL, etc.
});20 Active Domains
Each domain is an independent subpath. Barrel imports bundle types + shim installer + SDK helpers; granular subpaths isolate each surface.
| Domain | Barrel | Types | Shim | SDK | Purpose |
|--------|--------|-------|------|-----|---------|
| relay | @napplet/nap/relay | @napplet/nap/relay/types | @napplet/nap/relay/shim | @napplet/nap/relay/sdk | Nostr relay proxy (subscribe/publish/query) |
| storage | @napplet/nap/storage | @napplet/nap/storage/types | @napplet/nap/storage/shim | @napplet/nap/storage/sdk | Scoped key-value storage |
| inc | @napplet/nap/inc | @napplet/nap/inc/types | @napplet/nap/inc/shim | @napplet/nap/inc/sdk | Inter-napplet communication (topic pub/sub) |
| keys | @napplet/nap/keys | @napplet/nap/keys/types | @napplet/nap/keys/shim | @napplet/nap/keys/sdk | Keyboard bindings + action registration |
| theme | @napplet/nap/theme | @napplet/nap/theme/types | — | — | Read-only shell theme access (types-only today) |
| media | @napplet/nap/media | @napplet/nap/media/types | @napplet/nap/media/shim | @napplet/nap/media/sdk | Ownership-aware media sessions + playback |
| notify | @napplet/nap/notify | @napplet/nap/notify/types | @napplet/nap/notify/shim | @napplet/nap/notify/sdk | Shell-rendered notifications |
| identity | @napplet/nap/identity | @napplet/nap/identity/types | @napplet/nap/identity/shim | @napplet/nap/identity/sdk | Read-only user queries (pubkey, metadata) |
| config | @napplet/nap/config | @napplet/nap/config/types | @napplet/nap/config/shim | @napplet/nap/config/sdk | Declarative per-napplet config (schema-driven) |
| resource | @napplet/nap/resource | @napplet/nap/resource/types | @napplet/nap/resource/shim | @napplet/nap/resource/sdk | Sandboxed byte fetching (https/blossom/nostr/data) via bytes(url) → Blob |
| cvm | @napplet/nap/cvm | @napplet/nap/cvm/types | @napplet/nap/cvm/shim | @napplet/nap/cvm/sdk | Native ContextVM bridge — MCP-over-Nostr (discover/listTools/callTool/listResources/readResource); shell owns all transport |
| outbox | @napplet/nap/outbox | @napplet/nap/outbox/types | @napplet/nap/outbox/shim | @napplet/nap/outbox/sdk | Outbox-aware relay routing — query/subscribe/publish/resolveRelays; shell owns NIP-65 relay discovery, dedup, and fanout |
| upload | @napplet/nap/upload | @napplet/nap/upload/types | @napplet/nap/upload/shim | @napplet/nap/upload/sdk | Shell-mediated file/blob upload — upload/status/onStatus over NIP-96 + Blossom rails; shell signs auth, returns NIP-94 metadata |
| intent | @napplet/nap/intent | @napplet/nap/intent/types | @napplet/nap/intent/shim | @napplet/nap/intent/sdk | Archetype intent dispatch — invoke/open/available/handlers/onChanged; invoke another napplet by role, shell resolves the default handler |
| ble | @napplet/nap/ble | @napplet/nap/ble/types | @napplet/nap/ble/shim | @napplet/nap/ble/sdk | Runtime-mediated Bluetooth LE/GATT sessions — open/services/read/write/subscribe/unsubscribe/close/onEvent; shell owns chooser UI, permissions, device handles, and lifecycle |
| webrtc | @napplet/nap/webrtc | @napplet/nap/webrtc/types | @napplet/nap/webrtc/shim | @napplet/nap/webrtc/sdk | Runtime-mediated WebRTC data sessions — open/send/close/onEvent; shell owns signaling, SDP, ICE, and peer-connection lifecycle |
| link | @napplet/nap/link | @napplet/nap/link/types | @napplet/nap/link/shim | @napplet/nap/link/sdk | Shell-mediated external link opening — open(url, options?); shell owns navigation, policy, prompting, and opener isolation |
| lists | @napplet/nap/lists | @napplet/nap/lists/types | @napplet/nap/lists/shim | @napplet/nap/lists/sdk | Runtime-mediated NIP-51 list mutations — supported/add/remove; runtime owns lookup, merge, encryption, signing, and publishing |
| common | @napplet/nap/common | @napplet/nap/common/types | @napplet/nap/common/shim | @napplet/nap/common/sdk | Common social actions — public NIP-19 helpers, profile lookup, follows, follow/unfollow, reactions, and reports; shell owns identity/signing/publishing |
| serial | @napplet/nap/serial | @napplet/nap/serial/types | @napplet/nap/serial/shim | @napplet/nap/serial/sdk | Runtime-mediated serial device access — open/write/close/onEvent; shell owns permissions, port handles, streams, and lifecycle |
Deprecated IFC Compatibility
NAP-IFC was renamed to NAP-INC because the surface is inter-napplet communication, not generic inter-frame communication. New code must use @napplet/nap/inc, window.napplet.inc, the inc.* wire messages, and the Inc* TypeScript names.
The old @napplet/nap/ifc, @napplet/nap/ifc/types, @napplet/nap/ifc/shim, and @napplet/nap/ifc/sdk subpaths remain as deprecated thin wrappers. They re-export the INC implementation and aliases only; they do not define a separate ifc domain or ifc.* wire protocol.
Subpath Patterns
Each domain exposes up to three patterns (four including the barrel). Pick the shape that matches what your code actually needs:
- Barrel (
@napplet/nap/<domain>): re-exports types + shim installer + SDK helpers together. Most ergonomic for consumers that want everything a domain offers. - Types-only (
@napplet/nap/<domain>/types): pure TypeScript types, zero runtime code. Ideal for shells writing typed message handlers without shipping the shim installer. - Shim (
@napplet/nap/<domain>/shim): installer + message handlers. For shell/host code mounting a NAP into the napplet window. - SDK (
@napplet/nap/<domain>/sdk): named-function helpers (e.g.,relaySubscribe,storageGet). For napplet consumer code that wants a typed wrapper overwindow.napplet.
Tree-Shaking Contract
@napplet/nappublishes withsideEffects: false- Every subpath in the
exportsmap is a discrete entry point; a bundler importing only@napplet/nap/relay/typesproduces zero bytes from unrelated domains - Verified end-to-end in Phase 121 with a minimal-consumer smoke test
The exports map in package.json declares 88 entry points:
- 20 domain barrels (
@napplet/nap/<domain>) - 20 granular types entries (
@napplet/nap/<domain>/types) - 20 granular shim entries (
@napplet/nap/<domain>/shim) - 20 granular sdk entries (
@napplet/nap/<domain>/sdk) - 2 compatibility/foundational barrels (
ifc,shell) with matchingtypes,shim, andsdkentries
Each entry maps to its own pre-built .js + .d.ts pair under dist/<domain>/<surface>.{js,d.ts}. No root . key exists, and there is no top-level main/module/types field — attempting import '@napplet/nap' fails with ERR_PACKAGE_PATH_NOT_EXPORTED by design.
Compatibility Subpaths
shell is foundational rather than a NAP_DOMAINS capability, and ifc is a
deprecated compatibility wrapper for inc. They remain exported so older
consumers keep working, but new napplet capability checks should use the active
NAP domains.
Resource NAP (v0.28.0)
The resource domain ships in v0.28.0 alongside the milestone of browser-enforced
resource isolation. It defines scheme-pluggable byte-fetching primitives:
import { bytes, bytesMany, bytesAsObjectURL } from '@napplet/nap/resource/sdk';
// Fetch any URL the shell accepts under its policy. URL space is scheme-pluggable.
const blob: Blob = await bytes('https://example.com/avatar.png');
// Fetch many URLs in one envelope. Items preserve input order and length.
const items = await bytesMany([
'https://example.com/avatar.png',
'blossom:sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855',
]);
// Synchronous handle for <img src> use; revoke when done.
const handle = bytesAsObjectURL('blossom:sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
imgEl.src = handle.url;
// later
handle.revoke();Four canonical schemes are defined in the spec:
data:— RFC 2397, decoded inside the napplet shim with zero shell round-triphttps:— shell-side network fetch under the default resource policy (private-IP block list at DNS time, MIME byte-sniffing, size cap, timeout, rate limit, redirect cap)blossom:sha256:<hex>— Blossom hash → bytes; shell verifies hash before deliverynostr:<bech32>— single-hop NIP-19 resolution against the shell's relay pool
Errors arrive as one of 8 typed codes: not-found, blocked-by-policy, timeout,
too-large, unsupported-scheme, decode-failed, network-error, quota-exceeded.
See NAP-RESOURCE for the normative spec, the default shell resource policy, and the SVG rasterization MUSTs.
Identity NAP
The identity domain is read-only. It exposes the shell-user pubkey and public
identity data, but it does not sign, encrypt, or decrypt. Startup code should
take one snapshot with getPublicKey() and then subscribe to shell-pushed
identity.changed updates instead of polling while a signer connects.
import '@napplet/shim';
const pubkey = await window.napplet.identity.getPublicKey(); // "" when signed out
const sub = window.napplet.identity.onChanged((nextPubkey) => {
if (nextPubkey === '') {
// signed out or signer disconnected
return;
}
// identity-dependent work can refresh here
});The wire surface includes identity.changed as a shell-to-napplet push message
with { pubkey } and no correlation id. The public key shape matches
identity.getPublicKey.result: a hex pubkey when connected, or "" when no
user/signer is connected.
See the NAP-IDENTITY draft spec for the current read-only contract.
Package Surface
@napplet/nap is the compatibility package for every NAP domain. Import each domain through the package subpath:
import { mediaCreateSession } from '@napplet/nap/media/sdk';
import type { MediaNapMessage } from '@napplet/nap/media/types';Domain barrels are also available at @napplet/nap/relay, @napplet/nap/storage, @napplet/nap/inc, @napplet/nap/keys, @napplet/nap/theme, @napplet/nap/media, @napplet/nap/notify, @napplet/nap/identity, @napplet/nap/config, @napplet/nap/resource, @napplet/nap/cvm, @napplet/nap/outbox, @napplet/nap/upload, @napplet/nap/intent, @napplet/nap/ble, @napplet/nap/webrtc, @napplet/nap/link, @napplet/nap/lists, @napplet/nap/common, and @napplet/nap/serial.
Optional Peer Dependency
@napplet/nap declares json-schema-to-ts@^3.1.1 as an optional peer dependency (via peerDependenciesMeta.json-schema-to-ts.optional: true). Install it in your napplet if you want FromSchema<typeof schema> type inference for your config.subscribe callback — the values parameter is then inferred directly from your schema (enums, required fields, defaults all flow through). Authors who skip it pay no install cost and config.subscribe still works with the default Record<string, unknown> typing.
// With json-schema-to-ts installed — values is fully typed
import { configSubscribe } from '@napplet/nap/config/sdk';
import type { FromSchema } from 'json-schema-to-ts';
const schema = {
type: 'object',
properties: {
relayUrl: { type: 'string', format: 'uri' },
maxResults: { type: 'integer', minimum: 1, default: 20 },
},
required: ['relayUrl'],
} as const;
configSubscribe<FromSchema<typeof schema>>((values) => {
// values.relayUrl: string
// values.maxResults: number | undefined
});Protocol Reference
- NIP-5D — Napplet-shell protocol specification (JSON envelope + NAP negotiation)
License
MIT
Repository: github.com/sandwichfarm/napplet
