oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.56k stars 2.72k forks source link

fetch in node-appwrite not working using bun #12320

Open liudonghua123 opened 3 months ago

liudonghua123 commented 3 months ago

What version of Bun is running?

1.1.17

What platform is your computer?

Microsoft Windows [Version 10.0.22631.3810]

What steps can reproduce the bug?

  1. run the following main.ts code using the latest bun via bun main.ts.

    import { Client, Databases, ID, Query } from 'node-appwrite';
    
    const {
        APPWRITE_ENDPOINT: appwrite_endpoint,
        APPWRITE_FUNCTION_PROJECT_ID: appwrite_function_project_id,
        APPWRITE_API_KEY: appwrite_api_key,
        APPWRITE_DATABASE_ID: appwrite_database_id,
        APPWRITE_COLLECTION_ID: appwrite_collection_id,
    } = process.env;
    
    async function get_config() {
        console.info(`running create client`)
        const client = new Client()
            .setEndpoint(appwrite_endpoint!)
            .setProject(appwrite_function_project_id!)
            .setKey(appwrite_api_key!);
    
        console.info(`running create databases`)
        const databases = new Databases(client);
        console.info(`running list documents`)
        const { total, documents } = await databases.listDocuments(
            appwrite_database_id || 'sync_staff_to_wecom_docs',
            appwrite_collection_id || 'dep_to_sheet'
        );
        console.info(`running after databases.listDocuments`)
        console.info(`Found ${total} documents in the collection`);
    }
    
    await get_config();

What is the expected behavior?

working as expected.

What do you see instead?

just exited when invoke the response = await fetch(uri, options); in node-appwrite.

Additional information

I added some console.info in the implementation of databases.listDocuments. And the same code works using node.

image

Liu.D.H  sync_staff_to_wecom_docs  git(main)   0ms  00:36 > tsx --env-file=.env  main.ts
running create client
running create databases
running list documents
fetch get http://<some-host>/v1/databases/sync_staff_to_wecom_docs/collections/dep_to_sheet/documents {"method":"GET","headers":{"x-sdk-name":"Node.js","x-sdk-platform":"server","x-sdk-language":"nodejs","x-sdk-version":"13.0.0","user-agent":"AppwriteNodeJSSDK/13.0.0 (win32; x64) Node.js/v20.11.1","X-Appwrite-Response-Format":"1.5.0","X-Appwrite-Project":"<some-id>","X-Appwrite-Key":"<some-key>","content-type":"application/json"},"agent":{"_events":{},"_eventsCount":2,"defaultPort":80,"protocol":"http:","options":{"noDelay":true,"path":null},"requests":{},"sockets":{},"freeSockets":{},"keepAliveMsecs":1000,"keepAlive":false,"maxSockets":null,"maxFreeSockets":256,"scheduling":"lifo","maxTotalSockets":null,"totalSocketCount":0},"dispatcher":{"_events":{},"_eventsCount":0}}
fetch response {}
running after databases.listDocuments
Found 2 documents in the collection

Liu.D.H  sync_staff_to_wecom_docs  git(main)   804ms  00:36 > bun main.ts
running create client
running create databases
running list documents
fetch get http://<some-host>/v1/databases/sync_staff_to_wecom_docs/collections/dep_to_sheet/documents {"method":"GET","headers":{"x-sdk-name":"Node.js","x-sdk-platform":"server","x-sdk-language":"nodejs","x-sdk-version":"13.0.0","user-agent":"AppwriteNodeJSSDK/13.0.0 (win32; x64) Bun/1.1.17","X-Appwrite-Response-Format":"1.5.0","X-Appwrite-Project":"<some-id>","X-Appwrite-Key":"<some-key>","content-type":"application/json"},"agent":{"_events":{},"_eventsCount":0,"defaultPort":80,"protocol":"http:","options":{"path":null,"noDelay":true},"requests":{},"sockets":{},"freeSockets":{},"keepAliveMsecs":1000,"keepAlive":false,"maxSockets":null,"maxFreeSockets":256,"scheduling":"lifo","totalSocketCount":0},"dispatcher":{"_events":{},"_eventsCount":0}}

Liu.D.H  sync_staff_to_wecom_docs  git(main)   342ms  00:37 > echo %errorlevel%
-1073740791

Liu.D.H  sync_staff_to_wecom_docs  git(main)   5ms  00:37 >
liudonghua123 commented 3 months ago

I found it's similar as https://github.com/oven-sh/bun/issues/3386, https://github.com/oven-sh/bun/issues/3371, but maybe not the same exactly.

And the implementation of node-appwrite use node-fetch-native-with-agent.

See also https://github.com/oven-sh/bun/issues/10642, https://github.com/oven-sh/bun/issues/10834, https://github.com/nodejs/node/issues/48977, https://stackoverflow.com/questions/73817412/why-is-the-agent-option-not-available-in-node-native-fetch, https://bun.sh/docs/runtime/nodejs-apis#node-https.

liudonghua123 commented 3 months ago

I found that I can skip this unexpected no-message crash if I comment the following two lines of code in https://github.com/appwrite/sdk-for-node/blob/main/src/client.ts.

// import { createAgent } from 'node-fetch-native-with-agent/agent'; // line 2

//             ...createAgent(this.config.endpoint, { rejectUnauthorized: !this.config.selfSigned }), // line 228

https://github.com/appwrite/sdk-for-node/blob/8113032344a3f29cfd7947cabc14066c7be33320/src/client.ts#L2 https://github.com/appwrite/sdk-for-node/blob/8113032344a3f29cfd7947cabc14066c7be33320/src/client.ts#L228

The first import createAgent is required even createAgent is not used, maybe some overwrites made during the import.