@astrojs/cloudflare 0.0.0-cf-assets-20231021193132
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/README.md +412 -0
- package/dist/entrypoints/image-service.d.ts +6 -0
- package/dist/entrypoints/image-service.js +340 -0
- package/dist/entrypoints/server.advanced.d.ts +21 -0
- package/dist/entrypoints/server.advanced.js +44 -0
- package/dist/entrypoints/server.directory.d.ts +14 -0
- package/dist/entrypoints/server.directory.js +46 -0
- package/dist/getAdapter.d.ts +5 -0
- package/dist/getAdapter.js +31 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +517 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +13 -0
- package/dist/utils/deduplicatePatterns.d.ts +8 -0
- package/dist/utils/deduplicatePatterns.js +23 -0
- package/dist/utils/getCFObject.d.ts +2 -0
- package/dist/utils/getCFObject.js +67 -0
- package/dist/utils/parser.d.ts +27 -0
- package/dist/utils/parser.js +149 -0
- package/dist/utils/prependForwardSlash.d.ts +1 -0
- package/dist/utils/prependForwardSlash.js +3 -0
- package/dist/utils/rewriteWasmImportPath.d.ts +8 -0
- package/dist/utils/rewriteWasmImportPath.js +23 -0
- package/dist/utils/wasm-module-loader.d.ts +14 -0
- package/dist/utils/wasm-module-loader.js +101 -0
- package/package.json +65 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is a derivative work of wrangler by Cloudflare
|
|
3
|
+
* An upstream request for exposing this API was made here:
|
|
4
|
+
* https://github.com/cloudflare/workers-sdk/issues/3897
|
|
5
|
+
*
|
|
6
|
+
* Until further notice, we will be using this file as a workaround
|
|
7
|
+
* TODO: Tackle this file, once their is an decision on the upstream request
|
|
8
|
+
*/
|
|
9
|
+
import TOML from '@iarna/toml';
|
|
10
|
+
import dotenv from 'dotenv';
|
|
11
|
+
import { findUpSync } from 'find-up';
|
|
12
|
+
import * as fs from 'node:fs';
|
|
13
|
+
import { dirname, resolve } from 'node:path';
|
|
14
|
+
let _wrangler;
|
|
15
|
+
function findWranglerToml(referencePath = process.cwd(), preferJson = false) {
|
|
16
|
+
if (preferJson) {
|
|
17
|
+
return (findUpSync(`wrangler.json`, { cwd: referencePath }) ??
|
|
18
|
+
findUpSync(`wrangler.toml`, { cwd: referencePath }));
|
|
19
|
+
}
|
|
20
|
+
return findUpSync(`wrangler.toml`, { cwd: referencePath });
|
|
21
|
+
}
|
|
22
|
+
class ParseError extends Error {
|
|
23
|
+
text;
|
|
24
|
+
notes;
|
|
25
|
+
location;
|
|
26
|
+
kind;
|
|
27
|
+
constructor({ text, notes, location, kind }) {
|
|
28
|
+
super(text);
|
|
29
|
+
this.name = this.constructor.name;
|
|
30
|
+
this.text = text;
|
|
31
|
+
this.notes = notes ?? [];
|
|
32
|
+
this.location = location;
|
|
33
|
+
this.kind = kind ?? 'error';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const TOML_ERROR_NAME = 'TomlError';
|
|
37
|
+
const TOML_ERROR_SUFFIX = ' at row ';
|
|
38
|
+
function parseTOML(input, file) {
|
|
39
|
+
try {
|
|
40
|
+
// Normalize CRLF to LF to avoid hitting https://github.com/iarna/iarna-toml/issues/33.
|
|
41
|
+
const normalizedInput = input.replace(/\r\n/g, '\n');
|
|
42
|
+
return TOML.parse(normalizedInput);
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
const { name, message, line, col } = err;
|
|
46
|
+
if (name !== TOML_ERROR_NAME) {
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
const text = message.substring(0, message.lastIndexOf(TOML_ERROR_SUFFIX));
|
|
50
|
+
const lineText = input.split('\n')[line];
|
|
51
|
+
const location = {
|
|
52
|
+
lineText,
|
|
53
|
+
line: line + 1,
|
|
54
|
+
column: col - 1,
|
|
55
|
+
file,
|
|
56
|
+
fileText: input,
|
|
57
|
+
};
|
|
58
|
+
throw new ParseError({ text, location });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function tryLoadDotEnv(path) {
|
|
62
|
+
try {
|
|
63
|
+
const parsed = dotenv.parse(fs.readFileSync(path));
|
|
64
|
+
return { path, parsed };
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
// logger.debug(`Failed to load .env file "${path}":`, e);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Loads a dotenv file from <path>, preferring to read <path>.<environment> if
|
|
72
|
+
* <environment> is defined and that file exists.
|
|
73
|
+
*/
|
|
74
|
+
export function loadDotEnv(path) {
|
|
75
|
+
return tryLoadDotEnv(path);
|
|
76
|
+
}
|
|
77
|
+
function getVarsForDev(config, configPath) {
|
|
78
|
+
const configDir = resolve(dirname(configPath ?? '.'));
|
|
79
|
+
const devVarsPath = resolve(configDir, '.dev.vars');
|
|
80
|
+
const loaded = loadDotEnv(devVarsPath);
|
|
81
|
+
if (loaded !== undefined) {
|
|
82
|
+
return {
|
|
83
|
+
...config.vars,
|
|
84
|
+
...loaded.parsed,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
return config.vars;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function parseConfig() {
|
|
92
|
+
if (_wrangler)
|
|
93
|
+
return _wrangler;
|
|
94
|
+
let rawConfig;
|
|
95
|
+
const configPath = findWranglerToml(process.cwd(), false); // false = args.experimentalJsonConfig
|
|
96
|
+
if (!configPath) {
|
|
97
|
+
throw new Error('Could not find wrangler.toml');
|
|
98
|
+
}
|
|
99
|
+
// Load the configuration from disk if available
|
|
100
|
+
if (configPath?.endsWith('toml')) {
|
|
101
|
+
rawConfig = parseTOML(fs.readFileSync(configPath).toString(), configPath);
|
|
102
|
+
}
|
|
103
|
+
_wrangler = { rawConfig, configPath };
|
|
104
|
+
return { rawConfig, configPath };
|
|
105
|
+
}
|
|
106
|
+
export async function getEnvVars() {
|
|
107
|
+
const { rawConfig, configPath } = parseConfig();
|
|
108
|
+
const vars = getVarsForDev(rawConfig, configPath);
|
|
109
|
+
return vars;
|
|
110
|
+
}
|
|
111
|
+
export async function getD1Bindings() {
|
|
112
|
+
const { rawConfig } = parseConfig();
|
|
113
|
+
if (!rawConfig)
|
|
114
|
+
return [];
|
|
115
|
+
if (!rawConfig?.d1_databases)
|
|
116
|
+
return [];
|
|
117
|
+
const bindings = (rawConfig?.d1_databases).map((binding) => binding.binding);
|
|
118
|
+
return bindings;
|
|
119
|
+
}
|
|
120
|
+
export async function getR2Bindings() {
|
|
121
|
+
const { rawConfig } = parseConfig();
|
|
122
|
+
if (!rawConfig)
|
|
123
|
+
return [];
|
|
124
|
+
if (!rawConfig?.r2_buckets)
|
|
125
|
+
return [];
|
|
126
|
+
const bindings = (rawConfig?.r2_buckets).map((binding) => binding.binding);
|
|
127
|
+
return bindings;
|
|
128
|
+
}
|
|
129
|
+
export async function getKVBindings() {
|
|
130
|
+
const { rawConfig } = parseConfig();
|
|
131
|
+
if (!rawConfig)
|
|
132
|
+
return [];
|
|
133
|
+
if (!rawConfig?.kv_namespaces)
|
|
134
|
+
return [];
|
|
135
|
+
const bindings = (rawConfig?.kv_namespaces).map((binding) => binding.binding);
|
|
136
|
+
return bindings;
|
|
137
|
+
}
|
|
138
|
+
export function getDOBindings() {
|
|
139
|
+
const { rawConfig } = parseConfig();
|
|
140
|
+
if (!rawConfig)
|
|
141
|
+
return {};
|
|
142
|
+
if (!rawConfig?.durable_objects)
|
|
143
|
+
return {};
|
|
144
|
+
const output = new Object({});
|
|
145
|
+
for (const binding of rawConfig?.durable_objects.bindings) {
|
|
146
|
+
Reflect.set(output, binding.name, { className: binding.class_name });
|
|
147
|
+
}
|
|
148
|
+
return output;
|
|
149
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function prependForwardSlash(path: string): string;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import esbuild from 'esbuild';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param relativePathToAssets - relative path from the final location for the current esbuild output bundle, to the assets directory.
|
|
5
|
+
*/
|
|
6
|
+
export declare function rewriteWasmImportPath({ relativePathToAssets, }: {
|
|
7
|
+
relativePathToAssets: string;
|
|
8
|
+
}): esbuild.Plugin;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import esbuild from 'esbuild';
|
|
2
|
+
import { basename } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param relativePathToAssets - relative path from the final location for the current esbuild output bundle, to the assets directory.
|
|
6
|
+
*/
|
|
7
|
+
export function rewriteWasmImportPath({ relativePathToAssets, }) {
|
|
8
|
+
return {
|
|
9
|
+
name: 'wasm-loader',
|
|
10
|
+
setup(build) {
|
|
11
|
+
build.onResolve({ filter: /.*\.wasm.mjs$/ }, (args) => {
|
|
12
|
+
const updatedPath = [
|
|
13
|
+
relativePathToAssets.replaceAll('\\', '/'),
|
|
14
|
+
basename(args.path).replace(/\.mjs$/, ''),
|
|
15
|
+
].join('/');
|
|
16
|
+
return {
|
|
17
|
+
path: updatedPath,
|
|
18
|
+
external: true, // mark it as external in the bundle
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* Loads '*.wasm?module' imports as WebAssembly modules, which is the only way to load WASM in cloudflare workers.
|
|
4
|
+
* Current proposal for WASM modules: https://github.com/WebAssembly/esm-integration/tree/main/proposals/esm-integration
|
|
5
|
+
* Cloudflare worker WASM from javascript support: https://developers.cloudflare.com/workers/runtime-apis/webassembly/javascript/
|
|
6
|
+
* @param disabled - if true throws a helpful error message if wasm is encountered and wasm imports are not enabled,
|
|
7
|
+
* otherwise it will error obscurely in the esbuild and vite builds
|
|
8
|
+
* @param assetsDirectory - the folder name for the assets directory in the build directory. Usually '_astro'
|
|
9
|
+
* @returns Vite plugin to load WASM tagged with '?module' as a WASM modules
|
|
10
|
+
*/
|
|
11
|
+
export declare function wasmModuleLoader({ disabled, assetsDirectory, }: {
|
|
12
|
+
disabled: boolean;
|
|
13
|
+
assetsDirectory: string;
|
|
14
|
+
}): Plugin;
|
|
@@ -0,0 +1,101 @@
|
|
|
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 = `
|
|
40
|
+
const wasmModule = new WebAssembly.Module(Uint8Array.from(atob("${base64}"), c => c.charCodeAt(0)));
|
|
41
|
+
export default wasmModule
|
|
42
|
+
`;
|
|
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 `
|
|
68
|
+
import wasmModule from "__WASM_ASSET__${chunkId}.wasm.mjs";
|
|
69
|
+
export default wasmModule;
|
|
70
|
+
`;
|
|
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
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Returns a deterministic 32 bit hash code from a string
|
|
92
|
+
*/
|
|
93
|
+
function hashString(str) {
|
|
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);
|
|
101
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"//comment": "test changeset-bot",
|
|
3
|
+
"name": "@astrojs/cloudflare",
|
|
4
|
+
"description": "Deploy your site to Cloudflare Workers/Pages",
|
|
5
|
+
"version": "0.0.0-cf-assets-20231021193132",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"author": "withastro",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/withastro/adapters.git",
|
|
13
|
+
"directory": "packages/cloudflare"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"withastro",
|
|
17
|
+
"astro-adapter"
|
|
18
|
+
],
|
|
19
|
+
"bugs": "https://github.com/withastro/adapters/issues",
|
|
20
|
+
"homepage": "https://docs.astro.build/en/guides/integrations-guide/cloudflare/",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./dist/index.js",
|
|
23
|
+
"./entrypoints/server.advanced.js": "./dist/entrypoints/server.advanced.js",
|
|
24
|
+
"./entrypoints/server.directory.js": "./dist/entrypoints/server.directory.js",
|
|
25
|
+
"./image-service": "./dist/entrypoints/image-service.js",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@astrojs/underscore-redirects": "^0.3.0",
|
|
33
|
+
"@cloudflare/workers-types": "^4.20230821.0",
|
|
34
|
+
"miniflare": "3.20231010.0",
|
|
35
|
+
"@iarna/toml": "^2.2.5",
|
|
36
|
+
"dotenv": "^16.3.1",
|
|
37
|
+
"esbuild": "^0.19.2",
|
|
38
|
+
"find-up": "^6.3.0",
|
|
39
|
+
"tiny-glob": "^0.2.9",
|
|
40
|
+
"vite": "^4.4.9"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"astro": "^3.3.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"execa": "^8.0.1",
|
|
47
|
+
"fast-glob": "^3.3.1",
|
|
48
|
+
"@types/iarna__toml": "^2.0.2",
|
|
49
|
+
"strip-ansi": "^7.1.0",
|
|
50
|
+
"astro": "^3.2.3",
|
|
51
|
+
"chai": "^4.3.7",
|
|
52
|
+
"cheerio": "1.0.0-rc.12",
|
|
53
|
+
"mocha": "^10.2.0",
|
|
54
|
+
"wrangler": "^3.11.0",
|
|
55
|
+
"@astrojs/test-utils": "0.0.1"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"provenance": true
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "tsc",
|
|
62
|
+
"test": "mocha --exit --timeout 30000 test/",
|
|
63
|
+
"test:match": "mocha --exit --timeout 30000 -g"
|
|
64
|
+
}
|
|
65
|
+
}
|