dfx 1.0.5 → 1.0.6

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.
Files changed (237) hide show
  1. package/dist/version.js +1 -1
  2. package/package.json +5 -5
  3. package/src/version.ts +1 -1
  4. package/dist/Cache/driver.d.ts +0 -24
  5. package/dist/Cache/driver.d.ts.map +0 -1
  6. package/dist/Cache/memory.d.ts +0 -5
  7. package/dist/Cache/memory.d.ts.map +0 -1
  8. package/dist/Cache/memoryTTL.d.ts +0 -23
  9. package/dist/Cache/memoryTTL.d.ts.map +0 -1
  10. package/dist/Cache/prelude.d.ts +0 -32
  11. package/dist/Cache/prelude.d.ts.map +0 -1
  12. package/dist/Cache.d.ts +0 -81
  13. package/dist/Cache.d.ts.map +0 -1
  14. package/dist/DiscordConfig.d.ts +0 -35
  15. package/dist/DiscordConfig.d.ts.map +0 -1
  16. package/dist/DiscordGateway/DiscordWS.d.ts +0 -37
  17. package/dist/DiscordGateway/DiscordWS.d.ts.map +0 -1
  18. package/dist/DiscordGateway/Messaging.d.ts +0 -587
  19. package/dist/DiscordGateway/Messaging.d.ts.map +0 -1
  20. package/dist/DiscordGateway/Shard/StateStore.d.ts +0 -24
  21. package/dist/DiscordGateway/Shard/StateStore.d.ts.map +0 -1
  22. package/dist/DiscordGateway/Shard/heartbeats.d.ts +0 -8
  23. package/dist/DiscordGateway/Shard/heartbeats.d.ts.map +0 -1
  24. package/dist/DiscordGateway/Shard/identify.d.ts +0 -12
  25. package/dist/DiscordGateway/Shard/identify.d.ts.map +0 -1
  26. package/dist/DiscordGateway/Shard/sendEvents.d.ts +0 -8
  27. package/dist/DiscordGateway/Shard/sendEvents.d.ts.map +0 -1
  28. package/dist/DiscordGateway/Shard/utils.d.ts +0 -6
  29. package/dist/DiscordGateway/Shard/utils.d.ts.map +0 -1
  30. package/dist/DiscordGateway/Shard.d.ts +0 -29
  31. package/dist/DiscordGateway/Shard.d.ts.map +0 -1
  32. package/dist/DiscordGateway/ShardStore.d.ts +0 -19
  33. package/dist/DiscordGateway/ShardStore.d.ts.map +0 -1
  34. package/dist/DiscordGateway/Sharder.d.ts +0 -14
  35. package/dist/DiscordGateway/Sharder.d.ts.map +0 -1
  36. package/dist/DiscordGateway.d.ts +0 -33
  37. package/dist/DiscordGateway.d.ts.map +0 -1
  38. package/dist/DiscordREST/Generated.d.ts +0 -5577
  39. package/dist/DiscordREST/Generated.d.ts.map +0 -1
  40. package/dist/DiscordREST/utils.d.ts +0 -14
  41. package/dist/DiscordREST/utils.d.ts.map +0 -1
  42. package/dist/DiscordREST.d.ts +0 -22
  43. package/dist/DiscordREST.d.ts.map +0 -1
  44. package/dist/Helpers/flags.d.ts +0 -28
  45. package/dist/Helpers/flags.d.ts.map +0 -1
  46. package/dist/Helpers/intents.d.ts +0 -25
  47. package/dist/Helpers/intents.d.ts.map +0 -1
  48. package/dist/Helpers/interactions.d.ts +0 -116
  49. package/dist/Helpers/interactions.d.ts.map +0 -1
  50. package/dist/Helpers/members.d.ts +0 -10
  51. package/dist/Helpers/members.d.ts.map +0 -1
  52. package/dist/Helpers/permissions.d.ts +0 -42
  53. package/dist/Helpers/permissions.d.ts.map +0 -1
  54. package/dist/Helpers/ui.d.ts +0 -105
  55. package/dist/Helpers/ui.d.ts.map +0 -1
  56. package/dist/Interactions/builder.d.ts +0 -48
  57. package/dist/Interactions/builder.d.ts.map +0 -1
  58. package/dist/Interactions/commandHelper.d.ts +0 -83
  59. package/dist/Interactions/commandHelper.d.ts.map +0 -1
  60. package/dist/Interactions/context.d.ts +0 -53
  61. package/dist/Interactions/context.d.ts.map +0 -1
  62. package/dist/Interactions/definitions.d.ts +0 -43
  63. package/dist/Interactions/definitions.d.ts.map +0 -1
  64. package/dist/Interactions/error.d.ts +0 -2
  65. package/dist/Interactions/error.d.ts.map +0 -1
  66. package/dist/Interactions/gateway.d.ts +0 -20
  67. package/dist/Interactions/gateway.d.ts.map +0 -1
  68. package/dist/Interactions/handlers.d.ts +0 -15
  69. package/dist/Interactions/handlers.d.ts.map +0 -1
  70. package/dist/Interactions/index.d.ts +0 -17
  71. package/dist/Interactions/index.d.ts.map +0 -1
  72. package/dist/Interactions/utils.d.ts +0 -61
  73. package/dist/Interactions/utils.d.ts.map +0 -1
  74. package/dist/Interactions/webhook.d.ts +0 -71
  75. package/dist/Interactions/webhook.d.ts.map +0 -1
  76. package/dist/LICENSE +0 -21
  77. package/dist/README.md +0 -66
  78. package/dist/RateLimit/memory.d.ts +0 -3
  79. package/dist/RateLimit/memory.d.ts.map +0 -1
  80. package/dist/RateLimit/utils.d.ts +0 -3
  81. package/dist/RateLimit/utils.d.ts.map +0 -1
  82. package/dist/RateLimit.d.ts +0 -30
  83. package/dist/RateLimit.d.ts.map +0 -1
  84. package/dist/gateway.d.ts +0 -19
  85. package/dist/gateway.d.ts.map +0 -1
  86. package/dist/index.d.ts +0 -18
  87. package/dist/index.d.ts.map +0 -1
  88. package/dist/mjs/Cache/driver.mjs +0 -3
  89. package/dist/mjs/Cache/driver.mjs.map +0 -1
  90. package/dist/mjs/Cache/memory.mjs +0 -48
  91. package/dist/mjs/Cache/memory.mjs.map +0 -1
  92. package/dist/mjs/Cache/memoryTTL.mjs +0 -119
  93. package/dist/mjs/Cache/memoryTTL.mjs.map +0 -1
  94. package/dist/mjs/Cache/prelude.mjs +0 -131
  95. package/dist/mjs/Cache/prelude.mjs.map +0 -1
  96. package/dist/mjs/Cache.mjs +0 -90
  97. package/dist/mjs/Cache.mjs.map +0 -1
  98. package/dist/mjs/DiscordConfig.mjs +0 -32
  99. package/dist/mjs/DiscordConfig.mjs.map +0 -1
  100. package/dist/mjs/DiscordGateway/DiscordWS.mjs +0 -77
  101. package/dist/mjs/DiscordGateway/DiscordWS.mjs.map +0 -1
  102. package/dist/mjs/DiscordGateway/Messaging.mjs +0 -35
  103. package/dist/mjs/DiscordGateway/Messaging.mjs.map +0 -1
  104. package/dist/mjs/DiscordGateway/Shard/StateStore.mjs +0 -41
  105. package/dist/mjs/DiscordGateway/Shard/StateStore.mjs.map +0 -1
  106. package/dist/mjs/DiscordGateway/Shard/heartbeats.mjs +0 -25
  107. package/dist/mjs/DiscordGateway/Shard/heartbeats.mjs.map +0 -1
  108. package/dist/mjs/DiscordGateway/Shard/identify.mjs +0 -29
  109. package/dist/mjs/DiscordGateway/Shard/identify.mjs.map +0 -1
  110. package/dist/mjs/DiscordGateway/Shard/sendEvents.mjs +0 -26
  111. package/dist/mjs/DiscordGateway/Shard/sendEvents.mjs.map +0 -1
  112. package/dist/mjs/DiscordGateway/Shard/utils.mjs +0 -3
  113. package/dist/mjs/DiscordGateway/Shard/utils.mjs.map +0 -1
  114. package/dist/mjs/DiscordGateway/Shard.mjs +0 -147
  115. package/dist/mjs/DiscordGateway/Shard.mjs.map +0 -1
  116. package/dist/mjs/DiscordGateway/ShardStore.mjs +0 -24
  117. package/dist/mjs/DiscordGateway/ShardStore.mjs.map +0 -1
  118. package/dist/mjs/DiscordGateway/Sharder.mjs +0 -67
  119. package/dist/mjs/DiscordGateway/Sharder.mjs.map +0 -1
  120. package/dist/mjs/DiscordGateway.mjs +0 -21
  121. package/dist/mjs/DiscordGateway.mjs.map +0 -1
  122. package/dist/mjs/DiscordREST/Generated.mjs +0 -2471
  123. package/dist/mjs/DiscordREST/Generated.mjs.map +0 -1
  124. package/dist/mjs/DiscordREST/utils.mjs +0 -21
  125. package/dist/mjs/DiscordREST/utils.mjs.map +0 -1
  126. package/dist/mjs/DiscordREST.mjs +0 -119
  127. package/dist/mjs/DiscordREST.mjs.map +0 -1
  128. package/dist/mjs/Helpers/flags.mjs +0 -45
  129. package/dist/mjs/Helpers/flags.mjs.map +0 -1
  130. package/dist/mjs/Helpers/intents.mjs +0 -27
  131. package/dist/mjs/Helpers/intents.mjs.map +0 -1
  132. package/dist/mjs/Helpers/interactions.mjs +0 -115
  133. package/dist/mjs/Helpers/interactions.mjs.map +0 -1
  134. package/dist/mjs/Helpers/members.mjs +0 -9
  135. package/dist/mjs/Helpers/members.mjs.map +0 -1
  136. package/dist/mjs/Helpers/permissions.mjs +0 -84
  137. package/dist/mjs/Helpers/permissions.mjs.map +0 -1
  138. package/dist/mjs/Helpers/ui.mjs +0 -137
  139. package/dist/mjs/Helpers/ui.mjs.map +0 -1
  140. package/dist/mjs/Interactions/builder.mjs +0 -56
  141. package/dist/mjs/Interactions/builder.mjs.map +0 -1
  142. package/dist/mjs/Interactions/commandHelper.mjs +0 -49
  143. package/dist/mjs/Interactions/commandHelper.mjs.map +0 -1
  144. package/dist/mjs/Interactions/context.mjs +0 -28
  145. package/dist/mjs/Interactions/context.mjs.map +0 -1
  146. package/dist/mjs/Interactions/definitions.mjs +0 -51
  147. package/dist/mjs/Interactions/definitions.mjs.map +0 -1
  148. package/dist/mjs/Interactions/error.mjs +0 -2
  149. package/dist/mjs/Interactions/error.mjs.map +0 -1
  150. package/dist/mjs/Interactions/gateway.mjs +0 -82
  151. package/dist/mjs/Interactions/gateway.mjs.map +0 -1
  152. package/dist/mjs/Interactions/handlers.mjs +0 -64
  153. package/dist/mjs/Interactions/handlers.mjs.map +0 -1
  154. package/dist/mjs/Interactions/index.mjs +0 -11
  155. package/dist/mjs/Interactions/index.mjs.map +0 -1
  156. package/dist/mjs/Interactions/utils.mjs +0 -26
  157. package/dist/mjs/Interactions/utils.mjs.map +0 -1
  158. package/dist/mjs/Interactions/webhook.mjs +0 -75
  159. package/dist/mjs/Interactions/webhook.mjs.map +0 -1
  160. package/dist/mjs/RateLimit/memory.mjs +0 -39
  161. package/dist/mjs/RateLimit/memory.mjs.map +0 -1
  162. package/dist/mjs/RateLimit/utils.mjs +0 -18
  163. package/dist/mjs/RateLimit/utils.mjs.map +0 -1
  164. package/dist/mjs/RateLimit.mjs +0 -28
  165. package/dist/mjs/RateLimit.mjs.map +0 -1
  166. package/dist/mjs/gateway.mjs +0 -19
  167. package/dist/mjs/gateway.mjs.map +0 -1
  168. package/dist/mjs/index.mjs +0 -18
  169. package/dist/mjs/index.mjs.map +0 -1
  170. package/dist/mjs/types.mjs +0 -356
  171. package/dist/mjs/types.mjs.map +0 -1
  172. package/dist/mjs/utils/Effect.mjs +0 -44
  173. package/dist/mjs/utils/Effect.mjs.map +0 -1
  174. package/dist/mjs/version.mjs +0 -2
  175. package/dist/mjs/version.mjs.map +0 -1
  176. package/dist/mjs/webhooks.mjs +0 -6
  177. package/dist/mjs/webhooks.mjs.map +0 -1
  178. package/dist/package.json +0 -48
  179. package/dist/src/Cache/driver.ts +0 -37
  180. package/dist/src/Cache/memory.ts +0 -83
  181. package/dist/src/Cache/memoryTTL.ts +0 -225
  182. package/dist/src/Cache/prelude.ts +0 -279
  183. package/dist/src/Cache.ts +0 -261
  184. package/dist/src/DiscordConfig.ts +0 -67
  185. package/dist/src/DiscordGateway/DiscordWS.ts +0 -157
  186. package/dist/src/DiscordGateway/Messaging.ts +0 -71
  187. package/dist/src/DiscordGateway/Shard/StateStore.ts +0 -72
  188. package/dist/src/DiscordGateway/Shard/heartbeats.ts +0 -60
  189. package/dist/src/DiscordGateway/Shard/identify.ts +0 -50
  190. package/dist/src/DiscordGateway/Shard/sendEvents.ts +0 -43
  191. package/dist/src/DiscordGateway/Shard/utils.ts +0 -13
  192. package/dist/src/DiscordGateway/Shard.ts +0 -205
  193. package/dist/src/DiscordGateway/ShardStore.ts +0 -42
  194. package/dist/src/DiscordGateway/Sharder.ts +0 -118
  195. package/dist/src/DiscordGateway.ts +0 -79
  196. package/dist/src/DiscordREST/Generated.ts +0 -10832
  197. package/dist/src/DiscordREST/utils.ts +0 -42
  198. package/dist/src/DiscordREST.ts +0 -256
  199. package/dist/src/Helpers/flags.ts +0 -73
  200. package/dist/src/Helpers/intents.ts +0 -35
  201. package/dist/src/Helpers/interactions.ts +0 -317
  202. package/dist/src/Helpers/members.ts +0 -18
  203. package/dist/src/Helpers/permissions.ts +0 -145
  204. package/dist/src/Helpers/ui.ts +0 -253
  205. package/dist/src/Interactions/builder.ts +0 -171
  206. package/dist/src/Interactions/commandHelper.ts +0 -231
  207. package/dist/src/Interactions/context.ts +0 -104
  208. package/dist/src/Interactions/definitions.ts +0 -144
  209. package/dist/src/Interactions/error.ts +0 -3
  210. package/dist/src/Interactions/gateway.ts +0 -193
  211. package/dist/src/Interactions/handlers.ts +0 -108
  212. package/dist/src/Interactions/index.ts +0 -30
  213. package/dist/src/Interactions/utils.ts +0 -93
  214. package/dist/src/Interactions/webhook.ts +0 -204
  215. package/dist/src/RateLimit/memory.ts +0 -63
  216. package/dist/src/RateLimit/utils.ts +0 -28
  217. package/dist/src/RateLimit.ts +0 -90
  218. package/dist/src/gateway.ts +0 -50
  219. package/dist/src/index.ts +0 -44
  220. package/dist/src/types.ts +0 -3090
  221. package/dist/src/utils/Effect.ts +0 -65
  222. package/dist/src/version.ts +0 -1
  223. package/dist/src/webhooks.ts +0 -25
  224. package/dist/tsconfig.base.json +0 -54
  225. package/dist/tsconfig.build.json +0 -10
  226. package/dist/tsconfig.examples.json +0 -10
  227. package/dist/tsconfig.json +0 -14
  228. package/dist/tsconfig.madge.json +0 -12
  229. package/dist/tsconfig.test.json +0 -10
  230. package/dist/types.d.ts +0 -2458
  231. package/dist/types.d.ts.map +0 -1
  232. package/dist/utils/Effect.d.ts +0 -5
  233. package/dist/utils/Effect.d.ts.map +0 -1
  234. package/dist/version.d.ts +0 -2
  235. package/dist/version.d.ts.map +0 -1
  236. package/dist/webhooks.d.ts +0 -8
  237. package/dist/webhooks.d.ts.map +0 -1
@@ -1,204 +0,0 @@
1
- import * as Chunk from "effect/Chunk"
2
- import { identity } from "effect/Function"
3
- import * as Option from "effect/Option"
4
- import type * as Cause from "effect/Cause"
5
- import type * as Config from "effect/Config"
6
- import * as Redacted from "effect/Redacted"
7
- import * as Effect from "effect/Effect"
8
- import * as Layer from "effect/Layer"
9
- import * as ServiceMap from "effect/ServiceMap"
10
- import type * as D from "./definitions.ts"
11
- import type { DefinitionNotFound } from "./handlers.ts"
12
- import { handlers } from "./handlers.ts"
13
- import type { DiscordInteraction, InteractionBuilder } from "./index.ts"
14
- import { Interaction } from "./index.ts"
15
- import type * as Discord from "../types.ts"
16
- import * as Verify from "discord-verify"
17
- import * as Data from "effect/Data"
18
-
19
- export class BadWebhookSignature {
20
- readonly _tag = "BadWebhookSignature"
21
- }
22
-
23
- export type Headers = Record<string, string | Array<string> | undefined>
24
-
25
- const checkSignature = (
26
- publicKey: string,
27
- headers: Headers,
28
- body: string,
29
- crypto: SubtleCrypto,
30
- algorithm: any,
31
- ) =>
32
- Effect.fromOption(
33
- Option.all({
34
- signature: Option.fromNullishOr(headers["x-signature-ed25519"]),
35
- timestamp: Option.fromNullishOr(headers["x-signature-timestamp"]),
36
- }),
37
- ).pipe(
38
- Effect.flatMap(_ =>
39
- Effect.promise(() =>
40
- Verify.verify(
41
- body,
42
- _.signature as string,
43
- _.timestamp as string,
44
- publicKey,
45
- crypto,
46
- algorithm,
47
- ),
48
- ),
49
- ),
50
- Effect.filterOrFail(identity, () => new BadWebhookSignature()),
51
- Effect.catchCause(() => Effect.fail(new BadWebhookSignature())),
52
- Effect.asVoid,
53
- )
54
-
55
- export interface MakeConfigOpts {
56
- readonly applicationId: string
57
- readonly publicKey: Redacted.Redacted
58
- readonly crypto: SubtleCrypto
59
- readonly algorithm: keyof typeof Verify.PlatformAlgorithm
60
- }
61
- const makeConfig = ({
62
- algorithm,
63
- applicationId,
64
- crypto,
65
- publicKey,
66
- }: MakeConfigOpts) => ({
67
- applicationId,
68
- publicKey: Redacted.value(publicKey),
69
- crypto,
70
- algorithm: Verify.PlatformAlgorithm[algorithm],
71
- })
72
-
73
- export class WebhookConfig extends ServiceMap.Service<
74
- WebhookConfig,
75
- ReturnType<typeof makeConfig>
76
- >()("dfx/Interactions/WebhookConfig") {}
77
-
78
- export const layer = (opts: MakeConfigOpts) =>
79
- Layer.succeed(WebhookConfig, makeConfig(opts))
80
-
81
- export const layerConfig: (
82
- config: Config.Config<MakeConfigOpts>,
83
- ) => Layer.Layer<WebhookConfig, Config.ConfigError> = (
84
- config: Config.Config<MakeConfigOpts>,
85
- ) => Layer.effect(WebhookConfig, Effect.map(config.asEffect(), makeConfig))
86
-
87
- export class WebhookParseError extends Data.TaggedError("WebhookParseError")<{
88
- cause: unknown
89
- }> {}
90
-
91
- const fromHeadersAndBody = (headers: Headers, body: string) =>
92
- WebhookConfig.use(({ algorithm, crypto, publicKey }) =>
93
- checkSignature(publicKey, headers, body, crypto, algorithm).pipe(
94
- Effect.andThen(
95
- Effect.try({
96
- try: () => JSON.parse(body) as Discord.APIInteraction,
97
- catch: cause => new WebhookParseError({ cause }),
98
- }),
99
- ),
100
- ),
101
- )
102
-
103
- const run = <R, E>(
104
- definitions: Chunk.Chunk<
105
- readonly [
106
- handler: D.InteractionDefinition<R, E>,
107
- transform: (
108
- self: Effect.Effect<Discord.CreateInteractionResponseRequest, E, R>,
109
- ) => Effect.Effect<Discord.CreateInteractionResponseRequest, E, R>,
110
- ]
111
- >,
112
- handleResponse: (
113
- ix: Discord.APIInteraction,
114
- _: Discord.CreateInteractionResponseRequest,
115
- ) => Effect.Effect<Discord.CreateInteractionResponseRequest, E, R>,
116
- ) => {
117
- const handler = handlers(definitions, handleResponse)
118
- return (
119
- headers: Headers,
120
- body: string,
121
- ): Effect.Effect<
122
- Discord.CreateInteractionResponseRequest,
123
- BadWebhookSignature | WebhookParseError | E | DefinitionNotFound,
124
- WebhookConfig | Exclude<R, DiscordInteraction>
125
- > =>
126
- Effect.flatMap(fromHeadersAndBody(headers, body), interaction =>
127
- Effect.withSpan(
128
- Effect.provideService(
129
- handler[interaction.type](interaction),
130
- Interaction,
131
- interaction,
132
- ),
133
- "dfx.Interaction",
134
- {
135
- attributes: { interactionId: interaction.id },
136
- captureStackTrace: false,
137
- },
138
- ),
139
- )
140
- }
141
-
142
- export interface HandleWebhookOpts<E> {
143
- headers: Headers
144
- body: string
145
- success: (a: Discord.CreateInteractionResponseRequest) => Effect.Effect<void>
146
- error: (e: Cause.Cause<E>) => Effect.Effect<void>
147
- }
148
-
149
- /**
150
- * @tsplus getter dfx/InteractionBuilder webhookHandler
151
- */
152
- export const makeHandler = <R, E, TE>(
153
- ix: InteractionBuilder<R, E, TE>,
154
- ): (({
155
- body,
156
- error,
157
- headers,
158
- success,
159
- }: HandleWebhookOpts<
160
- E | WebhookParseError | BadWebhookSignature | DefinitionNotFound
161
- >) => Effect.Effect<void, never, WebhookConfig>) => {
162
- const handle = run(
163
- Chunk.map(ix.definitions, ([d]) => [d, identity] as any),
164
- (_i, r) => Effect.succeed(r),
165
- )
166
-
167
- return ({
168
- body,
169
- error,
170
- headers,
171
- success,
172
- }: HandleWebhookOpts<
173
- E | WebhookParseError | BadWebhookSignature | DefinitionNotFound
174
- >): Effect.Effect<void, never, WebhookConfig> =>
175
- handle(headers, body).pipe(
176
- Effect.flatMap(success),
177
- Effect.catchCause(error),
178
- )
179
- }
180
-
181
- /**
182
- * @tsplus getter dfx/InteractionBuilder simpleWebhookHandler
183
- */
184
- export const makeSimpleHandler = <R, E, TE>(
185
- ix: InteractionBuilder<R, E, TE>,
186
- ): (({
187
- body,
188
- headers,
189
- }: {
190
- headers: Headers
191
- body: string
192
- }) => Effect.Effect<
193
- Discord.CreateInteractionResponseRequest,
194
- BadWebhookSignature | WebhookParseError | DefinitionNotFound,
195
- WebhookConfig
196
- >) => {
197
- const handle = run(
198
- Chunk.map(ix.definitions, ([d]) => [d, identity] as any),
199
- (_i, r) => Effect.succeed(r),
200
- )
201
-
202
- return ({ body, headers }: { headers: Headers; body: string }) =>
203
- handle(headers, body)
204
- }
@@ -1,63 +0,0 @@
1
- import * as Option from "effect/Option"
2
- import * as Effect from "effect/Effect"
3
- import type { BucketDetails, RateLimitStoreService } from "../RateLimit.ts"
4
-
5
- interface Counter {
6
- count: number
7
- expires: number
8
- }
9
-
10
- export const make = (): RateLimitStoreService => {
11
- const buckets = new Map<string, BucketDetails>()
12
- const routes = new Map<string, string>()
13
- const counters = new Map<string, Counter>()
14
-
15
- const getCounter = (key: string) =>
16
- Option.filter(
17
- Option.fromNullishOr(counters.get(key)),
18
- c => c.expires > Date.now(),
19
- )
20
-
21
- const getBucketForRoute = (route: string) =>
22
- Effect.sync(() => Option.fromNullishOr(buckets.get(routes.get(route)!)))
23
-
24
- return {
25
- hasBucket: key => Effect.sync(() => buckets.has(key)),
26
-
27
- putBucket: bucket =>
28
- Effect.sync(() => {
29
- buckets.set(bucket.key, bucket)
30
- }),
31
-
32
- getBucketForRoute,
33
-
34
- putBucketRoute: (route, bucket) =>
35
- Effect.sync(() => {
36
- routes.set(route, bucket)
37
- }),
38
-
39
- removeCounter: key =>
40
- Effect.sync(() => {
41
- counters.delete(key)
42
- }),
43
-
44
- incrementCounter: (key, window, limit) =>
45
- Effect.sync(() => {
46
- const now = Date.now()
47
- const perRequest = Math.ceil(window / limit)
48
- const counter = Option.getOrElse(
49
- getCounter(key),
50
- (): Counter => ({
51
- expires: now,
52
- count: 0,
53
- }),
54
- )
55
-
56
- const count = counter.count + 1
57
- const expires = counter.expires + perRequest
58
- counters.set(key, { ...counter, count, expires })
59
-
60
- return [count, expires - now]
61
- }),
62
- }
63
- }
@@ -1,28 +0,0 @@
1
- import type { Duration } from "effect/Duration"
2
- import { millis, zero } from "effect/Duration"
3
-
4
- export const delayFrom = (
5
- window: number,
6
- limit: number,
7
- count: number,
8
- ttl: number,
9
- ): Duration => {
10
- const perRequest = Math.ceil(window / limit)
11
-
12
- const totalTime = count * perRequest
13
- const elapsedTime = totalTime - ttl
14
- const elapsedWindows = Math.floor(elapsedTime / window)
15
- const completedRequests = elapsedWindows * limit
16
- const remainingRequests = count - completedRequests
17
- const remainingWindows = Math.floor((remainingRequests - 1) / limit)
18
- const delayRemainder = elapsedTime % window
19
-
20
- const requestRemainder = count % limit
21
- const staggerDelay = requestRemainder * 50
22
-
23
- if (remainingWindows === 0) {
24
- return zero
25
- }
26
-
27
- return millis(remainingWindows * window - delayRemainder + staggerDelay)
28
- }
@@ -1,90 +0,0 @@
1
- import * as Duration from "effect/Duration"
2
- import type * as Option from "effect/Option"
3
- import * as Effect from "effect/Effect"
4
- import * as Layer from "effect/Layer"
5
- import * as Memory from "./RateLimit/memory.ts"
6
- import { delayFrom } from "./RateLimit/utils.ts"
7
- import * as ServiceMap from "effect/ServiceMap"
8
-
9
- export type BucketDetails = {
10
- key: "global" | string
11
- resetAfter: number
12
- limit: number
13
- }
14
-
15
- export interface RateLimitStoreService {
16
- readonly hasBucket: (bucketKey: string) => Effect.Effect<boolean>
17
-
18
- readonly putBucket: (bucket: BucketDetails) => Effect.Effect<void>
19
-
20
- readonly getBucketForRoute: (
21
- route: string,
22
- ) => Effect.Effect<Option.Option<BucketDetails>>
23
-
24
- readonly putBucketRoute: (
25
- route: string,
26
- bucketKey: string,
27
- ) => Effect.Effect<void>
28
-
29
- readonly incrementCounter: (
30
- key: string,
31
- window: number,
32
- limit: number,
33
- ) => Effect.Effect<readonly [count: number, ttl: number]>
34
-
35
- readonly removeCounter: (key: string) => Effect.Effect<void>
36
- }
37
-
38
- export class RateLimitStore extends ServiceMap.Service<
39
- RateLimitStore,
40
- RateLimitStoreService
41
- >()("dfx/RateLimit/RateLimitStore") {}
42
-
43
- export const MemoryRateLimitStoreLive = Layer.sync(RateLimitStore, Memory.make)
44
-
45
- const makeLimiter = Effect.gen(function* () {
46
- const store = yield* RateLimitStore
47
-
48
- const maybeWait = (
49
- key: string,
50
- window: Duration.Duration,
51
- limit: number,
52
- multiplier = 1.05,
53
- ) => {
54
- const windowMs = Duration.toMillis(window) * multiplier
55
-
56
- return store.incrementCounter(key, windowMs, limit).pipe(
57
- Effect.map(([count, ttl]) => delayFrom(windowMs, limit, count, ttl)),
58
- Effect.tap(d =>
59
- Effect.annotateLogs(Effect.logTrace("maybeWait"), {
60
- service: "RateLimit",
61
- key,
62
- window: Duration.toMillis(window),
63
- windowMs,
64
- limit,
65
- delay: Duration.toMillis(d),
66
- }),
67
- ),
68
- Effect.tap(_ =>
69
- Duration.toMillis(_) === 0 ? Effect.void : Effect.sleep(_),
70
- ),
71
- Effect.asVoid,
72
- )
73
- }
74
-
75
- return { maybeWait }
76
- })
77
-
78
- export class RateLimiter extends ServiceMap.Service<
79
- RateLimiter,
80
- {
81
- maybeWait: (
82
- key: string,
83
- window: Duration.Duration,
84
- limit: number,
85
- multiplier?: number,
86
- ) => Effect.Effect<void, never, never>
87
- }
88
- >()("dfx/RateLimit/RateLimiter") {}
89
-
90
- export const RateLimiterLive = Layer.effect(RateLimiter, makeLimiter)
@@ -1,50 +0,0 @@
1
- import * as CachePrelude from "./Cache/prelude.ts"
2
- import type { DiscordGateway } from "./DiscordGateway.ts"
3
- import { DiscordGatewayLive } from "./DiscordGateway.ts"
4
- import * as DiscordWS from "./DiscordGateway/DiscordWS.ts"
5
- import { JsonDiscordWSCodecLive } from "./DiscordGateway/DiscordWS.ts"
6
- import * as Shard from "./DiscordGateway/Shard.ts"
7
- import { ShardStateStore } from "./DiscordGateway/Shard/StateStore.ts"
8
- import * as SendEvent from "./DiscordGateway/Shard/sendEvents.ts"
9
- import * as ShardStore from "./DiscordGateway/ShardStore.ts"
10
- import { MemoryShardStoreLive } from "./DiscordGateway/ShardStore.ts"
11
- import type { DiscordREST } from "./DiscordREST.ts"
12
- import { DiscordRESTLive } from "./DiscordREST.ts"
13
- import type { InteractionsRegistry } from "./Interactions/gateway.ts"
14
- import { InteractionsRegistryLive } from "./Interactions/gateway.ts"
15
- import type { RateLimiter } from "./RateLimit.ts"
16
- import { MemoryRateLimitStoreLive, RateLimiterLive } from "./RateLimit.ts"
17
- import * as Layer from "effect/Layer"
18
- import type * as HttpClient from "effect/unstable/http/HttpClient"
19
- import type { DiscordConfig } from "./DiscordConfig.ts"
20
- import type { WebSocketConstructor } from "effect/unstable/socket/Socket"
21
-
22
- export { DiscordGateway, DiscordGatewayLive } from "./DiscordGateway.ts"
23
-
24
- export {
25
- InteractionsRegistry,
26
- InteractionsRegistryLive,
27
- interactionsSync,
28
- run as runIx,
29
- setInteractionsSync,
30
- } from "./Interactions/gateway.ts"
31
-
32
- export { CachePrelude, DiscordWS, SendEvent, Shard, ShardStore }
33
-
34
- export const DiscordLive: Layer.Layer<
35
- RateLimiter | DiscordGateway | DiscordREST,
36
- never,
37
- DiscordConfig | WebSocketConstructor | HttpClient.HttpClient
38
- > = Layer.mergeAll(RateLimiterLive, DiscordGatewayLive).pipe(
39
- Layer.provideMerge(DiscordRESTLive),
40
- Layer.provide(JsonDiscordWSCodecLive),
41
- Layer.provide(MemoryRateLimitStoreLive),
42
- Layer.provide(MemoryShardStoreLive),
43
- Layer.provide(ShardStateStore.MemoryLive),
44
- )
45
-
46
- export const DiscordIxLive: Layer.Layer<
47
- RateLimiter | DiscordGateway | DiscordREST | InteractionsRegistry,
48
- never,
49
- DiscordConfig | WebSocketConstructor | HttpClient.HttpClient
50
- > = InteractionsRegistryLive.pipe(Layer.provideMerge(DiscordLive))
package/dist/src/index.ts DELETED
@@ -1,44 +0,0 @@
1
- import type { HttpClient } from "effect/unstable/http/HttpClient"
2
- import * as Cache from "./Cache.ts"
3
- import * as DiscordConfig from "./DiscordConfig.ts"
4
- import type { DiscordREST } from "./DiscordREST.ts"
5
- import { DiscordRESTLive } from "./DiscordREST.ts"
6
- import * as Flags from "./Helpers/flags.ts"
7
- import * as Intents from "./Helpers/intents.ts"
8
- import * as IxHelpers from "./Helpers/interactions.ts"
9
- import * as Members from "./Helpers/members.ts"
10
- import * as Perms from "./Helpers/permissions.ts"
11
- import * as UI from "./Helpers/ui.ts"
12
- import * as Ix from "./Interactions/index.ts"
13
- import { MemoryRateLimitStoreLive } from "./RateLimit.ts"
14
- import * as Discord from "./types.ts"
15
- import * as Layer from "effect/Layer"
16
-
17
- export { DiscordREST, DiscordRESTLive } from "./DiscordREST.ts"
18
-
19
- export {
20
- type BucketDetails,
21
- MemoryRateLimitStoreLive,
22
- RateLimitStore,
23
- RateLimiter,
24
- RateLimiterLive,
25
- } from "./RateLimit.ts"
26
-
27
- export {
28
- Cache,
29
- Discord,
30
- DiscordConfig,
31
- Flags,
32
- Intents,
33
- Ix,
34
- IxHelpers,
35
- Members,
36
- Perms,
37
- UI,
38
- }
39
-
40
- export const DiscordRESTMemoryLive: Layer.Layer<
41
- DiscordREST,
42
- never,
43
- DiscordConfig.DiscordConfig | HttpClient
44
- > = DiscordRESTLive.pipe(Layer.provide(MemoryRateLimitStoreLive))