@ms-cloudpack/bundle-server 0.4.0 → 0.4.1
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/lib/handleBundleRequest.d.ts +8 -0
- package/lib/handleBundleRequest.d.ts.map +1 -0
- package/lib/handleBundleRequest.js +76 -0
- package/lib/handleBundleRequest.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/startBundleServer.d.ts +1 -1
- package/lib/startBundleServer.d.ts.map +1 -1
- package/lib/startBundleServer.js +4 -76
- package/lib/startBundleServer.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Request, Response } from '@ms-cloudpack/create-express-app';
|
|
2
|
+
import type { BundleServerContext } from './types/BundleServerContext.js';
|
|
3
|
+
import type { BundleServerOptions } from './types/BundleServerOptions.js';
|
|
4
|
+
export declare function handleBundleRequest(options: BundleServerOptions & {
|
|
5
|
+
req: Request;
|
|
6
|
+
res: Response;
|
|
7
|
+
}, context: BundleServerContext): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=handleBundleRequest.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handleBundleRequest.d.ts","sourceRoot":"","sources":["../src/handleBundleRequest.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAK1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,mBAAmB,GAAG;IAC7B,GAAG,EAAE,OAAO,CAAC;IACb,GAAG,EAAE,QAAQ,CAAC;CACf,EACD,OAAO,EAAE,mBAAmB,iBAqF7B"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ensurePackageBundled } from '@ms-cloudpack/api-server';
|
|
2
|
+
import { parseRequestInfo } from '@ms-cloudpack/import-map';
|
|
3
|
+
import { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';
|
|
4
|
+
import { slash } from '@ms-cloudpack/path-string-parsing';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
export async function handleBundleRequest(options, context) {
|
|
7
|
+
const { req, res, disableCache } = options;
|
|
8
|
+
const { session, packageHashes } = context;
|
|
9
|
+
const { resolveMap } = session;
|
|
10
|
+
const requestPath = slash(req.path.substring(1));
|
|
11
|
+
const force = req.query.force !== undefined;
|
|
12
|
+
const { packageName, version, hash, bundled, filePath } = parseRequestInfo(requestPath);
|
|
13
|
+
const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });
|
|
14
|
+
if (!packageEntry) {
|
|
15
|
+
// If the package is missing, invalid arguments.
|
|
16
|
+
console.debug(`Response (404): Unrecognized package "${packageName}"`);
|
|
17
|
+
res.status(404).send(`Unrecognized package "${packageName}".`);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
console.debug(`Request: "${packageName}@${version}${filePath}"`);
|
|
21
|
+
// If the package is missing, invalid arguments.
|
|
22
|
+
if (!requestPath || !packageName) {
|
|
23
|
+
console.debug(`Response (400): "${packageName}@${version}${filePath}"`);
|
|
24
|
+
res.status(400).send(`Requests must be in the format "/{packageName}@{version}/{type}/path/file.ext".`);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
// If the version is missing, redirect to the primary version.
|
|
28
|
+
if (!version) {
|
|
29
|
+
res.redirect(`/${packageName}@${packageEntry.version}${filePath}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// If we are accessing a non-bundled asset, return the file directly.
|
|
33
|
+
if (!bundled) {
|
|
34
|
+
const fullPath = path.join(packageEntry.path, filePath);
|
|
35
|
+
if (fullPath.length < packageEntry.path.length) {
|
|
36
|
+
console.debug(`Response (400): "${packageName}@${version}${filePath}"`);
|
|
37
|
+
res.status(400).send(`Invalid file path.`);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
console.debug(`Unbundled response returning: "${packageName}@${version}${filePath}"`);
|
|
41
|
+
// If the package is external, set cache headers.
|
|
42
|
+
if (isExternalPackage(packageEntry.path)) {
|
|
43
|
+
res.setHeader('Cache-Control', 'public, max-age=31536000');
|
|
44
|
+
}
|
|
45
|
+
res.sendFile(path.join(packageEntry.path, filePath));
|
|
46
|
+
}
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
console.debug(`Trying to bundle "${packageName}@${version}"`);
|
|
50
|
+
const { result } = await ensurePackageBundled({
|
|
51
|
+
input: {
|
|
52
|
+
name: packageName,
|
|
53
|
+
version,
|
|
54
|
+
shouldForce: force,
|
|
55
|
+
shouldRerun: force,
|
|
56
|
+
shouldWatch: true, // This will only watch the package if it is internal.
|
|
57
|
+
disableCache,
|
|
58
|
+
},
|
|
59
|
+
ctx: context,
|
|
60
|
+
});
|
|
61
|
+
if (result.errors?.length) {
|
|
62
|
+
res.status(500).type('json').send(result);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
console.debug(`Bundle returning: "${packageName}@${version}${filePath}"`);
|
|
66
|
+
// Try to return the result.
|
|
67
|
+
if (result.isExternal ||
|
|
68
|
+
(hash !== 'pending' &&
|
|
69
|
+
hash === (await packageHashes.get({ packagePath: packageEntry.path, isSourceHashingEnabled: true })))) {
|
|
70
|
+
res.setHeader('Cache-Control', 'public, max-age=31536000');
|
|
71
|
+
}
|
|
72
|
+
// The below header is need to be able to track the browser cache hit ratio.
|
|
73
|
+
res.setHeader('Timing-Allow-Origin', '*');
|
|
74
|
+
res.sendFile(path.join(result.outputPath, filePath));
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=handleBundleRequest.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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,CAAC;QACvD,CAAC;QACD,OAAO;IACT,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,qBAAqB,WAAW,IAAI,OAAO,GAAG,CAAC,CAAC;IAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,oBAAoB,CAAC;QAC5C,KAAK,EAAE;YACL,IAAI,EAAE,WAAW;YACjB,OAAO;YACP,WAAW,EAAE,KAAK;YAClB,WAAW,EAAE,KAAK;YAClB,WAAW,EAAE,IAAI,EAAE,sDAAsD;YACzE,YAAY;SACb;QACD,GAAG,EAAE,OAAO;KACb,CAAC,CAAC;IAEH,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;QAC1B,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC1C,OAAO;IACT,CAAC;IAED,OAAO,CAAC,KAAK,CAAC,sBAAsB,WAAW,IAAI,OAAO,GAAG,QAAQ,GAAG,CAAC,CAAC;IAC1E,4BAA4B;IAC5B,IACE,MAAM,CAAC,UAAU;QACjB,CAAC,IAAI,KAAK,SAAS;YACjB,IAAI,KAAK,CAAC,MAAM,aAAa,CAAC,GAAG,CAAC,EAAE,WAAW,EAAE,YAAY,CAAC,IAAI,EAAE,sBAAsB,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EACvG,CAAC;QACD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,0BAA0B,CAAC,CAAC;IAC7D,CAAC;IAED,4EAA4E;IAC5E,GAAG,CAAC,SAAS,CAAC,qBAAqB,EAAE,GAAG,CAAC,CAAC;IAE1C,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC;AACvD,CAAC","sourcesContent":["import { ensurePackageBundled } from '@ms-cloudpack/api-server';\nimport type { Request, Response } from '@ms-cloudpack/create-express-app';\nimport { parseRequestInfo } from '@ms-cloudpack/import-map';\nimport { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\nimport type { BundleServerContext } from './types/BundleServerContext.js';\nimport type { BundleServerOptions } from './types/BundleServerOptions.js';\n\nexport async function handleBundleRequest(\n options: BundleServerOptions & {\n req: Request;\n res: Response;\n },\n context: BundleServerContext,\n) {\n const { req, res, disableCache } = options;\n const { session, packageHashes } = context;\n const { resolveMap } = session;\n const requestPath = slash(req.path.substring(1));\n const force = req.query.force !== undefined;\n const { packageName, version, hash, bundled, filePath } = parseRequestInfo(requestPath);\n const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });\n\n if (!packageEntry) {\n // If the package is missing, invalid arguments.\n console.debug(`Response (404): Unrecognized package \"${packageName}\"`);\n res.status(404).send(`Unrecognized package \"${packageName}\".`);\n return;\n }\n\n console.debug(`Request: \"${packageName}@${version}${filePath}\"`);\n\n // If the package is missing, invalid arguments.\n if (!requestPath || !packageName) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\n\n res.status(400).send(`Requests must be in the format \"/{packageName}@{version}/{type}/path/file.ext\".`);\n return;\n }\n\n // If the version is missing, redirect to the primary version.\n if (!version) {\n res.redirect(`/${packageName}@${packageEntry.version}${filePath}`);\n return;\n }\n\n // If we are accessing a non-bundled asset, return the file directly.\n if (!bundled) {\n const fullPath = path.join(packageEntry.path, filePath);\n\n if (fullPath.length < packageEntry.path.length) {\n console.debug(`Response (400): \"${packageName}@${version}${filePath}\"`);\n\n res.status(400).send(`Invalid file path.`);\n } else {\n console.debug(`Unbundled response returning: \"${packageName}@${version}${filePath}\"`);\n // If the package is external, set cache headers.\n if (isExternalPackage(packageEntry.path)) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n res.sendFile(path.join(packageEntry.path, filePath));\n }\n return;\n }\n\n console.debug(`Trying to bundle \"${packageName}@${version}\"`);\n const { result } = await ensurePackageBundled({\n input: {\n name: packageName,\n version,\n shouldForce: force,\n shouldRerun: force,\n shouldWatch: true, // This will only watch the package if it is internal.\n disableCache,\n },\n ctx: context,\n });\n\n if (result.errors?.length) {\n res.status(500).type('json').send(result);\n return;\n }\n\n console.debug(`Bundle returning: \"${packageName}@${version}${filePath}\"`);\n // Try to return the result.\n if (\n result.isExternal ||\n (hash !== 'pending' &&\n hash === (await packageHashes.get({ packagePath: packageEntry.path, isSourceHashingEnabled: true })))\n ) {\n res.setHeader('Cache-Control', 'public, max-age=31536000');\n }\n\n // The below header is need to be able to track the browser cache hit ratio.\n res.setHeader('Timing-Allow-Origin', '*');\n\n res.sendFile(path.join(result.outputPath, filePath));\n}\n"]}
|
package/lib/index.d.ts
CHANGED
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,YAAY,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,YAAY,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC"}
|
package/lib/index.js
CHANGED
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC","sourcesContent":["export { startBundleServer } from './startBundleServer.js';\nexport type { BundleServer } from './types/BundleServer.js';\nexport type { BundleServerContext } from './types/BundleServerContext.js';\nexport type { BundleServerOptions } from './types/BundleServerOptions.js';\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC","sourcesContent":["export { handleBundleRequest } from './handleBundleRequest.js';\nexport { startBundleServer } from './startBundleServer.js';\nexport type { BundleServer } from './types/BundleServer.js';\nexport type { BundleServerContext } from './types/BundleServerContext.js';\nexport type { BundleServerOptions } from './types/BundleServerOptions.js';\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { BundleServer } from './types/BundleServer.js';
|
|
2
|
-
import type { BundleServerOptions } from './types/BundleServerOptions.js';
|
|
3
2
|
import type { BundleServerContext } from './types/BundleServerContext.js';
|
|
3
|
+
import type { BundleServerOptions } from './types/BundleServerOptions.js';
|
|
4
4
|
/**
|
|
5
5
|
* The bundle server handles package requests and returns bundled resources.
|
|
6
6
|
* Resources are bundled on demand and cached locally to avoid re-bundling on each request.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"startBundleServer.d.ts","sourceRoot":"","sources":["../src/startBundleServer.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"startBundleServer.d.ts","sourceRoot":"","sources":["../src/startBundleServer.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAC1E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,mBAAmB,EAC5B,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,YAAY,CAAC,CAoBvB"}
|
package/lib/startBundleServer.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { ensurePackageBundled } from '@ms-cloudpack/api-server';
|
|
2
1
|
import { createExpressApp } from '@ms-cloudpack/create-express-app';
|
|
3
|
-
import {
|
|
4
|
-
import { findResolveMapEntry, isExternalPackage } from '@ms-cloudpack/package-utilities';
|
|
5
|
-
import { slash } from '@ms-cloudpack/path-string-parsing';
|
|
6
|
-
import path from 'path';
|
|
2
|
+
import { handleBundleRequest } from './handleBundleRequest.js';
|
|
7
3
|
/**
|
|
8
4
|
* The bundle server handles package requests and returns bundled resources.
|
|
9
5
|
* Resources are bundled on demand and cached locally to avoid re-bundling on each request.
|
|
@@ -17,81 +13,13 @@ export async function startBundleServer(options, context) {
|
|
|
17
13
|
});
|
|
18
14
|
// Set up the express app routes.
|
|
19
15
|
app.get('*', (req, res) => {
|
|
20
|
-
|
|
16
|
+
handleBundleRequest({ req, res, ...options }, context).catch((err) => {
|
|
21
17
|
console.error(err?.stack || err);
|
|
22
18
|
res.status(500).send(`Error bundling: ${err}`);
|
|
23
19
|
});
|
|
24
20
|
});
|
|
21
|
+
// Save the URL.
|
|
22
|
+
context.session.urls.bundleServer = url;
|
|
25
23
|
return { close, port, url };
|
|
26
24
|
}
|
|
27
|
-
async function handleRequest(options, context) {
|
|
28
|
-
const { req, res, disableCache } = options;
|
|
29
|
-
const { session, packageHashes } = context;
|
|
30
|
-
const { resolveMap } = session;
|
|
31
|
-
const requestPath = slash(req.path.substring(1));
|
|
32
|
-
const force = req.query.force !== undefined;
|
|
33
|
-
const { packageName, version, hash, bundled, filePath } = parseRequestInfo(requestPath);
|
|
34
|
-
const packageEntry = findResolveMapEntry({ packageName, version, resolveMap });
|
|
35
|
-
if (!packageEntry) {
|
|
36
|
-
// If the package is missing, invalid arguments.
|
|
37
|
-
console.debug(`Response (404): Unrecognized package "${packageName}"`);
|
|
38
|
-
res.status(404).send(`Unrecognized package "${packageName}".`);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
console.debug(`Request: "${packageName}@${version}${filePath}"`);
|
|
42
|
-
// If the package is missing, invalid arguments.
|
|
43
|
-
if (!requestPath || !packageName) {
|
|
44
|
-
console.debug(`Response (400): "${packageName}@${version}${filePath}"`);
|
|
45
|
-
res.status(400).send(`Requests must be in the format "/{packageName}@{version}/{type}/path/file.ext".`);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
// If the version is missing, redirect to the primary version.
|
|
49
|
-
if (!version) {
|
|
50
|
-
res.redirect(`/${packageName}@${packageEntry.version}${filePath}`);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
// If we are accessing a non-bundled asset, return the file directly.
|
|
54
|
-
if (!bundled) {
|
|
55
|
-
const fullPath = path.join(packageEntry.path, filePath);
|
|
56
|
-
if (fullPath.length < packageEntry.path.length) {
|
|
57
|
-
console.debug(`Response (400): "${packageName}@${version}${filePath}"`);
|
|
58
|
-
res.status(400).send(`Invalid file path.`);
|
|
59
|
-
}
|
|
60
|
-
else {
|
|
61
|
-
console.debug(`Unbundled response returning: "${packageName}@${version}${filePath}"`);
|
|
62
|
-
// If the package is external, set cache headers.
|
|
63
|
-
if (isExternalPackage(packageEntry.path)) {
|
|
64
|
-
res.setHeader('Cache-Control', 'public, max-age=31536000');
|
|
65
|
-
}
|
|
66
|
-
res.sendFile(path.join(packageEntry.path, filePath));
|
|
67
|
-
}
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
console.debug(`Trying to bundle "${packageName}@${version}"`);
|
|
71
|
-
const { result } = await ensurePackageBundled({
|
|
72
|
-
input: {
|
|
73
|
-
name: packageName,
|
|
74
|
-
version,
|
|
75
|
-
shouldForce: force,
|
|
76
|
-
shouldRerun: force,
|
|
77
|
-
shouldWatch: true, // This will only watch the package if it is internal.
|
|
78
|
-
disableCache,
|
|
79
|
-
},
|
|
80
|
-
ctx: context,
|
|
81
|
-
});
|
|
82
|
-
if (result.errors?.length) {
|
|
83
|
-
res.status(500).type('json').send(result);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
console.debug(`Bundle returning: "${packageName}@${version}${filePath}"`);
|
|
87
|
-
// Try to return the result.
|
|
88
|
-
if (result.isExternal ||
|
|
89
|
-
(hash !== 'pending' &&
|
|
90
|
-
hash === (await packageHashes.get({ packagePath: packageEntry.path, isSourceHashingEnabled: true })))) {
|
|
91
|
-
res.setHeader('Cache-Control', 'public, max-age=31536000');
|
|
92
|
-
}
|
|
93
|
-
// The below header is need to be able to track the browser cache hit ratio.
|
|
94
|
-
res.setHeader('Timing-Allow-Origin', '*');
|
|
95
|
-
res.sendFile(path.join(result.outputPath, filePath));
|
|
96
|
-
}
|
|
97
25
|
//# sourceMappingURL=startBundleServer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"startBundleServer.js","sourceRoot":"","sources":["../src/startBundleServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"startBundleServer.js","sourceRoot":"","sources":["../src/startBundleServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAK/D;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,OAA4B,EAC5B,OAA4B;IAE5B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;IACnC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,MAAM,gBAAgB,CAAC;QACvD,SAAS,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC;QACnC,QAAQ,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM;QAClC,UAAU,EAAE,MAAM,CAAC,SAAS,EAAE,KAAK;KACpC,CAAC,CAAC;IAEH,iCAAiC;IACjC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACxB,mBAAmB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACnE,OAAO,CAAC,KAAK,CAAE,GAAa,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC;YAC5C,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,mBAAmB,GAAG,EAAE,CAAC,CAAC;QACjD,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,gBAAgB;IAChB,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,YAAY,GAAG,GAAG,CAAC;IAExC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import { createExpressApp } from '@ms-cloudpack/create-express-app';\nimport { handleBundleRequest } from './handleBundleRequest.js';\nimport type { BundleServer } from './types/BundleServer.js';\nimport type { BundleServerContext } from './types/BundleServerContext.js';\nimport type { BundleServerOptions } from './types/BundleServerOptions.js';\n\n/**\n * The bundle server handles package requests and returns bundled resources.\n * Resources are bundled on demand and cached locally to avoid re-bundling on each request.\n */\nexport async function startBundleServer(\n options: BundleServerOptions,\n context: BundleServerContext,\n): Promise<BundleServer> {\n const { config } = context.session;\n const { app, port, close, url } = await createExpressApp({\n portRange: [5500, 5501, 5502, 5503],\n hostname: config.devServer?.domain,\n sslOptions: config.devServer?.https,\n });\n\n // Set up the express app routes.\n app.get('*', (req, res) => {\n handleBundleRequest({ req, res, ...options }, context).catch((err) => {\n console.error((err as Error)?.stack || err);\n res.status(500).send(`Error bundling: ${err}`);\n });\n });\n\n // Save the URL.\n context.session.urls.bundleServer = url;\n\n return { close, port, url };\n}\n"]}
|
package/lib/tsdoc-metadata.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/bundle-server",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "An implementation of the Bundle server for Cloudpack.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/api-server": "^0.
|
|
18
|
-
"@ms-cloudpack/create-express-app": "^1.6.
|
|
19
|
-
"@ms-cloudpack/import-map": "^0.
|
|
20
|
-
"@ms-cloudpack/package-utilities": "^7.4.
|
|
17
|
+
"@ms-cloudpack/api-server": "^0.43.0",
|
|
18
|
+
"@ms-cloudpack/create-express-app": "^1.6.11",
|
|
19
|
+
"@ms-cloudpack/import-map": "^0.5.0",
|
|
20
|
+
"@ms-cloudpack/package-utilities": "^7.4.3",
|
|
21
21
|
"@ms-cloudpack/path-string-parsing": "^1.2.2"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|