@atxp/server 0.2.22 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_virtual/_commonjsHelpers.js +8 -0
- package/dist/_virtual/_commonjsHelpers.js.map +1 -0
- package/dist/_virtual/bom-handling.js +4 -0
- package/dist/_virtual/bom-handling.js.map +1 -0
- package/dist/_virtual/dbcs-codec.js +4 -0
- package/dist/_virtual/dbcs-codec.js.map +1 -0
- package/dist/_virtual/index.js +8 -0
- package/dist/_virtual/index.js.map +1 -0
- package/dist/_virtual/index2.js +4 -0
- package/dist/_virtual/index2.js.map +1 -0
- package/dist/_virtual/index3.js +4 -0
- package/dist/_virtual/index3.js.map +1 -0
- package/dist/_virtual/index4.js +4 -0
- package/dist/_virtual/index4.js.map +1 -0
- package/dist/_virtual/index5.js +4 -0
- package/dist/_virtual/index5.js.map +1 -0
- package/dist/_virtual/inherits.js +4 -0
- package/dist/_virtual/inherits.js.map +1 -0
- package/dist/_virtual/inherits_browser.js +4 -0
- package/dist/_virtual/inherits_browser.js.map +1 -0
- package/dist/_virtual/sbcs-codec.js +4 -0
- package/dist/_virtual/sbcs-codec.js.map +1 -0
- package/dist/_virtual/utf16.js +4 -0
- package/dist/_virtual/utf16.js.map +1 -0
- package/dist/_virtual/utf32.js +4 -0
- package/dist/_virtual/utf32.js.map +1 -0
- package/dist/_virtual/utf7.js +4 -0
- package/dist/_virtual/utf7.js.map +1 -0
- package/dist/atxpContext.js +9 -6
- package/dist/atxpContext.js.map +1 -1
- package/dist/atxpServer.js +19 -27
- package/dist/atxpServer.js.map +1 -1
- package/dist/core/mcp.js +39 -0
- package/dist/core/mcp.js.map +1 -0
- package/dist/{oAuthChallenge.js → core/oauth.js} +21 -12
- package/dist/core/oauth.js.map +1 -0
- package/dist/{token.js → core/token.js} +13 -7
- package/dist/core/token.js.map +1 -0
- package/dist/getResource.js +5 -3
- package/dist/getResource.js.map +1 -1
- package/dist/index.cjs +13727 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +141 -5
- package/dist/index.js +13708 -5
- package/dist/index.js.map +1 -1
- package/dist/{http.js → node/http.js} +21 -20
- package/dist/node/http.js.map +1 -0
- package/dist/node/oauth.js +23 -0
- package/dist/node/oauth.js.map +1 -0
- package/dist/node/token.js +15 -0
- package/dist/node/token.js.map +1 -0
- package/dist/node_modules/bytes/index.js +181 -0
- package/dist/node_modules/bytes/index.js.map +1 -0
- package/dist/node_modules/depd/index.js +551 -0
- package/dist/node_modules/depd/index.js.map +1 -0
- package/dist/node_modules/http-errors/index.js +307 -0
- package/dist/node_modules/http-errors/index.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/dbcs-codec.js +599 -0
- package/dist/node_modules/iconv-lite/encodings/dbcs-codec.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/dbcs-data.js +207 -0
- package/dist/node_modules/iconv-lite/encodings/dbcs-data.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/index.js +45 -0
- package/dist/node_modules/iconv-lite/encodings/index.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/internal.js +211 -0
- package/dist/node_modules/iconv-lite/encodings/internal.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-codec.js +84 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-codec.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-data-generated.js +461 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-data-generated.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-data.js +188 -0
- package/dist/node_modules/iconv-lite/encodings/sbcs-data.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/big5-added.json.js +507 -0
- package/dist/node_modules/iconv-lite/encodings/tables/big5-added.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp936.json.js +2622 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp936.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp949.json.js +2381 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp949.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp950.json.js +730 -0
- package/dist/node_modules/iconv-lite/encodings/tables/cp950.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/eucjp.json.js +823 -0
- package/dist/node_modules/iconv-lite/encodings/tables/eucjp.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/gb18030-ranges.json.js +425 -0
- package/dist/node_modules/iconv-lite/encodings/tables/gb18030-ranges.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/gbk-added.json.js +265 -0
- package/dist/node_modules/iconv-lite/encodings/tables/gbk-added.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/tables/shiftjis.json.js +549 -0
- package/dist/node_modules/iconv-lite/encodings/tables/shiftjis.json.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/utf16.js +207 -0
- package/dist/node_modules/iconv-lite/encodings/utf16.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/utf32.js +330 -0
- package/dist/node_modules/iconv-lite/encodings/utf32.js.map +1 -0
- package/dist/node_modules/iconv-lite/encodings/utf7.js +298 -0
- package/dist/node_modules/iconv-lite/encodings/utf7.js.map +1 -0
- package/dist/node_modules/iconv-lite/lib/bom-handling.js +62 -0
- package/dist/node_modules/iconv-lite/lib/bom-handling.js.map +1 -0
- package/dist/node_modules/iconv-lite/lib/index.js +193 -0
- package/dist/node_modules/iconv-lite/lib/index.js.map +1 -0
- package/dist/node_modules/iconv-lite/lib/streams.js +121 -0
- package/dist/node_modules/iconv-lite/lib/streams.js.map +1 -0
- package/dist/node_modules/inherits/inherits.js +22 -0
- package/dist/node_modules/inherits/inherits.js.map +1 -0
- package/dist/node_modules/inherits/inherits_browser.js +39 -0
- package/dist/node_modules/inherits/inherits_browser.js.map +1 -0
- package/dist/node_modules/raw-body/index.js +352 -0
- package/dist/node_modules/raw-body/index.js.map +1 -0
- package/dist/node_modules/safer-buffer/safer.js +89 -0
- package/dist/node_modules/safer-buffer/safer.js.map +1 -0
- package/dist/node_modules/setprototypeof/index.js +27 -0
- package/dist/node_modules/setprototypeof/index.js.map +1 -0
- package/dist/node_modules/statuses/codes.json.js +68 -0
- package/dist/node_modules/statuses/codes.json.js.map +1 -0
- package/dist/node_modules/statuses/index.js +158 -0
- package/dist/node_modules/statuses/index.js.map +1 -0
- package/dist/node_modules/toidentifier/index.js +42 -0
- package/dist/node_modules/toidentifier/index.js.map +1 -0
- package/dist/node_modules/unpipe/index.js +79 -0
- package/dist/node_modules/unpipe/index.js.map +1 -0
- package/dist/oAuthMetadata.js +7 -4
- package/dist/oAuthMetadata.js.map +1 -1
- package/dist/paymentServer.js +5 -20
- package/dist/paymentServer.js.map +1 -1
- package/dist/protectedResourceMetadata.js +7 -4
- package/dist/protectedResourceMetadata.js.map +1 -1
- package/dist/requirePayment.js +7 -4
- package/dist/requirePayment.js.map +1 -1
- package/dist/types.js +4 -2
- package/dist/types.js.map +1 -1
- package/dist/webapi/mcp.js +25 -0
- package/dist/webapi/mcp.js.map +1 -0
- package/dist/webapi/oauth.js +21 -0
- package/dist/webapi/oauth.js.map +1 -0
- package/dist/webapi/token.js +15 -0
- package/dist/webapi/token.js.map +1 -0
- package/package.json +21 -7
- package/dist/atxpContext.d.ts +0 -6
- package/dist/atxpContext.d.ts.map +0 -1
- package/dist/atxpServer.d.ts +0 -12
- package/dist/atxpServer.d.ts.map +0 -1
- package/dist/getResource.d.ts +0 -4
- package/dist/getResource.d.ts.map +0 -1
- package/dist/http.d.ts +0 -7
- package/dist/http.d.ts.map +0 -1
- package/dist/http.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/oAuthChallenge.d.ts +0 -4
- package/dist/oAuthChallenge.d.ts.map +0 -1
- package/dist/oAuthChallenge.js.map +0 -1
- package/dist/oAuthMetadata.d.ts +0 -6
- package/dist/oAuthMetadata.d.ts.map +0 -1
- package/dist/paymentServer.d.ts +0 -62
- package/dist/paymentServer.d.ts.map +0 -1
- package/dist/protectedResourceMetadata.d.ts +0 -5
- package/dist/protectedResourceMetadata.d.ts.map +0 -1
- package/dist/requirePayment.d.ts +0 -3
- package/dist/requirePayment.d.ts.map +0 -1
- package/dist/serverTestHelpers.d.ts +0 -55
- package/dist/serverTestHelpers.d.ts.map +0 -1
- package/dist/serverTestHelpers.js +0 -88
- package/dist/serverTestHelpers.js.map +0 -1
- package/dist/token.d.ts +0 -4
- package/dist/token.d.ts.map +0 -1
- package/dist/token.js.map +0 -1
- package/dist/types.d.ts +0 -60
- package/dist/types.d.ts.map +0 -1
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
import getRawBody from
|
|
2
|
-
import contentType from
|
|
3
|
-
import { isJSONRPCRequest } from
|
|
4
|
-
import { parseMcpMessages } from
|
|
1
|
+
import getRawBody from '../_virtual/index.js';
|
|
2
|
+
import contentType from 'content-type';
|
|
3
|
+
import { isJSONRPCRequest } from '@modelcontextprotocol/sdk/types.js';
|
|
4
|
+
import { parseMcpMessages } from '@atxp/common';
|
|
5
|
+
import { parseMcpRequestsCore } from '../core/mcp.js';
|
|
6
|
+
|
|
5
7
|
// Useful reference for dealing with low-level http requests:
|
|
6
8
|
// https://github.com/modelcontextprotocol/typescript-sdk/blob/c6ac083b1b37b222b5bfba5563822daa5d03372e/src/server/streamableHttp.ts#L375
|
|
7
9
|
// Using the same value as MCP SDK
|
|
8
10
|
const MAXIMUM_MESSAGE_SIZE = "4mb";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
const mountPath = config.mountPath.replace(/\/$/, '');
|
|
20
|
-
if (path !== mountPath && path !== `${mountPath}/message`) {
|
|
21
|
-
config.logger.debug(`Request path (${path}) does not match the mountPath (${mountPath}), skipping MCP middleware`);
|
|
11
|
+
/**
|
|
12
|
+
* Node.js HTTP implementation of MCP request parsing
|
|
13
|
+
* Handles Node.js IncomingMessage parsing and delegates to core logic
|
|
14
|
+
*/
|
|
15
|
+
async function parseMcpRequests(config, requestUrl, req, parsedBody) {
|
|
16
|
+
parsedBody = parsedBody ?? await parseBody(req, config.logger);
|
|
17
|
+
// Use the shared core logic for basic validation and filtering
|
|
18
|
+
const basicMessages = parseMcpRequestsCore(config, requestUrl, req.method || '', parsedBody);
|
|
19
|
+
// Only proceed with MCP processing if the basic validation passed
|
|
20
|
+
if (basicMessages.length === 0) {
|
|
22
21
|
return [];
|
|
23
22
|
}
|
|
24
|
-
|
|
23
|
+
// Apply additional MCP-specific processing (parseMcpMessages handles SSE and other formats)
|
|
25
24
|
const messages = await parseMcpMessages(parsedBody, config.logger);
|
|
26
25
|
const requests = messages.filter(msg => isJSONRPCRequest(msg));
|
|
27
26
|
if (requests.length !== messages.length) {
|
|
@@ -29,7 +28,7 @@ export async function parseMcpRequests(config, requestUrl, req, parsedBody) {
|
|
|
29
28
|
}
|
|
30
29
|
return requests;
|
|
31
30
|
}
|
|
32
|
-
|
|
31
|
+
async function parseBody(req, logger) {
|
|
33
32
|
try {
|
|
34
33
|
const ct = req.headers["content-type"];
|
|
35
34
|
let encoding = "utf-8";
|
|
@@ -48,4 +47,6 @@ export async function parseBody(req, logger) {
|
|
|
48
47
|
return undefined;
|
|
49
48
|
}
|
|
50
49
|
}
|
|
51
|
-
|
|
50
|
+
|
|
51
|
+
export { parseBody, parseMcpRequests };
|
|
52
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sources":["../../src/node/http.ts"],"sourcesContent":[null],"names":[],"mappings":";;;;;;AAQA;AACA;AAEA;AACA,MAAM,oBAAoB,GAAG,KAAK;AAElC;;;AAGG;AACI,eAAe,gBAAgB,CAAC,MAAkB,EAAE,UAAe,EAAE,GAAoB,EAAE,UAAoB,EAAA;AACpH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O,KAAK,EAAE;AACd,QAAA,MAAM,CAAC,KAAK,CAAE,KAAe,CAAC,OAAO,CAAC;AACtC,QAAA,OAAO,SAAS;IAClB;AACF;;;;"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createOAuthChallengeResponseCore } from '../core/oauth.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node.js HTTP implementation of OAuth challenge sending
|
|
5
|
+
* Uses Node.js ServerResponse and delegates to core logic
|
|
6
|
+
*/
|
|
7
|
+
function sendOAuthChallenge(res, tokenCheck) {
|
|
8
|
+
// Use the shared core logic to get response data
|
|
9
|
+
const responseData = createOAuthChallengeResponseCore(tokenCheck);
|
|
10
|
+
if (!responseData) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
// Apply the response data to Node.js ServerResponse
|
|
14
|
+
Object.entries(responseData.headers).forEach(([key, value]) => {
|
|
15
|
+
res.setHeader(key, value);
|
|
16
|
+
});
|
|
17
|
+
res.writeHead(responseData.status);
|
|
18
|
+
res.end(responseData.body);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { sendOAuthChallenge };
|
|
23
|
+
//# sourceMappingURL=oauth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oauth.js","sources":["../../src/node/oauth.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAIA;;;AAGG;AACG,SAAU,kBAAkB,CAAC,GAAmB,EAAE,UAAsB,EAAA;;AAE5E,IAAA,MAAM,YAAY,GAAG,gCAAgC,CAAC,UAAU,CAAC;IAEjE,IAAI,CAAC,YAAY,EAAE;AACjB,QAAA,OAAO,KAAK;IACd;;AAGA,IAAA,MAAM,CAAC,OAAO,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC5D,QAAA,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE,KAAK,CAAC;AAC3B,IAAA,CAAC,CAAC;AACF,IAAA,GAAG,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC;AAClC,IAAA,GAAG,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC;AAE1B,IAAA,OAAO,IAAI;AACb;;;;"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { checkTokenCore } from '../core/token.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Node.js HTTP implementation of token checking
|
|
5
|
+
* Extracts data from Node.js IncomingMessage and delegates to core logic
|
|
6
|
+
*/
|
|
7
|
+
async function checkToken(config, resourceURL, req) {
|
|
8
|
+
// Extract the authorization header from Node.js request
|
|
9
|
+
const authorizationHeader = req.headers.authorization || null;
|
|
10
|
+
// Use the shared core logic
|
|
11
|
+
return checkTokenCore(config, resourceURL, authorizationHeader);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { checkToken };
|
|
15
|
+
//# sourceMappingURL=token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sources":["../../src/node/token.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAIA;;;AAGG;AACI,eAAe,UAAU,CAAC,MAAkB,EAAE,WAAgB,EAAE,GAAoB,EAAA;;IAEzF,MAAM,mBAAmB,GAAG,GAAG,CAAC,OAAO,CAAC,aAAa,IAAI,IAAI;;IAG7D,OAAO,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,mBAAmB,CAAC;AACjE;;;;"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { __module as bytes } from '../../_virtual/index2.js';
|
|
2
|
+
|
|
3
|
+
/*!
|
|
4
|
+
* bytes
|
|
5
|
+
* Copyright(c) 2012-2014 TJ Holowaychuk
|
|
6
|
+
* Copyright(c) 2015 Jed Watson
|
|
7
|
+
* MIT Licensed
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
var hasRequiredBytes;
|
|
11
|
+
|
|
12
|
+
function requireBytes () {
|
|
13
|
+
if (hasRequiredBytes) return bytes.exports;
|
|
14
|
+
hasRequiredBytes = 1;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Module exports.
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
bytes.exports = bytes$1;
|
|
22
|
+
bytes.exports.format = format;
|
|
23
|
+
bytes.exports.parse = parse;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Module variables.
|
|
27
|
+
* @private
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
var formatThousandsRegExp = /\B(?=(\d{3})+(?!\d))/g;
|
|
31
|
+
|
|
32
|
+
var formatDecimalsRegExp = /(?:\.0*|(\.[^0]+)0+)$/;
|
|
33
|
+
|
|
34
|
+
var map = {
|
|
35
|
+
b: 1,
|
|
36
|
+
kb: 1 << 10,
|
|
37
|
+
mb: 1 << 20,
|
|
38
|
+
gb: 1 << 30,
|
|
39
|
+
tb: Math.pow(1024, 4),
|
|
40
|
+
pb: Math.pow(1024, 5),
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
var parseRegExp = /^((-|\+)?(\d+(?:\.\d+)?)) *(kb|mb|gb|tb|pb)$/i;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert the given value in bytes into a string or parse to string to an integer in bytes.
|
|
47
|
+
*
|
|
48
|
+
* @param {string|number} value
|
|
49
|
+
* @param {{
|
|
50
|
+
* case: [string],
|
|
51
|
+
* decimalPlaces: [number]
|
|
52
|
+
* fixedDecimals: [boolean]
|
|
53
|
+
* thousandsSeparator: [string]
|
|
54
|
+
* unitSeparator: [string]
|
|
55
|
+
* }} [options] bytes options.
|
|
56
|
+
*
|
|
57
|
+
* @returns {string|number|null}
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
function bytes$1(value, options) {
|
|
61
|
+
if (typeof value === 'string') {
|
|
62
|
+
return parse(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (typeof value === 'number') {
|
|
66
|
+
return format(value, options);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Format the given value in bytes into a string.
|
|
74
|
+
*
|
|
75
|
+
* If the value is negative, it is kept as such. If it is a float,
|
|
76
|
+
* it is rounded.
|
|
77
|
+
*
|
|
78
|
+
* @param {number} value
|
|
79
|
+
* @param {object} [options]
|
|
80
|
+
* @param {number} [options.decimalPlaces=2]
|
|
81
|
+
* @param {number} [options.fixedDecimals=false]
|
|
82
|
+
* @param {string} [options.thousandsSeparator=]
|
|
83
|
+
* @param {string} [options.unit=]
|
|
84
|
+
* @param {string} [options.unitSeparator=]
|
|
85
|
+
*
|
|
86
|
+
* @returns {string|null}
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
function format(value, options) {
|
|
91
|
+
if (!Number.isFinite(value)) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
var mag = Math.abs(value);
|
|
96
|
+
var thousandsSeparator = (options && options.thousandsSeparator) || '';
|
|
97
|
+
var unitSeparator = (options && options.unitSeparator) || '';
|
|
98
|
+
var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;
|
|
99
|
+
var fixedDecimals = Boolean(options && options.fixedDecimals);
|
|
100
|
+
var unit = (options && options.unit) || '';
|
|
101
|
+
|
|
102
|
+
if (!unit || !map[unit.toLowerCase()]) {
|
|
103
|
+
if (mag >= map.pb) {
|
|
104
|
+
unit = 'PB';
|
|
105
|
+
} else if (mag >= map.tb) {
|
|
106
|
+
unit = 'TB';
|
|
107
|
+
} else if (mag >= map.gb) {
|
|
108
|
+
unit = 'GB';
|
|
109
|
+
} else if (mag >= map.mb) {
|
|
110
|
+
unit = 'MB';
|
|
111
|
+
} else if (mag >= map.kb) {
|
|
112
|
+
unit = 'KB';
|
|
113
|
+
} else {
|
|
114
|
+
unit = 'B';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
var val = value / map[unit.toLowerCase()];
|
|
119
|
+
var str = val.toFixed(decimalPlaces);
|
|
120
|
+
|
|
121
|
+
if (!fixedDecimals) {
|
|
122
|
+
str = str.replace(formatDecimalsRegExp, '$1');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (thousandsSeparator) {
|
|
126
|
+
str = str.split('.').map(function (s, i) {
|
|
127
|
+
return i === 0
|
|
128
|
+
? s.replace(formatThousandsRegExp, thousandsSeparator)
|
|
129
|
+
: s
|
|
130
|
+
}).join('.');
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return str + unitSeparator + unit;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Parse the string value into an integer in bytes.
|
|
138
|
+
*
|
|
139
|
+
* If no unit is given, it is assumed the value is in bytes.
|
|
140
|
+
*
|
|
141
|
+
* @param {number|string} val
|
|
142
|
+
*
|
|
143
|
+
* @returns {number|null}
|
|
144
|
+
* @public
|
|
145
|
+
*/
|
|
146
|
+
|
|
147
|
+
function parse(val) {
|
|
148
|
+
if (typeof val === 'number' && !isNaN(val)) {
|
|
149
|
+
return val;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (typeof val !== 'string') {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Test if the string passed is valid
|
|
157
|
+
var results = parseRegExp.exec(val);
|
|
158
|
+
var floatValue;
|
|
159
|
+
var unit = 'b';
|
|
160
|
+
|
|
161
|
+
if (!results) {
|
|
162
|
+
// Nothing could be extracted from the given string
|
|
163
|
+
floatValue = parseInt(val, 10);
|
|
164
|
+
unit = 'b';
|
|
165
|
+
} else {
|
|
166
|
+
// Retrieve the value and the unit
|
|
167
|
+
floatValue = parseFloat(results[1]);
|
|
168
|
+
unit = results[4].toLowerCase();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (isNaN(floatValue)) {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return Math.floor(map[unit] * floatValue);
|
|
176
|
+
}
|
|
177
|
+
return bytes.exports;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export { requireBytes as __require };
|
|
181
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../../../node_modules/bytes/index.js"],"sourcesContent":["/*!\n * bytes\n * Copyright(c) 2012-2014 TJ Holowaychuk\n * Copyright(c) 2015 Jed Watson\n * MIT Licensed\n */\n\n'use strict';\n\n/**\n * Module exports.\n * @public\n */\n\nmodule.exports = bytes;\nmodule.exports.format = format;\nmodule.exports.parse = parse;\n\n/**\n * Module variables.\n * @private\n */\n\nvar formatThousandsRegExp = /\\B(?=(\\d{3})+(?!\\d))/g;\n\nvar formatDecimalsRegExp = /(?:\\.0*|(\\.[^0]+)0+)$/;\n\nvar map = {\n b: 1,\n kb: 1 << 10,\n mb: 1 << 20,\n gb: 1 << 30,\n tb: Math.pow(1024, 4),\n pb: Math.pow(1024, 5),\n};\n\nvar parseRegExp = /^((-|\\+)?(\\d+(?:\\.\\d+)?)) *(kb|mb|gb|tb|pb)$/i;\n\n/**\n * Convert the given value in bytes into a string or parse to string to an integer in bytes.\n *\n * @param {string|number} value\n * @param {{\n * case: [string],\n * decimalPlaces: [number]\n * fixedDecimals: [boolean]\n * thousandsSeparator: [string]\n * unitSeparator: [string]\n * }} [options] bytes options.\n *\n * @returns {string|number|null}\n */\n\nfunction bytes(value, options) {\n if (typeof value === 'string') {\n return parse(value);\n }\n\n if (typeof value === 'number') {\n return format(value, options);\n }\n\n return null;\n}\n\n/**\n * Format the given value in bytes into a string.\n *\n * If the value is negative, it is kept as such. If it is a float,\n * it is rounded.\n *\n * @param {number} value\n * @param {object} [options]\n * @param {number} [options.decimalPlaces=2]\n * @param {number} [options.fixedDecimals=false]\n * @param {string} [options.thousandsSeparator=]\n * @param {string} [options.unit=]\n * @param {string} [options.unitSeparator=]\n *\n * @returns {string|null}\n * @public\n */\n\nfunction format(value, options) {\n if (!Number.isFinite(value)) {\n return null;\n }\n\n var mag = Math.abs(value);\n var thousandsSeparator = (options && options.thousandsSeparator) || '';\n var unitSeparator = (options && options.unitSeparator) || '';\n var decimalPlaces = (options && options.decimalPlaces !== undefined) ? options.decimalPlaces : 2;\n var fixedDecimals = Boolean(options && options.fixedDecimals);\n var unit = (options && options.unit) || '';\n\n if (!unit || !map[unit.toLowerCase()]) {\n if (mag >= map.pb) {\n unit = 'PB';\n } else if (mag >= map.tb) {\n unit = 'TB';\n } else if (mag >= map.gb) {\n unit = 'GB';\n } else if (mag >= map.mb) {\n unit = 'MB';\n } else if (mag >= map.kb) {\n unit = 'KB';\n } else {\n unit = 'B';\n }\n }\n\n var val = value / map[unit.toLowerCase()];\n var str = val.toFixed(decimalPlaces);\n\n if (!fixedDecimals) {\n str = str.replace(formatDecimalsRegExp, '$1');\n }\n\n if (thousandsSeparator) {\n str = str.split('.').map(function (s, i) {\n return i === 0\n ? s.replace(formatThousandsRegExp, thousandsSeparator)\n : s\n }).join('.');\n }\n\n return str + unitSeparator + unit;\n}\n\n/**\n * Parse the string value into an integer in bytes.\n *\n * If no unit is given, it is assumed the value is in bytes.\n *\n * @param {number|string} val\n *\n * @returns {number|null}\n * @public\n */\n\nfunction parse(val) {\n if (typeof val === 'number' && !isNaN(val)) {\n return val;\n }\n\n if (typeof val !== 'string') {\n return null;\n }\n\n // Test if the string passed is valid\n var results = parseRegExp.exec(val);\n var floatValue;\n var unit = 'b';\n\n if (!results) {\n // Nothing could be extracted from the given string\n floatValue = parseInt(val, 10);\n unit = 'b'\n } else {\n // Retrieve the value and the unit\n floatValue = parseFloat(results[1]);\n unit = results[4].toLowerCase();\n }\n\n if (isNaN(floatValue)) {\n return null;\n }\n\n return Math.floor(map[unit] * floatValue);\n}\n"],"names":["bytesModule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