在将Google的电子表格API与Vercel一起使用时发行

发布于 2025-01-26 09:52:01 字数 1478 浏览 3 评论 0原文

我正在Vercel上托管ExpressJS应用程序,在某个时候,它使用电子表格API存储生成的产品密钥。该功能看起来像这样:

async function addActivationCode(activationCode)
{
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "ActivationCodes!A:C",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [activationCode, "1", "0"]
            ]
        }
    })
}

当本地托管时,此功能正常工作。但是在Vercel的实时日志中,我得到一个Fetcherror说:在建立安全TLS连接之前,客户网络套接字已断开。

调用此功能的try-catch块如下:

try
{
    await addActivationCode(generateActivationCode())
}
catch(err)
{
    console.log(err)
    console.log("Failed to add new activation code.")
    return
}

请注意,它不是将附加请求发送到电子表格API的唯一功能。还有另一个用于添加用户的功能,而且工作正常。在这里是:

async function addUser(username, password)
{
    // Generate salt and hash the password in one command
    const hashedPassword = await bcrypt.hash(password, 10)

    // Add user to the Users sheet
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "Users!A2:B",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [username, hashedPassword]
            ]
        }
    })
}

我找到了另一个

任何帮助都将受到赞赏。

I'm hosting an expressjs app on Vercel, at some point it uses the spreadsheet api to store a generated product key. The function looks like this:

async function addActivationCode(activationCode)
{
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "ActivationCodes!A:C",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [activationCode, "1", "0"]
            ]
        }
    })
}

This function works perfectly fine when hosted locally. But in Vercel's Realtime logs I get a FetchError saying: Client network socket disconnected before secure TLS connection was established.

the try-catch block that calls this function is the following:

try
{
    await addActivationCode(generateActivationCode())
}
catch(err)
{
    console.log(err)
    console.log("Failed to add new activation code.")
    return
}

Note that it is not the only function that sends an append request to the spreadsheet api. There's another function used to add users and it's working fine. Here it is:

async function addUser(username, password)
{
    // Generate salt and hash the password in one command
    const hashedPassword = await bcrypt.hash(password, 10)

    // Add user to the Users sheet
    await googleSheets.spreadsheets.values.append({
        auth: googleAuth,
        spreadsheetId: sheetID,
        range: "Users!A2:B",
        valueInputOption: "USER_ENTERED",
        resource: {
            values: [
                [username, hashedPassword]
            ]
        }
    })
}

I found another post talking about this issue but not in Vercel. It was related to proxies. My knowledge about proxies atm is limited so I didn't quite understand how to fix this in Vercel.

Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

儭儭莪哋寶赑 2025-02-02 09:52:01

我发现问题来自哪里。这是Vercel的 limits 。对于使用免费计划的用户,他们的应用程序不能花费10秒钟以上来响应请求。例如,我在应用程序中负责注册用户的功能做了5件事:

  1. 它通过从Google电子表格中检索用户列表来检查用户名是否已经存在。
  2. 它检查给定产品密钥是否有效,
  3. 它添加了用户
  4. 删除注册过程中使用的产品密钥,
  5. 它会生成新产品密钥并将其附加到键表。

那是发送给电子表格API的5个该死的请求!这肯定需要花费的时间超过限制才能完成。

I found where the problem is coming from. It's Vercel's limits. For users using the free plan, their app can't take more than 10 seconds to respond to a request. For instance, the function I have in my app that's responsible for registering users does 5 things:

  1. It checks if the username already exists by retrieving the list of users from google spreadsheet.
  2. It checks if the given product key is valid
  3. It adds the user
  4. It deletes the product key used during registration
  5. It generates a new product key and appends it to the table of keys.

That's 5 god damn requests sent to the spreadsheet api! That definitely takes more time than the limit to complete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文