Skip to content

Commit

Permalink
jsdoc: fix typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Jun 13, 2023
1 parent 361a9eb commit e2ecabc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/commons/bufutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import { Buffer } from "buffer";
import * as util from "./util.js";

const ZERO = new Uint8Array(0);
export const ZERO = new Uint8Array();
const ZEROSTR = "";
export const ZEROAB = new ArrayBuffer();
const encoder = new TextEncoder();
const decoder = new TextDecoder();

Expand All @@ -36,7 +37,7 @@ export function toB64(buf) {
}

export function hex(b) {
if (emptyBuf(b)) return "";
if (emptyBuf(b)) return ZEROSTR;
// avoids slicing Buffer (normalize8) to get hex
if (b instanceof Buffer) return b.toString("hex");
const ab = normalize8(b);
Expand All @@ -46,7 +47,7 @@ export function hex(b) {
}

/**
* @param { Buffer | Uint8Array | ArrayBuffer | null } b
* @param { Buffer | Uint8Array | ArrayBuffer } b
* @returns {number}
*/
export function len(b) {
Expand Down Expand Up @@ -125,7 +126,7 @@ export function raw(b) {
// normalize8 returns the underlying buffer if any, as Uint8Array
// b is either an ArrayBuffer, a TypedArray, or a node:Buffer
export function normalize8(b) {
if (emptyBuf(b)) return null;
if (emptyBuf(b)) return ZERO;

let underlyingBuffer = null;
// ... has byteLength property, b must be of type ArrayBuffer;
Expand All @@ -142,11 +143,11 @@ export function normalize8(b) {

/**
* @param {Uint8Array|Buffer} buf
* @returns {ArrayBuffer?}
* @returns {ArrayBuffer}
*/
export function arrayBufferOf(buf) {
// buf is either TypedArray or node:Buffer
if (emptyBuf(buf)) return null;
if (emptyBuf(buf)) return ZEROAB;

const offset = buf.byteOffset;
const len = buf.byteLength;
Expand All @@ -162,7 +163,7 @@ export function arrayBufferOf(buf) {

// stackoverflow.com/a/17064149
export function bufferOf(arrayBuf) {
if (emptyBuf(arrayBuf)) return null;
if (emptyBuf(arrayBuf)) return ZERO;
if (arrayBuf instanceof Uint8Array) return arrayBuf;

return Buffer.from(new Uint8Array(arrayBuf));
Expand Down
6 changes: 3 additions & 3 deletions src/commons/dnsutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function mkQ(qid, qs) {

export function servfail(qid, qs) {
// qid == 0 is valid; in fact qid is set to 0 by most doh clients
if (qid == null || qid < 0 || util.emptyArray(qs)) return null;
if (qid == null || qid < 0 || util.emptyArray(qs)) return bufutil.ZEROAB;

return encode({
id: qid,
Expand All @@ -65,13 +65,13 @@ export function servfail(qid, qs) {
}

export function servfailQ(q) {
if (bufutil.emptyBuf(q)) return null;
if (bufutil.emptyBuf(q)) return bufutil.ZEROAB;

try {
const p = decode(q);
return servfail(p.id, p.questions);
} catch (e) {
return null;
return bufutil.ZEROAB;
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/core/doh.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ import * as util from "../commons/util.js";
import * as dnsutil from "../commons/dnsutil.js";
import IOState from "./io-state.js";

// TODO: define FetchEventLike
/**
* @param {FetchEvent} event
* @typedef {any} FetchEventLike
*/

/**
* @param {FetchEvent|FetchEventLike} event
* @returns {Promise<Response>}
*/
export function handleRequest(event) {
Expand Down

0 comments on commit e2ecabc

Please sign in to comment.