import { type fetch } from 'cross-fetch'; import { FetchStrategy, PowerSyncCredentials, PowerSyncLogger } from '@powersync/common'; import { SimpleAsyncIterator } from '../../../utils/stream_transform.js'; import { WebSocketSupport, WebSocketSyncStreamPlatform } from './WebSocketSupport.js'; /** * @internal */ export type RemoteConnector = { fetchCredentials: () => Promise; invalidateCredentials?: () => void; }; /** * @internal */ export type SyncStreamOptions = { path: string; data: unknown; abortSignal: AbortSignal; }; /** * @internal */ export type FetchImplementation = typeof fetch; export declare function lazyFetchImplementation(getFetch: () => FetchImplementation): FetchImplementation; export interface FetchOptions { resource: string; request: RequestInit; expectStreamingResponse: boolean; } /** * @internal */ export interface SocketSyncStreamOptions { path: string; fetchStrategy: FetchStrategy; abortSignal: AbortSignal; data: unknown; } export interface PreparedRequest { url: string; headers: Record; userAgent: string; path: string; } /** * @internal */ export declare abstract class AbstractRemote { protected connector: RemoteConnector; readonly logger: PowerSyncLogger; protected credentials: PowerSyncCredentials | null; constructor(connector: RemoteConnector, logger: PowerSyncLogger); /** * Sends a request using a suitable `fetch` implementation for the current platform. */ protected abstract fetch(options: FetchOptions): Promise; /** * Get credentials currently cached, or fetch new credentials if none are * available. * * These credentials may have expired already. */ getCredentials(): Promise; /** * Fetch a new set of credentials and cache it. * * Until this call succeeds, `getCredentials` will still return the * old credentials. * * This may be called before the current credentials have expired. */ prefetchCredentials(): Promise; /** * Get credentials for PowerSync. * * This should always fetch a fresh set of credentials - don't use cached * values. */ fetchCredentials(): Promise; /*** * Immediately invalidate credentials. * * This may be called when the current credentials have expired. */ invalidateCredentials(): void; getUserAgent(): string; protected buildRequest(path: string): Promise; get(path: string, headers?: Record): Promise; /** * @returns A text decoder decoding UTF-8. This is a method to allow patching it for Hermes which doesn't support the * builtin, without forcing us to bundle a polyfill with `@powersync/common`. */ createTextDecoder(): TextDecoder; createSocket(url: string): WebSocket; /** * Loads `@powersync/shared-internals/websockets`. * * We prefer to load that as a lazy module with a dynamic `import()` expressions on most platforms. An exception is * React Native, where WebSocket support is preferred and we want to load this directly. */ protected abstract loadWebSocketSupport(platform: WebSocketSyncStreamPlatform): Promise; /** * Returns a data stream of sync line data, fetched via RSocket-over-WebSocket. * * The only mechanism to abort the returned stream is to use the abort signal in {@link SocketSyncStreamOptions}. */ socketStreamRaw(options: SocketSyncStreamOptions): Promise>; /** * Posts a `/sync/stream` request, asserts that it completes successfully and returns the streaming response as an * async iterator of byte blobs. * * To cancel the async iterator, use the abort signal from {@link SyncStreamOptions} passed to this method. */ protected fetchStreamRaw(options: SyncStreamOptions): Promise<{ isBson: boolean; stream: SimpleAsyncIterator; }>; /** * Posts a `/sync/stream` request. * * Depending on the `Content-Type` of the response, this returns strings for sync lines or encoded BSON documents as * `Uint8Array`s. */ fetchStream(options: SyncStreamOptions): Promise>; }