-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathWebRemote.ts
59 lines (52 loc) · 1.35 KB
/
WebRemote.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
AbstractRemote,
AbstractRemoteOptions,
BSONImplementation,
DEFAULT_REMOTE_LOGGER,
FetchImplementation,
FetchImplementationProvider,
ILogger,
RemoteConnector
} from '@powersync/common';
import { getUserAgentInfo } from './userAgent';
/*
* Depends on browser's implementation of global fetch.
*/
class WebFetchProvider extends FetchImplementationProvider {
getFetch(): FetchImplementation {
return fetch.bind(globalThis);
}
}
export class WebRemote extends AbstractRemote {
private _bson: BSONImplementation | undefined;
constructor(
protected connector: RemoteConnector,
protected logger: ILogger = DEFAULT_REMOTE_LOGGER,
options?: Partial<AbstractRemoteOptions>
) {
super(connector, logger, {
...(options ?? {}),
fetchImplementation: options?.fetchImplementation ?? new WebFetchProvider()
});
}
getUserAgent(): string {
let ua = [super.getUserAgent(), `powersync-web`];
try {
ua.push(...getUserAgentInfo());
} catch (e) {
this.logger.warn('Failed to get user agent info', e);
}
return ua.join(' ');
}
async getBSON(): Promise<BSONImplementation> {
if (this._bson) {
return this._bson;
}
/**
* Dynamic import to be used only when needed.
*/
const { BSON } = await import('bson');
this._bson = BSON;
return this._bson;
}
}