@graphty/graph-io 0.3.19 → 0.3.21

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 (201) hide show
  1. package/README.md +49 -4
  2. package/dist/chunks/{escape-CWExcecC.js → escape-B-tkk_Cl.js} +74 -16
  3. package/dist/chunks/escape-B-tkk_Cl.js.map +1 -0
  4. package/dist/chunks/{writer-DQiKgQJc.js → export-Bh60Dv-n.js} +5 -79
  5. package/dist/chunks/export-Bh60Dv-n.js.map +1 -0
  6. package/dist/chunks/{exporter-CbMZyVt-.js → exporter-BGrsWamJ.js} +7 -6
  7. package/dist/chunks/{exporter-CbMZyVt-.js.map → exporter-BGrsWamJ.js.map} +1 -1
  8. package/dist/chunks/exporter-DIeGJXAZ.js +834 -0
  9. package/dist/chunks/exporter-DIeGJXAZ.js.map +1 -0
  10. package/dist/chunks/{importer-BNFuV1-K.js → importer-BWY2FFc5.js} +28 -7
  11. package/dist/chunks/importer-BWY2FFc5.js.map +1 -0
  12. package/dist/chunks/importer-DD-xv9_Y.js +1471 -0
  13. package/dist/chunks/importer-DD-xv9_Y.js.map +1 -0
  14. package/dist/chunks/{importer-BAP4PrxR.js → importer-DET1tg6s.js} +30 -20
  15. package/dist/chunks/importer-DET1tg6s.js.map +1 -0
  16. package/dist/chunks/{importer-ByPGO-09.js → importer-DEcKhsmQ.js} +2 -2
  17. package/dist/chunks/{importer-ByPGO-09.js.map → importer-DEcKhsmQ.js.map} +1 -1
  18. package/dist/chunks/{importer-BW-Ft2ps.js → importer-DIrbbnAf.js} +6 -5
  19. package/dist/chunks/{importer-BW-Ft2ps.js.map → importer-DIrbbnAf.js.map} +1 -1
  20. package/dist/chunks/importer-D_e7e7LX.js +3055 -0
  21. package/dist/chunks/importer-D_e7e7LX.js.map +1 -0
  22. package/dist/chunks/{importer-Bm1_5vPS.js → importer-eUREhBGj.js} +11 -8
  23. package/dist/chunks/importer-eUREhBGj.js.map +1 -0
  24. package/dist/chunks/{importer-DOepkgnG.js → importer-vi3bSdtw.js} +4 -4
  25. package/dist/chunks/{importer-DOepkgnG.js.map → importer-vi3bSdtw.js.map} +1 -1
  26. package/dist/chunks/{importer-BVGtU1NA.js → importer-zGLo8Dg_.js} +6 -5
  27. package/dist/chunks/{importer-BVGtU1NA.js.map → importer-zGLo8Dg_.js.map} +1 -1
  28. package/dist/chunks/{json-elements-CZY1wiZh.js → json-elements-DDS8N2Dd.js} +2 -2
  29. package/dist/chunks/{json-elements-CZY1wiZh.js.map → json-elements-DDS8N2Dd.js.map} +1 -1
  30. package/dist/chunks/{records-BzNicMsf.js → records-dbRkxwaq.js} +2 -2
  31. package/dist/chunks/{records-BzNicMsf.js.map → records-dbRkxwaq.js.map} +1 -1
  32. package/dist/chunks/{report-BcWboivV.js → report-B1z4WT9e.js} +143 -111
  33. package/dist/chunks/{report-BcWboivV.js.map → report-B1z4WT9e.js.map} +1 -1
  34. package/dist/chunks/{weights-CwISIpCP.js → weights-Dzba96G3.js} +5 -5
  35. package/dist/chunks/{weights-CwISIpCP.js.map → weights-Dzba96G3.js.map} +1 -1
  36. package/dist/chunks/writer-C7flM-Ih.js +77 -0
  37. package/dist/chunks/writer-C7flM-Ih.js.map +1 -0
  38. package/dist/csv.js +6 -5
  39. package/dist/csv.js.map +1 -1
  40. package/dist/cx.js +1 -1
  41. package/dist/cx2.js +2 -2
  42. package/dist/cys.d.ts +1 -0
  43. package/dist/cys.js +6 -0
  44. package/dist/cys.js.map +1 -0
  45. package/dist/dot.js +1 -1
  46. package/dist/gexf.js +5 -4
  47. package/dist/gexf.js.map +1 -1
  48. package/dist/gml.js +12 -14
  49. package/dist/gml.js.map +1 -1
  50. package/dist/graph-io.js +288 -141
  51. package/dist/graph-io.js.map +1 -1
  52. package/dist/graphml.js +1 -1
  53. package/dist/json.js +1 -1
  54. package/dist/neo4j.js +6 -5
  55. package/dist/neo4j.js.map +1 -1
  56. package/dist/obo.js +1 -1
  57. package/dist/pajek.js +1 -1
  58. package/dist/src/common/cell-budget.d.ts +84 -0
  59. package/dist/src/common/cell-budget.d.ts.map +1 -0
  60. package/dist/src/common/cell-budget.js +138 -0
  61. package/dist/src/common/cell-budget.js.map +1 -0
  62. package/dist/src/common/input.d.ts +10 -0
  63. package/dist/src/common/input.d.ts.map +1 -1
  64. package/dist/src/common/input.js +39 -0
  65. package/dist/src/common/input.js.map +1 -1
  66. package/dist/src/common/options.d.ts +6 -0
  67. package/dist/src/common/options.d.ts.map +1 -1
  68. package/dist/src/common/options.js +1 -1
  69. package/dist/src/common/options.js.map +1 -1
  70. package/dist/src/common/xml.d.ts +37 -3
  71. package/dist/src/common/xml.d.ts.map +1 -1
  72. package/dist/src/common/xml.js +86 -6
  73. package/dist/src/common/xml.js.map +1 -1
  74. package/dist/src/common/zip.d.ts +92 -0
  75. package/dist/src/common/zip.d.ts.map +1 -0
  76. package/dist/src/common/zip.js +399 -0
  77. package/dist/src/common/zip.js.map +1 -0
  78. package/dist/src/formats/cx/importer.d.ts.map +1 -1
  79. package/dist/src/formats/cx/importer.js +26 -17
  80. package/dist/src/formats/cx/importer.js.map +1 -1
  81. package/dist/src/formats/cys/constants.d.ts +68 -0
  82. package/dist/src/formats/cys/constants.d.ts.map +1 -0
  83. package/dist/src/formats/cys/constants.js +76 -0
  84. package/dist/src/formats/cys/constants.js.map +1 -0
  85. package/dist/src/formats/cys/importer.d.ts +38 -0
  86. package/dist/src/formats/cys/importer.d.ts.map +1 -0
  87. package/dist/src/formats/cys/importer.js +830 -0
  88. package/dist/src/formats/cys/importer.js.map +1 -0
  89. package/dist/src/formats/cys/index.d.ts +7 -0
  90. package/dist/src/formats/cys/index.d.ts.map +1 -0
  91. package/dist/src/formats/cys/index.js +7 -0
  92. package/dist/src/formats/cys/index.js.map +1 -0
  93. package/dist/src/formats/cys/session.d.ts +132 -0
  94. package/dist/src/formats/cys/session.d.ts.map +1 -0
  95. package/dist/src/formats/cys/session.js +315 -0
  96. package/dist/src/formats/cys/session.js.map +1 -0
  97. package/dist/src/formats/cys/tables.d.ts +90 -0
  98. package/dist/src/formats/cys/tables.d.ts.map +1 -0
  99. package/dist/src/formats/cys/tables.js +293 -0
  100. package/dist/src/formats/cys/tables.js.map +1 -0
  101. package/dist/src/formats/dot/exporter.d.ts +2 -0
  102. package/dist/src/formats/dot/exporter.d.ts.map +1 -1
  103. package/dist/src/formats/dot/exporter.js +12 -0
  104. package/dist/src/formats/dot/exporter.js.map +1 -1
  105. package/dist/src/formats/dot/importer.js +6 -2
  106. package/dist/src/formats/dot/importer.js.map +1 -1
  107. package/dist/src/formats/gexf/importer.d.ts +2 -2
  108. package/dist/src/formats/gexf/importer.d.ts.map +1 -1
  109. package/dist/src/formats/gexf/importer.js +1 -1
  110. package/dist/src/formats/gexf/importer.js.map +1 -1
  111. package/dist/src/formats/gml/exporter.d.ts +0 -8
  112. package/dist/src/formats/gml/exporter.d.ts.map +1 -1
  113. package/dist/src/formats/gml/exporter.js +7 -17
  114. package/dist/src/formats/gml/exporter.js.map +1 -1
  115. package/dist/src/formats/json/exporter.d.ts.map +1 -1
  116. package/dist/src/formats/json/exporter.js +3 -1
  117. package/dist/src/formats/json/exporter.js.map +1 -1
  118. package/dist/src/formats/json/importer.d.ts.map +1 -1
  119. package/dist/src/formats/json/importer.js +3 -2
  120. package/dist/src/formats/json/importer.js.map +1 -1
  121. package/dist/src/formats/xgmml/columns.d.ts +152 -0
  122. package/dist/src/formats/xgmml/columns.d.ts.map +1 -0
  123. package/dist/src/formats/xgmml/columns.js +593 -0
  124. package/dist/src/formats/xgmml/columns.js.map +1 -0
  125. package/dist/src/formats/xgmml/constants.d.ts +196 -0
  126. package/dist/src/formats/xgmml/constants.d.ts.map +1 -0
  127. package/dist/src/formats/xgmml/constants.js +197 -0
  128. package/dist/src/formats/xgmml/constants.js.map +1 -0
  129. package/dist/src/formats/xgmml/document.d.ts +361 -0
  130. package/dist/src/formats/xgmml/document.d.ts.map +1 -0
  131. package/dist/src/formats/xgmml/document.js +695 -0
  132. package/dist/src/formats/xgmml/document.js.map +1 -0
  133. package/dist/src/formats/xgmml/emit.d.ts +341 -0
  134. package/dist/src/formats/xgmml/emit.d.ts.map +1 -0
  135. package/dist/src/formats/xgmml/emit.js +1275 -0
  136. package/dist/src/formats/xgmml/emit.js.map +1 -0
  137. package/dist/src/formats/xgmml/exporter.d.ts +24 -0
  138. package/dist/src/formats/xgmml/exporter.d.ts.map +1 -0
  139. package/dist/src/formats/xgmml/exporter.js +999 -0
  140. package/dist/src/formats/xgmml/exporter.js.map +1 -0
  141. package/dist/src/formats/xgmml/importer.d.ts +58 -0
  142. package/dist/src/formats/xgmml/importer.d.ts.map +1 -0
  143. package/dist/src/formats/xgmml/importer.js +360 -0
  144. package/dist/src/formats/xgmml/importer.js.map +1 -0
  145. package/dist/src/formats/xgmml/index.d.ts +8 -0
  146. package/dist/src/formats/xgmml/index.d.ts.map +1 -0
  147. package/dist/src/formats/xgmml/index.js +8 -0
  148. package/dist/src/formats/xgmml/index.js.map +1 -0
  149. package/dist/src/formats/xgmml/values.d.ts +60 -0
  150. package/dist/src/formats/xgmml/values.d.ts.map +1 -0
  151. package/dist/src/formats/xgmml/values.js +148 -0
  152. package/dist/src/formats/xgmml/values.js.map +1 -0
  153. package/dist/src/index.d.ts +2 -0
  154. package/dist/src/index.d.ts.map +1 -1
  155. package/dist/src/index.js +2 -0
  156. package/dist/src/index.js.map +1 -1
  157. package/dist/src/registry.d.ts +7 -0
  158. package/dist/src/registry.d.ts.map +1 -1
  159. package/dist/src/registry.js +25 -10
  160. package/dist/src/registry.js.map +1 -1
  161. package/dist/src/sniff.d.ts +1 -1
  162. package/dist/src/sniff.d.ts.map +1 -1
  163. package/dist/src/sniff.js +2 -0
  164. package/dist/src/sniff.js.map +1 -1
  165. package/dist/xgmml.d.ts +1 -0
  166. package/dist/xgmml.js +9 -0
  167. package/dist/xgmml.js.map +1 -0
  168. package/package.json +15 -2
  169. package/src/common/cell-budget.ts +170 -0
  170. package/src/common/input.ts +40 -0
  171. package/src/common/options.ts +1 -1
  172. package/src/common/xml.ts +127 -6
  173. package/src/common/zip.ts +472 -0
  174. package/src/formats/cx/importer.ts +27 -19
  175. package/src/formats/cys/constants.ts +99 -0
  176. package/src/formats/cys/importer.ts +1100 -0
  177. package/src/formats/cys/index.ts +7 -0
  178. package/src/formats/cys/session.ts +428 -0
  179. package/src/formats/cys/tables.ts +400 -0
  180. package/src/formats/dot/exporter.ts +17 -0
  181. package/src/formats/dot/importer.ts +6 -2
  182. package/src/formats/gexf/importer.ts +0 -1
  183. package/src/formats/gml/exporter.ts +7 -18
  184. package/src/formats/json/exporter.ts +3 -1
  185. package/src/formats/json/importer.ts +3 -2
  186. package/src/formats/xgmml/columns.ts +747 -0
  187. package/src/formats/xgmml/constants.ts +265 -0
  188. package/src/formats/xgmml/document.ts +975 -0
  189. package/src/formats/xgmml/emit.ts +1563 -0
  190. package/src/formats/xgmml/exporter.ts +1215 -0
  191. package/src/formats/xgmml/importer.ts +491 -0
  192. package/src/formats/xgmml/index.ts +8 -0
  193. package/src/formats/xgmml/values.ts +183 -0
  194. package/src/index.ts +9 -0
  195. package/src/registry.ts +41 -15
  196. package/src/sniff.ts +5 -1
  197. package/dist/chunks/escape-CWExcecC.js.map +0 -1
  198. package/dist/chunks/importer-BAP4PrxR.js.map +0 -1
  199. package/dist/chunks/importer-BNFuV1-K.js.map +0 -1
  200. package/dist/chunks/importer-Bm1_5vPS.js.map +0 -1
  201. package/dist/chunks/writer-DQiKgQJc.js.map +0 -1
@@ -0,0 +1,472 @@
1
+ /**
2
+ * A zip reader with no dependency (design `design/graph-io/cytoscape-and-obo/design.md` section
3
+ * 3.3; PKWARE APPNOTE 6.3.10, https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT), for
4
+ * the Cytoscape session importer.
5
+ *
6
+ * The whole archive is in memory: every Cytoscape-written entry has a data descriptor and zero
7
+ * sizes in its local header, so the sizes come from the central directory at the end. The end
8
+ * record is searched in the last 65,557 bytes (a comment can be 65,535 bytes long); the zip64
9
+ * locator and the `0x0001` extra field are followed; bytes prepended to the archive (a
10
+ * self-extracting stub) are tolerated by the shift between the directory's stated and actual
11
+ * offsets. Deflate (method 8) is inflated by wrapping the raw deflate bytes in a gzip member
12
+ * (header, data, the directory's CRC-32 and size) and passing it to `DecompressionStream("gzip")`,
13
+ * which every supported runtime has (Node 18, unlike `"deflate-raw"`) and which checks the CRC and
14
+ * the length itself; stored entries (method 0) are checked against a CRC-32 table here. Every
15
+ * other method, and encryption, is refused by name. Entry names are decoded by `decodeEntryName()`
16
+ * (the one place bytes become text) and are matching keys only, never paths.
17
+ */
18
+
19
+ import { decodeEntryName, throwIfAborted } from "./input.js";
20
+
21
+ /** Why a zip could not be read; the importer maps each kind to its own issue code. */
22
+ type ZipErrorKind = "not-zip" | "corrupt" | "unsupported" | "too-large";
23
+
24
+ /** A zip that cannot be read, or an entry that cannot be inflated. */
25
+ export class ZipError extends Error {
26
+ /** What went wrong. */
27
+ readonly kind: ZipErrorKind;
28
+
29
+ /**
30
+ * Create the error.
31
+ * @param kind - what went wrong
32
+ * @param message - a plain-ASCII message
33
+ */
34
+ constructor(kind: ZipErrorKind, message: string) {
35
+ super(message);
36
+ this.name = "ZipError";
37
+ this.kind = kind;
38
+ }
39
+ }
40
+
41
+ /** One entry of the central directory. */
42
+ export interface ZipEntry {
43
+ /** The name, decoded, as the central directory spells it. */
44
+ readonly name: string;
45
+ /** The compression method (0 stored, 8 deflate, ...). */
46
+ readonly method: number;
47
+ /** The general purpose bit flags. */
48
+ readonly flags: number;
49
+ /** The CRC-32 of the uncompressed data. */
50
+ readonly crc32: number;
51
+ /** The compressed size in bytes. */
52
+ readonly compressedSize: number;
53
+ /** The uncompressed size in bytes. */
54
+ readonly size: number;
55
+ /** The offset of the local header in the archive bytes (the prepended-data shift applied). */
56
+ readonly localOffset: number;
57
+ /** The name ends in `/`: a directory entry, which holds no data. */
58
+ readonly directory: boolean;
59
+ }
60
+
61
+ /** How one entry is read. */
62
+ export interface ReadEntryOptions {
63
+ /** The cancellation signal. */
64
+ readonly signal?: AbortSignal | null | undefined;
65
+ /** Called with the bytes inflated so far by this call. */
66
+ readonly onBytes?: ((inflated: number) => void) | undefined;
67
+ /** The most uncompressed bytes this entry may have (the import's remaining budget). */
68
+ readonly maxBytes: number;
69
+ /** The largest uncompressed-to-compressed ratio allowed (above `RATIO_FLOOR` bytes). */
70
+ readonly maxRatio: number;
71
+ }
72
+
73
+ /** A per-entry size under which the ratio limit does not apply (tiny entries compress well). */
74
+ export const RATIO_FLOOR = 1024 * 1024;
75
+
76
+ const EOCD_SIGNATURE = 0x06054b50;
77
+ const ZIP64_LOCATOR_SIGNATURE = 0x07064b50;
78
+ const ZIP64_EOCD_SIGNATURE = 0x06064b50;
79
+ const CENTRAL_SIGNATURE = 0x02014b50;
80
+ const LOCAL_SIGNATURE = 0x04034b50;
81
+ const EOCD_SIZE = 22;
82
+ const MAX_COMMENT = 65535;
83
+ const CENTRAL_SIZE = 46;
84
+ const LOCAL_SIZE = 30;
85
+ const ZIP64_EOCD_SIZE = 56;
86
+ const ZIP64_LOCATOR_SIZE = 20;
87
+ const U16_MAX = 0xffff;
88
+ const U32_MAX = 0xffffffff;
89
+ /** Bytes between two cancellation checks while a stored entry's CRC is computed. */
90
+ const CRC_SLICE = 1024 * 1024;
91
+
92
+ /** The compression methods other tools write, by number, for the refusal message. */
93
+ const METHOD_NAMES: Readonly<Record<number, string>> = {
94
+ 1: "shrink",
95
+ 6: "implode",
96
+ 9: "deflate64",
97
+ 12: "bzip2",
98
+ 14: "LZMA",
99
+ 93: "zstd",
100
+ 95: "xz",
101
+ 96: "JPEG",
102
+ 97: "WavPack",
103
+ 98: "PPMd",
104
+ 99: "AES encryption",
105
+ };
106
+
107
+ /**
108
+ * Whether bytes start with a local file header signature (`PK\x03\x04`).
109
+ * @param bytes - the bytes
110
+ * @returns true for the start of a zip
111
+ */
112
+ export function startsLikeZip(bytes: Uint8Array): boolean {
113
+ return bytes.byteLength >= 4 && bytes[0] === 0x50 && bytes[1] === 0x4b && bytes[2] === 3 && bytes[3] === 4;
114
+ }
115
+
116
+ /**
117
+ * Read the central directory.
118
+ * @param bytes - the whole archive
119
+ * @returns the entries in directory order; ZipError "not-zip" when there is no end record and the
120
+ * bytes do not start like a zip, "corrupt" for an end record or directory that does not fit,
121
+ * "unsupported" for a split archive
122
+ */
123
+ export function readZipDirectory(bytes: Uint8Array): ZipEntry[] {
124
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
125
+ const eocd = findEndRecord(bytes, view);
126
+ if (eocd < 0) {
127
+ throw startsLikeZip(bytes)
128
+ ? new ZipError("corrupt", "the archive has no end of central directory record (truncated?)")
129
+ : new ZipError("not-zip", "the input is not a zip archive");
130
+ }
131
+ let disk = view.getUint16(eocd + 4, true);
132
+ let directoryDisk = view.getUint16(eocd + 6, true);
133
+ let count = view.getUint16(eocd + 10, true);
134
+ let directorySize = view.getUint32(eocd + 12, true);
135
+ let directoryOffset = view.getUint32(eocd + 16, true);
136
+ let directoryEnd = eocd;
137
+ const locator = eocd - ZIP64_LOCATOR_SIZE;
138
+ if (locator >= 0 && view.getUint32(locator, true) === ZIP64_LOCATOR_SIGNATURE) {
139
+ const record = zip64EndRecord(view, locator);
140
+ disk = view.getUint32(record + 16, true);
141
+ directoryDisk = view.getUint32(record + 20, true);
142
+ count = readU64(view, record + 32);
143
+ directorySize = readU64(view, record + 40);
144
+ directoryOffset = readU64(view, record + 48);
145
+ directoryEnd = record;
146
+ } else if (count === U16_MAX || directorySize === U32_MAX || directoryOffset === U32_MAX) {
147
+ throw new ZipError("corrupt", "the end record needs a zip64 record the archive does not have");
148
+ }
149
+ if (disk !== 0 || directoryDisk !== 0) {
150
+ throw new ZipError("unsupported", "a split (multi-disk) archive cannot be read");
151
+ }
152
+ const start = directoryEnd - directorySize;
153
+ if (start < 0 || directoryOffset > bytes.byteLength) {
154
+ throw new ZipError("corrupt", "the central directory lies outside the archive");
155
+ }
156
+ // data prepended to the archive moves every offset by the same amount
157
+ const shift = start - directoryOffset;
158
+ if (shift < 0) {
159
+ throw new ZipError("corrupt", "the central directory offset does not match its position");
160
+ }
161
+ const entries: ZipEntry[] = [];
162
+ let at = start;
163
+ for (let i = 0; i < count; i++) {
164
+ if (at + CENTRAL_SIZE > directoryEnd || view.getUint32(at, true) !== CENTRAL_SIGNATURE) {
165
+ throw new ZipError("corrupt", `central directory entry ${i + 1} of ${count} is damaged`);
166
+ }
167
+ const nameLength = view.getUint16(at + 28, true);
168
+ const extraLength = view.getUint16(at + 30, true);
169
+ const commentLength = view.getUint16(at + 32, true);
170
+ const next = at + CENTRAL_SIZE + nameLength + extraLength + commentLength;
171
+ if (next > directoryEnd) {
172
+ throw new ZipError("corrupt", `central directory entry ${i + 1} runs past the directory`);
173
+ }
174
+ const name = decodeEntryName(bytes.subarray(at + CENTRAL_SIZE, at + CENTRAL_SIZE + nameLength));
175
+ let compressedSize = view.getUint32(at + 20, true);
176
+ let size = view.getUint32(at + 24, true);
177
+ let localOffset = view.getUint32(at + 42, true);
178
+ if (compressedSize === U32_MAX || size === U32_MAX || localOffset === U32_MAX) {
179
+ ({ compressedSize, size, localOffset } = zip64Extra(view, at + CENTRAL_SIZE + nameLength, extraLength, {
180
+ compressedSize,
181
+ size,
182
+ localOffset,
183
+ }));
184
+ }
185
+ entries.push(
186
+ Object.freeze({
187
+ name,
188
+ method: view.getUint16(at + 10, true),
189
+ flags: view.getUint16(at + 8, true),
190
+ crc32: view.getUint32(at + 16, true),
191
+ compressedSize,
192
+ size,
193
+ localOffset: localOffset + shift,
194
+ directory: name.endsWith("/"),
195
+ }),
196
+ );
197
+ at = next;
198
+ }
199
+ return entries;
200
+ }
201
+
202
+ /**
203
+ * The position of the end of central directory record: the last signature whose comment length
204
+ * fits the archive, else the last signature at all (an archive with bytes after its comment).
205
+ * @param bytes - the archive
206
+ * @param view - a view of it
207
+ * @returns the offset, or -1
208
+ */
209
+ function findEndRecord(bytes: Uint8Array, view: DataView): number {
210
+ const lowest = Math.max(0, bytes.byteLength - EOCD_SIZE - MAX_COMMENT);
211
+ let fallback = -1;
212
+ for (let at = bytes.byteLength - EOCD_SIZE; at >= lowest; at--) {
213
+ if (view.getUint32(at, true) !== EOCD_SIGNATURE) {
214
+ continue;
215
+ }
216
+ if (at + EOCD_SIZE + view.getUint16(at + 20, true) === bytes.byteLength) {
217
+ return at;
218
+ }
219
+ if (fallback < 0) {
220
+ fallback = at;
221
+ }
222
+ }
223
+ return fallback;
224
+ }
225
+
226
+ /**
227
+ * The zip64 end of central directory record a locator points at (or, when the archive has bytes
228
+ * prepended, the one right before the locator).
229
+ * @param view - the archive
230
+ * @param locator - the locator's offset
231
+ * @returns the record's offset
232
+ */
233
+ function zip64EndRecord(view: DataView, locator: number): number {
234
+ const stated = readU64(view, locator + 8);
235
+ for (const at of [stated, locator - ZIP64_EOCD_SIZE]) {
236
+ if (at >= 0 && at + ZIP64_EOCD_SIZE <= locator && view.getUint32(at, true) === ZIP64_EOCD_SIGNATURE) {
237
+ return at;
238
+ }
239
+ }
240
+ throw new ZipError("corrupt", "the zip64 end record the locator names is missing");
241
+ }
242
+
243
+ /**
244
+ * The zip64 extended information extra field (header id 0x0001) of a central entry: the 64-bit
245
+ * values of the fields whose 32-bit slot is 0xFFFFFFFF, in the order APPNOTE 4.5.3 gives.
246
+ * @param view - the archive
247
+ * @param at - the start of the extra field
248
+ * @param length - its length
249
+ * @param fields - the 32-bit values
250
+ * @param fields.compressedSize - the compressed size slot
251
+ * @param fields.size - the uncompressed size slot
252
+ * @param fields.localOffset - the local header offset slot
253
+ * @returns the resolved values
254
+ */
255
+ function zip64Extra(
256
+ view: DataView,
257
+ at: number,
258
+ length: number,
259
+ fields: { compressedSize: number; size: number; localOffset: number },
260
+ ): { compressedSize: number; size: number; localOffset: number } {
261
+ const out = { ...fields };
262
+ for (let p = at; p + 4 <= at + length; ) {
263
+ const id = view.getUint16(p, true);
264
+ const dataLength = view.getUint16(p + 2, true);
265
+ if (id === 1) {
266
+ let q = p + 4;
267
+ const end = q + dataLength;
268
+ for (const key of ["size", "compressedSize", "localOffset"] as const) {
269
+ if (fields[key] === U32_MAX) {
270
+ if (q + 8 > end) {
271
+ throw new ZipError("corrupt", "a zip64 extra field is too short");
272
+ }
273
+ out[key] = readU64(view, q);
274
+ q += 8;
275
+ }
276
+ }
277
+ return out;
278
+ }
279
+ p += 4 + dataLength;
280
+ }
281
+ throw new ZipError("corrupt", "an entry needs a zip64 extra field it does not have");
282
+ }
283
+
284
+ /**
285
+ * An unsigned 64-bit little-endian integer as a number (exact up to 2^53).
286
+ * @param view - the bytes
287
+ * @param at - the offset
288
+ * @returns the value; ZipError "unsupported" beyond 2^53
289
+ */
290
+ function readU64(view: DataView, at: number): number {
291
+ const high = view.getUint32(at + 4, true);
292
+ if (high >= 0x200000) {
293
+ throw new ZipError("unsupported", "a zip64 size or offset beyond 2^53 cannot be read");
294
+ }
295
+ return high * 0x100000000 + view.getUint32(at, true);
296
+ }
297
+
298
+ /**
299
+ * Read one entry's data: stored entries are sliced and CRC-checked, deflated ones inflated.
300
+ * @param bytes - the whole archive
301
+ * @param entry - the entry
302
+ * @param options - cancellation, progress and the size limits
303
+ * @returns the uncompressed bytes; ZipError for a damaged, encrypted, unsupported or oversized entry
304
+ */
305
+ export async function readZipEntry(bytes: Uint8Array, entry: ZipEntry, options: ReadEntryOptions): Promise<Uint8Array> {
306
+ if ((entry.flags & 1) !== 0 || entry.method === 99) {
307
+ throw new ZipError("unsupported", `${entry.name}: the entry is encrypted`);
308
+ }
309
+ if (entry.method !== 0 && entry.method !== 8) {
310
+ const name = METHOD_NAMES[entry.method] ?? `method ${entry.method}`;
311
+ throw new ZipError(
312
+ "unsupported",
313
+ `${entry.name}: compression ${name} is not supported (only stored and deflate)`,
314
+ );
315
+ }
316
+ if (entry.size > options.maxBytes) {
317
+ throw new ZipError(
318
+ "too-large",
319
+ `${entry.name}: ${entry.size} uncompressed bytes exceed the limit of ${options.maxBytes}`,
320
+ );
321
+ }
322
+ if (entry.size > RATIO_FLOOR && entry.size > options.maxRatio * Math.max(entry.compressedSize, 1)) {
323
+ throw new ZipError(
324
+ "too-large",
325
+ `${entry.name}: ${entry.size} bytes from ${entry.compressedSize} is a compression ratio above ${options.maxRatio}:1`,
326
+ );
327
+ }
328
+ const data = entryData(bytes, entry);
329
+ if (entry.method === 0) {
330
+ if (data.byteLength !== entry.size) {
331
+ throw new ZipError("corrupt", `${entry.name}: a stored entry's sizes disagree`);
332
+ }
333
+ let crc = 0xffffffff;
334
+ for (let at = 0; at < data.byteLength; at += CRC_SLICE) {
335
+ throwIfAborted(options.signal);
336
+ crc = crcUpdate(crc, data.subarray(at, Math.min(at + CRC_SLICE, data.byteLength)));
337
+ options.onBytes?.(Math.min(at + CRC_SLICE, data.byteLength));
338
+ }
339
+ if ((crc ^ 0xffffffff) >>> 0 !== entry.crc32) {
340
+ throw new ZipError("corrupt", `${entry.name}: the data does not match its CRC-32`);
341
+ }
342
+ return data;
343
+ }
344
+ return inflate(data, entry, options);
345
+ }
346
+
347
+ /**
348
+ * The compressed bytes of an entry, located through its local header (whose name and extra
349
+ * lengths can differ from the central directory's).
350
+ * @param bytes - the archive
351
+ * @param entry - the entry
352
+ * @returns exactly compressedSize bytes
353
+ */
354
+ function entryData(bytes: Uint8Array, entry: ZipEntry): Uint8Array {
355
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
356
+ const at = entry.localOffset;
357
+ if (at + LOCAL_SIZE > bytes.byteLength || view.getUint32(at, true) !== LOCAL_SIGNATURE) {
358
+ throw new ZipError("corrupt", `${entry.name}: the local header is missing`);
359
+ }
360
+ const start = at + LOCAL_SIZE + view.getUint16(at + 26, true) + view.getUint16(at + 28, true);
361
+ const end = start + entry.compressedSize;
362
+ if (end > bytes.byteLength) {
363
+ throw new ZipError("corrupt", `${entry.name}: the entry's data runs past the end of the archive (truncated?)`);
364
+ }
365
+ return bytes.subarray(start, end);
366
+ }
367
+
368
+ /**
369
+ * Inflate a deflate stream through `DecompressionStream("gzip")`, wrapped in a gzip member that
370
+ * carries the directory's CRC-32 and size, so the platform checks both.
371
+ * @param data - the raw deflate bytes
372
+ * @param entry - the entry
373
+ * @param options - cancellation, progress and limits
374
+ * @returns the bytes
375
+ */
376
+ async function inflate(data: Uint8Array, entry: ZipEntry, options: ReadEntryOptions): Promise<Uint8Array> {
377
+ if (typeof DecompressionStream !== "function") {
378
+ throw new ZipError(
379
+ "unsupported",
380
+ "this runtime has no DecompressionStream, so deflated entries cannot be read",
381
+ );
382
+ }
383
+ const member = new Uint8Array(10 + data.byteLength + 8);
384
+ member.set([0x1f, 0x8b, 8, 0, 0, 0, 0, 0, 0, 0xff]);
385
+ member.set(data, 10);
386
+ const trailer = new DataView(member.buffer, 10 + data.byteLength, 8);
387
+ trailer.setUint32(0, entry.crc32, true);
388
+ trailer.setUint32(4, entry.size % 0x100000000, true);
389
+ const source = new ReadableStream<BufferSource>({
390
+ start(controller): void {
391
+ controller.enqueue(member);
392
+ controller.close();
393
+ },
394
+ });
395
+ const reader = source.pipeThrough(new DecompressionStream("gzip")).getReader();
396
+ const parts: Uint8Array[] = [];
397
+ let total = 0;
398
+ try {
399
+ for (;;) {
400
+ throwIfAborted(options.signal);
401
+ let chunk: ReadableStreamReadResult<Uint8Array>;
402
+ try {
403
+ chunk = await reader.read();
404
+ } catch {
405
+ throw new ZipError(
406
+ "corrupt",
407
+ `${entry.name}: the deflate data is damaged or does not match its CRC-32 and size`,
408
+ );
409
+ }
410
+ if (chunk.done) {
411
+ break;
412
+ }
413
+ total += chunk.value.byteLength;
414
+ if (total > entry.size) {
415
+ throw new ZipError("corrupt", `${entry.name}: the data is longer than its directory size`);
416
+ }
417
+ parts.push(chunk.value);
418
+ options.onBytes?.(total);
419
+ }
420
+ } finally {
421
+ await reader.cancel().catch(() => undefined);
422
+ }
423
+ if (total !== entry.size) {
424
+ throw new ZipError("corrupt", `${entry.name}: the data is shorter than its directory size`);
425
+ }
426
+ if (parts.length === 1) {
427
+ return parts[0];
428
+ }
429
+ const out = new Uint8Array(total);
430
+ let at = 0;
431
+ for (const part of parts) {
432
+ out.set(part, at);
433
+ at += part.byteLength;
434
+ }
435
+ return out;
436
+ }
437
+
438
+ /** The CRC-32 table (polynomial 0xEDB88320). */
439
+ const CRC_TABLE = ((): Uint32Array => {
440
+ const table = new Uint32Array(256);
441
+ for (let n = 0; n < 256; n++) {
442
+ let c = n;
443
+ for (let k = 0; k < 8; k++) {
444
+ c = (c & 1) === 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
445
+ }
446
+ table[n] = c >>> 0;
447
+ }
448
+ return table;
449
+ })();
450
+
451
+ /**
452
+ * Continue a CRC-32 over more bytes.
453
+ * @param crc - the running value (start with 0xFFFFFFFF)
454
+ * @param bytes - the bytes
455
+ * @returns the running value (XOR with 0xFFFFFFFF to finish)
456
+ */
457
+ function crcUpdate(crc: number, bytes: Uint8Array): number {
458
+ let c = crc;
459
+ for (const byte of bytes) {
460
+ c = CRC_TABLE[(c ^ byte) & 0xff] ^ (c >>> 8);
461
+ }
462
+ return c >>> 0;
463
+ }
464
+
465
+ /**
466
+ * The CRC-32 of bytes.
467
+ * @param bytes - the bytes
468
+ * @returns the checksum
469
+ */
470
+ export function crc32(bytes: Uint8Array): number {
471
+ return (crcUpdate(0xffffffff, bytes) ^ 0xffffffff) >>> 0;
472
+ }
@@ -1194,6 +1194,16 @@ class CxReader {
1194
1194
  return this.plan.nodes === null || this.plan.nodes.has(id);
1195
1195
  }
1196
1196
 
1197
+ /**
1198
+ * The key of the node and edge row maps for a CX id as an aspect writes it: the id the `ids`
1199
+ * option makes of it, which is what idOf() stored the element under.
1200
+ * @param raw - the CX id
1201
+ * @returns the row key
1202
+ */
1203
+ private key(raw: NodeId): NodeId {
1204
+ return this.options.ids === "keep" ? raw : this.coercer.value(raw);
1205
+ }
1206
+
1197
1207
  /**
1198
1208
  * Whether a scoped value applies to this graph: unscoped, or scoped to its subnetwork. A scope
1199
1209
  * naming no subnetwork is counted as dangling.
@@ -1506,10 +1516,7 @@ class CxReader {
1506
1516
  continue;
1507
1517
  }
1508
1518
  const target = refId(value.po);
1509
- const row =
1510
- target === null
1511
- ? undefined
1512
- : rows.get(this.options.ids === "keep" ? target : this.coercer.value(target));
1519
+ const row = target === null ? undefined : rows.get(this.key(target));
1513
1520
  if (row === undefined) {
1514
1521
  if (target === null || !root.has(target)) {
1515
1522
  this.dangle(`${domain} attribute target`);
@@ -1705,7 +1712,8 @@ class CxReader {
1705
1712
  report.counts.skippedNodes++;
1706
1713
  continue;
1707
1714
  }
1708
- if (!this.inGraph(id)) {
1715
+ // membership lists hold CX ids as written, before the ids option coerces them
1716
+ if (!this.inGraph(refId(value["@id"]) ?? id)) {
1709
1717
  continue;
1710
1718
  }
1711
1719
  let row = this.nodeRows.get(id);
@@ -1803,18 +1811,18 @@ class CxReader {
1803
1811
  });
1804
1812
  continue;
1805
1813
  }
1806
- let groupRow = this.nodeRows.get(id);
1814
+ let groupRow = this.nodeRows.get(this.key(id));
1807
1815
  if (groupRow === undefined) {
1808
1816
  if (this.rootNodes.has(id) || !this.hasMemberIn(value.nodes)) {
1809
1817
  continue;
1810
1818
  }
1811
1819
  try {
1812
- groupRow = this.sink.addNode(id);
1820
+ groupRow = this.sink.addNode(this.key(id));
1813
1821
  } catch (err) {
1814
1822
  this.report.recordError(err, { line, element: String(id) });
1815
1823
  continue;
1816
1824
  }
1817
- this.nodeRows.set(id, groupRow);
1825
+ this.nodeRows.set(this.key(id), groupRow);
1818
1826
  this.report.counts.nodes++;
1819
1827
  this.report.warning(
1820
1828
  "coercion",
@@ -1831,7 +1839,7 @@ class CxReader {
1831
1839
  }
1832
1840
  for (const raw of Array.isArray(value.nodes) ? (value.nodes as unknown[]) : []) {
1833
1841
  const member = refId(raw);
1834
- const row = member === null ? undefined : this.nodeRows.get(member);
1842
+ const row = member === null ? undefined : this.nodeRows.get(this.key(member));
1835
1843
  if (row === undefined) {
1836
1844
  if (member === null || !this.rootNodes.has(member)) {
1837
1845
  this.report.error(
@@ -1888,7 +1896,7 @@ class CxReader {
1888
1896
  Array.isArray(raw) &&
1889
1897
  raw.some((item) => {
1890
1898
  const id = refId(item);
1891
- return id !== null && this.nodeRows.has(id);
1899
+ return id !== null && this.nodeRows.has(this.key(id));
1892
1900
  })
1893
1901
  );
1894
1902
  }
@@ -1976,7 +1984,7 @@ class CxReader {
1976
1984
  continue;
1977
1985
  }
1978
1986
  const node = refId(value.node);
1979
- const row = node === null ? undefined : this.nodeRows.get(node);
1987
+ const row = node === null ? undefined : this.nodeRows.get(this.key(node));
1980
1988
  if (row === undefined) {
1981
1989
  if (node === null || !this.rootNodes.has(node)) {
1982
1990
  this.dangle("layout entry");
@@ -2088,7 +2096,7 @@ class CxReader {
2088
2096
  report.counts.skippedEdges++;
2089
2097
  continue;
2090
2098
  }
2091
- if (members !== null && !members.has(id)) {
2099
+ if (members !== null && !members.has(refId(value["@id"]) ?? id)) {
2092
2100
  continue;
2093
2101
  }
2094
2102
  if (value.s === undefined || value.s === null || value.t === undefined || value.t === null) {
@@ -2206,7 +2214,7 @@ class CxReader {
2206
2214
  if (isRecord(value) && this.scopePeek(value.s) !== 0) {
2207
2215
  const po = refId(value.po);
2208
2216
  if (po !== null && value.v !== undefined && value.v !== null) {
2209
- this.weights.set(po, plainJson(value.v));
2217
+ this.weights.set(this.key(po), plainJson(value.v));
2210
2218
  }
2211
2219
  }
2212
2220
  }
@@ -2312,8 +2320,8 @@ class CxReader {
2312
2320
  case "edges": {
2313
2321
  const domain = value.properties_of === "nodes" ? "node" : "edge";
2314
2322
  const target = refId(value.applies_to);
2315
- const row =
2316
- target === null ? undefined : (domain === "node" ? this.nodeRows : this.edgeRows).get(target);
2323
+ const rows = domain === "node" ? this.nodeRows : this.edgeRows;
2324
+ const row = target === null ? undefined : rows.get(this.key(target));
2317
2325
  if (row === undefined) {
2318
2326
  if (target === null || !(domain === "node" ? this.rootNodes : this.rootEdges).has(target)) {
2319
2327
  this.dangle(`${domain} visual property entry`);
@@ -2408,7 +2416,7 @@ class CxReader {
2408
2416
  continue;
2409
2417
  }
2410
2418
  const po = refId(value.po);
2411
- const row = po === null ? undefined : this.nodeRows.get(po);
2419
+ const row = po === null ? undefined : this.nodeRows.get(this.key(po));
2412
2420
  if (row === undefined) {
2413
2421
  if (po === null || !this.rootNodes.has(po)) {
2414
2422
  this.dangle("functionTerms entry");
@@ -2426,8 +2434,8 @@ class CxReader {
2426
2434
  }
2427
2435
  const node = refId(value.node);
2428
2436
  const edge = refId(value.edge);
2429
- const row = node === null ? undefined : this.nodeRows.get(node);
2430
- const edgeRow = edge === null ? undefined : this.edgeRows.get(edge);
2437
+ const row = node === null ? undefined : this.nodeRows.get(this.key(node));
2438
+ const edgeRow = edge === null ? undefined : this.edgeRows.get(this.key(edge));
2431
2439
  if (row === undefined || edgeRow === undefined) {
2432
2440
  if (node === null || !this.rootNodes.has(node) || edge === null || !this.rootEdges.has(edge)) {
2433
2441
  this.dangle("reifiedEdges entry");
@@ -2501,7 +2509,7 @@ class CxReader {
2501
2509
  .map(Number);
2502
2510
  for (const raw of targets) {
2503
2511
  const target = refId(raw);
2504
- const row = target === null ? undefined : rows.get(target);
2512
+ const row = target === null ? undefined : rows.get(this.key(target));
2505
2513
  if (row === undefined) {
2506
2514
  if (target === null || !root.has(target)) {
2507
2515
  this.dangle(`${aspectName} entry`);