1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | 'use strict'
|
39 |
|
40 | import { Br } from './br'
|
41 | import { Bw } from './bw'
|
42 | import isHex from 'is-hex'
|
43 |
|
44 | class Struct {
|
45 | constructor (obj) {
|
46 | this.fromObject(obj)
|
47 | }
|
48 |
|
49 | fromObject (obj) {
|
50 | if (!obj) {
|
51 | return this
|
52 | }
|
53 | for (const key of Object.keys(obj)) {
|
54 | if (obj[key] !== undefined) {
|
55 | this[key] = obj[key]
|
56 | }
|
57 | }
|
58 | return this
|
59 | }
|
60 |
|
61 | static fromObject (obj) {
|
62 | return new this().fromObject(obj)
|
63 | }
|
64 |
|
65 | fromBr (br) {
|
66 | if (!(br instanceof Br)) {
|
67 | throw new Error('br must be a buffer reader')
|
68 | }
|
69 | throw new Error('not implemented')
|
70 | }
|
71 |
|
72 | static fromBr (br) {
|
73 | return new this().fromBr(br)
|
74 | }
|
75 |
|
76 | asyncFromBr (br) {
|
77 | if (!(br instanceof Br)) {
|
78 | throw new Error('br must be a buffer reader')
|
79 | }
|
80 | throw new Error('not implemented')
|
81 | }
|
82 |
|
83 | static asyncFromBr (br) {
|
84 | return new this().asyncFromBr(br)
|
85 | }
|
86 |
|
87 | toBw (bw) {
|
88 | throw new Error('not implemented')
|
89 | }
|
90 |
|
91 | asyncToBw (bw) {
|
92 | throw new Error('not implemented')
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 | * genFromBuffers () {
|
105 | throw new Error('not implemented')
|
106 | }
|
107 |
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | * expect (len, startbuf) {
|
116 | let buf = startbuf
|
117 | const bw = new Bw()
|
118 | let gotlen = 0
|
119 | if (startbuf) {
|
120 | bw.write(startbuf)
|
121 | gotlen += startbuf.length
|
122 | }
|
123 | while (gotlen < len) {
|
124 | const remainderlen = len - gotlen
|
125 | buf = yield remainderlen
|
126 | if (!buf) {
|
127 | continue
|
128 | }
|
129 | bw.write(buf)
|
130 | gotlen += buf.length
|
131 | }
|
132 | buf = bw.toBuffer()
|
133 | const overlen = gotlen - len
|
134 | const remainderbuf = buf.slice(buf.length - overlen, buf.length)
|
135 | buf = buf.slice(0, buf.length - overlen)
|
136 | return {
|
137 | buf: buf,
|
138 | remainderbuf: remainderbuf
|
139 | }
|
140 | }
|
141 |
|
142 | |
143 |
|
144 |
|
145 | fromBuffer (buf, ...rest) {
|
146 | if (!Buffer.isBuffer(buf)) {
|
147 | throw new Error('buf must be a buffer')
|
148 | }
|
149 | const br = new Br(buf)
|
150 | return this.fromBr(br, ...rest)
|
151 | }
|
152 |
|
153 | static fromBuffer (...rest) {
|
154 | return new this().fromBuffer(...rest)
|
155 | }
|
156 |
|
157 | asyncFromBuffer (buf, ...rest) {
|
158 | if (!Buffer.isBuffer(buf)) {
|
159 | throw new Error('buf must be a buffer')
|
160 | }
|
161 | const br = new Br(buf)
|
162 | return this.asyncFromBr(br, ...rest)
|
163 | }
|
164 |
|
165 | static asyncFromBuffer (buf, ...rest) {
|
166 | return new this().asyncFromBuffer(buf, ...rest)
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 | fromFastBuffer (buf, ...rest) {
|
173 | if (buf.length === 0) {
|
174 | return this
|
175 | } else {
|
176 | return this.fromBuffer(buf, ...rest)
|
177 | }
|
178 | }
|
179 |
|
180 | static fromFastBuffer (...rest) {
|
181 | return new this().fromFastBuffer(...rest)
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 | toBuffer (...rest) {
|
189 | return this.toBw(...rest).toBuffer()
|
190 | }
|
191 |
|
192 | asyncToBuffer (...rest) {
|
193 | return this.asyncToBw(...rest).then(bw => bw.toBuffer())
|
194 | }
|
195 |
|
196 | |
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | toFastBuffer (...rest) {
|
212 | if (Object.keys(this).length === 0) {
|
213 | return Buffer.alloc(0)
|
214 | } else {
|
215 | return this.toBuffer(...rest)
|
216 | }
|
217 | }
|
218 |
|
219 | fromHex (hex, ...rest) {
|
220 | if (!isHex(hex)) {
|
221 | throw new Error('invalid hex string')
|
222 | }
|
223 | const buf = Buffer.from(hex, 'hex')
|
224 | return this.fromBuffer(buf, ...rest)
|
225 | }
|
226 |
|
227 | static fromHex (hex, ...rest) {
|
228 | return new this().fromHex(hex, ...rest)
|
229 | }
|
230 |
|
231 | asyncFromHex (hex, ...rest) {
|
232 | if (!isHex(hex)) {
|
233 | throw new Error('invalid hex string')
|
234 | }
|
235 | const buf = Buffer.from(hex, 'hex')
|
236 | return this.asyncFromBuffer(buf, ...rest)
|
237 | }
|
238 |
|
239 | static asyncFromHex (hex, ...rest) {
|
240 | return new this().asyncFromHex(hex, ...rest)
|
241 | }
|
242 |
|
243 | fromFastHex (hex, ...rest) {
|
244 | if (!isHex(hex)) {
|
245 | throw new Error('invalid hex string')
|
246 | }
|
247 | const buf = Buffer.from(hex, 'hex')
|
248 | return this.fromFastBuffer(buf, ...rest)
|
249 | }
|
250 |
|
251 | static fromFastHex (hex, ...rest) {
|
252 | return new this().fromFastHex(hex, ...rest)
|
253 | }
|
254 |
|
255 | toHex (...rest) {
|
256 | return this.toBuffer(...rest).toString('hex')
|
257 | }
|
258 |
|
259 | asyncToHex (...rest) {
|
260 | return this.asyncToBuffer(...rest).then(buf => buf.toString('hex'))
|
261 | }
|
262 |
|
263 | toFastHex (...rest) {
|
264 | return this.toFastBuffer(...rest).toString('hex')
|
265 | }
|
266 |
|
267 | fromString (str, ...rest) {
|
268 | if (typeof str !== 'string') {
|
269 | throw new Error('str must be a string')
|
270 | }
|
271 | return this.fromHex(str, ...rest)
|
272 | }
|
273 |
|
274 | static fromString (str, ...rest) {
|
275 | return new this().fromString(str, ...rest)
|
276 | }
|
277 |
|
278 | asyncFromString (str, ...rest) {
|
279 | if (typeof str !== 'string') {
|
280 | throw new Error('str must be a string')
|
281 | }
|
282 | return this.asyncFromHex(str, ...rest)
|
283 | }
|
284 |
|
285 | static asyncFromString (str, ...rest) {
|
286 | return new this().asyncFromString(str, ...rest)
|
287 | }
|
288 |
|
289 | toString (...rest) {
|
290 | return this.toHex(...rest)
|
291 | }
|
292 |
|
293 | asyncToString (...rest) {
|
294 | return this.asyncToHex(...rest)
|
295 | }
|
296 |
|
297 | fromJSON (json) {
|
298 | throw new Error('not implemented')
|
299 | }
|
300 |
|
301 | static fromJSON (json, ...rest) {
|
302 | return new this().fromJSON(json, ...rest)
|
303 | }
|
304 |
|
305 | asyncFromJSON (json, ...rest) {
|
306 | throw new Error('not implemented')
|
307 | }
|
308 |
|
309 | static asyncFromJSON (json, ...rest) {
|
310 | return new this().asyncFromJSON(json, ...rest)
|
311 | }
|
312 |
|
313 | toJSON () {
|
314 | var json = {}
|
315 | for (var val in this) {
|
316 |
|
317 | if (Array.isArray(this[val])) {
|
318 | const arr = []
|
319 | for (var i in this[val]) {
|
320 | if (typeof this[val][i].toJSON === 'function') {
|
321 | arr.push(this[val][i].toJSON())
|
322 | } else {
|
323 | arr.push(JSON.stringify(this[val][i]))
|
324 | }
|
325 | }
|
326 | json[val] = arr
|
327 |
|
328 | } else if (this[val] === null) {
|
329 | json[val] = this[val]
|
330 | } else if (
|
331 | typeof this[val] === 'object' &&
|
332 | typeof this[val].toJSON === 'function'
|
333 | ) {
|
334 | json[val] = this[val].toJSON()
|
335 |
|
336 | } else if (
|
337 | typeof this[val] === 'boolean' ||
|
338 | typeof this[val] === 'number' ||
|
339 | typeof this[val] === 'string'
|
340 | ) {
|
341 | json[val] = this[val]
|
342 |
|
343 | } else if (Buffer.isBuffer(this[val])) {
|
344 | json[val] = this[val].toString('hex')
|
345 |
|
346 | } else if (this[val] instanceof Map) {
|
347 | json[val] = JSON.stringify(this[val])
|
348 |
|
349 | } else if (typeof this[val] === 'object') {
|
350 | throw new Error('not implemented')
|
351 | }
|
352 | }
|
353 | return json
|
354 |
|
355 | }
|
356 |
|
357 | asyncToJSON () {
|
358 | throw new Error('not implemented')
|
359 | }
|
360 |
|
361 | clone () {
|
362 |
|
363 |
|
364 | return this.cloneByJSON()
|
365 | }
|
366 |
|
367 | cloneByBuffer () {
|
368 | return new this.constructor().fromBuffer(this.toBuffer())
|
369 | }
|
370 |
|
371 | cloneByFastBuffer () {
|
372 | return new this.constructor().fromFastBuffer(this.toFastBuffer())
|
373 | }
|
374 |
|
375 | cloneByHex () {
|
376 | return new this.constructor().fromHex(this.toHex())
|
377 | }
|
378 |
|
379 | cloneByString () {
|
380 | return new this.constructor().fromString(this.toString())
|
381 | }
|
382 |
|
383 | cloneByJSON () {
|
384 | return new this.constructor().fromJSON(this.toJSON())
|
385 | }
|
386 | }
|
387 |
|
388 | export { Struct }
|