@ai-workbench/connect 0.0.0-stage → 0.7.6

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 (131) hide show
  1. package/NOTICE.md +18 -0
  2. package/README.md +26 -2
  3. package/client/index.cjs +124 -0
  4. package/cordis.patch.yml +3 -0
  5. package/helper/auth-status.cjs +18 -0
  6. package/helper/claude-callback.cjs +29 -0
  7. package/helper/main.cjs +19 -0
  8. package/helper/projects.cjs +57 -0
  9. package/helper/remote-command.cjs +15 -0
  10. package/helper/remote-login.cjs +25 -0
  11. package/helper/window.cjs +52 -0
  12. package/lib/api.d.ts +196 -0
  13. package/lib/api.js +1 -0
  14. package/lib/archive.d.ts +1 -0
  15. package/lib/archive.js +63 -0
  16. package/lib/auth-status.d.ts +3 -0
  17. package/lib/auth-status.js +18 -0
  18. package/lib/cache.d.ts +21 -0
  19. package/lib/cache.js +156 -0
  20. package/lib/component-download.d.ts +32 -0
  21. package/lib/component-download.js +76 -0
  22. package/lib/component-source.d.ts +52 -0
  23. package/lib/component-source.js +146 -0
  24. package/lib/components.d.ts +10 -0
  25. package/lib/components.js +154 -0
  26. package/lib/connect.d.ts +10 -0
  27. package/lib/connect.js +246 -0
  28. package/lib/cordis.d.ts +15 -0
  29. package/lib/cordis.js +1 -0
  30. package/lib/defaults.d.ts +30 -0
  31. package/lib/defaults.js +4 -0
  32. package/lib/electron.d.ts +8 -0
  33. package/lib/electron.js +83 -0
  34. package/lib/enrollment.d.ts +3 -0
  35. package/lib/enrollment.js +78 -0
  36. package/lib/index.d.ts +42 -0
  37. package/lib/index.js +55 -0
  38. package/lib/integration.d.ts +6 -0
  39. package/lib/integration.js +56 -0
  40. package/lib/login.d.ts +35 -0
  41. package/lib/login.js +81 -0
  42. package/lib/native-import-picker.d.ts +11 -0
  43. package/lib/native-import-picker.js +49 -0
  44. package/lib/needs.d.ts +9 -0
  45. package/lib/needs.js +52 -0
  46. package/lib/network.d.ts +26 -0
  47. package/lib/network.js +226 -0
  48. package/lib/physical-fs.d.ts +3 -0
  49. package/lib/physical-fs.js +7 -0
  50. package/lib/ports.d.ts +43 -0
  51. package/lib/ports.js +1 -0
  52. package/lib/private-files.d.ts +3 -0
  53. package/lib/private-files.js +41 -0
  54. package/lib/processes.d.ts +5 -0
  55. package/lib/processes.js +147 -0
  56. package/lib/profile-activation.d.ts +32 -0
  57. package/lib/profile-activation.js +118 -0
  58. package/lib/profile-enrollment.d.ts +21 -0
  59. package/lib/profile-enrollment.js +275 -0
  60. package/lib/profile-runtime.d.ts +6 -0
  61. package/lib/profile-runtime.js +47 -0
  62. package/lib/profile-settings.d.ts +2 -0
  63. package/lib/profile-settings.js +13 -0
  64. package/lib/projects.d.ts +4 -0
  65. package/lib/projects.js +41 -0
  66. package/lib/provider.d.ts +73 -0
  67. package/lib/provider.js +39 -0
  68. package/lib/remote-login.d.ts +2 -0
  69. package/lib/remote-login.js +9 -0
  70. package/lib/ssh.d.ts +10 -0
  71. package/lib/ssh.js +78 -0
  72. package/lib/startup.d.ts +18 -0
  73. package/lib/startup.js +211 -0
  74. package/lib/workspace-bindings.d.ts +32 -0
  75. package/lib/workspace-bindings.js +328 -0
  76. package/locale/en.json +6 -0
  77. package/locale/zh.json +6 -0
  78. package/node_modules/argparse/LICENSE +254 -0
  79. package/node_modules/argparse/README.md +84 -0
  80. package/node_modules/argparse/argparse.js +3707 -0
  81. package/node_modules/argparse/lib/sub.js +67 -0
  82. package/node_modules/argparse/lib/textwrap.js +440 -0
  83. package/node_modules/argparse/package.json +31 -0
  84. package/node_modules/js-yaml/LICENSE +21 -0
  85. package/node_modules/js-yaml/README.md +233 -0
  86. package/node_modules/js-yaml/bin/js-yaml.js +117 -0
  87. package/node_modules/js-yaml/dist/js-yaml.js +3022 -0
  88. package/node_modules/js-yaml/dist/js-yaml.js.map +1 -0
  89. package/node_modules/js-yaml/dist/js-yaml.min.js +2 -0
  90. package/node_modules/js-yaml/dist/js-yaml.min.js.map +1 -0
  91. package/node_modules/js-yaml/dist/js-yaml.mjs +3082 -0
  92. package/node_modules/js-yaml/dist/js-yaml.mjs.map +1 -0
  93. package/node_modules/js-yaml/index.js +44 -0
  94. package/node_modules/js-yaml/lib/common.js +50 -0
  95. package/node_modules/js-yaml/lib/dumper.js +937 -0
  96. package/node_modules/js-yaml/lib/exception.js +51 -0
  97. package/node_modules/js-yaml/lib/index_vite_proxy.tmp.mjs +37 -0
  98. package/node_modules/js-yaml/lib/loader.js +1803 -0
  99. package/node_modules/js-yaml/lib/schema/core.js +9 -0
  100. package/node_modules/js-yaml/lib/schema/default.js +20 -0
  101. package/node_modules/js-yaml/lib/schema/failsafe.js +14 -0
  102. package/node_modules/js-yaml/lib/schema/json.js +17 -0
  103. package/node_modules/js-yaml/lib/schema.js +109 -0
  104. package/node_modules/js-yaml/lib/snippet.js +96 -0
  105. package/node_modules/js-yaml/lib/type/binary.js +122 -0
  106. package/node_modules/js-yaml/lib/type/bool.js +35 -0
  107. package/node_modules/js-yaml/lib/type/float.js +99 -0
  108. package/node_modules/js-yaml/lib/type/int.js +142 -0
  109. package/node_modules/js-yaml/lib/type/map.js +8 -0
  110. package/node_modules/js-yaml/lib/type/merge.js +12 -0
  111. package/node_modules/js-yaml/lib/type/null.js +35 -0
  112. package/node_modules/js-yaml/lib/type/omap.js +45 -0
  113. package/node_modules/js-yaml/lib/type/pairs.js +50 -0
  114. package/node_modules/js-yaml/lib/type/seq.js +8 -0
  115. package/node_modules/js-yaml/lib/type/set.js +29 -0
  116. package/node_modules/js-yaml/lib/type/str.js +8 -0
  117. package/node_modules/js-yaml/lib/type/timestamp.js +88 -0
  118. package/node_modules/js-yaml/lib/type.js +66 -0
  119. package/node_modules/js-yaml/package.json +83 -0
  120. package/node_modules/pend/LICENSE +23 -0
  121. package/node_modules/pend/README.md +41 -0
  122. package/node_modules/pend/index.js +55 -0
  123. package/node_modules/pend/package.json +18 -0
  124. package/node_modules/pend/test.js +137 -0
  125. package/node_modules/yauzl/LICENSE +21 -0
  126. package/node_modules/yauzl/README.md +1015 -0
  127. package/node_modules/yauzl/crc32.js +45 -0
  128. package/node_modules/yauzl/fd-slicer.js +188 -0
  129. package/node_modules/yauzl/index.js +1096 -0
  130. package/node_modules/yauzl/package.json +40 -0
  131. package/package.json +89 -4
@@ -0,0 +1,937 @@
1
+ 'use strict'
2
+
3
+ const common = require('./common')
4
+ const YAMLException = require('./exception')
5
+ const DEFAULT_SCHEMA = require('./schema/default')
6
+
7
+ const _toString = Object.prototype.toString
8
+ const _hasOwnProperty = Object.prototype.hasOwnProperty
9
+
10
+ const CHAR_BOM = 0xFEFF
11
+ const CHAR_TAB = 0x09 /* Tab */
12
+ const CHAR_LINE_FEED = 0x0A /* LF */
13
+ const CHAR_CARRIAGE_RETURN = 0x0D /* CR */
14
+ const CHAR_SPACE = 0x20 /* Space */
15
+ const CHAR_EXCLAMATION = 0x21 /* ! */
16
+ const CHAR_DOUBLE_QUOTE = 0x22 /* " */
17
+ const CHAR_SHARP = 0x23 /* # */
18
+ const CHAR_PERCENT = 0x25 /* % */
19
+ const CHAR_AMPERSAND = 0x26 /* & */
20
+ const CHAR_SINGLE_QUOTE = 0x27 /* ' */
21
+ const CHAR_ASTERISK = 0x2A /* * */
22
+ const CHAR_COMMA = 0x2C /* , */
23
+ const CHAR_MINUS = 0x2D /* - */
24
+ const CHAR_COLON = 0x3A /* : */
25
+ const CHAR_EQUALS = 0x3D /* = */
26
+ const CHAR_GREATER_THAN = 0x3E /* > */
27
+ const CHAR_QUESTION = 0x3F /* ? */
28
+ const CHAR_COMMERCIAL_AT = 0x40 /* @ */
29
+ const CHAR_LEFT_SQUARE_BRACKET = 0x5B /* [ */
30
+ const CHAR_RIGHT_SQUARE_BRACKET = 0x5D /* ] */
31
+ const CHAR_GRAVE_ACCENT = 0x60 /* ` */
32
+ const CHAR_LEFT_CURLY_BRACKET = 0x7B /* { */
33
+ const CHAR_VERTICAL_LINE = 0x7C /* | */
34
+ const CHAR_RIGHT_CURLY_BRACKET = 0x7D /* } */
35
+
36
+ const ESCAPE_SEQUENCES = {}
37
+
38
+ ESCAPE_SEQUENCES[0x00] = '\\0'
39
+ ESCAPE_SEQUENCES[0x07] = '\\a'
40
+ ESCAPE_SEQUENCES[0x08] = '\\b'
41
+ ESCAPE_SEQUENCES[0x09] = '\\t'
42
+ ESCAPE_SEQUENCES[0x0A] = '\\n'
43
+ ESCAPE_SEQUENCES[0x0B] = '\\v'
44
+ ESCAPE_SEQUENCES[0x0C] = '\\f'
45
+ ESCAPE_SEQUENCES[0x0D] = '\\r'
46
+ ESCAPE_SEQUENCES[0x1B] = '\\e'
47
+ ESCAPE_SEQUENCES[0x22] = '\\"'
48
+ ESCAPE_SEQUENCES[0x5C] = '\\\\'
49
+ ESCAPE_SEQUENCES[0x85] = '\\N'
50
+ ESCAPE_SEQUENCES[0xA0] = '\\_'
51
+ ESCAPE_SEQUENCES[0x2028] = '\\L'
52
+ ESCAPE_SEQUENCES[0x2029] = '\\P'
53
+
54
+ const DEPRECATED_BOOLEANS_SYNTAX = [
55
+ 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON',
56
+ 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'
57
+ ]
58
+
59
+ const DEPRECATED_BASE60_SYNTAX = /^[-+]?[0-9_]+(?::[0-9_]+)+(?:\.[0-9_]*)?$/
60
+
61
+ function compileStyleMap (schema, map) {
62
+ if (map === null) return {}
63
+
64
+ const result = {}
65
+ const keys = Object.keys(map)
66
+
67
+ for (let index = 0, length = keys.length; index < length; index += 1) {
68
+ let tag = keys[index]
69
+ let style = String(map[tag])
70
+
71
+ if (tag.slice(0, 2) === '!!') {
72
+ tag = 'tag:yaml.org,2002:' + tag.slice(2)
73
+ }
74
+ const type = schema.compiledTypeMap['fallback'][tag]
75
+
76
+ if (type && _hasOwnProperty.call(type.styleAliases, style)) {
77
+ style = type.styleAliases[style]
78
+ }
79
+
80
+ result[tag] = style
81
+ }
82
+
83
+ return result
84
+ }
85
+
86
+ function encodeHex (character) {
87
+ let handle
88
+ let length
89
+
90
+ const string = character.toString(16).toUpperCase()
91
+
92
+ if (character <= 0xFF) {
93
+ handle = 'x'
94
+ length = 2
95
+ } else if (character <= 0xFFFF) {
96
+ handle = 'u'
97
+ length = 4
98
+ } else if (character <= 0xFFFFFFFF) {
99
+ handle = 'U'
100
+ length = 8
101
+ } else {
102
+ throw new YAMLException('code point within a string may not be greater than 0xFFFFFFFF')
103
+ }
104
+
105
+ return '\\' + handle + common.repeat('0', length - string.length) + string
106
+ }
107
+
108
+ const QUOTING_TYPE_SINGLE = 1
109
+ const QUOTING_TYPE_DOUBLE = 2
110
+
111
+ function State (options) {
112
+ this.schema = options['schema'] || DEFAULT_SCHEMA
113
+ this.indent = Math.max(1, (options['indent'] || 2))
114
+ this.noArrayIndent = options['noArrayIndent'] || false
115
+ this.skipInvalid = options['skipInvalid'] || false
116
+ this.flowLevel = (common.isNothing(options['flowLevel']) ? -1 : options['flowLevel'])
117
+ this.styleMap = compileStyleMap(this.schema, options['styles'] || null)
118
+ this.sortKeys = options['sortKeys'] || false
119
+ this.lineWidth = options['lineWidth'] || 80
120
+ this.noRefs = options['noRefs'] || false
121
+ this.noCompatMode = options['noCompatMode'] || false
122
+ this.condenseFlow = options['condenseFlow'] || false
123
+ this.quotingType = options['quotingType'] === '"' ? QUOTING_TYPE_DOUBLE : QUOTING_TYPE_SINGLE
124
+ this.forceQuotes = options['forceQuotes'] || false
125
+ this.replacer = typeof options['replacer'] === 'function' ? options['replacer'] : null
126
+
127
+ this.implicitTypes = this.schema.compiledImplicit
128
+ this.explicitTypes = this.schema.compiledExplicit
129
+
130
+ this.tag = null
131
+ this.result = ''
132
+
133
+ this.duplicates = []
134
+ this.usedDuplicates = null
135
+ }
136
+
137
+ // Indents every line in a string. Empty lines (\n only) are not indented.
138
+ function indentString (string, spaces) {
139
+ const ind = common.repeat(' ', spaces)
140
+ let position = 0
141
+ let result = ''
142
+ const length = string.length
143
+
144
+ while (position < length) {
145
+ let line
146
+ const next = string.indexOf('\n', position)
147
+ if (next === -1) {
148
+ line = string.slice(position)
149
+ position = length
150
+ } else {
151
+ line = string.slice(position, next + 1)
152
+ position = next + 1
153
+ }
154
+
155
+ if (line.length && line !== '\n') result += ind
156
+
157
+ result += line
158
+ }
159
+
160
+ return result
161
+ }
162
+
163
+ function generateNextLine (state, level) {
164
+ return '\n' + common.repeat(' ', state.indent * level)
165
+ }
166
+
167
+ function testImplicitResolving (state, str) {
168
+ for (let index = 0, length = state.implicitTypes.length; index < length; index += 1) {
169
+ const type = state.implicitTypes[index]
170
+
171
+ if (type.resolve(str)) {
172
+ return true
173
+ }
174
+ }
175
+
176
+ return false
177
+ }
178
+
179
+ // [33] s-white ::= s-space | s-tab
180
+ function isWhitespace (c) {
181
+ return c === CHAR_SPACE || c === CHAR_TAB
182
+ }
183
+
184
+ // Returns true if the character can be printed without escaping.
185
+ // From YAML 1.2: "any allowed characters known to be non-printable
186
+ // should also be escaped. [However,] This isn’t mandatory"
187
+ // Derived from nb-char - \t - #x85 - #xA0 - #x2028 - #x2029.
188
+ function isPrintable (c) {
189
+ return (c >= 0x00020 && c <= 0x00007E) ||
190
+ ((c >= 0x000A1 && c <= 0x00D7FF) && c !== 0x2028 && c !== 0x2029) ||
191
+ ((c >= 0x0E000 && c <= 0x00FFFD) && c !== CHAR_BOM) ||
192
+ (c >= 0x10000 && c <= 0x10FFFF)
193
+ }
194
+
195
+ // [34] ns-char ::= nb-char - s-white
196
+ // [27] nb-char ::= c-printable - b-char - c-byte-order-mark
197
+ // [26] b-char ::= b-line-feed | b-carriage-return
198
+ // Including s-white (for some reason, examples doesn't match specs in this aspect)
199
+ // ns-char ::= c-printable - b-line-feed - b-carriage-return - c-byte-order-mark
200
+ function isNsCharOrWhitespace (c) {
201
+ return isPrintable(c) &&
202
+ c !== CHAR_BOM &&
203
+ // - b-char
204
+ c !== CHAR_CARRIAGE_RETURN &&
205
+ c !== CHAR_LINE_FEED
206
+ }
207
+
208
+ // [127] ns-plain-safe(c) ::= c = flow-out ⇒ ns-plain-safe-out
209
+ // c = flow-in ⇒ ns-plain-safe-in
210
+ // c = block-key ⇒ ns-plain-safe-out
211
+ // c = flow-key ⇒ ns-plain-safe-in
212
+ // [128] ns-plain-safe-out ::= ns-char
213
+ // [129] ns-plain-safe-in ::= ns-char - c-flow-indicator
214
+ // [130] ns-plain-char(c) ::= ( ns-plain-safe(c) - “:” - “#” )
215
+ // | ( /* An ns-char preceding */ “#” )
216
+ // | ( “:” /* Followed by an ns-plain-safe(c) */ )
217
+ function isPlainSafe (c, prev, inblock) {
218
+ const cIsNsCharOrWhitespace = isNsCharOrWhitespace(c)
219
+ const cIsNsChar = cIsNsCharOrWhitespace && !isWhitespace(c)
220
+ return (
221
+ (
222
+ // ns-plain-safe
223
+ inblock // c = flow-in
224
+ ? cIsNsCharOrWhitespace
225
+ : cIsNsCharOrWhitespace &&
226
+ // - c-flow-indicator
227
+ c !== CHAR_COMMA &&
228
+ c !== CHAR_LEFT_SQUARE_BRACKET &&
229
+ c !== CHAR_RIGHT_SQUARE_BRACKET &&
230
+ c !== CHAR_LEFT_CURLY_BRACKET &&
231
+ c !== CHAR_RIGHT_CURLY_BRACKET
232
+ ) &&
233
+ // ns-plain-char
234
+ c !== CHAR_SHARP && // false on '#'
235
+ !(prev === CHAR_COLON && !cIsNsChar)
236
+ ) || // false on ': '
237
+ (isNsCharOrWhitespace(prev) && !isWhitespace(prev) && c === CHAR_SHARP) || // change to true on '[^ ]#'
238
+ (prev === CHAR_COLON && cIsNsChar) // change to true on ':[^ ]'
239
+ }
240
+
241
+ // Simplified test for values allowed as the first character in plain style.
242
+ function isPlainSafeFirst (c) {
243
+ // Uses a subset of ns-char - c-indicator
244
+ // where ns-char = nb-char - s-white.
245
+ // No support of ( ( “?” | “:” | “-” ) /* Followed by an ns-plain-safe(c)) */ ) part
246
+ return isPrintable(c) &&
247
+ c !== CHAR_BOM &&
248
+ !isWhitespace(c) && // - s-white
249
+ // - (c-indicator ::=
250
+ // “-” | “?” | “:” | “,” | “[” | “]” | “{” | “}”
251
+ c !== CHAR_MINUS &&
252
+ c !== CHAR_QUESTION &&
253
+ c !== CHAR_COLON &&
254
+ c !== CHAR_COMMA &&
255
+ c !== CHAR_LEFT_SQUARE_BRACKET &&
256
+ c !== CHAR_RIGHT_SQUARE_BRACKET &&
257
+ c !== CHAR_LEFT_CURLY_BRACKET &&
258
+ c !== CHAR_RIGHT_CURLY_BRACKET &&
259
+ // | “#” | “&” | “*” | “!” | “|” | “=” | “>” | “'” | “"”
260
+ c !== CHAR_SHARP &&
261
+ c !== CHAR_AMPERSAND &&
262
+ c !== CHAR_ASTERISK &&
263
+ c !== CHAR_EXCLAMATION &&
264
+ c !== CHAR_VERTICAL_LINE &&
265
+ c !== CHAR_EQUALS &&
266
+ c !== CHAR_GREATER_THAN &&
267
+ c !== CHAR_SINGLE_QUOTE &&
268
+ c !== CHAR_DOUBLE_QUOTE &&
269
+ // | “%” | “@” | “`”)
270
+ c !== CHAR_PERCENT &&
271
+ c !== CHAR_COMMERCIAL_AT &&
272
+ c !== CHAR_GRAVE_ACCENT
273
+ }
274
+
275
+ // Simplified test for values allowed as the last character in plain style.
276
+ function isPlainSafeLast (c) {
277
+ // just not whitespace or colon, it will be checked to be plain character later
278
+ return !isWhitespace(c) && c !== CHAR_COLON
279
+ }
280
+
281
+ // Same as 'string'.codePointAt(pos), but works in older browsers.
282
+ function codePointAt (string, pos) {
283
+ const first = string.charCodeAt(pos)
284
+ let second
285
+
286
+ if (first >= 0xD800 && first <= 0xDBFF && pos + 1 < string.length) {
287
+ second = string.charCodeAt(pos + 1)
288
+ if (second >= 0xDC00 && second <= 0xDFFF) {
289
+ // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
290
+ return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000
291
+ }
292
+ }
293
+ return first
294
+ }
295
+
296
+ // Determines whether block indentation indicator is required.
297
+ function needIndentIndicator (string) {
298
+ const leadingSpaceRe = /^\n* /
299
+ return leadingSpaceRe.test(string)
300
+ }
301
+
302
+ const STYLE_PLAIN = 1
303
+ const STYLE_SINGLE = 2
304
+ const STYLE_LITERAL = 3
305
+ const STYLE_FOLDED = 4
306
+ const STYLE_DOUBLE = 5
307
+
308
+ // Determines which scalar styles are possible and returns the preferred style.
309
+ // lineWidth = -1 => no limit.
310
+ // Pre-conditions: str.length > 0.
311
+ // Post-conditions:
312
+ // STYLE_PLAIN or STYLE_SINGLE => no \n are in the string.
313
+ // STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1).
314
+ // STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1).
315
+ function chooseScalarStyle (string, singleLineOnly, indentPerLevel, lineWidth,
316
+ testAmbiguousType, quotingType, forceQuotes, inblock) {
317
+ let i
318
+ let char = 0
319
+ let prevChar = null
320
+ let hasLineBreak = false
321
+ let hasFoldableLine = false // only checked if shouldTrackWidth
322
+ const shouldTrackWidth = lineWidth !== -1
323
+ let previousLineBreak = -1 // count the first line correctly
324
+ let plain = isPlainSafeFirst(codePointAt(string, 0)) &&
325
+ isPlainSafeLast(codePointAt(string, string.length - 1))
326
+
327
+ if (singleLineOnly || forceQuotes) {
328
+ // Case: no block styles.
329
+ // Check for disallowed characters to rule out plain and single.
330
+ for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
331
+ char = codePointAt(string, i)
332
+ if (!isPrintable(char)) {
333
+ return STYLE_DOUBLE
334
+ }
335
+ plain = plain && isPlainSafe(char, prevChar, inblock)
336
+ prevChar = char
337
+ }
338
+ } else {
339
+ // Case: block styles permitted.
340
+ for (i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
341
+ char = codePointAt(string, i)
342
+ if (char === CHAR_LINE_FEED) {
343
+ hasLineBreak = true
344
+ // Check if any line can be folded.
345
+ if (shouldTrackWidth) {
346
+ hasFoldableLine = hasFoldableLine ||
347
+ // Foldable line = too long, and not more-indented.
348
+ (i - previousLineBreak - 1 > lineWidth &&
349
+ string[previousLineBreak + 1] !== ' ')
350
+ previousLineBreak = i
351
+ }
352
+ } else if (!isPrintable(char)) {
353
+ return STYLE_DOUBLE
354
+ }
355
+ plain = plain && isPlainSafe(char, prevChar, inblock)
356
+ prevChar = char
357
+ }
358
+ // in case the end is missing a \n
359
+ hasFoldableLine = hasFoldableLine || (shouldTrackWidth &&
360
+ (i - previousLineBreak - 1 > lineWidth &&
361
+ string[previousLineBreak + 1] !== ' '))
362
+ }
363
+ // Although every style can represent \n without escaping, prefer block styles
364
+ // for multiline, since they're more readable and they don't add empty lines.
365
+ // Also prefer folding a super-long line.
366
+ if (!hasLineBreak && !hasFoldableLine) {
367
+ // Strings interpretable as another type have to be quoted;
368
+ // e.g. the string 'true' vs. the boolean true.
369
+ if (plain && !forceQuotes && !testAmbiguousType(string)) {
370
+ return STYLE_PLAIN
371
+ }
372
+ return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE
373
+ }
374
+ // Edge case: block indentation indicator can only have one digit.
375
+ if (indentPerLevel > 9 && needIndentIndicator(string)) {
376
+ return STYLE_DOUBLE
377
+ }
378
+ // At this point we know block styles are valid.
379
+ // Prefer literal style unless we want to fold.
380
+ if (!forceQuotes) {
381
+ return hasFoldableLine ? STYLE_FOLDED : STYLE_LITERAL
382
+ }
383
+ return quotingType === QUOTING_TYPE_DOUBLE ? STYLE_DOUBLE : STYLE_SINGLE
384
+ }
385
+
386
+ // Note: line breaking/folding is implemented for only the folded style.
387
+ // NB. We drop the last trailing newline (if any) of a returned block scalar
388
+ // since the dumper adds its own newline. This always works:
389
+ // • No ending newline => unaffected; already using strip "-" chomping.
390
+ // • Ending newline => removed then restored.
391
+ // Importantly, this keeps the "+" chomp indicator from gaining an extra line.
392
+ function writeScalar (state, string, level, iskey, inblock) {
393
+ state.dump = (function () {
394
+ if (string.length === 0) {
395
+ return state.quotingType === QUOTING_TYPE_DOUBLE ? '""' : "''"
396
+ }
397
+ if (!state.noCompatMode) {
398
+ if (DEPRECATED_BOOLEANS_SYNTAX.indexOf(string) !== -1 || DEPRECATED_BASE60_SYNTAX.test(string)) {
399
+ return state.quotingType === QUOTING_TYPE_DOUBLE ? ('"' + string + '"') : ("'" + string + "'")
400
+ }
401
+ }
402
+
403
+ const indent = state.indent * Math.max(1, level) // no 0-indent scalars
404
+ // As indentation gets deeper, let the width decrease monotonically
405
+ // to the lower bound min(state.lineWidth, 40).
406
+ // Note that this implies
407
+ // state.lineWidth ≤ 40 + state.indent: width is fixed at the lower bound.
408
+ // state.lineWidth > 40 + state.indent: width decreases until the lower bound.
409
+ // This behaves better than a constant minimum width which disallows narrower options,
410
+ // or an indent threshold which causes the width to suddenly increase.
411
+ const lineWidth = (state.lineWidth === -1)
412
+ ? -1
413
+ : Math.max(Math.min(state.lineWidth, 40), state.lineWidth - indent)
414
+
415
+ // Without knowing if keys are implicit/explicit, assume implicit for safety.
416
+ const singleLineOnly = iskey ||
417
+ // No block styles in flow mode.
418
+ (state.flowLevel > -1 && level >= state.flowLevel)
419
+ function testAmbiguity (string) {
420
+ return testImplicitResolving(state, string)
421
+ }
422
+
423
+ switch (chooseScalarStyle(string, singleLineOnly, state.indent, lineWidth,
424
+ testAmbiguity, state.quotingType, state.forceQuotes && !iskey, inblock)) {
425
+ case STYLE_PLAIN:
426
+ return string
427
+ case STYLE_SINGLE:
428
+ return "'" + string.replace(/'/g, "''") + "'"
429
+ case STYLE_LITERAL:
430
+ return '|' + blockHeader(string, state.indent) +
431
+ dropEndingNewline(indentString(string, indent))
432
+ case STYLE_FOLDED:
433
+ return '>' + blockHeader(string, state.indent) +
434
+ dropEndingNewline(indentString(foldString(string, lineWidth), indent))
435
+ case STYLE_DOUBLE:
436
+ return '"' + escapeString(string, lineWidth) + '"'
437
+ default:
438
+ throw new YAMLException('impossible error: invalid scalar style')
439
+ }
440
+ }())
441
+ }
442
+
443
+ // Pre-conditions: string is valid for a block scalar, 1 <= indentPerLevel <= 9.
444
+ function blockHeader (string, indentPerLevel) {
445
+ const indentIndicator = needIndentIndicator(string) ? String(indentPerLevel) : ''
446
+
447
+ // note the special case: the string '\n' counts as a "trailing" empty line.
448
+ const clip = string[string.length - 1] === '\n'
449
+ const keep = clip && (string[string.length - 2] === '\n' || string === '\n')
450
+ const chomp = keep ? '+' : (clip ? '' : '-')
451
+
452
+ return indentIndicator + chomp + '\n'
453
+ }
454
+
455
+ // (See the note for writeScalar.)
456
+ function dropEndingNewline (string) {
457
+ return string[string.length - 1] === '\n' ? string.slice(0, -1) : string
458
+ }
459
+
460
+ // Note: a long line without a suitable break point will exceed the width limit.
461
+ // Pre-conditions: every char in str isPrintable, str.length > 0, width > 0.
462
+ function foldString (string, width) {
463
+ // In folded style, $k$ consecutive newlines output as $k+1$ newlines—
464
+ // unless they're before or after a more-indented line, or at the very
465
+ // beginning or end, in which case $k$ maps to $k$.
466
+ // Therefore, parse each chunk as newline(s) followed by a content line.
467
+ const lineRe = /(\n+)([^\n]*)/g
468
+
469
+ // first line (possibly an empty line)
470
+ let result = (function () {
471
+ let nextLF = string.indexOf('\n')
472
+ nextLF = nextLF !== -1 ? nextLF : string.length
473
+ lineRe.lastIndex = nextLF
474
+ return foldLine(string.slice(0, nextLF), width)
475
+ }())
476
+ // If we haven't reached the first content line yet, don't add an extra \n.
477
+ let prevMoreIndented = string[0] === '\n' || string[0] === ' '
478
+ let moreIndented
479
+
480
+ // rest of the lines
481
+ let match
482
+ while ((match = lineRe.exec(string))) {
483
+ const prefix = match[1]
484
+ const line = match[2]
485
+
486
+ moreIndented = (line[0] === ' ')
487
+ result += prefix +
488
+ ((!prevMoreIndented && !moreIndented && line !== '') ? '\n' : '') +
489
+ foldLine(line, width)
490
+ prevMoreIndented = moreIndented
491
+ }
492
+
493
+ return result
494
+ }
495
+
496
+ // Greedy line breaking.
497
+ // Picks the longest line under the limit each time,
498
+ // otherwise settles for the shortest line over the limit.
499
+ // NB. More-indented lines *cannot* be folded, as that would add an extra \n.
500
+ function foldLine (line, width) {
501
+ if (line === '' || line[0] === ' ') return line
502
+
503
+ // Since a more-indented line adds a \n, breaks can't be followed by a space.
504
+ const breakRe = / [^ ]/g // note: the match index will always be <= length-2.
505
+ let match
506
+ // start is an inclusive index. end, curr, and next are exclusive.
507
+ let start = 0
508
+ let end
509
+ let curr = 0
510
+ let next = 0
511
+ let result = ''
512
+
513
+ // Invariants: 0 <= start <= length-1.
514
+ // 0 <= curr <= next <= max(0, length-2). curr - start <= width.
515
+ // Inside the loop:
516
+ // A match implies length >= 2, so curr and next are <= length-2.
517
+ while ((match = breakRe.exec(line))) {
518
+ next = match.index
519
+ // maintain invariant: curr - start <= width
520
+ if (next - start > width) {
521
+ end = (curr > start) ? curr : next // derive end <= length-2
522
+ result += '\n' + line.slice(start, end)
523
+ // skip the space that was output as \n
524
+ start = end + 1 // derive start <= length-1
525
+ }
526
+ curr = next
527
+ }
528
+
529
+ // By the invariants, start <= length-1, so there is something left over.
530
+ // It is either the whole string or a part starting from non-whitespace.
531
+ result += '\n'
532
+ // Insert a break if the remainder is too long and there is a break available.
533
+ if (line.length - start > width && curr > start) {
534
+ result += line.slice(start, curr) + '\n' + line.slice(curr + 1)
535
+ } else {
536
+ result += line.slice(start)
537
+ }
538
+
539
+ return result.slice(1) // drop extra \n joiner
540
+ }
541
+
542
+ // Escapes a double-quoted string.
543
+ function escapeString (string) {
544
+ let result = ''
545
+ let char = 0
546
+
547
+ for (let i = 0; i < string.length; char >= 0x10000 ? i += 2 : i++) {
548
+ char = codePointAt(string, i)
549
+ const escapeSeq = ESCAPE_SEQUENCES[char]
550
+
551
+ if (!escapeSeq && isPrintable(char)) {
552
+ result += string[i]
553
+ if (char >= 0x10000) result += string[i + 1]
554
+ } else {
555
+ result += escapeSeq || encodeHex(char)
556
+ }
557
+ }
558
+
559
+ return result
560
+ }
561
+
562
+ function writeFlowSequence (state, level, object) {
563
+ let _result = ''
564
+ const _tag = state.tag
565
+
566
+ for (let index = 0, length = object.length; index < length; index += 1) {
567
+ let value = object[index]
568
+
569
+ if (state.replacer) {
570
+ value = state.replacer.call(object, String(index), value)
571
+ }
572
+
573
+ // Write only valid elements, put null instead of invalid elements.
574
+ if (writeNode(state, level, value, false, false) ||
575
+ (typeof value === 'undefined' &&
576
+ writeNode(state, level, null, false, false))) {
577
+ if (_result !== '') _result += ',' + (!state.condenseFlow ? ' ' : '')
578
+ _result += state.dump
579
+ }
580
+ }
581
+
582
+ state.tag = _tag
583
+ state.dump = '[' + _result + ']'
584
+ }
585
+
586
+ function writeBlockSequence (state, level, object, compact) {
587
+ let _result = ''
588
+ const _tag = state.tag
589
+
590
+ for (let index = 0, length = object.length; index < length; index += 1) {
591
+ let value = object[index]
592
+
593
+ if (state.replacer) {
594
+ value = state.replacer.call(object, String(index), value)
595
+ }
596
+
597
+ // Write only valid elements, put null instead of invalid elements.
598
+ if (writeNode(state, level + 1, value, true, true, false, true) ||
599
+ (typeof value === 'undefined' &&
600
+ writeNode(state, level + 1, null, true, true, false, true))) {
601
+ if (!compact || _result !== '') {
602
+ _result += generateNextLine(state, level)
603
+ }
604
+
605
+ if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
606
+ _result += '-'
607
+ } else {
608
+ _result += '- '
609
+ }
610
+
611
+ _result += state.dump
612
+ }
613
+ }
614
+
615
+ state.tag = _tag
616
+ state.dump = _result || '[]' // Empty sequence if no valid values.
617
+ }
618
+
619
+ function writeFlowMapping (state, level, object) {
620
+ let _result = ''
621
+ const _tag = state.tag
622
+ const objectKeyList = Object.keys(object)
623
+
624
+ for (let index = 0, length = objectKeyList.length; index < length; index += 1) {
625
+ let pairBuffer = ''
626
+ if (_result !== '') pairBuffer += ', '
627
+
628
+ if (state.condenseFlow) pairBuffer += '"'
629
+
630
+ const objectKey = objectKeyList[index]
631
+ let objectValue = object[objectKey]
632
+
633
+ if (state.replacer) {
634
+ objectValue = state.replacer.call(object, objectKey, objectValue)
635
+ }
636
+
637
+ if (!writeNode(state, level, objectKey, false, false)) {
638
+ continue // Skip this pair because of invalid key;
639
+ }
640
+
641
+ if (state.dump.length > 1024) pairBuffer += '? '
642
+
643
+ pairBuffer += state.dump + (state.condenseFlow ? '"' : '') + ':' + (state.condenseFlow ? '' : ' ')
644
+
645
+ if (!writeNode(state, level, objectValue, false, false)) {
646
+ continue // Skip this pair because of invalid value.
647
+ }
648
+
649
+ pairBuffer += state.dump
650
+
651
+ // Both key and value are valid.
652
+ _result += pairBuffer
653
+ }
654
+
655
+ state.tag = _tag
656
+ state.dump = '{' + _result + '}'
657
+ }
658
+
659
+ function writeBlockMapping (state, level, object, compact) {
660
+ let _result = ''
661
+ const _tag = state.tag
662
+ const objectKeyList = Object.keys(object)
663
+
664
+ // Allow sorting keys so that the output file is deterministic
665
+ if (state.sortKeys === true) {
666
+ // Default sorting
667
+ objectKeyList.sort()
668
+ } else if (typeof state.sortKeys === 'function') {
669
+ // Custom sort function
670
+ objectKeyList.sort(state.sortKeys)
671
+ } else if (state.sortKeys) {
672
+ // Something is wrong
673
+ throw new YAMLException('sortKeys must be a boolean or a function')
674
+ }
675
+
676
+ for (let index = 0, length = objectKeyList.length; index < length; index += 1) {
677
+ let pairBuffer = ''
678
+
679
+ if (!compact || _result !== '') {
680
+ pairBuffer += generateNextLine(state, level)
681
+ }
682
+
683
+ const objectKey = objectKeyList[index]
684
+ let objectValue = object[objectKey]
685
+
686
+ if (state.replacer) {
687
+ objectValue = state.replacer.call(object, objectKey, objectValue)
688
+ }
689
+
690
+ if (!writeNode(state, level + 1, objectKey, true, true, true)) {
691
+ continue // Skip this pair because of invalid key.
692
+ }
693
+
694
+ const explicitPair = (state.tag !== null && state.tag !== '?') ||
695
+ (state.dump && state.dump.length > 1024)
696
+
697
+ if (explicitPair) {
698
+ if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
699
+ pairBuffer += '?'
700
+ } else {
701
+ pairBuffer += '? '
702
+ }
703
+ }
704
+
705
+ pairBuffer += state.dump
706
+
707
+ if (explicitPair) {
708
+ pairBuffer += generateNextLine(state, level)
709
+ }
710
+
711
+ if (!writeNode(state, level + 1, objectValue, true, explicitPair)) {
712
+ continue // Skip this pair because of invalid value.
713
+ }
714
+
715
+ if (state.dump && CHAR_LINE_FEED === state.dump.charCodeAt(0)) {
716
+ pairBuffer += ':'
717
+ } else {
718
+ pairBuffer += ': '
719
+ }
720
+
721
+ pairBuffer += state.dump
722
+
723
+ // Both key and value are valid.
724
+ _result += pairBuffer
725
+ }
726
+
727
+ state.tag = _tag
728
+ state.dump = _result || '{}' // Empty mapping if no valid pairs.
729
+ }
730
+
731
+ function detectType (state, object, explicit) {
732
+ const typeList = explicit ? state.explicitTypes : state.implicitTypes
733
+
734
+ for (let index = 0, length = typeList.length; index < length; index += 1) {
735
+ const type = typeList[index]
736
+
737
+ if ((type.instanceOf || type.predicate) &&
738
+ (!type.instanceOf || ((typeof object === 'object') && (object instanceof type.instanceOf))) &&
739
+ (!type.predicate || type.predicate(object))) {
740
+ if (explicit) {
741
+ if (type.multi && type.representName) {
742
+ state.tag = type.representName(object)
743
+ } else {
744
+ state.tag = type.tag
745
+ }
746
+ } else {
747
+ state.tag = '?'
748
+ }
749
+
750
+ if (type.represent) {
751
+ const style = state.styleMap[type.tag] || type.defaultStyle
752
+
753
+ let _result
754
+ if (_toString.call(type.represent) === '[object Function]') {
755
+ _result = type.represent(object, style)
756
+ } else if (_hasOwnProperty.call(type.represent, style)) {
757
+ _result = type.represent[style](object, style)
758
+ } else {
759
+ throw new YAMLException('!<' + type.tag + '> tag resolver accepts not "' + style + '" style')
760
+ }
761
+
762
+ state.dump = _result
763
+ }
764
+
765
+ return true
766
+ }
767
+ }
768
+
769
+ return false
770
+ }
771
+
772
+ // Serializes `object` and writes it to global `result`.
773
+ // Returns true on success, or false on invalid object.
774
+ //
775
+ function writeNode (state, level, object, block, compact, iskey, isblockseq) {
776
+ state.tag = null
777
+ state.dump = object
778
+
779
+ if (!detectType(state, object, false)) {
780
+ detectType(state, object, true)
781
+ }
782
+
783
+ const type = _toString.call(state.dump)
784
+ const inblock = block
785
+
786
+ if (block) {
787
+ block = (state.flowLevel < 0 || state.flowLevel > level)
788
+ }
789
+
790
+ const objectOrArray = type === '[object Object]' || type === '[object Array]'
791
+ let duplicateIndex
792
+ let duplicate
793
+
794
+ if (objectOrArray) {
795
+ duplicateIndex = state.duplicates.indexOf(object)
796
+ duplicate = duplicateIndex !== -1
797
+ }
798
+
799
+ if ((state.tag !== null && state.tag !== '?') || duplicate || (state.indent !== 2 && level > 0)) {
800
+ compact = false
801
+ }
802
+
803
+ if (duplicate && state.usedDuplicates[duplicateIndex]) {
804
+ state.dump = '*ref_' + duplicateIndex
805
+ } else {
806
+ if (objectOrArray && duplicate && !state.usedDuplicates[duplicateIndex]) {
807
+ state.usedDuplicates[duplicateIndex] = true
808
+ }
809
+ if (type === '[object Object]') {
810
+ if (block && (Object.keys(state.dump).length !== 0)) {
811
+ writeBlockMapping(state, level, state.dump, compact)
812
+ if (duplicate) {
813
+ state.dump = '&ref_' + duplicateIndex + state.dump
814
+ }
815
+ } else {
816
+ writeFlowMapping(state, level, state.dump)
817
+ if (duplicate) {
818
+ state.dump = '&ref_' + duplicateIndex + ' ' + state.dump
819
+ }
820
+ }
821
+ } else if (type === '[object Array]') {
822
+ if (block && (state.dump.length !== 0)) {
823
+ if (state.noArrayIndent && !isblockseq && level > 0) {
824
+ writeBlockSequence(state, level - 1, state.dump, compact)
825
+ } else {
826
+ writeBlockSequence(state, level, state.dump, compact)
827
+ }
828
+ if (duplicate) {
829
+ state.dump = '&ref_' + duplicateIndex + state.dump
830
+ }
831
+ } else {
832
+ writeFlowSequence(state, level, state.dump)
833
+ if (duplicate) {
834
+ state.dump = '&ref_' + duplicateIndex + ' ' + state.dump
835
+ }
836
+ }
837
+ } else if (type === '[object String]') {
838
+ if (state.tag !== '?') {
839
+ writeScalar(state, state.dump, level, iskey, inblock)
840
+ }
841
+ } else if (type === '[object Undefined]') {
842
+ return false
843
+ } else {
844
+ if (state.skipInvalid) return false
845
+ throw new YAMLException('unacceptable kind of an object to dump ' + type)
846
+ }
847
+
848
+ if (state.tag !== null && state.tag !== '?') {
849
+ // Need to encode all characters except those allowed by the spec:
850
+ //
851
+ // [35] ns-dec-digit ::= [#x30-#x39] /* 0-9 */
852
+ // [36] ns-hex-digit ::= ns-dec-digit
853
+ // | [#x41-#x46] /* A-F */ | [#x61-#x66] /* a-f */
854
+ // [37] ns-ascii-letter ::= [#x41-#x5A] /* A-Z */ | [#x61-#x7A] /* a-z */
855
+ // [38] ns-word-char ::= ns-dec-digit | ns-ascii-letter | “-”
856
+ // [39] ns-uri-char ::= “%” ns-hex-digit ns-hex-digit | ns-word-char | “#”
857
+ // | “;” | “/” | “?” | “:” | “@” | “&” | “=” | “+” | “$” | “,”
858
+ // | “_” | “.” | “!” | “~” | “*” | “'” | “(” | “)” | “[” | “]”
859
+ //
860
+ // Also need to encode '!' because it has special meaning (end of tag prefix).
861
+ //
862
+ let tagStr = encodeURI(
863
+ state.tag[0] === '!' ? state.tag.slice(1) : state.tag
864
+ ).replace(/!/g, '%21')
865
+
866
+ if (state.tag[0] === '!') {
867
+ tagStr = '!' + tagStr
868
+ } else if (tagStr.slice(0, 18) === 'tag:yaml.org,2002:') {
869
+ tagStr = '!!' + tagStr.slice(18)
870
+ } else {
871
+ tagStr = '!<' + tagStr + '>'
872
+ }
873
+
874
+ state.dump = tagStr + ' ' + state.dump
875
+ }
876
+ }
877
+
878
+ return true
879
+ }
880
+
881
+ function getDuplicateReferences (object, state) {
882
+ const objects = []
883
+ const duplicatesIndexes = []
884
+
885
+ inspectNode(object, objects, duplicatesIndexes)
886
+
887
+ const length = duplicatesIndexes.length
888
+ for (let index = 0; index < length; index += 1) {
889
+ state.duplicates.push(objects[duplicatesIndexes[index]])
890
+ }
891
+ state.usedDuplicates = new Array(length)
892
+ }
893
+
894
+ function inspectNode (object, objects, duplicatesIndexes) {
895
+ if (object !== null && typeof object === 'object') {
896
+ const index = objects.indexOf(object)
897
+ if (index !== -1) {
898
+ if (duplicatesIndexes.indexOf(index) === -1) {
899
+ duplicatesIndexes.push(index)
900
+ }
901
+ } else {
902
+ objects.push(object)
903
+
904
+ if (Array.isArray(object)) {
905
+ for (let i = 0, length = object.length; i < length; i += 1) {
906
+ inspectNode(object[i], objects, duplicatesIndexes)
907
+ }
908
+ } else {
909
+ const objectKeyList = Object.keys(object)
910
+
911
+ for (let i = 0, length = objectKeyList.length; i < length; i += 1) {
912
+ inspectNode(object[objectKeyList[i]], objects, duplicatesIndexes)
913
+ }
914
+ }
915
+ }
916
+ }
917
+ }
918
+
919
+ function dump (input, options) {
920
+ options = options || {}
921
+
922
+ const state = new State(options)
923
+
924
+ if (!state.noRefs) getDuplicateReferences(input, state)
925
+
926
+ let value = input
927
+
928
+ if (state.replacer) {
929
+ value = state.replacer.call({ '': value }, '', value)
930
+ }
931
+
932
+ if (writeNode(state, 0, value, true, true)) return state.dump + '\n'
933
+
934
+ return ''
935
+ }
936
+
937
+ module.exports.dump = dump