Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81149 views
1
'use strict';
2
3
/*eslint-disable no-bitwise*/
4
5
// A trick for browserified version.
6
// Since we make browserifier to ignore `buffer` module, NodeBuffer will be undefined
7
var NodeBuffer = require('buffer').Buffer;
8
var Type = require('../type');
9
10
11
// [ 64, 65, 66 ] -> [ padding, CR, LF ]
12
var BASE64_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r';
13
14
15
function resolveYamlBinary(data) {
16
if (null === data) {
17
return false;
18
}
19
20
var code, idx, bitlen = 0, len = 0, max = data.length, map = BASE64_MAP;
21
22
// Convert one by one.
23
for (idx = 0; idx < max; idx++) {
24
code = map.indexOf(data.charAt(idx));
25
26
// Skip CR/LF
27
if (code > 64) { continue; }
28
29
// Fail on illegal characters
30
if (code < 0) { return false; }
31
32
bitlen += 6;
33
}
34
35
// If there are any bits left, source was corrupted
36
return (bitlen % 8) === 0;
37
}
38
39
function constructYamlBinary(data) {
40
var code, idx, tailbits,
41
input = data.replace(/[\r\n=]/g, ''), // remove CR/LF & padding to simplify scan
42
max = input.length,
43
map = BASE64_MAP,
44
bits = 0,
45
result = [];
46
47
// Collect by 6*4 bits (3 bytes)
48
49
for (idx = 0; idx < max; idx++) {
50
if ((idx % 4 === 0) && idx) {
51
result.push((bits >> 16) & 0xFF);
52
result.push((bits >> 8) & 0xFF);
53
result.push(bits & 0xFF);
54
}
55
56
bits = (bits << 6) | map.indexOf(input.charAt(idx));
57
}
58
59
// Dump tail
60
61
tailbits = (max % 4) * 6;
62
63
if (tailbits === 0) {
64
result.push((bits >> 16) & 0xFF);
65
result.push((bits >> 8) & 0xFF);
66
result.push(bits & 0xFF);
67
} else if (tailbits === 18) {
68
result.push((bits >> 10) & 0xFF);
69
result.push((bits >> 2) & 0xFF);
70
} else if (tailbits === 12) {
71
result.push((bits >> 4) & 0xFF);
72
}
73
74
// Wrap into Buffer for NodeJS and leave Array for browser
75
if (NodeBuffer) {
76
return new NodeBuffer(result);
77
}
78
79
return result;
80
}
81
82
function representYamlBinary(object /*, style*/) {
83
var result = '', bits = 0, idx, tail,
84
max = object.length,
85
map = BASE64_MAP;
86
87
// Convert every three bytes to 4 ASCII characters.
88
89
for (idx = 0; idx < max; idx++) {
90
if ((idx % 3 === 0) && idx) {
91
result += map[(bits >> 18) & 0x3F];
92
result += map[(bits >> 12) & 0x3F];
93
result += map[(bits >> 6) & 0x3F];
94
result += map[bits & 0x3F];
95
}
96
97
bits = (bits << 8) + object[idx];
98
}
99
100
// Dump tail
101
102
tail = max % 3;
103
104
if (tail === 0) {
105
result += map[(bits >> 18) & 0x3F];
106
result += map[(bits >> 12) & 0x3F];
107
result += map[(bits >> 6) & 0x3F];
108
result += map[bits & 0x3F];
109
} else if (tail === 2) {
110
result += map[(bits >> 10) & 0x3F];
111
result += map[(bits >> 4) & 0x3F];
112
result += map[(bits << 2) & 0x3F];
113
result += map[64];
114
} else if (tail === 1) {
115
result += map[(bits >> 2) & 0x3F];
116
result += map[(bits << 4) & 0x3F];
117
result += map[64];
118
result += map[64];
119
}
120
121
return result;
122
}
123
124
function isBinary(object) {
125
return NodeBuffer && NodeBuffer.isBuffer(object);
126
}
127
128
module.exports = new Type('tag:yaml.org,2002:binary', {
129
kind: 'scalar',
130
resolve: resolveYamlBinary,
131
construct: constructYamlBinary,
132
predicate: isBinary,
133
represent: representYamlBinary
134
});
135
136