node.js - Azure Function 在 2022 年 4 月 26 日禁用了将 "user"字段设置为 HTTP 请求对象

标签 node.js azure-functions azure-functions-runtime

(!) 问题无法在本地重现。
Azure 函数版本 ~4
Node 版本 14.18.1

创建一个简单的 HTTP 触发 Azure 函数并设置两个简单的属性,我们得到以下代码:

module.exports = async function (context, req) {

    req.auth = {authField: 'some value'}
    req.user = {userField: 'some value'}
    context.log(`${JSON.stringify(req,null,2)}`);

    context.res = {
        body: 'responseMessage'
    };
}

记录器打印以下对象:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    /// ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" }
}

如您所见,仅设置了 auth 而未设置 user。 在版本 4.2.0 中可以看到相同的失败行为。

当我使用 Azure Function ~3 进行测试时,输出如下所示:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    // ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" },
  "user": { "userField": "some value" }
}

如您所见,该字段已设置。 以下自定义 v4 4.1.0-17156 还设置了 user 字段。

我们通过express-jwt 使用了字段user (v6.1.0) 即 using当没有为 requestProperty 提供任何值时。

我还不能重现它,但在从 Typescript 项目转译出来时,我们得到以下运行时错误:

FailureException: Cannot set property user of [object Object] which has only a getterStack: TypeError: Cannot set property user of [object Object] which has only a getterat Object.run

问题从2022年4月26日开始。

问题:

  • 原因是什么?
  • 是否可以快速回滚到正常运行的 Azure Functions 自定义版本?

最佳答案

我在 this 中找到了罪魁祸首PR,这是为 Azure Function runtime v4.2.0 添加的。

用户字段只添加了一个getter。
我们获取了代码并制作了最小示例:

class Request {
    #cachedUser?: string | null;
    constructor() {
    }
    get user(): string | null {
        if (this.#cachedUser === undefined) {
            this.#cachedUser = "some value";
        }
        return this.#cachedUser;
    }
}

并得到以下转译版本:

var __classPrivateFieldGet =
    (this && this.__classPrivateFieldGet) ||
    function (receiver, state, kind, f) {
        if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a getter')
        if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError('Cannot read private member from an object whose class did not declare it')
        return kind === 'm' ? f : kind === 'a' ? f.call(receiver) : f ? f.value : state.get(receiver)
    }
var __classPrivateFieldSet =
    (this && this.__classPrivateFieldSet) ||
    function (receiver, state, value, kind, f) {
        if (kind === 'm') throw new TypeError('Private method is not writable')
        if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a setter')
        if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError('Cannot write private member to an object whose class did not declare it')
        return kind === 'a' ? f.call(receiver, value) : f ? (f.value = value) : state.set(receiver, value), value
    }
var _Request_cachedUser
class Request {
    constructor() {
        _Request_cachedUser.set(this, void 0)
    }
    get user() {
        if (__classPrivateFieldGet(this, _Request_cachedUser, 'f') === undefined) {
            __classPrivateFieldSet(this, _Request_cachedUser, 'some value', 'f')
        }
        return __classPrivateFieldGet(this, _Request_cachedUser, 'f')
    }
}
_Request_cachedUser = new WeakMap()

// this section is added for testing
const request = new Request()
request.user = 'new value'
console.log(JSON.stringify(request.user))

因此,它总是返回初始值,在我们的示例中是“某个值”,但在原始代码中只是undefined 并且不允许设置它。

关于node.js - Azure Function 在 2022 年 4 月 26 日禁用了将 "user"字段设置为 HTTP 请求对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72047284/

相关文章:

javascript - async/await 和 pug 模板化

python - 为什么 python azure 函数无法在本地运行?

azure - 使用 ARM 的函数应用程序上的运行时白名单 APIM IP

python - 使用 Azure Function App 和 Python 更新 Azure 表

azure - 在 VS 中运行 Azure Function 并在读取应用程序设置时出错

javascript - 导入到 Node.js TypeScript 文件中的包无法被命名空间部分声明识别?

javascript - Node.js 或 Ubuntu Linux 在服务器启动时运行 cron

c# - Azure 函数 SDK

javascript - Shelljs:如何取消异步进程?

c# - 如何在本地测试 Azure Functions 身份验证