@poe-platform/safe-fs 0.1.814 → 0.1.815

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.
@@ -54,6 +54,8 @@ export interface XmlStepLimits extends XmlLimits {
54
54
  readonly events?: (event: XmlStreamEvent) => void;
55
55
  }
56
56
  export interface XmlSourceLimits extends XmlStepLimits {
57
+ /** Keep normalized declaration fields instead of the complete source spelling. */
58
+ readonly compactDeclaration?: boolean;
57
59
  /** Emit content bodies in bounded fragments, marking continuations of the same logical
58
60
  * node. Requires retainTree: false; consumers must preserve fragment identity. */
59
61
  readonly fragmentContent?: boolean;
@@ -176,11 +176,10 @@ function* entities(text, recover) {
176
176
  }
177
177
  return result;
178
178
  }
179
- function* validDeclaration(content, expectedEncoding) {
180
- let offset = 0;
179
+ function* readDeclaration(source, offset, end, expectedEncoding) {
181
180
  const whitespace = function* () {
182
181
  const start = offset;
183
- while (offset < content.length && " \t\n\r".includes(content[offset])) {
182
+ while (offset < end && " \t\n\r".includes(yield* source.slice(offset, offset + 1))) {
184
183
  offset++;
185
184
  if ((offset - start) % 512 === 0)
186
185
  yield 512;
@@ -189,53 +188,58 @@ function* validDeclaration(content, expectedEncoding) {
189
188
  yield (offset - start) % 512;
190
189
  return offset - start;
191
190
  };
192
- const field = function* (name) {
193
- if (content.slice(offset, offset + name.length) !== name)
191
+ const field = function* (name, maxLength) {
192
+ if ((yield* source.slice(offset, Math.min(end, offset + name.length))) !== name)
194
193
  return undefined;
195
194
  offset += name.length;
196
195
  yield name.length;
197
196
  yield* whitespace();
198
- if (content[offset++] !== "=")
197
+ if (offset >= end || (yield* source.charCodeAt(offset++)) !== 61)
199
198
  return undefined;
200
199
  yield* whitespace();
201
- const quote = content[offset++];
200
+ if (offset >= end)
201
+ return undefined;
202
+ const quote = yield* source.slice(offset++, offset);
202
203
  if (quote !== "'" && quote !== '"')
203
204
  return undefined;
204
205
  const start = offset;
205
- while (offset < content.length && content[offset] !== quote) {
206
+ while (offset < end && (yield* source.slice(offset, offset + 1)) !== quote) {
206
207
  offset++;
207
208
  if ((offset - start) % 512 === 0)
208
209
  yield 512;
209
210
  }
210
211
  if ((offset - start) % 512)
211
212
  yield (offset - start) % 512;
212
- if (offset >= content.length)
213
+ if (offset >= end || offset - start > maxLength)
213
214
  return undefined;
214
- return content.slice(start, offset++);
215
+ return yield* source.slice(start, offset++);
215
216
  };
216
- if (!(yield* whitespace()) || (yield* field("version")) !== "1.0")
217
- return false;
217
+ if (!(yield* whitespace()) || (yield* field("version", 3)) !== "1.0")
218
+ return undefined;
219
+ let result = '<?xml version="1.0"';
218
220
  let spacing = yield* whitespace();
219
- if (content.slice(offset, offset + 8) === "encoding") {
221
+ if ((yield* source.slice(offset, Math.min(end, offset + 8))) === "encoding") {
220
222
  if (!spacing)
221
- return false;
222
- const encoding = yield* field("encoding");
223
- if (encoding === undefined || encoding.length > 8 || !["utf-8", "utf-16", "utf-16le", "utf-16be"].includes(encoding.toLowerCase()))
224
- return false;
223
+ return undefined;
224
+ const encoding = yield* field("encoding", 8);
225
+ if (encoding === undefined || !["utf-8", "utf-16", "utf-16le", "utf-16be"].includes(encoding.toLowerCase()))
226
+ return undefined;
225
227
  if (expectedEncoding !== undefined && encoding.toLowerCase() !== expectedEncoding.toLowerCase()
226
228
  && !(encoding.toLowerCase() === "utf-16" && (expectedEncoding === "UTF-16LE" || expectedEncoding === "UTF-16BE")))
227
- return false;
229
+ return undefined;
230
+ result += ` encoding="${encoding}"`;
228
231
  spacing = yield* whitespace();
229
232
  }
230
- if (content.slice(offset, offset + 10) === "standalone") {
233
+ if ((yield* source.slice(offset, Math.min(end, offset + 10))) === "standalone") {
231
234
  if (!spacing)
232
- return false;
233
- const standalone = yield* field("standalone");
234
- if (standalone === undefined || standalone.length > 3 || !["yes", "no"].includes(standalone))
235
- return false;
235
+ return undefined;
236
+ const standalone = yield* field("standalone", 3);
237
+ if (standalone === undefined || !["yes", "no"].includes(standalone))
238
+ return undefined;
239
+ result += ` standalone="${standalone}"`;
236
240
  yield* whitespace();
237
241
  }
238
- return offset === content.length;
242
+ return offset === end ? result + "?>" : undefined;
239
243
  }
240
244
  function validateLimits(limits) {
241
245
  for (const limit of [limits.maxDepth, limits.maxNodes, limits.maxAttributes, limits.maxContentNodes, limits.maxAttributesPerElement, limits.maxNamespaces]) {
@@ -485,12 +489,13 @@ export function* parseXmlSourceSteps(length, limits = {}) {
485
489
  if (end < 0)
486
490
  invalid("unterminated processing instruction");
487
491
  if (target.length === 3 && target.toLowerCase() === "xml") {
488
- const content = yield* source.slice(offset, end);
489
- if (start !== 0 || target !== "xml"
490
- || !(yield* validDeclaration(content, limits.expectedEncoding)))
492
+ if (start !== 0 || target !== "xml")
493
+ invalid("unsupported XML declaration");
494
+ const parsed = yield* readDeclaration(source, offset, end, limits.expectedEncoding);
495
+ if (parsed === undefined)
491
496
  invalid("unsupported XML declaration");
492
497
  if (retainContent)
493
- declaration = yield* source.slice(start, end + 2);
498
+ declaration = limits.compactDeclaration ? parsed : yield* source.slice(start, end + 2);
494
499
  }
495
500
  else {
496
501
  if (offset < end && !" \t\n\r".includes(yield* source.slice(offset, offset + 1)))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@poe-platform/safe-fs",
3
- "version": "0.1.814",
3
+ "version": "0.1.815",
4
4
  "description": "Composable filesystem with a portable core and explicit Node adapters",
5
5
  "type": "module",
6
6
  "license": "MIT",