ngx-cloudflare 0.0.0-stage → 0.2.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.
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WRANGLER_CONFIG_FILES = void 0;
4
+ exports.parseWranglerConfig = parseWranglerConfig;
5
+ exports.workerName = workerName;
6
+ exports.hostsFromConfig = hostsFromConfig;
7
+ exports.createWranglerConfig = createWranglerConfig;
8
+ exports.repairWranglerConfig = repairWranglerConfig;
9
+ exports.addAllowedHostsVar = addAllowedHostsVar;
10
+ const jsonc_parser_1 = require("jsonc-parser");
11
+ exports.WRANGLER_CONFIG_FILES = ['wrangler.jsonc', 'wrangler.json', 'wrangler.toml'];
12
+ const DEFAULT_HOSTS = new Set(['localhost', '127.0.0.1', '*.workers.dev']);
13
+ function parseWranglerConfig(text) {
14
+ const errors = [];
15
+ const config = (0, jsonc_parser_1.parse)(text, errors, { allowTrailingComma: true });
16
+ if (errors.length || typeof config !== 'object' || config === null) {
17
+ throw new Error('ngx-cloudflare: could not parse the wrangler config. Fix its syntax and run ng add again.');
18
+ }
19
+ return config;
20
+ }
21
+ function workerName(projectName) {
22
+ const name = projectName
23
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
24
+ .toLowerCase()
25
+ .replace(/[^a-z0-9-]+/g, '-')
26
+ .replace(/-+/g, '-')
27
+ .replace(/^-|-$/g, '')
28
+ .slice(0, 63)
29
+ .replace(/-$/, '');
30
+ return name || 'angular-app';
31
+ }
32
+ function routeHost(route) {
33
+ const pattern = typeof route === 'string' ? route : route.pattern;
34
+ if (!pattern) {
35
+ return null;
36
+ }
37
+ const host = pattern
38
+ .replace(/^https?:\/\//, '')
39
+ .split('/')[0]
40
+ .split(':')[0]
41
+ .toLowerCase();
42
+ return host || null;
43
+ }
44
+ function hostsFromConfig(config) {
45
+ const routes = [];
46
+ const collect = (scope) => {
47
+ if (scope.route) {
48
+ routes.push(scope.route);
49
+ }
50
+ routes.push(...(scope.routes ?? []));
51
+ };
52
+ collect(config);
53
+ for (const env of Object.values(config.env ?? {})) {
54
+ collect(env);
55
+ }
56
+ const hosts = [];
57
+ for (const route of routes) {
58
+ const host = routeHost(route);
59
+ if (host && !DEFAULT_HOSTS.has(host) && !hosts.includes(host)) {
60
+ hosts.push(host);
61
+ }
62
+ }
63
+ return hosts;
64
+ }
65
+ function createWranglerConfig(name, output, compatibilityDate) {
66
+ return `{
67
+ "$schema": "node_modules/wrangler/config-schema.json",
68
+ "name": "${name}",
69
+ "main": "${output.main}",
70
+ "compatibility_date": "${compatibilityDate}",
71
+ "assets": {
72
+ "binding": "ASSETS",
73
+ "directory": "${output.assets}"
74
+ },
75
+ "observability": {
76
+ "enabled": true
77
+ }
78
+ }
79
+ `;
80
+ }
81
+ function repairWranglerConfig(text, output) {
82
+ const config = parseWranglerConfig(text);
83
+ const changes = [];
84
+ const formatting = { formattingOptions: { insertSpaces: !text.includes('\n\t'), tabSize: 2 } };
85
+ let result = text;
86
+ const set = (path, value) => {
87
+ result = (0, jsonc_parser_1.applyEdits)(result, (0, jsonc_parser_1.modify)(result, path, value, formatting));
88
+ };
89
+ if (config.main !== output.main) {
90
+ set(['main'], output.main);
91
+ changes.push(config.main
92
+ ? `main: ${config.main} -> ${output.main}`
93
+ : `main: ${output.main} (was missing, so the app was deployed without server rendering)`);
94
+ }
95
+ if (config.assets?.directory !== output.assets) {
96
+ set(['assets', 'directory'], output.assets);
97
+ changes.push(`assets.directory: ${config.assets?.directory ?? '(none)'} -> ${output.assets}`);
98
+ }
99
+ if (!config.assets?.binding) {
100
+ set(['assets', 'binding'], 'ASSETS');
101
+ changes.push('assets.binding: ASSETS');
102
+ }
103
+ return { text: result, changes };
104
+ }
105
+ function addAllowedHostsVar(text, hosts) {
106
+ const config = parseWranglerConfig(text);
107
+ const current = config.vars?.['ALLOWED_HOSTS'];
108
+ const existing = typeof current === 'string' ? current.split(/[\s,]+/).filter(Boolean) : [];
109
+ const added = hosts.filter((host) => !existing.includes(host));
110
+ if (!added.length) {
111
+ return { text, added };
112
+ }
113
+ const formatting = { formattingOptions: { insertSpaces: !text.includes('\n\t'), tabSize: 2 } };
114
+ const value = [...existing, ...added].join(',');
115
+ return {
116
+ text: (0, jsonc_parser_1.applyEdits)(text, (0, jsonc_parser_1.modify)(text, ['vars', 'ALLOWED_HOSTS'], value, formatting)),
117
+ added,
118
+ };
119
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,61 @@
1
+ declare global {
2
+ namespace Cloudflare {
3
+ interface Env {}
4
+ }
5
+ }
6
+ export declare const CLOUDFLARE_CONTEXT_MARKER: unique symbol;
7
+ export declare const DEFAULT_ALLOWED_HOSTS: readonly string[];
8
+ export declare const DEFAULT_API_PREFIX = "/api/";
9
+ export interface CloudflareExecutionContext {
10
+ waitUntil(promise: Promise<unknown>): void;
11
+ passThroughOnException(): void;
12
+ }
13
+ export type CloudflareRequestCf = Record<string, unknown>;
14
+ export type CloudflareContextSource = 'workers' | 'platform-proxy';
15
+ export interface CloudflarePlatform<Env = Cloudflare.Env, Cf = CloudflareRequestCf> {
16
+ readonly env: Env;
17
+ readonly ctx: CloudflareExecutionContext;
18
+ readonly cf: Cf | undefined;
19
+ }
20
+ export interface CloudflareContext<Env = Cloudflare.Env, Cf = CloudflareRequestCf> extends CloudflarePlatform<Env, Cf> {
21
+ readonly source: CloudflareContextSource;
22
+ readonly dispatch: (request: Request) => Promise<Response | null>;
23
+ readonly [CLOUDFLARE_CONTEXT_MARKER]: true;
24
+ }
25
+ export type ApiResult = Response | null | undefined;
26
+ export type ApiHandler<Env = Cloudflare.Env> = (request: Request, env: Env, ctx: CloudflareExecutionContext) => ApiResult | Promise<ApiResult>;
27
+ export type ApiOption<Env = Cloudflare.Env> = ApiHandler<Env> | {
28
+ fetch: ApiHandler<Env>;
29
+ };
30
+ export interface CloudflareHandlerOptions<Env = Cloudflare.Env> {
31
+ allowedHosts?: readonly string[];
32
+ api?: ApiOption<Env>;
33
+ apiPrefix?: string | false;
34
+ stripProxyHeaders?: boolean;
35
+ }
36
+ export type RenderFunction = (request: Request, context: CloudflareContext<any, any> | undefined) => Promise<Response | null>;
37
+ export type DevPlatformLoader = () => Promise<CloudflarePlatform<any, any> | undefined> | undefined;
38
+ export type CloudflareFetchHandler<Env = Cloudflare.Env> = (request: Request, env?: Env, ctx?: CloudflareExecutionContext) => Promise<Response>;
39
+ export declare function isInProcessRequest(request: Request): boolean;
40
+ export declare function isCloudflareContext(value: unknown): value is CloudflareContext<any, any>;
41
+ export declare const ALLOWED_HOSTS_VAR = "ALLOWED_HOSTS";
42
+ export declare function hostsFromEnv(env: unknown): string[];
43
+ export declare function resolveAllowedHosts(extra?: readonly string[]): string[];
44
+ export declare function isHostAllowed(hostname: string, allowedHosts: readonly string[]): boolean;
45
+ export declare function hostRefusal(hostname: string): Response;
46
+ export declare function isProxyHeader(name: string): boolean;
47
+ export declare function stripProxyHeaders(request: Request): Request;
48
+ export declare function toApiHandler<Env>(api: ApiOption<Env> | undefined): ApiHandler<Env> | undefined;
49
+ export declare function callInProcess<Env>(handler: ApiHandler<Env>, request: Request, env: Env, ctx: CloudflareExecutionContext): Promise<Response | null>;
50
+ export declare function createCloudflareFetch<Env = Cloudflare.Env>(render: RenderFunction, options?: CloudflareHandlerOptions<Env>, loadDevPlatform?: DevPlatformLoader): CloudflareFetchHandler<Env>;
51
+ export interface DevPlatformOptions {
52
+ configPath?: string;
53
+ environment?: string;
54
+ persist?: boolean | {
55
+ path: string;
56
+ };
57
+ }
58
+ export interface CloudflareRequestHandlerOptions<Env = Cloudflare.Env> extends CloudflareHandlerOptions<Env> {
59
+ devPlatform?: DevPlatformOptions | false;
60
+ }
61
+ export declare function createCloudflareRequestHandler<Env = Cloudflare.Env>(options?: CloudflareRequestHandlerOptions<Env>): CloudflareFetchHandler<Env>;
@@ -0,0 +1,16 @@
1
+ import { EnvironmentProviders } from "@angular/core";
2
+ import { ApiOption, CloudflareContext, CloudflareContextSource, CloudflareExecutionContext, CloudflareRequestCf } from "ngx-cloudflare/server";
3
+ export interface CloudflareTestingOptions<Env = Cloudflare.Env, Cf = CloudflareRequestCf> {
4
+ env?: Partial<Env>;
5
+ cf?: Cf;
6
+ ctx?: Partial<CloudflareExecutionContext>;
7
+ url?: string;
8
+ api?: ApiOption<Env>;
9
+ source?: CloudflareContextSource;
10
+ }
11
+ export interface TestExecutionContext extends CloudflareExecutionContext {
12
+ readonly waitUntilPromises: Promise<unknown>[];
13
+ }
14
+ export declare function createTestExecutionContext(overrides?: Partial<CloudflareExecutionContext>): TestExecutionContext;
15
+ export declare function createCloudflareTestContext<Env = Cloudflare.Env, Cf = CloudflareRequestCf>(options?: CloudflareTestingOptions<Env, Cf>): CloudflareContext<Env, Cf>;
16
+ export declare function provideCloudflareTesting<Env = Cloudflare.Env, Cf = CloudflareRequestCf>(options?: CloudflareTestingOptions<Env, Cf>): EnvironmentProviders;
@@ -0,0 +1,36 @@
1
+ import { CloudflareContext, CloudflareContext as CloudflareContext$1, CloudflareContextSource, CloudflareExecutionContext, CloudflareExecutionContext as CloudflareExecutionContext$1, CloudflareRequestCf, CloudflareRequestCf as CloudflareRequestCf$1 } from "ngx-cloudflare/server";
2
+ import * as i0 from "@angular/core";
3
+ import { EnvironmentProviders } from "@angular/core";
4
+ import { FetchBackend, HttpEvent, HttpRequest } from "@angular/common/http";
5
+ import { Observable } from "rxjs";
6
+ export interface InjectCloudflareOptions {
7
+ required?: boolean;
8
+ }
9
+ export declare function injectCloudflare<Env = Cloudflare.Env, Cf = CloudflareRequestCf$1>(options: {
10
+ required: true;
11
+ }): CloudflareContext$1<Env, Cf>;
12
+ export declare function injectCloudflare<Env = Cloudflare.Env, Cf = CloudflareRequestCf$1>(options?: InjectCloudflareOptions): CloudflareContext$1<Env, Cf> | null;
13
+ export declare function injectEnv<Env = Cloudflare.Env>(options: {
14
+ required: true;
15
+ }): Env;
16
+ export declare function injectEnv<Env = Cloudflare.Env>(options?: InjectCloudflareOptions): Env | null;
17
+ export declare function injectExecutionContext(options: {
18
+ required: true;
19
+ }): CloudflareExecutionContext$1;
20
+ export declare function injectExecutionContext(options?: InjectCloudflareOptions): CloudflareExecutionContext$1 | null;
21
+ export declare function injectRequestCf<Cf = CloudflareRequestCf$1>(options: {
22
+ required: true;
23
+ }): Cf;
24
+ export declare function injectRequestCf<Cf = CloudflareRequestCf$1>(options?: InjectCloudflareOptions): Cf | null;
25
+ export interface ProvideCloudflareOptions {
26
+ inProcessRequests?: boolean;
27
+ }
28
+ export declare function provideCloudflare(options?: ProvideCloudflareOptions): EnvironmentProviders;
29
+ export declare class CloudflareFetchBackend extends FetchBackend {
30
+ private readonly cloudflare;
31
+ private readonly incoming;
32
+ handle(req: HttpRequest<unknown>): Observable<HttpEvent<unknown>>;
33
+ static ɵfac: i0.ɵɵFactoryDeclaration<CloudflareFetchBackend, never>;
34
+ static ɵprov: i0.ɵɵInjectableDeclaration<any>;
35
+ }
36
+ export type { CloudflareContext, CloudflareContextSource, CloudflareExecutionContext, CloudflareRequestCf };