1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | import * as string from './string.js'
|
8 | import * as env from './environment.js'
|
9 | import * as array from './array.js'
|
10 | import * as math from './math.js'
|
11 | import * as encoding from './encoding.js'
|
12 | import * as decoding from './decoding.js'
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export const createUint8ArrayFromLen = len => new Uint8Array(len)
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | export const createUint8ArrayViewFromArrayBuffer = (buffer, byteOffset, length) => new Uint8Array(buffer, byteOffset, length)
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export const createUint8ArrayFromArrayBuffer = buffer => new Uint8Array(buffer)
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | const toBase64Browser = bytes => {
|
41 | let s = ''
|
42 | for (let i = 0; i < bytes.byteLength; i++) {
|
43 | s += string.fromCharCode(bytes[i])
|
44 | }
|
45 |
|
46 | return btoa(s)
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | const toBase64Node = bytes => Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('base64')
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | const fromBase64Browser = s => {
|
62 |
|
63 | const a = atob(s)
|
64 | const bytes = createUint8ArrayFromLen(a.length)
|
65 | for (let i = 0; i < a.length; i++) {
|
66 | bytes[i] = a.charCodeAt(i)
|
67 | }
|
68 | return bytes
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | const fromBase64Node = s => {
|
76 | const buf = Buffer.from(s, 'base64')
|
77 | return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength)
|
78 | }
|
79 |
|
80 |
|
81 | export const toBase64 = env.isBrowser ? toBase64Browser : toBase64Node
|
82 |
|
83 |
|
84 | export const fromBase64 = env.isBrowser ? fromBase64Browser : fromBase64Node
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | export const toBase64UrlEncoded = buf => toBase64(buf).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', '')
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | export const fromBase64UrlEncoded = base64 => fromBase64(base64.replaceAll('-', '+').replaceAll('_', '/'))
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export const toHexString = buf => array.map(buf, b => b.toString(16).padStart(2, '0')).join('')
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export const fromHexString = hex => {
|
110 | const hlen = hex.length
|
111 | const buf = new Uint8Array(math.ceil(hlen / 2))
|
112 | for (let i = 0; i < hlen; i += 2) {
|
113 | buf[buf.length - i / 2 - 1] = Number.parseInt(hex.slice(hlen - i - 2, hlen - i), 16)
|
114 | }
|
115 | return buf
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export const copyUint8Array = uint8Array => {
|
125 | const newBuf = createUint8ArrayFromLen(uint8Array.byteLength)
|
126 | newBuf.set(uint8Array)
|
127 | return newBuf
|
128 | }
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | export const encodeAny = data => {
|
138 | const encoder = encoding.createEncoder()
|
139 | encoding.writeAny(encoder, data)
|
140 | return encoding.toUint8Array(encoder)
|
141 | }
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | export const decodeAny = buf => decoding.readAny(decoding.createDecoder(buf))
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | export const shiftNBitsLeft = (bs, N) => {
|
158 | if (N === 0) return bs
|
159 | bs = new Uint8Array(bs)
|
160 | bs[0] <<= N
|
161 | for (let i = 1; i < bs.length; i++) {
|
162 | bs[i - 1] |= bs[i] >>> (8 - N)
|
163 | bs[i] <<= N
|
164 | }
|
165 | return bs
|
166 | }
|