@ovencord/util 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +190 -0
- package/README.md +67 -0
- package/package.json +55 -0
- package/src/AsyncEventEmitter.ts +28 -0
- package/src/AsyncQueue.ts +64 -0
- package/src/Equatable.ts +21 -0
- package/src/RawFile.ts +33 -0
- package/src/encodables.ts +61 -0
- package/src/functions/calculateShardId.ts +9 -0
- package/src/functions/embedLength.ts +16 -0
- package/src/functions/index.ts +6 -0
- package/src/functions/lazy.ts +18 -0
- package/src/functions/range.ts +61 -0
- package/src/functions/runtime.ts +14 -0
- package/src/functions/userAgentAppendix.ts +50 -0
- package/src/gatewayRateLimitError.ts +25 -0
- package/src/index.ts +16 -0
- package/src/shims.d.ts +59 -0
- package/src/types.ts +4 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2022 Noel Buechler
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://discord.gg/djs"><img src="https://img.shields.io/badge/join_us-on_discord-5865F2?logo=discord&logoColor=white" alt="Discord server" /></a>
|
|
9
|
+
<a href="https://github.com/ovencord/ovencord/actions"><img src="https://github.com/ovencord/ovencord/actions/workflows/tests.yml/badge.svg" alt="Build status" /></a>
|
|
10
|
+
<a href="https://github.com/ovencord/ovencord/commits/main/packages/util"><img alt="Last commit." src="https://img.shields.io/github/last-commit/ovencord/ovencord?logo=github&logoColor=ffffff&path=packages%2Futil" /></a>
|
|
11
|
+
<a href="https://opencollective.com/discordjs"><img src="https://img.shields.io/opencollective/backers/discordjs?maxAge=3600&logo=opencollective" alt="backers" /></a>
|
|
12
|
+
</p>
|
|
13
|
+
<p>
|
|
14
|
+
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/ovencord/ovencord/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
|
15
|
+
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/ovencord/ovencord/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
|
16
|
+
</p>
|
|
17
|
+
</div>
|
|
18
|
+
|
|
19
|
+
## About
|
|
20
|
+
|
|
21
|
+
`@ovencord/util` is a collection of utility functions for use with discord.js.
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
**Node.js 22.12.0 or newer is required.**
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install @ovencord/util
|
|
29
|
+
yarn add @ovencord/util
|
|
30
|
+
pnpm add @ovencord/util
|
|
31
|
+
bun add @ovencord/util
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Links
|
|
35
|
+
|
|
36
|
+
- [Website][website] ([source][website-source])
|
|
37
|
+
- [Documentation][documentation]
|
|
38
|
+
- [Guide][guide] ([source][guide-source])
|
|
39
|
+
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
|
40
|
+
- [discord.js Discord server][discord]
|
|
41
|
+
- [Discord Developers Discord server][discord-developers]
|
|
42
|
+
- [GitHub][source]
|
|
43
|
+
- [npm][npm]
|
|
44
|
+
- [Related libraries][related-libs]
|
|
45
|
+
|
|
46
|
+
## Contributing
|
|
47
|
+
|
|
48
|
+
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
|
49
|
+
[documentation][documentation].
|
|
50
|
+
See [the contribution guide][contributing] if you'd like to submit a PR.
|
|
51
|
+
|
|
52
|
+
## Help
|
|
53
|
+
|
|
54
|
+
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
|
55
|
+
|
|
56
|
+
[website]: https://discord.js.org
|
|
57
|
+
[website-source]: https://github.com/ovencord/ovencord/tree/main/apps/website
|
|
58
|
+
[documentation]: https://discord.js.org/docs/packages/util/stable
|
|
59
|
+
[guide]: https://discordjs.guide
|
|
60
|
+
[guide-source]: https://github.com/ovencord/ovencord/tree/main/apps/guide
|
|
61
|
+
[guide-update]: https://discordjs.guide/legacy/additional-info/changes-in-v14
|
|
62
|
+
[discord]: https://discord.gg/djs
|
|
63
|
+
[discord-developers]: https://discord.gg/discord-developers
|
|
64
|
+
[source]: https://github.com/ovencord/ovencord/tree/main/packages/util
|
|
65
|
+
[npm]: https://www.npmjs.com/package/@ovencord/util
|
|
66
|
+
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
|
67
|
+
[contributing]: https://github.com/ovencord/ovencord/blob/main/.github/CONTRIBUTING.md
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@ovencord/util",
|
|
4
|
+
"version": "1.1.1",
|
|
5
|
+
"description": "Utilities shared across Discord.js packages",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "bun test",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./src/index.ts",
|
|
13
|
+
"main": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"contributors": [
|
|
16
|
+
"Ovencord Contributors"
|
|
17
|
+
],
|
|
18
|
+
"license": "Apache-2.0",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"api",
|
|
21
|
+
"bot",
|
|
22
|
+
"client",
|
|
23
|
+
"node",
|
|
24
|
+
"discordjs"
|
|
25
|
+
],
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/ovencord/ovencord.git",
|
|
29
|
+
"directory": "packages/util"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/ovencord/ovencord/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://ovencord.dev",
|
|
35
|
+
"funding": "https://github.com/ovencord/ovencord?sponsor",
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"discord-api-types": "^0.38.36"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/bun": "^1.3.8",
|
|
41
|
+
"eslint": "^9.39.2",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.54.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"bun": ">=1.0.0"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"module": "./src/index.ts",
|
|
52
|
+
"files": [
|
|
53
|
+
"src"
|
|
54
|
+
]
|
|
55
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A typed EventEmitter shim to replace @vladfrangu/async_event_emitter
|
|
5
|
+
* This version uses the native node:events EventEmitter.
|
|
6
|
+
* Note: strict async emission (awaiting listeners) is not implemented as it was not used by the library internally.
|
|
7
|
+
*/
|
|
8
|
+
export class AsyncEventEmitter<TEvents extends { [K in keyof TEvents]: any[] }> extends EventEmitter {
|
|
9
|
+
public override on<K extends keyof TEvents & string>(event: K, listener: (...args: TEvents[K]) => void): this {
|
|
10
|
+
return super.on(event, listener as any);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public override once<K extends keyof TEvents & string>(event: K, listener: (...args: TEvents[K]) => void): this {
|
|
14
|
+
return super.once(event, listener as any);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public override emit<K extends keyof TEvents & string>(event: K, ...args: TEvents[K]): boolean {
|
|
18
|
+
return super.emit(event, ...args);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public override off<K extends keyof TEvents & string>(event: K, listener: (...args: TEvents[K]) => void): this {
|
|
22
|
+
return super.off(event, listener as any);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public override removeListener<K extends keyof TEvents & string>(event: K, listener: (...args: TEvents[K]) => void): this {
|
|
26
|
+
return super.removeListener(event, listener as any);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple asynchronous queue implementation
|
|
3
|
+
*/
|
|
4
|
+
export class AsyncQueue {
|
|
5
|
+
#promises: Array<{ promise: Promise<void>; resolve: () => void }> = [];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The remaining number of requests in the queue
|
|
9
|
+
*/
|
|
10
|
+
public get remaining(): number {
|
|
11
|
+
return this.#promises.length;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Waits for the queue to be free
|
|
16
|
+
* @param options - Options for the wait
|
|
17
|
+
* @param options.signal - An optional abort signal
|
|
18
|
+
*/
|
|
19
|
+
public wait(options?: { signal?: AbortSignal }): Promise<void> {
|
|
20
|
+
const next = this.#promises.length ? this.#promises.at(-1)!.promise : Promise.resolve();
|
|
21
|
+
|
|
22
|
+
let resolve!: () => void;
|
|
23
|
+
const promise = new Promise<void>((res) => {
|
|
24
|
+
resolve = res;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
this.#promises.push({ promise, resolve });
|
|
28
|
+
|
|
29
|
+
// If no signal, just return the promise we wait on
|
|
30
|
+
if (!options?.signal) {
|
|
31
|
+
return next;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new Promise((res, rej) => {
|
|
35
|
+
if (options.signal!.aborted) {
|
|
36
|
+
// Bridge immediately: when next resolves, we resolve our token
|
|
37
|
+
next.then(() => resolve());
|
|
38
|
+
rej(new Error('AbortError')); // TODO: Use DOMException or standard AbortError if available
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const abortHandler = () => {
|
|
43
|
+
// Bridge: when next resolves, we resolve our token
|
|
44
|
+
next.then(() => resolve());
|
|
45
|
+
rej(new Error('AbortError'));
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
options.signal!.addEventListener('abort', abortHandler, { once: true });
|
|
49
|
+
|
|
50
|
+
next.then(() => {
|
|
51
|
+
options.signal!.removeEventListener('abort', abortHandler);
|
|
52
|
+
res();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Shifts the queue, allowing the next task to run
|
|
59
|
+
*/
|
|
60
|
+
public shift(): void {
|
|
61
|
+
const item = this.#promises.shift();
|
|
62
|
+
item?.resolve();
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/Equatable.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a structure that can be checked against another
|
|
3
|
+
* given structure for equality
|
|
4
|
+
*
|
|
5
|
+
* @typeParam Value - The type of object to compare the current object to
|
|
6
|
+
*/
|
|
7
|
+
export interface Equatable<Value> {
|
|
8
|
+
/**
|
|
9
|
+
* Whether or not this is equal to another structure
|
|
10
|
+
*/
|
|
11
|
+
equals(other: Value): boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Indicates if an object is equatable or not.
|
|
16
|
+
*
|
|
17
|
+
* @param maybeEquatable - The object to check against
|
|
18
|
+
*/
|
|
19
|
+
export function isEquatable(maybeEquatable: unknown): maybeEquatable is Equatable<unknown> {
|
|
20
|
+
return maybeEquatable !== null && typeof maybeEquatable === 'object' && 'equals' in maybeEquatable;
|
|
21
|
+
}
|
package/src/RawFile.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* Represents a file to be added to a request with multipart/form-data encoding
|
|
4
|
+
*/
|
|
5
|
+
export interface RawFile {
|
|
6
|
+
/**
|
|
7
|
+
* Content-Type of the file.
|
|
8
|
+
* If not provided, it will be inferred from the file data when possible
|
|
9
|
+
*
|
|
10
|
+
* @example 'image/png'
|
|
11
|
+
* @example 'application/pdf'
|
|
12
|
+
*/
|
|
13
|
+
contentType?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The actual data for the file
|
|
16
|
+
*/
|
|
17
|
+
data: Uint8Array | boolean | number | string;
|
|
18
|
+
/**
|
|
19
|
+
* An explicit key to use for the formdata field for this file.
|
|
20
|
+
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
|
21
|
+
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
|
22
|
+
*/
|
|
23
|
+
key?: string;
|
|
24
|
+
/**
|
|
25
|
+
* The name of the file. This is the actual filename that will be used when uploading to Discord.
|
|
26
|
+
* This is also the name you'll use to reference the file with attachment:// URLs.
|
|
27
|
+
*
|
|
28
|
+
* @example 'image.png'
|
|
29
|
+
* @example 'document.pdf'
|
|
30
|
+
* @example 'SPOILER_secret.jpeg'
|
|
31
|
+
*/
|
|
32
|
+
name: string;
|
|
33
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { RawFile } from './RawFile.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents an object capable of representing itself as a JSON object
|
|
5
|
+
*
|
|
6
|
+
* @typeParam Value - The JSON type corresponding to {@link JSONEncodable.toJSON} outputs.
|
|
7
|
+
*/
|
|
8
|
+
export interface JSONEncodable<Value> {
|
|
9
|
+
/**
|
|
10
|
+
* Transforms this object to its JSON format
|
|
11
|
+
*/
|
|
12
|
+
toJSON(): Value;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Indicates if an object is encodable or not.
|
|
17
|
+
*
|
|
18
|
+
* @param maybeEncodable - The object to check against
|
|
19
|
+
*/
|
|
20
|
+
export function isJSONEncodable(maybeEncodable: unknown): maybeEncodable is JSONEncodable<unknown> {
|
|
21
|
+
return maybeEncodable !== null && typeof maybeEncodable === 'object' && 'toJSON' in maybeEncodable;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Result of encoding an object that includes file attachments
|
|
26
|
+
*
|
|
27
|
+
* @typeParam BodyValue - The JSON body type
|
|
28
|
+
*/
|
|
29
|
+
export interface FileBodyEncodableResult<BodyValue> {
|
|
30
|
+
/**
|
|
31
|
+
* The JSON body to send with the request
|
|
32
|
+
*/
|
|
33
|
+
body: BodyValue;
|
|
34
|
+
/**
|
|
35
|
+
* The files to attach to the request
|
|
36
|
+
*/
|
|
37
|
+
files: RawFile[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Represents an object capable of representing itself as a request body with file attachments.
|
|
42
|
+
* Objects implementing this interface can separate JSON body data from binary file data,
|
|
43
|
+
* which is necessary for multipart/form-data requests.
|
|
44
|
+
*
|
|
45
|
+
* @typeParam BodyValue - The JSON body type
|
|
46
|
+
*/
|
|
47
|
+
export interface FileBodyEncodable<BodyValue> {
|
|
48
|
+
/**
|
|
49
|
+
* Transforms this object to its file body format, separating the JSON body from file attachments.
|
|
50
|
+
*/
|
|
51
|
+
toFileBody(): FileBodyEncodableResult<BodyValue>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Indicates if an object is file body encodable or not.
|
|
56
|
+
*
|
|
57
|
+
* @param maybeEncodable - The object to check against
|
|
58
|
+
*/
|
|
59
|
+
export function isFileBodyEncodable(maybeEncodable: unknown): maybeEncodable is FileBodyEncodable<unknown> {
|
|
60
|
+
return maybeEncodable !== null && typeof maybeEncodable === 'object' && 'toFileBody' in maybeEncodable;
|
|
61
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculates the shard id for a given guild id.
|
|
3
|
+
*
|
|
4
|
+
* @param guildId - The guild id to calculate the shard id for
|
|
5
|
+
* @param shardCount - The total number of shards
|
|
6
|
+
*/
|
|
7
|
+
export function calculateShardId(guildId: string, shardCount: number) {
|
|
8
|
+
return Number(BigInt(guildId) >> 22n) % shardCount;
|
|
9
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { APIEmbed } from 'discord-api-types/v10';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Calculates the length of an embed.
|
|
5
|
+
*
|
|
6
|
+
* @param data - The embed data to check
|
|
7
|
+
*/
|
|
8
|
+
export function embedLength(data: APIEmbed) {
|
|
9
|
+
return (
|
|
10
|
+
(data.title?.length ?? 0) +
|
|
11
|
+
(data.description?.length ?? 0) +
|
|
12
|
+
(data.fields?.reduce((prev, curr) => prev + curr.name.length + curr.value.length, 0) ?? 0) +
|
|
13
|
+
(data.footer?.text.length ?? 0) +
|
|
14
|
+
(data.author?.name.length ?? 0)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lazy is a wrapper around a value that is computed lazily. It is useful for
|
|
3
|
+
* cases where the value is expensive to compute and the computation may not
|
|
4
|
+
* be needed at all.
|
|
5
|
+
*
|
|
6
|
+
* @param cb - The callback to lazily evaluate
|
|
7
|
+
* @typeParam Value - The type of the value
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const value = lazy(() => computeExpensiveValue());
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
// eslint-disable-next-line
|
|
14
|
+
export function lazy<Value>(cb: () => Value): () => Value {
|
|
15
|
+
let defaultValue: Value;
|
|
16
|
+
// eslint-disable-next-line
|
|
17
|
+
return () => (defaultValue ??= cb());
|
|
18
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for creating a range
|
|
3
|
+
*/
|
|
4
|
+
export interface RangeOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The end of the range (exclusive)
|
|
7
|
+
*/
|
|
8
|
+
end: number;
|
|
9
|
+
/**
|
|
10
|
+
* The start of the range (inclusive)
|
|
11
|
+
*/
|
|
12
|
+
start: number;
|
|
13
|
+
/**
|
|
14
|
+
* The amount to increment by
|
|
15
|
+
*
|
|
16
|
+
* @defaultValue `1`
|
|
17
|
+
*/
|
|
18
|
+
step?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A generator to yield numbers in a given range
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* This method is end-exclusive, for example the last number yielded by `range(5)` is 4. If you
|
|
26
|
+
* prefer for the end to be included add 1 to the range or `end` option.
|
|
27
|
+
* @param range - A number representing the range to yield (exclusive) or an object with start, end and step
|
|
28
|
+
* @example
|
|
29
|
+
* Basic range
|
|
30
|
+
* ```ts
|
|
31
|
+
* for (const number of range(5)) {
|
|
32
|
+
* console.log(number);
|
|
33
|
+
* }
|
|
34
|
+
* // Prints 0, 1, 2, 3, 4
|
|
35
|
+
* ```
|
|
36
|
+
* @example
|
|
37
|
+
* Range with a step
|
|
38
|
+
* ```ts
|
|
39
|
+
* for (const number of range({ start: 3, end: 10, step: 2 })) {
|
|
40
|
+
* console.log(number);
|
|
41
|
+
* }
|
|
42
|
+
* // Prints 3, 5, 7, 9
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export function* range(range: RangeOptions | number) {
|
|
46
|
+
let rangeEnd: number;
|
|
47
|
+
let start = 0;
|
|
48
|
+
let step = 1;
|
|
49
|
+
|
|
50
|
+
if (typeof range === 'number') {
|
|
51
|
+
rangeEnd = range;
|
|
52
|
+
} else {
|
|
53
|
+
start = range.start;
|
|
54
|
+
rangeEnd = range.end;
|
|
55
|
+
step = range.step ?? 1;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
for (let index = start; index < rangeEnd; index += step) {
|
|
59
|
+
yield index;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export function shouldUseGlobalFetchAndWebSocket() {
|
|
4
|
+
// Browser env and deno when ran directly
|
|
5
|
+
if (typeof globalThis.process === 'undefined') {
|
|
6
|
+
return 'fetch' in globalThis && 'WebSocket' in globalThis;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if ('versions' in globalThis.process) {
|
|
10
|
+
return 'deno' in globalThis.process.versions || 'bun' in globalThis.process.versions;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves the user agent appendix string for the current environment.
|
|
5
|
+
*/
|
|
6
|
+
export function getUserAgentAppendix(): string {
|
|
7
|
+
// https://vercel.com/docs/concepts/functions/edge-functions/edge-runtime#check-if-you're-running-on-the-edge-runtime
|
|
8
|
+
// @ts-expect-error Vercel Edge functions
|
|
9
|
+
if (typeof globalThis.EdgeRuntime !== 'undefined') {
|
|
10
|
+
return 'Vercel-Edge-Functions';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error Cloudflare Workers
|
|
14
|
+
if (typeof globalThis.R2 !== 'undefined' && typeof globalThis.WebSocketPair !== 'undefined') {
|
|
15
|
+
// https://developers.cloudflare.com/workers/runtime-apis/web-standards/#navigatoruseragent
|
|
16
|
+
return 'Cloudflare-Workers';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// https://docs.netlify.com/edge-functions/api/#netlify-global-object
|
|
20
|
+
// @ts-expect-error Netlify Edge functions
|
|
21
|
+
if (typeof globalThis.Netlify !== 'undefined') {
|
|
22
|
+
return 'Netlify-Edge-Functions';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Most (if not all) edge environments will have `process` defined. Within a web browser we'll extract it using `navigator.userAgent`.
|
|
26
|
+
if (typeof globalThis.process !== 'object') {
|
|
27
|
+
if (typeof globalThis.navigator === 'object') {
|
|
28
|
+
return globalThis.navigator.userAgent;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return 'UnknownEnvironment';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const versions = (globalThis.process as any).versions;
|
|
35
|
+
if (versions) {
|
|
36
|
+
if ('deno' in versions) {
|
|
37
|
+
return `Deno/${versions.deno}`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if ('bun' in versions) {
|
|
41
|
+
return `Bun/${versions.bun}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if ('node' in versions) {
|
|
45
|
+
return `Node.js/${versions.node}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return 'UnknownEnvironment';
|
|
50
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { GatewayOpcodeRateLimitMetadataMap, GatewayRateLimitedDispatchData } from 'discord-api-types/v10';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Represents the error thrown when the gateway emits a `RATE_LIMITED` event after a certain request.
|
|
5
|
+
*/
|
|
6
|
+
export class GatewayRateLimitError extends Error {
|
|
7
|
+
public override readonly name = GatewayRateLimitError.name;
|
|
8
|
+
|
|
9
|
+
public constructor(
|
|
10
|
+
/**
|
|
11
|
+
* The data associated with the rate limit event
|
|
12
|
+
*/
|
|
13
|
+
public readonly data: GatewayRateLimitedDispatchData<keyof GatewayOpcodeRateLimitMetadataMap>,
|
|
14
|
+
/**
|
|
15
|
+
* The payload data that lead to this rate limit
|
|
16
|
+
*
|
|
17
|
+
* @privateRemarks
|
|
18
|
+
* Too complicated to type properly here (i.e. extract the ['data']
|
|
19
|
+
* of event payloads that have t = keyof GatewayOpcodeRateLimitMetadataMap)
|
|
20
|
+
*/
|
|
21
|
+
public readonly payload: unknown,
|
|
22
|
+
) {
|
|
23
|
+
super(`Request with opcode ${data.opcode} was rate limited. Retry after ${data.retry_after} seconds.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type * from './types.js';
|
|
2
|
+
export * from './functions/index.js';
|
|
3
|
+
export * from './encodables.js';
|
|
4
|
+
export type * from './RawFile.js';
|
|
5
|
+
export * from './Equatable.js';
|
|
6
|
+
export * from './gatewayRateLimitError.js';
|
|
7
|
+
export * from './AsyncEventEmitter.js';
|
|
8
|
+
export * from './AsyncQueue.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The {@link https://github.com/ovencord/ovencord/blob/main/packages/util#readme | @ovencord/util} version
|
|
12
|
+
* that you are currently using.
|
|
13
|
+
*
|
|
14
|
+
* @privateRemarks This needs to explicitly be `string` so it is not typed as a "const string" that gets injected by esbuild.
|
|
15
|
+
*/
|
|
16
|
+
export const version = '[VI]{{inject}}[/VI]' as string;
|
package/src/shims.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type Shims for Bun Native Environment
|
|
3
|
+
*
|
|
4
|
+
* This file provides type declarations to eliminate conflicts between
|
|
5
|
+
* @types/node and @types/bun, allowing clean TypeScript compilation
|
|
6
|
+
* in a pure Bun environment.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
declare global {
|
|
10
|
+
/**
|
|
11
|
+
* Minimal NodeJS namespace declarations for compatibility with libraries
|
|
12
|
+
* that reference Node.js types but run in Bun runtime.
|
|
13
|
+
*/
|
|
14
|
+
namespace NodeJS {
|
|
15
|
+
/**
|
|
16
|
+
* TypedArray union type - maps to Web Standard TypedArrays
|
|
17
|
+
*/
|
|
18
|
+
type TypedArray =
|
|
19
|
+
| Uint8Array
|
|
20
|
+
| Uint8ClampedArray
|
|
21
|
+
| Uint16Array
|
|
22
|
+
| Uint32Array
|
|
23
|
+
| Int8Array
|
|
24
|
+
| Int16Array
|
|
25
|
+
| Int32Array
|
|
26
|
+
| Float32Array
|
|
27
|
+
| Float64Array
|
|
28
|
+
| BigUint64Array
|
|
29
|
+
| BigInt64Array;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Error with errno code - compatible with Bun's error handling
|
|
33
|
+
*/
|
|
34
|
+
interface ErrnoException extends Error {
|
|
35
|
+
errno?: number | undefined;
|
|
36
|
+
code?: string | undefined;
|
|
37
|
+
path?: string | undefined;
|
|
38
|
+
syscall?: string | undefined;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Process CWD function - maps to Bun.cwd()
|
|
43
|
+
*/
|
|
44
|
+
interface Process {
|
|
45
|
+
cwd(): string;
|
|
46
|
+
env: Record<string, string | undefined>;
|
|
47
|
+
platform: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface Timeout extends Number {}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Global declarations
|
|
54
|
+
var process: NodeJS.Process;
|
|
55
|
+
var __dirname: string;
|
|
56
|
+
var __filename: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export {};
|
package/src/types.ts
ADDED