@tileflow/static 0.1.0-alpha.3 → 0.1.0-alpha.30
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 +201 -0
- package/README.md +286 -7
- package/dist/chunk-6DWVCK64.js +406 -0
- package/dist/chunk-ALFVZ3PC.js +600 -0
- package/dist/client.d.ts +142 -0
- package/dist/client.js +382 -0
- package/dist/errors-CSz1xaDx.d.ts +23 -0
- package/dist/manifest.d.ts +545 -0
- package/dist/manifest.js +339 -0
- package/dist/overlays.d.ts +22 -0
- package/dist/overlays.js +176 -0
- package/dist/scene-contract-NFnIcQtt.d.ts +734 -0
- package/dist/scene.d.ts +290 -0
- package/dist/scene.js +40 -0
- package/package.json +29 -12
- package/dist/index.d.ts +0 -248
- package/dist/index.js +0 -396
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export { S as StaticMapError, a as StaticMapErrorResponse, s as staticMapErrorResponseSchema } from './errors-CSz1xaDx.js';
|
|
3
|
+
import { S as StaticAttributionPosition, a as StaticSceneInput } from './scene-contract-NFnIcQtt.js';
|
|
4
|
+
|
|
5
|
+
declare function stableStringify(value: unknown): string;
|
|
6
|
+
|
|
7
|
+
declare const STATIC_MAP_RESULT_MEDIA_TYPE = "application/vnd.tileflow.static-map-result+json;version=1";
|
|
8
|
+
|
|
9
|
+
type StaticMapAttributionEntry = Readonly<{
|
|
10
|
+
authority: 'platform-notice' | 'team-declared';
|
|
11
|
+
links: readonly Readonly<{
|
|
12
|
+
label: string;
|
|
13
|
+
url: string;
|
|
14
|
+
}>[];
|
|
15
|
+
text: string;
|
|
16
|
+
}>;
|
|
17
|
+
type StaticMapAttributionResult = Readonly<{
|
|
18
|
+
entries: readonly StaticMapAttributionEntry[];
|
|
19
|
+
mode: 'embedded' | 'external';
|
|
20
|
+
position: Exclude<StaticAttributionPosition, 'auto'> | null;
|
|
21
|
+
}>;
|
|
22
|
+
type StaticMapResultBase = Readonly<{
|
|
23
|
+
cached: boolean;
|
|
24
|
+
hash: string;
|
|
25
|
+
imageUrl: string;
|
|
26
|
+
remainingUnits: number | null;
|
|
27
|
+
status: 'ready';
|
|
28
|
+
}>;
|
|
29
|
+
type StaticMapImageResult = StaticMapResultBase & Readonly<{
|
|
30
|
+
operationId: null;
|
|
31
|
+
unitCost: 0;
|
|
32
|
+
}>;
|
|
33
|
+
type StaticMapHostedResult = StaticMapResultBase & Readonly<{
|
|
34
|
+
attribution: StaticMapAttributionResult;
|
|
35
|
+
operationId: string;
|
|
36
|
+
resultVersion: 1;
|
|
37
|
+
unitCost: 15;
|
|
38
|
+
}>;
|
|
39
|
+
type StaticMapResult = StaticMapImageResult | StaticMapHostedResult;
|
|
40
|
+
type StaticMapCreateOptions = {
|
|
41
|
+
apiKey?: string;
|
|
42
|
+
mapId?: string;
|
|
43
|
+
apiUrl?: string;
|
|
44
|
+
fetch?: typeof fetch;
|
|
45
|
+
idempotencyKey: string;
|
|
46
|
+
maxWaitMs?: number;
|
|
47
|
+
pollIntervalMs?: number;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
};
|
|
50
|
+
type StaticMapEndpointRequestOptions = Omit<StaticMapCreateOptions, 'apiUrl'> & {
|
|
51
|
+
createUrl: string;
|
|
52
|
+
};
|
|
53
|
+
type PreparedStaticMapRequest = Readonly<{
|
|
54
|
+
sceneKey: string;
|
|
55
|
+
}>;
|
|
56
|
+
declare const staticMapAttributionEntrySchema: z.ZodObject<{
|
|
57
|
+
authority: z.ZodEnum<{
|
|
58
|
+
"platform-notice": "platform-notice";
|
|
59
|
+
"team-declared": "team-declared";
|
|
60
|
+
}>;
|
|
61
|
+
links: z.ZodArray<z.ZodObject<{
|
|
62
|
+
label: z.ZodString;
|
|
63
|
+
url: z.ZodString;
|
|
64
|
+
}, z.core.$strict>>;
|
|
65
|
+
text: z.ZodString;
|
|
66
|
+
}, z.core.$strict>;
|
|
67
|
+
declare const staticMapAttributionResultSchema: z.ZodObject<{
|
|
68
|
+
entries: z.ZodArray<z.ZodObject<{
|
|
69
|
+
authority: z.ZodEnum<{
|
|
70
|
+
"platform-notice": "platform-notice";
|
|
71
|
+
"team-declared": "team-declared";
|
|
72
|
+
}>;
|
|
73
|
+
links: z.ZodArray<z.ZodObject<{
|
|
74
|
+
label: z.ZodString;
|
|
75
|
+
url: z.ZodString;
|
|
76
|
+
}, z.core.$strict>>;
|
|
77
|
+
text: z.ZodString;
|
|
78
|
+
}, z.core.$strict>>;
|
|
79
|
+
mode: z.ZodEnum<{
|
|
80
|
+
embedded: "embedded";
|
|
81
|
+
external: "external";
|
|
82
|
+
}>;
|
|
83
|
+
position: z.ZodNullable<z.ZodEnum<{
|
|
84
|
+
"bottom-left": "bottom-left";
|
|
85
|
+
"bottom-right": "bottom-right";
|
|
86
|
+
"top-left": "top-left";
|
|
87
|
+
"top-right": "top-right";
|
|
88
|
+
}>>;
|
|
89
|
+
}, z.core.$strict>;
|
|
90
|
+
declare const staticMapHostedResultSchema: z.ZodObject<{
|
|
91
|
+
cached: z.ZodBoolean;
|
|
92
|
+
hash: z.ZodString;
|
|
93
|
+
imageUrl: z.ZodString;
|
|
94
|
+
operationId: z.ZodString;
|
|
95
|
+
remainingUnits: z.ZodNullable<z.ZodNumber>;
|
|
96
|
+
status: z.ZodLiteral<"ready">;
|
|
97
|
+
unitCost: z.ZodLiteral<15>;
|
|
98
|
+
attribution: z.ZodObject<{
|
|
99
|
+
entries: z.ZodArray<z.ZodObject<{
|
|
100
|
+
authority: z.ZodEnum<{
|
|
101
|
+
"platform-notice": "platform-notice";
|
|
102
|
+
"team-declared": "team-declared";
|
|
103
|
+
}>;
|
|
104
|
+
links: z.ZodArray<z.ZodObject<{
|
|
105
|
+
label: z.ZodString;
|
|
106
|
+
url: z.ZodString;
|
|
107
|
+
}, z.core.$strict>>;
|
|
108
|
+
text: z.ZodString;
|
|
109
|
+
}, z.core.$strict>>;
|
|
110
|
+
mode: z.ZodEnum<{
|
|
111
|
+
embedded: "embedded";
|
|
112
|
+
external: "external";
|
|
113
|
+
}>;
|
|
114
|
+
position: z.ZodNullable<z.ZodEnum<{
|
|
115
|
+
"bottom-left": "bottom-left";
|
|
116
|
+
"bottom-right": "bottom-right";
|
|
117
|
+
"top-left": "top-left";
|
|
118
|
+
"top-right": "top-right";
|
|
119
|
+
}>>;
|
|
120
|
+
}, z.core.$strict>;
|
|
121
|
+
resultVersion: z.ZodLiteral<1>;
|
|
122
|
+
}, z.core.$strict>;
|
|
123
|
+
declare const staticMapProcessingResultSchema: z.ZodObject<{
|
|
124
|
+
operationId: z.ZodString;
|
|
125
|
+
retryAfterMs: z.ZodNumber;
|
|
126
|
+
status: z.ZodLiteral<"processing">;
|
|
127
|
+
}, z.core.$strict>;
|
|
128
|
+
|
|
129
|
+
declare function validateStaticMapIdempotencyKey(value: unknown): {
|
|
130
|
+
key: string;
|
|
131
|
+
ok: true;
|
|
132
|
+
} | {
|
|
133
|
+
error: string;
|
|
134
|
+
ok: false;
|
|
135
|
+
};
|
|
136
|
+
declare function createStaticMapIdempotencyKey(): string;
|
|
137
|
+
declare function prepareStaticMapRequest(scene: StaticSceneInput): PreparedStaticMapRequest;
|
|
138
|
+
declare function createStaticMap(scene: StaticSceneInput, options: StaticMapCreateOptions): Promise<StaticMapHostedResult>;
|
|
139
|
+
declare function precacheStaticMap(scene: StaticSceneInput, options: StaticMapCreateOptions): Promise<StaticMapHostedResult>;
|
|
140
|
+
declare function requestStaticMapUntilReady(request: PreparedStaticMapRequest, options: StaticMapEndpointRequestOptions): Promise<StaticMapHostedResult>;
|
|
141
|
+
|
|
142
|
+
export { type PreparedStaticMapRequest, STATIC_MAP_RESULT_MEDIA_TYPE, type StaticMapAttributionEntry, type StaticMapAttributionResult, type StaticMapCreateOptions, type StaticMapEndpointRequestOptions, type StaticMapHostedResult, type StaticMapImageResult, type StaticMapResult, createStaticMap, createStaticMapIdempotencyKey, precacheStaticMap, prepareStaticMapRequest, requestStaticMapUntilReady, stableStringify, staticMapAttributionEntrySchema, staticMapAttributionResultSchema, staticMapHostedResultSchema, staticMapProcessingResultSchema, validateStaticMapIdempotencyKey };
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StaticMapError,
|
|
3
|
+
StaticMapRequestError,
|
|
4
|
+
staticMapErrorResponseSchema,
|
|
5
|
+
staticMapRequestErrorResponseSchema,
|
|
6
|
+
validateStaticScene
|
|
7
|
+
} from "./chunk-ALFVZ3PC.js";
|
|
8
|
+
import {
|
|
9
|
+
isSafeHttpUrl,
|
|
10
|
+
jsonByteLength,
|
|
11
|
+
stableStringify
|
|
12
|
+
} from "./chunk-6DWVCK64.js";
|
|
13
|
+
|
|
14
|
+
// src/client.ts
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
var maxStaticMapResponseBytes = 64 * 1024;
|
|
17
|
+
var staticMapIdempotencyKeyPattern = /^[A-Za-z0-9][A-Za-z0-9._:-]{7,127}$/;
|
|
18
|
+
var STATIC_MAP_RESULT_MEDIA_TYPE = "application/vnd.tileflow.static-map-result+json;version=1";
|
|
19
|
+
var preparedStaticMapRequestBodies = /* @__PURE__ */ new WeakMap();
|
|
20
|
+
var staticMapReadyResultBaseSchema = z.object({
|
|
21
|
+
cached: z.boolean(),
|
|
22
|
+
hash: z.string().regex(/^[A-Za-z0-9_-]{43}$/),
|
|
23
|
+
imageUrl: z.string().trim().url().max(2048).refine(isSafeHttpUrl, { message: "Expected an http(s) URL without credentials" }),
|
|
24
|
+
operationId: z.string().min(20).max(68),
|
|
25
|
+
remainingUnits: z.number().int().nonnegative().nullable(),
|
|
26
|
+
status: z.literal("ready"),
|
|
27
|
+
unitCost: z.literal(15)
|
|
28
|
+
}).strict();
|
|
29
|
+
var safeAttributionTextSchema = z.string().min(1).max(16384).refine(hasNoControlCharacters, { message: "Attribution text contains control characters" });
|
|
30
|
+
var staticMapAttributionEntrySchema = z.object({
|
|
31
|
+
authority: z.enum(["platform-notice", "team-declared"]),
|
|
32
|
+
links: z.array(
|
|
33
|
+
z.object({
|
|
34
|
+
label: safeAttributionTextSchema,
|
|
35
|
+
url: z.string().trim().url().max(2048).refine(isSafeHttpUrl, {
|
|
36
|
+
message: "Expected an http(s) URL without credentials or a fragment"
|
|
37
|
+
})
|
|
38
|
+
}).strict()
|
|
39
|
+
).max(8),
|
|
40
|
+
text: safeAttributionTextSchema
|
|
41
|
+
}).strict();
|
|
42
|
+
var staticMapAttributionResultSchema = z.object({
|
|
43
|
+
entries: z.array(staticMapAttributionEntrySchema).max(18),
|
|
44
|
+
mode: z.enum(["embedded", "external"]),
|
|
45
|
+
position: z.enum(["top-left", "top-right", "bottom-left", "bottom-right"]).nullable()
|
|
46
|
+
}).strict().superRefine((result, context) => {
|
|
47
|
+
if (result.mode === "external" && result.position !== null || result.mode === "embedded" && result.position === null) {
|
|
48
|
+
context.addIssue({
|
|
49
|
+
code: "custom",
|
|
50
|
+
message: "Attribution position must match its mode",
|
|
51
|
+
path: ["position"]
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (jsonByteLength(result) > 32 * 1024) {
|
|
55
|
+
context.addIssue({ code: "custom", message: "Attribution result exceeds 32 KiB" });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
var staticMapHostedResultSchema = staticMapReadyResultBaseSchema.extend({
|
|
59
|
+
attribution: staticMapAttributionResultSchema,
|
|
60
|
+
resultVersion: z.literal(1)
|
|
61
|
+
}).strict();
|
|
62
|
+
var staticMapProcessingResultSchema = z.object({
|
|
63
|
+
operationId: z.string().min(20).max(68),
|
|
64
|
+
retryAfterMs: z.number().int().min(0).max(5e3),
|
|
65
|
+
status: z.literal("processing")
|
|
66
|
+
}).strict();
|
|
67
|
+
function validateStaticMapIdempotencyKey(value) {
|
|
68
|
+
if (typeof value !== "string" || !staticMapIdempotencyKeyPattern.test(value)) {
|
|
69
|
+
return {
|
|
70
|
+
error: "Idempotency key must contain 8-128 ASCII letters, digits, dot, underscore, colon, or hyphen and start with a letter or digit",
|
|
71
|
+
ok: false
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
return { key: value, ok: true };
|
|
75
|
+
}
|
|
76
|
+
function createStaticMapIdempotencyKey() {
|
|
77
|
+
const key = `static_${crypto.randomUUID()}`;
|
|
78
|
+
if (!validateStaticMapIdempotencyKey(key).ok) {
|
|
79
|
+
throw new Error("Unable to create a valid Tileflow Static Maps idempotency key");
|
|
80
|
+
}
|
|
81
|
+
return key;
|
|
82
|
+
}
|
|
83
|
+
function prepareStaticMapRequest(scene) {
|
|
84
|
+
const validation = validateStaticScene(scene);
|
|
85
|
+
if (!validation.ok) {
|
|
86
|
+
if (isStaticMapRequestFailure(validation)) {
|
|
87
|
+
const { ok: _ok, ...response } = validation;
|
|
88
|
+
throw new StaticMapRequestError(response);
|
|
89
|
+
}
|
|
90
|
+
throw new Error(`Invalid Tileflow static scene: ${validation.error}`);
|
|
91
|
+
}
|
|
92
|
+
const prepared = Object.freeze({
|
|
93
|
+
sceneKey: stableStringify(validation.scene)
|
|
94
|
+
});
|
|
95
|
+
preparedStaticMapRequestBodies.set(prepared, JSON.stringify(validation.scene));
|
|
96
|
+
return prepared;
|
|
97
|
+
}
|
|
98
|
+
async function createStaticMap(scene, options) {
|
|
99
|
+
return requestStaticMap(scene, options, "/v1/static/maps");
|
|
100
|
+
}
|
|
101
|
+
async function precacheStaticMap(scene, options) {
|
|
102
|
+
return requestStaticMap(scene, options, "/v1/static/maps/precache");
|
|
103
|
+
}
|
|
104
|
+
async function requestStaticMap(scene, options, path) {
|
|
105
|
+
const preparedRequest = prepareStaticMapRequest(scene);
|
|
106
|
+
const { apiUrl = "https://api.tileflow.dev", ...requestOptions } = options;
|
|
107
|
+
return requestStaticMapUntilReady(preparedRequest, {
|
|
108
|
+
...requestOptions,
|
|
109
|
+
createUrl: `${normalizeUrl(apiUrl)}${path}`
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
async function requestStaticMapUntilReady(request, options) {
|
|
113
|
+
const body = preparedStaticMapRequestBodies.get(request);
|
|
114
|
+
if (body === void 0) {
|
|
115
|
+
throw new Error("Tileflow static map request must be created with prepareStaticMapRequest");
|
|
116
|
+
}
|
|
117
|
+
const idempotency = validateStaticMapIdempotencyKey(options.idempotencyKey);
|
|
118
|
+
if (!idempotency.ok) {
|
|
119
|
+
throw new Error(`Invalid Tileflow Static Maps idempotency key: ${idempotency.error}`);
|
|
120
|
+
}
|
|
121
|
+
const maxWaitMs = boundedClientDuration(options.maxWaitMs, 3e4, 100, 12e4, "maxWaitMs");
|
|
122
|
+
const pollIntervalMs = boundedClientDuration(
|
|
123
|
+
options.pollIntervalMs,
|
|
124
|
+
500,
|
|
125
|
+
0,
|
|
126
|
+
5e3,
|
|
127
|
+
"pollIntervalMs"
|
|
128
|
+
);
|
|
129
|
+
const fetcher = options.fetch ?? fetch;
|
|
130
|
+
const createUrl = normalizeStaticMapEndpointUrl(options.createUrl);
|
|
131
|
+
const headers = {
|
|
132
|
+
Accept: STATIC_MAP_RESULT_MEDIA_TYPE,
|
|
133
|
+
"Content-Type": "application/json",
|
|
134
|
+
"Idempotency-Key": idempotency.key
|
|
135
|
+
};
|
|
136
|
+
if (options.apiKey) {
|
|
137
|
+
headers.Authorization = `Bearer ${options.apiKey}`;
|
|
138
|
+
}
|
|
139
|
+
if (options.mapId !== void 0) {
|
|
140
|
+
if (!/^map_[A-Za-z0-9_-]{16}$/u.test(options.mapId)) {
|
|
141
|
+
throw new Error("Invalid Tileflow Map ID.");
|
|
142
|
+
}
|
|
143
|
+
headers["X-Tileflow-Map-Id"] = options.mapId;
|
|
144
|
+
}
|
|
145
|
+
return runWithinStaticMapBudget(maxWaitMs, options.signal, async (signal) => {
|
|
146
|
+
let operationId = null;
|
|
147
|
+
while (true) {
|
|
148
|
+
const response = await fetcher(createUrl, {
|
|
149
|
+
body,
|
|
150
|
+
headers,
|
|
151
|
+
method: "POST",
|
|
152
|
+
signal
|
|
153
|
+
});
|
|
154
|
+
throwIfAborted(signal);
|
|
155
|
+
if (!response.ok) {
|
|
156
|
+
const errorBody = await readStaticMapErrorBody(response, 8 * 1024);
|
|
157
|
+
throwIfAborted(signal);
|
|
158
|
+
if (response.status === 422) {
|
|
159
|
+
const requestError = staticMapRequestErrorResponseSchema.safeParse(errorBody);
|
|
160
|
+
if (requestError.success) {
|
|
161
|
+
throw new StaticMapRequestError(requestError.data, response.status);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const structured = staticMapErrorResponseSchema.safeParse(errorBody);
|
|
165
|
+
if (structured.success) {
|
|
166
|
+
throw new StaticMapError(structured.data, response.status);
|
|
167
|
+
}
|
|
168
|
+
throw new Error(`Tileflow static map failed (${response.status}).`);
|
|
169
|
+
}
|
|
170
|
+
const json = await readJsonResponse(response);
|
|
171
|
+
throwIfAborted(signal);
|
|
172
|
+
if (response.status !== 202) {
|
|
173
|
+
const parsed = staticMapHostedResultSchema.safeParse(json);
|
|
174
|
+
if (!parsed.success) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
`Tileflow static map returned an invalid response: ${parsed.error.message}`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
assertStaticMapOperationIdentity(operationId, parsed.data.operationId);
|
|
180
|
+
return parsed.data;
|
|
181
|
+
}
|
|
182
|
+
const pending = staticMapProcessingResultSchema.safeParse(json);
|
|
183
|
+
if (!pending.success) {
|
|
184
|
+
throw new Error(
|
|
185
|
+
`Tileflow static map returned an invalid response: ${pending.error.message}`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
assertStaticMapOperationIdentity(operationId, pending.data.operationId);
|
|
189
|
+
operationId ??= pending.data.operationId;
|
|
190
|
+
await delay(Math.max(pollIntervalMs, pending.data.retryAfterMs), signal);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
function hasNoControlCharacters(value) {
|
|
195
|
+
return !Array.from(value).some((character) => {
|
|
196
|
+
const codePoint = character.codePointAt(0) ?? 0;
|
|
197
|
+
return codePoint <= 31 || codePoint === 127;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
function boundedClientDuration(value, fallback, minimum, maximum, name) {
|
|
201
|
+
const resolved = value ?? fallback;
|
|
202
|
+
if (!Number.isSafeInteger(resolved) || resolved < minimum || resolved > maximum) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`Tileflow Static Maps ${name} must be an integer from ${minimum} to ${maximum}`
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
return resolved;
|
|
208
|
+
}
|
|
209
|
+
async function readJsonResponse(response) {
|
|
210
|
+
const source = await readBoundedResponseText(response, maxStaticMapResponseBytes, "64 KiB");
|
|
211
|
+
try {
|
|
212
|
+
return JSON.parse(source);
|
|
213
|
+
} catch {
|
|
214
|
+
throw new Error("Tileflow static map returned an invalid response: expected JSON");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
async function readStaticMapErrorBody(response, maximumBytes) {
|
|
218
|
+
try {
|
|
219
|
+
const source = await readBoundedResponseText(response, maximumBytes, `${maximumBytes} bytes`);
|
|
220
|
+
return JSON.parse(source);
|
|
221
|
+
} catch {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function isStaticMapRequestFailure(value) {
|
|
226
|
+
if (!("code" in value)) return false;
|
|
227
|
+
const { ok: _ok, ...response } = value;
|
|
228
|
+
return staticMapRequestErrorResponseSchema.safeParse(response).success;
|
|
229
|
+
}
|
|
230
|
+
async function readBoundedResponseText(response, maximumBytes, displayLimit) {
|
|
231
|
+
if (!response.body) return "";
|
|
232
|
+
const reader = response.body.getReader();
|
|
233
|
+
const chunks = [];
|
|
234
|
+
let byteLength = 0;
|
|
235
|
+
while (true) {
|
|
236
|
+
const { done, value } = await reader.read();
|
|
237
|
+
if (done) break;
|
|
238
|
+
byteLength += value.byteLength;
|
|
239
|
+
if (byteLength > maximumBytes) {
|
|
240
|
+
await reader.cancel();
|
|
241
|
+
throw new Error(
|
|
242
|
+
`Tileflow static map returned an invalid response: response exceeded ${displayLimit}`
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
chunks.push(value);
|
|
246
|
+
}
|
|
247
|
+
const bytes = new Uint8Array(byteLength);
|
|
248
|
+
let offset = 0;
|
|
249
|
+
for (const chunk of chunks) {
|
|
250
|
+
bytes.set(chunk, offset);
|
|
251
|
+
offset += chunk.byteLength;
|
|
252
|
+
}
|
|
253
|
+
try {
|
|
254
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
255
|
+
} catch {
|
|
256
|
+
throw new Error("Tileflow static map returned an invalid response: expected UTF-8 JSON");
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
async function runWithinStaticMapBudget(maxWaitMs, externalSignal, operation) {
|
|
260
|
+
const controller = new AbortController();
|
|
261
|
+
const timeoutError = new Error(`Tileflow static map timed out after ${maxWaitMs}ms`);
|
|
262
|
+
const abortFromCaller = () => controller.abort(externalSignal?.reason);
|
|
263
|
+
if (externalSignal?.aborted) {
|
|
264
|
+
abortFromCaller();
|
|
265
|
+
} else {
|
|
266
|
+
externalSignal?.addEventListener("abort", abortFromCaller, { once: true });
|
|
267
|
+
}
|
|
268
|
+
const timeout = setTimeout(() => controller.abort(timeoutError), maxWaitMs);
|
|
269
|
+
try {
|
|
270
|
+
throwIfAborted(controller.signal);
|
|
271
|
+
const pending = operation(controller.signal);
|
|
272
|
+
return await raceWithAbort(pending, controller.signal);
|
|
273
|
+
} finally {
|
|
274
|
+
clearTimeout(timeout);
|
|
275
|
+
externalSignal?.removeEventListener("abort", abortFromCaller);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function delay(milliseconds, signal) {
|
|
279
|
+
if (milliseconds === 0) {
|
|
280
|
+
throwIfAborted(signal);
|
|
281
|
+
return Promise.resolve();
|
|
282
|
+
}
|
|
283
|
+
return new Promise((resolve, reject) => {
|
|
284
|
+
const timeout = setTimeout(() => {
|
|
285
|
+
signal.removeEventListener("abort", onAbort);
|
|
286
|
+
resolve();
|
|
287
|
+
}, milliseconds);
|
|
288
|
+
const onAbort = () => {
|
|
289
|
+
clearTimeout(timeout);
|
|
290
|
+
reject(abortReason(signal));
|
|
291
|
+
};
|
|
292
|
+
if (signal.aborted) {
|
|
293
|
+
onAbort();
|
|
294
|
+
} else {
|
|
295
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function raceWithAbort(operation, signal) {
|
|
300
|
+
if (signal.aborted) return Promise.reject(abortReason(signal));
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
const onAbort = () => {
|
|
303
|
+
cleanup();
|
|
304
|
+
reject(abortReason(signal));
|
|
305
|
+
};
|
|
306
|
+
const cleanup = () => signal.removeEventListener("abort", onAbort);
|
|
307
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
308
|
+
operation.then(
|
|
309
|
+
(value) => {
|
|
310
|
+
cleanup();
|
|
311
|
+
resolve(value);
|
|
312
|
+
},
|
|
313
|
+
(error) => {
|
|
314
|
+
cleanup();
|
|
315
|
+
reject(error);
|
|
316
|
+
}
|
|
317
|
+
);
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function throwIfAborted(signal) {
|
|
321
|
+
if (signal.aborted) throw abortReason(signal);
|
|
322
|
+
}
|
|
323
|
+
function abortReason(signal) {
|
|
324
|
+
if (signal.reason instanceof Error) return signal.reason;
|
|
325
|
+
const error = new Error("Tileflow static map request was aborted");
|
|
326
|
+
error.name = "AbortError";
|
|
327
|
+
return error;
|
|
328
|
+
}
|
|
329
|
+
function assertStaticMapOperationIdentity(expected, actual) {
|
|
330
|
+
if (expected !== null && actual !== expected) {
|
|
331
|
+
throw new Error("Tileflow static map changed operation identity while polling");
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
function normalizeUrl(value) {
|
|
335
|
+
const url = new URL(value);
|
|
336
|
+
if (!isSafeHttpUrl(value) || url.search) {
|
|
337
|
+
throw new TypeError(
|
|
338
|
+
"Tileflow Static Maps apiUrl must be HTTP(S) without credentials, query, or fragment"
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
return url.toString().replace(/\/+$/, "");
|
|
342
|
+
}
|
|
343
|
+
function normalizeStaticMapEndpointUrl(value) {
|
|
344
|
+
if (value.startsWith("/")) {
|
|
345
|
+
if (value.startsWith("//") || value.length > 2048 || /[\\?#]/u.test(value) || hasControlCharacter(value)) {
|
|
346
|
+
throw new TypeError(
|
|
347
|
+
"Tileflow Static Maps createUrl must be HTTP(S) without credentials, query, or fragment or a safe root-relative path"
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
return value;
|
|
351
|
+
}
|
|
352
|
+
const url = new URL(value);
|
|
353
|
+
if (!isSafeHttpUrl(value) || url.search) {
|
|
354
|
+
throw new TypeError(
|
|
355
|
+
"Tileflow Static Maps createUrl must be HTTP(S) without credentials, query, or fragment or a safe root-relative path"
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return url.toString();
|
|
359
|
+
}
|
|
360
|
+
function hasControlCharacter(value) {
|
|
361
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
362
|
+
const codePoint = value.charCodeAt(index);
|
|
363
|
+
if (codePoint <= 31 || codePoint === 127) return true;
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
export {
|
|
368
|
+
STATIC_MAP_RESULT_MEDIA_TYPE,
|
|
369
|
+
StaticMapError,
|
|
370
|
+
createStaticMap,
|
|
371
|
+
createStaticMapIdempotencyKey,
|
|
372
|
+
precacheStaticMap,
|
|
373
|
+
prepareStaticMapRequest,
|
|
374
|
+
requestStaticMapUntilReady,
|
|
375
|
+
stableStringify,
|
|
376
|
+
staticMapAttributionEntrySchema,
|
|
377
|
+
staticMapAttributionResultSchema,
|
|
378
|
+
staticMapErrorResponseSchema,
|
|
379
|
+
staticMapHostedResultSchema,
|
|
380
|
+
staticMapProcessingResultSchema,
|
|
381
|
+
validateStaticMapIdempotencyKey
|
|
382
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
declare const staticMapErrorResponseSchema: z.ZodObject<{
|
|
4
|
+
code: z.ZodOptional<z.ZodString>;
|
|
5
|
+
error: z.ZodString;
|
|
6
|
+
remainingUnits: z.ZodOptional<z.ZodNumber>;
|
|
7
|
+
requestId: z.ZodOptional<z.ZodString>;
|
|
8
|
+
requiredPlan: z.ZodOptional<z.ZodString>;
|
|
9
|
+
retryable: z.ZodOptional<z.ZodBoolean>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
type StaticMapErrorResponse = z.infer<typeof staticMapErrorResponseSchema>;
|
|
12
|
+
declare class StaticMapError extends Error {
|
|
13
|
+
readonly code: string | null;
|
|
14
|
+
readonly remainingUnits: number | null;
|
|
15
|
+
readonly requestId: string | null;
|
|
16
|
+
readonly requiredPlan: string | null;
|
|
17
|
+
readonly response: StaticMapErrorResponse;
|
|
18
|
+
readonly retryable: boolean | null;
|
|
19
|
+
readonly status: number;
|
|
20
|
+
constructor(response: StaticMapErrorResponse, status: number);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { StaticMapError as S, type StaticMapErrorResponse as a, staticMapErrorResponseSchema as s };
|