Skip to content

Commit e2f6155

Browse files
initialize
0 parents  commit e2f6155

File tree

8 files changed

+360
-0
lines changed

8 files changed

+360
-0
lines changed

.github/workflows/publish.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
name: Publish NPM package
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
16+
- name: Setup Node.js
17+
uses: actions/setup-node@v2
18+
with:
19+
node-version: "20"
20+
registry-url: "https://registry.npmjs.org"
21+
22+
- name: Install dependencies
23+
run: npm install
24+
25+
# - name: Test
26+
# run: npm test
27+
28+
- name: Check the version
29+
id: check
30+
run: |
31+
CURRENT_VERSION=$(jq -r .version package.json)
32+
echo "Current version: $CURRENT_VERSION"
33+
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
34+
echo "Latest tag: $LATEST_TAG"
35+
36+
LATEST_VERSION=${LATEST_TAG#v}
37+
38+
if [ "$LATEST_VERSION" = "$CURRENT_VERSION" ];
39+
then
40+
echo "Version is equal"
41+
echo "version_equal=true" >> $GITHUB_OUTPUT
42+
echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
43+
else
44+
echo "Version is not equal"
45+
echo "version_equal=false" >> $GITHUB_OUTPUT
46+
fi
47+
48+
- name: Build
49+
run: npm run build
50+
if: steps.check.outputs.version_equal == 'true'
51+
52+
- name: Publish
53+
if: steps.check.outputs.version_equal == 'true'
54+
run: npm publish --access public --no-git-checks
55+
env:
56+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
57+
58+
# - name: Git Info Update
59+
# if: steps.check.outputs.version_equal == 'true'
60+
# run: |
61+
# git config --local user.email "github-actions[bot]@users.noreply.github.com"
62+
# git config --local user.name "github-actions[bot]"
63+
64+
- name: Install GitHub CLI
65+
run: sudo apt-get install gh -y
66+
67+
- name: Create GitHub release
68+
if: steps.check.outputs.version_equal == 'true'
69+
run: |
70+
gh release create "v${{ steps.check.outputs.new_version }}" \
71+
dist/* \
72+
--title "v${{ steps.check.outputs.new_version }}"
73+
env:
74+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package-lock.json
2+
node_modules
3+
dist
4+
tsconfig.tsbuildinfo

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.github
2+
node_modules
3+
src
4+
tsconfig.json
5+
tsconfig.tsbuildinfo

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 discord.https
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Nodejs Runtime Adapter
2+
3+
[![npm version](https://img.shields.io/npm/v/@discord.https/nodejs-adapter.svg)](https://www.npmjs.com/package/@discord.https/nodejs-adapter)
4+
[![License](https://img.shields.io/npm/l/@discord.https/nodejs-adapter.svg)](LICENSE)
5+
[![Downloads](https://img.shields.io/npm/dm/@discord.https/nodejs-adapter.svg)](https://www.npmjs.com/package/@discord.https/nodejs-adapter)
6+
7+
**@discord.https/nodejs-adapter** is an adapter for integrating [**discord.https**](https://www.npmjs.com/package/discord.https) with [Nodejs Runtime](https://nodejs.org).
8+
9+
## Installation
10+
11+
```bash
12+
npm install @discord.https/nodejs-adapter discord.https
13+
```
14+
15+
## Usage
16+
17+
```typescript
18+
import Client from "discord.https";
19+
import NodeAdapter from "@discord.https/nodejs-adapter";
20+
const adapter = new NodeAdapter();
21+
22+
import UtilityRoute from "./command/utility/index.js";
23+
import HelloRoute from "./command/fun/hello.js";
24+
25+
const client = new Client({
26+
token: process.env.TOKEN,
27+
publicKey: process.env.PUBLIC_KEY,
28+
httpAdapter: adapter,
29+
debug: true,
30+
});
31+
32+
client.register(UtilityRoute, HelloRoute);
33+
34+
client.listen("interactions", 3000, () => {
35+
console.log("Server is active with the interaction webhook at /interactions");
36+
});
37+
```

package.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "@discord.https/nodejs-adapter",
3+
"version": "1.0.2",
4+
"description": "An adapter for integrating discord.https with nodejs runtime.",
5+
"main": "./dist/index.js",
6+
"type": "module",
7+
"scripts": {
8+
"build": "tsc --build"
9+
},
10+
"types": "./dist/index.d.ts",
11+
"files": [
12+
"dist",
13+
"README.md"
14+
],
15+
"exports": {
16+
".": {
17+
"import": "./dist/index.js",
18+
"types": "./dist/index.d.ts"
19+
}
20+
},
21+
"repository": {
22+
"type": "git",
23+
"url": "https://github.com/discord-http/nodejs-adapter.git"
24+
},
25+
"author": "discord.https",
26+
"license": "MIT",
27+
"keywords": [
28+
"discord-nodejs",
29+
"discord",
30+
"discord.js",
31+
"discord-http-interactions",
32+
"discord-interactions",
33+
"http-interactions",
34+
"discord.https",
35+
"discord-api",
36+
"interaction-router"
37+
],
38+
"devDependencies": {
39+
"@types/node": "^24.3.1"
40+
}
41+
}

src/index.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {
2+
createServer,
3+
Server,
4+
IncomingMessage,
5+
ServerResponse,
6+
} from "node:http";
7+
8+
export interface HttpAdapter {
9+
listen(
10+
endpoint: string,
11+
handler: (req: any, res: any) => Promise<any>,
12+
...args: any[]
13+
): Promise<any> | any;
14+
15+
getRequestBody(req: any): Promise<Uint8Array>;
16+
}
17+
18+
export interface HttpAdapterRequest {
19+
method: string;
20+
url: string;
21+
headers: Record<string, string | string[]>;
22+
}
23+
24+
export interface HttpAdapterSererResponse {
25+
headersSent: boolean;
26+
writeHead(status: number, headers?: Record<string, string>): void;
27+
end(chunk?: string): void;
28+
}
29+
/**
30+
* NodeAdapter implements the HttpAdapter interface for Node.js HTTP server.
31+
* This class provides methods to listen for incoming HTTP requests and read request bodies.
32+
*/
33+
34+
class NodeAdapter implements HttpAdapter {
35+
/**
36+
* Starts the HTTP server and begins listening for incoming requests
37+
* @internal
38+
*/
39+
listen(
40+
_: string,
41+
handler: (req: IncomingMessage, res: ServerResponse) => Promise<any>,
42+
...args: Parameters<Server["listen"]>
43+
) {
44+
const server = createServer(handler);
45+
return server.listen(...args);
46+
}
47+
/**
48+
* @internal
49+
* Reads the body of an incoming HTTP request and returns it as a Uint8Array.
50+
*/
51+
async getRequestBody(req: IncomingMessage): Promise<Uint8Array> {
52+
const chunks: Buffer[] = [];
53+
let totalLength = 0;
54+
try {
55+
for await (const chunk of req) {
56+
chunks.push(chunk);
57+
totalLength += chunk.length;
58+
}
59+
return new Uint8Array(Buffer.concat(chunks, totalLength));
60+
} catch (e) {
61+
console.error(e);
62+
throw new Error("Failed to read request body");
63+
}
64+
}
65+
}
66+
67+
export default NodeAdapter;

tsconfig.json

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
/* Projects */
5+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
6+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
7+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
8+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
9+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
10+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
11+
/* Language and Environment */
12+
"target": "es2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
13+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
14+
// "jsx": "preserve", /* Specify what JSX code is generated. */
15+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
16+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
17+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
18+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
19+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
20+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
21+
// "noLib": true, 3 /* Disable including any library files, including the default lib.d.ts. */
22+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
23+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
24+
/* Modules */
25+
"module": "nodenext", /* Specify what module code is generated. */
26+
"rootDir": "./src", /* Specify the root folder within your source files. */
27+
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
28+
"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
29+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
30+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
31+
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
32+
// "types": [
33+
// "node"
34+
// ], /* Specify type package names to be included without being referenced in a source file. */
35+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
36+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
37+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
38+
// "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
39+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
40+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
41+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
42+
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
43+
// "resolveJsonModule": true, /* Enable importing .json files. */
44+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
45+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
46+
/* JavaScript Support */
47+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
48+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
49+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
50+
/* Emit */
51+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
52+
"declarationMap": false, /* Create sourcemaps for d.ts files. */
53+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
54+
"sourceMap": false, /* Create source map files for emitted JavaScript files. */
55+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
56+
// "noEmit": true, /* Disable emitting files from a compilation. */
57+
// "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. */
58+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
59+
// "removeComments": true, /* Disable emitting comments. */
60+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
61+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
62+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
63+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
64+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
65+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
66+
// "newLine": "crlf", /* Set the newline character for emitting files. */
67+
"stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
68+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
69+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
70+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
71+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
72+
/* Interop Constraints */
73+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
74+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
75+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
76+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
77+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
78+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
79+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
80+
/* Type Checking */
81+
"strict": true, /* Enable all strict type-checking options. */
82+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
83+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
84+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
85+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
86+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
87+
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
88+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
89+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
90+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
91+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
92+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
93+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
94+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
95+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
96+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
97+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
98+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
99+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
100+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
101+
/* Completeness */
102+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
103+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
104+
},
105+
"include": [
106+
"src/**/*.ts"
107+
],
108+
"exclude": [
109+
"node_modules"
110+
]
111+
}

0 commit comments

Comments
 (0)