-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
575 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
type DocClock, | ||
type DocClocks, | ||
type DocRecord, | ||
DocStorageBase, | ||
type DocStorageOptions, | ||
type DocUpdate, | ||
} from '../../storage'; | ||
import { HttpConnection } from './http'; | ||
|
||
interface CloudDocStorageOptions extends DocStorageOptions { | ||
serverBaseUrl: string; | ||
} | ||
|
||
export class StaticCloudDocStorage extends DocStorageBase<CloudDocStorageOptions> { | ||
override connection = new HttpConnection(this.options.serverBaseUrl); | ||
override async pushDocUpdate( | ||
update: DocUpdate, | ||
_origin?: string | ||
): Promise<DocClock> { | ||
// http is readonly | ||
return { docId: update.docId, timestamp: new Date() }; | ||
} | ||
override async getDocTimestamp(docId: string): Promise<DocClock | null> { | ||
// http doesn't support this, so we just return a new timestamp | ||
return { | ||
docId, | ||
timestamp: new Date(), | ||
}; | ||
} | ||
override async getDocTimestamps(): Promise<DocClocks> { | ||
// http doesn't support this | ||
return {}; | ||
} | ||
override deleteDoc(_docId: string): Promise<void> { | ||
// http is readonly | ||
return Promise.resolve(); | ||
} | ||
protected override async getDocSnapshot( | ||
docId: string | ||
): Promise<DocRecord | null> { | ||
const arrayBuffer = await this.connection.fetchArrayBuffer( | ||
`/api/workspaces/${this.spaceId}/docs/${docId}`, | ||
{ | ||
priority: 'high', | ||
headers: { | ||
Accept: 'application/octet-stream', // this is necessary for ios native fetch to return arraybuffer | ||
}, | ||
} | ||
); | ||
if (!arrayBuffer) { | ||
return null; | ||
} | ||
return { | ||
docId: docId, | ||
bin: new Uint8Array(arrayBuffer), | ||
timestamp: new Date(), | ||
}; | ||
} | ||
protected override setDocSnapshot( | ||
_snapshot: DocRecord, | ||
_prevSnapshot: DocRecord | null | ||
): Promise<boolean> { | ||
// http is readonly | ||
return Promise.resolve(false); | ||
} | ||
protected override getDocUpdates(_docId: string): Promise<DocRecord[]> { | ||
return Promise.resolve([]); | ||
} | ||
protected override markUpdatesMerged( | ||
_docId: string, | ||
_updates: DocRecord[] | ||
): Promise<number> { | ||
return Promise.resolve(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { gqlFetcherFactory } from '@affine/graphql'; | ||
|
||
import { DummyConnection } from '../../connection'; | ||
|
||
export class HttpConnection extends DummyConnection { | ||
readonly fetch = async (input: string, init?: RequestInit) => { | ||
const externalSignal = init?.signal; | ||
if (externalSignal?.aborted) { | ||
throw externalSignal.reason; | ||
} | ||
const abortController = new AbortController(); | ||
externalSignal?.addEventListener('abort', reason => { | ||
abortController.abort(reason); | ||
}); | ||
|
||
const timeout = 15000; | ||
const timeoutId = setTimeout(() => { | ||
abortController.abort('timeout'); | ||
}, timeout); | ||
|
||
const res = await globalThis | ||
.fetch(new URL(input, this.serverBaseUrl), { | ||
...init, | ||
signal: abortController.signal, | ||
headers: { | ||
...init?.headers, | ||
'x-affine-version': BUILD_CONFIG.appVersion, | ||
}, | ||
}) | ||
.catch(err => { | ||
throw new Error('fetch error: ' + err); | ||
}); | ||
clearTimeout(timeoutId); | ||
if (!res.ok && res.status !== 404) { | ||
let reason: string | any = ''; | ||
if (res.headers.get('Content-Type')?.includes('application/json')) { | ||
try { | ||
reason = await res.json(); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
throw new Error('fetch error status: ' + res.status + ' ' + reason); | ||
} | ||
return res; | ||
}; | ||
|
||
readonly fetchArrayBuffer = async (input: string, init?: RequestInit) => { | ||
const res = await this.fetch(input, init); | ||
if (res.status === 404) { | ||
// 404 | ||
return null; | ||
} | ||
try { | ||
return await res.arrayBuffer(); | ||
} catch (err) { | ||
throw new Error('fetch download error: ' + err); | ||
} | ||
}; | ||
|
||
readonly gql = gqlFetcherFactory( | ||
new URL('/graphql', this.serverBaseUrl).href, | ||
this.fetch | ||
); | ||
|
||
constructor(private readonly serverBaseUrl: string) { | ||
super(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './awareness'; | ||
export * from './blob'; | ||
export * from './doc'; | ||
export * from './doc-static'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.