feat: add guarded document workbench foundations
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 15s
CI (SIAX Cloud) / security (push) Successful in 16s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 7s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / quality (push) Successful in 1m17s
CI (SIAX Cloud) / integration (pull_request) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m7s
CI (SIAX Cloud) / integration (push) Skipped
CI (SIAX Cloud) / sonar (push) Skipped
masterplan-lock / check (push) Successful in 5s
Security Scan (SIAX Cloud) / security (push) Successful in 15s
CI (SIAX Cloud) / security (push) Successful in 16s
CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 7s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 21s
CI (SIAX Cloud) / security (pull_request) Successful in 25s
CI (SIAX Cloud) / quality (push) Successful in 1m17s
CI (SIAX Cloud) / integration (pull_request) Successful in 54s
CI (SIAX Cloud) / quality (pull_request) Successful in 1m7s
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import { createHash } from 'node:crypto';
|
||||
|
||||
export type DocumentClassification =
|
||||
'public' | 'internal' | 'confidential' | 'restricted' | 'personal-data' | 'financial' | 'legal';
|
||||
export interface Doc0UploadInput {
|
||||
/** Must be verified and authorized by the caller, never sourced from unchecked headers. */
|
||||
tenantId: string;
|
||||
projectId: string;
|
||||
actorId: string;
|
||||
requestId: string;
|
||||
idempotencyKey: string;
|
||||
filename: string;
|
||||
mimeType: string;
|
||||
classification: DocumentClassification;
|
||||
bytes: Uint8Array;
|
||||
}
|
||||
export interface Doc0UploadReceipt {
|
||||
documentId: string;
|
||||
versionId: string;
|
||||
checksumSha256: string;
|
||||
pipelineStage: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal client only. The caller must enforce verified tenant/project/actor
|
||||
* permissions and data handling policy. The coordinated DOC0/N0D/ST0RE
|
||||
* sensitivity handling must be deployed and verified end to end before this
|
||||
* is wired to customer uploads.
|
||||
*/
|
||||
export class Doc0UploadClient {
|
||||
private readonly fetchImpl: typeof fetch;
|
||||
constructor(
|
||||
private readonly options: {
|
||||
baseUrl: string;
|
||||
serviceToken: string;
|
||||
maxBytes: number;
|
||||
timeoutMs?: number;
|
||||
fetchImpl?: typeof fetch;
|
||||
},
|
||||
) {
|
||||
if (
|
||||
!options.baseUrl.trim() ||
|
||||
!options.serviceToken.trim() ||
|
||||
!Number.isSafeInteger(options.maxBytes) ||
|
||||
options.maxBytes <= 0
|
||||
) {
|
||||
throw new Error('DOC0 endpoint, service credential and upload limit are required');
|
||||
}
|
||||
this.fetchImpl = options.fetchImpl ?? fetch;
|
||||
}
|
||||
|
||||
async upload(input: Doc0UploadInput): Promise<Doc0UploadReceipt> {
|
||||
if (
|
||||
![
|
||||
input.tenantId,
|
||||
input.projectId,
|
||||
input.actorId,
|
||||
input.requestId,
|
||||
input.idempotencyKey,
|
||||
input.filename,
|
||||
input.mimeType,
|
||||
].every((value) => typeof value === 'string' && value.trim()) ||
|
||||
!['public', 'internal', 'confidential', 'restricted', 'personal-data', 'financial', 'legal'].includes(
|
||||
input.classification,
|
||||
) ||
|
||||
!(input.bytes instanceof Uint8Array) ||
|
||||
input.bytes.byteLength === 0 ||
|
||||
input.bytes.byteLength > this.options.maxBytes
|
||||
) {
|
||||
throw new Error('Invalid DOC0 upload input');
|
||||
}
|
||||
const expectedHash = createHash('sha256').update(input.bytes).digest('hex');
|
||||
const body = new FormData();
|
||||
body.set(
|
||||
'input',
|
||||
JSON.stringify({
|
||||
projectId: input.projectId,
|
||||
displayName: input.filename,
|
||||
documentType: 'unknown',
|
||||
classification: input.classification,
|
||||
}),
|
||||
);
|
||||
body.set('file', new Blob([new Uint8Array(input.bytes)], { type: input.mimeType }), input.filename);
|
||||
const response = await this.fetchImpl(`${this.options.baseUrl.replace(/\/+$/, '')}/v1/doc0/documents`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
authorization: `Bearer ${this.options.serviceToken}`,
|
||||
'x-tenant-id': input.tenantId,
|
||||
'x-actor-id': input.actorId,
|
||||
'x-request-id': input.requestId,
|
||||
'x-idempotency-key': input.idempotencyKey,
|
||||
},
|
||||
body,
|
||||
signal: AbortSignal.timeout(this.options.timeoutMs ?? 120_000),
|
||||
});
|
||||
if (!response.ok) throw new Error(`DOC0 upload failed: HTTP ${response.status}`);
|
||||
const data = (await response.json()) as {
|
||||
document?: {
|
||||
documentId?: unknown;
|
||||
tenantId?: unknown;
|
||||
projectId?: unknown;
|
||||
currentVersionId?: unknown;
|
||||
};
|
||||
currentVersion?: {
|
||||
versionId?: unknown;
|
||||
tenantId?: unknown;
|
||||
documentId?: unknown;
|
||||
checksumSha256?: unknown;
|
||||
sizeBytes?: unknown;
|
||||
pipelineStage?: unknown;
|
||||
};
|
||||
};
|
||||
const doc = data.document;
|
||||
const version = data.currentVersion;
|
||||
if (
|
||||
!doc ||
|
||||
!version ||
|
||||
typeof doc.documentId !== 'string' ||
|
||||
!doc.documentId ||
|
||||
typeof version.versionId !== 'string' ||
|
||||
!version.versionId ||
|
||||
doc.tenantId !== input.tenantId ||
|
||||
version.tenantId !== input.tenantId ||
|
||||
doc.projectId !== input.projectId ||
|
||||
version.documentId !== doc.documentId ||
|
||||
doc.currentVersionId !== version.versionId ||
|
||||
version.checksumSha256 !== expectedHash ||
|
||||
version.sizeBytes !== input.bytes.byteLength ||
|
||||
typeof version.pipelineStage !== 'string' ||
|
||||
!version.pipelineStage
|
||||
) {
|
||||
throw new Error('DOC0 upload receipt does not match the submitted document');
|
||||
}
|
||||
return {
|
||||
documentId: doc.documentId,
|
||||
versionId: version.versionId,
|
||||
checksumSha256: expectedHash,
|
||||
pipelineStage: version.pipelineStage,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user