Skip to content
Snippets Groups Projects
Commit b26e4fd2 authored by Marin Karamihalev's avatar Marin Karamihalev
Browse files

comply to notify resp rules

parent e9c2af99
No related branches found
No related tags found
No related merge requests found
Pipeline #81652 passed
...@@ -10,8 +10,8 @@ const decode: DecodeFn = (msg) => { ...@@ -10,8 +10,8 @@ const decode: DecodeFn = (msg) => {
const parent = util.searchParent(msg, "subscriptionId"); const parent = util.searchParent(msg, "subscriptionId");
if (parent) { if (parent) {
const id = parent.subscriptionId; const id = parent.subscriptionId;
const relField = Object.keys(parent).find((k) => const relField = Object.keys(parent).find(
k !== "subscriptionId" && k !== "sendResp" (k) => k !== "subscriptionId" && k !== "sendResp"
); );
return id && relField return id && relField
? [parseInfo(relField, msg), id, null] ? [parseInfo(relField, msg), id, null]
...@@ -20,7 +20,7 @@ const decode: DecodeFn = (msg) => { ...@@ -20,7 +20,7 @@ const decode: DecodeFn = (msg) => {
return [null]; return [null];
}; };
const encode: EncodeFn = ({ paths }) => ({ const encode: EncodeFn = ({ sendResp, subscriptionId, ...rest }) => ({
lookup: "Msg", lookup: "Msg",
header: { header: {
msgId: util.uniq("NOTIFY@"), msgId: util.uniq("NOTIFY@"),
...@@ -31,8 +31,10 @@ const encode: EncodeFn = ({ paths }) => ({ ...@@ -31,8 +31,10 @@ const encode: EncodeFn = ({ paths }) => ({
lookup: "Body", lookup: "Body",
request: { request: {
lookup: "Request", lookup: "Request",
get: { notify: {
paramPaths: Array.isArray(paths) ? paths : [paths], subscriptionId,
sendResp,
...rest,
}, },
}, },
}, },
......
import { MakeFn, SubscribeRecipe } from "../../types"; import { CallbackOptions, MakeFn, SubscribeRecipe } from "../../types";
import { uniq } from "../util"; import { uniq } from "../util";
const subscriptionPath = "Device.LocalAgent.Subscription."; const subscriptionPath = "Device.LocalAgent.Subscription.";
...@@ -23,12 +23,12 @@ const make: MakeFn = ...@@ -23,12 +23,12 @@ const make: MakeFn =
}, },
}); });
const respond = opts?.forceNoResponse // from https://usp.technology/specification/07-index-messages.html#sec:responses-and-retry
? () => {}
: () => call("NOTIFY_RESP", { subscriptionId: id }); const respond = () => call("NOTIFY_RESP", { subscriptionId: id });
const clear = on(id, (...args) => { const clear = on(id, (msg, fullMsg, opts) => {
respond(); opts?.sendResp && respond();
callback(...args); callback(msg, fullMsg);
}); });
return () => { return () => {
clear(); clear();
......
...@@ -15,7 +15,7 @@ import { ...@@ -15,7 +15,7 @@ import {
BuildConnectionFn, BuildConnectionFn,
USPVersion, USPVersion,
} from "../types"; } from "../types";
import { knownUSPVersions, makeCallbackRouter, makeRouter } from "../util"; import { knownUSPVersions, makeCallbackRouter, makeRouter, parseCallbackOptions } from "../util";
const defaultPublishEndpoint = "/usp/endpoint"; const defaultPublishEndpoint = "/usp/endpoint";
const defaultSubscribeEndpoint = "/usp/controller"; const defaultSubscribeEndpoint = "/usp/controller";
...@@ -187,9 +187,10 @@ const buildConnect: BuildConnectionFn = ...@@ -187,9 +187,10 @@ const buildConnect: BuildConnectionFn =
} }
const cbs = callbackRouter.get(id); const cbs = callbackRouter.get(id);
const callbackOptions = parseCallbackOptions(parsedMsg)
cbs.forEach((cb) => { cbs.forEach((cb) => {
if (message) cb(message, parsedMsg); if (callbackOptions) cb(message || err || "", parsedMsg, callbackOptions)
else if (err) cb(err, parsedMsg); else cb(message || err || "", parsedMsg)
}); });
} }
}); });
......
...@@ -240,6 +240,7 @@ export type PbRequestCommand = ...@@ -240,6 +240,7 @@ export type PbRequestCommand =
| PbRequestCommandSupport | PbRequestCommandSupport
| PbRequestCommandInstance | PbRequestCommandInstance
| PbRequestCommandSupportProto | PbRequestCommandSupportProto
| PbRequestCommandNotify
| PbRequestCommandNotifyResp; | PbRequestCommandNotifyResp;
export interface SuportedCommandOpts { export interface SuportedCommandOpts {
...@@ -266,6 +267,14 @@ export type PbRequestCommandNotifyResp = { ...@@ -266,6 +267,14 @@ export type PbRequestCommandNotifyResp = {
}; };
}; };
export type PbRequestCommandNotify = {
notify: {
subscriptionId: string;
sendResp: boolean;
[key: string]: unknown;
};
};
export type PbRequestCommandSupportProto = { export type PbRequestCommandSupportProto = {
getSupportedProtocol: { getSupportedProtocol: {
controllerSupportedProtocolVersions: string; controllerSupportedProtocolVersions: string;
...@@ -615,7 +624,6 @@ export interface SubscriptionOptions { ...@@ -615,7 +624,6 @@ export interface SubscriptionOptions {
id?: string; id?: string;
notif: NotifType; notif: NotifType;
retry?: boolean; retry?: boolean;
forceNoResponse?: boolean;
reference: string | string[]; reference: string | string[];
} }
...@@ -726,10 +734,20 @@ export type EncodeArgs = { ...@@ -726,10 +734,20 @@ export type EncodeArgs = {
export type OnIdent = string | RegExp; export type OnIdent = string | RegExp;
export type CallbackOptions = {
sendResp: boolean
}
export type OnCallback = (
msg: Response,
fullMsg?: Record<string, any>,
opts?: CallbackOptions
) => void;
export type EncodeFn = (args: Record<string, any>) => PbRequestMessage; export type EncodeFn = (args: Record<string, any>) => PbRequestMessage;
export type CallArgs = Record<string, any>; export type CallArgs = Record<string, any>;
export type ClearFn = () => void; export type ClearFn = () => void;
export type OnFn = (ident: OnIdent, callback: SubscriptionCallback) => ClearFn; export type OnFn = (ident: OnIdent, callback: OnCallback) => ClearFn;
export type MakeFn = (call: CallFn, on: OnFn) => Command; export type MakeFn = (call: CallFn, on: OnFn) => Command;
export type MakeRecipeFn = (call: CallFn) => Recipe; export type MakeRecipeFn = (call: CallFn) => Recipe;
export type CommandTrigger = { export type CommandTrigger = {
......
import { CommandType, OnIdent, SubscriptionCallback } from "./types"; import { search } from "./commands/util";
import { CallbackOptions, CommandType, OnCallback, OnIdent, SubscriptionCallback } from "./types";
/** /**
* Makes a router for storing resolve/reject for a message * Makes a router for storing resolve/reject for a message
...@@ -24,19 +25,25 @@ const satisfies = (id: OnIdent, matches: string): boolean => ...@@ -24,19 +25,25 @@ const satisfies = (id: OnIdent, matches: string): boolean =>
export const makeCallbackRouter = () => { export const makeCallbackRouter = () => {
const routes = new Map< const routes = new Map<
string, string,
{ callback: SubscriptionCallback; ident: OnIdent } { callback: OnCallback; ident: OnIdent }
>(); >();
return { return {
get: (id: string): SubscriptionCallback[] => { get: (id: string): OnCallback[] => {
return Array.from(routes.values()) return Array.from(routes.values())
.filter(({ ident }) => satisfies(ident, id)) .filter(({ ident }) => satisfies(ident, id))
.map((v) => v.callback); .map((v) => v.callback);
}, },
add: (ident: OnIdent, callback: SubscriptionCallback) => { add: (ident: OnIdent, callback: OnCallback) => {
routes.set(toId(ident), { callback, ident }); routes.set(toId(ident), { callback, ident });
}, },
del: (id: OnIdent) => routes.delete(toId(id)), del: (id: OnIdent) => routes.delete(toId(id)),
}; };
}; };
export const parseCallbackOptions = (msg: Record<string, any>): CallbackOptions | null => {
const sendResp = search(msg, "sendResp")
if (typeof sendResp === "boolean") return { sendResp }
return null
}
export const knownUSPVersions = ["1.0", "1.1", "1.2"] as const; export const knownUSPVersions = ["1.0", "1.1", "1.2"] as const;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment