@ddd-ts-kit/kernel 0.111.0 → 0.112.0

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.
Files changed (126) hide show
  1. package/README.fr.md +52 -3
  2. package/README.md +48 -1
  3. package/dist/application/files/content-expectation.d.ts +6 -0
  4. package/dist/application/files/content-expectation.d.ts.map +1 -1
  5. package/dist/application/files/content-matches.d.ts +12 -3
  6. package/dist/application/files/content-matches.d.ts.map +1 -1
  7. package/dist/application/files/content-matches.js +23 -28
  8. package/dist/application/files/content-matches.js.map +1 -1
  9. package/dist/application/files/content-rule-defects.d.ts +9 -0
  10. package/dist/application/files/content-rule-defects.d.ts.map +1 -0
  11. package/dist/application/files/content-rule-defects.js +57 -0
  12. package/dist/application/files/content-rule-defects.js.map +1 -0
  13. package/dist/application/files/content-rule-of.d.ts +11 -0
  14. package/dist/application/files/content-rule-of.d.ts.map +1 -0
  15. package/dist/application/files/content-rule-of.js +28 -0
  16. package/dist/application/files/content-rule-of.js.map +1 -0
  17. package/dist/application/files/content-rule.d.ts +4 -0
  18. package/dist/application/files/content-rule.d.ts.map +1 -0
  19. package/dist/application/files/content-rule.js +2 -0
  20. package/dist/application/files/content-rule.js.map +1 -0
  21. package/dist/application/files/content-rules.d.ts +4 -0
  22. package/dist/application/files/content-rules.d.ts.map +1 -0
  23. package/dist/application/files/content-rules.js +2 -0
  24. package/dist/application/files/content-rules.js.map +1 -0
  25. package/dist/application/files/index.d.ts +10 -0
  26. package/dist/application/files/index.d.ts.map +1 -1
  27. package/dist/application/files/index.js +10 -0
  28. package/dist/application/files/index.js.map +1 -1
  29. package/dist/application/files/is-utf8.d.ts +8 -0
  30. package/dist/application/files/is-utf8.d.ts.map +1 -0
  31. package/dist/application/files/is-utf8.js +16 -0
  32. package/dist/application/files/is-utf8.js.map +1 -0
  33. package/dist/application/files/looks-like-text.d.ts +8 -0
  34. package/dist/application/files/looks-like-text.d.ts.map +1 -0
  35. package/dist/application/files/looks-like-text.js +28 -0
  36. package/dist/application/files/looks-like-text.js.map +1 -0
  37. package/dist/application/files/named-only-media-types.d.ts +6 -0
  38. package/dist/application/files/named-only-media-types.d.ts.map +1 -0
  39. package/dist/application/files/named-only-media-types.js +12 -0
  40. package/dist/application/files/named-only-media-types.js.map +1 -0
  41. package/dist/application/files/signed-media-types.d.ts +3 -0
  42. package/dist/application/files/signed-media-types.d.ts.map +1 -0
  43. package/dist/application/files/signed-media-types.js +13 -0
  44. package/dist/application/files/signed-media-types.js.map +1 -0
  45. package/dist/application/files/stated-content-rule.d.ts +7 -0
  46. package/dist/application/files/stated-content-rule.d.ts.map +1 -0
  47. package/dist/application/files/stated-content-rule.js +2 -0
  48. package/dist/application/files/stated-content-rule.js.map +1 -0
  49. package/dist/application/files/text-media-types.d.ts +3 -0
  50. package/dist/application/files/text-media-types.d.ts.map +1 -0
  51. package/dist/application/files/text-media-types.js +10 -0
  52. package/dist/application/files/text-media-types.js.map +1 -0
  53. package/dist/application/files/text-rule.d.ts +17 -0
  54. package/dist/application/files/text-rule.d.ts.map +1 -0
  55. package/dist/application/files/text-rule.js +2 -0
  56. package/dist/application/files/text-rule.js.map +1 -0
  57. package/dist/application/files/zip-containers.d.ts +3 -0
  58. package/dist/application/files/zip-containers.d.ts.map +1 -0
  59. package/dist/application/files/zip-containers.js +10 -0
  60. package/dist/application/files/zip-containers.js.map +1 -0
  61. package/dist/application/ports/read-model-table.d.ts +7 -0
  62. package/dist/application/ports/read-model-table.d.ts.map +1 -1
  63. package/dist/application/ports/read-model-table.js.map +1 -1
  64. package/dist/application/read-model/read-model-table-state.d.ts +53 -0
  65. package/dist/application/read-model/read-model-table-state.d.ts.map +1 -0
  66. package/dist/application/read-model/read-model-table-state.js +2 -0
  67. package/dist/application/read-model/read-model-table-state.js.map +1 -0
  68. package/dist/index.d.ts +3 -0
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +3 -0
  71. package/dist/index.js.map +1 -1
  72. package/dist/infrastructure/files/file-inspection.js +1 -1
  73. package/dist/infrastructure/files/file-inspection.js.map +1 -1
  74. package/dist/infrastructure/files/file-staging.d.ts +4 -2
  75. package/dist/infrastructure/files/file-staging.d.ts.map +1 -1
  76. package/dist/infrastructure/files/file-staging.js +14 -6
  77. package/dist/infrastructure/files/file-staging.js.map +1 -1
  78. package/dist/infrastructure/read-model/database-failure.d.ts +19 -0
  79. package/dist/infrastructure/read-model/database-failure.d.ts.map +1 -0
  80. package/dist/infrastructure/read-model/database-failure.js +71 -0
  81. package/dist/infrastructure/read-model/database-failure.js.map +1 -0
  82. package/dist/infrastructure/read-model/live-rows-columns-of.d.ts +7 -0
  83. package/dist/infrastructure/read-model/live-rows-columns-of.d.ts.map +1 -0
  84. package/dist/infrastructure/read-model/live-rows-columns-of.js +20 -0
  85. package/dist/infrastructure/read-model/live-rows-columns-of.js.map +1 -0
  86. package/dist/infrastructure/read-model/live-rows-constraint-form.d.ts +11 -0
  87. package/dist/infrastructure/read-model/live-rows-constraint-form.d.ts.map +1 -0
  88. package/dist/infrastructure/read-model/live-rows-constraint-form.js +13 -0
  89. package/dist/infrastructure/read-model/live-rows-constraint-form.js.map +1 -0
  90. package/dist/infrastructure/read-model/live-rows-constraint-of.d.ts +6 -0
  91. package/dist/infrastructure/read-model/live-rows-constraint-of.d.ts.map +1 -0
  92. package/dist/infrastructure/read-model/live-rows-constraint-of.js +18 -0
  93. package/dist/infrastructure/read-model/live-rows-constraint-of.js.map +1 -0
  94. package/dist/infrastructure/read-model/postgres-trace-columns.d.ts +17 -7
  95. package/dist/infrastructure/read-model/postgres-trace-columns.d.ts.map +1 -1
  96. package/dist/infrastructure/read-model/postgres-trace-columns.js +101 -14
  97. package/dist/infrastructure/read-model/postgres-trace-columns.js.map +1 -1
  98. package/dist/infrastructure/read-model/read-model-table-migration.d.ts +31 -0
  99. package/dist/infrastructure/read-model/read-model-table-migration.d.ts.map +1 -0
  100. package/dist/infrastructure/read-model/read-model-table-migration.js +82 -0
  101. package/dist/infrastructure/read-model/read-model-table-migration.js.map +1 -0
  102. package/dist/infrastructure/read-model/read-model-table-not-ready-error.d.ts +21 -5
  103. package/dist/infrastructure/read-model/read-model-table-not-ready-error.d.ts.map +1 -1
  104. package/dist/infrastructure/read-model/read-model-table-not-ready-error.js +123 -10
  105. package/dist/infrastructure/read-model/read-model-table-not-ready-error.js.map +1 -1
  106. package/dist/infrastructure/read-model/read-model-table-notes.d.ts +9 -0
  107. package/dist/infrastructure/read-model/read-model-table-notes.d.ts.map +1 -0
  108. package/dist/infrastructure/read-model/read-model-table-notes.js +36 -0
  109. package/dist/infrastructure/read-model/read-model-table-notes.js.map +1 -0
  110. package/dist/infrastructure/read-model/read-model-table-ready.d.ts +4 -0
  111. package/dist/infrastructure/read-model/read-model-table-ready.d.ts.map +1 -0
  112. package/dist/infrastructure/read-model/read-model-table-ready.js +5 -0
  113. package/dist/infrastructure/read-model/read-model-table-ready.js.map +1 -0
  114. package/dist/infrastructure/read-model/read-model-tables-not-ready-error.d.ts +24 -0
  115. package/dist/infrastructure/read-model/read-model-tables-not-ready-error.d.ts.map +1 -0
  116. package/dist/infrastructure/read-model/read-model-tables-not-ready-error.js +40 -0
  117. package/dist/infrastructure/read-model/read-model-tables-not-ready-error.js.map +1 -0
  118. package/dist/infrastructure/read-model/read-model-tables.d.ts +82 -0
  119. package/dist/infrastructure/read-model/read-model-tables.d.ts.map +1 -0
  120. package/dist/infrastructure/read-model/read-model-tables.js +161 -0
  121. package/dist/infrastructure/read-model/read-model-tables.js.map +1 -0
  122. package/dist/infrastructure/read-model/table-read-model-store.d.ts +7 -0
  123. package/dist/infrastructure/read-model/table-read-model-store.d.ts.map +1 -1
  124. package/dist/infrastructure/read-model/table-read-model-store.js +8 -0
  125. package/dist/infrastructure/read-model/table-read-model-store.js.map +1 -1
  126. package/package.json +1 -1
package/README.fr.md CHANGED
@@ -1,5 +1,5 @@
1
1
  [English](README.md) · **Français**
2
- <!-- translation-of: README.md sha256:849d095a5138f5c4 -->
2
+ <!-- translation-of: README.md sha256:bcc2bfaf4621aaee -->
3
3
 
4
4
  # @ddd-ts-kit/kernel
5
5
 
@@ -1505,8 +1505,10 @@ Une écriture dans le stockage n'est pas annulée avec une transaction.
1505
1505
 
1506
1506
  Pour un gros fichier, le client téléverse directement vers le stockage :
1507
1507
  `uploadUrl(key, { expiresInSeconds, mediaType, accepted, maxBytes })` renvoie le `link` pour le
1508
- client et l'autorisation `pending` (`PendingUpload` : clé, plafond de taille, types de média) que
1509
- la commande enregistre sur l'agrégat ; `confirm(pending)`, appelé par une commande qui relit
1508
+ client et l'autorisation `pending` (`PendingUpload` : clé, plafond de taille, types de média, et
1509
+ la règle `text` et les `contentRules` données avec eux) que la commande enregistre sur
1510
+ l'agrégat ; il lève une erreur quand un type accepté n'a pas de règle de contenu, puisqu'aucun
1511
+ fichier de ce type ne pourrait être confirmé ; `confirm(pending)`, appelé par une commande qui relit
1510
1512
  l'autorisation depuis l'agrégat, jamais depuis le client, vérifie ce qui est arrivé par rapport
1511
1513
  au plafond et sur ses octets (`contentMatches`), l'analyse, et renvoie le `StoredFile`. Aucune
1512
1514
  clé finale ne peut commencer par `staging/` (`assertFinalKey`).
@@ -1517,6 +1519,19 @@ média déclaré (`acceptsMediaType`, `contentMatches` : un PDF commence par `%P
1517
1519
  bureautiques sont des archives zip, le texte est de l'UTF-8 sans caractères de contrôle, SVG et
1518
1520
  les formats binaires inconnus ne correspondent jamais).
1519
1521
 
1522
+ Chaque type de média a une règle de contenu (`contentRuleOf`) : une signature
1523
+ (`SIGNED_MEDIA_TYPES`), le texte (`TEXT_MEDIA_TYPES`), ou celle que l'attente énonce dans
1524
+ `contentRules` pour un type pour lequel le kit n'en a pas, `text` ou `unchecked` (les octets
1525
+ sont pris tels quels) ; une règle énoncée pour une famille (`image/*`) n'atteint jamais un type
1526
+ qu'un navigateur exécute (`NAMED_ONLY_MEDIA_TYPES` : SVG, HTML, JavaScript), qui s'énonce par
1527
+ son nom. `contentRuleDefects` liste, avant qu'un contenu n'arrive, les types
1528
+ acceptés qu'aucun contenu ne pourrait prouver et les règles énoncées qui ne font rien :
1529
+ `@ddd-ts-kit/http` refuse une route là-dessus, `uploadUrl` un lien. L'attente dit aussi le
1530
+ texte qu'elle prend (`TextRule`) : `utf-8`, le défaut, ou `single-byte`, qui laisse aussi passer
1531
+ un texte en Windows-1252 ou en ISO-8859-1 (aucun NUL, aucun caractère de contrôle, aucune marque
1532
+ d'ordre d'octets UTF-16 ou UTF-32) et refuse toujours un fichier binaire. Le kit ne décode
1533
+ jamais les octets : `isUtf8(bytes)` dit à un lecteur quel décodeur prendre.
1534
+
1520
1535
  Connecteurs : `InMemoryFileStorage` pour les tests (`keys()` liste ce qu'il contient) et
1521
1536
  `LocalFileStorage({ directory, baseUrl, secret })` pour le développement, tous deux
1522
1537
  `ServedFileStorage` : leurs liens signés pointent vers l'application, qui y répond avec
@@ -2085,6 +2100,40 @@ adaptateur (trois instructions et la requête d'un plan) : `InMemoryReadModelSto
2085
2100
  *fakes*, `KnexReadModelStore`, `DrizzleReadModelStore`, `PrismaReadModelStore` et
2086
2101
  `TypeOrmReadModelStore` dans les *packages* d'adaptateurs, tous tenus à `readModelStoreContract`.
2087
2102
 
2103
+ Sur PostgreSQL, la table d'un tel store ne contient la trace d'une ligne supprimée que si chaque
2104
+ colonne que le store associe comme champ de sa ligne accepte NULL. `PostgresTraceColumns` le lit
2105
+ dans la base, pour ces colonnes et aucune autre : celles qui refusent NULL, celles que rien ne peut
2106
+ ouvrir (une partie de la clé primaire, une colonne d'identité ou générée, une colonne d'un domaine
2107
+ qui refuse NULL, une colonne qui n'existe pas), si la table existe, ce que sa contrainte de
2108
+ vérification `<table>_live_rows_whole` garde entier, quelles autres contraintes de vérification
2109
+ nomment une colonne de la ligne, et si c'est une vue. C'est le refus d'un store avant sa première
2110
+ trace, et `tableState()` d'un `TableReadModelStore` rend la même lecture sous la forme d'un
2111
+ `ReadModelTableState`. `ReadModelTables` le demande aux stores qu'une application liste, chaque
2112
+ entrée un nom et ce qui construit le store comme l'application le lie : `hosting(contexts)` garde
2113
+ les entrées des contextes qu'une application héberge et `contexts` nomme ceux que tient la liste ;
2114
+ `states()` et `notReady()` listent, `require()` refuse avec `ReadModelTablesNotReadyError`, `atStart(logger)` est ce
2115
+ qu'appelle une application à son démarrage (`atStart(logger, 'reported')` journalise les tables en
2116
+ une erreur et laisse le démarrage se poursuivre), et `report()` est ce qu'affiche une commande,
2117
+ avec un `outcome` qui distingue une table qui n'est pas prête d'une base qui n'a pas pu être lue.
2118
+ Chaque entrée est essayée : celle dont le store ne peut pas être construit ou dont la base refuse
2119
+ de lire la table est dite sous son nom, dans `unread` de l'erreur, avec sa cause en quelques mots
2120
+ (le message du pilote, jamais l'instruction), et les autres sont lues quand même. Une base
2121
+ injoignable n'est pas un refus de démarrage : `atStart` journalise un seul avertissement avec la
2122
+ cause courte et poursuit, `report()` rend `unread`. Le verdict est celui du store par construction, une table que personne ne liste n'est jamais
2123
+ lue, et un store sans table (mémoire, OpenSearch) n'a rien à dire. Ce que la vérification ne peut
2124
+ pas trancher est un avertissement : une contrainte non validée, une contrainte de ce nom qui n'est
2125
+ pas écrite comme le kit l'écrit (la forme est lue exactement, telle que PostgreSQL l'affiche), une
2126
+ autre vérification sur une colonne de la ligne sauf si elle est écrite pour les seules lignes
2127
+ vivantes (`deleted or ..`), une vue, un store que l'application nomme comme
2128
+ non listé. Un refus nomme la table, ce qui l'empêche de contenir une trace et la migration à
2129
+ écrire, dans `steps` (`statements` tient les deux dans l'ordre) : le changement de définition, une
2130
+ instruction qui ne touche que les colonnes associées, écrit la contrainte `not valid` et ne
2131
+ supprime jamais une contrainte qui n'est pas celle du kit ; puis la validation et le nettoyage des
2132
+ lignes supprimées auparavant, qu'une grande table exécute hors de la transaction de la première
2133
+ étape. Elle peut être exécutée deux fois. Le tout tenu à `readModelTablesContract` sur les quatre
2134
+ ORM : voir
2135
+ [Chaque table vérifiée au démarrage, et avant un déploiement](../../docs/concepts/cqrs.fr.md#chaque-table-vérifiée-au-démarrage-et-avant-un-déploiement).
2136
+
2088
2137
  ### Reconstruction
2089
2138
 
2090
2139
  Un modèle de lecture alimenté par des événements qu'aucun *event store* ne garde se reconstruit à
package/README.md CHANGED
@@ -1445,7 +1445,9 @@ reporter)` gives the order that keeps both consistent, each step once:
1445
1445
 
1446
1446
  For a large file the client uploads straight to the storage: `uploadUrl(key, { expiresInSeconds,
1447
1447
  mediaType, accepted, maxBytes })` answers the `link` for the client and the `pending` grant
1448
- (`PendingUpload`: key, size cap, media types) that the command records on the aggregate;
1448
+ (`PendingUpload`: key, size cap, media types, and the `text` rule and `contentRules` given with
1449
+ them) that the command records on the aggregate; it throws when an accepted type has no content
1450
+ rule, since no file of it could be confirmed;
1449
1451
  `confirm(pending)`, called by a command that reads the grant back from the aggregate, never
1450
1452
  from the client, checks what arrived against the cap and on its bytes (`contentMatches`), scans
1451
1453
  it, and answers the `StoredFile`. No final key may start with `staging/` (`assertFinalKey`).
@@ -1456,6 +1458,18 @@ with an expectation holds back the first bytes until they prove the declared med
1456
1458
  archives, text is UTF-8 without control characters, SVG and unknown binary formats never
1457
1459
  match).
1458
1460
 
1461
+ Each media type has a content rule (`contentRuleOf`): a signature (`SIGNED_MEDIA_TYPES`), text
1462
+ (`TEXT_MEDIA_TYPES`), or the one the expectation states in `contentRules` for a type the kit
1463
+ has none for, `text` or `unchecked` (the bytes are taken as they are); a rule stated for a
1464
+ family (`image/*`) never reaches a type a browser runs (`NAMED_ONLY_MEDIA_TYPES`: SVG, HTML,
1465
+ JavaScript), which is stated by its name. `contentRuleDefects`
1466
+ lists, before any content arrives, the accepted types no content could prove and the stated
1467
+ rules that do nothing: `@ddd-ts-kit/http` refuses a route on it, `uploadUrl` a link. The
1468
+ expectation also says the text it takes (`TextRule`): `utf-8`, the default, or `single-byte`,
1469
+ which also passes text in Windows-1252 or ISO-8859-1 (no NUL, no control character, no UTF-16
1470
+ or UTF-32 byte order mark) and still refuses a binary file. The bytes are never decoded by the
1471
+ kit: `isUtf8(bytes)` tells a reader which decoder to take.
1472
+
1459
1473
  Connectors: `InMemoryFileStorage` for the tests (`keys()` lists what it holds) and
1460
1474
  `LocalFileStorage({ directory, baseUrl, secret })` for development, both `ServedFileStorage`:
1461
1475
  their signed links point at the application, which answers them with `signedFileRoutes` of
@@ -2001,6 +2015,39 @@ adapter gives (three statements and the query of a plan): `InMemoryReadModelStor
2001
2015
  the fakes, `KnexReadModelStore`, `DrizzleReadModelStore`, `PrismaReadModelStore` and
2002
2016
  `TypeOrmReadModelStore` in the adapter packages, all held to `readModelStoreContract`.
2003
2017
 
2018
+ On PostgreSQL the table of such a store holds the trace of a deleted row only if every column the
2019
+ store maps as a field of its row accepts NULL. `PostgresTraceColumns` reads it from the database,
2020
+ for those columns and no other: which refuse NULL, which nothing can open (a part of the primary
2021
+ key, an identity or a generated column, a column of a domain that refuses NULL, a column that is
2022
+ not there), whether the table exists, what its check constraint `<table>_live_rows_whole` keeps
2023
+ whole, which other check constraints name a column of the row, and whether it is a view. It is
2024
+ the refusal of one store before its first trace, and `tableState()` of a `TableReadModelStore`
2025
+ answers the same reading as a `ReadModelTableState`. `ReadModelTables` asks it of the stores an
2026
+ application lists, each entry a name and what builds the store as the application binds it:
2027
+ `hosting(contexts)` keeps the entries of the contexts an application hosts and `contexts` names
2028
+ those the list holds; `states()` and `notReady()` list, `require()` refuses with
2029
+ `ReadModelTablesNotReadyError`,
2030
+ `atStart(logger)` is what an application calls when it starts (`atStart(logger, 'reported')` logs
2031
+ the tables as one error and lets the start go on), and `report()` is what a command prints, with
2032
+ an `outcome` that tells a table that is not ready from a database that could not be read. Every
2033
+ entry is tried: one whose store cannot be built or whose table the database refuses to read is
2034
+ told under its name, in `unread` of the error, with its cause in a few words (the message of the
2035
+ driver, never the statement), and the others are still read. A database that cannot be reached is
2036
+ no refusal of a start: `atStart` logs one warning with the short cause and goes on, `report()`
2037
+ answers `unread`. The verdict is the
2038
+ store's by construction, a table nobody lists is never read, and a store without a table (memory,
2039
+ OpenSearch) has nothing to say. What the check cannot decide is a warning: a constraint that is
2040
+ not validated, a constraint of that name that is not written as the kit writes it (the form is
2041
+ read exactly, as PostgreSQL prints it), another check on a column of the row unless it is written for the live rows only (`deleted or
2042
+ ..`), a view, a store the
2043
+ application names as not listed. A refusal names the table, what keeps it from holding a trace
2044
+ and the migration to write, in `steps` (`statements` holds both in order): the change of
2045
+ definition, one statement that touches the mapped columns alone, writes the constraint `not valid`
2046
+ and never drops a constraint that is not the kit's; then the validation and the cleaning of the
2047
+ rows deleted before, which a large table runs outside the transaction of the first step. It can
2048
+ be run twice. All held to `readModelTablesContract` on the four ORM: see
2049
+ [Every table checked at the start, and before a deployment](../../docs/concepts/cqrs.md#every-table-checked-at-the-start-and-before-a-deployment).
2050
+
2004
2051
  ### Rebuild
2005
2052
 
2006
2053
  A read model fed by events that no event store keeps is rebuilt from the source of truth, the
@@ -1,6 +1,12 @@
1
+ import type { ContentRules } from './content-rules.js';
2
+ import type { TextRule } from './text-rule.js';
1
3
  /** What an incoming content must be: one of the accepted media types, the one it declares, verified on its bytes. */
2
4
  export interface ContentExpectation {
3
5
  readonly mediaType: string;
4
6
  readonly accepted: readonly string[];
7
+ /** The text a text type may be. Default `utf-8`. */
8
+ readonly text?: TextRule;
9
+ /** The rule of each accepted media type the kit has none for: without one, such a type never passes. */
10
+ readonly contentRules?: ContentRules;
5
11
  }
6
12
  //# sourceMappingURL=content-expectation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"content-expectation.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-expectation.ts"],"names":[],"mappings":"AAAA,qHAAqH;AACrH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC"}
1
+ {"version":3,"file":"content-expectation.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-expectation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,qHAAqH;AACrH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,QAAQ,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,oDAAoD;IACpD,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,CAAC;IACzB,wGAAwG;IACxG,QAAQ,CAAC,YAAY,CAAC,EAAE,YAAY,CAAC;CACtC"}
@@ -1,7 +1,16 @@
1
+ import type { ContentExpectation } from './content-expectation.js';
1
2
  /**
2
3
  * Whether the first bytes of a content are what its declared media type says, read on the bytes
3
- * and never on a file name: a PDF starts with `%PDF-`, a PNG with its eight bytes. A binary
4
- * format the kit does not recognise, SVG included, never matches.
4
+ * and never on a file name, by the content rule of that type (`contentRuleOf`):
5
+ *
6
+ * - a signature: a PDF starts with `%PDF-`, a PNG with its eight bytes, an office document is a zip;
7
+ * - text, for the text types of the kit and those the route states as `text`: no control
8
+ * character, valid UTF-8 unless the route takes `single-byte` text (`TextRule`), and never the
9
+ * signature of a binary format;
10
+ * - `unchecked`, stated by the route: the bytes are taken as they are.
11
+ *
12
+ * A type with no rule never matches, and the rule of a family is not one for SVG, HTML or
13
+ * JavaScript: those pass only when the route states them by name.
5
14
  */
6
- export declare function contentMatches(head: Uint8Array, declared: string): boolean;
15
+ export declare function contentMatches(head: Uint8Array, declared: string, expectation?: Pick<ContentExpectation, 'text' | 'contentRules'>): boolean;
7
16
  //# sourceMappingURL=content-matches.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"content-matches.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-matches.ts"],"names":[],"mappings":"AAoBA;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAS1E"}
1
+ {"version":3,"file":"content-matches.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-matches.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAOhE;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,UAAU,EAChB,QAAQ,EAAE,MAAM,EAChB,WAAW,GAAE,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,cAAc,CAAM,GAClE,OAAO,CAeT"}
@@ -1,39 +1,34 @@
1
- import { signatureOf } from './signature-of.js';
1
+ import { contentRuleOf } from './content-rule-of.js';
2
+ import { looksLikeText } from './looks-like-text.js';
2
3
  import { mediaTypeOf } from './media-type-of.js';
3
- /** Office and e-book documents are zip archives: their signature is the one of a zip. */
4
- const ZIP_CONTAINERS = new Set([
5
- 'application/zip',
6
- 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
7
- 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
8
- 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
9
- 'application/vnd.oasis.opendocument.text',
10
- 'application/vnd.oasis.opendocument.spreadsheet',
11
- 'application/epub+zip',
12
- ]);
13
- /** Text has no signature: it is accepted when its first bytes are UTF-8 without control characters. */
14
- const TEXT_TYPES = new Set(['text/plain', 'text/csv', 'text/markdown', 'application/json']);
15
- /** Tab, line feed, form feed and carriage return are text; every other C0 control character is not. */
16
- const TEXT_CONTROLS = new Set([9, 10, 12, 13]);
4
+ import { signatureOf } from './signature-of.js';
5
+ import { ZIP_CONTAINERS } from './zip-containers.js';
17
6
  /**
18
7
  * Whether the first bytes of a content are what its declared media type says, read on the bytes
19
- * and never on a file name: a PDF starts with `%PDF-`, a PNG with its eight bytes. A binary
20
- * format the kit does not recognise, SVG included, never matches.
8
+ * and never on a file name, by the content rule of that type (`contentRuleOf`):
9
+ *
10
+ * - a signature: a PDF starts with `%PDF-`, a PNG with its eight bytes, an office document is a zip;
11
+ * - text, for the text types of the kit and those the route states as `text`: no control
12
+ * character, valid UTF-8 unless the route takes `single-byte` text (`TextRule`), and never the
13
+ * signature of a binary format;
14
+ * - `unchecked`, stated by the route: the bytes are taken as they are.
15
+ *
16
+ * A type with no rule never matches, and the rule of a family is not one for SVG, HTML or
17
+ * JavaScript: those pass only when the route states them by name.
21
18
  */
22
- export function contentMatches(head, declared) {
19
+ export function contentMatches(head, declared, expectation = {}) {
23
20
  const essence = mediaTypeOf(declared);
21
+ const rule = contentRuleOf(essence, expectation.contentRules);
22
+ if (rule === 'unchecked') {
23
+ return true;
24
+ }
24
25
  const signature = signatureOf(head);
25
26
  if (signature !== undefined) {
26
- return signature === essence || (signature === 'application/zip' && ZIP_CONTAINERS.has(essence));
27
+ return rule === 'signature' && proves(signature, essence);
27
28
  }
28
- return TEXT_TYPES.has(essence) && looksLikeText(head);
29
+ return rule === 'text' && looksLikeText(head, expectation.text ?? 'utf-8');
29
30
  }
30
- function looksLikeText(head) {
31
- try {
32
- new TextDecoder('utf-8', { fatal: true }).decode(head, { stream: true });
33
- }
34
- catch {
35
- return false;
36
- }
37
- return head.every((byte) => byte >= 32 || TEXT_CONTROLS.has(byte));
31
+ function proves(signature, essence) {
32
+ return signature === essence || (signature === 'application/zip' && ZIP_CONTAINERS.includes(essence));
38
33
  }
39
34
  //# sourceMappingURL=content-matches.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"content-matches.js","sourceRoot":"","sources":["../../../src/application/files/content-matches.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,yFAAyF;AACzF,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC;IAC7B,iBAAiB;IACjB,yEAAyE;IACzE,mEAAmE;IACnE,2EAA2E;IAC3E,yCAAyC;IACzC,gDAAgD;IAChD,sBAAsB;CACvB,CAAC,CAAC;AAEH,u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EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,cAAc,CAC5B,IAAgB,EAChB,QAAgB,EAChB,WAAW,GAAsD,EAAE;IAEnE,MAAM,OAAO,GAAG,WAAW,CAAC,QAAQ,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,EAAE,WAAW,CAAC,YAAY,CAAC,CAAC;IAE9D,IAAI,IAAI,KAAK,WAAW,EAAE,CAAC;QACzB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAEpC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,OAAO,IAAI,KAAK,WAAW,IAAI,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC5D,CAAC;IAED,OAAO,IAAI,KAAK,MAAM,IAAI,aAAa,CAAC,IAAI,EAAE,WAAW,CAAC,IAAI,IAAI,OAAO,CAAC,CAAC;AAC7E,CAAC;AAED,SAAS,MAAM,CAAC,SAAiB,EAAE,OAAe;IAChD,OAAO,SAAS,KAAK,OAAO,IAAI,CAAC,SAAS,KAAK,iBAAiB,IAAI,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC;AACxG,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { ContentExpectation } from './content-expectation.js';
2
+ /**
3
+ * What is wrong with the media types an upload accepts, one sentence each, before any request: an
4
+ * accepted type no content could ever prove (no signature, no text rule, none stated), and a stated
5
+ * rule that does nothing (for a type the kit already verifies, or one not accepted). Empty when
6
+ * every accepted type has a content rule: whoever declares the upload refuses to start otherwise.
7
+ */
8
+ export declare function contentRuleDefects(expectation: Pick<ContentExpectation, 'accepted' | 'contentRules'>): string[];
9
+ //# sourceMappingURL=content-rule-defects.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule-defects.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-rule-defects.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAahE;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,IAAI,CAAC,kBAAkB,EAAE,UAAU,GAAG,cAAc,CAAC,GAAG,MAAM,EAAE,CAO/G"}
@@ -0,0 +1,57 @@
1
+ import { acceptsMediaType } from './accepts-media-type.js';
2
+ import { contentRuleOf } from './content-rule-of.js';
3
+ import { mediaTypeOf } from './media-type-of.js';
4
+ import { SIGNED_MEDIA_TYPES } from './signed-media-types.js';
5
+ import { TEXT_MEDIA_TYPES } from './text-media-types.js';
6
+ /** `type/subtype`, or a family `type/*`. */
7
+ const MEDIA_TYPE = /^[^/*\s]+\/(?:[^/*\s]+|\*)$/;
8
+ const STATE_ONE = "State one in `contentRules`: 'text' (held to the text rule) or 'unchecked' (the bytes are taken as they are).";
9
+ /**
10
+ * What is wrong with the media types an upload accepts, one sentence each, before any request: an
11
+ * accepted type no content could ever prove (no signature, no text rule, none stated), and a stated
12
+ * rule that does nothing (for a type the kit already verifies, or one not accepted). Empty when
13
+ * every accepted type has a content rule: whoever declares the upload refuses to start otherwise.
14
+ */
15
+ export function contentRuleDefects(expectation) {
16
+ const { accepted, contentRules = {} } = expectation;
17
+ return [
18
+ ...accepted.flatMap((entry) => unprovable(entry, contentRules)),
19
+ ...Object.keys(contentRules).flatMap((written) => misstated(written, accepted)),
20
+ ];
21
+ }
22
+ function unprovable(entry, stated) {
23
+ const essence = mediaTypeOf(entry);
24
+ if (!MEDIA_TYPE.test(essence)) {
25
+ return [`"${entry}" is neither a media type nor a family of them (\`image/*\`): no file is ever of it.`];
26
+ }
27
+ const proven = essence.endsWith('/*') ? familyHasRule(essence, stated) : contentRuleOf(essence, stated) !== undefined;
28
+ return proven
29
+ ? []
30
+ : [
31
+ `"${entry}" has no content rule: the kit knows neither a signature nor a text rule for it, so no file of that type ever passes. ${STATE_ONE}`,
32
+ ];
33
+ }
34
+ /** A family is provable when one type of it is: one the kit knows, or one a stated rule names, the family itself included. */
35
+ function familyHasRule(family, stated) {
36
+ const prefix = family.slice(0, -1);
37
+ const known = [...SIGNED_MEDIA_TYPES, ...TEXT_MEDIA_TYPES, ...Object.keys(stated).map(mediaTypeOf)];
38
+ return known.some((type) => type.startsWith(prefix));
39
+ }
40
+ function misstated(written, accepted) {
41
+ const essence = mediaTypeOf(written);
42
+ const ofTheKit = contentRuleOf(essence);
43
+ if (ofTheKit !== undefined) {
44
+ return [
45
+ `\`contentRules\` states "${written}", which the kit already verifies (${ofTheKit}): a stated rule does not replace it. Remove it.`,
46
+ ];
47
+ }
48
+ const taken = essence.endsWith('/*')
49
+ ? accepted.map(mediaTypeOf).includes(essence)
50
+ : acceptsMediaType(accepted, essence);
51
+ return taken
52
+ ? []
53
+ : [
54
+ `\`contentRules\` states "${written}", which is not one of the accepted media types: add it there, or remove it.`,
55
+ ];
56
+ }
57
+ //# sourceMappingURL=content-rule-defects.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule-defects.js","sourceRoot":"","sources":["../../../src/application/files/content-rule-defects.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAExD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAEtD,4CAA4C;AAC5C,MAAM,UAAU,GAAG,6BAA6B,CAAC;AAEjD,MAAM,SAAS,GACb,+G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AAC,WAAW,CAAC,CAAC,CAAC;IAEpG,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,SAAS,CAAC,OAAe,EAAE,QAA2B;IAC7D,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IACrC,MAAM,QAAQ,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IAExC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;QAC3B,OAAO;YACL,4BAA4B,OAAO,sCAAsC,QAAQ,kDAAkD;SACpI,CAAC;IACJ,CAAC;IAED,MAAM,KAAK,GAAG,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;QAClC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;QAC7C,CAAC,CAAC,gBAAgB,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAExC,OAAO,KAAK;QACV,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC;YACE,4BAA4B,OAAO,8EAA8E;SAClH,CAAC;AACR,CAAC"}
@@ -0,0 +1,11 @@
1
+ import type { ContentRule } from './content-rule.js';
2
+ import type { ContentRules } from './content-rules.js';
3
+ /**
4
+ * The content rule of a media type: the one of the kit when it knows the type (a signature, or
5
+ * text), else the one the route states for it, else for its family. `undefined` when neither says:
6
+ * a content of that type never passes, which `contentRuleDefects` reports before any request.
7
+ * The rule of a family never reaches a type a browser runs (`NAMED_ONLY_MEDIA_TYPES`: SVG, HTML,
8
+ * JavaScript): the route states that one by its name, or does not take it.
9
+ */
10
+ export declare function contentRuleOf(mediaType: string, stated?: ContentRules): ContentRule | undefined;
11
+ //# sourceMappingURL=content-rule-of.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule-of.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-rule-of.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAOpD;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,GAAE,YAAiB,GAAG,WAAW,GAAG,SAAS,CAInG"}
@@ -0,0 +1,28 @@
1
+ import { mediaTypeOf } from './media-type-of.js';
2
+ import { NAMED_ONLY_MEDIA_TYPES } from './named-only-media-types.js';
3
+ import { SIGNED_MEDIA_TYPES } from './signed-media-types.js';
4
+ import { TEXT_MEDIA_TYPES } from './text-media-types.js';
5
+ /**
6
+ * The content rule of a media type: the one of the kit when it knows the type (a signature, or
7
+ * text), else the one the route states for it, else for its family. `undefined` when neither says:
8
+ * a content of that type never passes, which `contentRuleDefects` reports before any request.
9
+ * The rule of a family never reaches a type a browser runs (`NAMED_ONLY_MEDIA_TYPES`: SVG, HTML,
10
+ * JavaScript): the route states that one by its name, or does not take it.
11
+ */
12
+ export function contentRuleOf(mediaType, stated = {}) {
13
+ const essence = mediaTypeOf(mediaType);
14
+ return kitRuleOf(essence) ?? statedFor(essence, stated) ?? statedForFamilyOf(essence, stated);
15
+ }
16
+ function kitRuleOf(essence) {
17
+ if (SIGNED_MEDIA_TYPES.includes(essence)) {
18
+ return 'signature';
19
+ }
20
+ return TEXT_MEDIA_TYPES.includes(essence) ? 'text' : undefined;
21
+ }
22
+ function statedForFamilyOf(essence, stated) {
23
+ return NAMED_ONLY_MEDIA_TYPES.includes(essence) ? undefined : statedFor(`${essence.split('/')[0] ?? ''}/*`, stated);
24
+ }
25
+ function statedFor(essence, stated) {
26
+ return Object.entries(stated).find(([written]) => mediaTypeOf(written) === essence)?.[1];
27
+ }
28
+ //# sourceMappingURL=content-rule-of.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule-of.js","sourceRoot":"","sources":["../../../src/application/files/content-rule-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AAC,CAAC;AAC3F,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { StatedContentRule } from './stated-content-rule.js';
2
+ /** How the content of a media type is verified: by the signature of its format, or as its route states. */
3
+ export type ContentRule = 'signature' | StatedContentRule;
4
+ //# sourceMappingURL=content-rule.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-rule.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE/D,2GAA2G;AAC3G,MAAM,MAAM,WAAW,GAAG,WAAW,GAAG,iBAAiB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=content-rule.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rule.js","sourceRoot":"","sources":["../../../src/application/files/content-rule.ts"],"names":[],"mappings":""}
@@ -0,0 +1,4 @@
1
+ import type { StatedContentRule } from './stated-content-rule.js';
2
+ /** The content rules a route states, by exact media type or by family (`video/*`): the exact one wins. */
3
+ export type ContentRules = Readonly<Record<string, StatedContentRule>>;
4
+ //# sourceMappingURL=content-rules.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rules.d.ts","sourceRoot":"","sources":["../../../src/application/files/content-rules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAE/D,0GAA0G;AAC1G,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=content-rules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-rules.js","sourceRoot":"","sources":["../../../src/application/files/content-rules.ts"],"names":[],"mappings":""}
@@ -3,6 +3,10 @@ export * from './assert-expiry.js';
3
3
  export * from './content-disposition.js';
4
4
  export * from './content-expectation.js';
5
5
  export * from './content-matches.js';
6
+ export * from './content-rule.js';
7
+ export * from './content-rule-defects.js';
8
+ export * from './content-rule-of.js';
9
+ export * from './content-rules.js';
6
10
  export * from './download-url-options.js';
7
11
  export * from './file-content.js';
8
12
  export * from './file-error.js';
@@ -13,19 +17,25 @@ export * from './file-metadata.js';
13
17
  export * from './file-source.js';
14
18
  export * from './file-upload.js';
15
19
  export * from './inspection-limits.js';
20
+ export * from './is-utf8.js';
16
21
  export * from './listed-file.js';
17
22
  export * from './max-signed-url-seconds.js';
18
23
  export * from './media-type-of.js';
24
+ export * from './named-only-media-types.js';
19
25
  export * from './pending-upload.js';
20
26
  export * from './pending-upload-link.js';
21
27
  export * from './put-options.js';
22
28
  export * from './received-file.js';
23
29
  export * from './scan-verdict.js';
24
30
  export * from './signature-of.js';
31
+ export * from './signed-media-types.js';
25
32
  export * from './signed-download.js';
26
33
  export * from './signed-upload.js';
27
34
  export * from './signed-url.js';
28
35
  export * from './staged-key-of.js';
36
+ export * from './stated-content-rule.js';
29
37
  export * from './stored-file.js';
38
+ export * from './text-media-types.js';
39
+ export * from './text-rule.js';
30
40
  export * from './upload-url-options.js';
31
41
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/application/files/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;A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C/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC"}
@@ -3,6 +3,10 @@ export * from './assert-expiry.js';
3
3
  export * from './content-disposition.js';
4
4
  export * from './content-expectation.js';
5
5
  export * from './content-matches.js';
6
+ export * from './content-rule.js';
7
+ export * from './content-rule-defects.js';
8
+ export * from './content-rule-of.js';
9
+ export * from './content-rules.js';
6
10
  export * from './download-url-options.js';
7
11
  export * from './file-content.js';
8
12
  export * from './file-error.js';
@@ -13,19 +17,25 @@ export * from './file-metadata.js';
13
17
  export * from './file-source.js';
14
18
  export * from './file-upload.js';
15
19
  export * from './inspection-limits.js';
20
+ export * from './is-utf8.js';
16
21
  export * from './listed-file.js';
17
22
  export * from './max-signed-url-seconds.js';
18
23
  export * from './media-type-of.js';
24
+ export * from './named-only-media-types.js';
19
25
  export * from './pending-upload.js';
20
26
  export * from './pending-upload-link.js';
21
27
  export * from './put-options.js';
22
28
  export * from './received-file.js';
23
29
  export * from './scan-verdict.js';
24
30
  export * from './signature-of.js';
31
+ export * from './signed-media-types.js';
25
32
  export * from './signed-download.js';
26
33
  export * from './signed-upload.js';
27
34
  export * from './signed-url.js';
28
35
  export * from './staged-key-of.js';
36
+ export * from './stated-content-rule.js';
29
37
  export * from './stored-file.js';
38
+ export * from './text-media-types.js';
39
+ export * from './text-rule.js';
30
40
  export * from './upload-url-options.js';
31
41
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/application/files/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC;AACrC,cAAc,i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AiB,CAAC;AAChC,cAAc,eAAe,CAAC;AAC9B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,0BAA0B,CAAC;AACzC,cAAc,iBAAiB,CAAC;AAChC,cAAc,0BAA0B,CAAC;AACzC,cAAc,kBAAkB,CAAC;AACjC,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,sBAAsB,CAAC;AACrC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,uBAAuB,CAAC;AACtC,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Whether bytes are valid UTF-8 as a whole: what a reader of a file received under the text rule
3
+ * `single-byte` asks before it decodes, `new TextDecoder(isUtf8(bytes) ? 'utf-8' : 'windows-1252')`.
4
+ * Text in a single-byte encoding with one accented letter is almost never valid UTF-8, and pure
5
+ * ASCII reads the same in both.
6
+ */
7
+ export declare function isUtf8(bytes: Uint8Array): boolean;
8
+ //# sourceMappingURL=is-utf8.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-utf8.d.ts","sourceRoot":"","sources":["../../../src/application/files/is-utf8.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAQjD"}
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Whether bytes are valid UTF-8 as a whole: what a reader of a file received under the text rule
3
+ * `single-byte` asks before it decodes, `new TextDecoder(isUtf8(bytes) ? 'utf-8' : 'windows-1252')`.
4
+ * Text in a single-byte encoding with one accented letter is almost never valid UTF-8, and pure
5
+ * ASCII reads the same in both.
6
+ */
7
+ export function isUtf8(bytes) {
8
+ try {
9
+ new TextDecoder('utf-8', { fatal: true }).decode(bytes);
10
+ }
11
+ catch {
12
+ return false;
13
+ }
14
+ return true;
15
+ }
16
+ //# sourceMappingURL=is-utf8.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"is-utf8.js","sourceRoot":"","sources":["../../../src/application/files/is-utf8.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,KAAiB;IACtC,IAAI,CAAC;QACH,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC1D,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { TextRule } from './text-rule.js';
2
+ /**
3
+ * Whether the first bytes of a content are text under a rule. Under both rules a NUL or another
4
+ * control character refuses, which is what the head of a binary file holds; `utf-8` also demands
5
+ * valid UTF-8, cut anywhere, and `single-byte` refuses a UTF-16 or UTF-32 byte order mark.
6
+ */
7
+ export declare function looksLikeText(head: Uint8Array, rule: TextRule): boolean;
8
+ //# sourceMappingURL=looks-like-text.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"looks-like-text.d.ts","sourceRoot":"","sources":["../../../src/application/files/looks-like-text.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAK5C;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAMvE"}
@@ -0,0 +1,28 @@
1
+ /** Tab, line feed, form feed and carriage return are text; every other C0 control character is not. */
2
+ const TEXT_CONTROLS = new Set([9, 10, 12, 13]);
3
+ /**
4
+ * Whether the first bytes of a content are text under a rule. Under both rules a NUL or another
5
+ * control character refuses, which is what the head of a binary file holds; `utf-8` also demands
6
+ * valid UTF-8, cut anywhere, and `single-byte` refuses a UTF-16 or UTF-32 byte order mark.
7
+ */
8
+ export function looksLikeText(head, rule) {
9
+ if (!head.every((byte) => byte >= 32 || TEXT_CONTROLS.has(byte))) {
10
+ return false;
11
+ }
12
+ return rule === 'utf-8' ? startsValidUtf8(head) : !startsWithWideMark(head);
13
+ }
14
+ function startsValidUtf8(head) {
15
+ try {
16
+ new TextDecoder('utf-8', { fatal: true }).decode(head, { stream: true });
17
+ }
18
+ catch {
19
+ return false;
20
+ }
21
+ return true;
22
+ }
23
+ /** `FF FE` and `FE FF` open UTF-16, and UTF-32 in little endian: "ÿþ" opens no file of single-byte text. */
24
+ function startsWithWideMark(head) {
25
+ const [first, second] = head;
26
+ return (first === 0xff && second === 0xfe) || (first === 0xfe && second === 0xff);
27
+ }
28
+ //# sourceMappingURL=looks-like-text.js.map