create-heresay 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 +21 -0
- package/README.md +31 -0
- package/bin/create-heresay.mjs +9 -0
- package/package.json +38 -0
- package/release/VERSION +1 -0
- package/release/firebase.json +111 -0
- package/release/firestore.indexes.json +11 -0
- package/release/firestore.rules +10 -0
- package/release/functions/lib/core/handler.js +366 -0
- package/release/functions/lib/core/store.js +66 -0
- package/release/functions/lib/core/types.js +23 -0
- package/release/functions/lib/core/version.js +3 -0
- package/release/functions/lib/index.js +7 -0
- package/release/functions/lib/providers/firebase/admin.js +13 -0
- package/release/functions/lib/providers/firebase/api.js +55 -0
- package/release/functions/lib/providers/firebase/firestore-store.js +106 -0
- package/release/functions/package-lock.json +2993 -0
- package/release/functions/package.json +23 -0
- package/release/public/app/index.html +57 -0
- package/release/public/app-icon.svg +1 -0
- package/release/public/apple-touch-icon.png +0 -0
- package/release/public/dashboard.css +152 -0
- package/release/public/dashboard.js +580 -0
- package/release/public/demo.html +30 -0
- package/release/public/docs.html +152 -0
- package/release/public/favicon.svg +1 -0
- package/release/public/icon-192.png +0 -0
- package/release/public/index.html +166 -0
- package/release/public/logo-white.svg +1 -0
- package/release/public/logo.svg +1 -0
- package/release/public/mark.svg +1 -0
- package/release/public/sdk.js +380 -0
- package/release/public/site.css +150 -0
- package/src/cli.mjs +382 -0
- package/src/firebase.mjs +61 -0
- package/src/google.mjs +106 -0
- package/src/state.mjs +43 -0
- package/src/steps.mjs +306 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const REPORT_TYPES = ['broken', 'confusing', 'improvement', 'idea'];
|
|
2
|
+
export const isReportType = (v) => typeof v === 'string' && REPORT_TYPES.includes(v);
|
|
3
|
+
/**
|
|
4
|
+
* Most urgent first. `confusing` above `improvement` on purpose: a feature nobody can operate
|
|
5
|
+
* is closer to broken than to imperfect.
|
|
6
|
+
*/
|
|
7
|
+
export const REPORT_ORDER = {
|
|
8
|
+
broken: 0, confusing: 1, improvement: 2, idea: 3,
|
|
9
|
+
};
|
|
10
|
+
export const PLATFORMS = ['web', 'ios', 'macos', 'android', 'react-native', 'flutter'];
|
|
11
|
+
export const isPlatform = (v) => typeof v === 'string' && PLATFORMS.includes(v);
|
|
12
|
+
export const toReporterView = (r) => ({
|
|
13
|
+
id: r.id, type: r.type, text: r.text, status: r.status,
|
|
14
|
+
decline_reason: r.decline_reason, created_at: r.created_at, updated_at: r.updated_at,
|
|
15
|
+
});
|
|
16
|
+
export const LIMITS = {
|
|
17
|
+
textMax: 2000,
|
|
18
|
+
contextFieldMax: 200,
|
|
19
|
+
deviceIdMin: 16,
|
|
20
|
+
deviceIdMax: 64,
|
|
21
|
+
reasonMax: 1000,
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deploy entry point. Heresay's logic lives in core/ and knows nothing about where it runs;
|
|
3
|
+
* providers/ holds one adapter per backend. Today that is Firebase; Catalyst would be a second
|
|
4
|
+
* adapter, chosen by `create-heresay`. core/ must never import from providers/.
|
|
5
|
+
*/
|
|
6
|
+
export { api } from './providers/firebase/api.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ONE firebase-admin instance. Import it from here and nowhere else: two copies of
|
|
3
|
+
* firebase-admin in one process break FieldValue instanceof checks, and that surfaces as
|
|
4
|
+
* unrelated-looking 502s.
|
|
5
|
+
*/
|
|
6
|
+
import { initializeApp, getApps } from 'firebase-admin/app';
|
|
7
|
+
import { getAuth } from 'firebase-admin/auth';
|
|
8
|
+
import { getFirestore } from 'firebase-admin/firestore';
|
|
9
|
+
if (getApps().length === 0)
|
|
10
|
+
initializeApp();
|
|
11
|
+
export const db = getFirestore();
|
|
12
|
+
export const auth = getAuth();
|
|
13
|
+
//# sourceMappingURL=admin.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Firebase adapter: Cloud Functions in front of the core handler, Firestore behind it.
|
|
3
|
+
* A Catalyst adapter would be a sibling of this folder with the same shape.
|
|
4
|
+
*/
|
|
5
|
+
import { onRequest } from 'firebase-functions/v2/https';
|
|
6
|
+
import { logger } from 'firebase-functions';
|
|
7
|
+
import { auth, db } from './admin.js';
|
|
8
|
+
import { FirestoreStore } from './firestore-store.js';
|
|
9
|
+
import { handle } from '../../core/handler.js';
|
|
10
|
+
import { VERSION } from '../../core/version.js';
|
|
11
|
+
const deps = {
|
|
12
|
+
store: new FirestoreStore(db),
|
|
13
|
+
async verifyIdToken(token) {
|
|
14
|
+
try {
|
|
15
|
+
const t = await auth.verifyIdToken(token);
|
|
16
|
+
return { uid: t.uid, email: t.email ?? null, emailVerified: t.email_verified === true };
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
now: () => Date.now(),
|
|
23
|
+
log: (event, fields) => logger.info(event, { event, ...fields }),
|
|
24
|
+
version: VERSION,
|
|
25
|
+
selfOrigins: selfOrigins(),
|
|
26
|
+
};
|
|
27
|
+
function selfOrigins() {
|
|
28
|
+
const id = process.env.GCLOUD_PROJECT;
|
|
29
|
+
if (process.env.FUNCTIONS_EMULATOR === 'true')
|
|
30
|
+
return ['http://127.0.0.1:5055', 'http://localhost:5055'];
|
|
31
|
+
return id ? [`https://${id}.web.app`, `https://${id}.firebaseapp.com`] : [];
|
|
32
|
+
}
|
|
33
|
+
/** Hosting rewrites /v1/** here, so the SDK, the API and the dashboard share one origin. */
|
|
34
|
+
// create-heresay writes HERESAY_REGION to functions/.env; it is read when the deploy loads this file.
|
|
35
|
+
export const api = onRequest({ region: process.env.HERESAY_REGION || 'us-central1', maxInstances: 10 }, async (req, res) => {
|
|
36
|
+
const headers = {};
|
|
37
|
+
for (const [k, v] of Object.entries(req.headers))
|
|
38
|
+
headers[k] = Array.isArray(v) ? v[0] : v;
|
|
39
|
+
const query = {};
|
|
40
|
+
for (const [k, v] of Object.entries(req.query))
|
|
41
|
+
if (typeof v === 'string')
|
|
42
|
+
query[k] = v;
|
|
43
|
+
const out = await handle({
|
|
44
|
+
method: req.method, path: req.path, query, headers, ip: req.ip ?? '',
|
|
45
|
+
body: req.body,
|
|
46
|
+
}, deps);
|
|
47
|
+
for (const [k, v] of Object.entries(out.headers ?? {}))
|
|
48
|
+
res.setHeader(k, v);
|
|
49
|
+
if (out.status === 204) {
|
|
50
|
+
res.status(204).end();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
res.status(out.status).json(out.body);
|
|
54
|
+
});
|
|
55
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* meta/instance the team: members and roles
|
|
3
|
+
* projects/{project_id}
|
|
4
|
+
* projects/{project_id}/reports/{report_id}
|
|
5
|
+
* projects/{project_id}/tasks/{report_id} written only by accept(), read only by the prompt
|
|
6
|
+
* rate/{bucket} has expire_at for a Firestore TTL policy
|
|
7
|
+
*
|
|
8
|
+
* Clients never touch Firestore directly (firestore.rules denies everything); all of it goes
|
|
9
|
+
* through the function.
|
|
10
|
+
*/
|
|
11
|
+
export class FirestoreStore {
|
|
12
|
+
db;
|
|
13
|
+
constructor(db) {
|
|
14
|
+
this.db = db;
|
|
15
|
+
}
|
|
16
|
+
projects() { return this.db.collection('projects'); }
|
|
17
|
+
reports(p) { return this.projects().doc(p).collection('reports'); }
|
|
18
|
+
tasks(p) { return this.projects().doc(p).collection('tasks'); }
|
|
19
|
+
instanceRef() { return this.db.collection('meta').doc('instance'); }
|
|
20
|
+
async getInstance() {
|
|
21
|
+
const s = await this.instanceRef().get();
|
|
22
|
+
return s.exists ? s.data() : null;
|
|
23
|
+
}
|
|
24
|
+
async updateInstance(fn) {
|
|
25
|
+
return this.db.runTransaction(async (tx) => {
|
|
26
|
+
const s = await tx.get(this.instanceRef());
|
|
27
|
+
const next = fn(s.exists ? s.data() : null);
|
|
28
|
+
tx.set(this.instanceRef(), next);
|
|
29
|
+
return next;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
async createProject(p) { await this.projects().doc(p.id).create(p); }
|
|
33
|
+
async getProject(id) {
|
|
34
|
+
const s = await this.projects().doc(id).get();
|
|
35
|
+
return s.exists ? s.data() : null;
|
|
36
|
+
}
|
|
37
|
+
async getProjectByKey(key) {
|
|
38
|
+
const q = await this.projects().where('key', '==', key).limit(1).get();
|
|
39
|
+
return q.empty ? null : q.docs[0].data();
|
|
40
|
+
}
|
|
41
|
+
async listProjects() {
|
|
42
|
+
const q = await this.projects().get();
|
|
43
|
+
return q.docs.map((d) => d.data());
|
|
44
|
+
}
|
|
45
|
+
async createReport(r) { await this.reports(r.project_id).doc(r.id).create(r); }
|
|
46
|
+
async getReport(project_id, id) {
|
|
47
|
+
const s = await this.reports(project_id).doc(id).get();
|
|
48
|
+
return s.exists ? s.data() : null;
|
|
49
|
+
}
|
|
50
|
+
async listReports(project_id) {
|
|
51
|
+
const q = await this.reports(project_id).orderBy('created_at', 'desc').limit(500).get();
|
|
52
|
+
return q.docs.map((d) => d.data());
|
|
53
|
+
}
|
|
54
|
+
async listReportsForDevice(project_id, device_id) {
|
|
55
|
+
const q = await this.reports(project_id).where('device_id', '==', device_id).limit(50).get();
|
|
56
|
+
return q.docs.map((d) => d.data());
|
|
57
|
+
}
|
|
58
|
+
async accept(project_id, report_id, task, at) {
|
|
59
|
+
const ref = this.reports(project_id).doc(report_id);
|
|
60
|
+
return this.db.runTransaction(async (tx) => {
|
|
61
|
+
const s = await tx.get(ref);
|
|
62
|
+
if (!s.exists)
|
|
63
|
+
return { ok: false, reason: 'not_found' };
|
|
64
|
+
const r = s.data();
|
|
65
|
+
if (r.status !== 'open')
|
|
66
|
+
return { ok: false, reason: 'wrong_status', status: r.status };
|
|
67
|
+
const next = { ...r, status: 'accepted', triaged_by: task.accepted_by, updated_at: at };
|
|
68
|
+
tx.set(ref, next);
|
|
69
|
+
tx.create(this.tasks(project_id).doc(report_id), task);
|
|
70
|
+
return { ok: true, report: next };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
async move(project_id, report_id, from, to, patch) {
|
|
74
|
+
const ref = this.reports(project_id).doc(report_id);
|
|
75
|
+
return this.db.runTransaction(async (tx) => {
|
|
76
|
+
const s = await tx.get(ref);
|
|
77
|
+
if (!s.exists)
|
|
78
|
+
return { ok: false, reason: 'not_found' };
|
|
79
|
+
const r = s.data();
|
|
80
|
+
if (r.status !== from)
|
|
81
|
+
return { ok: false, reason: 'wrong_status', status: r.status };
|
|
82
|
+
const next = {
|
|
83
|
+
...r, status: to, triaged_by: patch.triaged_by, updated_at: patch.updated_at,
|
|
84
|
+
decline_reason: patch.decline_reason ?? r.decline_reason,
|
|
85
|
+
};
|
|
86
|
+
tx.set(ref, next);
|
|
87
|
+
return { ok: true, report: next };
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async getTask(project_id, report_id) {
|
|
91
|
+
const s = await this.tasks(project_id).doc(report_id).get();
|
|
92
|
+
return s.exists ? s.data() : null;
|
|
93
|
+
}
|
|
94
|
+
async hit(bucket, limit, expires_at) {
|
|
95
|
+
const ref = this.db.collection('rate').doc(bucket.replaceAll('/', '_'));
|
|
96
|
+
return this.db.runTransaction(async (tx) => {
|
|
97
|
+
const s = await tx.get(ref);
|
|
98
|
+
const n = s.exists ? s.data().n : 0;
|
|
99
|
+
if (n >= limit)
|
|
100
|
+
return false;
|
|
101
|
+
tx.set(ref, { n: n + 1, expire_at: new Date(expires_at) });
|
|
102
|
+
return true;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=firestore-store.js.map
|