@btps/sdk 1.2.0-rc.2 → 1.2.0-rc.3.1
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 +87 -0
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.js +7 -1
- package/dist/core/crypto/decryptVerify.js +6 -2
- package/dist/core/crypto/types.d.ts +3 -0
- package/dist/core/server/constants/btps-protocol.js +4 -2
- package/dist/core/server/constants/currency.d.ts +6 -0
- package/dist/core/server/constants/currency.js +27 -2
- package/dist/core/server/constants/limits.d.ts +25 -0
- package/dist/core/server/constants/limits.js +27 -0
- package/dist/core/server/constants/permissions.d.ts +1 -1
- package/dist/core/server/schemas/boundView.d.ts +9 -0
- package/dist/core/server/schemas/boundView.js +28 -0
- package/dist/core/server/schemas/boundViewBindings.d.ts +40 -0
- package/dist/core/server/schemas/boundViewBindings.js +128 -0
- package/dist/core/server/schemas/boundViewChecks.d.ts +49 -0
- package/dist/core/server/schemas/boundViewChecks.js +344 -0
- package/dist/core/server/schemas/boundViewSchema.d.ts +1637 -0
- package/dist/core/server/schemas/boundViewSchema.js +186 -0
- package/dist/core/server/schemas/btpsDocsSchema.d.ts +36528 -9259
- package/dist/core/server/schemas/btpsDocsSchema.js +48 -35
- package/dist/core/server/schemas/helpers.d.ts +11119 -2337
- package/dist/server/btpsServer.d.ts +2 -1
- package/dist/server/btpsServer.js +10 -2
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +9 -1
- package/dist/server/types.d.ts +23 -26
- package/package.json +14 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { Unzlib } from "fflate";
|
|
10
|
+
import { BTPS_FIELD_LIMITS } from "../constants/limits.js";
|
|
11
|
+
import { boundedArray, boundedRecord } from "./bounded.js";
|
|
12
|
+
const limits = BTPS_FIELD_LIMITS.boundView, hasTwoDecimals = (value) => Math.abs(value * 100 - Math.round(value * 100)) < 1e-6, decimal = (min, max) => z.number().min(min).max(max).refine(hasTwoDecimals, "has at most two decimal places"), position = decimal(-limits.position, limits.position), size = decimal(0, limits.position), point = z.tuple([position, position]), opacity = z.number().min(0).max(1), colour = z.string().regex(/^#[0-9a-f]{6}([0-9a-f]{2})?$/, "must be #rrggbb or #rrggbbaa"), solidColour = z.string().regex(/^#[0-9a-f]{6}$/, "must be #rrggbb"), stops = boundedArray(z.tuple([z.number().min(0).max(1), colour]), limits.stops, 2).refine(
|
|
13
|
+
(list) => list.every(([offset], at) => at === 0 || offset >= (list[at - 1]?.[0] ?? 0)),
|
|
14
|
+
"stops are in increasing order"
|
|
15
|
+
), paint = z.union([
|
|
16
|
+
colour,
|
|
17
|
+
z.object({ linear: z.object({ from: point, to: point, stops }).strict() }).strict(),
|
|
18
|
+
z.object({ radial: z.object({ centre: point, radius: size, stops }).strict() }).strict()
|
|
19
|
+
]), ARITY = { M: 2, L: 2, H: 1, V: 1, Q: 4, C: 6, Z: 0 }, isPathData = (d) => {
|
|
20
|
+
const tokens = d.trim().split(/[\s,]+|(?=[A-Z])|(?<=[A-Z])/).filter(Boolean);
|
|
21
|
+
let command, numbers = 0;
|
|
22
|
+
for (const token of tokens)
|
|
23
|
+
if (token in ARITY) {
|
|
24
|
+
if (command !== void 0 && numbers !== ARITY[command] || command === void 0 && token !== "M") return !1;
|
|
25
|
+
command = token, numbers = 0;
|
|
26
|
+
} else if (/^-?\d+(\.\d{1,2})?$/.test(token) && command !== void 0) {
|
|
27
|
+
if (numbers++, numbers > (ARITY[command] ?? 0)) return !1;
|
|
28
|
+
} else
|
|
29
|
+
return !1;
|
|
30
|
+
return command !== void 0 && numbers === ARITY[command];
|
|
31
|
+
}, pathData = (max) => z.string().min(1).max(max).refine(isPathData, "must be M, L, H, V, Q, C and Z commands"), fill = { fill: paint.optional(), opacity: opacity.optional() }, stroke = { stroke: paint.optional(), strokeWidth: decimal(0, 100).optional() }, box = { x: position, y: position, width: size, height: size }, plainText = (text) => [...text].every((char) => {
|
|
32
|
+
const code = char.codePointAt(0) ?? 0, hidden = code >= 8203 && code <= 8207 || code >= 8234 && code <= 8238, isolate = code >= 8294 && code <= 8297;
|
|
33
|
+
return code >= 32 && code !== 127 && !hidden && !isolate && code !== 65279;
|
|
34
|
+
}), MONEY_SEPARATORS = [",", ".", " ", "'", ""], DATE_PATTERNS = [
|
|
35
|
+
"d MMM yyyy",
|
|
36
|
+
"d MMMM yyyy",
|
|
37
|
+
"MMM d, yyyy",
|
|
38
|
+
"MMMM d, yyyy",
|
|
39
|
+
"dd/MM/yyyy",
|
|
40
|
+
"MM/dd/yyyy",
|
|
41
|
+
"dd.MM.yyyy",
|
|
42
|
+
"dd-MM-yyyy",
|
|
43
|
+
"yyyy-MM-dd"
|
|
44
|
+
], affix = z.string().max(limits.affix).refine(plainText, "must be plain text"), format = z.union([
|
|
45
|
+
z.object({
|
|
46
|
+
money: z.object({
|
|
47
|
+
group: z.enum(MONEY_SEPARATORS),
|
|
48
|
+
decimal: z.enum([".", ","]),
|
|
49
|
+
prefix: affix.optional(),
|
|
50
|
+
suffix: affix.optional()
|
|
51
|
+
}).strict().refine((money) => money.group !== money.decimal, "group and decimal differ")
|
|
52
|
+
}).strict(),
|
|
53
|
+
z.object({ date: z.enum(DATE_PATTERNS) }).strict(),
|
|
54
|
+
z.object({
|
|
55
|
+
percent: z.object({ decimals: z.union([z.literal(0), z.literal(1), z.literal(2)]), decimal: z.enum([".", ","]) }).strict()
|
|
56
|
+
}).strict(),
|
|
57
|
+
z.object({
|
|
58
|
+
groups: z.object({
|
|
59
|
+
sizes: boundedArray(z.number().int().min(1), limits.groups, 1),
|
|
60
|
+
separator: z.string().refine((value) => [...value].length === 1 && plainText(value), "is one character")
|
|
61
|
+
}).strict()
|
|
62
|
+
}).strict()
|
|
63
|
+
]), binding = z.object({
|
|
64
|
+
field: z.string().min(1).max(BTPS_FIELD_LIMITS.documentId * 4),
|
|
65
|
+
format: format.optional(),
|
|
66
|
+
range: z.tuple([z.number().int().min(0), z.number().int().min(0)]).refine(([start, end]) => start < end, "a range starts before it ends").optional()
|
|
67
|
+
}).strict().refine((bind) => bind.range === void 0 || bind.format === void 0, {
|
|
68
|
+
message: "a range is for text written as it is",
|
|
69
|
+
path: ["range"]
|
|
70
|
+
}), textBox = {
|
|
71
|
+
x: position,
|
|
72
|
+
y: position,
|
|
73
|
+
width: size,
|
|
74
|
+
align: z.enum(["start", "centre", "end"]),
|
|
75
|
+
font: z.string().min(1),
|
|
76
|
+
size: decimal(6, 400),
|
|
77
|
+
colour: solidColour,
|
|
78
|
+
letterSpacing: decimal(-10, 50).optional(),
|
|
79
|
+
transform: z.enum(["uppercase", "lowercase"]).optional()
|
|
80
|
+
}, element = z.discriminatedUnion("type", [
|
|
81
|
+
z.object({
|
|
82
|
+
type: z.literal("rect"),
|
|
83
|
+
...box,
|
|
84
|
+
radius: z.union([size, z.tuple([size, size, size, size])]).optional(),
|
|
85
|
+
...fill,
|
|
86
|
+
...stroke
|
|
87
|
+
}).strict(),
|
|
88
|
+
z.object({
|
|
89
|
+
type: z.literal("line"),
|
|
90
|
+
from: point,
|
|
91
|
+
to: point,
|
|
92
|
+
stroke: paint,
|
|
93
|
+
strokeWidth: decimal(0, 100),
|
|
94
|
+
dash: z.tuple([decimal(0, 1e3), decimal(0, 1e3)]).optional(),
|
|
95
|
+
opacity: opacity.optional()
|
|
96
|
+
}).strict(),
|
|
97
|
+
z.object({
|
|
98
|
+
type: z.literal("path"),
|
|
99
|
+
d: pathData(limits.path),
|
|
100
|
+
fillRule: z.enum(["nonzero", "evenodd"]).optional(),
|
|
101
|
+
...fill,
|
|
102
|
+
...stroke
|
|
103
|
+
}).strict(),
|
|
104
|
+
z.object({ type: z.literal("image"), image: z.string().min(1), ...box, opacity: opacity.optional() }).strict(),
|
|
105
|
+
z.object({ type: z.literal("text"), bind: binding, ...textBox }).strict(),
|
|
106
|
+
z.object({
|
|
107
|
+
type: z.literal("label"),
|
|
108
|
+
text: z.string().min(1).max(limits.label).refine(plainText, "must be plain text"),
|
|
109
|
+
...textBox
|
|
110
|
+
}).strict(),
|
|
111
|
+
z.object({ type: z.literal("signature"), ...box, colour: solidColour }).strict()
|
|
112
|
+
]), fontUnits = decimal(-65535, 65535), oneCharacter = z.string().regex(/^.$/su, "is one character"), twoCharacters = z.string().regex(/^.{2}$/su, "is two characters"), font = z.object({
|
|
113
|
+
unitsPerEm: z.number().int().min(16).max(16384),
|
|
114
|
+
ascent: decimal(0, 65535),
|
|
115
|
+
descent: decimal(0, 65535),
|
|
116
|
+
glyphs: boundedRecord(
|
|
117
|
+
z.object({ advance: decimal(0, 65535), path: pathData(limits.glyphPath).optional() }).strict(),
|
|
118
|
+
limits.glyphs,
|
|
119
|
+
oneCharacter
|
|
120
|
+
).optional(),
|
|
121
|
+
kerning: boundedRecord(fontUnits, limits.kerning, twoCharacters).optional(),
|
|
122
|
+
fallback: boundedArray(
|
|
123
|
+
z.enum(["arimo", "tinos", "cousine", "carlito", "caladea", "sans-serif", "serif", "monospace"]),
|
|
124
|
+
limits.fallback,
|
|
125
|
+
1
|
|
126
|
+
).optional()
|
|
127
|
+
}).strict().refine((value) => value.glyphs !== void 0 || value.fallback !== void 0, {
|
|
128
|
+
message: "a font has glyphs, a fallback, or both"
|
|
129
|
+
}), base64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/, inflatesTo = (data, length) => {
|
|
130
|
+
const bytes = Buffer.from(data, "base64");
|
|
131
|
+
let total = 0;
|
|
132
|
+
const stream = new Unzlib((chunk) => {
|
|
133
|
+
total += chunk.length;
|
|
134
|
+
});
|
|
135
|
+
try {
|
|
136
|
+
for (let at = 0; at < bytes.length && total <= length; at += 1024)
|
|
137
|
+
stream.push(bytes.subarray(at, at + 1024), at + 1024 >= bytes.length);
|
|
138
|
+
} catch {
|
|
139
|
+
return !1;
|
|
140
|
+
}
|
|
141
|
+
return total === length;
|
|
142
|
+
}, image = z.object({
|
|
143
|
+
width: z.number().int().min(1).max(limits.imageSide),
|
|
144
|
+
height: z.number().int().min(1).max(limits.imageSide),
|
|
145
|
+
encoding: z.literal("rgba8-deflate"),
|
|
146
|
+
data: z.string().min(1).max(limits.imageData).regex(base64, "must be base64")
|
|
147
|
+
}).strict().superRefine((value, ctx) => {
|
|
148
|
+
const pixels = value.width * value.height;
|
|
149
|
+
pixels > limits.imagePixels ? ctx.addIssue({
|
|
150
|
+
code: z.ZodIssueCode.custom,
|
|
151
|
+
message: `an image has at most ${limits.imagePixels} pixels`
|
|
152
|
+
}) : inflatesTo(value.data, pixels * 4) || ctx.addIssue({
|
|
153
|
+
code: z.ZodIssueCode.custom,
|
|
154
|
+
message: "data inflates to exactly width \xD7 height \xD7 4 bytes",
|
|
155
|
+
path: ["data"]
|
|
156
|
+
});
|
|
157
|
+
}), tooLarge = (value) => new TextEncoder().encode(JSON.stringify(value)).length > limits.json, BtpBoundViewSchema = z.preprocess(
|
|
158
|
+
(value, ctx) => (typeof value == "object" && value !== null && tooLarge(value) && ctx.addIssue({
|
|
159
|
+
code: z.ZodIssueCode.custom,
|
|
160
|
+
message: `a BoundView is at most ${limits.json} bytes as JSON`,
|
|
161
|
+
fatal: !0
|
|
162
|
+
}), value),
|
|
163
|
+
z.object({
|
|
164
|
+
format: z.literal(1),
|
|
165
|
+
page: z.object({
|
|
166
|
+
width: decimal(1, limits.pageWidth),
|
|
167
|
+
height: decimal(1, limits.pageHeight),
|
|
168
|
+
background: colour.optional(),
|
|
169
|
+
breaks: boundedArray(position, limits.breaks).optional()
|
|
170
|
+
}).strict(),
|
|
171
|
+
fonts: boundedRecord(font, limits.fonts).optional(),
|
|
172
|
+
images: boundedRecord(image, limits.images).optional(),
|
|
173
|
+
elements: boundedArray(element, limits.elements)
|
|
174
|
+
}).strict().superRefine((view, ctx) => {
|
|
175
|
+
const issue = (message, path) => ctx.addIssue({ code: z.ZodIssueCode.custom, message, path }), breaks = view.page.breaks ?? [];
|
|
176
|
+
breaks.some((at, index) => at <= 0 || at >= view.page.height || at <= (breaks[index - 1] ?? 0)) && issue("breaks are inside the page, in increasing order", ["page", "breaks"]);
|
|
177
|
+
let texts = 0, paths = 0;
|
|
178
|
+
view.elements.forEach((item, index) => {
|
|
179
|
+
(item.type === "rect" || item.type === "path") && item.stroke === void 0 != (item.strokeWidth === void 0) && issue("stroke and strokeWidth come together", ["elements", index, "strokeWidth"]), (item.type === "text" || item.type === "label" || item.type === "signature") && texts++, item.type === "path" && (paths += item.d.length), (item.type === "text" || item.type === "label") && view.fonts?.[item.font] === void 0 && issue("names a font the BoundView has", ["elements", index, "font"]), item.type === "text" && view.fonts?.[item.font]?.glyphs === void 0 && issue("a bound value is drawn with its font's own glyphs", ["elements", index, "font"]), item.type === "image" && view.images?.[item.image] === void 0 && issue("names an image the BoundView has", ["elements", index, "image"]);
|
|
180
|
+
}), texts > limits.textElements && issue(`at most ${limits.textElements} text, label and signature elements`, ["elements"]), paths > limits.paths && issue(`at most ${limits.paths} characters of paths`, ["elements"]);
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
export {
|
|
184
|
+
BtpBoundViewSchema,
|
|
185
|
+
isPathData
|
|
186
|
+
};
|