-
Notifications
You must be signed in to change notification settings - Fork 96
Fix/@typescript eslint/no unnecessary condition #1483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d4fd2e5
477dc14
bf19d2f
583c2ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import rootConfig from "../../eslint.config.mjs"; | ||
|
|
||
| export default [ | ||
| ...rootConfig, | ||
| { | ||
| rules: { | ||
| "@typescript-eslint/no-unnecessary-condition": "warn", | ||
| }, | ||
| }, | ||
| ]; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -41,7 +41,7 @@ const { debug, warn, info, error } = createLoggers("binding-coap", "coap-server" | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| type CoreLinkFormatParameters = Map<string, string[] | number[]>; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| type AffordanceElement = PropertyElement | ActionElement | EventElement; | ||||||||||||||||||||||||||||
| type AffordanceElement = Omit<PropertyElement, "forms"> | Omit<ActionElement, "forms"> | Omit<EventElement, "forms">; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // TODO: Move to core? | ||||||||||||||||||||||||||||
| type AugmentedInteractionOptions = WoT.InteractionOptions & { formIndex: number }; | ||||||||||||||||||||||||||||
|
|
@@ -145,11 +145,6 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
| const port = this.getPort(); | ||||||||||||||||||||||||||||
| const urlPath = this.createThingUrlPath(thing); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (port === -1) { | ||||||||||||||||||||||||||||
| warn("CoapServer is assigned an invalid port, aborting expose process."); | ||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| this.fillInBindingData(thing, port, urlPath); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| debug(`CoapServer on port ${port} exposes '${thing.title}' as unique '/${urlPath}'`); | ||||||||||||||||||||||||||||
|
|
@@ -236,12 +231,8 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| private addFormToAffordance(form: Form, affordance: AffordanceElement): void { | ||||||||||||||||||||||||||||
| const affordanceForms = affordance.forms; | ||||||||||||||||||||||||||||
| if (affordanceForms == null) { | ||||||||||||||||||||||||||||
| affordance.forms = [form]; | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| affordanceForms.push(form); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| const withForms = affordance as AffordanceElement & { forms?: Form[] }; | ||||||||||||||||||||||||||||
| (withForms.forms ??= []).push(form); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| private fillInPropertyBindingData(thing: ExposedThing, base: string, offeredMediaType: string) { | ||||||||||||||||||||||||||||
|
|
@@ -354,9 +345,8 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| public async destroy(thingId: string): Promise<boolean> { | ||||||||||||||||||||||||||||
| debug(`CoapServer on port ${this.getPort()} destroying thingId '${thingId}'`); | ||||||||||||||||||||||||||||
| for (const name of this.things.keys()) { | ||||||||||||||||||||||||||||
| const exposedThing = this.things.get(name); | ||||||||||||||||||||||||||||
| if (exposedThing?.id === thingId) { | ||||||||||||||||||||||||||||
| for (const [name, exposedThing] of this.things.entries()) { | ||||||||||||||||||||||||||||
| if (exposedThing.id === thingId) { | ||||||||||||||||||||||||||||
| this.things.delete(name); | ||||||||||||||||||||||||||||
| this.coreResources.delete(name); | ||||||||||||||||||||||||||||
| this.mdnsIntroducer?.delete(name); | ||||||||||||||||||||||||||||
|
|
@@ -374,7 +364,7 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
| return Array.from(this.coreResources.values()) | ||||||||||||||||||||||||||||
| .map((resource) => { | ||||||||||||||||||||||||||||
| const formattedPath = `</${resource.urlPath}>`; | ||||||||||||||||||||||||||||
| const parameters = Array.from(resource.parameters?.entries() ?? []); | ||||||||||||||||||||||||||||
| const parameters = resource.parameters ? Array.from(resource.parameters.entries()) : []; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| const parameterValues = parameters.map((parameter) => { | ||||||||||||||||||||||||||||
| const key = parameter[0]; | ||||||||||||||||||||||||||||
|
|
@@ -499,20 +489,24 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
| const { thingKey, affordanceType, affordanceKey } = this.parseUriSegments(requestUri); | ||||||||||||||||||||||||||||
| const thing = this.things.get(thingKey); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if (thing == null) { | ||||||||||||||||||||||||||||
| if (thing === undefined) { | ||||||||||||||||||||||||||||
| this.sendNotFoundResponse(res); | ||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // TODO: Remove support for trailing slashes (or rather: trailing empty URI path segments) | ||||||||||||||||||||||||||||
| if (affordanceType == null || affordanceType === "") { | ||||||||||||||||||||||||||||
| if (!affordanceType) { | ||||||||||||||||||||||||||||
| await this.handleTdRequest(req, res, thing); | ||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| switch (affordanceType) { | ||||||||||||||||||||||||||||
| case this.PROPERTY_DIR: | ||||||||||||||||||||||||||||
| this.handlePropertyRequest(thing, affordanceKey, req, res, contentType); | ||||||||||||||||||||||||||||
| if (!affordanceKey) { | ||||||||||||||||||||||||||||
| this.handlePropertiesRequest(req, contentType, thing, res); | ||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||
| this.handlePropertyRequest(thing, affordanceKey, req, res, contentType); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||
| case this.ACTION_DIR: | ||||||||||||||||||||||||||||
| this.handleActionRequest(thing, affordanceKey, req, res, contentType); | ||||||||||||||||||||||||||||
|
|
@@ -554,11 +548,6 @@ export default class CoapServer implements ProtocolServer { | |||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||
| const property = thing.properties[affordanceKey]; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
| if (!property) { | |
| this.sendResponse(res, "4.04", "Property not found"); | |
| return; | |
| } |
Copilot
AI
Feb 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using JSON.parse here changes behavior compared to ContentSerdes.contentToValue/JsonCodec: it will throw on non-JSON payloads (e.g. plain strings without quotes) and the catch block then drops that property entirely. This breaks the prior relaxed decoding behavior (and will omit string-valued properties encoded as non-JSON). Consider switching back to ContentSerdes.get().contentToValue({ type: content.type, body: buffer }, /* schema */ ...) so decoding is consistent with the rest of the stack.
| const parsed = JSON.parse(buffer.toString()); | |
| recordResponse[key] = parsed; | |
| } catch { | |
| // Ignore non-JSON properties | |
| const parsed = ContentSerdes.get().contentToValue( | |
| { type: content.type, body: buffer }, | |
| undefined | |
| ); | |
| recordResponse[key] = parsed; | |
| } catch { | |
| // Ignore non-JSON properties or decoding errors |
Copilot
AI
Feb 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
action can be undefined when an unknown action name (or missing segment) is requested, but handleActionRequest later dereferences action.forms / action.uriVariables unconditionally. Please add/restore a not-found guard (e.g., 4.04) when action is undefined to avoid runtime exceptions.
Copilot
AI
Feb 15, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event can be undefined when an unknown event name (or missing segment) is requested, but the code later dereferences event.forms / event.uriVariables unconditionally. Please add/restore a not-found guard (4.04) when event is undefined to prevent runtime exceptions.
| if (!event) { | |
| debug( | |
| `CoapServer on port ${this.getPort()} received request for unknown event '${affordanceKey}' from ${Helpers.toUriLiteral( | |
| req.rsinfo.address | |
| )}:${req.rsinfo.port}` | |
| ); | |
| this.sendResponse(res, "4.04", "Not Found"); | |
| return; | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,13 +110,13 @@ export default class CoapsClient implements ProtocolClient { | |
| ): Promise<Subscription> { | ||
| return new Promise<Subscription>((resolve, reject) => { | ||
| const requestUri = new URL(form.href.replace(/$coaps/, "https")); | ||
| if (this.authorization != null) { | ||
| if (this.authorization !== undefined) { | ||
| coaps.setSecurityParams(requestUri.hostname, this.authorization); | ||
| } | ||
|
|
||
| const callback = (resp: CoapResponse) => { | ||
| if (resp.payload != null) { | ||
| next(new Content(form?.contentType ?? ContentSerdes.DEFAULT, Readable.from(resp.payload))); | ||
| next(new Content(form.contentType ?? ContentSerdes.DEFAULT, Readable.from(resp.payload))); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -163,14 +163,14 @@ export default class CoapsClient implements ProtocolClient { | |
| } | ||
|
|
||
| public setSecurity(metadata: Array<SecurityScheme>, credentials?: pskSecurityParameters): boolean { | ||
| if (metadata === undefined || !Array.isArray(metadata) || metadata.length === 0) { | ||
| if (!Array.isArray(metadata) || metadata.length === 0) { | ||
| warn(`CoapsClient received empty security metadata`); | ||
| return false; | ||
| } | ||
|
|
||
| const security: SecurityScheme = metadata[0]; | ||
|
|
||
| if (security.scheme === "psk" && credentials != null) { | ||
| if (security.scheme === "psk" && credentials !== undefined) { | ||
| this.authorization = { psk: {} }; | ||
| this.authorization.psk[credentials.identity] = credentials.psk; | ||
| } else if (security.scheme === "apikey") { | ||
|
Comment on lines
+173
to
176
|
||
|
|
@@ -224,7 +224,7 @@ export default class CoapsClient implements ProtocolClient { | |
| ): Promise<CoapResponse> { | ||
| // url only works with http* | ||
| const requestUri = new URL(form.href.replace(/$coaps/, "https")); | ||
| if (this.authorization != null) { | ||
| if (this.authorization !== undefined) { | ||
| coaps.setSecurityParams(requestUri.hostname, this.authorization); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expose()now proceeds even whengetPort()returns-1(server not listening). That will generate invalidcoap://...:-1/...URLs and register mDNS with an invalid port. Consider restoring the guard (warn + return) or throwing a clear error whenport === -1to prevent exposing beforestart()completes.