@constructive-io/url-domains 2.3.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.
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,145 @@
1
+ # @constructive-io/url-domains
2
+
3
+ <p align="center" width="100%">
4
+ <img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
5
+ </p>
6
+
7
+ <p align="center" width="100%">
8
+ <a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
9
+ <img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
10
+ </a>
11
+ <a href="https://github.com/constructive-io/constructive/blob/main/LICENSE"><img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/></a>
12
+ <a href="https://www.npmjs.com/package/@constructive-io/url-domains"><img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=packages%2Furl-domains%2Fpackage.json"/></a>
13
+ </p>
14
+
15
+ for use with express
16
+
17
+ ## why?
18
+
19
+ Because, express requires that you do things like this when you need `req.subdomains` on `localhost`:
20
+
21
+ ```js
22
+ if (env.SERVER_HOST === 'localhost') {
23
+ app.set('subdomain offset', 1);
24
+ }
25
+ ```
26
+
27
+ and I believe the developer experience shouldn't include any off-by-one errors imo if you forgot this or your environments are changing and/or dynamic.
28
+
29
+ So why not just make a `subdomains` object you can trust no matter what?
30
+
31
+ ## install
32
+
33
+ ```sh
34
+ pnpm add @constructive-io/url-domains
35
+ ```
36
+
37
+ ### middleware
38
+
39
+ ```js
40
+ import {
41
+ middleware as parseDomains
42
+ } from '@constructive-io/url-domains';
43
+
44
+
45
+ app.use(parseDomains());
46
+
47
+ app.use(async (req, res, next) => {
48
+ // have fun!
49
+ await fn(req.urlDomains.domain, req.urlDomains.subdomains);
50
+ });
51
+ ```
52
+
53
+ ### manual usage
54
+
55
+ ```js
56
+ import {
57
+ parseReq
58
+ } from '@constructive-io/url-domains';
59
+
60
+ app.use(async (req, res, next) => {
61
+ const {
62
+ domain,
63
+ subdomains
64
+ } = parseReq(req);
65
+
66
+ // cheers!!
67
+ await fn(domain, subdomains);
68
+ });
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Education and Tutorials
74
+
75
+ 1. ๐Ÿš€ [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
76
+ Get started with modular databases in minutes. Install prerequisites and deploy your first module.
77
+
78
+ 2. ๐Ÿ“ฆ [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
79
+ Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
80
+
81
+ 3. โœ๏ธ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
82
+ Master the workflow for adding, organizing, and managing database changes with pgpm.
83
+
84
+ 4. ๐Ÿงช [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
85
+ Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
86
+
87
+ 5. โšก [Supabase Testing](https://constructive.io/learn/supabase)
88
+ Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
89
+
90
+ 6. ๐Ÿ’ง [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
91
+ Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
92
+
93
+ 7. ๐Ÿ”ง [Troubleshooting](https://constructive.io/learn/troubleshooting)
94
+ Common issues and solutions for pgpm, PostgreSQL, and testing.
95
+
96
+ ## Related Constructive Tooling
97
+
98
+ ### ๐Ÿงช Testing
99
+
100
+ * [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.
101
+ * [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.
102
+ * [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.
103
+ * [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.
104
+
105
+ ### ๐Ÿง  Parsing & AST
106
+
107
+ * [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **๐Ÿ”„ SQL conversion engine** that interprets and converts PostgreSQL syntax.
108
+ * [libpg-query-node](https://www.npmjs.com/package/libpg-query): **๐ŸŒ‰ Node.js bindings** for `libpg_query`, converting SQL into parse trees.
109
+ * [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.
110
+ * [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **๐Ÿท๏ธ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
111
+ * [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **๐Ÿ“ Type definitions** for PostgreSQL AST nodes in TypeScript.
112
+ * [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **๐Ÿ› ๏ธ AST utilities** for constructing and transforming PostgreSQL syntax trees.
113
+ * [pg-ast](https://www.npmjs.com/package/pg-ast): **๐Ÿ” Low-level AST tools** and transformations for Postgres query structures.
114
+
115
+ ### ๐Ÿš€ API & Dev Tools
116
+
117
+ * [@constructive-io/graphql-server](https://github.com/constructive-io/constructive/tree/main/graphql/server): **โšก Express-based API server** powered by PostGraphile to expose a secure, scalable GraphQL API over your Postgres database.
118
+ * [@constructive-io/graphql-explorer](https://github.com/constructive-io/constructive/tree/main/graphql/explorer): **๐Ÿ”Ž Visual API explorer** with GraphiQL for browsing across all databases and schemasโ€”useful for debugging, documentation, and API prototyping.
119
+
120
+ ### ๐Ÿ” Streaming & Uploads
121
+
122
+ * [etag-hash](https://github.com/constructive-io/constructive/tree/main/streaming/etag-hash): **๐Ÿท๏ธ S3-compatible ETags** created by streaming and hashing file uploads in chunks.
123
+ * [etag-stream](https://github.com/constructive-io/constructive/tree/main/streaming/etag-stream): **๐Ÿ”„ ETag computation** via Node stream transformer during upload or transfer.
124
+ * [uuid-hash](https://github.com/constructive-io/constructive/tree/main/streaming/uuid-hash): **๐Ÿ†” Deterministic UUIDs** generated from hashed content, great for deduplication and asset referencing.
125
+ * [uuid-stream](https://github.com/constructive-io/constructive/tree/main/streaming/uuid-stream): **๐ŸŒŠ Streaming UUID generation** based on piped file contentโ€”ideal for upload pipelines.
126
+ * [@constructive-io/s3-streamer](https://github.com/constructive-io/constructive/tree/main/streaming/s3-streamer): **๐Ÿ“ค Direct S3 streaming** for large files with support for metadata injection and content validation.
127
+ * [@constructive-io/upload-names](https://github.com/constructive-io/constructive/tree/main/streaming/upload-names): **๐Ÿ“‚ Collision-resistant filenames** utility for structured and unique file names for uploads.
128
+
129
+ ### ๐Ÿงฐ CLI & Codegen
130
+
131
+ * [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.
132
+ * [@constructive-io/cli](https://github.com/constructive-io/constructive/tree/main/packages/cli): **๐Ÿ–ฅ๏ธ Command-line toolkit** for managing Constructive projectsโ€”supports database scaffolding, migrations, seeding, code generation, and automation.
133
+ * [@constructive-io/graphql-codegen](https://github.com/constructive-io/constructive/tree/main/graphql/codegen): **โœจ GraphQL code generation** (types, operations, SDK) from schema/endpoint introspection.
134
+ * [@constructive-io/query-builder](https://github.com/constructive-io/constructive/tree/main/packages/query-builder): **๐Ÿ—๏ธ SQL constructor** providing a robust TypeScript-based query builder for dynamic generation of `SELECT`, `INSERT`, `UPDATE`, `DELETE`, and stored procedure callsโ€”supports advanced SQL features like `JOIN`, `GROUP BY`, and schema-qualified queries.
135
+ * [@constructive-io/graphql-query](https://github.com/constructive-io/constructive/tree/main/graphql/query): **๐Ÿงฉ Fluent GraphQL builder** for PostGraphile schemas. โšก Schema-aware via introspection, ๐Ÿงฉ composable and ergonomic for building deeply nested queries.
136
+
137
+ ## Credits
138
+
139
+ **๐Ÿ›  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).**
140
+
141
+ ## Disclaimer
142
+
143
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
144
+
145
+ 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.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from './middleware';
2
+ export * from './requests';
3
+ export * from './urls';
@@ -0,0 +1,7 @@
1
+ import { parseReq } from './requests';
2
+ export const middleware = () => {
3
+ return (req, res, next) => {
4
+ req.urlDomains = parseReq(req);
5
+ next();
6
+ };
7
+ };
@@ -0,0 +1,5 @@
1
+ import { parseUrl } from './urls';
2
+ export const parseReq = (req) => {
3
+ const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
4
+ return parseUrl(fullUrl);
5
+ };
package/esm/urls.js ADDED
@@ -0,0 +1,33 @@
1
+ const ipRegExp = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
2
+ export const parseUrl = (input) => {
3
+ const url = typeof input === 'string' ? new URL(input) : input;
4
+ let hostname = url.hostname.replace(/^www\./, '');
5
+ const parts = hostname.split('.');
6
+ let domain = null;
7
+ let subdomains = null;
8
+ if (hostname === 'localhost') {
9
+ domain = 'localhost';
10
+ subdomains = [];
11
+ }
12
+ else if (hostname.endsWith('.localhost')) {
13
+ domain = 'localhost';
14
+ subdomains = parts.slice(0, -1); // everything before 'localhost'
15
+ }
16
+ else if (ipRegExp.test(hostname)) {
17
+ domain = hostname;
18
+ subdomains = [];
19
+ }
20
+ else if (parts.length >= 2) {
21
+ domain = parts.slice(-2).join('.');
22
+ subdomains = parts.slice(0, -2);
23
+ }
24
+ else {
25
+ domain = hostname;
26
+ subdomains = [];
27
+ }
28
+ return {
29
+ hostname: url.hostname,
30
+ domain,
31
+ subdomains
32
+ };
33
+ };
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './middleware';
2
+ export * from './requests';
3
+ export * from './urls';
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./middleware"), exports);
18
+ __exportStar(require("./requests"), exports);
19
+ __exportStar(require("./urls"), exports);
@@ -0,0 +1,13 @@
1
+ import { NextFunction, Request, Response } from 'express';
2
+ interface UrlDomains {
3
+ [key: string]: string | string[];
4
+ }
5
+ declare global {
6
+ namespace Express {
7
+ interface Request {
8
+ urlDomains?: UrlDomains;
9
+ }
10
+ }
11
+ }
12
+ export declare const middleware: () => (req: Request, res: Response, next: NextFunction) => void;
13
+ export {};
package/middleware.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.middleware = void 0;
4
+ const requests_1 = require("./requests");
5
+ const middleware = () => {
6
+ return (req, res, next) => {
7
+ req.urlDomains = (0, requests_1.parseReq)(req);
8
+ next();
9
+ };
10
+ };
11
+ exports.middleware = middleware;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@constructive-io/url-domains",
3
+ "version": "2.3.4",
4
+ "author": "Constructive <developers@constructive.io>",
5
+ "description": "url domains",
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
+ "dependencies": {
32
+ "express": "^5.1.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/express": "^5.0.6",
36
+ "makage": "^0.1.8"
37
+ },
38
+ "keywords": [],
39
+ "gitHead": "22cfe32e994e26a6490e04e28bab26d1e7e6345c"
40
+ }
package/requests.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Request } from 'express';
2
+ export declare const parseReq: (req: Request) => {
3
+ hostname: string;
4
+ domain: string;
5
+ subdomains: string[];
6
+ };
package/requests.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseReq = void 0;
4
+ const urls_1 = require("./urls");
5
+ const parseReq = (req) => {
6
+ const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`;
7
+ return (0, urls_1.parseUrl)(fullUrl);
8
+ };
9
+ exports.parseReq = parseReq;
package/urls.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ export declare const parseUrl: (input: string | URL) => {
2
+ hostname: string;
3
+ domain: string;
4
+ subdomains: string[];
5
+ };
package/urls.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseUrl = void 0;
4
+ const ipRegExp = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
5
+ const parseUrl = (input) => {
6
+ const url = typeof input === 'string' ? new URL(input) : input;
7
+ let hostname = url.hostname.replace(/^www\./, '');
8
+ const parts = hostname.split('.');
9
+ let domain = null;
10
+ let subdomains = null;
11
+ if (hostname === 'localhost') {
12
+ domain = 'localhost';
13
+ subdomains = [];
14
+ }
15
+ else if (hostname.endsWith('.localhost')) {
16
+ domain = 'localhost';
17
+ subdomains = parts.slice(0, -1); // everything before 'localhost'
18
+ }
19
+ else if (ipRegExp.test(hostname)) {
20
+ domain = hostname;
21
+ subdomains = [];
22
+ }
23
+ else if (parts.length >= 2) {
24
+ domain = parts.slice(-2).join('.');
25
+ subdomains = parts.slice(0, -2);
26
+ }
27
+ else {
28
+ domain = hostname;
29
+ subdomains = [];
30
+ }
31
+ return {
32
+ hostname: url.hostname,
33
+ domain,
34
+ subdomains
35
+ };
36
+ };
37
+ exports.parseUrl = parseUrl;