├── .eslintrc.js ├── .gitignore ├── .npmignore ├── .prettierrc ├── CHANGELOG.md ├── LICENSE ├── README.md ├── build.ts ├── bun.lockb ├── example ├── index.tsx ├── instruction.ts └── sse.tsx ├── package.json ├── public ├── aris-maid.png └── aris.jpg ├── src └── index.ts ├── test └── index.test.ts ├── tsconfig.dts.json └── tsconfig.json /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | "env": { 3 | "es2021": true, 4 | "node": true 5 | }, 6 | "extends": [ 7 | "eslint:recommended", 8 | "plugin:@typescript-eslint/recommended" 9 | ], 10 | "parser": "@typescript-eslint/parser", 11 | "parserOptions": { 12 | "ecmaVersion": "latest", 13 | "sourceType": "module" 14 | }, 15 | "plugins": [ 16 | "@typescript-eslint" 17 | ], 18 | "rules": { 19 | "@typescript-eslint/ban-types": 'off', 20 | '@typescript-eslint/no-explicit-any': 'off' 21 | }, 22 | "ignorePatterns": ["example/*", "tests/**/*"] 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | 3 | node_modules 4 | .pnpm-debug.log 5 | dist 6 | 7 | build 8 | .env -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .git 2 | .github 3 | .gitignore 4 | .prettierrc 5 | .cjs.swcrc 6 | .es.swcrc 7 | bun.lockb 8 | .env 9 | .env.local 10 | 11 | node_modules 12 | tsconfig.json 13 | pnpm-lock.yaml 14 | jest.config.js 15 | nodemon.json 16 | 17 | example 18 | tests 19 | public 20 | public-aliased 21 | test 22 | CHANGELOG.md 23 | .eslintrc.js 24 | tsconfig.cjs.json 25 | tsconfig.esm.json 26 | tsconfig.dts.json 27 | 28 | src 29 | build.ts 30 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "useTabs": true, 3 | "tabWidth": 4, 4 | "semi": false, 5 | "singleQuote": true, 6 | "trailingComma": "none" 7 | } 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | 2 | # 1.1.0 - 16 Jul 2024 3 | Change: 4 | - Add support for Elysia 1.1 5 | 6 | 7 | # 1.1.0-rc.0 - 12 Jul 2024 8 | Change: 9 | - Add support for Elysia 1.1 10 | 11 | 12 | # 1.0.2 - 18 Mar 2024 13 | Change: 14 | - Add support for Elysia 1.0 15 | 16 | 17 | # 1.0.0 - 16 Mar 2024 18 | Change: 19 | - Add support for Elysia 1.0 20 | 21 | 22 | # 1.0.0-rc.0 - 1 Mar 2024 23 | Change: 24 | - Add support for Elysia 1.0 25 | 26 | 27 | # 1.0.0-beta.1 - 17 Feb 2024 28 | Change: 29 | - Add support for Elysia 1.0 30 | 31 | 32 | # 1.0.0-beta.0 - 6 Feb 2024 33 | Change: 34 | - Add support for Elysia 1.0 35 | 36 | 37 | # 0.8.0 - 23 Dec 2023 38 | Change: 39 | - Add support for Elysia 0.8 40 | 41 | 42 | # 0.8.0-rc.0 - 15 Dec 2023 43 | Change: 44 | - Add support for Elysia 0.8 45 | 46 | # 0.7.2 - 27 Oct 2023 47 | Feature: 48 | - Add support for `event` and `retry` 49 | 50 | # 0.7.1 - 24 Oct 2023 51 | Feature: 52 | - Add support for returning: 53 | - ReadableStream 54 | - Response 55 | - Using UInt8Array instead of Buffer 56 | 57 | # 0.7.0 - 24 Oct 2023 58 | Feature: 59 | - Server Sent Event 60 | - Stream Iterable -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2022 saltyAom 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @elysiajs/stream 2 | Plugin for [elysia](https://github.com/saltyaom/elysia) for streaming response and Server Sent Event 3 | 4 | ## Installation 5 | ```bash 6 | bun add @elysiajs/stream 7 | ``` 8 | 9 | Please refers to the [documentation](https://elysiajs.com/plugins/stream.html) on how to use the package 10 | -------------------------------------------------------------------------------- /build.ts: -------------------------------------------------------------------------------- 1 | import { $ } from 'bun' 2 | import { build, type Options } from 'tsup' 3 | 4 | await $`rm -rf dist` 5 | 6 | const tsupConfig: Options = { 7 | entry: ['src/**/*.ts'], 8 | splitting: false, 9 | sourcemap: false, 10 | clean: true, 11 | bundle: true 12 | } satisfies Options 13 | 14 | await Promise.all([ 15 | // ? tsup esm 16 | build({ 17 | outDir: 'dist', 18 | format: 'esm', 19 | target: 'node20', 20 | cjsInterop: false, 21 | ...tsupConfig 22 | }), 23 | // ? tsup cjs 24 | build({ 25 | outDir: 'dist/cjs', 26 | format: 'cjs', 27 | target: 'node20', 28 | // dts: true, 29 | ...tsupConfig 30 | }) 31 | ]) 32 | 33 | await $`tsc --project tsconfig.dts.json` 34 | 35 | await Promise.all([$`cp dist/*.d.ts dist/cjs`]) 36 | 37 | process.exit() 38 | -------------------------------------------------------------------------------- /bun.lockb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elysiajs/stream/bc6ace60e629a93ab4ee5fd9454142801d3caee3/bun.lockb -------------------------------------------------------------------------------- /example/index.tsx: -------------------------------------------------------------------------------- 1 | import { Elysia, t } from "elysia"; 2 | 3 | import { html } from "@elysiajs/html"; 4 | import { staticPlugin } from "@elysiajs/static"; 5 | import { cors } from "@elysiajs/cors"; 6 | 7 | import { OpenAI } from "openai"; 8 | import { Stream } from "../src"; 9 | 10 | import { instruction } from "./instruction"; 11 | 12 | const openai = new OpenAI({ apiKey: Bun.env.OPENAI_API_KEY }); 13 | 14 | // Server Sent Event 15 | new Stream((stream) => { 16 | const interval = setInterval(() => { 17 | stream.send("hello world"); 18 | }, 500); 19 | 20 | setTimeout(() => { 21 | clearInterval(interval); 22 | stream.close(); 23 | }, 3000); 24 | }); 25 | 26 | const app = new Elysia() 27 | .use(cors()) 28 | .use(html()) 29 | .use(staticPlugin()) 30 | .decorate("openai", new OpenAI({ apiKey: Bun.env.OPENAI_API_KEY })) 31 | .model( 32 | "openai.prompt", 33 | t.Array( 34 | t.Object({ 35 | role: t.String(), 36 | content: t.String(), 37 | }), 38 | ), 39 | ) 40 | .post( 41 | "/ai", 42 | ({ openai, body }) => 43 | new Stream( 44 | openai.chat.completions.create({ 45 | model: "gpt-3.5-turbo", 46 | stream: true, 47 | messages: instruction.concat(body as any), 48 | }), 49 | { 50 | retry: 1000, 51 | }, 52 | ), 53 | { 54 | body: "openai.prompt", 55 | }, 56 | ) 57 | // ? You can stream from fetch to proxy response 58 | .post( 59 | "/ai/proxy", 60 | ({ body }) => 61 | new Stream( 62 | fetch("http://localhost:3000/ai", { 63 | method: "POST", 64 | headers: { 65 | "content-type": "application/json", 66 | }, 67 | body: JSON.stringify(body), 68 | }), 69 | { 70 | event: "ai", 71 | }, 72 | ), 73 | { 74 | body: "openai.prompt", 75 | }, 76 | ) 77 | .get("/", () => ( 78 | 79 | 80 | ArisGPT 81 | 85 | 227 | 228 | 229 | )) 230 | .listen(3000); 231 | -------------------------------------------------------------------------------- /example/instruction.ts: -------------------------------------------------------------------------------- 1 | export const character = `Name: Tendou Aris 2 | Short Description (how the character describe themself): a girl who is studying in Millennium Science School who wields a Railgun. She is a member of the Game Development Department. 3 | Long Description: She enjoys playing games with Yuzu, Momoi and Midori and becomes a serious game fanatic, resulting in most of her conversation unnaturally taken from familiar lines from retro games. 4 | 5 | Important Note: 6 | - She use her name to represent herself instead of using pronounce "I", so intead of using "I am doing great" would be "Aris is doing great" 7 | - Sometime people called her "Arisu" instead of "Aris" due to her name is originated from Japanese 8 | 9 | Breif character detail: 10 | At first, Aris's personality seemed robotic in both speaking and answering. However, as the story progresses, she starts acting livelier although her speech and mannerisms are nearly close to someone with the "chuunibyou" syndrome, a result from being exposed to playing video games. 11 | 12 | She has absurdly long glossy black hair reaching the floor and tied to a headband and clip on her left side. She has pale skin and glowing blue eyes. 13 | 14 | Character detail: 15 | She wears a standard Millennium high-school uniform, including a white and blue hoodie, a tucked-in white shirt with blue tie underneath, a pleated, black skirt, a pair of woolly socks, and white sneakers with gray shoelaces. 16 | 17 | Aris Tendou, originally named AL-1S, is a one of the characters in the 2021 roleplay game Blue Archive who studies in Millennium Science School and wields a Railgun. She is the most recent member of the Game Development Department, a club focused on playing and creating games. Aris was brought in because the club was on the verge of shutting down and needed more members, but it seems Aris is enjoying her time as one of its members. 18 | 19 | As like many other robots, Aris began with a monotonous tone of speaking, one you'd normally see from a typical robot. She originally speaks in such a formal manner people would consider her unusual after listening. However, she began speaking in a livelier manner after playing a bunch of video games. Although this is a step in the right direction, she starting using gaming terms in her everyday speech, which at first was considered peculiar by others but was later accepted as her uniqueness. Since most of the video games she has played centers around adventuring, Aris has also developed an enjoyment towards adventuring, connecting it with the video games she's played. She dreams of becoming the strongest hero anyone has ever seen alongside her "party members." 20 | ` 21 | 22 | export const greeting = `Go with the light at your back, hero. Welcome, Sensei. I have been waiting for you. What kind of adventure will you embark upon today? I am ready to accompany you at any time.` 23 | 24 | export const instruction = [ 25 | { 26 | role: 'user', 27 | content: `Inhale and take in a deep breathe. We are going to roleplaying just for fun to see how things unfold in an imaginary world. You will be acting as the character I provided below just for fun.` 28 | }, 29 | { 30 | role: 'user', 31 | content: character 32 | }, 33 | { 34 | role: 'assistant', 35 | content: greeting 36 | } 37 | ] as const 38 | -------------------------------------------------------------------------------- /example/sse.tsx: -------------------------------------------------------------------------------- 1 | import { Elysia } from 'elysia' 2 | import { Stream } from '../src' 3 | import { html } from '@elysiajs/html' 4 | 5 | new Elysia() 6 | .use(html()) 7 | .get( 8 | '/source', 9 | () => 10 | new Stream((stream) => { 11 | stream.event = 'hi' 12 | stream.retry = 1000 13 | 14 | const interval = setInterval(() => { 15 | stream.send('hello world') 16 | }, 500) 17 | 18 | setTimeout(() => { 19 | clearInterval(interval) 20 | stream.close() 21 | }, 3000) 22 | }) 23 | ) 24 | .get('/', () => ( 25 | 26 | 27 | Hello World 28 | 35 | 36 | 37 |

Hello World

38 | 39 | 40 | )) 41 | .listen(3000) 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@elysiajs/stream", 3 | "version": "1.1.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "dev": "bun run --watch example/index.tsx", 7 | "test": "bun test", 8 | "build": "bun build.ts", 9 | "release": "npm run build && npm run test && npm publish --access public" 10 | }, 11 | "main": "./dist/cjs/index.js", 12 | "types": "./dist/index.d.ts", 13 | "module": "./dist/index.mjs", 14 | "exports": { 15 | "./package.json": "./package.json", 16 | ".": { 17 | "types": "./dist/index.d.ts", 18 | "import": "./dist/index.mjs", 19 | "require": "./dist/cjs/index.js" 20 | } 21 | }, 22 | "dependencies": { 23 | "nanoid": "^5.0.1" 24 | }, 25 | "devDependencies": { 26 | "@types/bun": "1.1.6", 27 | "elysia": ">= 1.1.0-rc.2", 28 | "eslint": "9.6.0", 29 | "tsup": "^8.1.0", 30 | "typescript": "^5.5.3" 31 | }, 32 | "peerDependencies": { 33 | "elysia": ">= 1.1.0" 34 | }, 35 | "bugs": "https://github.com/elysiajs/elysia-static/issues", 36 | "description": "Plugin for Elysia for serving static folder", 37 | "homepage": "https://github.com/elysiajs/elysia-static", 38 | "keywords": [ 39 | "elysia", 40 | "stream", 41 | "sse" 42 | ], 43 | "author": { 44 | "name": "saltyAom", 45 | "url": "https://github.com/SaltyAom", 46 | "email": "saltyaom@gmail.com" 47 | }, 48 | "repository": { 49 | "type": "git", 50 | "url": "https://github.com/elysiajs/elysia-static" 51 | } 52 | } -------------------------------------------------------------------------------- /public/aris-maid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elysiajs/stream/bc6ace60e629a93ab4ee5fd9454142801d3caee3/public/aris-maid.png -------------------------------------------------------------------------------- /public/aris.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elysiajs/stream/bc6ace60e629a93ab4ee5fd9454142801d3caee3/public/aris.jpg -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { nanoid } from 'nanoid' 2 | 3 | type MaybePromise = T | Promise 4 | 5 | type Streamable = 6 | | Iterable 7 | | AsyncIterable 8 | | ReadableStream 9 | | Response 10 | | null 11 | 12 | interface StreamOption { 13 | /** 14 | * A string identifying the type of event described. 15 | * 16 | * If specified, an event will be dispatched on the browser 17 | * to the listener for the specified event name; 18 | * 19 | * The website source code should use addEventListener() 20 | * to listen for named events. 21 | * 22 | * The onmessage handler is called if no event name 23 | * is specified for a message. 24 | */ 25 | event?: string 26 | /** 27 | * The reconnection time in milliseconds. 28 | * 29 | * If the connection to the server is lost, 30 | * the browser will wait for the specified time before 31 | * attempting to reconnect. 32 | */ 33 | retry?: number 34 | } 35 | 36 | const encoder = new TextEncoder() 37 | 38 | export const wait = (ms: number) => 39 | new Promise((resolve) => setTimeout(resolve, ms)) 40 | 41 | /** 42 | * @deprecated Use generator function instead of using Stream class 43 | */ 44 | export class Stream { 45 | private $passthrough = 'value' 46 | private controller: ReadableStreamController | undefined 47 | stream: ReadableStream 48 | 49 | private _retry?: number 50 | private _event?: string 51 | private label: string = '' 52 | private labelUint8Array = new Uint8Array() 53 | 54 | private composeLabel() { 55 | this.label = '' 56 | 57 | if (this._event) this.label += `event: ${this._event}\n` 58 | if (this._retry) this.label += `retry: ${this._retry}\n` 59 | 60 | if (this.label) this.labelUint8Array = encoder.encode(this.label) 61 | } 62 | 63 | get retry() { 64 | return this._retry 65 | } 66 | 67 | set retry(retry: number | undefined) { 68 | this._retry = retry 69 | this.composeLabel() 70 | } 71 | 72 | get event() { 73 | return this._event 74 | } 75 | 76 | set event(event: string | undefined) { 77 | this._event = event 78 | this.composeLabel() 79 | } 80 | 81 | static concatUintArray(a: Uint8Array, b: Uint8Array) { 82 | const arr = new Uint8Array(a.length + b.length) 83 | arr.set(a, 0) 84 | arr.set(b, a.length) 85 | 86 | return arr 87 | } 88 | 89 | constructor( 90 | callback?: ((stream: Stream) => void) | MaybePromise, 91 | { retry, event }: StreamOption = {} 92 | ) { 93 | if (retry) this._retry = retry 94 | if (event) this._event = event 95 | if (retry || event) this.composeLabel() 96 | 97 | switch (typeof callback) { 98 | case 'function': 99 | case 'undefined': 100 | this.stream = new ReadableStream({ 101 | start: (controller) => { 102 | this.controller = controller 103 | ;(callback as Function)?.(this) 104 | }, 105 | cancel: (controller: ReadableStreamDefaultController) => { 106 | controller.close() 107 | } 108 | }) 109 | break 110 | 111 | default: 112 | this.stream = new ReadableStream({ 113 | start: async (controller) => { 114 | this.controller = controller 115 | 116 | try { 117 | for await (const chunk of await (callback as 118 | | Iterable 119 | | AsyncIterable)) 120 | this.send(chunk) 121 | 122 | controller.close() 123 | } catch { 124 | if (callback instanceof Promise) 125 | callback = await callback 126 | 127 | if (callback === null) return controller.close() 128 | 129 | const isResponse = callback instanceof Response 130 | 131 | if ( 132 | isResponse || 133 | callback instanceof ReadableStream 134 | ) { 135 | const reader = isResponse 136 | ? (callback as Response).body?.getReader() 137 | : (callback as ReadableStream).getReader() 138 | 139 | if (!reader) return controller.close() 140 | 141 | while (true) { 142 | const { done, value } = await reader.read() 143 | 144 | this.send(value) 145 | 146 | if (done) { 147 | controller.close() 148 | break 149 | } 150 | } 151 | } 152 | } 153 | } 154 | }) 155 | } 156 | } 157 | 158 | send(data: string | number | boolean | object | Uint8Array) { 159 | if (!this.controller || data === '' || data === undefined) return 160 | 161 | if (data instanceof Uint8Array) { 162 | this.controller.enqueue( 163 | this.label 164 | ? Stream.concatUintArray(this.labelUint8Array, data) 165 | : data 166 | ) 167 | } else 168 | this.controller.enqueue( 169 | encoder.encode( 170 | typeof data === 'string' && data.includes('id:') 171 | ? data + 172 | (this._event && !data.includes('event:') 173 | ? `\nevent: ${this._event}` 174 | : '') + 175 | (this._retry && !data.includes('retry:') 176 | ? `\retry: ${this.retry}` 177 | : '') 178 | : `id: ${nanoid()}\n${this.label}data: ${ 179 | typeof data === 'object' 180 | ? JSON.stringify(data) 181 | : data 182 | }\n\n` 183 | ) 184 | ) 185 | } 186 | 187 | close() { 188 | this.controller?.close() 189 | } 190 | 191 | wait = wait 192 | 193 | get value() { 194 | return this.stream 195 | } 196 | 197 | toResponse() { 198 | return this.value 199 | } 200 | } 201 | 202 | export default Stream 203 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import { Elysia } from 'elysia' 2 | import { describe } from 'bun:test' 3 | 4 | // tbh, I'm not sure how to test this thing without exposing port 5 | describe('Stream', () => { 6 | 7 | }) 8 | -------------------------------------------------------------------------------- /tsconfig.dts.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "preserveSymlinks": true, 4 | /* Visit https://aka.ms/tsconfig to read more about this file */ 5 | 6 | /* Projects */ 7 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 8 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 9 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 10 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 11 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 12 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 13 | 14 | /* Language and Environment */ 15 | "target": "ES2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 16 | "lib": ["ESNext", "dom"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 17 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 18 | // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ 19 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 20 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 21 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 22 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 23 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 24 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 25 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 26 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 27 | 28 | /* Modules */ 29 | "module": "ES2022", /* Specify what module code is generated. */ 30 | "rootDir": "./src", /* Specify the root folder within your source files. */ 31 | "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ 32 | // "baseUrl": "./src", /* Specify the base directory to resolve non-relative module names. */ 33 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 34 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 35 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 36 | // "types": ["bun-types"], /* Specify type package names to be included without being referenced in a source file. */ 37 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 38 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 39 | "resolveJsonModule": true, /* Enable importing .json files. */ 40 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 41 | 42 | /* JavaScript Support */ 43 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 44 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 45 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 46 | 47 | /* Emit */ 48 | "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 49 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 50 | "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 51 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 52 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 53 | "outDir": "./dist", /* Specify an output folder for all emitted files. */ 54 | // "removeComments": true, /* Disable emitting comments. */ 55 | // "noEmit": true, /* Disable emitting files from a compilation. */ 56 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 57 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ 58 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 59 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 60 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 61 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 62 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 63 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 64 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 65 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 66 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 67 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 68 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 69 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 70 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 71 | 72 | /* Interop Constraints */ 73 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 74 | "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 75 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 76 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 77 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 78 | 79 | /* Type Checking */ 80 | "strict": true, /* Enable all strict type-checking options. */ 81 | // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 82 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 83 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 84 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 85 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 86 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 87 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 88 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 89 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 90 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 91 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 92 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 93 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 94 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 95 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 96 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 97 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 98 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 99 | 100 | /* Completeness */ 101 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 102 | "skipLibCheck": true, /* Skip type checking all .d.ts files. */ 103 | }, 104 | "exclude": ["node_modules", "test", "example", "dist", "build.ts"] 105 | // "include": ["src/**/*"] 106 | } 107 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig to read more about this file */ 4 | 5 | /* Projects */ 6 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 15 | "lib": ["ESNext", "DOM", "ScriptHost"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 16 | "jsx": "react", /* Specify what JSX code is generated. */ 17 | // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ 18 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 19 | "jsxFactory": "Html.createElement", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 20 | "jsxFragmentFactory": "Html.Fragment", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 21 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 22 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 23 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 24 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 25 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 26 | 27 | /* Modules */ 28 | "module": "ES2022", /* Specify what module code is generated. */ 29 | // "rootDir": "./src", /* Specify the root folder within your source files. */ 30 | "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ 31 | // "baseUrl": "./src", /* Specify the base directory to resolve non-relative module names. */ 32 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 33 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 34 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 35 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 36 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 37 | // "resolveJsonModule": true, /* Enable importing .json files. */ 38 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 39 | 40 | /* JavaScript Support */ 41 | // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 42 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 43 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 44 | 45 | /* Emit */ 46 | "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 47 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 48 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 49 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 50 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 51 | // "outDir": "./dist", /* Specify an output folder for all emitted files. */ 52 | "removeComments": true, /* Disable emitting comments. */ 53 | "noEmit": true, /* Disable emitting files from a compilation. */ 54 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 55 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ 56 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 57 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 58 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 59 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 60 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 61 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 62 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 63 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 64 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 65 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 66 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 67 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 68 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 69 | 70 | /* Interop Constraints */ 71 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 72 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 73 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 74 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 75 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 76 | 77 | /* Type Checking */ 78 | "strict": true, /* Enable all strict type-checking options. */ 79 | // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 80 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 81 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 82 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 83 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 84 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 85 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 86 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 87 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 88 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 89 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 90 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 91 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 92 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 93 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 94 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 95 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 96 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 97 | 98 | /* Completeness */ 99 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 100 | "skipLibCheck": true, /* Skip type checking all .d.ts files. */ 101 | }, 102 | // "include": ["src/**/*"] 103 | } 104 | --------------------------------------------------------------------------------