@astrojs/cloudflare 7.5.3 → 7.5.4

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.
@@ -1,88 +1,101 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import {} from "vite";
4
- function wasmModuleLoader({
5
- disabled,
6
- assetsDirectory
7
- }) {
8
- const postfix = ".wasm?module";
9
- let isDev = false;
10
- return {
11
- name: "vite:wasm-module-loader",
12
- enforce: "pre",
13
- configResolved(config) {
14
- isDev = config.command === "serve";
15
- },
16
- config(_, __) {
17
- return {
18
- assetsInclude: ["**/*.wasm?module"],
19
- build: { rollupOptions: { external: /^__WASM_ASSET__.+\.wasm\.mjs$/i } }
20
- };
21
- },
22
- load(id, _) {
23
- if (!id.endsWith(postfix)) {
24
- return;
25
- }
26
- if (disabled) {
27
- throw new Error(
28
- `WASM module's cannot be loaded unless you add \`wasmModuleImports: true\` to your astro config.`
29
- );
30
- }
31
- const filePath = id.slice(0, -1 * "?module".length);
32
- const data = fs.readFileSync(filePath);
33
- const base64 = data.toString("base64");
34
- const base64Module = `
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ import {} from 'vite';
4
+ /**
5
+ * Loads '*.wasm?module' imports as WebAssembly modules, which is the only way to load WASM in cloudflare workers.
6
+ * Current proposal for WASM modules: https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
7
+ * Cloudflare worker WASM from javascript support: https://developers.cloudflare.com/workers/runtime-apis/webassembly/javascript/
8
+ * @param disabled - if true throws a helpful error message if wasm is encountered and wasm imports are not enabled,
9
+ * otherwise it will error obscurely in the esbuild and vite builds
10
+ * @param assetsDirectory - the folder name for the assets directory in the build directory. Usually '_astro'
11
+ * @returns Vite plugin to load WASM tagged with '?module' as a WASM modules
12
+ */
13
+ export function wasmModuleLoader({ disabled, assetsDirectory, }) {
14
+ const postfix = '.wasm?module';
15
+ let isDev = false;
16
+ return {
17
+ name: 'vite:wasm-module-loader',
18
+ enforce: 'pre',
19
+ configResolved(config) {
20
+ isDev = config.command === 'serve';
21
+ },
22
+ config(_, __) {
23
+ // let vite know that file format and the magic import string is intentional, and will be handled in this plugin
24
+ return {
25
+ assetsInclude: ['**/*.wasm?module'],
26
+ build: { rollupOptions: { external: /^__WASM_ASSET__.+\.wasm\.mjs$/i } },
27
+ };
28
+ },
29
+ load(id, _) {
30
+ if (!id.endsWith(postfix)) {
31
+ return;
32
+ }
33
+ if (disabled) {
34
+ throw new Error(`WASM module's cannot be loaded unless you add \`wasmModuleImports: true\` to your astro config.`);
35
+ }
36
+ const filePath = id.slice(0, -1 * '?module'.length);
37
+ const data = fs.readFileSync(filePath);
38
+ const base64 = data.toString('base64');
39
+ const base64Module = `
35
40
  const wasmModule = new WebAssembly.Module(Uint8Array.from(atob("${base64}"), c => c.charCodeAt(0)));
36
41
  export default wasmModule
37
42
  `;
38
- if (isDev) {
39
- return base64Module;
40
- } else {
41
- let hash = hashString(base64);
42
- const assetName = path.basename(filePath).split(".")[0] + "." + hash + ".wasm";
43
- this.emitFile({
44
- type: "asset",
45
- // put it explicitly in the _astro assets directory with `fileName` rather than `name` so that
46
- // vite doesn't give it a random id in its name. We need to be able to easily rewrite from
47
- // the .mjs loader and the actual wasm asset later in the ESbuild for the worker
48
- fileName: path.join(assetsDirectory, assetName),
49
- source: fs.readFileSync(filePath)
50
- });
51
- const chunkId = this.emitFile({
52
- type: "prebuilt-chunk",
53
- fileName: assetName + ".mjs",
54
- code: base64Module
55
- });
56
- return `
43
+ if (isDev) {
44
+ // no need to wire up the assets in dev mode, just rewrite
45
+ return base64Module;
46
+ }
47
+ else {
48
+ // just some shared ID
49
+ let hash = hashString(base64);
50
+ // emit the wasm binary as an asset file, to be picked up later by the esbuild bundle for the worker.
51
+ // give it a shared deterministic name to make things easy for esbuild to switch on later
52
+ const assetName = path.basename(filePath).split('.')[0] + '.' + hash + '.wasm';
53
+ this.emitFile({
54
+ type: 'asset',
55
+ // put it explicitly in the _astro assets directory with `fileName` rather than `name` so that
56
+ // vite doesn't give it a random id in its name. We need to be able to easily rewrite from
57
+ // the .mjs loader and the actual wasm asset later in the ESbuild for the worker
58
+ fileName: path.join(assetsDirectory, assetName),
59
+ source: fs.readFileSync(filePath),
60
+ });
61
+ // however, by default, the SSG generator cannot import the .wasm as a module, so embed as a base64 string
62
+ const chunkId = this.emitFile({
63
+ type: 'prebuilt-chunk',
64
+ fileName: assetName + '.mjs',
65
+ code: base64Module,
66
+ });
67
+ return `
57
68
  import wasmModule from "__WASM_ASSET__${chunkId}.wasm.mjs";
58
69
  export default wasmModule;
59
70
  `;
60
- }
61
- },
62
- // output original wasm file relative to the chunk
63
- renderChunk(code, chunk, _) {
64
- if (isDev)
65
- return;
66
- if (!/__WASM_ASSET__/g.test(code))
67
- return;
68
- const final = code.replaceAll(/__WASM_ASSET__([a-z\d]+).wasm.mjs/g, (s, assetId) => {
69
- const fileName = this.getFileName(assetId);
70
- const relativePath = path.relative(path.dirname(chunk.fileName), fileName).replaceAll("\\", "/");
71
- return `./${relativePath}`;
72
- });
73
- return { code: final };
74
- }
75
- };
71
+ }
72
+ },
73
+ // output original wasm file relative to the chunk
74
+ renderChunk(code, chunk, _) {
75
+ if (isDev)
76
+ return;
77
+ if (!/__WASM_ASSET__/g.test(code))
78
+ return;
79
+ const final = code.replaceAll(/__WASM_ASSET__([a-z\d]+).wasm.mjs/g, (s, assetId) => {
80
+ const fileName = this.getFileName(assetId);
81
+ const relativePath = path
82
+ .relative(path.dirname(chunk.fileName), fileName)
83
+ .replaceAll('\\', '/'); // fix windows paths for import
84
+ return `./${relativePath}`;
85
+ });
86
+ return { code: final };
87
+ },
88
+ };
76
89
  }
90
+ /**
91
+ * Returns a deterministic 32 bit hash code from a string
92
+ */
77
93
  function hashString(str) {
78
- let hash = 0;
79
- for (let i = 0; i < str.length; i++) {
80
- const char = str.charCodeAt(i);
81
- hash = (hash << 5) - hash + char;
82
- hash &= hash;
83
- }
84
- return new Uint32Array([hash])[0].toString(36);
94
+ let hash = 0;
95
+ for (let i = 0; i < str.length; i++) {
96
+ const char = str.charCodeAt(i);
97
+ hash = (hash << 5) - hash + char;
98
+ hash &= hash; // Convert to 32bit integer
99
+ }
100
+ return new Uint32Array([hash])[0].toString(36);
85
101
  }
86
- export {
87
- wasmModuleLoader
88
- };
package/package.json CHANGED
@@ -1,21 +1,22 @@
1
1
  {
2
+ "//comment": "test changeset-bot",
2
3
  "name": "@astrojs/cloudflare",
3
4
  "description": "Deploy your site to Cloudflare Workers/Pages",
4
- "version": "7.5.3",
5
+ "version": "7.5.4",
5
6
  "type": "module",
6
7
  "types": "./dist/index.d.ts",
7
8
  "author": "withastro",
8
9
  "license": "MIT",
9
10
  "repository": {
10
11
  "type": "git",
11
- "url": "https://github.com/withastro/astro.git",
12
- "directory": "packages/integrations/cloudflare"
12
+ "url": "https://github.com/withastro/adapters.git",
13
+ "directory": "packages/cloudflare"
13
14
  },
14
15
  "keywords": [
15
16
  "withastro",
16
17
  "astro-adapter"
17
18
  ],
18
- "bugs": "https://github.com/withastro/astro/issues",
19
+ "bugs": "https://github.com/withastro/adapters/issues",
19
20
  "homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
20
21
  "exports": {
21
22
  ".": "./dist/index.js",
@@ -27,38 +28,36 @@
27
28
  "dist"
28
29
  ],
29
30
  "dependencies": {
31
+ "@astrojs/underscore-redirects": "^0.3.0",
30
32
  "@cloudflare/workers-types": "^4.20230821.0",
31
- "miniflare": "^3.20230918.0",
33
+ "miniflare": "3.20231010.0",
32
34
  "@iarna/toml": "^2.2.5",
33
- "@miniflare/cache": "^2.14.1",
34
- "@miniflare/shared": "^2.14.1",
35
- "@miniflare/storage-memory": "^2.14.1",
36
35
  "dotenv": "^16.3.1",
37
36
  "esbuild": "^0.19.2",
38
37
  "find-up": "^6.3.0",
39
38
  "tiny-glob": "^0.2.9",
40
- "vite": "^4.4.9",
41
- "@astrojs/underscore-redirects": "0.3.1"
39
+ "vite": "^4.4.9"
42
40
  },
43
41
  "peerDependencies": {
44
42
  "astro": "^3.3.0"
45
43
  },
46
44
  "devDependencies": {
45
+ "execa": "^8.0.1",
46
+ "fast-glob": "^3.3.1",
47
47
  "@types/iarna__toml": "^2.0.2",
48
+ "strip-ansi": "^7.1.0",
49
+ "astro": "^3.2.3",
48
50
  "chai": "^4.3.7",
49
51
  "cheerio": "1.0.0-rc.12",
50
52
  "mocha": "^10.2.0",
51
- "wrangler": "^3.5.1",
52
- "astro": "3.3.0",
53
- "astro-scripts": "0.0.14"
53
+ "wrangler": "^3.11.0",
54
+ "@astrojs/test-utils": "0.0.1"
54
55
  },
55
56
  "publishConfig": {
56
57
  "provenance": true
57
58
  },
58
59
  "scripts": {
59
- "build": "astro-scripts build \"src/**/*.ts\" && tsc",
60
- "build:ci": "astro-scripts build \"src/**/*.ts\"",
61
- "dev": "astro-scripts dev \"src/**/*.ts\"",
60
+ "build": "tsc",
62
61
  "test": "mocha --exit --timeout 30000 test/",
63
62
  "test:match": "mocha --exit --timeout 30000 -g"
64
63
  }
package/LICENSE DELETED
@@ -1,61 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Fred K. Schott
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
22
-
23
-
24
- """
25
- This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
26
-
27
- Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
28
-
29
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
30
-
31
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
32
-
33
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
- """
35
-
36
-
37
- """
38
- This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
39
-
40
- MIT License
41
-
42
- Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
43
-
44
- Permission is hereby granted, free of charge, to any person obtaining a copy
45
- of this software and associated documentation files (the "Software"), to deal
46
- in the Software without restriction, including without limitation the rights
47
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
48
- copies of the Software, and to permit persons to whom the Software is
49
- furnished to do so, subject to the following conditions:
50
-
51
- The above copyright notice and this permission notice shall be included in all
52
- copies or substantial portions of the Software.
53
-
54
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
60
- SOFTWARE.
61
- """