build: dual CJS/ESM output, ES2020 target, Node 18 floor
"exports" gains "import" and "types" next to "require": import "alex2node" failed with ERR_PACKAGE_PATH_NOT_EXPORTED. scripts/build.mjs compiles src/ to dist/cjs, dist/esm and dist/types in a staging directory and swaps them in only when both compiler runs passed. "main" and "types" follow for resolvers that do not read "exports". Target ES6 -> ES2020, "engines": node >= 18, relative imports carry ".js" for Node's ESM loader. uuid gives way to crypto.randomUUID(); mqtt is the only runtime dependency left. Tests load the package by name. New: both entry points, the modules the builds import, dist/ equal to a fresh build, a round trip on the ES module build, and two compiled type fixtures in place of the .d.ts regexes. 22 tests pass in 5.7 s. dist/: 16 files, 79,711 B -> 25 files, 132,291 B; the npm tarball is 25,125 B. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
58869d44c1
commit
1d88c7811a
50 changed files with 1472 additions and 195 deletions
143
dist/esm/Device.js
vendored
Normal file
143
dist/esm/Device.js
vendored
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
import { AlexaInterface } from "./AlexaInterface.js";
|
||||
import { DisplayCategory } from "./DisplayCategory.js";
|
||||
import { EventEmitter } from "events";
|
||||
import { randomUUID } from "crypto";
|
||||
import { AlexaStatusMessage } from "./AlexaStatusMessage.js";
|
||||
import { AlexaErrorResponse } from "./AlexaErrorResponse.js";
|
||||
class Device extends EventEmitter {
|
||||
constructor(mqttClient, rootTopic, name, endpointId, displayCategory, description = "Alexa to Node.js bridge", manufacturerName = "Alex2Node", manufacturer = "Alex2Node", model = "Alex2Node_v1.0.0") {
|
||||
super();
|
||||
this.mqttClient = mqttClient;
|
||||
this.rootTopic = rootTopic;
|
||||
this.name = name;
|
||||
this.endpointId = endpointId;
|
||||
this.displayCategory = displayCategory;
|
||||
this.description = description;
|
||||
this.manufacturerName = manufacturerName;
|
||||
this.manufacturer = manufacturer;
|
||||
this.model = model;
|
||||
this.softwareVersion = "1.0.0";
|
||||
this.capabilities = [];
|
||||
}
|
||||
/** Internal (1.5.2): Alex2MQTT.connect() re-binds every registered device to its new broker client after a disconnect(). */
|
||||
setMqttClient(client) {
|
||||
this.mqttClient = client;
|
||||
}
|
||||
getName() {
|
||||
return this.name;
|
||||
}
|
||||
setName(name) {
|
||||
this.name = name;
|
||||
}
|
||||
getEndpointId() {
|
||||
return this.endpointId;
|
||||
}
|
||||
setDisplayCategory(category) {
|
||||
this.displayCategory = Array.isArray(category) ? category : [category];
|
||||
}
|
||||
getDisplayCategory() {
|
||||
return this.displayCategory || [DisplayCategory.LIGHT];
|
||||
}
|
||||
setDescription(description) {
|
||||
this.description = description;
|
||||
}
|
||||
getDescription() {
|
||||
return this.description;
|
||||
}
|
||||
getErrorMessage(correlationToken) {
|
||||
const msg = new AlexaErrorResponse(correlationToken, this.rootTopic, this.endpointId, this.mqttClient);
|
||||
msg.onPublishError = this.onPublishError;
|
||||
return msg;
|
||||
}
|
||||
getStatusMessage(correlationToken, isResponse = false, isDeferred = false) {
|
||||
const msg = new AlexaStatusMessage(correlationToken, this.rootTopic, this.endpointId, this.mqttClient, isResponse, isDeferred);
|
||||
msg.onPublishError = this.onPublishError;
|
||||
return msg;
|
||||
}
|
||||
/**
|
||||
* A proactive Alexa.ChangeReport (1.5.1): add the changed properties (the default target), optionally
|
||||
* .unchanged() then the others, and .send() - it goes to <root>/changeReport, which Alex2MQTT forwards to the Alexa
|
||||
* event gateway with the user's token. Alexa only accepts it for capabilities registered with proactivelyReported.
|
||||
*/
|
||||
getChangeReport(cause = "PHYSICAL_INTERACTION") {
|
||||
const msg = new AlexaStatusMessage("", this.rootTopic, this.endpointId, this.mqttClient, false, false, cause);
|
||||
msg.onPublishError = this.onPublishError;
|
||||
return msg;
|
||||
}
|
||||
/**
|
||||
* Alexa.SceneController: answer an Activate / Deactivate directive with ActivationStarted / DeactivationStarted
|
||||
* (1.5.1). Resolves with the topic published to, or "" when the publish failed (never rejects, 1.5.2).
|
||||
*/
|
||||
sendSceneResponse(correlationToken, activated, cause = "VOICE_INTERACTION", sendAsync = false) {
|
||||
const payload = {
|
||||
context: {},
|
||||
event: {
|
||||
header: { namespace: "Alexa.SceneController", name: activated ? "ActivationStarted" : "DeactivationStarted", messageId: randomUUID(), correlationToken, payloadVersion: "3" },
|
||||
endpoint: { endpointId: this.endpointId },
|
||||
payload: { cause: { type: cause }, timestamp: new Date().toISOString() },
|
||||
},
|
||||
};
|
||||
const topic = `${this.rootTopic}/${this.endpointId}/${sendAsync ? "deferredResponse" : "alexaResponce"}`;
|
||||
return new Promise((resolve) => {
|
||||
this.mqttClient.publish(topic, JSON.stringify(payload), (err) => {
|
||||
if (!err)
|
||||
return resolve(topic);
|
||||
if (this.onPublishError)
|
||||
this.onPublishError(err); // -> the bridge's "error" event (when somebody listens)
|
||||
resolve(""); // 1.5.1 rejected here, and an un-caught send() then killed the host on any broker hiccup
|
||||
});
|
||||
});
|
||||
}
|
||||
getCapabilities() {
|
||||
return this.capabilities.slice();
|
||||
}
|
||||
setManufacturerName(name) {
|
||||
this.manufacturerName = name;
|
||||
}
|
||||
getManufacturerName() {
|
||||
return this.manufacturerName;
|
||||
}
|
||||
setManufacturer(manufacturer) {
|
||||
this.manufacturer = manufacturer;
|
||||
}
|
||||
getManufacturer() {
|
||||
return this.manufacturer;
|
||||
}
|
||||
setModel(model) {
|
||||
this.model = model;
|
||||
}
|
||||
getModel() {
|
||||
return this.model;
|
||||
}
|
||||
getSoftwareVersion() {
|
||||
return this.softwareVersion;
|
||||
}
|
||||
/** Add a capability. 1.5.1: options.retrievable / proactivelyReported / instance (a change report needs proactivelyReported). */
|
||||
addCapability(type, options = {}) {
|
||||
const newCapability = new AlexaInterface(type, options.retrievable ?? true, options.proactivelyReported ?? false, options.instance ?? "");
|
||||
this.capabilities.push(newCapability);
|
||||
// console.log(
|
||||
// `Created new interface with type: ${newCapability.getTypeString()}`
|
||||
// );
|
||||
return newCapability;
|
||||
}
|
||||
getJSON() {
|
||||
return {
|
||||
endpointId: this.endpointId,
|
||||
friendlyName: this.name,
|
||||
description: this.description,
|
||||
manufacturerName: this.manufacturerName,
|
||||
displayCategories: this.getDisplayCategory(),
|
||||
additionalAttributes: {
|
||||
manufacturer: this.manufacturer,
|
||||
model: this.model,
|
||||
serialNumber: "Alex2Node",
|
||||
firmwareVersion: "1.0.0",
|
||||
softwareVersion: this.softwareVersion,
|
||||
customIdentifier: "Alex2Node",
|
||||
},
|
||||
capabilities: this.capabilities.map((cap) => cap.getJSON()),
|
||||
};
|
||||
}
|
||||
}
|
||||
export default Device;
|
||||
Loading…
Add table
Add a link
Reference in a new issue