vite-plugin-mock-dev-server
Version:
Vite Plugin for API mock dev server.
159 lines (155 loc) • 4.42 kB
JavaScript
const require_dist = require('./dist-DrfpZ4UT.cjs');
const node_stream = require_dist.__toESM(require("node:stream"));
//#region src/core/defineMock.ts
function defineMock(config) {
return config;
}
/**
* Return a custom defineMock function to support preprocessing of mock config.
*
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
* @param transformer preprocessing function
* @example
* ```ts
* const definePostMock = createDefineMock((mock) => {
* mock.url = '/api/post/' + mock.url
* })
* export default definePostMock({
* url: 'list',
* body: [{ title: '1' }, { title: '2' }],
* })
* ```
*/
function createDefineMock(transformer) {
const define = (config) => {
if (require_dist.isArray(config)) config = config.map((item) => transformer(item) || item);
else config = transformer(config) || config;
return config;
};
return define;
}
//#endregion
//#region src/core/defineMockData.ts
const mockDataCache = /* @__PURE__ */ new Map();
const responseCache = /* @__PURE__ */ new WeakMap();
const staleInterval = 70;
var CacheImpl = class {
value;
#initialValue;
#lastUpdate;
constructor(value) {
this.value = value;
this.#initialValue = require_dist.deepClone(value);
this.#lastUpdate = Date.now();
}
hotUpdate(value) {
if (Date.now() - this.#lastUpdate < staleInterval) return;
if (!require_dist.deepEqual(value, this.#initialValue)) {
this.value = value;
this.#initialValue = require_dist.deepClone(value);
this.#lastUpdate = Date.now();
}
}
};
function defineMockData(key, initialData) {
if (!mockDataCache.has(key)) mockDataCache.set(key, new CacheImpl(initialData));
const cache = mockDataCache.get(key);
cache.hotUpdate(initialData);
if (responseCache.has(cache)) return responseCache.get(cache);
const res = [() => cache.value, (val) => {
if (require_dist.isFunction(val)) val = val(cache.value) ?? cache.value;
cache.value = val;
}];
Object.defineProperty(res, "value", {
get() {
return cache.value;
},
set(val) {
cache.value = val;
}
});
responseCache.set(cache, res);
return res;
}
//#endregion
//#region src/core/sse.ts
/**
* Transforms "messages" to W3C event stream content.
* See https://html.spec.whatwg.org/multipage/server-sent-events.html
* A message is an object with one or more of the following properties:
* - data (String or object, which gets turned into JSON)
* - event
* - id
* - retry
* - comment
*
* If constructed with a HTTP Request, it will optimise the socket for streaming.
* If this stream is piped to an HTTP Response, it will set appropriate headers.
*/
var SSEStream = class extends node_stream.Transform {
constructor(req) {
super({ objectMode: true });
req.socket.setKeepAlive(true);
req.socket.setNoDelay(true);
req.socket.setTimeout(0);
}
pipe(destination, options) {
if (destination.writeHead) {
destination.writeHead(200, {
"Content-Type": "text/event-stream; charset=utf-8",
"Transfer-Encoding": "identity",
"Cache-Control": "no-cache",
"Connection": "keep-alive"
});
destination.flushHeaders?.();
}
destination.write(":ok\n\n");
return super.pipe(destination, options);
}
_transform(message, encoding, callback) {
if (message.comment) this.push(`: ${message.comment}\n`);
if (message.event) this.push(`event: ${message.event}\n`);
if (message.id) this.push(`id: ${message.id}\n`);
if (message.retry) this.push(`retry: ${message.retry}\n`);
if (message.data) this.push(dataString(message.data));
this.push("\n");
callback();
}
write(message, ...args) {
return super.write(message, ...args);
}
};
function dataString(data) {
if (typeof data === "object") return dataString(JSON.stringify(data));
return data.split(/\r\n|\r|\n/).map((line) => `data: ${line}\n`).join("");
}
function createSSEStream(req, res) {
const sse = new SSEStream(req);
sse.pipe(res);
return sse;
}
//#endregion
Object.defineProperty(exports, 'createDefineMock', {
enumerable: true,
get: function () {
return createDefineMock;
}
});
Object.defineProperty(exports, 'createSSEStream', {
enumerable: true,
get: function () {
return createSSEStream;
}
});
Object.defineProperty(exports, 'defineMock', {
enumerable: true,
get: function () {
return defineMock;
}
});
Object.defineProperty(exports, 'defineMockData', {
enumerable: true,
get: function () {
return defineMockData;
}
});