shay
shay
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by shay on 7/11/2024 in #djs-questions
Non-internal sharding without using a full `Client` instance
Hi there, I own a large bot that I'm rewriting to be mostly HTTP-based. However, there are a number of features that require a gateway connection, so I am going to be running a discord.js application alongside it which will proxy events to the HTTP server (the latency introduced with this method is not a big concern). I have written the gateway-enabled bot portion with @discordjs/rest and @discordjs/ws in order to minimize ram usage. Unfortunately it seems that ShardingManager requires you to use a full-fat Client instance, which I don't want to do because of the limited cache reduction options - I do not need any cache, only an array of guild IDs for each shard, which I am currently tracking myself.
% node src/index.js
[launched] Shard ID 0
[hello] Shard ID 0
ready with 1 shards on 2 guilds
/Users/shay/Documents/GitHub/project/packages/bot-ws/node_modules/discord.js/src/sharding/Shard.js:183
reject(new DiscordjsError(ErrorCodes.ShardingReadyTimeout, this.id));
^

Error [ShardingReadyTimeout]: Shard 0's Client took too long to become ready.
at Timeout.onTimeout (/Users/shay/Documents/GitHub/project/packages/bot-ws/node_modules/discord.js/src/sharding/Shard.js:183:16)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
code: 'ShardingReadyTimeout'
}
% node src/index.js
[launched] Shard ID 0
[hello] Shard ID 0
ready with 1 shards on 2 guilds
/Users/shay/Documents/GitHub/project/packages/bot-ws/node_modules/discord.js/src/sharding/Shard.js:183
reject(new DiscordjsError(ErrorCodes.ShardingReadyTimeout, this.id));
^

Error [ShardingReadyTimeout]: Shard 0's Client took too long to become ready.
at Timeout.onTimeout (/Users/shay/Documents/GitHub/project/packages/bot-ws/node_modules/discord.js/src/sharding/Shard.js:183:16)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
code: 'ShardingReadyTimeout'
}
^ the ready with [...] log there is from my WebSocketManager#on WebSocketShardEvents.Ready callback. The manager is ready but it seems like the parent ShardManager expects a Client instead. Is there any way to maintain my desired philosophy? Thanks discord.js 14.15.3 @discordjs/rest 2.3.0 @discordjs/ws 1.1.1 node 21.6.2
21 replies
CDCloudflare Developers
Created by shay on 7/8/2024 in #workers-help
Unable to bind durable objects on Remix + Vite project
Hello, I'm trying to incorporate a durable object into my project that I have recently moved to Vite (from the classic Remix compiler). However I think this issue may be more relevant to Wrangler due to the nature of the error. I'm on MacOS and using Node 21.6.2. When running wrangler types (which in turn runs my Vite build process):
% npx wrangler types

⛅️ wrangler 3.63.1
-------------------

Running custom build: npm run build

> project@3.0.0 build
> remix vite:build

Using vars defined in .dev.vars
service core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER: Worker "core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER"'s binding "BasicDurableObject" refers to a service "core:user:worker", but no such service is defined.
MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
at #assembleAndUpdateConfig (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9178:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Mutex.runWith (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:3521:16)
at async #waitForReady (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9235:5)
at async Miniflare2._getProxyClient (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9359:5)
at async Miniflare2.getBindings (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9378:25)
at async getPlatformProxy (/Users/shay/Documents/GitHub/project/packages/site/node_modules/wrangler/wrangler-dist/cli.js:210460:20)
at async configureServer (/Users/shay/Documents/GitHub/project/packages/site/node_modules/@remix-run/dev/dist/vite/cloudflare-proxy-plugin.js:55:11)
at async _createServer (file:///Users/shay/Documents/GitHub/project/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:63370:20)
at async configResolved (/Users/shay/Documents/GitHub/project/packages/site/node_modules/@remix-run/dev/dist/vite/plugin.js:750:27) {
code: 'ERR_RUNTIME_FAILURE',
cause: undefined
}
npm ERR! Lifecycle script `build` failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: project@3.0.0
npm ERR! at location: /Users/shay/Documents/GitHub/project/packages/site

✘ [ERROR] Running custom build `npm run build` failed. There are likely more logs from your build command above.
% npx wrangler types

⛅️ wrangler 3.63.1
-------------------

Running custom build: npm run build

> project@3.0.0 build
> remix vite:build

Using vars defined in .dev.vars
service core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER: Worker "core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER"'s binding "BasicDurableObject" refers to a service "core:user:worker", but no such service is defined.
MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.
at #assembleAndUpdateConfig (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9178:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Mutex.runWith (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:3521:16)
at async #waitForReady (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9235:5)
at async Miniflare2._getProxyClient (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9359:5)
at async Miniflare2.getBindings (/Users/shay/Documents/GitHub/project/packages/site/node_modules/miniflare/dist/src/index.js:9378:25)
at async getPlatformProxy (/Users/shay/Documents/GitHub/project/packages/site/node_modules/wrangler/wrangler-dist/cli.js:210460:20)
at async configureServer (/Users/shay/Documents/GitHub/project/packages/site/node_modules/@remix-run/dev/dist/vite/cloudflare-proxy-plugin.js:55:11)
at async _createServer (file:///Users/shay/Documents/GitHub/project/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:63370:20)
at async configResolved (/Users/shay/Documents/GitHub/project/packages/site/node_modules/@remix-run/dev/dist/vite/plugin.js:750:27) {
code: 'ERR_RUNTIME_FAILURE',
cause: undefined
}
npm ERR! Lifecycle script `build` failed with error:
npm ERR! Error: command failed
npm ERR! in workspace: project@3.0.0
npm ERR! at location: /Users/shay/Documents/GitHub/project/packages/site

✘ [ERROR] Running custom build `npm run build` failed. There are likely more logs from your build command above.
service core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER: Worker "core:user:__WRANGLER_EXTERNAL_DURABLE_OBJECTS_WORKER"'s binding "BasicDurableObject" refers to a service "core:user:worker", but no such service is defined.
Am I defining my binding incorrectly? I don't really understand what to do with this information and I couldn't find anything online. My object, which is exported from my main file:
export class BasicDurableObject extends DurableObject {
constructor(state, env) {
super();
}

async fetch(request) {
return new Response(undefined, { status: 204 });
}
}
export class BasicDurableObject extends DurableObject {
constructor(state, env) {
super();
}

async fetch(request) {
return new Response(undefined, { status: 204 });
}
}
binding:
[[durable_objects.bindings]]
name = "BASIC_DO"
class_name = "BasicDurableObject"
[[durable_objects.bindings]]
name = "BASIC_DO"
class_name = "BasicDurableObject"
3 replies
DTDrizzle Team
Created by shay on 7/7/2024 in #help
One-to-many relation always returning no records for the `many` relation
Hello, I need some help debugging a relation that I have set up for a postgres database. The schema is as follows:
// `snowflakePk()` is a shortcut for generating a snowflake as the primary key with the name `id`
export const flows = pgTable("Flow", {
id: snowflakePk(),
name: text("name"),
});

export const flowsRelations = relations(flows, ({ many }) => ({
actions: many(flowActions, { relationName: "Flow_Action" }),
}));

export const flowActions = pgTable("Action", {
id: snowflakePk(),
type: integer("type").notNull().$type<FlowActionType>(),
data: json("data").notNull().$type<FlowAction>(),
flowId: snowflake("flowId")
.notNull()
.references(() => flows.id, { onDelete: "cascade" }),
});

export const flowActionsRelations = relations(flowActions, ({ one }) => ({
flow: one(flows, {
fields: [flowActions.id],
references: [flows.id],
relationName: "Flow_Action",
}),
}));
// `snowflakePk()` is a shortcut for generating a snowflake as the primary key with the name `id`
export const flows = pgTable("Flow", {
id: snowflakePk(),
name: text("name"),
});

export const flowsRelations = relations(flows, ({ many }) => ({
actions: many(flowActions, { relationName: "Flow_Action" }),
}));

export const flowActions = pgTable("Action", {
id: snowflakePk(),
type: integer("type").notNull().$type<FlowActionType>(),
data: json("data").notNull().$type<FlowAction>(),
flowId: snowflake("flowId")
.notNull()
.references(() => flows.id, { onDelete: "cascade" }),
});

export const flowActionsRelations = relations(flowActions, ({ one }) => ({
flow: one(flows, {
fields: [flowActions.id],
references: [flows.id],
relationName: "Flow_Action",
}),
}));
The expected behavior here is to be able to query flows { with: { actions: true }} and get all Action records that have the same flowId as the "parent" Flow. However, this is not the case:
const flow = await db.query.flows.findFirst({
where: (flows, { eq }) => eq(flows.id, 57101725823143939n),
columns: { id: true },
with: { actions: true },
});
console.log(flow); // { id: 57101725823143939n, actions: [] }
const flow = await db.query.flows.findFirst({
where: (flows, { eq }) => eq(flows.id, 57101725823143939n),
columns: { id: true },
with: { actions: true },
});
console.log(flow); // { id: 57101725823143939n, actions: [] }
psql query proving that the expected records do exist:
shay=# select * from "Action" where "flowId" = 57101725823143939;
id | type | data | flowId
-------------------+------+-------------------------------+-------------------
57101725831532548 | 0 | "{\"type\":0}" | 57101725823143939
(1 row)
shay=# select * from "Action" where "flowId" = 57101725823143939;
id | type | data | flowId
-------------------+------+-------------------------------+-------------------
57101725831532548 | 0 | "{\"type\":0}" | 57101725823143939
(1 row)
So what is going on here? Thanks.
drizzle-kit: v0.22.7
drizzle-orm: v0.31.2
drizzle-kit: v0.22.7
drizzle-orm: v0.31.2
7 replies
CDCloudflare Developers
Created by shay on 2/8/2024 in #workers-help
Failed module resolutions using `pg` and Drizzle
Hi there, I'm using this Remix template, which works well on its own, but when I attempt to use the pg package to interface with my remote Postgres database, Miniflare is suddenly unable to resolve a number of dependencies after Remix finishes building.
✘ [ERROR] Could not resolve "events"

../../node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
✘ [ERROR] Could not resolve "events"

../../node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
Reproduce 1. npx create-remix@latest directory-name --template https://github.com/remix-run/remix/tree/main/templates/cloudflare-workers 2. cd directory-name 3. npm install pg drizzle-orm 4. npm install --save-dev @types/pg 4. Create an app/store.server.ts:
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

export const getDb = () => {
const pool = new Pool({ connectionString: "postgres://..." });
return drizzle(pool);
};
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

export const getDb = () => {
const pool = new Pool({ connectionString: "postgres://..." });
return drizzle(pool);
};
7. And import it in a route:
// app/routes/_index.tsx
import { getDb } from "~/store.server";

export const loader = async () => {
const db = getDb();
return null;
}

...
// app/routes/_index.tsx
import { getDb } from "~/store.server";

export const loader = async () => {
const db = getDb();
return null;
}

...
8. npm run dev - see above errors platform: node Naturally, I would like to not build for Node and be forced to use node_compat, but complying with the previous error anyway yields the following:
✘ [ERROR] service core:user:remix-cloudflare-workers: Uncaught Error: Dynamic require of "events" is not supported

at null.<anonymous> (index.js:6:9)
at null.<anonymous> (index.js:33583:25) in node_modules/pg/lib/client.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:34323:18) in node_modules/pg/lib/index.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:35442:26)
✘ [ERROR] service core:user:remix-cloudflare-workers: Uncaught Error: Dynamic require of "events" is not supported

at null.<anonymous> (index.js:6:9)
at null.<anonymous> (index.js:33583:25) in node_modules/pg/lib/client.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:34323:18) in node_modules/pg/lib/index.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:35442:26)
3 replies