Comparing version 0.7.0 to 0.7.1
@@ -10,2 +10,10 @@ /// <reference types="node" resolution-mode="require"/> | ||
} | ||
export interface Context<T, Params extends any[], R = any, Next = (...np: any[]) => any> { | ||
value: Awaited<T>; | ||
get(req: IncomingMessage): T; | ||
set(req: IncomingMessage, value: T): void; | ||
(...params: Params): Promise<T>; | ||
chain(...params: Params): Chainable<Params, R, Next>; | ||
} | ||
export declare function makeDxContext<T, Params extends any[], R = any, Next = (...np: any[]) => any>(maker: (...params: Params) => T): Context<T, Params, R, Next>; | ||
export declare function dxServer(req: IncomingMessage, res: ServerResponse, options?: { | ||
@@ -37,7 +45,1 @@ jsonBeautify?: boolean; | ||
export declare function setRedirect(url: string, status: 301 | 302): void; | ||
export interface Context<T, Params extends any[], R = any, Next = (...np: any[]) => any> { | ||
value: Awaited<T>; | ||
chain(...params: Params): Chainable<Params, R, Next>; | ||
(...params: Params): Promise<T>; | ||
} | ||
export declare function makeDxContext<T, Params extends any[], R = any, Next = (...np: any[]) => any>(maker: (...params: Params) => T): Context<T, Params, R, Next>; |
107
cjs/dx.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.makeDxContext = exports.setRedirect = exports.setJson = exports.setWebStream = exports.setNodeStream = exports.setBuffer = exports.setFile = exports.setHtml = exports.setText = exports.getRes = exports.getReq = exports.dxServer = void 0; | ||
exports.setRedirect = exports.setJson = exports.setWebStream = exports.setNodeStream = exports.setBuffer = exports.setFile = exports.setHtml = exports.setText = exports.getRes = exports.getReq = exports.dxServer = exports.makeDxContext = void 0; | ||
const node_async_hooks_1 = require("node:async_hooks"); | ||
const dxHelpers_js_1 = require("./dxHelpers.js"); | ||
const reqStorage = new node_async_hooks_1.AsyncLocalStorage(); | ||
const resStorage = new node_async_hooks_1.AsyncLocalStorage(); | ||
const dxStorage = new node_async_hooks_1.AsyncLocalStorage(); | ||
function makeDxContext(maker) { | ||
const promiseSymbol = Symbol('promise'); | ||
const valueSymbol = Symbol('value'); | ||
// wrap in an async function to ensure the maker is called only once | ||
const context = (...params) => getReq()[promiseSymbol] ??= (async () => { | ||
try { | ||
return getReq()[valueSymbol] = await maker(...params); | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
})(); | ||
Object.defineProperty(context, 'value', { | ||
get() { return getReq()[valueSymbol]; }, | ||
set(value) { | ||
getReq()[promiseSymbol] = Promise.resolve(value); | ||
getReq()[valueSymbol] = value; | ||
} | ||
}); | ||
context.chain = (...params) => async (next) => { | ||
await context(...params); | ||
return next(); | ||
}; | ||
context.set = (req, value) => { | ||
req[promiseSymbol] = Promise.resolve(value); | ||
req[valueSymbol] = value; | ||
}; | ||
context.get = req => req[valueSymbol]; | ||
return context; | ||
} | ||
exports.makeDxContext = makeDxContext; | ||
const requestStorage = new node_async_hooks_1.AsyncLocalStorage(); | ||
const dxContext = makeDxContext(options => ({ ...options })); | ||
function dxServer(req, res, options = {}) { | ||
return async (next) => { | ||
const dx = { ...options }; | ||
const result = await dxStorage.run(dx, () => reqStorage.run(req, () => resStorage.run(res, next))); | ||
await (0, dxHelpers_js_1.writeRes)(req, res, dx); | ||
dxContext.set(req, { ...options }); | ||
const result = await requestStorage.run({ req, res }, next); | ||
await (0, dxHelpers_js_1.writeRes)(req, res, dxContext.get(req)); | ||
return result; | ||
@@ -22,13 +52,9 @@ }; | ||
// rawHeaders: list of header name and value in a flat array. Case is preserved. | ||
function getReq() { | ||
return reqStorage.getStore(); | ||
} | ||
function getReq() { return requestStorage.getStore().req; } | ||
exports.getReq = getReq; | ||
function getRes() { | ||
return resStorage.getStore(); | ||
} | ||
function getRes() { return requestStorage.getStore().res; } | ||
exports.getRes = getRes; | ||
function setText(text, { status } = {}) { | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -42,3 +68,3 @@ res.statusCode = status; | ||
setText(html, opts); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.type = 'html'; | ||
@@ -48,3 +74,3 @@ } | ||
function setFile(filePath, options) { | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.data = filePath; | ||
@@ -57,3 +83,3 @@ dx.type = 'file'; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -67,3 +93,3 @@ res.statusCode = status; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -77,3 +103,3 @@ res.statusCode = status; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -89,3 +115,3 @@ res.statusCode = status; | ||
res.statusCode = status; | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.data = json; | ||
@@ -97,3 +123,3 @@ dx.type = 'json'; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
res.statusCode = status; | ||
@@ -104,28 +130,13 @@ dx.data = url; | ||
exports.setRedirect = setRedirect; | ||
function makeDxContext(maker) { | ||
const promiseSymbol = Symbol('promise'); | ||
const valueSymbol = Symbol('value'); | ||
// wrap in an async function to ensure the maker is called only once | ||
const context = (...params) => getReq()[promiseSymbol] ??= (async () => { | ||
try { | ||
return getReq()[valueSymbol] = await maker(...params); | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
})(); | ||
Object.defineProperty(context, 'value', { | ||
get() { | ||
if (!getReq()[promiseSymbol]) | ||
throw new Error('value is not ready'); | ||
return getReq()[valueSymbol]; | ||
} | ||
}); | ||
context.chain = (...params) => async (next) => { | ||
await context(...params); | ||
return next(); | ||
}; | ||
return context; | ||
} | ||
exports.makeDxContext = makeDxContext; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZHgudHMiXSwibmFtZXMiOltdLCJtYXBwaW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BQ0osT0FBTyxNQUFNLEVBQUUsQ0FBQyxXQUFXLENBQUMsR0FBRyxNQUFNLEtBQUssQ0FBQyxHQUFHLE1BQU0sQ0FBQyxDQUFBO1FBQ3RELENBQUM7UUFBQyxPQUFPLENBQUMsRUFBRSxDQUFDO1lBQ1osTUFBTSxDQUFDLENBQUE7UUFDUixDQUFDO0lBQ0YsQ0FBQyxDQUFDLEVBQUUsQ0FBQTtJQUNKLE1BQU0sQ0FBQyxjQUFjLENBQUMsT0FBTyxFQUFFLE9BQU8sRUFBRTtRQUN2QyxHQUFHO1lBQ0YsSUFBSSxDQUFDLE1BQU0sRUFBRSxDQUFDLGFBQWEsQ0FBQztnQkFBRSxNQUFNLElBQUksS0FBSyxDQUFDLG9CQUFvQixDQUFDLENBQUE7WUFDbkUsT0FBTyxNQUFNLEVBQUUsQ0FBQyxXQUFXLENBQUMsQ0FBQTtRQUM3QixDQUFDO0tBQ0QsQ0FBQyxDQUFBO0lBQ0YsT0FBTyxDQUFDLEtBQUssR0FBRyxDQUFDLEdBQUcsTUFBTSxFQUFFLEVBQUUsQ0FBQyxLQUFLLEVBQUMsSUFBSSxFQUFDLEVBQUU7UUFDM0MsTUFBTSxPQUFPLENBQUMsR0FBRyxNQUFNLENBQUMsQ0FBQTtRQUN4QixPQUFPLElBQUksRUFBRSxDQUFBO0lBQ2QsQ0FBQyxDQUFBO0lBQ0QsT0FBTyxPQUFPLENBQUE7QUFDZixDQUFDO0FBM0JELHNDQTJCQyJ9 | ||
// for download, set content-disposition header | ||
// res.setHeader('Content-disposition', 'attachment; filename=my-movie.MOV') | ||
// res.setHeader('Content-type', 'video/quicktime') | ||
// fileStream.pipe(res) | ||
// or | ||
// send(req, filePath, options).pipe(res) // which will set content-type, content-length, and other cache related headers like staticHelpers.sendFile | ||
// implementing this require a strict validation for the type (attachment) and filename. | ||
// For example: express relies on this | ||
// https://github.com/jshttp/content-disposition/blob/1037e24e4790273da96645ad250061f39e77968c/index.js#L186 | ||
// because in most applications, users can specify a simple filename which usually doesn't need to be validated. | ||
// we leave setDownload() implementation for users, for now. | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZHgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7O0FBRUEsdURBQWtEO0FBQ2xELGlEQUF1RDtBQXVCdkQsU0FBZ0IsYUFBYSxDQUszQixLQUErQjtJQUNoQyxNQUFNLGFBQWEsR0FBRyxNQUFNLENBQUMsU0FBUyxDQUFDLENBQUE7SUFDdkMsTUFBTSxXQUFXLEdBQUcsTUFBTSxDQUFDLE9BQU8sQ0FBQyxDQUFBO0lBQ25DLG9FQUFvRTtJQUNwRSxNQUFNLE9BQU8sR0FBZ0MsQ0FBQyxHQUFHLE1BQWMsRUFBRSxFQUFFLENBQUMsTUFBTSxFQUFFLENBQUMsYUFBYSxDQUFDLEtBQUssQ0FBQyxLQUFLLElBQUksRUFBRTtRQUMzRyxJQUFJLENBQUM7WUFDSixPQUFPLE1BQU0sRUFBRSxDQUFDLFdBQVcsQ0FBQyxHQUFHLE1BQU0sS0FBSyxDQUFDLEdBQUcsTUFBTSxDQUFDLENBQUE7UUFDdEQsQ0FBQztRQUFDLE9BQU8sQ0FBQyxFQUFFLENBQUM7WUFDWixNQUFNLENBQUMsQ0FBQTtRQUNSLENBQUM7SUFDRixDQUFDLENBQUMsRUFBRSxDQUFBO0lBQ0osTUFBTSxDQUFDLGNBQWMsQ0FBQyxPQUFPLEVBQUUsT0FBTyxFQUFFO1FBQ3ZDLEdBQUcsS0FBSSxPQUFPLE1BQU0sRUFBRSxDQUFDLFdBQVcsQ0FBQyxDQUFBLENBQUEsQ0FBQztRQUNwQyxHQUFHLENBQUMsS0FBSztZQUNSLE1BQU0sRUFBRSxDQUFDLGFBQWEsQ0FBQyxHQUFHLE9BQU8sQ0FBQyxPQUFPLENBQUMsS0FBSyxDQU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QUFNLEVBQUUsQ0FBQTtJQUNwQixNQUFNLEVBQUUsR0FBRyxTQUFTLENBQUMsS0FBSyxDQUFBO0lBQzFCLEdBQUcsQ0FBQyxVQUFVLEdBQUcsTUFBTSxDQUFBO0lBQ3ZCLEVBQUUsQ0FBQyxJQUFJLEdBQUcsR0FBRyxDQUFBO0lBQ2IsRUFBRSxDQUFDLElBQUksR0FBRyxVQUFVLENBQUE7QUFDckIsQ0FBQztBQU5ELGtDQU1DO0FBRUQsK0NBQStDO0FBQy9DLDRFQUE0RTtBQUU1RSxtREFBbUQ7QUFDbkQsdUJBQXVCO0FBQ3ZCLEtBQUs7QUFDTCxxSkFBcUo7QUFFckosd0ZBQXdGO0FBQ3hGLHNDQUFzQztBQUN0Qyw0R0FBNEc7QUFDNUcsZ0hBQWdIO0FBQ2hILDREQUE0RCJ9 |
@@ -10,2 +10,10 @@ /// <reference types="node" resolution-mode="require"/> | ||
} | ||
export interface Context<T, Params extends any[], R = any, Next = (...np: any[]) => any> { | ||
value: Awaited<T>; | ||
get(req: IncomingMessage): T; | ||
set(req: IncomingMessage, value: T): void; | ||
(...params: Params): Promise<T>; | ||
chain(...params: Params): Chainable<Params, R, Next>; | ||
} | ||
export declare function makeDxContext<T, Params extends any[], R = any, Next = (...np: any[]) => any>(maker: (...params: Params) => T): Context<T, Params, R, Next>; | ||
export declare function dxServer(req: IncomingMessage, res: ServerResponse, options?: { | ||
@@ -37,7 +45,1 @@ jsonBeautify?: boolean; | ||
export declare function setRedirect(url: string, status: 301 | 302): void; | ||
export interface Context<T, Params extends any[], R = any, Next = (...np: any[]) => any> { | ||
value: Awaited<T>; | ||
chain(...params: Params): Chainable<Params, R, Next>; | ||
(...params: Params): Promise<T>; | ||
} | ||
export declare function makeDxContext<T, Params extends any[], R = any, Next = (...np: any[]) => any>(maker: (...params: Params) => T): Context<T, Params, R, Next>; |
103
esm/dx.js
import { AsyncLocalStorage } from 'node:async_hooks'; | ||
import { writeRes } from './dxHelpers.js'; | ||
const reqStorage = new AsyncLocalStorage(); | ||
const resStorage = new AsyncLocalStorage(); | ||
const dxStorage = new AsyncLocalStorage(); | ||
export function makeDxContext(maker) { | ||
const promiseSymbol = Symbol('promise'); | ||
const valueSymbol = Symbol('value'); | ||
// wrap in an async function to ensure the maker is called only once | ||
const context = (...params) => getReq()[promiseSymbol] ??= (async () => { | ||
try { | ||
return getReq()[valueSymbol] = await maker(...params); | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
})(); | ||
Object.defineProperty(context, 'value', { | ||
get() { return getReq()[valueSymbol]; }, | ||
set(value) { | ||
getReq()[promiseSymbol] = Promise.resolve(value); | ||
getReq()[valueSymbol] = value; | ||
} | ||
}); | ||
context.chain = (...params) => async (next) => { | ||
await context(...params); | ||
return next(); | ||
}; | ||
context.set = (req, value) => { | ||
req[promiseSymbol] = Promise.resolve(value); | ||
req[valueSymbol] = value; | ||
}; | ||
context.get = req => req[valueSymbol]; | ||
return context; | ||
} | ||
const requestStorage = new AsyncLocalStorage(); | ||
const dxContext = makeDxContext(options => ({ ...options })); | ||
export function dxServer(req, res, options = {}) { | ||
return async (next) => { | ||
const dx = { ...options }; | ||
const result = await dxStorage.run(dx, () => reqStorage.run(req, () => resStorage.run(res, next))); | ||
await writeRes(req, res, dx); | ||
dxContext.set(req, { ...options }); | ||
const result = await requestStorage.run({ req, res }, next); | ||
await writeRes(req, res, dxContext.get(req)); | ||
return result; | ||
@@ -18,11 +47,7 @@ }; | ||
// rawHeaders: list of header name and value in a flat array. Case is preserved. | ||
export function getReq() { | ||
return reqStorage.getStore(); | ||
} | ||
export function getRes() { | ||
return resStorage.getStore(); | ||
} | ||
export function getReq() { return requestStorage.getStore().req; } | ||
export function getRes() { return requestStorage.getStore().res; } | ||
export function setText(text, { status } = {}) { | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -35,7 +60,7 @@ res.statusCode = status; | ||
setText(html, opts); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.type = 'html'; | ||
} | ||
export function setFile(filePath, options) { | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.data = filePath; | ||
@@ -47,3 +72,3 @@ dx.type = 'file'; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -56,3 +81,3 @@ res.statusCode = status; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -65,3 +90,3 @@ res.statusCode = status; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
if (status) | ||
@@ -76,3 +101,3 @@ res.statusCode = status; | ||
res.statusCode = status; | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
dx.data = json; | ||
@@ -83,3 +108,3 @@ dx.type = 'json'; | ||
const res = getRes(); | ||
const dx = dxStorage.getStore(); | ||
const dx = dxContext.value; | ||
res.statusCode = status; | ||
@@ -89,27 +114,13 @@ dx.data = url; | ||
} | ||
export function makeDxContext(maker) { | ||
const promiseSymbol = Symbol('promise'); | ||
const valueSymbol = Symbol('value'); | ||
// wrap in an async function to ensure the maker is called only once | ||
const context = (...params) => getReq()[promiseSymbol] ??= (async () => { | ||
try { | ||
return getReq()[valueSymbol] = await maker(...params); | ||
} | ||
catch (e) { | ||
throw e; | ||
} | ||
})(); | ||
Object.defineProperty(context, 'value', { | ||
get() { | ||
if (!getReq()[promiseSymbol]) | ||
throw new Error('value is not ready'); | ||
return getReq()[valueSymbol]; | ||
} | ||
}); | ||
context.chain = (...params) => async (next) => { | ||
await context(...params); | ||
return next(); | ||
}; | ||
return context; | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZHguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvZHgudHMiXSwibmFtZXMiOltdLCJtYXBwaW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LENBQUMsR0FBRyxNQUFNLEtBQUssQ0FBQyxHQUFHLE1BQU0sQ0FBQyxDQUFBO1FBQ3RELENBQUM7UUFBQyxPQUFPLENBQUMsRUFBRSxDQUFDO1lBQ1osTUFBTSxDQUFDLENBQUE7UUFDUixDQUFDO0lBQ0YsQ0FBQyxDQUFDLEVBQUUsQ0FBQTtJQUNKLE1BQU0sQ0FBQyxjQUFjLENBQUMsT0FBTyxFQUFFLE9BQU8sRUFBRTtRQUN2QyxHQUFHO1lBQ0YsSUFBSSxDQUFDLE1BQU0sRUFBRSxDQUFDLGFBQWEsQ0FBQztnQkFBRSxNQUFNLElBQUksS0FBSyxDQUFDLG9CQUFvQixDQUFDLENBQUE7WUFDbkUsT0FBTyxNQUFNLEVBQUUsQ0FBQyxXQUFXLENBQUMsQ0FBQTtRQUM3QixDQUFDO0tBQ0QsQ0FBQyxDQUFBO0lBQ0YsT0FBTyxDQUFDLEtBQUssR0FBRyxDQUFDLEdBQUcsTUFBTSxFQUFFLEVBQUUsQ0FBQyxLQUFLLEVBQUMsSUFBSSxFQUFDLEVBQUU7UUFDM0MsTUFBTSxPQUFPLENBQUMsR0FBRyxNQUFNLENBQUMsQ0FBQTtRQUN4QixPQUFPLElBQUksRUFBRSxDQUFBO0lBQ2QsQ0FBQyxDQUFBO0lBQ0QsT0FBTyxPQUFPLENBQUE7QUFDZixDQUFDIn0= | ||
// for download, set content-disposition header | ||
// res.setHeader('Content-disposition', 'attachment; filename=my-movie.MOV') | ||
// res.setHeader('Content-type', 'video/quicktime') | ||
// fileStream.pipe(res) | ||
// or | ||
// send(req, filePath, options).pipe(res) // which will set content-type, content-length, and other cache related headers like staticHelpers.sendFile | ||
// implementing this require a strict validation for the type (attachment) and filename. | ||
// For example: express relies on this | ||
// https://github.com/jshttp/content-disposition/blob/1037e24e4790273da96645ad250061f39e77968c/index.js#L186 | ||
// because in most applications, users can specify a simple filename which usually doesn't need to be validated. | ||
// we leave setDownload() implementation for users, for now. | ||
//# sourceMapp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BQVUsR0FBRyxNQUFNLENBQUE7SUFDdkIsRUFBRSxDQUFDLElBQUksR0FBRyxHQUFHLENBQUE7SUFDYixFQUFFLENBQUMsSUFBSSxHQUFHLFVBQVUsQ0FBQTtBQUNyQixDQUFDO0FBRUQsK0NBQStDO0FBQy9DLDRFQUE0RTtBQUU1RSxtREFBbUQ7QUFDbkQsdUJBQXVCO0FBQ3ZCLEtBQUs7QUFDTCxxSkFBcUo7QUFFckosd0ZBQXdGO0FBQ3hGLHNDQUFzQztBQUN0Qyw0R0FBNEc7QUFDNUcsZ0hBQWdIO0FBQ2hILDREQUE0RCJ9 |
{ | ||
"name": "dx-server", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"main": "./cjs/index.js", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/tranvansang/dx-server", |
@@ -248,4 +248,17 @@ # dx-server - modern, unopinionated, and satisfactory server | ||
- Chain it to get the value synchronously: `chain(getJson.chain(), next => console.log(getJson.value))`. Note that the value is calculated in every request. | ||
- `makeDxContext(fn)`: create a DX context object. | ||
- `ctx = makeDxContext(fn)`: create a DX context object. | ||
`fn` is called once per request to calculate the value. | ||
The value is cached for subsequent calls. | ||
- Context value can be accessed by: | ||
- `const value = await ctx()`: lazily load the context, once loaded, it is cached for subsequent calls. | ||
- `const value = ctx.value`: get the value synchronously. Note that the value must be fetched at least once before via `await ctx()` or by chaining `chain(ctx.chain())`. | ||
- `const value = ctx.get(req)`: similar condition as `ctx.value`. | ||
- Context value can be set or overridden by: | ||
- `ctx.value = value`. | ||
- `ctx.set(req, value)`. | ||
- `setHtml`, `setJson`, `setText`, `setBuffer`, `setRedirect`, `setNodeStream`, `setWebStream`, `setFile`: set response body. | ||
@@ -252,0 +265,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
175166
2021
361