{"_id":"@bagatka/apns","_rev":"2-efcf5d969e8194f9b4a480551a1240df","name":"@bagatka/apns","dist-tags":{"latest":"0.1.0"},"versions":{"0.0.0-stage":{"name":"@bagatka/apns","version":"0.0.0-stage","_id":"@bagatka/apns@0.0.0-stage","stub":true,"maintainers":[{"name":"bagatka","email":"alex.bagatka@gmail.com"}],"dist":{"shasum":"cb88b27aa02b0e31b17413b4edac92e967261464","tarball":"https://registry.npmjs.org/@bagatka/apns/-/apns-0.0.0-stage.tgz","fileCount":2,"integrity":"sha512-xVLkIxpilZCfD47B9S2O+LJiGQm7mMvixev6HWNFLYTkDVhkbUVwwcQ0JXHdwacy7ixnFpXcIH/UJ8na1bLxIg==","signatures":[{"sig":"MEUCIB8wmMvqn33bKQ23OW1EHZVnEZrSV+VaznXbF9oOF/HHAiEAx3CyUNTH1rSUFnEdSCPBT+c2xX6mV2z/tlQgbCUESMc=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":335},"_npmUser":{"name":"bagatka","email":"alex.bagatka@gmail.com"},"description":"Temporary package placeholder for staged publishing","directories":{}},"0.1.0":{"_id":"@bagatka/apns@0.1.0","bugs":{"url":"https://github.com/bagatka/kit/issues"},"dist":{"shasum":"22b4255c8caf30921b64ee6841004274101bf88a","tarball":"https://registry.npmjs.org/@bagatka/apns/-/apns-0.1.0.tgz","integrity":"sha512-nV/n5vmp120oCWG8yrs2EjZErxSBloAvsCtbQVvH9adMx+MWZ0jYeTQhYviSTUvqCG7oln3Rl5HvLwLXwDFN0w==","fileCount":19,"unpackedSize":57656,"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@bagatka%2fapns@0.1.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCtnIP2Pc0a64lQOjuBPN8AI1ZeC2bp02v5MuWfHHmuFAIhAKEe3RyOeq/WXeGAJCHmzpdAvKximpQ1hgM4BHkL/hoF"}]},"name":"@bagatka/apns","type":"module","_from":"file:bagatka-apns-0.1.0.tgz","author":{"name":"Aleksandr Bagatka"},"exports":{".":{"types":"./dist/index.d.ts","default":"./dist/index.js"}},"license":"MIT","version":"0.1.0","_npmUser":{"name":"bagatka","email":"alex.bagatka@gmail.com","approver":{"name":"bagatka","email":"alex.bagatka@gmail.com"}},"homepage":"https://github.com/bagatka/kit/tree/main/packages/apns#readme","keywords":["apns","apple","effect","ios","live-activity","notifications","push","push-notifications","voip"],"_resolved":"/home/runner/work/_temp/release/bagatka-apns-0.1.0.tgz","_integrity":"sha512-nV/n5vmp120oCWG8yrs2EjZErxSBloAvsCtbQVvH9adMx+MWZ0jYeTQhYviSTUvqCG7oln3Rl5HvLwLXwDFN0w==","repository":{"url":"git+https://github.com/bagatka/kit.git","type":"git","directory":"packages/apns"},"_npmVersion":"12.2.1","description":"Sends push notifications to Apple devices through APNs with a token-signing key, every push type, with Effect.","directories":{},"maintainers":[{"name":"bagatka","email":"alex.bagatka@gmail.com"}],"sideEffects":false,"_nodeVersion":"26.12.0","dependencies":{"@bagatka/jwt":"0.1.0"},"peerDependencies":{"effect":"~4.0.6"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/apns_0.1.0_1792943633664_0.5230062707964038"},"_hasShrinkwrap":false}},"time":{"created":"2026-10-25T15:51:03.096Z","modified":"2026-10-25T15:53:54.042Z","0.0.0-stage":"2026-10-25T15:51:03.096Z","0.1.0":"2026-10-25T15:53:53.756Z"},"description":"Sends push notifications to Apple devices through APNs with a token-signing key, every push type, with Effect.","maintainers":[{"name":"bagatka","email":"alex.bagatka@gmail.com"}],"readme":"# @bagatka/apns\n\nSends push notifications to Apple devices through APNs with a token-signing key, every push type,\nwith Effect.\n\n## Install\n\n```sh\nnpm install @bagatka/apns effect\n```\n\nIts peer dependency is `effect`, at the minor version it is built with: your program uses that one\ntoo. It signs with the platform's Web Crypto, which the host passes, and sends through the Effect\n`HttpClient` the host provides, which must speak HTTP/2, the only protocol APNs speaks. Effect's\n`FetchHttpClient` does on Cloudflare Workers once deployed, Node 26, and Deno 2. Bun 1.4's `fetch`\ndoesn't, so there every push fails with `ApnsUnavailable`.\n\n## Use it\n\n```ts\nimport { Apns, DeviceToken } from \"@bagatka/apns\"\nimport { Effect, Layer, Redacted, Schema } from \"effect\"\nimport { FetchHttpClient } from \"effect/http\"\n\ndeclare const p8Text: string // the text of a token-signing key's .p8 file\ndeclare const registered: string // the device token the app sent you\n\n// In the host, once: fails with ApnsCredentialsInvalid if the key or IDs can't sign APNs's tokens.\nconst ApnsLive = Apns.layer(\n  { key: Redacted.make(p8Text), keyId: \"ABCDE12345\", teamId: \"TEAM123456\" },\n  crypto.subtle,\n).pipe(Layer.provide(FetchHttpClient.layer))\n\nconst program = Effect.gen(function* () {\n  const apns = yield* Apns\n  // Where the app's device token comes in: hex, or a SchemaError.\n  const deviceToken = yield* Schema.decodeUnknownEffect(DeviceToken)(registered)\n  const answer = yield* apns.send({\n    deviceToken,\n    environment: \"production\",\n    bundleId: \"com.example.notes\",\n    pushType: \"alert\",\n    priority: 10,\n    payload: { aps: { alert: { title: \"Notes · Down\", body: \"/health has failed for 3 minutes\" } } },\n  })\n  switch (answer._tag) {\n    case \"Accepted\": // { apnsId }\n      return\n    case \"Inactive\": // { reason: \"Unregistered\", since }: the app no longer has this token.\n      return yield* Effect.log(\"Forget the token, unless the app registered it again after\", answer.since)\n    case \"Rejected\": // { status: 400, reason: \"BadDeviceToken\" }: sending it again won't help.\n      return yield* Effect.logWarning(`APNs refused the push: ${answer.reason}`)\n  }\n}).pipe(Effect.provide(ApnsLive))\n```\n\nWhen APNs can't be reached, doesn't answer in 10 seconds, or answers 429 or a server error, `send`\nfails with `ApnsUnavailable`: the push may be sent again later. When APNs refuses the credentials\nthemselves (a key that was revoked, or the wrong key or team ID), `send` fails with\n`ApnsCredentialsRefused`: every push will until the settings change, so alert on it, and don't\nforget the device token.\n\n## Contract\n\n`src/index.ts`. Callers build the layer with a key and the platform's Web Crypto, and send one push\nat a time, getting APNs's answer: accepted, inactive, or rejected with Apple's reason.\n\n## Does not own\n\n- **What a push says,** its payload, which is the app's.\n- **Which device tokens a user has,** and when to forget one: an `Inactive` answer is the signal to\n  forget a token, unless the app registered it again after `since`.\n- **Sending again** after `ApnsUnavailable`: Apple asks for 15 minutes after a server error.\n- **Certificates.** Only token-based authentication, which every push type takes.\n\n## Depends on\n\n`@bagatka/jwt`, to sign its provider tokens; `effect`; the platform's Web Crypto, as\n`crypto.subtle`; and an `HttpClient` the host provides.\n\n## Settings\n\nThe key's `.p8` text, or its base64 alone, its key ID, and its team ID; they differ per Apple\ndeveloper account. The environment is per push, since a device token belongs to one APNs:\ndevelopment for builds Xcode installs, production for TestFlight and the App Store.\n\n## Decisions and constraints\n\n- **Trusts its caller, its key, and APNs,** reached over TLS; an answer of another shape is still a\n  failure, never a guess. **Doesn't trust device tokens,** which come from apps through the caller\n  and are checked before any request.\n- **Every push type, and its topic.** `pushType` is APNs's `apns-push-type`; the topic is the bundle\n  ID with the suffix that type needs (`.voip`, `.push-type.liveactivity`, `.location-query`, and the\n  rest), which this package writes, so a push can't name the wrong one. `mdm`, whose topic comes\n  from a certificate, isn't supported.\n- **A bad key fails the layer,** never a push: a key that isn't P-256 in PKCS #8, or IDs that\n  aren't 10 capital letters and digits, fail with `ApnsCredentialsInvalid` when the layer is built,\n  so no push is ever answered as rejected for the key, and no caller forgets device tokens over it.\n  A key that imports but that APNs refuses, 403 `InvalidProviderToken`, can only be found by\n  sending: it is `ApnsCredentialsRefused`, an error, never a `Rejected` answer, for the same reason.\n- **Inactive is its own answer.** Only APNs's 410 says a token is gone; `BadDeviceToken` also\n  answers a token sent to the wrong environment, and `DeviceTokenNotForTopic` a push with the wrong\n  bundle ID, so forgetting tokens on those would forget every token over a setting.\n- **One provider token for 50 minutes, signed once.** APNs takes a token for an hour and refuses\n  one renewed more often than every 20 minutes. Pushes sent together share one token, signed\n  while the others wait; a token APNs calls expired anyway is signed again, once, and the push sent\n  once more.\n- **What goes into a request is checked first:** a device token is a `DeviceToken`, hex, decoded\n  where the product reads it, so it is one path segment; a bundle ID of letters, digits, `-` and\n  `.`, and a collapse ID of at most 64 characters of printable ASCII, or the push is a bug and dies,\n  sending nothing, never one to send again later.\n- **10 seconds for the whole answer,** its body included: an answer that stalls part-way is\n  `ApnsUnavailable`.\n- **Expiration is explicit:** a time, until which APNs keeps trying; \"immediately\", to deliver it\n  once and never keep it; or absent, for Apple's policy of up to 30 days.\n- **An answer that isn't APNs's** (no JSON body, or a reason that isn't one of Apple's camel-case\n  words) is `ApnsUnavailable`, never a reason made up or passed into logs.\n- **Traced without the device token.** `Apns.send` is a span with the push type, the environment,\n  and APNs's answer and reason (`apns.answer`, `apns.reason`); each request in it is an HTTP client\n  span with OpenTelemetry's attributes and the path as `url.template`, `/3/device/{deviceToken}`,\n  never the token, which is a device's identifier. That span is this package's, not Effect's\n  `HttpClient`'s, so no `traceparent` goes to APNs. It logs nothing (PATTERNS.md, \"Calling a\n  service\").\n- **HTTP/2 is the host's.** APNs speaks only HTTP/2; a client of HTTP/1.1 can't read its answer.\n  `fetch` speaks it on deployed Workers, Node 26 and Deno 2, as pushes to APNs's sandbox show; Bun\n  1.4's doesn't, and neither does workerd's under `wrangler dev`. Not reaching APNs is\n  `ApnsUnavailable`, which reads as an outage: on Bun, every push is.\n","readmeFilename":"README.md","homepage":"https://github.com/bagatka/kit/tree/main/packages/apns#readme","keywords":["apns","apple","effect","ios","live-activity","notifications","push","push-notifications","voip"],"repository":{"url":"git+https://github.com/bagatka/kit.git","type":"git","directory":"packages/apns"},"author":{"name":"Aleksandr Bagatka"},"bugs":{"url":"https://github.com/bagatka/kit/issues"},"license":"MIT"}