-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathutils.js
More file actions
87 lines (77 loc) · 2.68 KB
/
utils.js
File metadata and controls
87 lines (77 loc) · 2.68 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function getField (countField, context) {
const countFieldArr = countField.split('/')
let i = 0
if (countFieldArr[i] === '') {
while (context['..'] !== undefined) { context = context['..'] }
i++
}
for (; i < countFieldArr.length; i++) { context = context[countFieldArr[i]] }
return context
}
function getFieldInfo (fieldInfo) {
if (typeof fieldInfo === 'string') { return { type: fieldInfo } } else if (Array.isArray(fieldInfo)) { return { type: fieldInfo[0], typeArgs: fieldInfo[1] } } else if (typeof fieldInfo.type === 'string') { return fieldInfo } else { throw new Error('Not a fieldinfo') }
}
function getCount (buffer, offset, { count, countType }, rootNode) {
let c = 0
let size = 0
if (typeof count === 'number') { c = count } else if (typeof count !== 'undefined') {
c = getField(count, rootNode)
} else if (typeof countType !== 'undefined') {
({ size, value: c } = tryDoc(() => this.read(buffer, offset, getFieldInfo(countType), rootNode), '$count'))
} else { // TODO : broken schema, should probably error out.
c = 0
}
return { count: c, size }
}
function sendCount (len, buffer, offset, { count, countType }, rootNode) {
if (typeof count !== 'undefined' && len !== count) {
// TODO: Throw
} else if (typeof countType !== 'undefined') {
offset = this.write(len, buffer, offset, getFieldInfo(countType), rootNode)
} else {
// TODO: Throw
}
return offset
}
function calcCount (len, { count, countType }, rootNode) {
if (typeof count === 'undefined' && typeof countType !== 'undefined') { return tryDoc(() => this.sizeOf(len, getFieldInfo(countType), rootNode), '$count') } else { return 0 }
}
function addErrorField (e, field) {
e.field = e.field ? field + '.' + e.field : field
throw e
}
function tryCatch(tryfn, catchfn) {
const oldLimit = Error.stackTraceLimit
Error.stackTraceLimit = Math.max(40, oldLimit)
try { return tryfn() } catch (e) { catchfn(e) } finally { Error.stackTraceLimit = oldLimit }
}
function tryDoc (tryfn, field) {
return tryCatch(tryfn, (e) => addErrorField(e, field))
}
class ExtendableError extends Error {
constructor (message) {
super(message)
this.name = this.constructor.name
this.message = message
if (Error.captureStackTrace != null) {
Error.captureStackTrace(this, this.constructor.name)
}
}
}
class PartialReadError extends ExtendableError {
constructor (message) {
super(message)
this.partialReadError = true
}
}
module.exports = {
getField: getField,
getFieldInfo: getFieldInfo,
addErrorField: addErrorField,
getCount: getCount,
sendCount: sendCount,
calcCount: calcCount,
tryCatch: tryCatch,
tryDoc: tryDoc,
PartialReadError: PartialReadError
}