@tokamakdev/platform-macos-x64 0.1.1-beta.155 → 0.1.1-beta.157

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
1
  import Foundation
2
2
 
3
- /// A plugin whose `apple/Exports.swift` lists the methods the Worker calls
3
+ /// A plugin whose `Exports.swift` lists the methods the Worker calls
4
4
  /// and the events it receives. `tok plugin generate apple` reads it and writes
5
5
  /// the plugin's dispatch and TypeScript API.
6
- protocol TokamakExports: TokamakPlugin {
6
+ public protocol TokamakExports: TokamakPlugin {
7
7
  /// The plugin's host, whose resources hold the native references methods take.
8
8
  var host: TokamakHost { get }
9
9
 
@@ -19,17 +19,17 @@ protocol TokamakExports: TokamakPlugin {
19
19
  }
20
20
 
21
21
  extension TokamakExports {
22
- func eventPayload(_ name: String, _ event: Any) throws(TokamakPluginError) -> Data {
22
+ public func eventPayload(_ name: String, _ event: Any) throws(TokamakPluginError) -> Data {
23
23
  throw .unlistedEvent(name)
24
24
  }
25
25
 
26
- func eventReply(_ name: String, _ reply: Data) throws(TokamakPluginError) -> Any? {
26
+ public func eventReply(_ name: String, _ reply: Data) throws(TokamakPluginError) -> Any? {
27
27
  throw .unlistedReply(name)
28
28
  }
29
29
  }
30
30
 
31
31
  /// What a stream does with values a slow reader has not taken.
32
- enum TokamakOverflow {
32
+ public enum TokamakOverflow {
33
33
  /// Ends the stream with `QuotaExceededError`.
34
34
  case error
35
35
  /// Drops the oldest values, for samples where only recent ones matter.
@@ -39,52 +39,52 @@ enum TokamakOverflow {
39
39
  /// Lists a plugin's exported methods and events: method parameters and event
40
40
  /// replies are `Decodable`, method results and events `Encodable`, and options
41
41
  /// literal values.
42
- struct TokamakExporter<Plugin> {
42
+ public struct TokamakExporter<Plugin> {
43
43
  /// Lists the event `name`, whose listeners receive an `E`.
44
- mutating func event<E: Encodable>(_ name: String, _ type: E.Type, doc: String = "") {}
44
+ public mutating func event<E: Encodable>(_ name: String, _ type: E.Type, doc: String = "") {}
45
45
 
46
46
  /// Lists the event `name`, whose listeners receive an `E` and may reply with an `R`.
47
- mutating func event<E: Encodable, R: Decodable>(
47
+ public mutating func event<E: Encodable, R: Decodable>(
48
48
  _ name: String, _ type: E.Type, reply: R.Type, doc: String = ""
49
49
  ) {}
50
50
 
51
51
  /// Exports `method`, which returns once.
52
- mutating func call<each A: Decodable, R: Encodable>(
52
+ public mutating func call<each A: Decodable, R: Encodable>(
53
53
  _ method: (Plugin) -> (repeat each A) async throws -> R, doc: String = ""
54
54
  ) {}
55
55
 
56
- mutating func call<each A: Decodable>(
56
+ public mutating func call<each A: Decodable>(
57
57
  _ method: (Plugin) -> (repeat each A) async throws -> Void, doc: String = ""
58
58
  ) {}
59
59
 
60
60
  /// Exports `method`, whose last parameter reads the bytes the Worker streams.
61
- mutating func call<each A: Decodable, R: Encodable>(
61
+ public mutating func call<each A: Decodable, R: Encodable>(
62
62
  _ method: (Plugin) -> (repeat each A, TokamakPluginBody) async throws -> R, doc: String = ""
63
63
  ) {}
64
64
 
65
- mutating func call<each A: Decodable>(
65
+ public mutating func call<each A: Decodable>(
66
66
  _ method: (Plugin) -> (repeat each A, TokamakPluginBody) async throws -> Void, doc: String = ""
67
67
  ) {}
68
68
 
69
69
  /// Exports `method`, which returns the values it sends until the Worker cancels.
70
- mutating func stream<each A: Decodable, S: AsyncSequence>(
70
+ public mutating func stream<each A: Decodable, S: AsyncSequence>(
71
71
  _ method: (Plugin) -> (repeat each A) throws -> S, overflow: TokamakOverflow = .error,
72
72
  doc: String = ""
73
73
  ) where S.Element: Encodable {}
74
74
 
75
75
  /// Exports `method`, which returns a native object the Worker holds as a
76
76
  /// `NativeReference`.
77
- mutating func resource<each A: Decodable, R>(
77
+ public mutating func resource<each A: Decodable, R>(
78
78
  _ method: (Plugin) -> (repeat each A) async throws -> R, doc: String = ""
79
79
  ) {}
80
80
  }
81
81
 
82
82
  /// A native object the Worker passes as a `NativeReference`, which another
83
83
  /// plugin retained.
84
- struct TokamakReference<Object>: Decodable {
85
- let object: Object
84
+ public struct TokamakReference<Object>: Decodable {
85
+ public let object: Object
86
86
 
87
- init(from decoder: any Decoder) throws {
87
+ public init(from decoder: any Decoder) throws {
88
88
  let id = try decoder.singleValueContainer().decode(String.self)
89
89
  guard let resources = decoder.userInfo[.tokamakResources] as? TokamakResources else {
90
90
  throw TokamakPluginError(name: "InvalidStateError", message: "No native references here")
@@ -94,7 +94,7 @@ struct TokamakReference<Object>: Decodable {
94
94
  }
95
95
 
96
96
  /// Any JSON value, for data a plugin passes through or reads itself.
97
- enum TokamakJSON: Codable, Equatable {
97
+ public enum TokamakJSON: Codable, Equatable {
98
98
  case null
99
99
  case bool(Bool)
100
100
  case number(Double)
@@ -102,7 +102,7 @@ enum TokamakJSON: Codable, Equatable {
102
102
  case array([TokamakJSON])
103
103
  case object([String: TokamakJSON])
104
104
 
105
- init(from decoder: any Decoder) throws {
105
+ public init(from decoder: any Decoder) throws {
106
106
  // Each failed attempt throws, so the commonest kinds of value come first.
107
107
  let container = try decoder.singleValueContainer()
108
108
  if container.decodeNil() {
@@ -120,7 +120,7 @@ enum TokamakJSON: Codable, Equatable {
120
120
  }
121
121
  }
122
122
 
123
- func encode(to encoder: any Encoder) throws {
123
+ public func encode(to encoder: any Encoder) throws {
124
124
  var container = encoder.singleValueContainer()
125
125
  switch self {
126
126
  case .null: try container.encodeNil()
@@ -133,7 +133,7 @@ enum TokamakJSON: Codable, Equatable {
133
133
  }
134
134
 
135
135
  /// `value` as JSON, which `JSONSerialization` writes it as, or nil when it is not JSON.
136
- init?(_ value: Any) {
136
+ public init?(_ value: Any) {
137
137
  switch value {
138
138
  case is NSNull: self = .null
139
139
  case let number as NSNumber where CFGetTypeID(number) == CFBooleanGetTypeID():
@@ -154,7 +154,7 @@ enum TokamakJSON: Codable, Equatable {
154
154
 
155
155
  /// The value as `JSONSerialization` reads it: `NSNull`, an `NSNumber` for a boolean or a
156
156
  /// number, which `as? Int` reads when whole, `String`, arrays and dictionaries.
157
- var value: Any {
157
+ public var value: Any {
158
158
  switch self {
159
159
  case .null: NSNull()
160
160
  case .bool(let bool): NSNumber(value: bool)
@@ -169,38 +169,38 @@ enum TokamakJSON: Codable, Equatable {
169
169
  /// An enum whose cases are the forms its values take: a case without an associated value is
170
170
  /// the string of its name, and a case with one unlabeled associated value is that value.
171
171
  /// `tok plugin generate apple` writes its coding in place of these defaults, which throw.
172
- protocol TokamakUnion: Codable {}
172
+ public protocol TokamakUnion: Codable {}
173
173
 
174
174
  /// A `TokamakUnion` whose values are objects that name their case in the field `tag`, with a
175
175
  /// struct case's fields.
176
- protocol TokamakTaggedUnion: TokamakUnion {
176
+ public protocol TokamakTaggedUnion: TokamakUnion {
177
177
  /// The tag field's name, as a string literal, which `tok plugin generate apple` reads.
178
178
  static var tag: String { get }
179
179
  }
180
180
 
181
181
  extension TokamakUnion {
182
- init(from decoder: any Decoder) throws {
182
+ public init(from decoder: any Decoder) throws {
183
183
  throw TokamakPluginError.ungenerated(Self.self)
184
184
  }
185
185
 
186
- func encode(to encoder: any Encoder) throws {
186
+ public func encode(to encoder: any Encoder) throws {
187
187
  throw TokamakPluginError.ungenerated(Self.self)
188
188
  }
189
189
  }
190
190
 
191
191
  /// The JSON value whose kind a union's generated decoder chooses its case by.
192
- struct TokamakUnionValue {
193
- enum Kind {
192
+ public struct TokamakUnionValue {
193
+ public enum Kind {
194
194
  case null, boolean, number, array
195
195
  case string(String)
196
196
  /// An object, with its keys.
197
197
  case object(Set<String>)
198
198
  }
199
199
 
200
- let kind: Kind
200
+ public let kind: Kind
201
201
  private let decoder: any Decoder
202
202
 
203
- init(_ decoder: any Decoder) throws {
203
+ public init(_ decoder: any Decoder) throws {
204
204
  self.decoder = decoder
205
205
  let value = try decoder.singleValueContainer()
206
206
  if value.decodeNil() {
@@ -219,17 +219,17 @@ struct TokamakUnionValue {
219
219
  }
220
220
 
221
221
  /// The value as a `T`.
222
- func decode<T: Decodable>(_ type: T.Type) throws -> T {
222
+ public func decode<T: Decodable>(_ type: T.Type) throws -> T {
223
223
  try decoder.singleValueContainer().decode(type)
224
224
  }
225
225
 
226
226
  /// The string in the object's field `tag`.
227
- func tag(_ tag: String) throws -> String {
227
+ public func tag(_ tag: String) throws -> String {
228
228
  try decoder.container(keyedBy: TokamakUnionKey.self).decode(String.self, forKey: .init(tag))
229
229
  }
230
230
 
231
231
  /// The error of a value that is none of the union `name`'s forms.
232
- func mismatch(_ name: String) -> DecodingError {
232
+ public func mismatch(_ name: String) -> DecodingError {
233
233
  .dataCorrupted(
234
234
  .init(codingPath: decoder.codingPath, debugDescription: "must be one of \(name)'s forms"))
235
235
  }
@@ -237,7 +237,7 @@ struct TokamakUnionValue {
237
237
 
238
238
  /// Encodes the case `name` of a tagged union as an object with `value`'s fields, if any, and the
239
239
  /// field `tag` naming the case.
240
- func tokamakEncode(
240
+ public func tokamakEncode(
241
241
  tag: String, _ name: String, _ value: (any Encodable)? = nil, to encoder: any Encoder
242
242
  ) throws {
243
243
  try value?.encode(to: encoder)
@@ -246,19 +246,19 @@ func tokamakEncode(
246
246
  }
247
247
 
248
248
  /// A JSON object's key, as a union's coding reads and writes it.
249
- struct TokamakUnionKey: CodingKey {
250
- let stringValue: String
251
- var intValue: Int? { nil }
249
+ public struct TokamakUnionKey: CodingKey {
250
+ public let stringValue: String
251
+ public var intValue: Int? { nil }
252
252
 
253
- init(_ stringValue: String) {
253
+ public init(_ stringValue: String) {
254
254
  self.stringValue = stringValue
255
255
  }
256
256
 
257
- init?(stringValue: String) {
257
+ public init?(stringValue: String) {
258
258
  self.stringValue = stringValue
259
259
  }
260
260
 
261
- init?(intValue: Int) {
261
+ public init?(intValue: Int) {
262
262
  nil
263
263
  }
264
264
  }
@@ -271,7 +271,7 @@ extension CodingUserInfoKey {
271
271
  extension TokamakPluginError {
272
272
  /// A `TypeError` naming where the `root` value, such as a call's
273
273
  /// arguments, fails to decode.
274
- init(decoding error: DecodingError, in root: String = "arguments") {
274
+ public init(decoding error: DecodingError, in root: String = "arguments") {
275
275
  let (path, problem): ([any CodingKey], String) =
276
276
  switch error {
277
277
  case .keyNotFound(let key, let context): (context.codingPath + [key], "is missing")
@@ -288,24 +288,24 @@ extension TokamakPluginError {
288
288
  }
289
289
 
290
290
  /// A `TokamakUnion` without the coding `tok plugin generate apple` writes.
291
- static func ungenerated(_ union: Any.Type) -> Self {
291
+ public static func ungenerated(_ union: Any.Type) -> Self {
292
292
  Self(name: "TypeError", message: "run tok plugin generate apple to code \(union)")
293
293
  }
294
294
 
295
295
  /// The plugin emitted an event its registration does not list.
296
- static func unlistedEvent(_ name: String) -> Self {
297
- Self(name: "TypeError", message: "apple/Exports.swift lists no event \(name)")
296
+ public static func unlistedEvent(_ name: String) -> Self {
297
+ Self(name: "TypeError", message: "Exports.swift lists no event \(name)")
298
298
  }
299
299
 
300
300
  /// The plugin asked for the reply to an event its registration lists without one.
301
- static func unlistedReply(_ name: String) -> Self {
302
- Self(name: "TypeError", message: "apple/Exports.swift lists no reply to event \(name)")
301
+ public static func unlistedReply(_ name: String) -> Self {
302
+ Self(name: "TypeError", message: "Exports.swift lists no reply to event \(name)")
303
303
  }
304
304
  }
305
305
 
306
306
  /// Decodes a call's JSON arguments, or another `root` value, finding the
307
307
  /// native references they name in `resources`.
308
- func tokamakDecode<T: Decodable>(
308
+ public func tokamakDecode<T: Decodable>(
309
309
  _ type: T.Type, from payload: Data, resources: TokamakResources? = nil,
310
310
  in root: String = "arguments"
311
311
  ) throws(TokamakPluginError) -> T {
@@ -322,7 +322,7 @@ func tokamakDecode<T: Decodable>(
322
322
 
323
323
  /// `value` as a reply's payload: `Data` as it is, and any other value as the
324
324
  /// JSON `encoder` writes.
325
- func tokamakEncode<T: Encodable>(
325
+ public func tokamakEncode<T: Encodable>(
326
326
  _ value: T, with encoder: JSONEncoder = JSONEncoder()
327
327
  ) throws(TokamakPluginError) -> Data {
328
328
  if let data = value as? Data { return data }
@@ -334,7 +334,7 @@ func tokamakEncode<T: Encodable>(
334
334
  }
335
335
 
336
336
  /// `event` as the JSON data of the event `name`, whose listeners receive a `T`.
337
- func tokamakEvent<T: Encodable>(
337
+ public func tokamakEvent<T: Encodable>(
338
338
  _ name: String, _ event: Any, as type: T.Type
339
339
  ) throws(TokamakPluginError) -> Data {
340
340
  guard let event = event as? T else {
@@ -346,14 +346,16 @@ func tokamakEvent<T: Encodable>(
346
346
 
347
347
  /// Replies with what `method` returns. A synchronous method runs on the
348
348
  /// call's executor and has nothing to cancel.
349
- func tokamakCall<T: Encodable>(
349
+ public func tokamakCall<T: Encodable>(
350
350
  _ reply: TokamakPluginBytesReply, _ method: () throws -> T
351
351
  ) -> (() -> Void)? {
352
352
  reply(result { try tokamakEncode(try method()) })
353
353
  return nil
354
354
  }
355
355
 
356
- func tokamakCall(_ reply: TokamakPluginBytesReply, _ method: () throws -> Void) -> (() -> Void)? {
356
+ public func tokamakCall(_ reply: TokamakPluginBytesReply, _ method: () throws -> Void) -> (
357
+ () -> Void
358
+ )? {
357
359
  reply(
358
360
  result {
359
361
  try method()
@@ -364,13 +366,13 @@ func tokamakCall(_ reply: TokamakPluginBytesReply, _ method: () throws -> Void)
364
366
 
365
367
  /// Runs `method` in a task, replying with what it returns. Cancelling the
366
368
  /// call cancels the task.
367
- func tokamakCall<T: Encodable>(
369
+ public func tokamakCall<T: Encodable>(
368
370
  _ reply: @escaping TokamakPluginBytesReply, _ method: @escaping () async throws -> T
369
371
  ) -> (() -> Void)? {
370
372
  task { reply(await awaitResult { try tokamakEncode(try await method()) }) }
371
373
  }
372
374
 
373
- func tokamakCall(
375
+ public func tokamakCall(
374
376
  _ reply: @escaping TokamakPluginBytesReply, _ method: @escaping () async throws -> Void
375
377
  ) -> (() -> Void)? {
376
378
  task {
@@ -385,14 +387,14 @@ func tokamakCall(
385
387
  /// Replies with each value `method`'s sequence sends, until it ends or the
386
388
  /// subscription is cancelled. A synchronous method runs on the
387
389
  /// subscription's executor.
388
- func tokamakStream<S: AsyncSequence>(
390
+ public func tokamakStream<S: AsyncSequence>(
389
391
  _ reply: @escaping TokamakPluginBytesReply, _ method: () throws -> S
390
392
  ) throws(TokamakPluginError) -> () -> Void where S.Element: Encodable {
391
393
  let values = try catching(method)
392
394
  return task { await send(values, reply) }
393
395
  }
394
396
 
395
- func tokamakStream<S: AsyncSequence>(
397
+ public func tokamakStream<S: AsyncSequence>(
396
398
  _ reply: @escaping TokamakPluginBytesReply, _ method: @escaping () async throws -> S
397
399
  ) -> () -> Void where S.Element: Encodable {
398
400
  task {
@@ -407,14 +409,14 @@ func tokamakStream<S: AsyncSequence>(
407
409
  /// Retains the object `method` returns in `resources`, replying with its
408
410
  /// ID, until the subscription is cancelled. A synchronous method runs on the
409
411
  /// subscription's executor.
410
- func tokamakResource(
412
+ public func tokamakResource(
411
413
  _ resources: TokamakResources, _ reply: @escaping TokamakPluginBytesReply,
412
414
  _ method: () throws -> Any
413
415
  ) throws(TokamakPluginError) -> () -> Void {
414
416
  resources.retain(try catching(method), reply: reply)
415
417
  }
416
418
 
417
- func tokamakResource(
419
+ public func tokamakResource(
418
420
  _ resources: TokamakResources, _ reply: @escaping TokamakPluginBytesReply,
419
421
  _ method: @escaping () async throws -> Any
420
422
  ) -> () -> Void {
@@ -441,20 +443,20 @@ private let heldCapacity = 64
441
443
  /// value its reader has not taken, so a Worker's subscription applies its own
442
444
  /// overflow policy. When `holding`, the newest 64 values sent while none
443
445
  /// listen wait for the first listener.
444
- final class TokamakListeners<Value>: @unchecked Sendable {
446
+ public final class TokamakListeners<Value>: @unchecked Sendable {
445
447
  private let lock = NSLock()
446
448
  private let holding: Bool
447
449
  private var continuations: [UUID: AsyncStream<Value>.Continuation] = [:]
448
450
  private var held: [Value] = []
449
451
 
450
- init(holding: Bool = false) {
452
+ public init(holding: Bool = false) {
451
453
  self.holding = holding
452
454
  }
453
455
 
454
456
  /// A new listener's stream.
455
457
  // Values are yielded under the lock, so one sent while a listener starts
456
458
  // follows the held ones. Yielding only buffers.
457
- func stream() -> AsyncStream<Value> {
459
+ public func stream() -> AsyncStream<Value> {
458
460
  let (stream, continuation) = AsyncStream.makeStream(of: Value.self)
459
461
  let key = UUID()
460
462
  continuation.onTermination = { [weak self] _ in
@@ -470,7 +472,7 @@ final class TokamakListeners<Value>: @unchecked Sendable {
470
472
  return stream
471
473
  }
472
474
 
473
- func send(_ value: Value) {
475
+ public func send(_ value: Value) {
474
476
  lock.withLock {
475
477
  if holding && continuations.isEmpty {
476
478
  held.append(value)