Netlify 和 Faunadb 客户端网络套接字在建立安全 TLS 连接之前断开连接

问题描述

我仅在生产模式下调用 Netlify AWS 无服务器函数时出现以下错误

ERROR FetchError:请求 https://registry.npmjs.org/faunadb 失败,原因:客户端网络套接字在安全 TLS 连接建立之前断开 在客户端请求。 (/var/task/node_modules/node-fetch/lib/index.js:1461:11) 在 ClientRequest.emit (events.js:314:20) 在 TLSSocket.socketErrorListener (_http_client.js:427:9) 在 TLSSocket.emit (events.js:314:20) 在emitErrorNT (internal/streams/destroy.js:92:8) 在emitErrorAndCloseNT (internal/streams/destroy.js:60:3) 在 processticksAndRejections (internal/process/task_queues.js:84:21) { 类型:'系统', errno: 'ECONNRESET',代码:'ECONNRESET' }

代码如下:

const faunadb = require('faunadb');
q = faunadb.query;
var fauna_client = new faunadb.Client({ secret: '[..]' });

const redirect_uri = "/.netlify/functions/follow_spotify_callback";
const client_id = "[..]";
const client_secret = "[..]";
const basic =  Buffer.from(client_id + ":" + client_secret).toString('base64');

const qs = require('querystring');
const axios = require('axios');

exports.handler = async function(event,context) {

    const state = JSON.parse(event.querystringparameters.state)


    axios.post(
        'https://accounts.spotify.com/api/token',qs.stringify({
            'grant_type': 'authorization_code','code': event.querystringparameters.code,'redirect_uri': process.env.URL + redirect_uri
        }),{
            headers: {
                'Authorization': `Basic ${basic}`,'Content-Type': 'application/x-www-form-urlencoded'
            }
        }
    ).then(res => {
        const access_token = res.data.access_token;
        const headers = {
            'Authorization': `Bearer ${res.data.access_token}`,'Content-Type': 'application/json'
        }
        axios.get(
            "https://api.spotify.com/v1/me",{
                headers: headers,}
        ).then(result => {

            fauna_client.query(
                q.Exists(
                    q.Match(
                        q.Index('users_by_id'),result.data.id
                    )
                )
            ).then(ret => {
                if (ret == false){
                    fauna_client.query(
                        q.Create(
                            q.Collection('users'),{ data: { 
                                display_name: result.data.display_name,id: result.data.id,access_token: access_token,campaign: state.artisturi,playlist: state.playlisturi,referred_by: state.referrer
                            } }
                        )
                    )
                }else{
                    console.log("user already exists")
                }
            })
            .catch(err => console.log(err));
        }
        ).catch(err =>{
            console.log(err.message)
        });
        axios.put('https://api.spotify.com/v1/me/following?type=artist',{
            'ids': [state.artisturi],},{
            headers: headers
        });
        axios.put(`https://api.spotify.com/v1/playlists/${state.playlisturi}/followers`,{
            'public': true,{
            headers: headers,});
        axios.put('https://api.spotify.com/v1/me/tracks',{
            'ids': [state.trackuri],})
    }).catch(err =>{
        console.log(err.message)
    })

    return{
        statusCode: 302,headers: {
            Location: 'https://open.spotify.com/playlist/'+state.playlisturi
        }
    }
}

一切都在本地运行 netlify dev,但不是在生产模式下,它在 https 上使用 SSL / TLS 加密

解决方法

您似乎遇到了此处描述的问题:https://docs.fauna.com/fauna/current/drivers/known_issues

如果您将实例化 Fauna 客户端的代码移到处理程序逻辑中,问题可能会消失。