@embedder/embedder 2.0.29 → 2.0.31

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.
@@ -1,835 +0,0 @@
1
- #!/usr/bin/env node
2
- import { D as Diff, t as tokenize } from "./cli-Cvo-V2B8.js";
3
- import { b, c, d, a, f, l, s } from "./cli-Cvo-V2B8.js";
4
- import "./_commonjsHelpers-CSHRO93D.js";
5
- import "signal-exit";
6
- import "node:process";
7
- import "node:events";
8
- import "node:fs";
9
- import "module";
10
- import "node:os";
11
- import "node:tty";
12
- import "node:buffer";
13
- import "node:util";
14
- import "node:path";
15
- import "node:url";
16
- import "node:child_process";
17
- import "node:fs/promises";
18
- import "@sentry/node";
19
- import "fs";
20
- import "crypto";
21
- import "os";
22
- import "path";
23
- import "http";
24
- import "url";
25
- import "util";
26
- import "stream";
27
- import "buffer";
28
- import "events";
29
- import "zlib";
30
- import "tty";
31
- import "string_decoder";
32
- import "https";
33
- import "node:crypto";
34
- import "node:stream";
35
- import "node:string_decoder";
36
- import "fs/promises";
37
- import "child_process";
38
- class CharacterDiff extends Diff {
39
- }
40
- const characterDiff = new CharacterDiff();
41
- function diffChars(oldStr, newStr, options) {
42
- return characterDiff.diff(oldStr, newStr, options);
43
- }
44
- function longestCommonPrefix(str1, str2) {
45
- let i;
46
- for (i = 0; i < str1.length && i < str2.length; i++) {
47
- if (str1[i] != str2[i]) {
48
- return str1.slice(0, i);
49
- }
50
- }
51
- return str1.slice(0, i);
52
- }
53
- function longestCommonSuffix(str1, str2) {
54
- let i;
55
- if (!str1 || !str2 || str1[str1.length - 1] != str2[str2.length - 1]) {
56
- return "";
57
- }
58
- for (i = 0; i < str1.length && i < str2.length; i++) {
59
- if (str1[str1.length - (i + 1)] != str2[str2.length - (i + 1)]) {
60
- return str1.slice(-i);
61
- }
62
- }
63
- return str1.slice(-i);
64
- }
65
- function replacePrefix(string, oldPrefix, newPrefix) {
66
- if (string.slice(0, oldPrefix.length) != oldPrefix) {
67
- throw Error(`string ${JSON.stringify(string)} doesn't start with prefix ${JSON.stringify(oldPrefix)}; this is a bug`);
68
- }
69
- return newPrefix + string.slice(oldPrefix.length);
70
- }
71
- function replaceSuffix(string, oldSuffix, newSuffix) {
72
- if (!oldSuffix) {
73
- return string + newSuffix;
74
- }
75
- if (string.slice(-oldSuffix.length) != oldSuffix) {
76
- throw Error(`string ${JSON.stringify(string)} doesn't end with suffix ${JSON.stringify(oldSuffix)}; this is a bug`);
77
- }
78
- return string.slice(0, -oldSuffix.length) + newSuffix;
79
- }
80
- function removePrefix(string, oldPrefix) {
81
- return replacePrefix(string, oldPrefix, "");
82
- }
83
- function removeSuffix(string, oldSuffix) {
84
- return replaceSuffix(string, oldSuffix, "");
85
- }
86
- function maximumOverlap(string1, string2) {
87
- return string2.slice(0, overlapCount(string1, string2));
88
- }
89
- function overlapCount(a2, b2) {
90
- let startA = 0;
91
- if (a2.length > b2.length) {
92
- startA = a2.length - b2.length;
93
- }
94
- let endB = b2.length;
95
- if (a2.length < b2.length) {
96
- endB = a2.length;
97
- }
98
- const map = Array(endB);
99
- let k = 0;
100
- map[0] = 0;
101
- for (let j = 1; j < endB; j++) {
102
- if (b2[j] == b2[k]) {
103
- map[j] = map[k];
104
- } else {
105
- map[j] = k;
106
- }
107
- while (k > 0 && b2[j] != b2[k]) {
108
- k = map[k];
109
- }
110
- if (b2[j] == b2[k]) {
111
- k++;
112
- }
113
- }
114
- k = 0;
115
- for (let i = startA; i < a2.length; i++) {
116
- while (k > 0 && a2[i] != b2[k]) {
117
- k = map[k];
118
- }
119
- if (a2[i] == b2[k]) {
120
- k++;
121
- }
122
- }
123
- return k;
124
- }
125
- function hasOnlyWinLineEndings(string) {
126
- return string.includes("\r\n") && !string.startsWith("\n") && !string.match(/[^\r]\n/);
127
- }
128
- function hasOnlyUnixLineEndings(string) {
129
- return !string.includes("\r\n") && string.includes("\n");
130
- }
131
- function trailingWs(string) {
132
- let i;
133
- for (i = string.length - 1; i >= 0; i--) {
134
- if (!string[i].match(/\s/)) {
135
- break;
136
- }
137
- }
138
- return string.substring(i + 1);
139
- }
140
- function leadingWs(string) {
141
- const match = string.match(/^\s*/);
142
- return match ? match[0] : "";
143
- }
144
- const extendedWordChars = "a-zA-Z0-9_\\u{C0}-\\u{FF}\\u{D8}-\\u{F6}\\u{F8}-\\u{2C6}\\u{2C8}-\\u{2D7}\\u{2DE}-\\u{2FF}\\u{1E00}-\\u{1EFF}";
145
- const tokenizeIncludingWhitespace = new RegExp(`[${extendedWordChars}]+|\\s+|[^${extendedWordChars}]`, "ug");
146
- class WordDiff extends Diff {
147
- equals(left, right, options) {
148
- if (options.ignoreCase) {
149
- left = left.toLowerCase();
150
- right = right.toLowerCase();
151
- }
152
- return left.trim() === right.trim();
153
- }
154
- tokenize(value, options = {}) {
155
- let parts;
156
- if (options.intlSegmenter) {
157
- const segmenter = options.intlSegmenter;
158
- if (segmenter.resolvedOptions().granularity != "word") {
159
- throw new Error('The segmenter passed must have a granularity of "word"');
160
- }
161
- parts = Array.from(segmenter.segment(value), (segment) => segment.segment);
162
- } else {
163
- parts = value.match(tokenizeIncludingWhitespace) || [];
164
- }
165
- const tokens = [];
166
- let prevPart = null;
167
- parts.forEach((part) => {
168
- if (/\s/.test(part)) {
169
- if (prevPart == null) {
170
- tokens.push(part);
171
- } else {
172
- tokens.push(tokens.pop() + part);
173
- }
174
- } else if (prevPart != null && /\s/.test(prevPart)) {
175
- if (tokens[tokens.length - 1] == prevPart) {
176
- tokens.push(tokens.pop() + part);
177
- } else {
178
- tokens.push(prevPart + part);
179
- }
180
- } else {
181
- tokens.push(part);
182
- }
183
- prevPart = part;
184
- });
185
- return tokens;
186
- }
187
- join(tokens) {
188
- return tokens.map((token, i) => {
189
- if (i == 0) {
190
- return token;
191
- } else {
192
- return token.replace(/^\s+/, "");
193
- }
194
- }).join("");
195
- }
196
- postProcess(changes, options) {
197
- if (!changes || options.oneChangePerToken) {
198
- return changes;
199
- }
200
- let lastKeep = null;
201
- let insertion = null;
202
- let deletion = null;
203
- changes.forEach((change) => {
204
- if (change.added) {
205
- insertion = change;
206
- } else if (change.removed) {
207
- deletion = change;
208
- } else {
209
- if (insertion || deletion) {
210
- dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, change);
211
- }
212
- lastKeep = change;
213
- insertion = null;
214
- deletion = null;
215
- }
216
- });
217
- if (insertion || deletion) {
218
- dedupeWhitespaceInChangeObjects(lastKeep, deletion, insertion, null);
219
- }
220
- return changes;
221
- }
222
- }
223
- const wordDiff = new WordDiff();
224
- function diffWords(oldStr, newStr, options) {
225
- if ((options === null || options === void 0 ? void 0 : options.ignoreWhitespace) != null && !options.ignoreWhitespace) {
226
- return diffWordsWithSpace(oldStr, newStr, options);
227
- }
228
- return wordDiff.diff(oldStr, newStr, options);
229
- }
230
- function dedupeWhitespaceInChangeObjects(startKeep, deletion, insertion, endKeep) {
231
- if (deletion && insertion) {
232
- const oldWsPrefix = leadingWs(deletion.value);
233
- const oldWsSuffix = trailingWs(deletion.value);
234
- const newWsPrefix = leadingWs(insertion.value);
235
- const newWsSuffix = trailingWs(insertion.value);
236
- if (startKeep) {
237
- const commonWsPrefix = longestCommonPrefix(oldWsPrefix, newWsPrefix);
238
- startKeep.value = replaceSuffix(startKeep.value, newWsPrefix, commonWsPrefix);
239
- deletion.value = removePrefix(deletion.value, commonWsPrefix);
240
- insertion.value = removePrefix(insertion.value, commonWsPrefix);
241
- }
242
- if (endKeep) {
243
- const commonWsSuffix = longestCommonSuffix(oldWsSuffix, newWsSuffix);
244
- endKeep.value = replacePrefix(endKeep.value, newWsSuffix, commonWsSuffix);
245
- deletion.value = removeSuffix(deletion.value, commonWsSuffix);
246
- insertion.value = removeSuffix(insertion.value, commonWsSuffix);
247
- }
248
- } else if (insertion) {
249
- if (startKeep) {
250
- const ws = leadingWs(insertion.value);
251
- insertion.value = insertion.value.substring(ws.length);
252
- }
253
- if (endKeep) {
254
- const ws = leadingWs(endKeep.value);
255
- endKeep.value = endKeep.value.substring(ws.length);
256
- }
257
- } else if (startKeep && endKeep) {
258
- const newWsFull = leadingWs(endKeep.value), delWsStart = leadingWs(deletion.value), delWsEnd = trailingWs(deletion.value);
259
- const newWsStart = longestCommonPrefix(newWsFull, delWsStart);
260
- deletion.value = removePrefix(deletion.value, newWsStart);
261
- const newWsEnd = longestCommonSuffix(removePrefix(newWsFull, newWsStart), delWsEnd);
262
- deletion.value = removeSuffix(deletion.value, newWsEnd);
263
- endKeep.value = replacePrefix(endKeep.value, newWsFull, newWsEnd);
264
- startKeep.value = replaceSuffix(startKeep.value, newWsFull, newWsFull.slice(0, newWsFull.length - newWsEnd.length));
265
- } else if (endKeep) {
266
- const endKeepWsPrefix = leadingWs(endKeep.value);
267
- const deletionWsSuffix = trailingWs(deletion.value);
268
- const overlap = maximumOverlap(deletionWsSuffix, endKeepWsPrefix);
269
- deletion.value = removeSuffix(deletion.value, overlap);
270
- } else if (startKeep) {
271
- const startKeepWsSuffix = trailingWs(startKeep.value);
272
- const deletionWsPrefix = leadingWs(deletion.value);
273
- const overlap = maximumOverlap(startKeepWsSuffix, deletionWsPrefix);
274
- deletion.value = removePrefix(deletion.value, overlap);
275
- }
276
- }
277
- class WordsWithSpaceDiff extends Diff {
278
- tokenize(value) {
279
- const regex = new RegExp(`(\\r?\\n)|[${extendedWordChars}]+|[^\\S\\n\\r]+|[^${extendedWordChars}]`, "ug");
280
- return value.match(regex) || [];
281
- }
282
- }
283
- const wordsWithSpaceDiff = new WordsWithSpaceDiff();
284
- function diffWordsWithSpace(oldStr, newStr, options) {
285
- return wordsWithSpaceDiff.diff(oldStr, newStr, options);
286
- }
287
- function isSentenceEndPunct(char) {
288
- return char == "." || char == "!" || char == "?";
289
- }
290
- class SentenceDiff extends Diff {
291
- tokenize(value) {
292
- var _a;
293
- const result = [];
294
- let tokenStartI = 0;
295
- for (let i = 0; i < value.length; i++) {
296
- if (i == value.length - 1) {
297
- result.push(value.slice(tokenStartI));
298
- break;
299
- }
300
- if (isSentenceEndPunct(value[i]) && value[i + 1].match(/\s/)) {
301
- result.push(value.slice(tokenStartI, i + 1));
302
- i = tokenStartI = i + 1;
303
- while ((_a = value[i + 1]) === null || _a === void 0 ? void 0 : _a.match(/\s/)) {
304
- i++;
305
- }
306
- result.push(value.slice(tokenStartI, i + 1));
307
- tokenStartI = i + 1;
308
- }
309
- }
310
- return result;
311
- }
312
- }
313
- const sentenceDiff = new SentenceDiff();
314
- function diffSentences(oldStr, newStr, options) {
315
- return sentenceDiff.diff(oldStr, newStr, options);
316
- }
317
- class CssDiff extends Diff {
318
- tokenize(value) {
319
- return value.split(/([{}:;,]|\s+)/);
320
- }
321
- }
322
- const cssDiff = new CssDiff();
323
- function diffCss(oldStr, newStr, options) {
324
- return cssDiff.diff(oldStr, newStr, options);
325
- }
326
- class JsonDiff extends Diff {
327
- constructor() {
328
- super(...arguments);
329
- this.tokenize = tokenize;
330
- }
331
- get useLongestToken() {
332
- return true;
333
- }
334
- castInput(value, options) {
335
- const { undefinedReplacement, stringifyReplacer = (k, v) => typeof v === "undefined" ? undefinedReplacement : v } = options;
336
- return typeof value === "string" ? value : JSON.stringify(canonicalize(value, null, null, stringifyReplacer), null, " ");
337
- }
338
- equals(left, right, options) {
339
- return super.equals(left.replace(/,([\r\n])/g, "$1"), right.replace(/,([\r\n])/g, "$1"), options);
340
- }
341
- }
342
- const jsonDiff = new JsonDiff();
343
- function diffJson(oldStr, newStr, options) {
344
- return jsonDiff.diff(oldStr, newStr, options);
345
- }
346
- function canonicalize(obj, stack, replacementStack, replacer, key) {
347
- stack = stack || [];
348
- replacementStack = replacementStack || [];
349
- if (replacer) {
350
- obj = replacer(key === void 0 ? "" : key, obj);
351
- }
352
- let i;
353
- for (i = 0; i < stack.length; i += 1) {
354
- if (stack[i] === obj) {
355
- return replacementStack[i];
356
- }
357
- }
358
- let canonicalizedObj;
359
- if ("[object Array]" === Object.prototype.toString.call(obj)) {
360
- stack.push(obj);
361
- canonicalizedObj = new Array(obj.length);
362
- replacementStack.push(canonicalizedObj);
363
- for (i = 0; i < obj.length; i += 1) {
364
- canonicalizedObj[i] = canonicalize(obj[i], stack, replacementStack, replacer, String(i));
365
- }
366
- stack.pop();
367
- replacementStack.pop();
368
- return canonicalizedObj;
369
- }
370
- if (obj && obj.toJSON) {
371
- obj = obj.toJSON();
372
- }
373
- if (typeof obj === "object" && obj !== null) {
374
- stack.push(obj);
375
- canonicalizedObj = {};
376
- replacementStack.push(canonicalizedObj);
377
- const sortedKeys = [];
378
- let key2;
379
- for (key2 in obj) {
380
- if (Object.prototype.hasOwnProperty.call(obj, key2)) {
381
- sortedKeys.push(key2);
382
- }
383
- }
384
- sortedKeys.sort();
385
- for (i = 0; i < sortedKeys.length; i += 1) {
386
- key2 = sortedKeys[i];
387
- canonicalizedObj[key2] = canonicalize(obj[key2], stack, replacementStack, replacer, key2);
388
- }
389
- stack.pop();
390
- replacementStack.pop();
391
- } else {
392
- canonicalizedObj = obj;
393
- }
394
- return canonicalizedObj;
395
- }
396
- class ArrayDiff extends Diff {
397
- tokenize(value) {
398
- return value.slice();
399
- }
400
- join(value) {
401
- return value;
402
- }
403
- removeEmpty(value) {
404
- return value;
405
- }
406
- }
407
- const arrayDiff = new ArrayDiff();
408
- function diffArrays(oldArr, newArr, options) {
409
- return arrayDiff.diff(oldArr, newArr, options);
410
- }
411
- function unixToWin(patch) {
412
- if (Array.isArray(patch)) {
413
- return patch.map((p) => unixToWin(p));
414
- }
415
- return Object.assign(Object.assign({}, patch), { hunks: patch.hunks.map((hunk) => Object.assign(Object.assign({}, hunk), { lines: hunk.lines.map((line, i) => {
416
- var _a;
417
- return line.startsWith("\\") || line.endsWith("\r") || ((_a = hunk.lines[i + 1]) === null || _a === void 0 ? void 0 : _a.startsWith("\\")) ? line : line + "\r";
418
- }) })) });
419
- }
420
- function winToUnix(patch) {
421
- if (Array.isArray(patch)) {
422
- return patch.map((p) => winToUnix(p));
423
- }
424
- return Object.assign(Object.assign({}, patch), { hunks: patch.hunks.map((hunk) => Object.assign(Object.assign({}, hunk), { lines: hunk.lines.map((line) => line.endsWith("\r") ? line.substring(0, line.length - 1) : line) })) });
425
- }
426
- function isUnix(patch) {
427
- if (!Array.isArray(patch)) {
428
- patch = [patch];
429
- }
430
- return !patch.some((index) => index.hunks.some((hunk) => hunk.lines.some((line) => !line.startsWith("\\") && line.endsWith("\r"))));
431
- }
432
- function isWin(patch) {
433
- if (!Array.isArray(patch)) {
434
- patch = [patch];
435
- }
436
- return patch.some((index) => index.hunks.some((hunk) => hunk.lines.some((line) => line.endsWith("\r")))) && patch.every((index) => index.hunks.every((hunk) => hunk.lines.every((line, i) => {
437
- var _a;
438
- return line.startsWith("\\") || line.endsWith("\r") || ((_a = hunk.lines[i + 1]) === null || _a === void 0 ? void 0 : _a.startsWith("\\"));
439
- })));
440
- }
441
- function parsePatch(uniDiff) {
442
- const diffstr = uniDiff.split(/\n/), list = [];
443
- let i = 0;
444
- function parseIndex() {
445
- const index = {};
446
- list.push(index);
447
- while (i < diffstr.length) {
448
- const line = diffstr[i];
449
- if (/^(---|\+\+\+|@@)\s/.test(line)) {
450
- break;
451
- }
452
- const header = /^(?:Index:|diff(?: -r \w+)+)\s+(.+?)\s*$/.exec(line);
453
- if (header) {
454
- index.index = header[1];
455
- }
456
- i++;
457
- }
458
- parseFileHeader(index);
459
- parseFileHeader(index);
460
- index.hunks = [];
461
- while (i < diffstr.length) {
462
- const line = diffstr[i];
463
- if (/^(Index:\s|diff\s|---\s|\+\+\+\s|===================================================================)/.test(line)) {
464
- break;
465
- } else if (/^@@/.test(line)) {
466
- index.hunks.push(parseHunk());
467
- } else if (line) {
468
- throw new Error("Unknown line " + (i + 1) + " " + JSON.stringify(line));
469
- } else {
470
- i++;
471
- }
472
- }
473
- }
474
- function parseFileHeader(index) {
475
- const fileHeader = /^(---|\+\+\+)\s+(.*)\r?$/.exec(diffstr[i]);
476
- if (fileHeader) {
477
- const data = fileHeader[2].split(" ", 2), header = (data[1] || "").trim();
478
- let fileName = data[0].replace(/\\\\/g, "\\");
479
- if (/^".*"$/.test(fileName)) {
480
- fileName = fileName.substr(1, fileName.length - 2);
481
- }
482
- if (fileHeader[1] === "---") {
483
- index.oldFileName = fileName;
484
- index.oldHeader = header;
485
- } else {
486
- index.newFileName = fileName;
487
- index.newHeader = header;
488
- }
489
- i++;
490
- }
491
- }
492
- function parseHunk() {
493
- var _a;
494
- const chunkHeaderIndex = i, chunkHeaderLine = diffstr[i++], chunkHeader = chunkHeaderLine.split(/@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
495
- const hunk = {
496
- oldStart: +chunkHeader[1],
497
- oldLines: typeof chunkHeader[2] === "undefined" ? 1 : +chunkHeader[2],
498
- newStart: +chunkHeader[3],
499
- newLines: typeof chunkHeader[4] === "undefined" ? 1 : +chunkHeader[4],
500
- lines: []
501
- };
502
- if (hunk.oldLines === 0) {
503
- hunk.oldStart += 1;
504
- }
505
- if (hunk.newLines === 0) {
506
- hunk.newStart += 1;
507
- }
508
- let addCount = 0, removeCount = 0;
509
- for (; i < diffstr.length && (removeCount < hunk.oldLines || addCount < hunk.newLines || ((_a = diffstr[i]) === null || _a === void 0 ? void 0 : _a.startsWith("\\"))); i++) {
510
- const operation = diffstr[i].length == 0 && i != diffstr.length - 1 ? " " : diffstr[i][0];
511
- if (operation === "+" || operation === "-" || operation === " " || operation === "\\") {
512
- hunk.lines.push(diffstr[i]);
513
- if (operation === "+") {
514
- addCount++;
515
- } else if (operation === "-") {
516
- removeCount++;
517
- } else if (operation === " ") {
518
- addCount++;
519
- removeCount++;
520
- }
521
- } else {
522
- throw new Error(`Hunk at line ${chunkHeaderIndex + 1} contained invalid line ${diffstr[i]}`);
523
- }
524
- }
525
- if (!addCount && hunk.newLines === 1) {
526
- hunk.newLines = 0;
527
- }
528
- if (!removeCount && hunk.oldLines === 1) {
529
- hunk.oldLines = 0;
530
- }
531
- if (addCount !== hunk.newLines) {
532
- throw new Error("Added line count did not match for hunk at line " + (chunkHeaderIndex + 1));
533
- }
534
- if (removeCount !== hunk.oldLines) {
535
- throw new Error("Removed line count did not match for hunk at line " + (chunkHeaderIndex + 1));
536
- }
537
- return hunk;
538
- }
539
- while (i < diffstr.length) {
540
- parseIndex();
541
- }
542
- return list;
543
- }
544
- function distanceIterator(start, minLine, maxLine) {
545
- let wantForward = true, backwardExhausted = false, forwardExhausted = false, localOffset = 1;
546
- return function iterator() {
547
- if (wantForward && !forwardExhausted) {
548
- if (backwardExhausted) {
549
- localOffset++;
550
- } else {
551
- wantForward = false;
552
- }
553
- if (start + localOffset <= maxLine) {
554
- return start + localOffset;
555
- }
556
- forwardExhausted = true;
557
- }
558
- if (!backwardExhausted) {
559
- if (!forwardExhausted) {
560
- wantForward = true;
561
- }
562
- if (minLine <= start - localOffset) {
563
- return start - localOffset++;
564
- }
565
- backwardExhausted = true;
566
- return iterator();
567
- }
568
- return void 0;
569
- };
570
- }
571
- function applyPatch(source, patch, options = {}) {
572
- let patches;
573
- if (typeof patch === "string") {
574
- patches = parsePatch(patch);
575
- } else if (Array.isArray(patch)) {
576
- patches = patch;
577
- } else {
578
- patches = [patch];
579
- }
580
- if (patches.length > 1) {
581
- throw new Error("applyPatch only works with a single input.");
582
- }
583
- return applyStructuredPatch(source, patches[0], options);
584
- }
585
- function applyStructuredPatch(source, patch, options = {}) {
586
- if (options.autoConvertLineEndings || options.autoConvertLineEndings == null) {
587
- if (hasOnlyWinLineEndings(source) && isUnix(patch)) {
588
- patch = unixToWin(patch);
589
- } else if (hasOnlyUnixLineEndings(source) && isWin(patch)) {
590
- patch = winToUnix(patch);
591
- }
592
- }
593
- const lines = source.split("\n"), hunks = patch.hunks, compareLine = options.compareLine || ((lineNumber, line, operation, patchContent) => line === patchContent), fuzzFactor = options.fuzzFactor || 0;
594
- let minLine = 0;
595
- if (fuzzFactor < 0 || !Number.isInteger(fuzzFactor)) {
596
- throw new Error("fuzzFactor must be a non-negative integer");
597
- }
598
- if (!hunks.length) {
599
- return source;
600
- }
601
- let prevLine = "", removeEOFNL = false, addEOFNL = false;
602
- for (let i = 0; i < hunks[hunks.length - 1].lines.length; i++) {
603
- const line = hunks[hunks.length - 1].lines[i];
604
- if (line[0] == "\\") {
605
- if (prevLine[0] == "+") {
606
- removeEOFNL = true;
607
- } else if (prevLine[0] == "-") {
608
- addEOFNL = true;
609
- }
610
- }
611
- prevLine = line;
612
- }
613
- if (removeEOFNL) {
614
- if (addEOFNL) {
615
- if (!fuzzFactor && lines[lines.length - 1] == "") {
616
- return false;
617
- }
618
- } else if (lines[lines.length - 1] == "") {
619
- lines.pop();
620
- } else if (!fuzzFactor) {
621
- return false;
622
- }
623
- } else if (addEOFNL) {
624
- if (lines[lines.length - 1] != "") {
625
- lines.push("");
626
- } else if (!fuzzFactor) {
627
- return false;
628
- }
629
- }
630
- function applyHunk(hunkLines, toPos, maxErrors, hunkLinesI = 0, lastContextLineMatched = true, patchedLines = [], patchedLinesLength = 0) {
631
- let nConsecutiveOldContextLines = 0;
632
- let nextContextLineMustMatch = false;
633
- for (; hunkLinesI < hunkLines.length; hunkLinesI++) {
634
- const hunkLine = hunkLines[hunkLinesI], operation = hunkLine.length > 0 ? hunkLine[0] : " ", content = hunkLine.length > 0 ? hunkLine.substr(1) : hunkLine;
635
- if (operation === "-") {
636
- if (compareLine(toPos + 1, lines[toPos], operation, content)) {
637
- toPos++;
638
- nConsecutiveOldContextLines = 0;
639
- } else {
640
- if (!maxErrors || lines[toPos] == null) {
641
- return null;
642
- }
643
- patchedLines[patchedLinesLength] = lines[toPos];
644
- return applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1);
645
- }
646
- }
647
- if (operation === "+") {
648
- if (!lastContextLineMatched) {
649
- return null;
650
- }
651
- patchedLines[patchedLinesLength] = content;
652
- patchedLinesLength++;
653
- nConsecutiveOldContextLines = 0;
654
- nextContextLineMustMatch = true;
655
- }
656
- if (operation === " ") {
657
- nConsecutiveOldContextLines++;
658
- patchedLines[patchedLinesLength] = lines[toPos];
659
- if (compareLine(toPos + 1, lines[toPos], operation, content)) {
660
- patchedLinesLength++;
661
- lastContextLineMatched = true;
662
- nextContextLineMustMatch = false;
663
- toPos++;
664
- } else {
665
- if (nextContextLineMustMatch || !maxErrors) {
666
- return null;
667
- }
668
- return lines[toPos] && (applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength + 1) || applyHunk(hunkLines, toPos + 1, maxErrors - 1, hunkLinesI, false, patchedLines, patchedLinesLength + 1)) || applyHunk(hunkLines, toPos, maxErrors - 1, hunkLinesI + 1, false, patchedLines, patchedLinesLength);
669
- }
670
- }
671
- }
672
- patchedLinesLength -= nConsecutiveOldContextLines;
673
- toPos -= nConsecutiveOldContextLines;
674
- patchedLines.length = patchedLinesLength;
675
- return {
676
- patchedLines,
677
- oldLineLastI: toPos - 1
678
- };
679
- }
680
- const resultLines = [];
681
- let prevHunkOffset = 0;
682
- for (let i = 0; i < hunks.length; i++) {
683
- const hunk = hunks[i];
684
- let hunkResult;
685
- const maxLine = lines.length - hunk.oldLines + fuzzFactor;
686
- let toPos;
687
- for (let maxErrors = 0; maxErrors <= fuzzFactor; maxErrors++) {
688
- toPos = hunk.oldStart + prevHunkOffset - 1;
689
- const iterator = distanceIterator(toPos, minLine, maxLine);
690
- for (; toPos !== void 0; toPos = iterator()) {
691
- hunkResult = applyHunk(hunk.lines, toPos, maxErrors);
692
- if (hunkResult) {
693
- break;
694
- }
695
- }
696
- if (hunkResult) {
697
- break;
698
- }
699
- }
700
- if (!hunkResult) {
701
- return false;
702
- }
703
- for (let i2 = minLine; i2 < toPos; i2++) {
704
- resultLines.push(lines[i2]);
705
- }
706
- for (let i2 = 0; i2 < hunkResult.patchedLines.length; i2++) {
707
- const line = hunkResult.patchedLines[i2];
708
- resultLines.push(line);
709
- }
710
- minLine = hunkResult.oldLineLastI + 1;
711
- prevHunkOffset = toPos + 1 - hunk.oldStart;
712
- }
713
- for (let i = minLine; i < lines.length; i++) {
714
- resultLines.push(lines[i]);
715
- }
716
- return resultLines.join("\n");
717
- }
718
- function applyPatches(uniDiff, options) {
719
- const spDiff = typeof uniDiff === "string" ? parsePatch(uniDiff) : uniDiff;
720
- let currentIndex = 0;
721
- function processIndex() {
722
- const index = spDiff[currentIndex++];
723
- if (!index) {
724
- return options.complete();
725
- }
726
- options.loadFile(index, function(err, data) {
727
- if (err) {
728
- return options.complete(err);
729
- }
730
- const updatedContent = applyPatch(data, index, options);
731
- options.patched(index, updatedContent, function(err2) {
732
- if (err2) {
733
- return options.complete(err2);
734
- }
735
- processIndex();
736
- });
737
- });
738
- }
739
- processIndex();
740
- }
741
- function reversePatch(structuredPatch) {
742
- if (Array.isArray(structuredPatch)) {
743
- return structuredPatch.map((patch) => reversePatch(patch)).reverse();
744
- }
745
- return Object.assign(Object.assign({}, structuredPatch), { oldFileName: structuredPatch.newFileName, oldHeader: structuredPatch.newHeader, newFileName: structuredPatch.oldFileName, newHeader: structuredPatch.oldHeader, hunks: structuredPatch.hunks.map((hunk) => {
746
- return {
747
- oldLines: hunk.newLines,
748
- oldStart: hunk.newStart,
749
- newLines: hunk.oldLines,
750
- newStart: hunk.oldStart,
751
- lines: hunk.lines.map((l2) => {
752
- if (l2.startsWith("-")) {
753
- return `+${l2.slice(1)}`;
754
- }
755
- if (l2.startsWith("+")) {
756
- return `-${l2.slice(1)}`;
757
- }
758
- return l2;
759
- })
760
- };
761
- }) });
762
- }
763
- function convertChangesToDMP(changes) {
764
- const ret = [];
765
- let change, operation;
766
- for (let i = 0; i < changes.length; i++) {
767
- change = changes[i];
768
- if (change.added) {
769
- operation = 1;
770
- } else if (change.removed) {
771
- operation = -1;
772
- } else {
773
- operation = 0;
774
- }
775
- ret.push([operation, change.value]);
776
- }
777
- return ret;
778
- }
779
- function convertChangesToXML(changes) {
780
- const ret = [];
781
- for (let i = 0; i < changes.length; i++) {
782
- const change = changes[i];
783
- if (change.added) {
784
- ret.push("<ins>");
785
- } else if (change.removed) {
786
- ret.push("<del>");
787
- }
788
- ret.push(escapeHTML(change.value));
789
- if (change.added) {
790
- ret.push("</ins>");
791
- } else if (change.removed) {
792
- ret.push("</del>");
793
- }
794
- }
795
- return ret.join("");
796
- }
797
- function escapeHTML(s2) {
798
- let n = s2;
799
- n = n.replace(/&/g, "&amp;");
800
- n = n.replace(/</g, "&lt;");
801
- n = n.replace(/>/g, "&gt;");
802
- n = n.replace(/"/g, "&quot;");
803
- return n;
804
- }
805
- export {
806
- Diff,
807
- applyPatch,
808
- applyPatches,
809
- arrayDiff,
810
- canonicalize,
811
- characterDiff,
812
- convertChangesToDMP,
813
- convertChangesToXML,
814
- b as createPatch,
815
- c as createTwoFilesPatch,
816
- cssDiff,
817
- diffArrays,
818
- diffChars,
819
- diffCss,
820
- diffJson,
821
- d as diffLines,
822
- diffSentences,
823
- a as diffTrimmedLines,
824
- diffWords,
825
- diffWordsWithSpace,
826
- f as formatPatch,
827
- jsonDiff,
828
- l as lineDiff,
829
- parsePatch,
830
- reversePatch,
831
- sentenceDiff,
832
- s as structuredPatch,
833
- wordDiff,
834
- wordsWithSpaceDiff
835
- };