@constructive-io/node 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
4
+ Copyright (c) 2025 Constructive <developers@constructive.io>
5
+ Copyright (c) 2020-present, Interweb, Inc.
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,125 @@
1
+ # @constructive-io/node
2
+
3
+ Drop-in replacement for `@constructive-io/sdk` for Node.js applications.
4
+
5
+ ## Why?
6
+
7
+ The base `@constructive-io/sdk` uses the Fetch API, which works great in browsers but has two limitations in Node.js:
8
+
9
+ 1. **DNS resolution**: Node.js cannot resolve `*.localhost` subdomains (e.g., `auth.localhost`), returning `ENOTFOUND`. Browsers handle this automatically.
10
+ 2. **Host header**: The Fetch API treats `Host` as a forbidden header and silently drops it. The Constructive GraphQL server uses Host-header subdomain routing, so this header must be preserved.
11
+
12
+ `@constructive-io/node` includes `NodeHttpAdapter`, which uses `node:http`/`node:https` directly to bypass both issues.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @constructive-io/node
18
+ # or
19
+ pnpm add @constructive-io/node
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Everything from `@constructive-io/sdk` is re-exported, so this is a drop-in replacement. Just change your import:
25
+
26
+ ```ts
27
+ // Before (browser/universal):
28
+ import { admin, auth, public_ } from '@constructive-io/sdk';
29
+
30
+ // After (Node.js):
31
+ import { admin, auth, public_, NodeHttpAdapter } from '@constructive-io/node';
32
+ ```
33
+
34
+ ### Using NodeHttpAdapter with the ORM client
35
+
36
+ ```ts
37
+ import { admin, NodeHttpAdapter } from '@constructive-io/node';
38
+
39
+ const adapter = new NodeHttpAdapter('http://auth.localhost:3000/graphql', {
40
+ Authorization: 'Bearer token',
41
+ });
42
+
43
+ const db = admin.orm.createClient({ adapter });
44
+
45
+ const results = await db.appPermission.findMany({
46
+ select: { id: true, name: true },
47
+ }).execute();
48
+ ```
49
+
50
+ ### Per-request options
51
+
52
+ `NodeHttpAdapter.execute()` supports per-request headers and cancellation:
53
+
54
+ ```ts
55
+ const adapter = new NodeHttpAdapter('http://localhost:3000/graphql');
56
+
57
+ // Per-request headers
58
+ await adapter.execute(query, variables, {
59
+ headers: { 'X-Request-Id': '123' },
60
+ });
61
+
62
+ // Request cancellation
63
+ const controller = new AbortController();
64
+ await adapter.execute(query, variables, {
65
+ signal: controller.signal,
66
+ });
67
+ ```
68
+
69
+ ---
70
+
71
+ ## Education and Tutorials
72
+
73
+ 1. ๐Ÿš€ [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
74
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
75
+
76
+ 2. ๐Ÿ“ฆ [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
77
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
78
+
79
+ 3. โœ๏ธ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
80
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
81
+
82
+ 4. ๐Ÿงช [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
83
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
84
+
85
+ 5. โšก [Supabase Testing](https://constructive.io/learn/supabase)
86
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
87
+
88
+ 6. ๐Ÿ’ง [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
89
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
90
+
91
+ 7. ๐Ÿ”ง [Troubleshooting](https://constructive.io/learn/troubleshooting)
92
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
93
+
94
+ ## Related Constructive Tooling
95
+
96
+ ### ๐Ÿ“ฆ Package Management
97
+
98
+ * [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **๐Ÿ–ฅ๏ธ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
99
+
100
+ ### ๐Ÿงช Testing
101
+
102
+ * [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **๐Ÿ“Š Isolated testing environments** with per-test transaction rollbacksโ€”ideal for integration tests, complex migrations, and RLS simulation.
103
+ * [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **๐ŸŒฑ PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
104
+ * [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **๐Ÿงช Supabase-native test harness** preconfigured for the local Supabase stackโ€”per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
105
+ * [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **๐Ÿ” Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
106
+ * [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **๐Ÿ”’ Session context injection** to add session-local context (e.g., `SET LOCAL`) into queriesโ€”ideal for setting `role`, `jwt.claims`, and other session settings.
107
+
108
+ ### ๐Ÿง  Parsing & AST
109
+
110
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **๐Ÿ”„ SQL conversion engine** that interprets and converts PostgreSQL syntax.
111
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **๐ŸŒ‰ Node.js bindings** for `libpg_query`, converting SQL into parse trees.
112
+ * [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **๐Ÿ“ฆ Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
113
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **๐Ÿท๏ธ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
114
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **๐Ÿ“ Type definitions** for PostgreSQL AST nodes in TypeScript.
115
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **๐Ÿ› ๏ธ AST utilities** for constructing and transforming PostgreSQL syntax trees.
116
+
117
+ ## Credits
118
+
119
+ **๐Ÿ›  Built by the [Constructive](https://constructive.io) team โ€” creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
120
+
121
+ ## Disclaimer
122
+
123
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
124
+
125
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/esm/index.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @constructive-io/node
3
+ *
4
+ * Drop-in replacement for @constructive-io/sdk with Node.js HTTP adapter.
5
+ *
6
+ * For Node.js applications, use this package instead of @constructive-io/sdk.
7
+ * It re-exports everything from the base SDK and adds NodeHttpAdapter,
8
+ * which uses node:http/node:https instead of the Fetch API to handle:
9
+ *
10
+ * 1. *.localhost subdomain DNS resolution (Node can't resolve these natively)
11
+ * 2. Host header preservation (Fetch API silently drops it)
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { auth, NodeHttpAdapter } from '@constructive-io/node';
16
+ *
17
+ * const adapter = new NodeHttpAdapter(
18
+ * 'http://auth.localhost:3000/graphql',
19
+ * { Authorization: 'Bearer token' },
20
+ * );
21
+ *
22
+ * const db = auth.orm.createClient({ adapter });
23
+ *
24
+ * const users = await db.user.findMany({
25
+ * select: { id: true, name: true },
26
+ * }).execute();
27
+ * ```
28
+ */
29
+ export * from '@constructive-io/sdk';
30
+ export { NodeHttpAdapter } from './node-http-adapter';
31
+ export type { NodeHttpExecuteOptions } from './node-http-adapter';
package/esm/index.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @constructive-io/node
3
+ *
4
+ * Drop-in replacement for @constructive-io/sdk with Node.js HTTP adapter.
5
+ *
6
+ * For Node.js applications, use this package instead of @constructive-io/sdk.
7
+ * It re-exports everything from the base SDK and adds NodeHttpAdapter,
8
+ * which uses node:http/node:https instead of the Fetch API to handle:
9
+ *
10
+ * 1. *.localhost subdomain DNS resolution (Node can't resolve these natively)
11
+ * 2. Host header preservation (Fetch API silently drops it)
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { auth, NodeHttpAdapter } from '@constructive-io/node';
16
+ *
17
+ * const adapter = new NodeHttpAdapter(
18
+ * 'http://auth.localhost:3000/graphql',
19
+ * { Authorization: 'Bearer token' },
20
+ * );
21
+ *
22
+ * const db = auth.orm.createClient({ adapter });
23
+ *
24
+ * const users = await db.user.findMany({
25
+ * select: { id: true, name: true },
26
+ * }).execute();
27
+ * ```
28
+ */
29
+ // Re-export everything from the base SDK
30
+ export * from '@constructive-io/sdk';
31
+ // Export the Node.js HTTP adapter
32
+ export { NodeHttpAdapter } from './node-http-adapter';
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Node HTTP Adapter for Node.js applications
3
+ *
4
+ * Implements the GraphQLAdapter interface using node:http / node:https
5
+ * instead of the Fetch API. This solves two Node.js limitations:
6
+ *
7
+ * 1. DNS: Node.js cannot resolve *.localhost subdomains (ENOTFOUND).
8
+ * Browsers handle this automatically, but Node requires manual resolution.
9
+ *
10
+ * 2. Host header: The Fetch API treats "Host" as a forbidden request header
11
+ * and silently drops it. The Constructive GraphQL server uses Host-header
12
+ * subdomain routing (enableServicesApi), so this header must be preserved.
13
+ *
14
+ * By using node:http.request directly, both issues are bypassed cleanly
15
+ * without any global patching.
16
+ */
17
+ import type { GraphQLAdapter, QueryResult } from '@constructive-io/graphql-types';
18
+ /**
19
+ * Options for individual execute calls.
20
+ * Allows per-request header overrides and request cancellation.
21
+ */
22
+ export interface NodeHttpExecuteOptions {
23
+ /** Additional headers to include in this request only */
24
+ headers?: Record<string, string>;
25
+ /** AbortSignal for request cancellation */
26
+ signal?: AbortSignal;
27
+ }
28
+ /**
29
+ * GraphQL adapter that uses node:http/node:https for requests.
30
+ *
31
+ * Handles *.localhost subdomains by rewriting the hostname to "localhost"
32
+ * and injecting the original Host header for server-side subdomain routing.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * import { NodeHttpAdapter } from '@constructive-io/node';
37
+ *
38
+ * const adapter = new NodeHttpAdapter('http://auth.localhost:3000/graphql');
39
+ * const db = createClient({ adapter });
40
+ * ```
41
+ */
42
+ export declare class NodeHttpAdapter implements GraphQLAdapter {
43
+ private endpoint;
44
+ private headers;
45
+ private url;
46
+ constructor(endpoint: string, headers?: Record<string, string>);
47
+ execute<T>(document: string, variables?: Record<string, unknown>, options?: NodeHttpExecuteOptions): Promise<QueryResult<T>>;
48
+ setHeaders(headers: Record<string, string>): void;
49
+ getEndpoint(): string;
50
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Node HTTP Adapter for Node.js applications
3
+ *
4
+ * Implements the GraphQLAdapter interface using node:http / node:https
5
+ * instead of the Fetch API. This solves two Node.js limitations:
6
+ *
7
+ * 1. DNS: Node.js cannot resolve *.localhost subdomains (ENOTFOUND).
8
+ * Browsers handle this automatically, but Node requires manual resolution.
9
+ *
10
+ * 2. Host header: The Fetch API treats "Host" as a forbidden request header
11
+ * and silently drops it. The Constructive GraphQL server uses Host-header
12
+ * subdomain routing (enableServicesApi), so this header must be preserved.
13
+ *
14
+ * By using node:http.request directly, both issues are bypassed cleanly
15
+ * without any global patching.
16
+ */
17
+ import http from 'node:http';
18
+ import https from 'node:https';
19
+ /**
20
+ * Check if a hostname is a localhost subdomain that needs special handling.
21
+ * Returns true for *.localhost (e.g. auth.localhost) but not bare "localhost".
22
+ */
23
+ function isLocalhostSubdomain(hostname) {
24
+ return hostname.endsWith('.localhost') && hostname !== 'localhost';
25
+ }
26
+ /**
27
+ * Make an HTTP/HTTPS request using native Node modules.
28
+ * Supports optional AbortSignal for request cancellation.
29
+ */
30
+ function makeRequest(url, options, body, signal) {
31
+ return new Promise((resolve, reject) => {
32
+ if (signal?.aborted) {
33
+ reject(new Error('The operation was aborted'));
34
+ return;
35
+ }
36
+ const protocol = url.protocol === 'https:' ? https : http;
37
+ const req = protocol.request(url, options, (res) => {
38
+ let data = '';
39
+ res.setEncoding('utf8');
40
+ res.on('data', (chunk) => {
41
+ data += chunk;
42
+ });
43
+ res.on('end', () => {
44
+ resolve({
45
+ statusCode: res.statusCode || 0,
46
+ statusMessage: res.statusMessage || '',
47
+ data,
48
+ });
49
+ });
50
+ });
51
+ req.on('error', reject);
52
+ if (signal) {
53
+ const onAbort = () => {
54
+ req.destroy(new Error('The operation was aborted'));
55
+ };
56
+ signal.addEventListener('abort', onAbort, { once: true });
57
+ req.on('close', () => {
58
+ signal.removeEventListener('abort', onAbort);
59
+ });
60
+ }
61
+ req.write(body);
62
+ req.end();
63
+ });
64
+ }
65
+ /**
66
+ * GraphQL adapter that uses node:http/node:https for requests.
67
+ *
68
+ * Handles *.localhost subdomains by rewriting the hostname to "localhost"
69
+ * and injecting the original Host header for server-side subdomain routing.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * import { NodeHttpAdapter } from '@constructive-io/node';
74
+ *
75
+ * const adapter = new NodeHttpAdapter('http://auth.localhost:3000/graphql');
76
+ * const db = createClient({ adapter });
77
+ * ```
78
+ */
79
+ export class NodeHttpAdapter {
80
+ endpoint;
81
+ headers;
82
+ url;
83
+ constructor(endpoint, headers) {
84
+ this.endpoint = endpoint;
85
+ this.headers = headers ?? {};
86
+ this.url = new URL(endpoint);
87
+ }
88
+ async execute(document, variables, options) {
89
+ const requestUrl = new URL(this.url.href);
90
+ const requestHeaders = {
91
+ 'Content-Type': 'application/json',
92
+ Accept: 'application/json',
93
+ ...this.headers,
94
+ ...options?.headers,
95
+ };
96
+ // For *.localhost subdomains, rewrite hostname and inject Host header
97
+ if (isLocalhostSubdomain(requestUrl.hostname)) {
98
+ requestHeaders['Host'] = requestUrl.host;
99
+ requestUrl.hostname = 'localhost';
100
+ }
101
+ const body = JSON.stringify({
102
+ query: document,
103
+ variables: variables ?? {},
104
+ });
105
+ const requestOptions = {
106
+ method: 'POST',
107
+ headers: requestHeaders,
108
+ };
109
+ const response = await makeRequest(requestUrl, requestOptions, body, options?.signal);
110
+ if (response.statusCode < 200 || response.statusCode >= 300) {
111
+ return {
112
+ ok: false,
113
+ data: null,
114
+ errors: [
115
+ {
116
+ message: `HTTP ${response.statusCode}: ${response.statusMessage}`,
117
+ },
118
+ ],
119
+ };
120
+ }
121
+ const json = JSON.parse(response.data);
122
+ if (json.errors && json.errors.length > 0) {
123
+ return {
124
+ ok: false,
125
+ data: null,
126
+ errors: json.errors,
127
+ };
128
+ }
129
+ return {
130
+ ok: true,
131
+ data: json.data,
132
+ errors: undefined,
133
+ };
134
+ }
135
+ setHeaders(headers) {
136
+ this.headers = { ...this.headers, ...headers };
137
+ }
138
+ getEndpoint() {
139
+ return this.endpoint;
140
+ }
141
+ }
package/index.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @constructive-io/node
3
+ *
4
+ * Drop-in replacement for @constructive-io/sdk with Node.js HTTP adapter.
5
+ *
6
+ * For Node.js applications, use this package instead of @constructive-io/sdk.
7
+ * It re-exports everything from the base SDK and adds NodeHttpAdapter,
8
+ * which uses node:http/node:https instead of the Fetch API to handle:
9
+ *
10
+ * 1. *.localhost subdomain DNS resolution (Node can't resolve these natively)
11
+ * 2. Host header preservation (Fetch API silently drops it)
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import { auth, NodeHttpAdapter } from '@constructive-io/node';
16
+ *
17
+ * const adapter = new NodeHttpAdapter(
18
+ * 'http://auth.localhost:3000/graphql',
19
+ * { Authorization: 'Bearer token' },
20
+ * );
21
+ *
22
+ * const db = auth.orm.createClient({ adapter });
23
+ *
24
+ * const users = await db.user.findMany({
25
+ * select: { id: true, name: true },
26
+ * }).execute();
27
+ * ```
28
+ */
29
+ export * from '@constructive-io/sdk';
30
+ export { NodeHttpAdapter } from './node-http-adapter';
31
+ export type { NodeHttpExecuteOptions } from './node-http-adapter';
package/index.js ADDED
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ /**
3
+ * @constructive-io/node
4
+ *
5
+ * Drop-in replacement for @constructive-io/sdk with Node.js HTTP adapter.
6
+ *
7
+ * For Node.js applications, use this package instead of @constructive-io/sdk.
8
+ * It re-exports everything from the base SDK and adds NodeHttpAdapter,
9
+ * which uses node:http/node:https instead of the Fetch API to handle:
10
+ *
11
+ * 1. *.localhost subdomain DNS resolution (Node can't resolve these natively)
12
+ * 2. Host header preservation (Fetch API silently drops it)
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * import { auth, NodeHttpAdapter } from '@constructive-io/node';
17
+ *
18
+ * const adapter = new NodeHttpAdapter(
19
+ * 'http://auth.localhost:3000/graphql',
20
+ * { Authorization: 'Bearer token' },
21
+ * );
22
+ *
23
+ * const db = auth.orm.createClient({ adapter });
24
+ *
25
+ * const users = await db.user.findMany({
26
+ * select: { id: true, name: true },
27
+ * }).execute();
28
+ * ```
29
+ */
30
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ var desc = Object.getOwnPropertyDescriptor(m, k);
33
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
34
+ desc = { enumerable: true, get: function() { return m[k]; } };
35
+ }
36
+ Object.defineProperty(o, k2, desc);
37
+ }) : (function(o, m, k, k2) {
38
+ if (k2 === undefined) k2 = k;
39
+ o[k2] = m[k];
40
+ }));
41
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
42
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
43
+ };
44
+ Object.defineProperty(exports, "__esModule", { value: true });
45
+ exports.NodeHttpAdapter = void 0;
46
+ // Re-export everything from the base SDK
47
+ __exportStar(require("@constructive-io/sdk"), exports);
48
+ // Export the Node.js HTTP adapter
49
+ var node_http_adapter_1 = require("./node-http-adapter");
50
+ Object.defineProperty(exports, "NodeHttpAdapter", { enumerable: true, get: function () { return node_http_adapter_1.NodeHttpAdapter; } });
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Node HTTP Adapter for Node.js applications
3
+ *
4
+ * Implements the GraphQLAdapter interface using node:http / node:https
5
+ * instead of the Fetch API. This solves two Node.js limitations:
6
+ *
7
+ * 1. DNS: Node.js cannot resolve *.localhost subdomains (ENOTFOUND).
8
+ * Browsers handle this automatically, but Node requires manual resolution.
9
+ *
10
+ * 2. Host header: The Fetch API treats "Host" as a forbidden request header
11
+ * and silently drops it. The Constructive GraphQL server uses Host-header
12
+ * subdomain routing (enableServicesApi), so this header must be preserved.
13
+ *
14
+ * By using node:http.request directly, both issues are bypassed cleanly
15
+ * without any global patching.
16
+ */
17
+ import type { GraphQLAdapter, QueryResult } from '@constructive-io/graphql-types';
18
+ /**
19
+ * Options for individual execute calls.
20
+ * Allows per-request header overrides and request cancellation.
21
+ */
22
+ export interface NodeHttpExecuteOptions {
23
+ /** Additional headers to include in this request only */
24
+ headers?: Record<string, string>;
25
+ /** AbortSignal for request cancellation */
26
+ signal?: AbortSignal;
27
+ }
28
+ /**
29
+ * GraphQL adapter that uses node:http/node:https for requests.
30
+ *
31
+ * Handles *.localhost subdomains by rewriting the hostname to "localhost"
32
+ * and injecting the original Host header for server-side subdomain routing.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * import { NodeHttpAdapter } from '@constructive-io/node';
37
+ *
38
+ * const adapter = new NodeHttpAdapter('http://auth.localhost:3000/graphql');
39
+ * const db = createClient({ adapter });
40
+ * ```
41
+ */
42
+ export declare class NodeHttpAdapter implements GraphQLAdapter {
43
+ private endpoint;
44
+ private headers;
45
+ private url;
46
+ constructor(endpoint: string, headers?: Record<string, string>);
47
+ execute<T>(document: string, variables?: Record<string, unknown>, options?: NodeHttpExecuteOptions): Promise<QueryResult<T>>;
48
+ setHeaders(headers: Record<string, string>): void;
49
+ getEndpoint(): string;
50
+ }
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ /**
3
+ * Node HTTP Adapter for Node.js applications
4
+ *
5
+ * Implements the GraphQLAdapter interface using node:http / node:https
6
+ * instead of the Fetch API. This solves two Node.js limitations:
7
+ *
8
+ * 1. DNS: Node.js cannot resolve *.localhost subdomains (ENOTFOUND).
9
+ * Browsers handle this automatically, but Node requires manual resolution.
10
+ *
11
+ * 2. Host header: The Fetch API treats "Host" as a forbidden request header
12
+ * and silently drops it. The Constructive GraphQL server uses Host-header
13
+ * subdomain routing (enableServicesApi), so this header must be preserved.
14
+ *
15
+ * By using node:http.request directly, both issues are bypassed cleanly
16
+ * without any global patching.
17
+ */
18
+ var __importDefault = (this && this.__importDefault) || function (mod) {
19
+ return (mod && mod.__esModule) ? mod : { "default": mod };
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.NodeHttpAdapter = void 0;
23
+ const node_http_1 = __importDefault(require("node:http"));
24
+ const node_https_1 = __importDefault(require("node:https"));
25
+ /**
26
+ * Check if a hostname is a localhost subdomain that needs special handling.
27
+ * Returns true for *.localhost (e.g. auth.localhost) but not bare "localhost".
28
+ */
29
+ function isLocalhostSubdomain(hostname) {
30
+ return hostname.endsWith('.localhost') && hostname !== 'localhost';
31
+ }
32
+ /**
33
+ * Make an HTTP/HTTPS request using native Node modules.
34
+ * Supports optional AbortSignal for request cancellation.
35
+ */
36
+ function makeRequest(url, options, body, signal) {
37
+ return new Promise((resolve, reject) => {
38
+ if (signal?.aborted) {
39
+ reject(new Error('The operation was aborted'));
40
+ return;
41
+ }
42
+ const protocol = url.protocol === 'https:' ? node_https_1.default : node_http_1.default;
43
+ const req = protocol.request(url, options, (res) => {
44
+ let data = '';
45
+ res.setEncoding('utf8');
46
+ res.on('data', (chunk) => {
47
+ data += chunk;
48
+ });
49
+ res.on('end', () => {
50
+ resolve({
51
+ statusCode: res.statusCode || 0,
52
+ statusMessage: res.statusMessage || '',
53
+ data,
54
+ });
55
+ });
56
+ });
57
+ req.on('error', reject);
58
+ if (signal) {
59
+ const onAbort = () => {
60
+ req.destroy(new Error('The operation was aborted'));
61
+ };
62
+ signal.addEventListener('abort', onAbort, { once: true });
63
+ req.on('close', () => {
64
+ signal.removeEventListener('abort', onAbort);
65
+ });
66
+ }
67
+ req.write(body);
68
+ req.end();
69
+ });
70
+ }
71
+ /**
72
+ * GraphQL adapter that uses node:http/node:https for requests.
73
+ *
74
+ * Handles *.localhost subdomains by rewriting the hostname to "localhost"
75
+ * and injecting the original Host header for server-side subdomain routing.
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * import { NodeHttpAdapter } from '@constructive-io/node';
80
+ *
81
+ * const adapter = new NodeHttpAdapter('http://auth.localhost:3000/graphql');
82
+ * const db = createClient({ adapter });
83
+ * ```
84
+ */
85
+ class NodeHttpAdapter {
86
+ endpoint;
87
+ headers;
88
+ url;
89
+ constructor(endpoint, headers) {
90
+ this.endpoint = endpoint;
91
+ this.headers = headers ?? {};
92
+ this.url = new URL(endpoint);
93
+ }
94
+ async execute(document, variables, options) {
95
+ const requestUrl = new URL(this.url.href);
96
+ const requestHeaders = {
97
+ 'Content-Type': 'application/json',
98
+ Accept: 'application/json',
99
+ ...this.headers,
100
+ ...options?.headers,
101
+ };
102
+ // For *.localhost subdomains, rewrite hostname and inject Host header
103
+ if (isLocalhostSubdomain(requestUrl.hostname)) {
104
+ requestHeaders['Host'] = requestUrl.host;
105
+ requestUrl.hostname = 'localhost';
106
+ }
107
+ const body = JSON.stringify({
108
+ query: document,
109
+ variables: variables ?? {},
110
+ });
111
+ const requestOptions = {
112
+ method: 'POST',
113
+ headers: requestHeaders,
114
+ };
115
+ const response = await makeRequest(requestUrl, requestOptions, body, options?.signal);
116
+ if (response.statusCode < 200 || response.statusCode >= 300) {
117
+ return {
118
+ ok: false,
119
+ data: null,
120
+ errors: [
121
+ {
122
+ message: `HTTP ${response.statusCode}: ${response.statusMessage}`,
123
+ },
124
+ ],
125
+ };
126
+ }
127
+ const json = JSON.parse(response.data);
128
+ if (json.errors && json.errors.length > 0) {
129
+ return {
130
+ ok: false,
131
+ data: null,
132
+ errors: json.errors,
133
+ };
134
+ }
135
+ return {
136
+ ok: true,
137
+ data: json.data,
138
+ errors: undefined,
139
+ };
140
+ }
141
+ setHeaders(headers) {
142
+ this.headers = { ...this.headers, ...headers };
143
+ }
144
+ getEndpoint() {
145
+ return this.endpoint;
146
+ }
147
+ }
148
+ exports.NodeHttpAdapter = NodeHttpAdapter;
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@constructive-io/node",
3
+ "version": "0.1.0",
4
+ "author": "Constructive <developers@constructive.io>",
5
+ "description": "Constructive SDK for Node.js - Drop-in replacement for @constructive-io/sdk with Node.js HTTP adapter",
6
+ "main": "index.js",
7
+ "module": "esm/index.js",
8
+ "types": "index.d.ts",
9
+ "homepage": "https://github.com/constructive-io/constructive",
10
+ "license": "MIT",
11
+ "publishConfig": {
12
+ "access": "public",
13
+ "directory": "dist"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/constructive-io/constructive"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/constructive-io/constructive/issues"
21
+ },
22
+ "scripts": {
23
+ "clean": "makage clean",
24
+ "prepack": "npm run build",
25
+ "build": "makage build",
26
+ "build:dev": "makage build --dev",
27
+ "lint": "eslint . --fix",
28
+ "test": "jest --passWithNoTests",
29
+ "test:watch": "jest --watch"
30
+ },
31
+ "keywords": [
32
+ "graphql",
33
+ "sdk",
34
+ "orm",
35
+ "node",
36
+ "constructive",
37
+ "postgraphile",
38
+ "server-side"
39
+ ],
40
+ "dependencies": {
41
+ "@constructive-io/graphql-types": "^3.1.1",
42
+ "@constructive-io/sdk": "^0.2.1"
43
+ },
44
+ "devDependencies": {
45
+ "@types/node": "^20.12.7",
46
+ "makage": "^0.1.12",
47
+ "typescript": "^5.9.3"
48
+ },
49
+ "gitHead": "eebb09e7cb0085b0e9856d34803e3142f9f73a15"
50
+ }