@n-dream/builders-sdk 0.3.1 → 1.0.0-next.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/package.json CHANGED
@@ -1,49 +1,29 @@
1
1
  {
2
2
  "name": "@n-dream/builders-sdk",
3
- "version": "0.3.1",
4
- "description": "Builders platform helpers for app code: signed-in user, database, AI, file storage, notifications.",
3
+ "version": "1.0.0-next.0",
4
+ "description": "Builders SDK for app code: app information now, platform services later.",
5
5
  "license": "UNLICENSED",
6
- "homepage": "https://builders.n-dream.com",
7
- "keywords": [
8
- "builders",
9
- "nextjs"
10
- ],
11
6
  "type": "module",
12
7
  "main": "./dist/index.js",
13
8
  "types": "./dist/index.d.ts",
14
9
  "exports": {
15
10
  ".": {
16
11
  "types": "./dist/index.d.ts",
17
- "default": "./dist/index.js"
18
- },
19
- "./next": {
20
- "types": "./dist/next.d.ts",
21
- "default": "./dist/next.js"
22
- },
23
- "./package.json": "./package.json"
12
+ "import": "./dist/index.js"
13
+ }
24
14
  },
25
15
  "files": [
26
16
  "dist",
27
17
  "README.md"
28
18
  ],
29
- "engines": {
30
- "node": ">=22"
31
- },
32
- "dependencies": {
33
- "@google-cloud/firestore": "^9.4.0",
34
- "@google-cloud/storage": "^8.4.0",
35
- "jose": "^6.0.0",
36
- "zod": "^4.6.7"
37
- },
38
- "peerDependencies": {
39
- "next": ">=15"
40
- },
41
- "peerDependenciesMeta": {
42
- "next": {
43
- "optional": true
44
- }
45
- },
19
+ "sideEffects": false,
46
20
  "publishConfig": {
47
- "access": "public"
21
+ "access": "public",
22
+ "tag": "next"
23
+ },
24
+ "scripts": {
25
+ "build": "tsc -p tsconfig.build.json",
26
+ "test": "vitest run --minWorkers=1 --maxWorkers=2",
27
+ "typecheck": "tsc --noEmit"
48
28
  }
49
- }
29
+ }
@@ -1,136 +0,0 @@
1
- // ../core/src/identity-token.ts
2
- var IDENTITY_HEADER = "x-builders-identity";
3
- var DEV_USER_HEADER = "x-builders-dev-user";
4
- var SIGN_OUT_PATH = "/_builders/sign-out";
5
- var IDENTITY_ALG = "ES256";
6
- var IDENTITY_TOKEN_TTL_S = 60;
7
- var IDENTITY_CLOCK_TOLERANCE_S = 30;
8
- var IDENTITY_ENV = {
9
- audience: "BUILDERS_IDENTITY_AUDIENCE",
10
- issuer: "BUILDERS_IDENTITY_ISSUER",
11
- jwksUrl: "BUILDERS_IDENTITY_JWKS_URL",
12
- // Inline JWKS (JSON), for tests and BYOC setups without the bucket.
13
- jwks: "BUILDERS_IDENTITY_JWKS"
14
- };
15
-
16
- // src/errors.ts
17
- var BuildersError = class extends Error {
18
- constructor(message, status = 500) {
19
- super(message);
20
- this.status = status;
21
- this.name = "BuildersError";
22
- }
23
- status;
24
- };
25
- var NotSignedInError = class extends BuildersError {
26
- constructor(message) {
27
- super(message, 401);
28
- this.name = "NotSignedInError";
29
- }
30
- };
31
- var NotFoundError = class extends BuildersError {
32
- constructor(message) {
33
- super(message, 404);
34
- this.name = "NotFoundError";
35
- }
36
- };
37
- var ADDON_LABELS = {
38
- ai: "AI",
39
- storage: "file storage",
40
- cron: "scheduled jobs",
41
- notify: "notifications",
42
- internet: "internet access"
43
- };
44
- var AddOnNotEnabledError = class extends BuildersError {
45
- constructor(addOn) {
46
- super(
47
- `This add-on (${ADDON_LABELS[addOn]}) is not switched on for your app. Ask for it in the Builders portal.`,
48
- 503
49
- );
50
- this.addOn = addOn;
51
- this.name = "AddOnNotEnabledError";
52
- }
53
- addOn;
54
- };
55
-
56
- // src/user.ts
57
- import { createLocalJWKSet, createRemoteJWKSet, jwtVerify } from "jose";
58
- var remoteKeys = /* @__PURE__ */ new Map();
59
- function keysFrom(env) {
60
- const inline = env[IDENTITY_ENV.jwks];
61
- if (inline) return createLocalJWKSet(JSON.parse(inline));
62
- const url = env[IDENTITY_ENV.jwksUrl];
63
- if (!url) throw new BuildersError(`${IDENTITY_ENV.jwksUrl} is not set, so the signed-in user cannot be checked. The platform sets it on deploy.`);
64
- let k = remoteKeys.get(url);
65
- if (!k) {
66
- k = createRemoteJWKSet(new URL(url), { cooldownDuration: 3e4, cacheMaxAge: 10 * 6e4 });
67
- remoteKeys.set(url, k);
68
- }
69
- return k;
70
- }
71
- async function getUser(headers, opts = {}) {
72
- const env = opts.env ?? process.env;
73
- const token = readHeader(headers, IDENTITY_HEADER);
74
- if (token) {
75
- const audience = opts.audience ?? env[IDENTITY_ENV.audience];
76
- const issuer = opts.issuer ?? env[IDENTITY_ENV.issuer];
77
- if (!audience || !issuer) {
78
- throw new BuildersError(
79
- `${IDENTITY_ENV.audience} and ${IDENTITY_ENV.issuer} must be set, so the signed-in user can be checked. The platform sets them on deploy.`
80
- );
81
- }
82
- let payload;
83
- try {
84
- ({ payload } = await jwtVerify(token, opts.keys ?? keysFrom(env), {
85
- issuer,
86
- audience,
87
- algorithms: [IDENTITY_ALG],
88
- requiredClaims: ["exp", "iat", "email"],
89
- maxTokenAge: IDENTITY_TOKEN_TTL_S + IDENTITY_CLOCK_TOLERANCE_S,
90
- clockTolerance: IDENTITY_CLOCK_TOLERANCE_S
91
- }));
92
- } catch (e) {
93
- if (e instanceof BuildersError) throw e;
94
- throw new NotSignedInError(`The sign-in token is not valid: ${e.message}`);
95
- }
96
- if (typeof payload.email !== "string" || !payload.email || payload.email_verified !== true) {
97
- throw new NotSignedInError("The sign-in token has no verified email address.");
98
- }
99
- const email = payload.email.toLowerCase();
100
- return { email, name: typeof payload.name === "string" && payload.name ? payload.name : nameFromEmail(email) };
101
- }
102
- const viewer = readHeader(headers, DEV_USER_HEADER)?.trim();
103
- const devUser = (viewer && viewer.length <= 254 && /^[^\s@]+@[^\s@]+$/.test(viewer) ? viewer : void 0) || env.BUILDERS_DEV_USER;
104
- if (env.NODE_ENV !== "production" && devUser) {
105
- return { email: devUser.toLowerCase(), name: nameFromEmail(devUser) };
106
- }
107
- throw new NotSignedInError(
108
- env.NODE_ENV === "production" ? "No signed-in user. Open the app through its Builders link, which signs you in." : "No signed-in user. For local development set BUILDERS_DEV_USER (the dev script does this)."
109
- );
110
- }
111
- function nameFromEmail(email) {
112
- const local = email.split("@")[0] ?? "";
113
- const parts = local.split(/[._-]+/).filter((p) => /^[a-z]+$/i.test(p));
114
- if (parts.length === 0) return void 0;
115
- return parts.map((p) => p[0].toUpperCase() + p.slice(1).toLowerCase()).join(" ");
116
- }
117
- function readHeader(headers, name) {
118
- if (typeof headers.get === "function") {
119
- return headers.get(name) ?? void 0;
120
- }
121
- const rec = headers;
122
- const key = Object.keys(rec).find((k) => k.toLowerCase() === name);
123
- const v = key === void 0 ? void 0 : rec[key];
124
- return (Array.isArray(v) ? v[0] : v) || void 0;
125
- }
126
-
127
- export {
128
- IDENTITY_HEADER,
129
- SIGN_OUT_PATH,
130
- BuildersError,
131
- NotSignedInError,
132
- NotFoundError,
133
- AddOnNotEnabledError,
134
- getUser,
135
- nameFromEmail
136
- };
@@ -1,20 +0,0 @@
1
- import { JWTVerifyGetKey } from 'jose';
2
-
3
- interface User {
4
- email: string;
5
- name?: string;
6
- }
7
- type HeaderSource = {
8
- get(name: string): string | null;
9
- } | Record<string, string | string[] | undefined>;
10
- interface GetUserOptions {
11
- audience?: string;
12
- issuer?: string;
13
- keys?: JWTVerifyGetKey;
14
- env?: Record<string, string | undefined>;
15
- }
16
- declare function getUser(headers: HeaderSource, opts?: GetUserOptions): Promise<User>;
17
- declare function nameFromEmail(email: string): string | undefined;
18
-
19
- export { getUser as g, nameFromEmail as n };
20
- export type { GetUserOptions as G, HeaderSource as H, User as U };
package/dist/next.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { User } from './index.js';
2
- import 'jose';
3
-
4
- declare function currentUser(): Promise<User>;
5
-
6
- export { currentUser };
package/dist/next.js DELETED
@@ -1,12 +0,0 @@
1
- import {
2
- getUser
3
- } from "./chunks/chunk-RUOQMEEZ.js";
4
-
5
- // src/next.ts
6
- import { headers } from "next/headers.js";
7
- async function currentUser() {
8
- return getUser(await headers());
9
- }
10
- export {
11
- currentUser
12
- };