@valbuild/server 0.150.14 → 0.150.17
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/CHANGELOG.md +37 -0
- package/browser/dist/valbuild-server-browser.cjs.dev.js +1 -1
- package/browser/dist/valbuild-server-browser.cjs.prod.js +1 -1
- package/browser/dist/valbuild-server-browser.esm.js +1 -1
- package/dist/{ValRouterCore-83973a50.cjs.prod.js → ValRouterCore-6cae8ef3.cjs.prod.js} +601 -7
- package/dist/{ValRouterCore-c4b07665.cjs.dev.js → ValRouterCore-71b88a4c.cjs.dev.js} +601 -7
- package/dist/{ValRouterCore-26bf9ed9.esm.js → ValRouterCore-8ec75007.esm.js} +596 -9
- package/dist/declarations/src/ValOps.d.ts +6 -0
- package/dist/declarations/src/ValOpsFS.d.ts +3 -0
- package/dist/declarations/src/ValOpsHttp.d.ts +3 -0
- package/dist/declarations/src/ValOpsMemory.d.ts +3 -0
- package/dist/declarations/src/ValRouter.d.ts +12 -0
- package/dist/declarations/src/ValServer.d.ts +7 -0
- package/dist/declarations/src/sketchpads/SketchpadStore.d.ts +76 -0
- package/dist/declarations/src/valServerConfig.d.ts +5 -4
- package/dist/{getJsonEntryAtCommit-64632fba.cjs.prod.js → getJsonEntryAtCommit-4649bb37.cjs.prod.js} +1 -1
- package/dist/{getJsonEntryAtCommit-7d550e7f.cjs.dev.js → getJsonEntryAtCommit-54478fa6.cjs.dev.js} +1 -1
- package/dist/{getJsonEntryAtCommit-28be5ffd.esm.js → getJsonEntryAtCommit-a5932dc3.esm.js} +1 -1
- package/dist/valbuild-server.cjs.dev.js +234 -7
- package/dist/valbuild-server.cjs.prod.js +234 -7
- package/dist/valbuild-server.esm.js +236 -9
- package/package.json +3 -3
|
@@ -4564,6 +4564,11 @@ class ValOps {
|
|
|
4564
4564
|
* narrow on it with type-only imports.
|
|
4565
4565
|
*/
|
|
4566
4566
|
|
|
4567
|
+
/**
|
|
4568
|
+
* The project's sketchpads, kept wherever this mode keeps things: see
|
|
4569
|
+
* `sketchpads/SketchpadStore.ts`. Not patches, and never published.
|
|
4570
|
+
*/
|
|
4571
|
+
|
|
4567
4572
|
/**
|
|
4568
4573
|
* Save a patch's binary file from a `data:...;base64,...` URL.
|
|
4569
4574
|
*
|
|
@@ -5504,7 +5509,7 @@ var packageJson = {
|
|
|
5504
5509
|
],
|
|
5505
5510
|
exports: true
|
|
5506
5511
|
},
|
|
5507
|
-
version: "0.150.
|
|
5512
|
+
version: "0.150.17",
|
|
5508
5513
|
scripts: {
|
|
5509
5514
|
typecheck: "tsc --noEmit",
|
|
5510
5515
|
test: "jest",
|
|
@@ -5547,6 +5552,397 @@ var packageJson = {
|
|
|
5547
5552
|
*/
|
|
5548
5553
|
const VERSION = packageJson.version;
|
|
5549
5554
|
|
|
5555
|
+
/**
|
|
5556
|
+
* `/sketchpads` and `/sketchpad`. Here rather than inline in `ValServer` so
|
|
5557
|
+
* the rules every mode shares are in one place: a text is a sketch in the
|
|
5558
|
+
* one spelling or it is refused, line by line; an archived sketchpad is
|
|
5559
|
+
* reopened before it is changed; a change names the version it was made on.
|
|
5560
|
+
*/
|
|
5561
|
+
function createSketchpadRoutes(options) {
|
|
5562
|
+
const {
|
|
5563
|
+
store,
|
|
5564
|
+
getAuth,
|
|
5565
|
+
profileOf
|
|
5566
|
+
} = options;
|
|
5567
|
+
return {
|
|
5568
|
+
"/sketchpads": {
|
|
5569
|
+
GET: async req => {
|
|
5570
|
+
const auth = getAuth(req.cookies);
|
|
5571
|
+
if (auth.error) {
|
|
5572
|
+
return {
|
|
5573
|
+
status: 401,
|
|
5574
|
+
json: {
|
|
5575
|
+
message: auth.error
|
|
5576
|
+
}
|
|
5577
|
+
};
|
|
5578
|
+
}
|
|
5579
|
+
const archived = req.query.archived ?? "exclude";
|
|
5580
|
+
if (!isArchivedFilter(archived)) {
|
|
5581
|
+
return {
|
|
5582
|
+
status: 400,
|
|
5583
|
+
json: {
|
|
5584
|
+
message: `"archived" is exclude, include or only, not "${archived}".`
|
|
5585
|
+
}
|
|
5586
|
+
};
|
|
5587
|
+
}
|
|
5588
|
+
try {
|
|
5589
|
+
const sketchpads = await store().list({
|
|
5590
|
+
archived,
|
|
5591
|
+
...(req.query.page !== undefined ? {
|
|
5592
|
+
page: req.query.page
|
|
5593
|
+
} : {})
|
|
5594
|
+
});
|
|
5595
|
+
return {
|
|
5596
|
+
status: 200,
|
|
5597
|
+
json: {
|
|
5598
|
+
sketchpads
|
|
5599
|
+
}
|
|
5600
|
+
};
|
|
5601
|
+
} catch (error) {
|
|
5602
|
+
return failed(error);
|
|
5603
|
+
}
|
|
5604
|
+
},
|
|
5605
|
+
POST: async req => {
|
|
5606
|
+
const auth = getAuth(req.cookies);
|
|
5607
|
+
if (auth.error) {
|
|
5608
|
+
return {
|
|
5609
|
+
status: 401,
|
|
5610
|
+
json: {
|
|
5611
|
+
message: auth.error
|
|
5612
|
+
}
|
|
5613
|
+
};
|
|
5614
|
+
}
|
|
5615
|
+
const refused = refuseText(req.body.text);
|
|
5616
|
+
if (refused) {
|
|
5617
|
+
return refused;
|
|
5618
|
+
}
|
|
5619
|
+
try {
|
|
5620
|
+
const sketchpad = await store().create({
|
|
5621
|
+
text: req.body.text,
|
|
5622
|
+
title: titleOf(req.body.text),
|
|
5623
|
+
page: req.body.page,
|
|
5624
|
+
by: profileOf(auth)
|
|
5625
|
+
});
|
|
5626
|
+
return {
|
|
5627
|
+
status: 201,
|
|
5628
|
+
json: {
|
|
5629
|
+
sketchpad
|
|
5630
|
+
}
|
|
5631
|
+
};
|
|
5632
|
+
} catch (error) {
|
|
5633
|
+
return failed(error);
|
|
5634
|
+
}
|
|
5635
|
+
}
|
|
5636
|
+
},
|
|
5637
|
+
"/sketchpad": {
|
|
5638
|
+
GET: async req => {
|
|
5639
|
+
const auth = getAuth(req.cookies);
|
|
5640
|
+
if (auth.error) {
|
|
5641
|
+
return {
|
|
5642
|
+
status: 401,
|
|
5643
|
+
json: {
|
|
5644
|
+
message: auth.error
|
|
5645
|
+
}
|
|
5646
|
+
};
|
|
5647
|
+
}
|
|
5648
|
+
try {
|
|
5649
|
+
const sketchpad = await store().get(req.query.id);
|
|
5650
|
+
if (sketchpad === null) {
|
|
5651
|
+
return notFound(req.query.id);
|
|
5652
|
+
}
|
|
5653
|
+
return {
|
|
5654
|
+
status: 200,
|
|
5655
|
+
json: {
|
|
5656
|
+
sketchpad
|
|
5657
|
+
}
|
|
5658
|
+
};
|
|
5659
|
+
} catch (error) {
|
|
5660
|
+
return failed(error);
|
|
5661
|
+
}
|
|
5662
|
+
},
|
|
5663
|
+
PATCH: async req => {
|
|
5664
|
+
const auth = getAuth(req.cookies);
|
|
5665
|
+
if (auth.error) {
|
|
5666
|
+
return {
|
|
5667
|
+
status: 401,
|
|
5668
|
+
json: {
|
|
5669
|
+
message: auth.error
|
|
5670
|
+
}
|
|
5671
|
+
};
|
|
5672
|
+
}
|
|
5673
|
+
const {
|
|
5674
|
+
id,
|
|
5675
|
+
baseVersion,
|
|
5676
|
+
text,
|
|
5677
|
+
page,
|
|
5678
|
+
archived
|
|
5679
|
+
} = req.body;
|
|
5680
|
+
if (text !== undefined) {
|
|
5681
|
+
const refused = refuseText(text);
|
|
5682
|
+
if (refused) {
|
|
5683
|
+
return refused;
|
|
5684
|
+
}
|
|
5685
|
+
}
|
|
5686
|
+
try {
|
|
5687
|
+
const current = await store().get(id);
|
|
5688
|
+
if (current === null) {
|
|
5689
|
+
return notFound(id);
|
|
5690
|
+
}
|
|
5691
|
+
// Reopening is the one change an archived sketchpad takes, and it
|
|
5692
|
+
// can come with others: "reopen and put this in it".
|
|
5693
|
+
if (current.archived && archived !== false && (text !== undefined || page !== undefined)) {
|
|
5694
|
+
return {
|
|
5695
|
+
status: 400,
|
|
5696
|
+
json: {
|
|
5697
|
+
message: "This sketchpad is archived, so it is not changed. Reopen it first (archived: false), or ask the person to."
|
|
5698
|
+
}
|
|
5699
|
+
};
|
|
5700
|
+
}
|
|
5701
|
+
const result = await store().update(id, {
|
|
5702
|
+
baseVersion,
|
|
5703
|
+
...(text !== undefined ? {
|
|
5704
|
+
text,
|
|
5705
|
+
title: titleOf(text)
|
|
5706
|
+
} : {}),
|
|
5707
|
+
...(page !== undefined ? {
|
|
5708
|
+
page
|
|
5709
|
+
} : {}),
|
|
5710
|
+
...(archived !== undefined ? {
|
|
5711
|
+
archived
|
|
5712
|
+
} : {}),
|
|
5713
|
+
by: profileOf(auth)
|
|
5714
|
+
});
|
|
5715
|
+
if (result.status === "not-found") {
|
|
5716
|
+
return notFound(id);
|
|
5717
|
+
}
|
|
5718
|
+
if (result.status === "conflict") {
|
|
5719
|
+
return {
|
|
5720
|
+
status: 409,
|
|
5721
|
+
json: {
|
|
5722
|
+
message: `The sketchpad changed since version ${baseVersion}: it is at version ${result.sketchpad.version} now. Here it is; make the change again on top of it.`,
|
|
5723
|
+
sketchpad: result.sketchpad
|
|
5724
|
+
}
|
|
5725
|
+
};
|
|
5726
|
+
}
|
|
5727
|
+
return {
|
|
5728
|
+
status: 200,
|
|
5729
|
+
json: {
|
|
5730
|
+
sketchpad: result.sketchpad
|
|
5731
|
+
}
|
|
5732
|
+
};
|
|
5733
|
+
} catch (error) {
|
|
5734
|
+
return failed(error);
|
|
5735
|
+
}
|
|
5736
|
+
}
|
|
5737
|
+
}
|
|
5738
|
+
};
|
|
5739
|
+
}
|
|
5740
|
+
function isArchivedFilter(value) {
|
|
5741
|
+
return value === "exclude" || value === "include" || value === "only";
|
|
5742
|
+
}
|
|
5743
|
+
function titleOf(text) {
|
|
5744
|
+
return internal.sketchTitle(internal.parseSketch(text).blocks);
|
|
5745
|
+
}
|
|
5746
|
+
/** A 400 for a text the server will not keep, saying which lines and why. */
|
|
5747
|
+
function refuseText(text) {
|
|
5748
|
+
// Bytes, not characters, as the content service counts them: a page in
|
|
5749
|
+
// Norwegian is longer in UTF-8 than it looks.
|
|
5750
|
+
const bytes = new TextEncoder().encode(text).length;
|
|
5751
|
+
if (bytes > internal.SKETCHPAD_MAX_TEXT_LENGTH) {
|
|
5752
|
+
return {
|
|
5753
|
+
status: 400,
|
|
5754
|
+
json: {
|
|
5755
|
+
message: `A sketchpad holds at most ${internal.SKETCHPAD_MAX_TEXT_LENGTH / 1024} KiB of text; this is ${Math.ceil(bytes / 1024)} KiB.`
|
|
5756
|
+
}
|
|
5757
|
+
};
|
|
5758
|
+
}
|
|
5759
|
+
const errors = internal.checkSketch(text);
|
|
5760
|
+
if (errors.length === 0) {
|
|
5761
|
+
return null;
|
|
5762
|
+
}
|
|
5763
|
+
return {
|
|
5764
|
+
status: 400,
|
|
5765
|
+
json: {
|
|
5766
|
+
message: `That is not a sketch the Sketchpad can show: ${errors.map(error => `line ${error.line}: ${error.message}`).join(" ")}`,
|
|
5767
|
+
errors
|
|
5768
|
+
}
|
|
5769
|
+
};
|
|
5770
|
+
}
|
|
5771
|
+
function notFound(id) {
|
|
5772
|
+
return {
|
|
5773
|
+
status: 404,
|
|
5774
|
+
json: {
|
|
5775
|
+
message: `There is no sketchpad "${id}" in this project.`
|
|
5776
|
+
}
|
|
5777
|
+
};
|
|
5778
|
+
}
|
|
5779
|
+
function failed(error) {
|
|
5780
|
+
return {
|
|
5781
|
+
status: 500,
|
|
5782
|
+
json: {
|
|
5783
|
+
message: error instanceof Error ? error.message : "The sketchpads could not be reached."
|
|
5784
|
+
}
|
|
5785
|
+
};
|
|
5786
|
+
}
|
|
5787
|
+
|
|
5788
|
+
/**
|
|
5789
|
+
* Content analytics on a SELF-HOSTED site: handing a visitor's beacon on to
|
|
5790
|
+
* Val's ingest, which counts it.
|
|
5791
|
+
*
|
|
5792
|
+
* On a site Val hosts, the platform's loader answers `POST /api/val/analytics`
|
|
5793
|
+
* before the app sees it. Anywhere else the beacon reaches this server, and
|
|
5794
|
+
* the only thing that can count it is the ingest -- which only takes it from a
|
|
5795
|
+
* server holding the project's api key, so the browser cannot send it there
|
|
5796
|
+
* itself.
|
|
5797
|
+
*
|
|
5798
|
+
* Kept out of `ValServer.ts` so it can be tested on its own, and free of Node
|
|
5799
|
+
* for the same reason the rest of the request handlers are: the demo runs
|
|
5800
|
+
* them in a browser tab (`noNodeFromBrowser.test.ts`).
|
|
5801
|
+
*/
|
|
5802
|
+
|
|
5803
|
+
/** Where beacons go, unless `VAL_ANALYTICS_INGEST_URL` says otherwise. */
|
|
5804
|
+
const DEFAULT_ANALYTICS_INGEST_URL = "https://platform.valstart.dev/__ingest/analytics";
|
|
5805
|
+
|
|
5806
|
+
/**
|
|
5807
|
+
* The most the ingest takes: it answers 413 above this. The browser script
|
|
5808
|
+
* keeps its beacons well below it (`MAX_BEACON_BYTES` in
|
|
5809
|
+
* `@valbuild/react/analytics`), so one that is not was not built by it.
|
|
5810
|
+
*/
|
|
5811
|
+
const MAX_FORWARDED_BEACON_BYTES = 16 * 1024;
|
|
5812
|
+
|
|
5813
|
+
/** A forward is a visitor's request waiting on another one, so it is short. */
|
|
5814
|
+
const ANALYTICS_FORWARD_TIMEOUT_MS = 3_000;
|
|
5815
|
+
|
|
5816
|
+
/**
|
|
5817
|
+
* The request headers the route reads, by lower-case name. Declared on the
|
|
5818
|
+
* route (`Api["/analytics"]`), so the router hands over these and no others:
|
|
5819
|
+
* the visitor's IP is never among them.
|
|
5820
|
+
*/
|
|
5821
|
+
|
|
5822
|
+
/**
|
|
5823
|
+
* Where hosts in front of an app say which country a request came from, in
|
|
5824
|
+
* the order they are asked. Each is set by the host, never by the browser.
|
|
5825
|
+
*/
|
|
5826
|
+
const COUNTRY_HEADERS = ["x-vercel-ip-country", "cf-ipcountry", "cloudfront-viewer-country"];
|
|
5827
|
+
|
|
5828
|
+
/**
|
|
5829
|
+
* The visitor's country, as ISO 3166-1 alpha-2, when a host in front of the
|
|
5830
|
+
* app said: the first of {@link COUNTRY_HEADERS} that holds two letters.
|
|
5831
|
+
*
|
|
5832
|
+
* Anything else is skipped rather than passed on: Cloudflare's `T1` (Tor)
|
|
5833
|
+
* is not a country, and `XX` is how it says it does not know.
|
|
5834
|
+
*/
|
|
5835
|
+
function visitorCountry(headers) {
|
|
5836
|
+
for (const name of COUNTRY_HEADERS) {
|
|
5837
|
+
var _headers$name;
|
|
5838
|
+
const value = (_headers$name = headers[name]) === null || _headers$name === void 0 ? void 0 : _headers$name.trim();
|
|
5839
|
+
if (value && /^[A-Za-z]{2}$/.test(value)) {
|
|
5840
|
+
const country = value.toUpperCase();
|
|
5841
|
+
if (country !== "XX") {
|
|
5842
|
+
return country;
|
|
5843
|
+
}
|
|
5844
|
+
}
|
|
5845
|
+
}
|
|
5846
|
+
return undefined;
|
|
5847
|
+
}
|
|
5848
|
+
|
|
5849
|
+
/**
|
|
5850
|
+
* Why a beacon was not handed on, when the reason is the request's. Each of
|
|
5851
|
+
* these is answered as before -- `200 { counted: false }` -- because none of
|
|
5852
|
+
* them is something the script can do anything about.
|
|
5853
|
+
*/
|
|
5854
|
+
|
|
5855
|
+
/**
|
|
5856
|
+
* Whether this request may be handed on, and if so what to send. The checks
|
|
5857
|
+
* are the ones only this server can make: a server-to-server forward loses
|
|
5858
|
+
* `Sec-Fetch-Site`, and the ingest would see this server's User-Agent rather
|
|
5859
|
+
* than the visitor's.
|
|
5860
|
+
*/
|
|
5861
|
+
function prepareAnalyticsForward(headers, body) {
|
|
5862
|
+
// A beacon from another site's page is not one of this site's visitors.
|
|
5863
|
+
if (headers["sec-fetch-site"] !== "same-origin") {
|
|
5864
|
+
return {
|
|
5865
|
+
ok: false,
|
|
5866
|
+
reason: "not-same-origin"
|
|
5867
|
+
};
|
|
5868
|
+
}
|
|
5869
|
+
// The ingest tells a bot from a person by its User-Agent; without one there
|
|
5870
|
+
// is nothing to tell, and nothing a browser sends goes without.
|
|
5871
|
+
const userAgent = headers["user-agent"];
|
|
5872
|
+
if (!userAgent) {
|
|
5873
|
+
return {
|
|
5874
|
+
ok: false,
|
|
5875
|
+
reason: "no-user-agent"
|
|
5876
|
+
};
|
|
5877
|
+
}
|
|
5878
|
+
if (utf8ToBytes(body).length > MAX_FORWARDED_BEACON_BYTES) {
|
|
5879
|
+
return {
|
|
5880
|
+
ok: false,
|
|
5881
|
+
reason: "too-large"
|
|
5882
|
+
};
|
|
5883
|
+
}
|
|
5884
|
+
return {
|
|
5885
|
+
ok: true,
|
|
5886
|
+
userAgent,
|
|
5887
|
+
country: visitorCountry(headers)
|
|
5888
|
+
};
|
|
5889
|
+
}
|
|
5890
|
+
|
|
5891
|
+
/**
|
|
5892
|
+
* What the ingest said, or that it could not be asked: `null` for a forward
|
|
5893
|
+
* that threw or timed out. Never a throw -- a visitor's beacon failing to go
|
|
5894
|
+
* further is not the visitor's problem.
|
|
5895
|
+
*/
|
|
5896
|
+
async function forwardAnalyticsBeacon(args) {
|
|
5897
|
+
const controller = new AbortController();
|
|
5898
|
+
const timer = setTimeout(() => controller.abort(), args.timeoutMs ?? ANALYTICS_FORWARD_TIMEOUT_MS);
|
|
5899
|
+
const headers = {
|
|
5900
|
+
authorization: `Bearer ${args.apiKey}`,
|
|
5901
|
+
"x-val-project": args.project,
|
|
5902
|
+
"x-val-visitor-user-agent": args.userAgent,
|
|
5903
|
+
"content-type": "text/plain;charset=UTF-8"
|
|
5904
|
+
};
|
|
5905
|
+
if (args.country !== undefined) {
|
|
5906
|
+
headers["x-val-visitor-country"] = args.country;
|
|
5907
|
+
}
|
|
5908
|
+
try {
|
|
5909
|
+
const res = await fetch(args.ingestUrl, {
|
|
5910
|
+
method: "POST",
|
|
5911
|
+
headers,
|
|
5912
|
+
body: args.body,
|
|
5913
|
+
signal: controller.signal
|
|
5914
|
+
});
|
|
5915
|
+
// Nothing in an answer is read but its status. Cancelled rather than left
|
|
5916
|
+
// for the garbage collector, which in Node holds the connection until then.
|
|
5917
|
+
try {
|
|
5918
|
+
var _res$body;
|
|
5919
|
+
await ((_res$body = res.body) === null || _res$body === void 0 ? void 0 : _res$body.cancel());
|
|
5920
|
+
} catch {
|
|
5921
|
+
// Already read or closed: nothing to free.
|
|
5922
|
+
}
|
|
5923
|
+
return res.status;
|
|
5924
|
+
} catch (err) {
|
|
5925
|
+
core.Internal.log.debug(`analytics: could not forward a beacon to ${args.ingestUrl}`, err);
|
|
5926
|
+
return null;
|
|
5927
|
+
} finally {
|
|
5928
|
+
clearTimeout(timer);
|
|
5929
|
+
}
|
|
5930
|
+
}
|
|
5931
|
+
let warnedUnauthorized = false;
|
|
5932
|
+
|
|
5933
|
+
/**
|
|
5934
|
+
* The ingest refused this server's api key: a misconfiguration the site's
|
|
5935
|
+
* developer can fix, and the same one on every beacon. So it is said once a
|
|
5936
|
+
* process, where a warning per visitor would bury everything else.
|
|
5937
|
+
*/
|
|
5938
|
+
function warnAnalyticsUnauthorizedOnce(project) {
|
|
5939
|
+
if (warnedUnauthorized) {
|
|
5940
|
+
return;
|
|
5941
|
+
}
|
|
5942
|
+
warnedUnauthorized = true;
|
|
5943
|
+
core.Internal.log.warn(`Content analytics is not being counted: Val's analytics ingest did not accept this site's api key for the project "${project}" (401). ` + "Check that VAL_API_KEY is an api key of that project, and that VAL_PROJECT names it as org/project.");
|
|
5944
|
+
}
|
|
5945
|
+
|
|
5550
5946
|
/** What {@link ValServerOptions.publishOverride} is given. */
|
|
5551
5947
|
|
|
5552
5948
|
/** What a publish reports back, in either shape. */
|
|
@@ -6049,6 +6445,11 @@ const ValServer = (valModules, options, callbacks, platform) => {
|
|
|
6049
6445
|
};
|
|
6050
6446
|
};
|
|
6051
6447
|
return {
|
|
6448
|
+
...createSketchpadRoutes({
|
|
6449
|
+
store: () => serverOps.sketchpads,
|
|
6450
|
+
getAuth,
|
|
6451
|
+
profileOf
|
|
6452
|
+
}),
|
|
6052
6453
|
"/draft/enable": {
|
|
6053
6454
|
GET: async req => {
|
|
6054
6455
|
const cookies = req.cookies;
|
|
@@ -6101,10 +6502,20 @@ const ValServer = (valModules, options, callbacks, platform) => {
|
|
|
6101
6502
|
* Content analytics beacons, outside the hosted platform.
|
|
6102
6503
|
*
|
|
6103
6504
|
* On a site Val hosts, the platform's loader answers this route before a
|
|
6104
|
-
* request reaches the app, and counts.
|
|
6105
|
-
*
|
|
6106
|
-
*
|
|
6107
|
-
*
|
|
6505
|
+
* request reaches the app, and counts.
|
|
6506
|
+
*
|
|
6507
|
+
* A SELF-HOSTED site in http mode has the project's api key, and hands
|
|
6508
|
+
* each beacon on to Val's ingest, which counts it (`analyticsForward.ts`).
|
|
6509
|
+
* For that the site needs `VAL_API_KEY` and `VAL_PROJECT` (which http
|
|
6510
|
+
* mode needs anyway), the script started -- `VAL_ANALYTICS`, read by the
|
|
6511
|
+
* layout and passed to `<ValProvider analytics>` -- and analytics switched
|
|
6512
|
+
* on for the project in Val's admin. While it is off there, the ingest
|
|
6513
|
+
* answers 410, which is passed on, and the script stops.
|
|
6514
|
+
*
|
|
6515
|
+
* Everywhere else -- fs mode, memory mode -- nothing counts: this checks
|
|
6516
|
+
* the beacon and answers, so a site with analytics on does not fill its
|
|
6517
|
+
* visitors' consoles with 404s. In local dev it also logs a line per
|
|
6518
|
+
* beacon, which is how the showcase shows analytics working.
|
|
6108
6519
|
*/
|
|
6109
6520
|
"/analytics": {
|
|
6110
6521
|
POST: async req => {
|
|
@@ -6133,10 +6544,52 @@ const ValServer = (valModules, options, callbacks, platform) => {
|
|
|
6133
6544
|
const count = paths => Object.values(paths).reduce((n, list) => n + list.length, 0);
|
|
6134
6545
|
core.Internal.log.debug(`analytics: ${beacon.route}${beacon.view ? " (page view)" : ""}: ${count(beacon.rendered)} rendered, ${count(beacon.viewed)} viewed`);
|
|
6135
6546
|
}
|
|
6547
|
+
if (options.mode !== "http") {
|
|
6548
|
+
return {
|
|
6549
|
+
status: 200,
|
|
6550
|
+
json: {
|
|
6551
|
+
counted: false
|
|
6552
|
+
}
|
|
6553
|
+
};
|
|
6554
|
+
}
|
|
6555
|
+
const body = JSON.stringify(parsed.data);
|
|
6556
|
+
const plan = prepareAnalyticsForward(req.headers, body);
|
|
6557
|
+
if (!plan.ok) {
|
|
6558
|
+
core.Internal.log.debug(`analytics: not forwarded (${plan.reason})`);
|
|
6559
|
+
return {
|
|
6560
|
+
status: 200,
|
|
6561
|
+
json: {
|
|
6562
|
+
counted: false
|
|
6563
|
+
}
|
|
6564
|
+
};
|
|
6565
|
+
}
|
|
6566
|
+
const ingestStatus = await forwardAnalyticsBeacon({
|
|
6567
|
+
ingestUrl: options.valAnalyticsIngestUrl ?? DEFAULT_ANALYTICS_INGEST_URL,
|
|
6568
|
+
apiKey: options.apiKey,
|
|
6569
|
+
project: options.project,
|
|
6570
|
+
body,
|
|
6571
|
+
userAgent: plan.userAgent,
|
|
6572
|
+
country: plan.country
|
|
6573
|
+
});
|
|
6574
|
+
if (ingestStatus === 410) {
|
|
6575
|
+
// Off for this project: the script stops on a 410, so it is passed
|
|
6576
|
+
// on rather than answered as "not counted", which it would retry.
|
|
6577
|
+
return {
|
|
6578
|
+
status: 410,
|
|
6579
|
+
json: {
|
|
6580
|
+
message: "Content analytics is switched off for this project."
|
|
6581
|
+
}
|
|
6582
|
+
};
|
|
6583
|
+
}
|
|
6584
|
+
if (ingestStatus === 401) {
|
|
6585
|
+
warnAnalyticsUnauthorizedOnce(options.project);
|
|
6586
|
+
} else if (ingestStatus !== null && ingestStatus !== 204) {
|
|
6587
|
+
core.Internal.log.debug(`analytics: the ingest answered ${ingestStatus} to a beacon`);
|
|
6588
|
+
}
|
|
6136
6589
|
return {
|
|
6137
6590
|
status: 200,
|
|
6138
6591
|
json: {
|
|
6139
|
-
counted:
|
|
6592
|
+
counted: ingestStatus === 204
|
|
6140
6593
|
}
|
|
6141
6594
|
};
|
|
6142
6595
|
}
|
|
@@ -9242,7 +9695,7 @@ const ValServer = (valModules, options, callbacks, platform) => {
|
|
|
9242
9695
|
// TypeScript compiler. See `patch/ts/publishKit.ts`.
|
|
9243
9696
|
const {
|
|
9244
9697
|
getJsonEntryAtCommit
|
|
9245
|
-
} = await Promise.resolve().then(function () { return require('./getJsonEntryAtCommit-
|
|
9698
|
+
} = await Promise.resolve().then(function () { return require('./getJsonEntryAtCommit-54478fa6.cjs.dev.js'); });
|
|
9246
9699
|
const res = await getJsonEntryAtCommit(serverOps, req.query.commit_sha, req.query.path, req.query.key);
|
|
9247
9700
|
if (fp.result.isErr(res)) {
|
|
9248
9701
|
return historyErrorResponse(res.error);
|
|
@@ -9972,6 +10425,138 @@ async function uploadRemoteFile(contentHost, project, bucket, fileHash, fileExt,
|
|
|
9972
10425
|
};
|
|
9973
10426
|
}
|
|
9974
10427
|
|
|
10428
|
+
/**
|
|
10429
|
+
* Where a project's sketchpads are kept, in each of the server's modes:
|
|
10430
|
+
* `.val/sketchpads` in `fs` mode, the content service in `http` mode, the
|
|
10431
|
+
* tab's memory in the demo. See `Sketchpad` in `ApiRoutes.ts` for what one
|
|
10432
|
+
* is.
|
|
10433
|
+
*
|
|
10434
|
+
* The store keeps what it is given. Whether a text is a sketch at all is
|
|
10435
|
+
* decided before it gets here (`checkSketch`, in `ValServer`), once for every
|
|
10436
|
+
* mode, so no store has its own idea of the format.
|
|
10437
|
+
*/
|
|
10438
|
+
|
|
10439
|
+
/**
|
|
10440
|
+
* Whether `id` is in the shape a sketchpad's id has. Every store checks it
|
|
10441
|
+
* before the id goes anywhere it could mean something else: a file name in
|
|
10442
|
+
* fs mode, a URL path in http mode, where `..` is not an id but a way out.
|
|
10443
|
+
*/
|
|
10444
|
+
function isSketchpadId(id) {
|
|
10445
|
+
return /^[0-9a-f-]{36}$/.test(id);
|
|
10446
|
+
}
|
|
10447
|
+
|
|
10448
|
+
/**
|
|
10449
|
+
* A random id in the shape of a UUID. `getRandomValues` rather than
|
|
10450
|
+
* `randomUUID`, which a browser only has in a secure context -- and the demo
|
|
10451
|
+
* runs this server in the visitor's tab.
|
|
10452
|
+
*/
|
|
10453
|
+
function newSketchpadId() {
|
|
10454
|
+
const bytes = new Uint8Array(16);
|
|
10455
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
10456
|
+
bytes[6] = bytes[6] & 0x0f | 0x40;
|
|
10457
|
+
bytes[8] = bytes[8] & 0x3f | 0x80;
|
|
10458
|
+
const hex = Array.from(bytes, b => b.toString(16).padStart(2, "0"));
|
|
10459
|
+
return [hex.slice(0, 4).join(""), hex.slice(4, 6).join(""), hex.slice(6, 8).join(""), hex.slice(8, 10).join(""), hex.slice(10, 16).join("")].join("-");
|
|
10460
|
+
}
|
|
10461
|
+
function summaryOf(sketchpad) {
|
|
10462
|
+
const {
|
|
10463
|
+
text,
|
|
10464
|
+
...summary
|
|
10465
|
+
} = sketchpad;
|
|
10466
|
+
return summary;
|
|
10467
|
+
}
|
|
10468
|
+
function matchesFilter(sketchpad, filter) {
|
|
10469
|
+
if (filter.page !== undefined && sketchpad.page !== filter.page) {
|
|
10470
|
+
return false;
|
|
10471
|
+
}
|
|
10472
|
+
return filter.archived === "include" ? true : filter.archived === "only" ? sketchpad.archived : !sketchpad.archived;
|
|
10473
|
+
}
|
|
10474
|
+
|
|
10475
|
+
/** Newest change first, as every list of them is read. */
|
|
10476
|
+
function byUpdatedAt(a, b) {
|
|
10477
|
+
return b.updatedAt.localeCompare(a.updatedAt);
|
|
10478
|
+
}
|
|
10479
|
+
function newSketchpad(input, now) {
|
|
10480
|
+
const at = now.toISOString();
|
|
10481
|
+
return {
|
|
10482
|
+
id: newSketchpadId(),
|
|
10483
|
+
page: input.page,
|
|
10484
|
+
title: input.title,
|
|
10485
|
+
text: input.text,
|
|
10486
|
+
version: 1,
|
|
10487
|
+
archived: false,
|
|
10488
|
+
createdAt: at,
|
|
10489
|
+
updatedAt: at,
|
|
10490
|
+
createdBy: input.by,
|
|
10491
|
+
updatedBy: input.by
|
|
10492
|
+
};
|
|
10493
|
+
}
|
|
10494
|
+
|
|
10495
|
+
/** `current` with `change` applied, or why not. Pure: storing is the caller's. */
|
|
10496
|
+
function applySketchpadChange(current, change, now) {
|
|
10497
|
+
if (current === null) {
|
|
10498
|
+
return {
|
|
10499
|
+
status: "not-found"
|
|
10500
|
+
};
|
|
10501
|
+
}
|
|
10502
|
+
if (current.version !== change.baseVersion) {
|
|
10503
|
+
return {
|
|
10504
|
+
status: "conflict",
|
|
10505
|
+
sketchpad: current
|
|
10506
|
+
};
|
|
10507
|
+
}
|
|
10508
|
+
return {
|
|
10509
|
+
status: "updated",
|
|
10510
|
+
sketchpad: {
|
|
10511
|
+
...current,
|
|
10512
|
+
...(change.text !== undefined ? {
|
|
10513
|
+
text: change.text
|
|
10514
|
+
} : {}),
|
|
10515
|
+
...(change.title !== undefined ? {
|
|
10516
|
+
title: change.title
|
|
10517
|
+
} : {}),
|
|
10518
|
+
...(change.page !== undefined ? {
|
|
10519
|
+
page: change.page
|
|
10520
|
+
} : {}),
|
|
10521
|
+
...(change.archived !== undefined ? {
|
|
10522
|
+
archived: change.archived
|
|
10523
|
+
} : {}),
|
|
10524
|
+
version: current.version + 1,
|
|
10525
|
+
updatedAt: now.toISOString(),
|
|
10526
|
+
updatedBy: change.by
|
|
10527
|
+
}
|
|
10528
|
+
};
|
|
10529
|
+
}
|
|
10530
|
+
|
|
10531
|
+
/**
|
|
10532
|
+
* Sketchpads kept in memory, for as long as the server runs: the demo's
|
|
10533
|
+
* store, where a reload starts over anyway, and the tests'.
|
|
10534
|
+
*/
|
|
10535
|
+
function createMemorySketchpadStore(now = () => new Date()) {
|
|
10536
|
+
const sketchpads = new Map();
|
|
10537
|
+
return {
|
|
10538
|
+
async list(filter) {
|
|
10539
|
+
return Array.from(sketchpads.values()).map(summaryOf).filter(sketchpad => matchesFilter(sketchpad, filter)).sort(byUpdatedAt);
|
|
10540
|
+
},
|
|
10541
|
+
async get(id) {
|
|
10542
|
+
return sketchpads.get(id) ?? null;
|
|
10543
|
+
},
|
|
10544
|
+
async create(input) {
|
|
10545
|
+
const sketchpad = newSketchpad(input, now());
|
|
10546
|
+
sketchpads.set(sketchpad.id, sketchpad);
|
|
10547
|
+
return sketchpad;
|
|
10548
|
+
},
|
|
10549
|
+
async update(id, change) {
|
|
10550
|
+
// Synchronous from the read to the write, so nothing interleaves.
|
|
10551
|
+
const result = applySketchpadChange(sketchpads.get(id) ?? null, change, now());
|
|
10552
|
+
if (result.status === "updated") {
|
|
10553
|
+
sketchpads.set(id, result.sketchpad);
|
|
10554
|
+
}
|
|
10555
|
+
return result;
|
|
10556
|
+
}
|
|
10557
|
+
};
|
|
10558
|
+
}
|
|
10559
|
+
|
|
9975
10560
|
/**
|
|
9976
10561
|
* One stored patch. Everything the ordered chain needs and nothing else.
|
|
9977
10562
|
*/
|
|
@@ -10104,6 +10689,8 @@ class ValOpsMemory extends ValOps {
|
|
|
10104
10689
|
*/
|
|
10105
10690
|
patchesAreLocal = true;
|
|
10106
10691
|
kind = "memory";
|
|
10692
|
+
/** For as long as the tab is open, like everything else in the demo. */
|
|
10693
|
+
sketchpads = createMemorySketchpadStore();
|
|
10107
10694
|
/**
|
|
10108
10695
|
* Required, unless the host explicitly takes the boundary itself.
|
|
10109
10696
|
*
|
|
@@ -10994,12 +11581,15 @@ function getCookies(req, cookiesDef) {
|
|
|
10994
11581
|
return z.z.object(cookiesDef).safeParse(input);
|
|
10995
11582
|
}
|
|
10996
11583
|
|
|
11584
|
+
exports.DEFAULT_ANALYTICS_INGEST_URL = DEFAULT_ANALYTICS_INGEST_URL;
|
|
10997
11585
|
exports.InMemoryPatchStore = InMemoryPatchStore;
|
|
10998
11586
|
exports.ValOps = ValOps;
|
|
10999
11587
|
exports.ValOpsMemory = ValOpsMemory;
|
|
11000
11588
|
exports.ValServer = ValServer;
|
|
11589
|
+
exports.applySketchpadChange = applySketchpadChange;
|
|
11001
11590
|
exports.askOncePerHost = askOncePerHost;
|
|
11002
11591
|
exports.bufferFromDataUrl = bufferFromDataUrl;
|
|
11592
|
+
exports.byUpdatedAt = byUpdatedAt;
|
|
11003
11593
|
exports.bytesToBase64 = bytesToBase64;
|
|
11004
11594
|
exports.bytesToUtf8 = bytesToUtf8;
|
|
11005
11595
|
exports.checkRemoteFiles = checkRemoteFiles;
|
|
@@ -11017,15 +11607,19 @@ exports.getSettings = getSettings;
|
|
|
11017
11607
|
exports.guessMimeTypeFromPath = guessMimeTypeFromPath;
|
|
11018
11608
|
exports.isHlsMasterPlaylist = isHlsMasterPlaylist;
|
|
11019
11609
|
exports.isJsonValuesRootOp = isJsonValuesRootOp;
|
|
11610
|
+
exports.isSketchpadId = isSketchpadId;
|
|
11020
11611
|
exports.largestHlsVariant = largestHlsVariant;
|
|
11021
11612
|
exports.mapHlsUris = mapHlsUris;
|
|
11613
|
+
exports.matchesFilter = matchesFilter;
|
|
11022
11614
|
exports.movedRemoteHost = movedRemoteHost;
|
|
11023
11615
|
exports.needsHostCheck = needsHostCheck;
|
|
11616
|
+
exports.newSketchpad = newSketchpad;
|
|
11024
11617
|
exports.parseHlsMasterVariants = parseHlsMasterVariants;
|
|
11025
11618
|
exports.posixPath = posixPath;
|
|
11026
11619
|
exports.rebaseContentOp = rebaseContentOp;
|
|
11027
11620
|
exports.resolveExistingJsonPath = resolveExistingJsonPath;
|
|
11028
11621
|
exports.rewriteRefHost = rewriteRefHost;
|
|
11029
11622
|
exports.sumHlsSegmentDurations = sumHlsSegmentDurations;
|
|
11623
|
+
exports.summaryOf = summaryOf;
|
|
11030
11624
|
exports.uploadRemoteFile = uploadRemoteFile;
|
|
11031
11625
|
exports.validateJsonValuesEntries = validateJsonValuesEntries;
|