-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarReader.swift
200 lines (178 loc) · 5.68 KB
/
CarReader.swift
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import API
import Foundation
import SwiftAtproto
import SwiftCbor
public struct MessageHeader: Codable, Equatable {
public let op: UInt8
public let t: String
}
public enum RepoValue: Codable {
case commit(comatprototypes.SyncSubscribeRepos_Commit)
case handle(comatprototypes.SyncSubscribeRepos_Handle)
case identity(comatprototypes.SyncSubscribeRepos_Identity)
case account(comatprototypes.SyncSubscribeRepos_Account)
case info(comatprototypes.SyncSubscribeRepos_Info)
case migrate(comatprototypes.SyncSubscribeRepos_Migrate)
case tombstone(comatprototypes.SyncSubscribeRepos_Tombstone)
case labels(comatprototypes.LabelSubscribeLabels_Labels)
}
public struct RepoMessage: Codable {
public let header: MessageHeader
public let value: RepoValue
public init(from decoder: any Decoder) throws {
var container = try decoder.unkeyedContainer()
header = try container.decode(MessageHeader.self)
switch header.t {
case "#commit":
value = try .commit(container.decode(comatprototypes.SyncSubscribeRepos_Commit.self))
case "#handle":
value = try .handle(container.decode(comatprototypes.SyncSubscribeRepos_Handle.self))
case "#identity":
value = try .identity(container.decode(comatprototypes.SyncSubscribeRepos_Identity.self))
case "#account":
value = try .account(container.decode(comatprototypes.SyncSubscribeRepos_Account.self))
case "#info":
value = try .info(container.decode(comatprototypes.SyncSubscribeRepos_Info.self))
case "#migrate":
value = try .migrate(container.decode(comatprototypes.SyncSubscribeRepos_Migrate.self))
case "#tombstone":
value = try .tombstone(container.decode(comatprototypes.SyncSubscribeRepos_Tombstone.self))
case "#labels":
value = try .labels(container.decode(comatprototypes.LabelSubscribeLabels_Labels.self))
default:
fatalError("t: \(header.t)")
}
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(header)
switch value {
case let .commit(value):
try container.encode(value)
case let .identity(value):
try container.encode(value)
case let .account(value):
try container.encode(value)
case let .handle(value):
try container.encode(value)
case let .info(value):
try container.encode(value)
case let .migrate(value):
try container.encode(value)
case let .tombstone(value):
try container.encode(value)
case let .labels(value):
try container.encode(value)
}
}
}
enum CarError: Error {
case unexpectedEOF
case overflow
}
public struct Block {
public let cid: LexLink
public let data: Data
}
public class CarReader {
let stream: InputStream
let header: CarHeader
public init(stream: InputStream) throws {
self.stream = stream
header = try Self.readHeader(stream: stream)
}
func next() throws -> Block {
let (cid, data) = try Self.readNode(stream: stream)
return Block(cid: cid, data: data)
}
public func readAll() throws -> [Block] {
var blocks = [Block]()
repeat {
try blocks.append(next())
} while stream.hasBytesAvailable
return blocks
}
private static let maxVarintLen64 = 10
public static func readUvarint(stream: InputStream) throws -> UInt64 {
var x: UInt64 = 0
var s: UInt = 0
var i = 0
while i < Self.maxVarintLen64 {
guard let b = try ([UInt8](unsafeUninitializedCapacity: 1) { buf, initializedCount in
let bytesRead = stream.read(buf.baseAddress!, maxLength: buf.count)
initializedCount = bytesRead
guard bytesRead >= 0 else {
throw stream.streamError!
}
}).first else { throw CarError.unexpectedEOF }
if b < 0x80 {
if i == Self.maxVarintLen64 - 1, b > 1 {
throw CarError.overflow
}
return x | UInt64(b) << s
}
x |= UInt64(b & 0x7F) << s
s += 7
i += 1
}
throw CarError.overflow
}
static func ldRead(stream: InputStream) throws -> Data {
let n = try Self.readUvarint(stream: stream)
let bytes = try [UInt8](unsafeUninitializedCapacity: Int(n)) { buf, initializedCount in
let bytesRead = stream.read(buf.baseAddress!, maxLength: buf.count)
initializedCount = bytesRead
guard bytesRead >= 0 else {
throw stream.streamError!
}
}
return Data(bytes)
}
static func readHeader(stream: InputStream) throws -> CarHeader {
let hb = try Self.ldRead(stream: stream)
let decoder = CborDecoder()
return try decoder.decode(CarHeader.self, from: hb)
}
static func readNode(stream: InputStream) throws -> (LexLink, Data) {
let data = try Self.ldRead(stream: stream)
let ss = InputStream(data: data)
ss.open()
let version = try CarReader.readUvarint(stream: ss)
assert(version == 1)
_ = try CarReader.readUvarint(stream: ss)
_ = try CarReader.readUvarint(stream: ss)
let mhl = try CarReader.readUvarint(stream: ss)
let cidLength = mhl + 4
let cid = try LexLink(data[..<cidLength])
return (cid, Data(data[cidLength...]))
}
}
extension LexLink: CborCodable {
public var tag: UInt64 {
42
}
}
public struct CarHeader: Codable {
let roots: [LexLink]
let version: UInt64
}
public struct NodeData: Codable, Equatable {
private enum CodingKeys: String, CodingKey {
case left = "l"
case entries = "e"
}
let left: LexLink?
let entries: [TreeEntry]
}
public struct TreeEntry: Codable, Equatable {
private enum CodingKeys: String, CodingKey {
case prefixLen = "p"
case keySuffix = "k"
case value = "v"
case tree = "t"
}
let prefixLen: Int
let keySuffix: Data
let value: LexLink
let tree: LexLink?
}