feat(sire): TUS.IO upload — reemplazar propuesta + importar comprobantes#6
Merged
Conversation
Adds the second half of SIRE: not just downloading SUNAT's proposal but uploading your own .zip files to replace it or import extra comprobantes. This was the biggest blocker called out in PR #4: SUNAT's manual notes "deben ser desarrollados en JAVA" for these endpoints. That's an implementation suggestion (only Java samples ship), NOT a protocol requirement — TUS.IO is HTTP-based and language-agnostic. Verified by spec review and 15 unit tests. What's new: 1. TUS.IO 1.0.0 client (src/sunat-rest/tus.ts) - encodeMetadata(): pairs of "key base64Value" joined with "," per spec - tusCreate(): POST with Tus-Resumable + Upload-Length + Upload-Metadata, returns Location → uploadUrl (resolves relative against endpoint) - tusHead(): read current Upload-Offset (for resumption — interface ready, automatic retry-from-offset deferred) - tusPatch(): PATCH chunk with Upload-Offset + offset+octet-stream - tusUpload(): high-level create + chunked PATCH loop with onProgress - Default chunk size 8 MB; configurable per upload - Bearer token from oauth.ts 2. SIRE upload wrappers (src/sunat-rest/sire.ts) - sireUpload({kind, codLibro, perTributario, filename, data}, creds) - 5 upload kinds (codProceso from Anexo I): - reemplazoPropuesta (3) - importarPropuestaCp (1) - importarPreliminarCp (4) - ajustesPosteriores (6) - ajustesPosterioresAnteriores (7) - SUNAT-required metadata: filename, filetype, perTributario, codOrigenEnvio (=2), codProceso, codTipoCorrelativo (=01), nomArchivoImportacion, codLibro - Best-effort numTicket extraction from upload Location URL 3. Commands: sunat sire {ventas|compras} {reemplazar|importar} - reemplazar --periodo --file --yes [--wait] [--timeout] [--chunk-size] - importar --periodo --file --tipo X --yes [--wait] - --tipo: propuesta | preliminar | ajustes | ajustes-anteriores - File size validated client-side (0 < size <= 6GB per SUNAT limit 1346) - Progress bar to stderr (suppressed in --json mode) - Audit log entry per upload with bytesSent + numTicket Tests: 238 pass / 2 skip / 0 fail in 2.8s (was 223) 15 new unit tests in tus.test.ts: - encodeMetadata: single key, multiple pairs, UTF-8 multibyte (acentos), empty - TUS_VERSION = 1.0.0 - tusCreate: headers (Tus-Resumable, Upload-Length, Upload-Metadata, Bearer), relative Location resolution, error on non-201, error on missing Location - tusHead: reads Upload-Offset + Upload-Length, errors on missing - tusPatch: PATCH method, Upload-Offset header, offset+octet-stream content-type, body length, error on non-204 - tusUpload: chunked end-to-end (20MB → 3 chunks), respects --chunk-size LIMITATIONS.md updated: - Move Reemplazar / Importar from 🚧 to⚠️ (verified shape, untested live) - New TUS.IO implementation notes section: spec version, chunk size, 6GB limit, metadata encoding, codProceso table, resumability caveat, why we ignored "JAVA required" note - Note ticket extraction is best-effort (SUNAT response shape varies)
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the second half of SIRE: not just downloading SUNAT's proposal but uploading your own .zip files to replace it or import extra comprobantes.
This was the biggest blocker called out in PR #4. SUNAT's manual notes "deben ser desarrollados en JAVA" for these endpoints — that's an implementation suggestion (only Java samples ship), NOT a protocol requirement. TUS.IO is HTTP-based and language-agnostic. Verified by spec review and 15 unit tests.
What's in this PR
TUS.IO 1.0.0 client (
src/sunat-rest/tus.ts)encodeMetadata()— pairs ofkey base64Valuejoined with,per spectusCreate()— POST withTus-Resumable+Upload-Length+Upload-Metadata, returnsLocation→ uploadUrltusHead()— read currentUpload-Offset(interface for resumption)tusPatch()— PATCH chunk withUpload-Offset+application/offset+octet-streamtusUpload()— high-level create + chunked PATCH loop withonProgressSIRE upload wrappers (
src/sunat-rest/sire.ts)5 upload kinds (codProceso from Anexo I):
reemplazoPropuesta/receptorpropuesta/.../uploadimportarPropuestaCp/receptorpropuesta/.../uploadimportarPreliminarCp/receptorpreliminar/.../uploadajustesPosteriores/receptorajustesposteriores/.../uploadajustesPosterioresAnteriores/receptorajustesposteriores/.../uploadSUNAT-required metadata keys:
filename, filetype, perTributario, codOrigenEnvio (=2), codProceso, codTipoCorrelativo (=01), nomArchivoImportacion, codLibroCommands
--jsonmode)bytesSent+numTicket--yesTests
Known limitations (added to LIMITATIONS.md)
numTicketreturns empty, upload itself succeeded but operator pollsconsultaestadoticketsmanually usingperTributario+codProceso.tusHead()is implemented), but currenttusUpload()doesn't auto-retry from last offset on network errors. Re-run command. Future PR.Test plan
bun testgreen (238 pass / 2 skip / 0 fail)sunat sire ventas --helpshowsreemplazar+importarsunat sire compras --helpshows them too