@mikrojs/native 0.18.2 → 0.19.0-pr-352.20260829215534

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.
Files changed (43) hide show
  1. package/CMakeLists.txt +25 -2
  2. package/include/mikrojs/mem.h +4 -0
  3. package/include/mikrojs/ota_client.h +3 -3
  4. package/include/mikrojs/platform.h +8 -0
  5. package/include/mikrojs/private.h +28 -0
  6. package/package.json +4 -5
  7. package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
  8. package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
  9. package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
  10. package/runtime/http/helpers.ts +5 -0
  11. package/runtime/internal.d.ts +41 -2
  12. package/runtime/ota/client.ts +4 -0
  13. package/runtime/ota/ota.ts +4 -0
  14. package/runtime/ota/types.ts +75 -0
  15. package/runtime/schema/schema.ts +25 -3
  16. package/runtime/schema/types.ts +193 -15
  17. package/runtime/sys/types.ts +11 -1
  18. package/runtime/test/test.ts +6 -2
  19. package/runtime/wifi/types.ts +3 -0
  20. package/scripts/gen-schema-fixtures.js +317 -0
  21. package/src/mem.cpp +83 -37
  22. package/src/mik_app_config.cpp +9 -1
  23. package/src/mik_http_client.cpp +1357 -0
  24. package/src/mik_ota_client.cpp +3 -3
  25. package/src/mik_repl.cpp +44 -5
  26. package/src/mik_schema.cpp +892 -0
  27. package/src/mik_sys.cpp +9 -1
  28. package/src/mik_wifi_client.cpp +742 -0
  29. package/src/mikrojs.cpp +34 -8
  30. package/src/modules.cpp +66 -0
  31. package/dist/runtime/schema/core.d.ts +0 -115
  32. package/dist/runtime/schema/core.d.ts.map +0 -1
  33. package/dist/runtime/schema/core.js +0 -259
  34. package/dist/runtime/schema/core.js.map +0 -1
  35. package/dist/runtime/schema/shared.d.ts +0 -54
  36. package/dist/runtime/schema/shared.d.ts.map +0 -1
  37. package/dist/runtime/schema/shared.js +0 -489
  38. package/dist/runtime/schema/shared.js.map +0 -1
  39. package/runtime/http/native.ts +0 -160
  40. package/runtime/http/request.ts +0 -25
  41. package/runtime/schema/core.ts +0 -539
  42. package/runtime/schema/shared.ts +0 -494
  43. package/runtime/wifi/wifi.ts +0 -191
@@ -1,494 +0,0 @@
1
- /* Host-side helpers for config schemas, shared by the CLI and registries.
2
- * Not part of the mikro/schema device bundle: the device never validates a
3
- * schema AST (its manifest copy was written by the CLI) and never derives an
4
- * overlay. Imports core.ts only, so hosts load it without resolving mikro/*
5
- * builtins; results are plain {ok} shapes for the same reason. */
6
-
7
- import {applyDefaults, type ObjectSchema, type Schema, SchemaError, validate} from './core.js'
8
-
9
- export type SchemaCheck<T> = {ok: true; value: T} | {ok: false; error: SchemaError}
10
-
11
- const MAX_DEPTH = 8
12
-
13
- const KINDS = new Set([
14
- 'string',
15
- 'number',
16
- 'boolean',
17
- 'unknown',
18
- 'literal',
19
- 'array',
20
- 'object',
21
- 'optional',
22
- 'tuple',
23
- 'union',
24
- 'taggedUnion',
25
- ])
26
-
27
- function fail(message: string, path: string) {
28
- return {ok: false as const, error: SchemaError.ValidationFailed(message, path)}
29
- }
30
-
31
- /* JSON.parse creates `__proto__` as an own key, and downstream walks assign
32
- * overlay values through `out[key] = …` — with these keys that assignment
33
- * writes the prototype, not a property. The AST is untrusted, so refuse them
34
- * outright. */
35
- const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype'])
36
-
37
- /**
38
- * Validates an untrusted serialized schema AST as a config schema: well-formed
39
- * nodes only, an object at the root, no `unknown()`, no `optional()` around an
40
- * object or array (an overlay needs every absence to mean exactly one thing),
41
- * defaults that match their own node, and nesting of at most 8 levels.
42
- * The size cap is the caller's, since only the caller sees encoded bytes.
43
- */
44
- export function parseConfigSchema(value: unknown): SchemaCheck<Schema> {
45
- if (!isPlainObject(value) || (value as {kind?: unknown}).kind !== 'object') {
46
- return fail('config schema root must be an object()', '')
47
- }
48
- const result = walk(value, '', 1)
49
- if (result !== null) return result
50
- return {ok: true, value: value as unknown as Schema}
51
- }
52
-
53
- function isPlainObject(value: unknown): value is Record<string, unknown> {
54
- return typeof value === 'object' && value !== null && !Array.isArray(value)
55
- }
56
-
57
- /* A default below a wholesale unit never applies: applyDefaults replaces the
58
- * unit whole, so only the unit's own whole-value default fills anything. The
59
- * constructors reject it where it is written (core.ts, rejectInnerDefaults);
60
- * a schema that arrived as JSON ran none of them, so the same rule is enforced
61
- * here. The whole subtree is walked, not just the plain-object path, because
62
- * nothing checked a nested unit's contents on the way in. Structure is already
63
- * validated by `walk`, so a non-node here is left for it to report. */
64
- function rejectInnerDefaults(
65
- value: unknown,
66
- path: string,
67
- unit: string,
68
- self: string,
69
- ): ReturnType<typeof fail> | null {
70
- if (!isPlainObject(value)) return null
71
- if (value.default !== undefined) {
72
- return fail(
73
- `a default under ${unit} never applies; give ${self} itself a whole-value default instead`,
74
- path,
75
- )
76
- }
77
- const children: [unknown, string][] = []
78
- for (const key of ['shape', 'branches'] as const) {
79
- const map = value[key]
80
- if (isPlainObject(map)) {
81
- for (const name of Object.keys(map)) children.push([map[name], `${path}.${name}`])
82
- }
83
- }
84
- for (const key of ['element', 'inner'] as const) {
85
- if (value[key] !== undefined) children.push([value[key], path])
86
- }
87
- for (const key of ['elements', 'members'] as const) {
88
- const items = value[key]
89
- if (Array.isArray(items)) {
90
- for (let i = 0; i < items.length; i++) children.push([items[i], `${path}[${i}]`])
91
- }
92
- }
93
- for (const [child, childPath] of children) {
94
- const result = rejectInnerDefaults(child, childPath, unit, self)
95
- if (result !== null) return result
96
- }
97
- return null
98
- }
99
-
100
- function walk(value: unknown, path: string, depth: number): ReturnType<typeof fail> | null {
101
- if (depth > MAX_DEPTH) return fail(`nesting deeper than ${MAX_DEPTH} levels`, path)
102
- if (!isPlainObject(value)) return fail('expected a schema node', path)
103
- const node = value
104
- const kind = node.kind
105
- if (typeof kind !== 'string' || !KINDS.has(kind)) {
106
- return fail(`unknown schema kind ${JSON.stringify(kind)}`, path)
107
- }
108
- if (kind === 'unknown') return fail('unknown() is not allowed in a config schema', path)
109
-
110
- switch (kind) {
111
- case 'literal': {
112
- const t = typeof node.value
113
- if (t !== 'string' && t !== 'number' && t !== 'boolean') {
114
- return fail('literal value must be a primitive', path)
115
- }
116
- break
117
- }
118
- case 'array': {
119
- const result = walk(node.element, `${path}.element`, depth + 1)
120
- if (result !== null) return result
121
- const inner = rejectInnerDefaults(node.element, `${path}.element`, 'an array', 'the array')
122
- if (inner !== null) return inner
123
- break
124
- }
125
- case 'object': {
126
- if (!isPlainObject(node.shape)) return fail('object shape must be a map', path)
127
- if (node.default !== undefined) return fail('object() cannot carry a default', path)
128
- for (const key of Object.keys(node.shape)) {
129
- if (UNSAFE_KEYS.has(key)) return fail(`unsafe field name ${JSON.stringify(key)}`, path)
130
- const result = walk(node.shape[key], `${path}.${key}`, depth + 1)
131
- if (result !== null) return result
132
- }
133
- break
134
- }
135
- case 'optional': {
136
- const inner = node.inner
137
- if (isPlainObject(inner) && (inner.kind === 'object' || inner.kind === 'array')) {
138
- return fail(`optional() cannot wrap an ${inner.kind} in a config schema`, path)
139
- }
140
- if (node.default !== undefined) {
141
- // The wrapper never carries one (the constructor forbids it); in an
142
- // untrusted AST it would validate here and then be ignored by
143
- // applyDefaults, a default that silently never applies.
144
- return fail('optional() cannot carry a default', path)
145
- }
146
- if (isPlainObject(inner) && inner.default !== undefined) {
147
- return fail('optional() cannot wrap a schema with a default', path)
148
- }
149
- const result = walk(inner, path, depth + 1)
150
- if (result !== null) return result
151
- break
152
- }
153
- case 'tuple':
154
- case 'union': {
155
- const items = kind === 'tuple' ? node.elements : node.members
156
- if (!Array.isArray(items)) return fail(`${kind} items must be an array`, path)
157
- if (kind === 'union' && items.length === 0) {
158
- // Unsatisfiable: no value matches an empty union, so it would only
159
- // fail later, at serve, one device at a time.
160
- return fail('union needs at least one member', path)
161
- }
162
- for (let i = 0; i < items.length; i++) {
163
- const result = walk(items[i], `${path}[${i}]`, depth + 1)
164
- if (result !== null) return result
165
- const unit = kind === 'tuple' ? 'a tuple' : 'a union'
166
- const inner = rejectInnerDefaults(items[i], `${path}[${i}]`, unit, `the ${kind}`)
167
- if (inner !== null) return inner
168
- }
169
- break
170
- }
171
- case 'taggedUnion': {
172
- if (typeof node.key !== 'string') return fail('taggedUnion key must be a string', path)
173
- if (!isPlainObject(node.branches)) return fail('taggedUnion branches must be a map', path)
174
- if (Object.keys(node.branches).length === 0) {
175
- // Unsatisfiable, like an empty union: it would only fail at serve.
176
- return fail('taggedUnion needs at least one branch', path)
177
- }
178
- for (const tag of Object.keys(node.branches)) {
179
- if (UNSAFE_KEYS.has(tag)) return fail(`unsafe branch tag ${JSON.stringify(tag)}`, path)
180
- const branch = node.branches[tag]
181
- if (!isPlainObject(branch) || branch.kind !== 'object') {
182
- return fail('taggedUnion branch must be an object()', `${path}.${tag}`)
183
- }
184
- const result = walk(branch, `${path}.${tag}`, depth + 1)
185
- if (result !== null) return result
186
- const inner = rejectInnerDefaults(branch, `${path}.${tag}`, 'a taggedUnion', 'the union')
187
- if (inner !== null) return inner
188
- }
189
- break
190
- }
191
- }
192
-
193
- if (node.default !== undefined) {
194
- const check = validate(node as unknown as Schema, node.default, '')
195
- if (check !== null) {
196
- return fail(`default does not match the schema: ${check.error.message}`, path)
197
- }
198
- }
199
- return null
200
- }
201
-
202
- /**
203
- * Derives the overlay to store or serve from operator-supplied values: drops
204
- * keys the schema does not know, strips values structurally equal to the
205
- * schema default, and prunes empty objects and arrays. Returns undefined when
206
- * nothing deviates from the defaults. Wholesale nodes (arrays, tuples, unions)
207
- * are compared and kept as units; nothing inside them is stripped.
208
- */
209
- export function deriveOverlay(schema: Schema, values: unknown): unknown {
210
- switch (schema.kind) {
211
- case 'object': {
212
- if (values === undefined) return undefined
213
- // A defined value of the wrong kind is kept, not dropped: dropping it
214
- // would derive a clean overlay from garbage, so a typo'd save would
215
- // succeed while storing nothing and rule 5 would never gate on it.
216
- // Kept, it fails the merge-validate that every caller runs next.
217
- if (!isPlainObject(values)) return values
218
- const out: Record<string, unknown> = {}
219
- for (const key of Object.keys(schema.shape)) {
220
- const field = schema.shape[key]!
221
- const value = values[key]
222
- if (value === undefined) continue
223
- const derived =
224
- field.kind === 'optional'
225
- ? deriveOverlay(field.inner, value)
226
- : deriveOverlay(field, value)
227
- if (derived !== undefined) out[key] = derived
228
- }
229
- return Object.keys(out).length > 0 ? out : undefined
230
- }
231
- case 'array': {
232
- if (values === undefined) return undefined
233
- // Wrong kind kept for the same reason as objects above; only a real
234
- // empty array prunes (emptiness is never a deliberate overlay state).
235
- if (!Array.isArray(values)) return values
236
- if (values.length === 0) return undefined
237
- if (schema.default !== undefined && structuralEquals(values, schema.default)) {
238
- return undefined
239
- }
240
- return stripDangerousKeys(values)
241
- }
242
- default: {
243
- const fallback = (schema as {default?: unknown}).default
244
- if (fallback !== undefined && structuralEquals(values, fallback)) return undefined
245
- return stripDangerousKeys(values)
246
- }
247
- }
248
- }
249
-
250
- /** Wholesale units travel with their unknown keys intact, but the three
251
- * prototype-writing names never survive into a stored or served value:
252
- * downstream walks and consumers assign through `out[key]`. */
253
- function stripDangerousKeys(value: unknown): unknown {
254
- if (Array.isArray(value)) return value.map(stripDangerousKeys)
255
- if (isPlainObject(value)) {
256
- const out: Record<string, unknown> = {}
257
- for (const key of Object.keys(value)) {
258
- if (UNSAFE_KEYS.has(key)) continue
259
- out[key] = stripDangerousKeys(value[key])
260
- }
261
- return out
262
- }
263
- return value
264
- }
265
-
266
- export function structuralEquals(a: unknown, b: unknown): boolean {
267
- if (a === b) return true
268
- if (Array.isArray(a) && Array.isArray(b)) {
269
- if (a.length !== b.length) return false
270
- for (let i = 0; i < a.length; i++) {
271
- if (!structuralEquals(a[i], b[i])) return false
272
- }
273
- return true
274
- }
275
- if (isPlainObject(a) && isPlainObject(b)) {
276
- const keysA = Object.keys(a)
277
- const keysB = Object.keys(b)
278
- if (keysA.length !== keysB.length) return false
279
- for (const key of keysA) {
280
- if (!Object.hasOwn(b, key) || !structuralEquals(a[key], b[key])) return false
281
- }
282
- return true
283
- }
284
- return false
285
- }
286
-
287
- /**
288
- * The effective config for an overlay: defaults filled in, then validated.
289
- * What a registry runs before serving and what `ota.config()` runs on read.
290
- */
291
- export function parseEffective(schema: Schema, overlay: unknown): SchemaCheck<unknown> {
292
- const effective = applyDefaults(schema, overlay)
293
- const result = validate(schema, effective, '')
294
- return result !== null ? result : {ok: true, value: effective}
295
- }
296
-
297
- /**
298
- * The partial defaults a schema materializes with no overrides: every field a
299
- * default covers, and nothing else. Unlike parseEffective it never fails on a
300
- * required defaultless field, it omits it. This is what pack bakes into the
301
- * manifest and what a device reads when it holds no served document.
302
- *
303
- * Plain objects compose, so a nested one is included only when defaults fill
304
- * it completely: a half-filled object would not validate, and the read type
305
- * makes that field optional anyway. Wholesale units (array, tuple, union,
306
- * taggedUnion) need a whole-value default on the node itself, matching
307
- * applyDefaults, where a default inside an element or branch is a form hint.
308
- * Optional fields rest on absence, so they are omitted too.
309
- */
310
- export function materializeDefaults(schema: Schema): Record<string, unknown> {
311
- return schema.kind === 'object' ? fillObject(schema).value : {}
312
- }
313
-
314
- /** What defaults cover in an object shape. `complete` (every field covered or
315
- * optional) is what makes a NESTED object safe to include. */
316
- function fillObject(node: ObjectSchema): {value: Record<string, unknown>; complete: boolean} {
317
- const out: Record<string, unknown> = {}
318
- let complete = true
319
- for (const key of Object.keys(node.shape)) {
320
- const field = node.shape[key]!
321
- if (field.kind === 'optional') continue
322
- const filled = fillField(field)
323
- if (filled === undefined) complete = false
324
- else out[key] = filled.value
325
- }
326
- return {value: out, complete}
327
- }
328
-
329
- /** The value defaults give one field, or undefined when nothing covers it. */
330
- function fillField(node: Schema): {value: unknown} | undefined {
331
- if (node.kind === 'object') {
332
- const filled = fillObject(node)
333
- return filled.complete ? {value: filled.value} : undefined
334
- }
335
- const fallback = (node as {default?: unknown}).default
336
- return fallback === undefined ? undefined : {value: fallback}
337
- }
338
-
339
- /** A node with its annotations removed, recursively, so two schemas can be
340
- * compared on structure alone. */
341
- function stripAnnotations(node: unknown): unknown {
342
- if (!isPlainObject(node)) return node
343
- const {default: _default, ...rest} = node
344
- const out: Record<string, unknown> = {}
345
- for (const key of Object.keys(rest)) {
346
- const value = rest[key]
347
- if (key === 'shape' || key === 'branches') {
348
- const map = value as Record<string, unknown>
349
- const stripped: Record<string, unknown> = {}
350
- for (const k of Object.keys(map)) stripped[k] = stripAnnotations(map[k])
351
- out[key] = stripped
352
- } else if (key === 'element' || key === 'inner') {
353
- out[key] = stripAnnotations(value)
354
- } else if (key === 'elements' || key === 'members') {
355
- out[key] = (value as unknown[]).map(stripAnnotations)
356
- } else {
357
- out[key] = value
358
- }
359
- }
360
- return out
361
- }
362
-
363
- /** required / defaulted / optional, per the spec's scalar-leaf classes.
364
- * Containers have no class; objects report 'object' so the walk descends. */
365
- function leafClass(node: Schema): 'object' | 'optional' | 'defaulted' | 'required' | 'array' {
366
- if (node.kind === 'optional') return 'optional'
367
- if (node.kind === 'object') return 'object'
368
- if (node.kind === 'array') return 'array'
369
- return (node as {default?: unknown}).default !== undefined ? 'defaulted' : 'required'
370
- }
371
-
372
- /** Every required scalar leaf reachable in `node`, for reporting an added
373
- * subtree that will gate offers until an operator supplies values. */
374
- function requiredLeaves(node: Schema, path: string, out: string[]): void {
375
- if (node.kind === 'object') {
376
- for (const key of Object.keys(node.shape)) {
377
- requiredLeaves(node.shape[key]!, `${path}.${key}`, out)
378
- }
379
- return
380
- }
381
- if (leafClass(node) === 'required') out.push(path)
382
- }
383
-
384
- /**
385
- * Human-readable warnings for what changed between two releases' config
386
- * schemas, per the spec's change taxonomy (registry-spec.md, "Schema changes
387
- * between releases"). Safe changes (new defaulted or optional fields, added
388
- * union members, loosened requirements) produce nothing. "requires an
389
- * operator" lines gate offers under rule 5 until someone supplies or fixes a
390
- * value; "note" lines are compatible but worth telling the operator about.
391
- */
392
- export function diffConfigSchemas(previous: Schema, next: Schema): string[] {
393
- const warnings: string[] = []
394
-
395
- function walk(prev: Schema, curr: Schema, path: string): void {
396
- const prevInner = prev.kind === 'optional' ? prev.inner : prev
397
- const currInner = curr.kind === 'optional' ? curr.inner : curr
398
-
399
- if (prevInner.kind === 'object' && currInner.kind === 'object') {
400
- const prevShape = prevInner.shape
401
- const currShape = currInner.shape
402
- for (const key of Object.keys(currShape)) {
403
- const fieldPath = `${path}.${key}`
404
- if (Object.hasOwn(prevShape, key)) {
405
- walk(prevShape[key]!, currShape[key]!, fieldPath)
406
- } else {
407
- const added: string[] = []
408
- requiredLeaves(currShape[key]!, fieldPath, added)
409
- for (const leaf of added) {
410
- warnings.push(
411
- `requires an operator: new required field ${leaf} (devices are not offered ` +
412
- `this release until a value is set)`,
413
- )
414
- }
415
- }
416
- }
417
- for (const key of Object.keys(prevShape)) {
418
- if (!Object.hasOwn(currShape, key)) {
419
- warnings.push(
420
- `note: removed field ${path}.${key} (stored overrides for it stay, but are no ` +
421
- `longer served)`,
422
- )
423
- }
424
- }
425
- return
426
- }
427
-
428
- if (prevInner.kind === 'union' && currInner.kind === 'union') {
429
- // Member sets, not wholesale structure: adding a member (the common
430
- // safe widening) must not read as a type change. Only removals can
431
- // invalidate a stored override.
432
- const removed = prevInner.members.filter(
433
- (prevMember) =>
434
- !currInner.members.some((currMember) =>
435
- structuralEquals(stripAnnotations(prevMember), stripAnnotations(currMember)),
436
- ),
437
- )
438
- if (removed.length > 0) {
439
- warnings.push(
440
- `requires an operator: ${path} removed ${removed.length} union member(s) ` +
441
- `(stored overrides using them no longer validate)`,
442
- )
443
- }
444
- } else if (
445
- prevInner.kind === 'taggedUnion' &&
446
- currInner.kind === 'taggedUnion' &&
447
- prevInner.key === currInner.key
448
- ) {
449
- // Same idea per branch: added branches are safe, removed or reshaped
450
- // ones can strand a stored override.
451
- for (const tag of Object.keys(prevInner.branches)) {
452
- const prevBranch = prevInner.branches[tag]!
453
- const currBranch = currInner.branches[tag]
454
- if (currBranch === undefined || !Object.hasOwn(currInner.branches, tag)) {
455
- warnings.push(
456
- `requires an operator: ${path} removed branch ${JSON.stringify(tag)} ` +
457
- `(stored overrides using it no longer validate)`,
458
- )
459
- } else if (!structuralEquals(stripAnnotations(prevBranch), stripAnnotations(currBranch))) {
460
- warnings.push(
461
- `requires an operator: ${path}.${tag} changed type ` +
462
- `(stored overrides may no longer validate)`,
463
- )
464
- }
465
- }
466
- } else if (!structuralEquals(stripAnnotations(prevInner), stripAnnotations(currInner))) {
467
- warnings.push(
468
- `requires an operator: ${path} changed type (stored overrides may no longer validate)`,
469
- )
470
- return
471
- }
472
-
473
- const prevClass = leafClass(prev)
474
- const currClass = leafClass(curr)
475
- if (currClass === 'required' && prevClass !== 'required') {
476
- warnings.push(
477
- `requires an operator: ${path} is now required (devices without a value are not ` +
478
- `offered this release)`,
479
- )
480
- }
481
- const prevDefault = (prevInner as {default?: unknown}).default
482
- const currDefault = (currInner as {default?: unknown}).default
483
- // A removed default is the required-transition above, not also a default
484
- // change.
485
- if (currDefault !== undefined && !structuralEquals(prevDefault, currDefault)) {
486
- warnings.push(
487
- `note: default of ${path} changed (takes effect on every device without an override)`,
488
- )
489
- }
490
- }
491
-
492
- walk(previous, next, '')
493
- return warnings
494
- }
@@ -1,191 +0,0 @@
1
- import {lazyEvent} from 'mikro/observable/lazy'
2
- import {err, ok} from 'mikro/result'
3
- import {Wifi as NativeWifi} from 'native:mikro/wifi'
4
-
5
- import type {Result} from '../result/types.js'
6
- import type {
7
- ApStartOptions,
8
- ApStationInfo,
9
- IpConfig,
10
- PowerSaveMode,
11
- ScanOptions,
12
- ScanResult,
13
- StaticIpConfig,
14
- Wifi,
15
- WifiAp,
16
- WifiConnectionInfo,
17
- WifiConnectOptions,
18
- WifiCountryCode,
19
- WifiDisconnectOptions,
20
- WifiDisconnectReason,
21
- WifiError,
22
- WifiStatus,
23
- } from './types.js'
24
-
25
- const WifiStatusCodes: Record<WifiStatus, number> = {
26
- STOPPED: 254,
27
- IDLE: 0,
28
- NO_SSID_AVAIL: 1,
29
- SCAN_COMPLETED: 2,
30
- CONNECTED: 3,
31
- CONNECT_FAILED: 4,
32
- CONNECTION_LOST: 5,
33
- DISCONNECTED: 6,
34
- }
35
-
36
- const StatusFromCode = new Map<number, WifiStatus>(
37
- Object.entries(WifiStatusCodes).map(([k, v]) => [v, k as WifiStatus]),
38
- )
39
-
40
- const native = new NativeWifi()
41
-
42
- const ap: WifiAp = {
43
- start(options: ApStartOptions): Result<void, WifiError> {
44
- return native.apStart(options as Parameters<typeof native.apStart>[0])
45
- },
46
-
47
- stop(): Result<void, WifiError> {
48
- return native.apStop()
49
- },
50
-
51
- get isActive(): boolean {
52
- return native.apIsActive()
53
- },
54
-
55
- get ip(): string | undefined {
56
- return native.apIp()
57
- },
58
-
59
- get stations(): ApStationInfo[] {
60
- return native.apStations()
61
- },
62
-
63
- deauthStation(mac: string): Result<void, WifiError> {
64
- return native.apDeauthStation(mac)
65
- },
66
-
67
- get inactiveTimeout(): number {
68
- const result = native.apGetInactiveTimeout()
69
- return result.ok ? result.value : 0
70
- },
71
-
72
- set inactiveTimeout(seconds: number) {
73
- native.apSetInactiveTimeout(seconds)
74
- },
75
-
76
- onStationConnect: lazyEvent<ApStationInfo>(native, 'station-connect'),
77
- onStationDisconnect: lazyEvent<ApStationInfo>(native, 'station-disconnect'),
78
- }
79
-
80
- const MAX_CONNECT_RETRIES = 5
81
- const RETRY_DELAY_MS = 2000
82
-
83
- const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms))
84
-
85
- const wifi: Wifi = {
86
- async connect(options: WifiConnectOptions): Promise<Result<WifiConnectionInfo, WifiError>> {
87
- const {ssid, passphrase, txPower} = options
88
- if (txPower !== undefined) {
89
- const txResult = native.setTxPower(txPower)
90
- if (!txResult.ok) return txResult
91
- }
92
- for (let attempt = 0; attempt <= MAX_CONNECT_RETRIES; attempt++) {
93
- const startResult = native.connect(ssid, passphrase)
94
- if (!startResult.ok) return startResult
95
-
96
- const asyncResult = await startResult.value
97
- if (asyncResult.ok) return ok(asyncResult.value as WifiConnectionInfo)
98
-
99
- if (attempt === MAX_CONNECT_RETRIES) return asyncResult
100
-
101
- // eslint-disable-next-line no-console
102
- console.warn(`WiFi connect failed, retrying in ${RETRY_DELAY_MS}ms…`)
103
- // Keep the radio up between retries so the next attempt reconnects fast.
104
- native.disconnect(false)
105
- await sleep(RETRY_DELAY_MS)
106
- }
107
- return err({name: 'ConnectFailed' as const, message: 'max retries exceeded'})
108
- },
109
-
110
- disconnect(options?: WifiDisconnectOptions): Result<void, WifiError> {
111
- return native.disconnect(options?.shutdown)
112
- },
113
-
114
- rssi(): Result<number, WifiError> {
115
- return native.rssi()
116
- },
117
-
118
- ip(): string | undefined {
119
- const ip = native.ip()
120
- return ip === '0.0.0.0' ? undefined : ip
121
- },
122
-
123
- status(): WifiStatus {
124
- const code = native.status()
125
- return StatusFromCode.get(code) ?? 'DISCONNECTED'
126
- },
127
-
128
- get isConnected(): boolean {
129
- return native.status() === WifiStatusCodes.CONNECTED
130
- },
131
-
132
- async scan(opts?: ScanOptions): Promise<Result<ScanResult[], WifiError>> {
133
- const startResult = native.scan(opts)
134
- if (!startResult.ok) return startResult
135
-
136
- const asyncResult = await startResult.value
137
- if (!asyncResult.ok) return asyncResult
138
- return ok(asyncResult.value as ScanResult[])
139
- },
140
-
141
- onConnect: lazyEvent<WifiConnectionInfo>(native, 'connect'),
142
- onDisconnect: lazyEvent<WifiDisconnectReason>(native, 'disconnect'),
143
- onRssiLow: lazyEvent<number>(native, 'rssi-low'),
144
-
145
- get mac(): string {
146
- const result = native.mac()
147
- return result.ok ? result.value : ''
148
- },
149
-
150
- get hostname(): string | undefined {
151
- return native.getHostname()
152
- },
153
-
154
- ipConfig: ((
155
- opts?: StaticIpConfig,
156
- ): Result<IpConfig | undefined, WifiError> | Result<void, WifiError> => {
157
- if (opts === undefined) {
158
- return native.getIpConfig()
159
- }
160
- return native.setIpConfig(opts)
161
- }) as Wifi['ipConfig'],
162
-
163
- ap,
164
-
165
- get txPower(): number {
166
- const result = native.getTxPower()
167
- return result.ok ? result.value : 0
168
- },
169
-
170
- get rssiThreshold(): number {
171
- return native.getRssiThreshold()
172
- },
173
-
174
- set rssiThreshold(value: number) {
175
- native.setRssiThreshold(value)
176
- },
177
-
178
- get powerSave(): PowerSaveMode {
179
- return native.getPowerSave() as PowerSaveMode
180
- },
181
-
182
- set powerSave(mode: PowerSaveMode) {
183
- native.setPowerSave(mode)
184
- },
185
-
186
- get country(): WifiCountryCode | undefined {
187
- return native.getCountry() as WifiCountryCode | undefined
188
- },
189
- }
190
-
191
- export {wifi}