From 034873e754feef033c5c6ea4632141b8a26d8970 Mon Sep 17 00:00:00 2001 From: VityaSchel <59040542+VityaSchel@users.noreply.github.com> Date: Thu, 20 Jun 2024 20:10:35 +0400 Subject: [PATCH] Initial commit --- .gitignore | 175 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 30 ++++++++ bun.lockb | Bin 0 -> 14338 bytes package.json | 31 ++++++++ src/crypto.ts | 20 +++++ src/fetch.ts | 23 ++++++ src/index.ts | 10 +++ src/runtime.ts | 23 ++++++ src/validation.ts | 26 +++++++ tsconfig.build.json | 28 +++++++ tsconfig.json | 27 +++++++ 11 files changed, 393 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 bun.lockb create mode 100644 package.json create mode 100644 src/crypto.ts create mode 100644 src/fetch.ts create mode 100644 src/index.ts create mode 100644 src/runtime.ts create mode 100644 src/validation.ts create mode 100644 tsconfig.build.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..c04186c --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# @session.js/errors + +Session.js error classes. This module can be used to check instance of thrown errors in Session.js. All errors extend from `SessionJsError` class which itself extends from generic Error class. + +```ts +import { SessionRuntimeError, SessionJsError } from '@session.js/error' + +try { + // something throws +} catch(e) { + if(e instanceof SessionRuntimeError) { + console.error('Session.js runtime error handled') + } else { + if(e instanceof SessionJsError) { + console.error('It\'s not Session.js runtime error... but at least we know it\'s Session\.js error') + } else { + console.error('No idea what is this error') + throw e + } + } +} +``` + +## Made for session.js + +Use Session messenger programmatically with [Session.js](https://github.com/sessionjs/client): Session bots, custom Session clients, and more. + +## Donate + +[hloth.dev/donate](https://hloth.dev/donate) \ No newline at end of file diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000000000000000000000000000000000000..46f3363570fe8c092b906c4bd790751acf8660b2 GIT binary patch literal 14338 zcmeHOc|6qH`yWiSiQG!IENzMzJB8>*g*I2Kq;G@4lo`y7LXt|`C0C29+ahfWEwZHL zDitj%?V@X|v?$V^7WqBTXXfzf)?de8Ia1#nrufkHk<%$jf9RhGeEEDyf^^kY@`$duYoZ!6!0 zrpX4Cs;!qPSG_UYoWG(fZL-h`8bKH&i5Q*#7Dq|*JxBv8ufkwNX+o+C#H%3g3URSC z9}aO3$n(PjI3h^<1q6C|g)tZ+$ofN`&lUP{O&JWHpCC}kWiZamNO-P-y2#T#6>fU$A+`qhBsgom1ERA)fO#zgMaJu``dfN4&~ERHSU-&zxa8<+6NQ z;ZP%|PmLbEUM8(L?y$G1N6La|--ql;Ar@V?-n|p8aNq2t;pRIwQ~SEizvptpc;XsQ zr2(U-*e4jyy4-!j&6zn3LslvbNt&=TbB~R-)9mndF5_Z+HXQo%&V?#bLE&E)6B_&v zlpn9t6&b1**W?9ljC)g6(qp(lrS~{zzx-c4HSCR!9f@D6y8PLyNoT2fAxtyJdIbL^sF)Dee~3)>vFaYj50J>&h-Mb|5pKqlM0sb*9OJ|`Cw>pEIJej zejT*7mEvKlFgmIi!QTdqwGbl~wB7gG z?=IkR{-XUcd@r62hQt1M)NT?ft>femcsr?llui0Y+Wk{Z+NA&<){rQOsPMh^dk8?Z zAM*Vh`{{w<>;X@7$2F`Yf#@#=Jih;khkfb0?Y{@`*nh-hAa;=C{`oJFUjg{hfG7Qj zY=;8DvtiL4DV0ykIzm9&E&}`*8vc97?+oD4{)F#)^_PW-I}GqdF0o^KeIW820FU#( zqjo2}1iuLIIR7!feXXg|!O4T&AwBO-W_ z6py%nBR?JRj{k-I@X+W&)4x5vNj=fu1@L74;P`!S|Kk9U>kp0{&Y|xeL(~K9hU*T> z$9aI^TQRnSvosaH6{GB4@E~U&s+A;0UR8K7?kkPe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ZS (https://hloth.dev)", + "repository": { + "type": "git", + "url": "git+https://github.com/sessionjs/errors.git" + }, + "bugs": { + "url": "https://github.com/sessionjs/errors/issues" + }, + "homepage": "https://github.com/sessionjs/errors#readme", + "description": "Session.js errors classes", + "type": "module", + "files": [ + "dist/**.js", + "dist/**.d.ts" + ], + "scripts": { + "build": "tsc --project tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "devDependencies": { + "tsc-alias": "^1.8.10" + } +} diff --git a/src/crypto.ts b/src/crypto.ts new file mode 100644 index 0000000..1929273 --- /dev/null +++ b/src/crypto.ts @@ -0,0 +1,20 @@ +import { SessionJsError } from "./index" + +export enum SessionCryptoErrorCode { + MessageEncryptionFailed = 'message_encryption_failed', + MessageDecryptionFailed = 'message_decryption_failed', + MessageVerificationFailed = 'message_verification_failed', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionCryptoError extends SessionJsError { + code: SessionCryptoErrorCode + + constructor({ code, message }: { + code: SessionCryptoErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/fetch.ts b/src/fetch.ts new file mode 100644 index 0000000..42425b7 --- /dev/null +++ b/src/fetch.ts @@ -0,0 +1,23 @@ +import { SessionJsError } from "./index" + +export enum SessionFetchErrorCode { + RetryWithOtherNode421Error = 'retry_with_other_node_421_error', + NoSnodesAvailable = 'no_snodes_available', + NoSwarmsAvailable = 'no_swarms_available', + FetchFailed = 'fetch_failed', + InvalidResponse = 'invalid_response', + PollingFailed = 'polling_failed', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionFetchError extends SessionJsError { + code: SessionFetchErrorCode + + constructor({ code, message }: { + code: SessionFetchErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..36b1ad7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,10 @@ +export { SessionValidationError, SessionValidationErrorCode } from './validation' +export { SessionCryptoError, SessionCryptoErrorCode } from './crypto' +export { SessionRuntimeError, SessionRuntimeErrorCode } from './runtime' +export { SessionFetchError, SessionFetchErrorCode } from './fetch' + +export class SessionJsError extends Error { + constructor(message: string) { + super(message) + } +} \ No newline at end of file diff --git a/src/runtime.ts b/src/runtime.ts new file mode 100644 index 0000000..d819c10 --- /dev/null +++ b/src/runtime.ts @@ -0,0 +1,23 @@ +import { SessionJsError } from "./index" + +export enum SessionRuntimeErrorCode { + EmptyUser = 'empty_user', + Generic = 'generic', + NoSwarmsAvailable = 'no_swarms_available', + MultipleInstancesNotAllowed = 'multiple_instances_not_allowed', + NoInstancePolling = 'no_instance_polling', + InstanceAlreadyAuthorized = 'instance_already_authorized', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionRuntimeError extends SessionJsError { + code: SessionRuntimeErrorCode + + constructor({ code, message }: { + code: SessionRuntimeErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/validation.ts b/src/validation.ts new file mode 100644 index 0000000..00f870a --- /dev/null +++ b/src/validation.ts @@ -0,0 +1,26 @@ +import { SessionJsError } from "./index" + +export enum SessionValidationErrorCode { + InvalidMnemonic = 'invalid_mnemonic', + InvalidDisplayName = 'invalid_display_name', + InvalidSessionID = 'invalid_session_id', + InvalidPoller = 'invalid_poller', + InvalidNamespaces = 'invalid_namespaces', + NotOurPubkeyNotLegacyClosedGroup = 'not_our_pubkey_not_legacy_closed_group', + NotZeroNamespaceNotLegacyClosedGroup = 'not_zero_namespace_not_legacy_closed_group', + UnsupportedFeature = 'unsupported_feature', + InvalidMessage = 'invalid_message', +} + +/** Validation error, indicating that the developer provided invalid input */ +export class SessionValidationError extends SessionJsError { + code: SessionValidationErrorCode + + constructor({ code, message }: { + code: SessionValidationErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..fbdb151 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + "declaration": true, + + // Bundler mode + "moduleResolution": "bundler", + "verbatimModuleSyntax": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false, + "outDir": "dist", + }, + "include": ["src/index.ts"] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}