@tearleads/client-sdk 0.1.261 → 0.1.263
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/README.md +1 -1
- package/client/documents/index.d.ts +11 -0
- package/client/documents/index.js +7 -1
- package/client/documents/index.js.map +1 -1
- package/client/index.d.ts +1 -1
- package/client/index.js.map +1 -1
- package/index.d.ts +1 -1
- package/index.js.map +1 -1
- package/internal/sqlite-worker/crossTabConnectionMachine.js +67 -0
- package/internal/sqlite-worker/crossTabConnectionMachine.js.map +1 -0
- package/internal/sqlite-worker/crossTabLocalClient.js +76 -14
- package/internal/sqlite-worker/crossTabLocalClient.js.map +1 -1
- package/internal/sqlite-worker/crossTabProtocol.js +17 -0
- package/internal/sqlite-worker/crossTabProtocol.js.map +1 -1
- package/internal/sqlite-worker/crossTabRuntime.js +20 -18
- package/internal/sqlite-worker/crossTabRuntime.js.map +1 -1
- package/internal/sqlite-worker/runtime.d.ts +1 -1
- package/internal/sqlite-worker/runtime.js +1 -0
- package/internal/sqlite-worker/runtime.js.map +1 -1
- package/internal/sqlite-worker/types.d.ts +7 -0
- package/internal/sqlite-worker/types.js +12 -0
- package/internal/sqlite-worker/types.js.map +1 -1
- package/internal/sqlite-worker/workerCrash.d.ts +23 -0
- package/internal/sqlite-worker/workerCrash.js +17 -0
- package/internal/sqlite-worker/workerCrash.js.map +1 -1
- package/package.json +1 -1
- package/sqlite/index.d.ts +1 -1
- package/sqlite/index.js +1 -1
- package/sqlite/index.js.map +1 -1
- package/sqlite/worker.js +4 -4
- package/stores/documents/downloadRetry.d.ts +16 -0
- package/stores/documents/downloadRetry.js +28 -0
- package/stores/documents/downloadRetry.js.map +1 -0
- package/stores/documents/index.d.ts +1 -0
- package/stores/documents/index.js +1 -0
- package/stores/documents/index.js.map +1 -1
- package/workflows/container-contents/documentQueries/pendingDownloads.d.ts +23 -2
- package/workflows/container-contents/documentQueries/pendingDownloads.js +63 -1
- package/workflows/container-contents/documentQueries/pendingDownloads.js.map +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { DocumentsRuntime } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Ask one synced document to pull again and re-list its attachment bindings,
|
|
4
|
+
* e.g. for a pending first download in Explorer. The request names the
|
|
5
|
+
* document id it was read with: when the local record has since been relinked
|
|
6
|
+
* to another document, nothing runs and the caller re-reads its queue.
|
|
7
|
+
*
|
|
8
|
+
* The store is resolved by local id only, so an already-open store is reused
|
|
9
|
+
* as-is (its runtime, draft and identity untouched) and a retry never maps one
|
|
10
|
+
* local record onto another document's store.
|
|
11
|
+
*/
|
|
12
|
+
export declare function retryDocumentDownload(input: {
|
|
13
|
+
documentId: string;
|
|
14
|
+
localId: string;
|
|
15
|
+
workflowRuntime: (containerId: string | null) => DocumentsRuntime;
|
|
16
|
+
}): Promise<boolean>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defaultDocumentsPersistence } from "../../workflows/documents/index.js";
|
|
2
|
+
import { getOrCreateDocumentStore } from "./documentStore/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Ask one synced document to pull again and re-list its attachment bindings,
|
|
5
|
+
* e.g. for a pending first download in Explorer. The request names the
|
|
6
|
+
* document id it was read with: when the local record has since been relinked
|
|
7
|
+
* to another document, nothing runs and the caller re-reads its queue.
|
|
8
|
+
*
|
|
9
|
+
* The store is resolved by local id only, so an already-open store is reused
|
|
10
|
+
* as-is (its runtime, draft and identity untouched) and a retry never maps one
|
|
11
|
+
* local record onto another document's store.
|
|
12
|
+
*/
|
|
13
|
+
export async function retryDocumentDownload(input) {
|
|
14
|
+
const { execSql, dbStatus } = input.workflowRuntime(null).infra;
|
|
15
|
+
if (dbStatus !== "ready")
|
|
16
|
+
return false;
|
|
17
|
+
const record = await defaultDocumentsPersistence.loadDocument(execSql, input.localId);
|
|
18
|
+
if (!record || record.documentId !== input.documentId)
|
|
19
|
+
return false;
|
|
20
|
+
const runtime = input.workflowRuntime(record.containerId);
|
|
21
|
+
const store = getOrCreateDocumentStore(runtime.state.domainScope, input.localId, runtime, null, "", record.documentKind);
|
|
22
|
+
// A store nothing has opened yet loads its record before it can sync.
|
|
23
|
+
if (!(await store.ensureInitialized()))
|
|
24
|
+
return false;
|
|
25
|
+
store.requestRemoteSync();
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=downloadRetry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"downloadRetry.js","sourceRoot":"","sources":["downloadRetry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,2BAA2B,CAAC;AACxE,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAC;AAG3D;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,KAI3C;IACC,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC;IAChE,IAAI,QAAQ,KAAK,OAAO;QAAE,OAAO,KAAK,CAAC;IACvC,MAAM,MAAM,GAAG,MAAM,2BAA2B,CAAC,YAAY,CAC3D,OAAO,EACP,KAAK,CAAC,OAAO,CACd,CAAC;IACF,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,UAAU,KAAK,KAAK,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IACpE,MAAM,OAAO,GAAG,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC1D,MAAM,KAAK,GAAG,wBAAwB,CACpC,OAAO,CAAC,KAAK,CAAC,WAAW,EACzB,KAAK,CAAC,OAAO,EACb,OAAO,EACP,IAAI,EACJ,EAAE,EACF,MAAM,CAAC,YAAY,CACpB,CAAC;IACF,sEAAsE;IACtE,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,iBAAiB,EAAE,CAAC;QAAE,OAAO,KAAK,CAAC;IACrD,KAAK,CAAC,iBAAiB,EAAE,CAAC;IAC1B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { defaultDocumentsPersistence } from \"../../workflows/documents\";\nimport { getOrCreateDocumentStore } from \"./documentStore\";\nimport type { DocumentsRuntime } from \"./types\";\n\n/**\n * Ask one synced document to pull again and re-list its attachment bindings,\n * e.g. for a pending first download in Explorer. The request names the\n * document id it was read with: when the local record has since been relinked\n * to another document, nothing runs and the caller re-reads its queue.\n *\n * The store is resolved by local id only, so an already-open store is reused\n * as-is (its runtime, draft and identity untouched) and a retry never maps one\n * local record onto another document's store.\n */\nexport async function retryDocumentDownload(input: {\n documentId: string;\n localId: string;\n workflowRuntime: (containerId: string | null) => DocumentsRuntime;\n}): Promise<boolean> {\n const { execSql, dbStatus } = input.workflowRuntime(null).infra;\n if (dbStatus !== \"ready\") return false;\n const record = await defaultDocumentsPersistence.loadDocument(\n execSql,\n input.localId,\n );\n if (!record || record.documentId !== input.documentId) return false;\n const runtime = input.workflowRuntime(record.containerId);\n const store = getOrCreateDocumentStore(\n runtime.state.domainScope,\n input.localId,\n runtime,\n null,\n \"\",\n record.documentKind,\n );\n // A store nothing has opened yet loads its record before it can sync.\n if (!(await store.ensureInitialized())) return false;\n store.requestRemoteSync();\n return true;\n}\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from "./documentStore/index.js";
|
|
2
2
|
export { createSeededDocument, type SeededDocumentInput, } from "./documentStore/initialization/createSeededDocument.js";
|
|
3
|
+
export { retryDocumentDownload } from "./downloadRetry.js";
|
|
3
4
|
export { hasPendingLocalDocumentWrites } from "./registry.js";
|
|
4
5
|
export * from "./types.js";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from "./documentStore/index.js";
|
|
2
2
|
export { createSeededDocument, } from "./documentStore/initialization/createSeededDocument.js";
|
|
3
|
+
export { retryDocumentDownload } from "./downloadRetry.js";
|
|
3
4
|
export { hasPendingLocalDocumentWrites } from "./registry.js";
|
|
4
5
|
export * from "./types.js";
|
|
5
6
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,OAAO,EACL,oBAAoB,GAErB,MAAM,qDAAqD,CAAC;AAC7D,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAC3D,cAAc,SAAS,CAAC","sourcesContent":["export * from \"./documentStore\";\nexport {\n createSeededDocument,\n type SeededDocumentInput,\n} from \"./documentStore/initialization/createSeededDocument\";\nexport { hasPendingLocalDocumentWrites } from \"./registry\";\nexport * from \"./types\";\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,OAAO,EACL,oBAAoB,GAErB,MAAM,qDAAqD,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,6BAA6B,EAAE,MAAM,YAAY,CAAC;AAC3D,cAAc,SAAS,CAAC","sourcesContent":["export * from \"./documentStore\";\nexport {\n createSeededDocument,\n type SeededDocumentInput,\n} from \"./documentStore/initialization/createSeededDocument\";\nexport { retryDocumentDownload } from \"./downloadRetry\";\nexport { hasPendingLocalDocumentWrites } from \"./registry\";\nexport * from \"./types\";\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExecSql } from "../../../data/sqlite/sqlSchema.js";
|
|
2
|
-
|
|
2
|
+
interface PendingContentDownload {
|
|
3
3
|
localId: string;
|
|
4
4
|
documentId: string;
|
|
5
5
|
objectKind: "document" | "container";
|
|
@@ -8,5 +8,26 @@ export interface PendingDownloadQueueItem {
|
|
|
8
8
|
reason: "initial" | "continuation";
|
|
9
9
|
lastError: string | null;
|
|
10
10
|
}
|
|
11
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* The first download of a file a synced document records for a slot. The row
|
|
13
|
+
* carries the identity hydration fetches with: the slot and the stored
|
|
14
|
+
* record's document id.
|
|
15
|
+
*/
|
|
16
|
+
interface PendingAttachmentDownload {
|
|
17
|
+
localId: string;
|
|
18
|
+
documentId: string;
|
|
19
|
+
objectKind: "attachment";
|
|
20
|
+
/** The owning document's projected title. */
|
|
21
|
+
title: string;
|
|
22
|
+
reason: "initial";
|
|
23
|
+
lastError: string | null;
|
|
24
|
+
slotId: string;
|
|
25
|
+
fileName: string;
|
|
26
|
+
}
|
|
27
|
+
export type PendingDownloadQueueItem = PendingContentDownload | PendingAttachmentDownload;
|
|
28
|
+
/**
|
|
29
|
+
* Durable, already-discovered content pulls, including folder metadata, then
|
|
30
|
+
* the first downloads of files recorded by synced documents.
|
|
31
|
+
*/
|
|
12
32
|
export declare function listPendingDownloads(execSql: ExecSql): Promise<ReadonlyArray<PendingDownloadQueueItem>>;
|
|
33
|
+
export {};
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { ensureContainerTables } from "../../../data/persistence/container-contents/containerPersistence.js";
|
|
2
2
|
import { sqlDocumentsPersistence } from "../../../data/persistence/documents/documentsPersistence.js";
|
|
3
3
|
import { emptyContentFrontierSql } from "./hydrationSql.js";
|
|
4
|
-
/**
|
|
4
|
+
/**
|
|
5
|
+
* Durable, already-discovered content pulls, including folder metadata, then
|
|
6
|
+
* the first downloads of files recorded by synced documents.
|
|
7
|
+
*/
|
|
5
8
|
export async function listPendingDownloads(execSql) {
|
|
6
9
|
await ensureContainerTables(execSql);
|
|
7
10
|
await sqlDocumentsPersistence.ensureSchema(execSql);
|
|
11
|
+
const contentDownloads = await listPendingContentDownloads(execSql);
|
|
12
|
+
const attachmentDownloads = await listPendingAttachmentDownloads(execSql);
|
|
13
|
+
return [...contentDownloads, ...attachmentDownloads];
|
|
14
|
+
}
|
|
15
|
+
async function listPendingContentDownloads(execSql) {
|
|
8
16
|
const rows = await execSql(`
|
|
9
17
|
SELECT stored.local_id, stored.document_id, stored.app_kind,
|
|
10
18
|
CASE WHEN ${emptyContentFrontierSql("stored")} THEN NULL
|
|
@@ -39,4 +47,58 @@ export async function listPendingDownloads(execSql) {
|
|
|
39
47
|
return [item];
|
|
40
48
|
});
|
|
41
49
|
}
|
|
50
|
+
// `attachment_names` keys each slot recorded by the content as
|
|
51
|
+
// `<contentSha256>:<slotId>`; the hex digest never contains a colon, while a
|
|
52
|
+
// collection slot id (`photos:<uuid>`) can. Any local row for the slot, held
|
|
53
|
+
// or awaiting a remote detach, means this is no longer a first download, and a
|
|
54
|
+
// pending upload is this device's own file.
|
|
55
|
+
async function listPendingAttachmentDownloads(execSql) {
|
|
56
|
+
const rows = await execSql(`
|
|
57
|
+
SELECT stored.local_id, stored.document_id, d.title,
|
|
58
|
+
stored.sync_failure_message AS last_error,
|
|
59
|
+
substr(recorded.key, instr(recorded.key, ':') + 1) AS slot_id,
|
|
60
|
+
json_extract(recorded.value, '$.name') AS file_name
|
|
61
|
+
FROM documents stored
|
|
62
|
+
JOIN document_projection d ON d.local_id = stored.local_id
|
|
63
|
+
JOIN document_projection_text projected
|
|
64
|
+
ON projected.local_id = stored.local_id
|
|
65
|
+
JOIN json_each(projected.attachment_names) recorded
|
|
66
|
+
WHERE stored.app_kind = 'documents'
|
|
67
|
+
AND stored.document_id IS NOT NULL
|
|
68
|
+
AND NOT ${emptyContentFrontierSql("stored")}
|
|
69
|
+
AND projected.attachment_names IS NOT NULL
|
|
70
|
+
AND NOT EXISTS (
|
|
71
|
+
SELECT 1 FROM document_attachment_blob_projection held
|
|
72
|
+
WHERE held.local_id = stored.local_id
|
|
73
|
+
AND held.slot_id = substr(recorded.key, instr(recorded.key, ':') + 1)
|
|
74
|
+
)
|
|
75
|
+
AND NOT EXISTS (
|
|
76
|
+
SELECT 1 FROM document_pending_attachments upload
|
|
77
|
+
WHERE upload.local_id = stored.local_id
|
|
78
|
+
AND upload.slot_id = substr(recorded.key, instr(recorded.key, ':') + 1)
|
|
79
|
+
)
|
|
80
|
+
ORDER BY stored.updated_at, stored.local_id, slot_id
|
|
81
|
+
`);
|
|
82
|
+
return rows.flatMap((row) => {
|
|
83
|
+
const { local_id: localId, document_id: documentId, title, last_error: lastError, slot_id: slotId, file_name: fileName, } = row;
|
|
84
|
+
if (typeof localId !== "string" ||
|
|
85
|
+
typeof documentId !== "string" ||
|
|
86
|
+
typeof title !== "string" ||
|
|
87
|
+
typeof slotId !== "string" ||
|
|
88
|
+
slotId.length === 0 ||
|
|
89
|
+
typeof fileName !== "string")
|
|
90
|
+
return [];
|
|
91
|
+
const item = {
|
|
92
|
+
localId,
|
|
93
|
+
documentId,
|
|
94
|
+
objectKind: "attachment",
|
|
95
|
+
title,
|
|
96
|
+
reason: "initial",
|
|
97
|
+
lastError: typeof lastError === "string" ? lastError : null,
|
|
98
|
+
slotId,
|
|
99
|
+
fileName,
|
|
100
|
+
};
|
|
101
|
+
return [item];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
42
104
|
//# sourceMappingURL=pendingDownloads.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pendingDownloads.js","sourceRoot":"","sources":["pendingDownloads.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mEAAmE,CAAC;AAC1G,OAAO,EAAE,uBAAuB,EAAE,MAAM,0DAA0D,CAAC;AAEnG,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"pendingDownloads.js","sourceRoot":"","sources":["pendingDownloads.ts"],"names":[],"mappings":"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","sourcesContent":["import { ensureContainerTables } from \"../../../data/persistence/container-contents/containerPersistence\";\nimport { sqlDocumentsPersistence } from \"../../../data/persistence/documents/documentsPersistence\";\nimport type { ExecSql } from \"../../../data/sqlite/sqlSchema\";\nimport { emptyContentFrontierSql } from \"./hydrationSql\";\n\ninterface PendingContentDownload {\n localId: string;\n documentId: string;\n objectKind: \"document\" | \"container\";\n /** Null until content supplies a reliable title and type. */\n title: string | null;\n reason: \"initial\" | \"continuation\";\n lastError: string | null;\n}\n\n/**\n * The first download of a file a synced document records for a slot. The row\n * carries the identity hydration fetches with: the slot and the stored\n * record's document id.\n */\ninterface PendingAttachmentDownload {\n localId: string;\n documentId: string;\n objectKind: \"attachment\";\n /** The owning document's projected title. */\n title: string;\n reason: \"initial\";\n lastError: string | null;\n slotId: string;\n fileName: string;\n}\n\nexport type PendingDownloadQueueItem =\n | PendingContentDownload\n | PendingAttachmentDownload;\n\n/**\n * Durable, already-discovered content pulls, including folder metadata, then\n * the first downloads of files recorded by synced documents.\n */\nexport async function listPendingDownloads(\n execSql: ExecSql,\n): Promise<ReadonlyArray<PendingDownloadQueueItem>> {\n await ensureContainerTables(execSql);\n await sqlDocumentsPersistence.ensureSchema(execSql);\n const contentDownloads = await listPendingContentDownloads(execSql);\n const attachmentDownloads = await listPendingAttachmentDownloads(execSql);\n return [...contentDownloads, ...attachmentDownloads];\n}\n\nasync function listPendingContentDownloads(\n execSql: ExecSql,\n): Promise<ReadonlyArray<PendingContentDownload>> {\n const rows = await execSql(`\n SELECT stored.local_id, stored.document_id, stored.app_kind,\n CASE WHEN ${emptyContentFrontierSql(\"stored\")} THEN NULL\n WHEN stored.app_kind = 'documents' THEN d.title\n ELSE folder.display_name END AS title,\n CASE WHEN ${emptyContentFrontierSql(\"stored\")} THEN 'initial'\n ELSE 'continuation' END AS reason,\n stored.sync_failure_message AS last_error\n FROM documents stored\n LEFT JOIN document_projection d ON stored.app_kind = 'documents'\n AND d.local_id = stored.local_id\n LEFT JOIN containers folder ON stored.app_kind = 'container-metadata'\n AND folder.id = stored.local_id\n WHERE stored.document_id IS NOT NULL\n AND (${emptyContentFrontierSql(\"stored\")} OR stored.pull_continuation IS NOT NULL)\n AND ((stored.app_kind = 'documents' AND d.local_id IS NOT NULL)\n OR (stored.app_kind = 'container-metadata' AND folder.id IS NOT NULL))\n ORDER BY stored.updated_at, stored.app_kind, stored.local_id\n `);\n return rows.flatMap((row) => {\n const {\n local_id: localId,\n document_id: documentId,\n app_kind: appKind,\n title,\n reason,\n last_error: lastError,\n } = row;\n if (typeof localId !== \"string\" || typeof documentId !== \"string\")\n return [];\n const item: PendingContentDownload = {\n localId,\n documentId,\n objectKind: appKind === \"documents\" ? \"document\" : \"container\",\n title: typeof title === \"string\" ? title : null,\n reason: reason === \"initial\" ? \"initial\" : \"continuation\",\n lastError: typeof lastError === \"string\" ? lastError : null,\n };\n return [item];\n });\n}\n\n// `attachment_names` keys each slot recorded by the content as\n// `<contentSha256>:<slotId>`; the hex digest never contains a colon, while a\n// collection slot id (`photos:<uuid>`) can. Any local row for the slot, held\n// or awaiting a remote detach, means this is no longer a first download, and a\n// pending upload is this device's own file.\nasync function listPendingAttachmentDownloads(\n execSql: ExecSql,\n): Promise<ReadonlyArray<PendingAttachmentDownload>> {\n const rows = await execSql(`\n SELECT stored.local_id, stored.document_id, d.title,\n stored.sync_failure_message AS last_error,\n substr(recorded.key, instr(recorded.key, ':') + 1) AS slot_id,\n json_extract(recorded.value, '$.name') AS file_name\n FROM documents stored\n JOIN document_projection d ON d.local_id = stored.local_id\n JOIN document_projection_text projected\n ON projected.local_id = stored.local_id\n JOIN json_each(projected.attachment_names) recorded\n WHERE stored.app_kind = 'documents'\n AND stored.document_id IS NOT NULL\n AND NOT ${emptyContentFrontierSql(\"stored\")}\n AND projected.attachment_names IS NOT NULL\n AND NOT EXISTS (\n SELECT 1 FROM document_attachment_blob_projection held\n WHERE held.local_id = stored.local_id\n AND held.slot_id = substr(recorded.key, instr(recorded.key, ':') + 1)\n )\n AND NOT EXISTS (\n SELECT 1 FROM document_pending_attachments upload\n WHERE upload.local_id = stored.local_id\n AND upload.slot_id = substr(recorded.key, instr(recorded.key, ':') + 1)\n )\n ORDER BY stored.updated_at, stored.local_id, slot_id\n `);\n return rows.flatMap((row) => {\n const {\n local_id: localId,\n document_id: documentId,\n title,\n last_error: lastError,\n slot_id: slotId,\n file_name: fileName,\n } = row;\n if (\n typeof localId !== \"string\" ||\n typeof documentId !== \"string\" ||\n typeof title !== \"string\" ||\n typeof slotId !== \"string\" ||\n slotId.length === 0 ||\n typeof fileName !== \"string\"\n )\n return [];\n const item: PendingAttachmentDownload = {\n localId,\n documentId,\n objectKind: \"attachment\",\n title,\n reason: \"initial\",\n lastError: typeof lastError === \"string\" ? lastError : null,\n slotId,\n fileName,\n };\n return [item];\n });\n}\n"]}
|