@astrojs/cloudflare 0.0.0-404-fix-20231115224256
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 +421 -0
- package/dist/entrypoints/image-service.d.ts +3 -0
- package/dist/entrypoints/image-service.js +34 -0
- package/dist/entrypoints/server.advanced.d.ts +21 -0
- package/dist/entrypoints/server.advanced.js +38 -0
- package/dist/entrypoints/server.directory.d.ts +14 -0
- package/dist/entrypoints/server.directory.js +40 -0
- package/dist/getAdapter.d.ts +5 -0
- package/dist/getAdapter.js +30 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +511 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +13 -0
- package/dist/utils/assets.d.ts +14 -0
- package/dist/utils/assets.js +95 -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/image-config.d.ts +12 -0
- package/dist/utils/image-config.js +21 -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,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove duplicates and redundant patterns from an `include` or `exclude` list.
|
|
3
|
+
* Otherwise Cloudflare will throw an error on deployment. Plus, it saves more entries.
|
|
4
|
+
* E.g. `['/foo/*', '/foo/*', '/foo/bar'] => ['/foo/*']`
|
|
5
|
+
* @param patterns a list of `include` or `exclude` patterns
|
|
6
|
+
* @returns a deduplicated list of patterns
|
|
7
|
+
*/
|
|
8
|
+
export function deduplicatePatterns(patterns) {
|
|
9
|
+
const openPatterns = [];
|
|
10
|
+
// A value in the set may only occur once; it is unique in the set's collection.
|
|
11
|
+
// ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
|
|
12
|
+
return [...new Set(patterns)]
|
|
13
|
+
.sort((a, b) => a.length - b.length)
|
|
14
|
+
.filter((pattern) => {
|
|
15
|
+
if (openPatterns.some((p) => p.test(pattern))) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
if (pattern.endsWith('*')) {
|
|
19
|
+
openPatterns.push(new RegExp(`^${pattern.replace(/(\*\/)*\*$/g, '.*')}`));
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export async function getCFObject(runtimeMode) {
|
|
2
|
+
const CF_ENDPOINT = 'https://workers.cloudflare.com/cf.json';
|
|
3
|
+
const CF_FALLBACK = {
|
|
4
|
+
asOrganization: '',
|
|
5
|
+
asn: 395747,
|
|
6
|
+
colo: 'DFW',
|
|
7
|
+
city: 'Austin',
|
|
8
|
+
region: 'Texas',
|
|
9
|
+
regionCode: 'TX',
|
|
10
|
+
metroCode: '635',
|
|
11
|
+
postalCode: '78701',
|
|
12
|
+
country: 'US',
|
|
13
|
+
continent: 'NA',
|
|
14
|
+
timezone: 'America/Chicago',
|
|
15
|
+
latitude: '30.27130',
|
|
16
|
+
longitude: '-97.74260',
|
|
17
|
+
clientTcpRtt: 0,
|
|
18
|
+
httpProtocol: 'HTTP/1.1',
|
|
19
|
+
requestPriority: 'weight=192;exclusive=0',
|
|
20
|
+
tlsCipher: 'AEAD-AES128-GCM-SHA256',
|
|
21
|
+
tlsVersion: 'TLSv1.3',
|
|
22
|
+
tlsClientAuth: {
|
|
23
|
+
certPresented: '0',
|
|
24
|
+
certVerified: 'NONE',
|
|
25
|
+
certRevoked: '0',
|
|
26
|
+
certIssuerDN: '',
|
|
27
|
+
certSubjectDN: '',
|
|
28
|
+
certIssuerDNRFC2253: '',
|
|
29
|
+
certSubjectDNRFC2253: '',
|
|
30
|
+
certIssuerDNLegacy: '',
|
|
31
|
+
certSubjectDNLegacy: '',
|
|
32
|
+
certSerial: '',
|
|
33
|
+
certIssuerSerial: '',
|
|
34
|
+
certSKI: '',
|
|
35
|
+
certIssuerSKI: '',
|
|
36
|
+
certFingerprintSHA1: '',
|
|
37
|
+
certFingerprintSHA256: '',
|
|
38
|
+
certNotBefore: '',
|
|
39
|
+
certNotAfter: '',
|
|
40
|
+
},
|
|
41
|
+
edgeRequestKeepAliveStatus: 0,
|
|
42
|
+
hostMetadata: undefined,
|
|
43
|
+
clientTrustScore: 99,
|
|
44
|
+
botManagement: {
|
|
45
|
+
corporateProxy: false,
|
|
46
|
+
verifiedBot: false,
|
|
47
|
+
ja3Hash: '25b4882c2bcb50cd6b469ff28c596742',
|
|
48
|
+
staticResource: false,
|
|
49
|
+
detectionIds: [],
|
|
50
|
+
score: 99,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
if (runtimeMode === 'local') {
|
|
54
|
+
return CF_FALLBACK;
|
|
55
|
+
}
|
|
56
|
+
else if (runtimeMode === 'remote') {
|
|
57
|
+
try {
|
|
58
|
+
const res = await fetch(CF_ENDPOINT);
|
|
59
|
+
const cfText = await res.text();
|
|
60
|
+
const storedCf = JSON.parse(cfText);
|
|
61
|
+
return storedCf;
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
return CF_FALLBACK;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AstroConfig, AstroIntegrationLogger } from 'astro';
|
|
2
|
+
export declare function prepareImageConfig(service: string, config: AstroConfig['image'], command: 'dev' | 'build' | 'preview', logger: AstroIntegrationLogger): {
|
|
3
|
+
service: import("astro").ImageServiceConfig<Record<string, any>>;
|
|
4
|
+
domains: string[];
|
|
5
|
+
remotePatterns: {
|
|
6
|
+
protocol?: string | undefined;
|
|
7
|
+
hostname?: string | undefined;
|
|
8
|
+
port?: string | undefined;
|
|
9
|
+
pathname?: string | undefined;
|
|
10
|
+
}[];
|
|
11
|
+
endpoint?: string | undefined;
|
|
12
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { passthroughImageService, sharpImageService } from 'astro/config';
|
|
2
|
+
export function prepareImageConfig(service, config, command, logger) {
|
|
3
|
+
switch (service) {
|
|
4
|
+
case 'passthrough':
|
|
5
|
+
return { ...config, service: passthroughImageService() };
|
|
6
|
+
case 'cloudflare':
|
|
7
|
+
return {
|
|
8
|
+
...config,
|
|
9
|
+
service: command === 'dev'
|
|
10
|
+
? sharpImageService()
|
|
11
|
+
: { entrypoint: '@astrojs/cloudflare/image-service' },
|
|
12
|
+
};
|
|
13
|
+
default:
|
|
14
|
+
if (config.service.entrypoint === 'astro/assets/services/sharp' ||
|
|
15
|
+
config.service.entrypoint === 'astro/assets/services/squoosh') {
|
|
16
|
+
logger.warn(`The current configuration does not support image optimization. To allow your project to build with the original, unoptimized images, the image service has been automatically switched to the 'noop' option. See https://docs.astro.build/en/reference/configuration-reference/#imageservice`);
|
|
17
|
+
return { ...config, service: passthroughImageService() };
|
|
18
|
+
}
|
|
19
|
+
return { ...config };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
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 dotenv from 'dotenv';
|
|
10
|
+
export interface DotEnv {
|
|
11
|
+
path: string;
|
|
12
|
+
parsed: dotenv.DotenvParseOutput;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Loads a dotenv file from <path>, preferring to read <path>.<environment> if
|
|
16
|
+
* <environment> is defined and that file exists.
|
|
17
|
+
*/
|
|
18
|
+
export declare function loadDotEnv(path: string): DotEnv | undefined;
|
|
19
|
+
export declare function getEnvVars(): Promise<any>;
|
|
20
|
+
export declare function getD1Bindings(): Promise<string[]>;
|
|
21
|
+
export declare function getR2Bindings(): Promise<string[]>;
|
|
22
|
+
export declare function getKVBindings(): Promise<string[]>;
|
|
23
|
+
export declare function getDOBindings(): Record<string, {
|
|
24
|
+
scriptName?: string | undefined;
|
|
25
|
+
unsafeUniqueKey?: string | undefined;
|
|
26
|
+
className: string;
|
|
27
|
+
}>;
|
|
@@ -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-404-fix-20231115224256",
|
|
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.3",
|
|
33
|
+
"@cloudflare/workers-types": "^4.20231025.0",
|
|
34
|
+
"miniflare": "3.20231025.1",
|
|
35
|
+
"@iarna/toml": "^2.2.5",
|
|
36
|
+
"dotenv": "^16.3.1",
|
|
37
|
+
"esbuild": "^0.19.5",
|
|
38
|
+
"find-up": "^6.3.0",
|
|
39
|
+
"tiny-glob": "^0.2.9",
|
|
40
|
+
"vite": "^4.5.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"astro": "^3.4.3"
|
|
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.4.3",
|
|
51
|
+
"chai": "^4.3.10",
|
|
52
|
+
"cheerio": "1.0.0-rc.12",
|
|
53
|
+
"mocha": "^10.2.0",
|
|
54
|
+
"wrangler": "^3.15.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
|
+
}
|