mirror of https://github.com/Budibase/budibase.git
6 changed files with 53 additions and 87 deletions
@ -1,84 +1,47 @@ |
|||||
const cls = require("cls-hooked") |
const cls = require("../clshooked") |
||||
const { newid } = require("../hashing") |
const { newid } = require("../hashing") |
||||
|
|
||||
const REQUEST_ID_KEY = "requestId" |
const REQUEST_ID_KEY = "requestId" |
||||
|
const MAIN_CTX = cls.createNamespace("main") |
||||
class FunctionContext { |
|
||||
static getMiddleware( |
function getContextStorage(namespace) { |
||||
updateCtxFn = null, |
if (namespace && namespace.active) { |
||||
destroyFn = null, |
let contextData = namespace.active |
||||
contextName = "session" |
delete contextData.id |
||||
) { |
delete contextData._ns_name |
||||
const namespace = this.createNamespace(contextName) |
return contextData |
||||
|
|
||||
return async function (ctx, next) { |
|
||||
await new Promise( |
|
||||
namespace.bind(function (resolve, reject) { |
|
||||
// store a contextual request ID that can be used anywhere (audit logs)
|
|
||||
namespace.set(REQUEST_ID_KEY, newid()) |
|
||||
namespace.bindEmitter(ctx.req) |
|
||||
namespace.bindEmitter(ctx.res) |
|
||||
|
|
||||
if (updateCtxFn) { |
|
||||
updateCtxFn(ctx) |
|
||||
} |
|
||||
next() |
|
||||
.then(resolve) |
|
||||
.catch(reject) |
|
||||
.finally(() => { |
|
||||
if (destroyFn) { |
|
||||
return destroyFn(ctx) |
|
||||
} |
|
||||
}) |
|
||||
}) |
|
||||
) |
|
||||
} |
|
||||
} |
} |
||||
|
return {} |
||||
|
} |
||||
|
|
||||
static run(callback, contextName = "session") { |
class FunctionContext { |
||||
const namespace = this.createNamespace(contextName) |
static run(callback) { |
||||
|
return MAIN_CTX.runAndReturn(async () => { |
||||
return namespace.runAndReturn(callback) |
const namespaceId = newid() |
||||
|
MAIN_CTX.set(REQUEST_ID_KEY, namespaceId) |
||||
|
const namespace = cls.createNamespace(namespaceId) |
||||
|
let response = await namespace.runAndReturn(callback) |
||||
|
cls.destroyNamespace(namespaceId) |
||||
|
return response |
||||
|
}) |
||||
} |
} |
||||
|
|
||||
static setOnContext(key, value, contextName = "session") { |
static setOnContext(key, value) { |
||||
const namespace = this.createNamespace(contextName) |
const namespaceId = MAIN_CTX.get(REQUEST_ID_KEY) |
||||
|
const namespace = cls.getNamespace(namespaceId) |
||||
namespace.set(key, value) |
namespace.set(key, value) |
||||
} |
} |
||||
|
|
||||
static getContextStorage() { |
|
||||
if (this._namespace && this._namespace.active) { |
|
||||
let contextData = this._namespace.active |
|
||||
delete contextData.id |
|
||||
delete contextData._ns_name |
|
||||
return contextData |
|
||||
} |
|
||||
|
|
||||
return {} |
|
||||
} |
|
||||
|
|
||||
static getFromContext(key) { |
static getFromContext(key) { |
||||
const context = this.getContextStorage() |
const namespaceId = MAIN_CTX.get(REQUEST_ID_KEY) |
||||
|
const namespace = cls.getNamespace(namespaceId) |
||||
|
const context = getContextStorage(namespace) |
||||
if (context) { |
if (context) { |
||||
return context[key] |
return context[key] |
||||
} else { |
} else { |
||||
return null |
return null |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
static destroyNamespace(name = "session") { |
|
||||
if (this._namespace) { |
|
||||
cls.destroyNamespace(name) |
|
||||
this._namespace = null |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
static createNamespace(name = "session") { |
|
||||
if (!this._namespace) { |
|
||||
this._namespace = cls.createNamespace(name) |
|
||||
} |
|
||||
return this._namespace |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
module.exports = FunctionContext |
module.exports = FunctionContext |
||||
|
|||||
Loading…
Reference in new issue