miniflare 0.0.0-e4fe35cc5 → 0.0.0-ecd82e847

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.
@@ -58,6 +58,13 @@ export declare class __MiniflareFunctionWrapper {
58
58
 
59
59
  export declare type AnyHeaders = http.IncomingHttpHeaders | string[];
60
60
 
61
+ export declare type AssetReverseMap = {
62
+ [pathHash: string]: {
63
+ filePath: string;
64
+ contentType: string;
65
+ };
66
+ };
67
+
61
68
  export declare const ASSETS_PLUGIN: Plugin<typeof AssetsOptionsSchema>;
62
69
 
63
70
  export declare const AssetsOptionsSchema: z.ZodObject<{
@@ -141,7 +148,19 @@ export declare function base64Encode(value: string): string;
141
148
 
142
149
  export { BodyInit }
143
150
 
144
- export declare const buildAssetsManifest: (dir: string) => Promise<Uint8Array>;
151
+ /**
152
+ * The Asset Manifest and Asset Reverse Map are used to map a request path to an asset.
153
+ * 1. Hash path of request
154
+ * 2. Use this path hash to find the manifest entry
155
+ * 3. Get content hash from manifest entry
156
+ * 4a. In prod, use content hash to get asset from KV
157
+ * 4b. In dev, we fake out the KV store and use the file system instead.
158
+ * Use content hash to get file path from asset reverse map.
159
+ */
160
+ export declare const buildAssetManifest: (dir: string) => Promise<{
161
+ encodedAssetManifest: Uint8Array;
162
+ assetsReverseMap: AssetReverseMap;
163
+ }>;
145
164
 
146
165
  export declare const CACHE_PLUGIN: Plugin<typeof CacheOptionsSchema, typeof CacheSharedOptionsSchema>;
147
166
 
@@ -1673,6 +1692,11 @@ export declare interface LogOptions {
1673
1692
  suffix?: string;
1674
1693
  }
1675
1694
 
1695
+ export declare type ManifestEntry = {
1696
+ pathHash: Uint8Array;
1697
+ contentHash: Uint8Array;
1698
+ };
1699
+
1676
1700
  export declare interface MatcherRegExps {
1677
1701
  include: RegExp[];
1678
1702
  exclude: RegExp[];
package/dist/src/index.js CHANGED
@@ -2547,7 +2547,7 @@ __export(src_exports, {
2547
2547
  _transformsForContentEncodingAndContentType: () => _transformsForContentEncodingAndContentType,
2548
2548
  base64Decode: () => base64Decode,
2549
2549
  base64Encode: () => base64Encode,
2550
- buildAssetsManifest: () => buildAssetsManifest,
2550
+ buildAssetManifest: () => buildAssetManifest,
2551
2551
  compileModuleRules: () => compileModuleRules,
2552
2552
  coupleWebSocket: () => coupleWebSocket,
2553
2553
  createFetchMock: () => createFetchMock,
@@ -7499,12 +7499,14 @@ var ASSETS_PLUGIN = {
7499
7499
  if (!options.assets) {
7500
7500
  return [];
7501
7501
  }
7502
- const assetsReverseMap = await createReverseMap(options.assets?.path);
7503
7502
  const storageServiceName = `${ASSETS_PLUGIN_NAME}:storage`;
7504
7503
  const storageService = {
7505
7504
  name: storageServiceName,
7506
7505
  disk: { path: options.assets.path, writable: true }
7507
7506
  };
7507
+ const { encodedAssetManifest, assetsReverseMap } = await buildAssetManifest(
7508
+ options.assets.path
7509
+ );
7508
7510
  const namespaceService = {
7509
7511
  name: ASSETS_KV_SERVICE_NAME,
7510
7512
  worker: {
@@ -7523,12 +7525,11 @@ var ASSETS_PLUGIN = {
7523
7525
  },
7524
7526
  {
7525
7527
  name: "ASSETS_REVERSE_MAP",
7526
- json: assetsReverseMap
7528
+ json: JSON.stringify(assetsReverseMap)
7527
7529
  }
7528
7530
  ]
7529
7531
  }
7530
7532
  };
7531
- const assetsManifest = await buildAssetsManifest(options.assets.path);
7532
7533
  const assetService = {
7533
7534
  name: ASSETS_SERVICE_NAME,
7534
7535
  worker: {
@@ -7546,7 +7547,7 @@ var ASSETS_PLUGIN = {
7546
7547
  },
7547
7548
  {
7548
7549
  name: "ASSETS_MANIFEST",
7549
- data: assetsManifest
7550
+ data: encodedAssetManifest
7550
7551
  },
7551
7552
  {
7552
7553
  name: "CONFIG",
@@ -7584,15 +7585,16 @@ var ASSETS_PLUGIN = {
7584
7585
  return [storageService, namespaceService, assetService, routerService];
7585
7586
  }
7586
7587
  };
7587
- var buildAssetsManifest = async (dir) => {
7588
- const manifest = await walk(dir);
7588
+ var buildAssetManifest = async (dir) => {
7589
+ const { manifest, assetsReverseMap } = await walk(dir);
7589
7590
  const sortedAssetManifest = sortManifest(manifest);
7590
7591
  const encodedAssetManifest = encodeManifest(sortedAssetManifest);
7591
- return encodedAssetManifest;
7592
+ return { encodedAssetManifest, assetsReverseMap };
7592
7593
  };
7593
7594
  var walk = async (dir) => {
7594
7595
  const files = await import_promises7.default.readdir(dir, { recursive: true });
7595
7596
  const manifest = [];
7597
+ const assetsReverseMap = {};
7596
7598
  let counter = 0;
7597
7599
  await Promise.all(
7598
7600
  files.map(async (file) => {
@@ -7619,9 +7621,20 @@ Cloudflare Workers supports assets with sizes of up to ${prettyBytes(
7619
7621
  Ensure all assets in your assets directory "${dir}" conform with the Workers maximum size requirement.`
7620
7622
  );
7621
7623
  }
7622
- manifest.push(
7623
- await hashPath(encodeFilePath(relativeFilepath, import_node_path.default.sep))
7624
- );
7624
+ const [pathHash, contentHash] = await Promise.all([
7625
+ hashPath(encodeFilePath(relativeFilepath, import_node_path.default.sep)),
7626
+ hashPath(
7627
+ encodeFilePath(filepath, import_node_path.default.sep) + filestat.mtimeMs.toString()
7628
+ )
7629
+ ]);
7630
+ manifest.push({
7631
+ pathHash,
7632
+ contentHash
7633
+ });
7634
+ assetsReverseMap[bytesToHex(contentHash)] = {
7635
+ filePath: relativeFilepath,
7636
+ contentType: getContentType(filepath)
7637
+ };
7625
7638
  counter++;
7626
7639
  }
7627
7640
  })
@@ -7633,23 +7646,23 @@ Cloudflare Workers supports up to ${MAX_ASSET_COUNT.toLocaleString()} assets in
7633
7646
  Ensure your assets directory contains a maximum of ${MAX_ASSET_COUNT.toLocaleString()} files, and that you have specified your assets directory correctly.`
7634
7647
  );
7635
7648
  }
7636
- return manifest;
7649
+ return { manifest, assetsReverseMap };
7637
7650
  };
7638
7651
  var sortManifest = (manifest) => {
7639
7652
  return manifest.sort(comparisonFn);
7640
7653
  };
7641
7654
  var comparisonFn = (a, b) => {
7642
- if (a.length < b.length) {
7655
+ if (a.pathHash.length < b.pathHash.length) {
7643
7656
  return -1;
7644
7657
  }
7645
- if (a.length > b.length) {
7658
+ if (a.pathHash.length > b.pathHash.length) {
7646
7659
  return 1;
7647
7660
  }
7648
- for (const [i, v] of a.entries()) {
7649
- if (v < b[i]) {
7661
+ for (const [i, v] of a.pathHash.entries()) {
7662
+ if (v < b.pathHash[i]) {
7650
7663
  return -1;
7651
7664
  }
7652
- if (v > b[i]) {
7665
+ if (v > b.pathHash[i]) {
7653
7666
  return 1;
7654
7667
  }
7655
7668
  }
@@ -7661,34 +7674,14 @@ var encodeManifest = (manifest) => {
7661
7674
  );
7662
7675
  for (const [i, entry] of manifest.entries()) {
7663
7676
  const entryOffset = HEADER_SIZE + i * ENTRY_SIZE;
7664
- assetManifestBytes.set(entry, entryOffset + PATH_HASH_OFFSET);
7665
- assetManifestBytes.set(entry, entryOffset + CONTENT_HASH_OFFSET);
7677
+ assetManifestBytes.set(entry.pathHash, entryOffset + PATH_HASH_OFFSET);
7678
+ assetManifestBytes.set(
7679
+ entry.contentHash,
7680
+ entryOffset + CONTENT_HASH_OFFSET
7681
+ );
7666
7682
  }
7667
7683
  return assetManifestBytes;
7668
7684
  };
7669
- var createReverseMap = async (dir) => {
7670
- const files = await import_promises7.default.readdir(dir, { recursive: true });
7671
- const assetsReverseMap = {};
7672
- await Promise.all(
7673
- files.map(async (file) => {
7674
- const filepath = import_node_path.default.join(dir, file);
7675
- const relativeFilepath = import_node_path.default.relative(dir, filepath);
7676
- const filestat = await import_promises7.default.stat(filepath);
7677
- if (filestat.isSymbolicLink() || filestat.isDirectory()) {
7678
- return;
7679
- } else {
7680
- const pathHash = bytesToHex(
7681
- await hashPath(encodeFilePath(relativeFilepath, import_node_path.default.sep))
7682
- );
7683
- assetsReverseMap[pathHash] = {
7684
- filePath: relativeFilepath,
7685
- contentType: getContentType(filepath)
7686
- };
7687
- }
7688
- })
7689
- );
7690
- return JSON.stringify(assetsReverseMap);
7691
- };
7692
7685
  var bytesToHex = (buffer) => {
7693
7686
  return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, "0")).join("");
7694
7687
  };
@@ -9084,8 +9077,15 @@ function getQueueProducers(allWorkerOpts) {
9084
9077
  ])
9085
9078
  );
9086
9079
  }
9087
- for (const [bindingName, opts] of Object.entries(workerProducers)) {
9088
- queueProducers.set(bindingName, { workerName, ...opts });
9080
+ const producersIterable = Object.entries(
9081
+ workerProducers
9082
+ );
9083
+ for (const [bindingName, opts] of producersIterable) {
9084
+ if (typeof opts === "string") {
9085
+ queueProducers.set(bindingName, { workerName, queueName: opts });
9086
+ } else {
9087
+ queueProducers.set(bindingName, { workerName, ...opts });
9088
+ }
9089
9089
  }
9090
9090
  }
9091
9091
  }
@@ -10195,7 +10195,7 @@ var Miniflare2 = class {
10195
10195
  _transformsForContentEncodingAndContentType,
10196
10196
  base64Decode,
10197
10197
  base64Encode,
10198
- buildAssetsManifest,
10198
+ buildAssetManifest,
10199
10199
  compileModuleRules,
10200
10200
  coupleWebSocket,
10201
10201
  createFetchMock,