@rebasepro/client 0.31.0-canary.g902a0ce → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import { createSchema } from "./schema.js";
7
7
  import { createApiKeys, CreateApiKeysOptions, createPersonalKeys, CreatePersonalKeysOptions } from "./api-keys.js";
8
8
  import { TableClient } from "./table.js";
9
9
  import { createFunctionsClient } from "./functions.js";
10
+ import { type PushClient } from "./push.js";
10
11
  import { RebaseWebSocketClient } from "./websocket.js";
11
12
  import { RebaseRealtimeChannel, type ChannelOptions } from "./realtime-channel.js";
12
13
  import type { OfflineApi, OfflineConfig } from "./offline.js";
@@ -36,6 +37,7 @@ export { SCHEMA_UNAVAILABLE, isSchemaUnavailable } from "./schema.js";
36
37
  export type { SchemaAPI, SchemaTablesOptions, TableDescriptor } from "@rebasepro/types";
37
38
  export type { ApiKeyKind, ApiKeyMasked, ApiKeyWithSecret, CreateApiKeyRequest, CreateApiKeysOptions, CreatePersonalKeyRequest, CreatePersonalKeysOptions, UpdateApiKeyRequest } from "./api-keys.js";
38
39
  export type { FunctionInvokeOptions, FunctionsClient } from "./functions.js";
40
+ export type { PushClient, PushSubscribeOptions, PushRegistrationOptions } from "./push.js";
39
41
  export { RebaseWebSocketClient } from "./websocket.js";
40
42
  export { RebaseRealtimeChannel } from "./realtime-channel.js";
41
43
  export type { PresenceState, PresenceDiff, BroadcastEvent, ChannelTransport, ChannelOptions, ChannelHistoryEntry, ChannelHistoryResult } from "./realtime-channel.js";
@@ -176,6 +178,8 @@ export type CreateRebaseClientResult<DB = Record<string, unknown>> = Omit<Rebase
176
178
  apiKeys: ReturnType<typeof createApiKeys>;
177
179
  personalKeys: ReturnType<typeof createPersonalKeys>;
178
180
  functions: ReturnType<typeof createFunctionsClient>;
181
+ /** Web Push from the browser: subscribe and unsubscribe through a service worker. Needs `push: true` on the backend. */
182
+ push: PushClient;
179
183
  ws?: RebaseWebSocketClient;
180
184
  /**
181
185
  * Broadcast and presence channels.
package/dist/index.es.js CHANGED
@@ -2208,6 +2208,77 @@ function createFunctionsClient(transport) {
2208
2208
  } };
2209
2209
  }
2210
2210
  //#endregion
2211
+ //#region src/push.ts
2212
+ /** A VAPID key, as base64url, in the form `PushManager.subscribe` takes. */
2213
+ function keyBytes(base64url) {
2214
+ const base64 = base64url.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(base64url.length / 4) * 4, "=");
2215
+ const binary = atob(base64);
2216
+ const bytes = new Uint8Array(new ArrayBuffer(binary.length));
2217
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
2218
+ return bytes;
2219
+ }
2220
+ function sameKey(current, wanted) {
2221
+ if (!current) return false;
2222
+ const bytes = new Uint8Array(current);
2223
+ return bytes.length === wanted.length && bytes.every((byte, i) => byte === wanted[i]);
2224
+ }
2225
+ function supported() {
2226
+ return typeof navigator !== "undefined" && "serviceWorker" in navigator && typeof window !== "undefined" && "PushManager" in window && "Notification" in window;
2227
+ }
2228
+ function createPushClient(transport, pushPath = "/push") {
2229
+ const registrationOf = async (options) => {
2230
+ if (!supported()) throw new RebaseClientError$1("This browser does not support push notifications.", { code: "PUSH_UNSUPPORTED" });
2231
+ const registration = options?.registration ?? await navigator.serviceWorker.getRegistration();
2232
+ if (!registration) throw new RebaseClientError$1("Push needs a service worker: register one first, e.g. navigator.serviceWorker.register(\"/sw.js\").", { code: "PUSH_NO_SERVICE_WORKER" });
2233
+ return registration;
2234
+ };
2235
+ return {
2236
+ isSupported: supported,
2237
+ async getSubscription(options) {
2238
+ if (!supported()) return null;
2239
+ const registration = options?.registration ?? await navigator.serviceWorker.getRegistration();
2240
+ return registration ? registration.pushManager.getSubscription() : null;
2241
+ },
2242
+ async subscribe(options) {
2243
+ const registration = await registrationOf(options);
2244
+ if ((Notification.permission === "default" ? await Notification.requestPermission() : Notification.permission) !== "granted") throw new RebaseClientError$1("Notifications are blocked for this site.", { code: "PUSH_PERMISSION_DENIED" });
2245
+ const { key } = await transport.request(`${pushPath}/key`, { method: "GET" });
2246
+ const applicationServerKey = keyBytes(key);
2247
+ let subscription = await registration.pushManager.getSubscription();
2248
+ if (subscription && !sameKey(subscription.options.applicationServerKey, applicationServerKey)) {
2249
+ await subscription.unsubscribe();
2250
+ subscription = null;
2251
+ }
2252
+ subscription ??= await registration.pushManager.subscribe({
2253
+ userVisibleOnly: true,
2254
+ applicationServerKey
2255
+ });
2256
+ const lang = options?.lang ?? (typeof navigator !== "undefined" ? navigator.language : void 0);
2257
+ const { id } = await transport.request(`${pushPath}/subscriptions`, {
2258
+ method: "POST",
2259
+ body: JSON.stringify({
2260
+ subscription: subscription.toJSON(),
2261
+ lang
2262
+ })
2263
+ });
2264
+ return {
2265
+ id,
2266
+ subscription
2267
+ };
2268
+ },
2269
+ async unsubscribe(options) {
2270
+ const subscription = await this.getSubscription(options);
2271
+ if (!subscription) return false;
2272
+ await transport.request(`${pushPath}/subscriptions`, {
2273
+ method: "DELETE",
2274
+ body: JSON.stringify({ endpoint: subscription.endpoint })
2275
+ });
2276
+ await subscription.unsubscribe();
2277
+ return true;
2278
+ }
2279
+ };
2280
+ }
2281
+ //#endregion
2211
2282
  //#region src/storage.ts
2212
2283
  /**
2213
2284
  * Create a StorageSource that talks to the Rebase backend REST API.
@@ -5157,7 +5228,7 @@ var LazyOfflineManager = class {
5157
5228
  keepsLastUser;
5158
5229
  constructor(config, createInner) {
5159
5230
  this.keepsLastUser = config.keepLastUser === true;
5160
- import("./offline-D_wKJcO3.js").then(({ OfflineManager: Engine }) => {
5231
+ import("./offline-ET6xI1ZO.js").then(({ OfflineManager: Engine }) => {
5161
5232
  const engine = new Engine(config, createInner);
5162
5233
  this.engine = engine;
5163
5234
  const queued = this.queued;
@@ -5355,6 +5426,7 @@ function createRebaseClient(options) {
5355
5426
  const personalKeys = createPersonalKeys(transport, options.personalKeys);
5356
5427
  const storage = createStorage(transport);
5357
5428
  const functions = createFunctionsClient(transport);
5429
+ const push = createPushClient(transport);
5358
5430
  const createStorageSource = (storageId) => storageId === DEFAULT_STORAGE_SOURCE_KEY ? storage : createStorage(transport, storageId);
5359
5431
  const storageRegistry = new ClientStorageSourceRegistry();
5360
5432
  storageRegistry.register(DEFAULT_STORAGE_SOURCE_KEY, storage);
@@ -5605,6 +5677,7 @@ function createRebaseClient(options) {
5605
5677
  apiKeys,
5606
5678
  personalKeys,
5607
5679
  functions,
5680
+ push,
5608
5681
  storage,
5609
5682
  storageRegistry,
5610
5683
  createStorageSource,