@mikrojs/native 0.22.0 → 0.23.0
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.
- package/CMakeLists.txt +19 -0
- package/dist/runtime/result/native-result.node-shim.d.ts +2 -0
- package/dist/runtime/result/native-result.node-shim.d.ts.map +1 -1
- package/dist/runtime/result/native-result.node-shim.js +57 -0
- package/dist/runtime/result/native-result.node-shim.js.map +1 -1
- package/dist/runtime/result/types.d.ts +33 -0
- package/dist/runtime/result/types.d.ts.map +1 -1
- package/include/mikrojs/app_store.h +16 -0
- package/include/mikrojs/ble_port.h +50 -0
- package/include/mikrojs/build_install.h +36 -0
- package/include/mikrojs/device_store.h +84 -0
- package/include/mikrojs/http_native.h +52 -0
- package/include/mikrojs/mikrojs.h +19 -9
- package/include/mikrojs/ota.h +75 -0
- package/include/mikrojs/ota_env.h +1 -1
- package/include/mikrojs/platform.h +29 -0
- package/include/mikrojs/private.h +4 -1
- package/include/mikrojs/utils.h +5 -0
- package/package.json +4 -4
- package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
- package/runtime/http/server-impl.ts +5 -60
- package/runtime/http/server.ts +0 -1
- package/runtime/internal.d.ts +2 -0
- package/runtime/result/native-result.node-shim.ts +60 -0
- package/runtime/result/result.ts +1 -1
- package/runtime/result/types.ts +61 -0
- package/runtime/sys/sys.ts +1 -2
- package/src/mik_app_store.cpp +32 -0
- package/src/mik_ble.cpp +1573 -0
- package/src/mik_build_install.cpp +785 -0
- package/src/mik_config.cpp +330 -0
- package/src/mik_console.cpp +15 -24
- package/src/mik_deploy.cpp +759 -0
- package/src/mik_ota.cpp +1403 -0
- package/src/mik_ota_client_module.cpp +1142 -0
- package/src/mik_ota_env.cpp +318 -0
- package/src/mik_repl.cpp +12 -4
- package/src/mik_result.cpp +159 -0
- package/src/mik_sys.cpp +25 -51
- package/src/mik_test_supervisor.cpp +151 -0
- package/src/mik_watchdog.cpp +4 -5
- package/src/mik_wifi_client.cpp +1 -2
- package/src/mikrojs.cpp +15 -6
- package/src/modules.cpp +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikrojs/native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.23.0",
|
|
4
4
|
"description": "Mikro.js C++ runtime library and Node.js native addon",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"esp32",
|
|
@@ -86,14 +86,14 @@
|
|
|
86
86
|
"./runtime/wifi/types": "./runtime/wifi/types.ts"
|
|
87
87
|
},
|
|
88
88
|
"dependencies": {
|
|
89
|
-
"@mikrojs/quickjs": "0.
|
|
89
|
+
"@mikrojs/quickjs": "0.23.0",
|
|
90
90
|
"cmake-js": "^8.0.0",
|
|
91
91
|
"node-addon-api": "^8.9.2",
|
|
92
92
|
"node-gyp-build": "^4.8.4"
|
|
93
93
|
},
|
|
94
94
|
"devDependencies": {
|
|
95
|
-
"@mikrojs/registry": "0.
|
|
96
|
-
"@mikrojs/schema": "0.
|
|
95
|
+
"@mikrojs/registry": "0.23.0",
|
|
96
|
+
"@mikrojs/schema": "0.23.0",
|
|
97
97
|
"@swc/core": "^1.16.2",
|
|
98
98
|
"@types/node": "^24.12.2",
|
|
99
99
|
"esbuild": "^0.28.2",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -54,22 +54,9 @@ export interface ServerResponse {
|
|
|
54
54
|
|
|
55
55
|
export type Handler = (req: ServerRequest) => ServerResponse | Promise<ServerResponse>
|
|
56
56
|
|
|
57
|
-
/**
|
|
58
|
-
* Called when a handler panics (throws or rejects) — an unexpected failure, not
|
|
59
|
-
* the place for normal error handling. Receives the thrown value and the
|
|
60
|
-
* request. Return a `ServerResponse` to send it; return nothing to fall back to
|
|
61
|
-
* a plain 500. If the hook itself throws, the 500 fallback is used.
|
|
62
|
-
*/
|
|
63
|
-
export type PanicHandler = (
|
|
64
|
-
error: unknown,
|
|
65
|
-
req: ServerRequest,
|
|
66
|
-
) => ServerResponse | undefined | void | Promise<ServerResponse | undefined | void>
|
|
67
|
-
|
|
68
57
|
export interface ServerOptions {
|
|
69
58
|
/** Cap on the buffered request body, in bytes (default 16 KiB). */
|
|
70
59
|
maxBodySize?: number
|
|
71
|
-
/** Hook invoked when a handler panics (throws). See {@link PanicHandler}. */
|
|
72
|
-
onPanic?: PanicHandler
|
|
73
60
|
}
|
|
74
61
|
|
|
75
62
|
export interface Server {
|
|
@@ -201,10 +188,6 @@ async function streamResponse(
|
|
|
201
188
|
// means the client disconnected, so stop pulling from the generator.
|
|
202
189
|
if (!(await native.respondChunk(id, bytes))) break
|
|
203
190
|
}
|
|
204
|
-
} catch {
|
|
205
|
-
// The generator threw after status + headers were already sent, so the
|
|
206
|
-
// status can't change. End the response below to truncate it cleanly; the
|
|
207
|
-
// server keeps running.
|
|
208
191
|
} finally {
|
|
209
192
|
// Only end a stream we actually started, so the worker is always released
|
|
210
193
|
// exactly once.
|
|
@@ -230,58 +213,20 @@ async function sendResponse(
|
|
|
230
213
|
}
|
|
231
214
|
}
|
|
232
215
|
|
|
233
|
-
const FALLBACK_500: ServerResponse = {status: 500, body: 'Internal Server Error'}
|
|
234
|
-
|
|
235
|
-
// A handler that panics (throws) is isolated to its one request so the server
|
|
236
|
-
// keeps serving: run the optional onPanic hook for a custom response (and
|
|
237
|
-
// server-side visibility), else send a plain 500. The httpd worker is blocked
|
|
238
|
-
// until we respond, so every branch must produce exactly one response.
|
|
239
|
-
async function handlePanic(
|
|
240
|
-
onPanic: PanicHandler | undefined,
|
|
241
|
-
error: unknown,
|
|
242
|
-
req: ServerRequest,
|
|
243
|
-
): Promise<ServerResponse> {
|
|
244
|
-
if (!onPanic) return FALLBACK_500
|
|
245
|
-
try {
|
|
246
|
-
return (await onPanic(error, req)) ?? FALLBACK_500
|
|
247
|
-
} catch {
|
|
248
|
-
// The hook itself threw; don't let its bug hang the worker.
|
|
249
|
-
return FALLBACK_500
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
216
|
async function serveLoop(
|
|
254
217
|
native: NativeHttpServerModule,
|
|
255
218
|
handler: Handler,
|
|
256
219
|
isRunning: () => boolean,
|
|
257
220
|
cap: number,
|
|
258
|
-
onPanic: PanicHandler | undefined,
|
|
259
221
|
): Promise<void> {
|
|
260
222
|
while (isRunning()) {
|
|
261
|
-
|
|
262
|
-
try {
|
|
263
|
-
desc = await native.nextRequest()
|
|
264
|
-
} catch {
|
|
265
|
-
break
|
|
266
|
-
}
|
|
223
|
+
const desc = await native.nextRequest()
|
|
267
224
|
if ('closed' in desc) break
|
|
268
225
|
|
|
269
226
|
const req = makeServerRequest(native, desc, cap)
|
|
270
|
-
//
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
try {
|
|
274
|
-
let response: ServerResponse
|
|
275
|
-
try {
|
|
276
|
-
response = await handler(req)
|
|
277
|
-
} catch (e) {
|
|
278
|
-
response = await handlePanic(onPanic, e, req)
|
|
279
|
-
}
|
|
280
|
-
await sendResponse(native, desc.id, response)
|
|
281
|
-
} catch {
|
|
282
|
-
// Sending the response failed; the request is already past the point
|
|
283
|
-
// where we can change it. Keep serving the next request.
|
|
284
|
-
}
|
|
227
|
+
// A throw from the handler or its body generator is a panic, like a throw
|
|
228
|
+
// anywhere else: it leaves this loop as an unhandled rejection.
|
|
229
|
+
await sendResponse(native, desc.id, await handler(req))
|
|
285
230
|
}
|
|
286
231
|
}
|
|
287
232
|
|
|
@@ -295,7 +240,7 @@ export function createServerFromNative(native: NativeHttpServerModule): CreateSe
|
|
|
295
240
|
const started = native.start(port, opts.maxBodySize)
|
|
296
241
|
if (!started.ok) return started
|
|
297
242
|
running = true
|
|
298
|
-
void serveLoop(native, handler, () => running, cap
|
|
243
|
+
void serveLoop(native, handler, () => running, cap)
|
|
299
244
|
return ok()
|
|
300
245
|
},
|
|
301
246
|
close() {
|
package/runtime/http/server.ts
CHANGED
package/runtime/internal.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ declare module 'native:mikro/result' {
|
|
|
59
59
|
export function ok(): OkResult<void>
|
|
60
60
|
export function ok<T>(value: T): OkResult<T>
|
|
61
61
|
export function err<E>(error: E): ErrResult<E>
|
|
62
|
+
export {safe, tryCall} from '@mikrojs/native/runtime/result/types'
|
|
62
63
|
}
|
|
63
64
|
|
|
64
65
|
declare module 'native:mikro/observable' {
|
|
@@ -92,6 +93,7 @@ declare module 'native:mikro/sys' {
|
|
|
92
93
|
export function setTime(millisSinceEpoch: number): void
|
|
93
94
|
export function uptime(): {boot: number; rtc: number}
|
|
94
95
|
export function restart(): never
|
|
96
|
+
export function getWakeupCause(): string
|
|
95
97
|
export const version: string
|
|
96
98
|
export const board: {
|
|
97
99
|
name: string
|
|
@@ -36,6 +36,66 @@ export function ok(value?: unknown): unknown {
|
|
|
36
36
|
return r
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
// atob/btoa are untyped in this tsconfig
|
|
40
|
+
const g = globalThis as unknown as Record<string, unknown>
|
|
41
|
+
const safeBuiltins: unknown[] = [
|
|
42
|
+
JSON.parse,
|
|
43
|
+
JSON.stringify,
|
|
44
|
+
g.atob,
|
|
45
|
+
g.btoa,
|
|
46
|
+
decodeURI,
|
|
47
|
+
decodeURIComponent,
|
|
48
|
+
encodeURI,
|
|
49
|
+
encodeURIComponent,
|
|
50
|
+
RegExp,
|
|
51
|
+
String.fromCodePoint,
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
export function safe(fn: (...args: unknown[]) => unknown, ...args: unknown[]): unknown {
|
|
55
|
+
if (!safeBuiltins.includes(fn)) {
|
|
56
|
+
throw new TypeError(
|
|
57
|
+
'safe: expected one of JSON.parse, JSON.stringify, atob, btoa, decodeURI, ' +
|
|
58
|
+
'decodeURIComponent, encodeURI, encodeURIComponent, RegExp, String.fromCodePoint',
|
|
59
|
+
)
|
|
60
|
+
}
|
|
61
|
+
const name =
|
|
62
|
+
fn === JSON.parse
|
|
63
|
+
? 'JSON.parse reviver'
|
|
64
|
+
: fn === JSON.stringify
|
|
65
|
+
? 'JSON.stringify replacer'
|
|
66
|
+
: ''
|
|
67
|
+
const callArgs = name
|
|
68
|
+
? args.slice(0, 3).map((a, i) =>
|
|
69
|
+
i === 1 && typeof a === 'function'
|
|
70
|
+
? function (this: unknown, ...cbArgs: unknown[]) {
|
|
71
|
+
try {
|
|
72
|
+
return a.apply(this, cbArgs)
|
|
73
|
+
} catch (cause) {
|
|
74
|
+
throw new Error(`${name} threw`, {cause})
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
: a,
|
|
78
|
+
)
|
|
79
|
+
: args
|
|
80
|
+
try {
|
|
81
|
+
return ok(fn(...callArgs))
|
|
82
|
+
} catch (e) {
|
|
83
|
+
return err(e)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function tryCall(fn: (() => unknown) | Promise<unknown>): unknown {
|
|
88
|
+
if (fn instanceof Promise) return fn.then(ok, err)
|
|
89
|
+
if (typeof fn !== 'function') throw new TypeError('tryCall: expected a function or a promise')
|
|
90
|
+
let value: unknown
|
|
91
|
+
try {
|
|
92
|
+
value = fn()
|
|
93
|
+
} catch (e) {
|
|
94
|
+
return err(e)
|
|
95
|
+
}
|
|
96
|
+
return value instanceof Promise ? value.then(ok, err) : ok(value)
|
|
97
|
+
}
|
|
98
|
+
|
|
39
99
|
export function err(error: unknown): unknown {
|
|
40
100
|
const r = Object.create(proto)
|
|
41
101
|
r.ok = false
|
package/runtime/result/result.ts
CHANGED
package/runtime/result/types.ts
CHANGED
|
@@ -37,6 +37,67 @@ export declare function ok<T>(value: T): OkResult<T>
|
|
|
37
37
|
|
|
38
38
|
export declare function err<E>(error: E): ErrResult<E>
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Call a builtin that throws on bad input and return the outcome as a Result:
|
|
42
|
+
* `ok(value)`, or `err(error)` with what it threw. Synchronous.
|
|
43
|
+
*
|
|
44
|
+
* `fn` must be one of `JSON.parse`, `JSON.stringify`, `atob`, `btoa`,
|
|
45
|
+
* `decodeURI`, `decodeURIComponent`, `encodeURI`, `encodeURIComponent`,
|
|
46
|
+
* `RegExp` or `String.fromCodePoint`. Any other function is a TypeError.
|
|
47
|
+
* If a reviver or replacer throws, the error is an `Error` whose `cause` is
|
|
48
|
+
* what it threw, so a bug in it isn't mistaken for bad input.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const body = safe(JSON.parse, text)
|
|
52
|
+
* if (!body.ok) return err(new Error('invalid body', {cause: body.error}))
|
|
53
|
+
*/
|
|
54
|
+
export declare function safe(
|
|
55
|
+
fn: RegExpConstructor,
|
|
56
|
+
pattern: string | RegExp,
|
|
57
|
+
flags?: string,
|
|
58
|
+
): Result<RegExp, Error>
|
|
59
|
+
export declare function safe<A extends unknown[], R>(
|
|
60
|
+
fn: (...args: A) => R,
|
|
61
|
+
...args: A
|
|
62
|
+
): Result<unknown extends R ? unknown : R, Error>
|
|
63
|
+
// After the generic one, so other builtins never match these: inference from an
|
|
64
|
+
// overloaded function sees only its last overload, which lacks the callback form
|
|
65
|
+
export declare function safe(
|
|
66
|
+
fn: typeof JSON.parse,
|
|
67
|
+
text: string,
|
|
68
|
+
reviver?: (this: any, key: string, value: any) => any,
|
|
69
|
+
): Result<unknown, Error>
|
|
70
|
+
export declare function safe(
|
|
71
|
+
fn: typeof JSON.stringify,
|
|
72
|
+
value: unknown,
|
|
73
|
+
replacer?: ((this: any, key: string, value: any) => any) | (number | string)[] | null,
|
|
74
|
+
space?: string | number,
|
|
75
|
+
): Result<string, Error>
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Call `fn` and return the outcome as a Result: `ok(value)`, or `err(error)`
|
|
79
|
+
* with what it threw. For code that throws and that {@link safe} doesn't
|
|
80
|
+
* cover, such as a library.
|
|
81
|
+
*
|
|
82
|
+
* Given a promise, or a `fn` that returns one, it returns a promise of a
|
|
83
|
+
* Result that never rejects. Passing the promise directly misses a throw that
|
|
84
|
+
* happens before the promise exists; the callback form catches both.
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* const doc = tryCall(() => yaml.parse(text))
|
|
88
|
+
* const body = await tryCall(() => lib.download(url))
|
|
89
|
+
*/
|
|
90
|
+
export declare function tryCall<T>(promise: Promise<T>): Promise<Result<T, unknown>>
|
|
91
|
+
export declare function tryCall<T>(
|
|
92
|
+
fn: () => T,
|
|
93
|
+
): 0 extends 1 & T
|
|
94
|
+
? Result<any, unknown>
|
|
95
|
+
: [T] extends [never]
|
|
96
|
+
? Result<never, unknown>
|
|
97
|
+
: T extends Promise<infer U>
|
|
98
|
+
? Promise<Result<U, unknown>>
|
|
99
|
+
: Result<T, unknown>
|
|
100
|
+
|
|
40
101
|
export declare function matchError<E extends {name: string}, R>(
|
|
41
102
|
error: E,
|
|
42
103
|
handlers: {[K in E['name']]: (error: Extract<E, {name: K}>) => R},
|
package/runtime/sys/sys.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {err, ok, PanicError, type Result} from 'mikro/result'
|
|
2
|
-
import {getWakeupCause as nativeGetWakeupCause} from 'native:mikro/sleep'
|
|
3
2
|
import * as native from 'native:mikro/sys'
|
|
4
3
|
|
|
5
4
|
import type {DeviceName} from './types.js'
|
|
@@ -80,7 +79,7 @@ export function restart(): never {
|
|
|
80
79
|
}
|
|
81
80
|
|
|
82
81
|
export function getWakeupCause(): string {
|
|
83
|
-
return
|
|
82
|
+
return native.getWakeupCause()
|
|
84
83
|
}
|
|
85
84
|
|
|
86
85
|
export function exit(_exitCode?: number): never {
|
package/src/mik_app_store.cpp
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
#include <dirent.h>
|
|
4
4
|
#include <errno.h>
|
|
5
5
|
#include <stdio.h>
|
|
6
|
+
#include <stdlib.h>
|
|
6
7
|
#include <string.h>
|
|
7
8
|
#include <sys/stat.h>
|
|
8
9
|
#include <unistd.h>
|
|
@@ -178,3 +179,34 @@ void mik__app_recover(const char* base) {
|
|
|
178
179
|
mik__rmdir_recursive(tmp);
|
|
179
180
|
}
|
|
180
181
|
}
|
|
182
|
+
|
|
183
|
+
long mik__file_size(const char* path) {
|
|
184
|
+
struct stat st;
|
|
185
|
+
if (stat(path, &st) != 0) return -1;
|
|
186
|
+
return (long)st.st_size;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
bool mik__fs_space(long* total, long* free_bytes) {
|
|
190
|
+
const MIKPlatform* p = MIK_GetPlatform();
|
|
191
|
+
size_t t = 0;
|
|
192
|
+
size_t u = 0;
|
|
193
|
+
if (!p || !p->get_fs_info || !p->get_fs_info("user", &t, &u)) return false;
|
|
194
|
+
*total = (long)t;
|
|
195
|
+
*free_bytes = t > u ? (long)(t - u) : 0;
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
char* mik__join_paths(const char* base, const char* const* suffixes, size_t count,
|
|
200
|
+
const char** const* slots) {
|
|
201
|
+
const size_t base_len = strlen(base);
|
|
202
|
+
size_t total = 0;
|
|
203
|
+
for (size_t i = 0; i < count; i++) total += base_len + strlen(suffixes[i]) + 1;
|
|
204
|
+
char* block = static_cast<char*>(malloc(total));
|
|
205
|
+
if (!block) return nullptr;
|
|
206
|
+
size_t at = 0;
|
|
207
|
+
for (size_t i = 0; i < count; i++) {
|
|
208
|
+
*slots[i] = block + at;
|
|
209
|
+
at += (size_t)snprintf(block + at, total - at, "%s%s", base, suffixes[i]) + 1;
|
|
210
|
+
}
|
|
211
|
+
return block;
|
|
212
|
+
}
|