src.nth.io/

summaryrefslogtreecommitdiff
path: root/scrypted
diff options
context:
space:
mode:
authorLuke Hoersten <[email protected]>2026-06-14 18:42:29 -0500
committerLuke Hoersten <[email protected]>2026-06-14 18:42:29 -0500
commit731040bcf844c4ad88dca1ada2f84c2e400d3c87 (patch)
tree8a0b06bce2cd819b77d5ba372cbf30cd5aeb5c63 /scrypted
parentd2608f76820eee161622faee764c81e87c40d2da (diff)
scrypted: rewrite top to use globals — Scripts plugin can't resolve @scrypted/sdk import
The Scripts plugin sandbox (in @scrypted/core) doesn't resolve ESM imports of npm modules — saving the script with `import sdk, {...} from '@scrypted/sdk'` produced: Error: Cannot find module '@scrypted/sdk' ... at t.scryptedEval (.../scrypted-eval.ts:102:29) Fix: destructure runtime values from the `sdk` global the plugin injects, use `require('dns').promises` for the Node mDNS lookup, and declare runtime type aliases as `any` so the file still parses on a machine without @scrypted/sdk installed locally. Also drop the `as ScryptedInterface` cast in handleCameraEvent — it was the only place an SDK enum was used as a type rather than a value, and just storing the string is fine. Editor-only diagnostics (NodeJS namespace, Buffer) remain — they resolve in the Scrypted Node runtime and only show up in clangd/tsc without @types/node locally.
Diffstat (limited to 'scrypted')
-rw-r--r--scrypted/scrypted-viewport.ts50
1 files changed, 33 insertions, 17 deletions
diff --git a/scrypted/scrypted-viewport.ts b/scrypted/scrypted-viewport.ts
index e2c5ad6..c23dbce 100644
--- a/scrypted/scrypted-viewport.ts
+++ b/scrypted/scrypted-viewport.ts
@@ -34,25 +34,41 @@
// - Camera must respect picture.width/height OR be paired with a snapshot
// plugin that resizes. Otherwise /frame returns 400.
-import sdk, {
- DeviceCreator,
- DeviceCreatorSettings,
- DeviceProvider,
- EventListenerRegister,
- HttpRequest,
- HttpRequestHandler,
- HttpResponse,
+// The Scripts plugin (in @scrypted/core) evaluates this file inside a
+// sandbox that does NOT resolve ESM `import` of npm modules — it injects
+// `sdk` as a global and lets you `require()` Node built-ins. So we:
+// - destructure runtime values from the injected `sdk` global
+// - require('dns') for the Node mDNS lookup
+// Type aliases below are declared loosely as `any` so the script works
+// even when the SDK type package isn't installed locally (the Scrypted
+// runtime doesn't care about types).
+declare const sdk: any;
+declare const require: any;
+
+const {
ScryptedDeviceBase,
ScryptedDeviceType,
ScryptedInterface,
- Setting,
- Settings,
- SettingValue,
-} from "@scrypted/sdk";
-
-import { promises as dns } from "dns";
-
-const { systemManager, endpointManager, mediaManager, deviceManager } = sdk;
+ systemManager,
+ endpointManager,
+ mediaManager,
+ deviceManager,
+} = sdk;
+
+const dns = require("dns").promises;
+
+// Loose type aliases — the runtime values come from `sdk`, so these
+// are purely cosmetic for the rest of the script's signatures.
+type DeviceCreator = any;
+type DeviceCreatorSettings = any;
+type DeviceProvider = any;
+type EventListenerRegister = any;
+type HttpRequest = any;
+type HttpRequestHandler = any;
+type HttpResponse = any;
+type Setting = any;
+type Settings = any;
+type SettingValue = any;
// Tuning constants. Frame interval is also exposed on the parent's
// Settings page so it can be tweaked without editing the script.
@@ -385,7 +401,7 @@ class ScryptedViewportProvider extends ScryptedDeviceBase
// ------------------------------------------------------------------------
private handleCameraEvent(v: Viewport, details: any, data: any) {
- const iface = details.eventInterface as ScryptedInterface;
+ const iface = details.eventInterface;
let trigger = false;
if (iface === ScryptedInterface.BinarySensor && data === true) trigger = true;
if (iface === ScryptedInterface.MotionSensor && data === true) trigger = true;