feat: add "strictNullChecks"

This commit is contained in:
Adhiraj Singh
2022-07-08 10:38:25 +05:30
parent 7426b7aa2f
commit 40a1e268aa
42 changed files with 350 additions and 339 deletions

View File

@@ -201,5 +201,5 @@ export const SINGLE_BYTE_TOKENS = [
export const TOKEN_MAP: { [token: string]: { dict?: number, index: number } } = { }
for(let i = 0;i < SINGLE_BYTE_TOKENS.length;i++) {
TOKEN_MAP[SINGLE_BYTE_TOKENS[i]] = { index: i }
TOKEN_MAP[SINGLE_BYTE_TOKENS[i]!] = { index: i }
}

File diff suppressed because one or more lines are too long

View File

@@ -153,7 +153,7 @@ export const decodeDecompressedBinaryNode = (
const readString = (tag: number): string => {
if(tag >= 1 && tag < SINGLE_BYTE_TOKENS.length) {
return SINGLE_BYTE_TOKENS[tag]
return SINGLE_BYTE_TOKENS[tag] || ''
}
switch (tag) {
@@ -163,7 +163,7 @@ export const decodeDecompressedBinaryNode = (
case TAGS.DICTIONARY_3:
return getTokenDouble(tag - TAGS.DICTIONARY_0, readByte())
case TAGS.LIST_EMPTY:
return null
return ''
case TAGS.BINARY_8:
return readStringFromChars(readByte())
case TAGS.BINARY_20:

View File

@@ -1,6 +1,6 @@
import * as constants from './constants'
import { jidDecode } from './jid-utils'
import { FullJid, jidDecode } from './jid-utils'
import type { BinaryNode, BinaryNodeCodingOptions } from './types'
export const encodeBinaryNode = (
@@ -52,7 +52,7 @@ export const encodeBinaryNode = (
pushBytes(bytes)
}
const writeJid = ({ agent, device, user, server }: ReturnType<typeof jidDecode>) => {
const writeJid = ({ agent, device, user, server }: FullJid) => {
if(typeof agent !== 'undefined' || typeof device !== 'undefined') {
pushByte(TAGS.AD_JID)
pushByte(agent || 0)

View File

@@ -4,9 +4,9 @@ import { BinaryNode } from './types'
// some extra useful utilities
export const getBinaryNodeChildren = ({ content }: BinaryNode, childTag: string) => {
if(Array.isArray(content)) {
return content.filter(item => item.tag === childTag)
export const getBinaryNodeChildren = (node: BinaryNode | undefined, childTag: string) => {
if(Array.isArray(node?.content)) {
return node!.content.filter(item => item.tag === childTag)
}
return []
@@ -20,20 +20,20 @@ export const getAllBinaryNodeChildren = ({ content }: BinaryNode) => {
return []
}
export const getBinaryNodeChild = ({ content }: BinaryNode, childTag: string) => {
if(Array.isArray(content)) {
return content.find(item => item.tag === childTag)
export const getBinaryNodeChild = (node: BinaryNode | undefined, childTag: string) => {
if(Array.isArray(node?.content)) {
return node?.content.find(item => item.tag === childTag)
}
}
export const getBinaryNodeChildBuffer = (node: BinaryNode, childTag: string) => {
export const getBinaryNodeChildBuffer = (node: BinaryNode | undefined, childTag: string) => {
const child = getBinaryNodeChild(node, childTag)?.content
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
return child
}
}
export const getBinaryNodeChildString = (node: BinaryNode, childTag: string) => {
export const getBinaryNodeChildString = (node: BinaryNode | undefined, childTag: string) => {
const child = getBinaryNodeChild(node, childTag)?.content
if(Buffer.isBuffer(child) || child instanceof Uint8Array) {
return Buffer.from(child).toString('utf-8')

View File

@@ -11,18 +11,23 @@ export type JidWithDevice = {
device?: number
}
export type FullJid = JidWithDevice & {
server: JidServer | string
agent?: number
}
export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
}
export const jidDecode = (jid: string) => {
export const jidDecode = (jid: string | undefined): FullJid | undefined => {
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
if(sepIdx < 0) {
return undefined
}
const server = jid.slice(sepIdx + 1)
const userCombined = jid.slice(0, sepIdx)
const server = jid!.slice(sepIdx + 1)
const userCombined = jid!.slice(0, sepIdx)
const [userAgent, device] = userCombined.split(':')
const [user, agent] = userAgent.split('_')
@@ -36,7 +41,7 @@ export const jidDecode = (jid: string) => {
}
/** is the jid a user */
export const areJidsSameUser = (jid1: string, jid2: string) => (
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
jidDecode(jid1)?.user === jidDecode(jid2)?.user
)
/** is the jid a user */
@@ -49,6 +54,6 @@ export const isJidGroup = (jid: string) => (jid?.endsWith('@g.us'))
export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
export const jidNormalizedUser = (jid: string) => {
const { user, server } = jidDecode(jid)
const { user, server } = jidDecode(jid)!
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
}