@nodii/document-cloud-client
v0.10.0
Published
Typed S2S gRPC client for Document Cloud uploads, downloads, metadata and context links.
Readme
@nodii/document-cloud-client
The standard Nodii S2S client for Document Cloud. It hashes content, requests and confirms presigned uploads, sends bytes directly to S3, and links business contexts without carrying document bytes over gRPC.
A caller says what a document is and whose it is, never where it goes. There is
no bucket, key, path or folder parameter: Document Cloud derives the storage key
and the location (DOC-OV-1.2, DOC-OV-1.4). Publish your service's closed list of
kinds once with publishDocumentKinds; an unregistered kind is refused.
const docs = createDocumentCloudClient({
address: process.env.DOCUMENT_CLOUD_GRPC!, tenantId, serviceName: "hr-service",
getS2SToken: ({ audience, scopes }) => tokens.get({ audience, scopes }),
});
const payslip = await docs.uploadDocument({
documentKind: "hr.payslip", owner: { contextKind: "hr.employee", ref: employeeId },
retentionBucket: "statutory", dataClass: "hr_payslip", // what you answer for each file (DOC-OV-1.8)
title: "October payslip", body: pdfBuffer,
contentType: "application/pdf", idempotencyKey: `payslip:${employeeId}:2026-10`,
});
console.log(payslip.documentId);The owner record is linked for you. For a browser upload, get the URL now and confirm once the browser has finished:
const url = await docs.requestUploadUrl({
documentKind: "hr.id_proof", owner: { contextKind: "hr.employee", ref: employeeId },
fileName, contentType, sizeBytes, checksumSha256, idempotencyKey,
});
// The browser PUTs to url.url with url.method and exactly url.headers.
await docs.confirmUpload({ uploadId: url.uploadId, title, idempotencyKey: confirmKey });A one-off copy records where each old object went with recordLegacyKeys, and
rewrites its references from resolveLegacyKeys.
Readable bodies are hashed during a first pass into a private temporary file,
including when sizeBytes is supplied. The PUT then sends that file as a
disk-backed Blob (Bun.file under Bun, fs.openAsBlob under Node), so it is
replayable for retries without retaining the document in memory. Every PUT
carries a real Content-Length and never uses chunked transfer encoding, which
S3 rejects when content-length is a signed header. Pass an AbortSignal as
signal; PUTs also have a 120-second size-scaled overall deadline and a
30-second socket-idle deadline by default. Configure these with
uploadTimeoutMs, uploadMinBytesPerSecond, and uploadSocketIdleTimeoutMs.
The runtime sends the body without progress events, so stalls are detected per
phase. Sending the body and waiting for response headers is bounded by
uploadSocketIdleTimeoutMs plus the time the body needs at
uploadMinBytesPerSecond, so a link slower than that floor is cut off and
retried. When that bound is shorter than the overall deadline, a peer that stops
reading ends the attempt with a retryable UploadTimeoutError before the
overall deadline. With the defaults this covers bodies up to about 22.5 MiB.
For larger bodies the bound reaches the overall deadline, so a stall ends at
the overall deadline with no retry; raise uploadTimeoutMs if that matters.
The socket-idle deadline then applies while the response is read. Each attempt
uses a fresh connection. If a spooled temp file disappears before a retry, the
upload fails with UploadBodyUnavailableError (reason
upload_body_unavailable), which is not retried.
The low-level putOnce / putWithRetry exports take a Blob. They still
accept a Node Readable for compatibility, but buffer it entirely into memory
before sending, so peak memory is at least the body size per attempt. Pass a
Blob, or use uploadDocument / uploadNewVersion, which spool streams to
disk instead.
body accepts Buffer, Uint8Array, or a Node Readable. PUT retries are
limited to timeouts, network failures, and HTTP 5xx; aborts and HTTP 4xx
responses are terminal.
The client defaults to TLS and requests only the scope required by each RPC through
@nodii/grpc-auth (document read/write, kind publishing, the old key map, or
context-link read/write). The caller-provided token function remains responsible for
minting the service token. Local plaintext test peers must opt in with
allowInsecure: true; call close() during graceful shutdown.
downloadStream callers must either consume the returned stream to completion
or call destroy() when abandoning it; otherwise download resources remain
until the deadline expires. Checksum mismatch is reported at EOF; use
downloadBytes when bytes must be verified before use.
