@pgxsinkit/pgwasm 0.5.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/LICENSE-APACHE-2.0 +202 -0
- package/NOTICE +111 -0
- package/README.md +56 -0
- package/dist/build/index.d.ts +8 -0
- package/dist/build/index.d.ts.map +1 -0
- package/dist/build/index.js +21 -0
- package/dist/build/index.js.map +9 -0
- package/dist/build/seam.d.ts +174 -0
- package/dist/build/seam.d.ts.map +1 -0
- package/dist/chunk-0w227vw2.js +233 -0
- package/dist/chunk-0w227vw2.js.map +11 -0
- package/dist/chunk-74gcrk47.js +563 -0
- package/dist/chunk-74gcrk47.js.map +14 -0
- package/dist/chunk-at1py5s5.js +373 -0
- package/dist/chunk-at1py5s5.js.map +12 -0
- package/dist/chunk-cpsnmzrf.js +126 -0
- package/dist/chunk-cpsnmzrf.js.map +10 -0
- package/dist/chunk-d82gg1hx.js +306 -0
- package/dist/chunk-d82gg1hx.js.map +10 -0
- package/dist/chunk-gjf102c9.js +1005 -0
- package/dist/chunk-gjf102c9.js.map +16 -0
- package/dist/chunk-kb3rsr7n.js +19 -0
- package/dist/chunk-kb3rsr7n.js.map +9 -0
- package/dist/chunk-tdvrvgt5.js +83 -0
- package/dist/chunk-tdvrvgt5.js.map +11 -0
- package/dist/chunk-z0c40tja.js +50 -0
- package/dist/chunk-z0c40tja.js.map +10 -0
- package/dist/core/data-dir-archive.d.ts +17 -0
- package/dist/core/data-dir-archive.d.ts.map +1 -0
- package/dist/core/internals.d.ts +59 -0
- package/dist/core/internals.d.ts.map +1 -0
- package/dist/core/marker.d.ts +35 -0
- package/dist/core/marker.d.ts.map +1 -0
- package/dist/core/mutex.d.ts +9 -0
- package/dist/core/mutex.d.ts.map +1 -0
- package/dist/core/names.d.ts +11 -0
- package/dist/core/names.d.ts.map +1 -0
- package/dist/core/parse.d.ts +11 -0
- package/dist/core/parse.d.ts.map +1 -0
- package/dist/core/pgwasm.d.ts +53 -0
- package/dist/core/pgwasm.d.ts.map +1 -0
- package/dist/core/storage.d.ts +14 -0
- package/dist/core/storage.d.ts.map +1 -0
- package/dist/create.d.ts +10 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/drizzle/codecs.d.ts +9 -0
- package/dist/drizzle/codecs.d.ts.map +1 -0
- package/dist/drizzle/driver.d.ts +50 -0
- package/dist/drizzle/driver.d.ts.map +1 -0
- package/dist/drizzle/index.d.ts +4 -0
- package/dist/drizzle/index.d.ts.map +1 -0
- package/dist/drizzle/index.js +261 -0
- package/dist/drizzle/index.js.map +12 -0
- package/dist/drizzle/session.d.ts +39 -0
- package/dist/drizzle/session.d.ts.map +1 -0
- package/dist/errors.d.ts +115 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fs/base-filesystem.d.ts +118 -0
- package/dist/fs/base-filesystem.d.ts.map +1 -0
- package/dist/fs/index.d.ts +2 -0
- package/dist/fs/index.d.ts.map +1 -0
- package/dist/fs/index.js +11 -0
- package/dist/fs/index.js.map +9 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +9 -0
- package/dist/interface.d.ts +165 -0
- package/dist/interface.d.ts.map +1 -0
- package/dist/live/debounce-mutex.d.ts +6 -0
- package/dist/live/debounce-mutex.d.ts.map +1 -0
- package/dist/live/format-query.d.ts +8 -0
- package/dist/live/format-query.d.ts.map +1 -0
- package/dist/live/index.d.ts +9 -0
- package/dist/live/index.d.ts.map +1 -0
- package/dist/live/index.js +601 -0
- package/dist/live/index.js.map +12 -0
- package/dist/live/interface.d.ts +86 -0
- package/dist/live/interface.d.ts.map +1 -0
- package/dist/opfs/broker/client.d.ts +163 -0
- package/dist/opfs/broker/client.d.ts.map +1 -0
- package/dist/opfs/broker/protocol.d.ts +326 -0
- package/dist/opfs/broker/protocol.d.ts.map +1 -0
- package/dist/opfs/broker/server.d.ts +84 -0
- package/dist/opfs/broker/server.d.ts.map +1 -0
- package/dist/opfs/core/checksum.d.ts +3 -0
- package/dist/opfs/core/checksum.d.ts.map +1 -0
- package/dist/opfs/core/codec.d.ts +73 -0
- package/dist/opfs/core/codec.d.ts.map +1 -0
- package/dist/opfs/core/errors.d.ts +80 -0
- package/dist/opfs/core/errors.d.ts.map +1 -0
- package/dist/opfs/core/limits.d.ts +35 -0
- package/dist/opfs/core/limits.d.ts.map +1 -0
- package/dist/opfs/core/memory-port.d.ts +51 -0
- package/dist/opfs/core/memory-port.d.ts.map +1 -0
- package/dist/opfs/core/metadata-store.d.ts +19 -0
- package/dist/opfs/core/metadata-store.d.ts.map +1 -0
- package/dist/opfs/core/mounted-vfs.d.ts +119 -0
- package/dist/opfs/core/mounted-vfs.d.ts.map +1 -0
- package/dist/opfs/core/path.d.ts +14 -0
- package/dist/opfs/core/path.d.ts.map +1 -0
- package/dist/opfs/core/port.d.ts +30 -0
- package/dist/opfs/core/port.d.ts.map +1 -0
- package/dist/opfs/core/repacked-vfs.d.ts +138 -0
- package/dist/opfs/core/repacked-vfs.d.ts.map +1 -0
- package/dist/opfs/core/state-machine.d.ts +296 -0
- package/dist/opfs/core/state-machine.d.ts.map +1 -0
- package/dist/opfs/create.d.ts +50 -0
- package/dist/opfs/create.d.ts.map +1 -0
- package/dist/opfs/file-port.d.ts +14 -0
- package/dist/opfs/file-port.d.ts.map +1 -0
- package/dist/opfs/index.d.ts +37 -0
- package/dist/opfs/index.d.ts.map +1 -0
- package/dist/opfs/index.js +6671 -0
- package/dist/opfs/index.js.map +28 -0
- package/dist/opfs/opfs-port.d.ts +34 -0
- package/dist/opfs/opfs-port.d.ts.map +1 -0
- package/dist/opfs/opfs-repacked-fs.d.ts +55 -0
- package/dist/opfs/opfs-repacked-fs.d.ts.map +1 -0
- package/dist/opfs/wasi/preview1.d.ts +228 -0
- package/dist/opfs/wasi/preview1.d.ts.map +1 -0
- package/dist/protocol/index.d.ts +15 -0
- package/dist/protocol/index.d.ts.map +1 -0
- package/dist/protocol/index.js +23 -0
- package/dist/protocol/index.js.map +10 -0
- package/dist/protocol/wire/buffer-reader.d.ts +13 -0
- package/dist/protocol/wire/buffer-reader.d.ts.map +1 -0
- package/dist/protocol/wire/buffer-writer.d.ts +12 -0
- package/dist/protocol/wire/buffer-writer.d.ts.map +1 -0
- package/dist/protocol/wire/index.d.ts +5 -0
- package/dist/protocol/wire/index.d.ts.map +1 -0
- package/dist/protocol/wire/messages.d.ts +194 -0
- package/dist/protocol/wire/messages.d.ts.map +1 -0
- package/dist/protocol/wire/parser.d.ts +12 -0
- package/dist/protocol/wire/parser.d.ts.map +1 -0
- package/dist/protocol/wire/serializer.d.ts +61 -0
- package/dist/protocol/wire/serializer.d.ts.map +1 -0
- package/dist/protocol/wire/string-utils.d.ts +6 -0
- package/dist/protocol/wire/string-utils.d.ts.map +1 -0
- package/dist/protocol/wire/types.d.ts +7 -0
- package/dist/protocol/wire/types.d.ts.map +1 -0
- package/dist/tar/gzip.d.ts +7 -0
- package/dist/tar/gzip.d.ts.map +1 -0
- package/dist/tar/tar.d.ts +39 -0
- package/dist/tar/tar.d.ts.map +1 -0
- package/dist/templating.d.ts +42 -0
- package/dist/templating.d.ts.map +1 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +69 -0
- package/skills/opfs/SKILL.md +138 -0
- package/src/build/index.ts +23 -0
- package/src/build/seam.ts +179 -0
- package/src/core/data-dir-archive.ts +68 -0
- package/src/core/internals.ts +87 -0
- package/src/core/marker.ts +94 -0
- package/src/core/mutex.ts +16 -0
- package/src/core/names.ts +26 -0
- package/src/core/parse.ts +79 -0
- package/src/core/pgwasm.ts +876 -0
- package/src/core/storage.ts +70 -0
- package/src/create.ts +134 -0
- package/src/drizzle/codecs.ts +114 -0
- package/src/drizzle/driver.ts +141 -0
- package/src/drizzle/index.ts +15 -0
- package/src/drizzle/session.ts +133 -0
- package/src/errors.ts +201 -0
- package/src/fs/base-filesystem.ts +149 -0
- package/src/fs/index.ts +7 -0
- package/src/index.ts +44 -0
- package/src/interface.ts +186 -0
- package/src/live/debounce-mutex.ts +45 -0
- package/src/live/format-query.ts +47 -0
- package/src/live/index.ts +707 -0
- package/src/live/interface.ts +109 -0
- package/src/opfs/broker/client.ts +482 -0
- package/src/opfs/broker/protocol.ts +624 -0
- package/src/opfs/broker/server.ts +522 -0
- package/src/opfs/core/checksum.ts +85 -0
- package/src/opfs/core/codec.ts +1051 -0
- package/src/opfs/core/errors.ts +134 -0
- package/src/opfs/core/limits.ts +68 -0
- package/src/opfs/core/memory-port.ts +459 -0
- package/src/opfs/core/metadata-store.ts +512 -0
- package/src/opfs/core/mounted-vfs.ts +407 -0
- package/src/opfs/core/path.ts +82 -0
- package/src/opfs/core/port.ts +114 -0
- package/src/opfs/core/repacked-vfs.ts +1464 -0
- package/src/opfs/core/state-machine.ts +1699 -0
- package/src/opfs/create.ts +125 -0
- package/src/opfs/file-port.ts +183 -0
- package/src/opfs/index.ts +113 -0
- package/src/opfs/opfs-port.ts +90 -0
- package/src/opfs/opfs-repacked-fs.ts +210 -0
- package/src/opfs/wasi/preview1.ts +1016 -0
- package/src/protocol/index.ts +24 -0
- package/src/protocol/wire/buffer-reader.ts +60 -0
- package/src/protocol/wire/buffer-writer.ts +93 -0
- package/src/protocol/wire/index.ts +15 -0
- package/src/protocol/wire/messages.ts +332 -0
- package/src/protocol/wire/parser.ts +373 -0
- package/src/protocol/wire/serializer.ts +233 -0
- package/src/protocol/wire/string-utils.ts +18 -0
- package/src/protocol/wire/types.ts +12 -0
- package/src/tar/gzip.ts +36 -0
- package/src/tar/tar.ts +253 -0
- package/src/templating.ts +103 -0
- package/src/types.ts +283 -0
package/src/tar/tar.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal ustar codec: regular files and directories, which is everything a Postgres data
|
|
3
|
+
* directory and an npm tarball hold.
|
|
4
|
+
*
|
|
5
|
+
* Writing produces the layout pgwasm's Store backups have always had: a 512-byte ustar header per
|
|
6
|
+
* entry, the name in the `name` field when it fits (99 bytes), the payload padded to a record, and
|
|
7
|
+
* two zero records at the end. Reading also accepts what other writers add around that: a name
|
|
8
|
+
* split into `prefix` + `name`, PAX extended headers (`x`, per entry; `g`, global and ignored) and GNU
|
|
9
|
+
* long names (`L`). Links, devices and FIFOs are refused rather than skipped, so an archive with
|
|
10
|
+
* content this codec cannot represent fails loudly instead of restoring partially.
|
|
11
|
+
*
|
|
12
|
+
* Reading is strict about completeness, for the same reason: a data directory restored from part of
|
|
13
|
+
* a backup is a corrupt database. A header or payload cut short, a size that is not a number, a
|
|
14
|
+
* header whose checksum fails and an archive without its two zero end records are all refused. The
|
|
15
|
+
* end records are required because every writer whose archives reach this reader writes them: every
|
|
16
|
+
* PGlite Store backup came from tinytar's `tar()`, which always appends them (checked for the fork's
|
|
17
|
+
* dumps and the prepopulated data directory), as do npm's tarballs and the extension bundles.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** One archive member. */
|
|
21
|
+
export interface TarEntry {
|
|
22
|
+
/** The member's path as stored, `/`-separated. */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
readonly type: "file" | "directory";
|
|
25
|
+
/** Permission bits (the `0o7777` part of the mode). */
|
|
26
|
+
readonly mode: number;
|
|
27
|
+
/** Modification time, whole seconds since the epoch. */
|
|
28
|
+
readonly mtimeSeconds: number;
|
|
29
|
+
/** The file's bytes; empty for a directory. */
|
|
30
|
+
readonly data: Uint8Array;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** An archive this codec cannot read or represent. */
|
|
34
|
+
export class TarFormatError extends Error {
|
|
35
|
+
override readonly name = "TarFormatError";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const RECORD = 512;
|
|
39
|
+
const NAME_LENGTH = 100;
|
|
40
|
+
const PREFIX_LENGTH = 155;
|
|
41
|
+
const encoder = new TextEncoder();
|
|
42
|
+
const decoder = new TextDecoder();
|
|
43
|
+
|
|
44
|
+
/** Serialize entries into an uncompressed ustar archive. */
|
|
45
|
+
export function writeTar(entries: readonly TarEntry[]): Uint8Array {
|
|
46
|
+
let total = RECORD * 2;
|
|
47
|
+
for (const entry of entries) total += RECORD + padded(entry.type === "file" ? entry.data.byteLength : 0);
|
|
48
|
+
const archive = new Uint8Array(total);
|
|
49
|
+
let offset = 0;
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
const size = entry.type === "file" ? entry.data.byteLength : 0;
|
|
52
|
+
writeHeader(archive, offset, entry, size);
|
|
53
|
+
offset += RECORD;
|
|
54
|
+
if (size > 0) archive.set(entry.data, offset);
|
|
55
|
+
offset += padded(size);
|
|
56
|
+
}
|
|
57
|
+
return archive;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Parse an uncompressed ustar archive. Throws {@link TarFormatError} for anything it cannot represent. */
|
|
61
|
+
export function readTar(archive: Uint8Array): TarEntry[] {
|
|
62
|
+
const entries: TarEntry[] = [];
|
|
63
|
+
let offset = 0;
|
|
64
|
+
let pendingName: string | undefined;
|
|
65
|
+
let pendingSize: number | undefined;
|
|
66
|
+
for (;;) {
|
|
67
|
+
if (offset === archive.byteLength) {
|
|
68
|
+
throw new TarFormatError(
|
|
69
|
+
`tar archive ends at byte ${offset} without its end-of-archive records: it is truncated`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (offset + RECORD > archive.byteLength) {
|
|
73
|
+
throw new TarFormatError(`tar archive ends inside the header at byte ${offset}: it is truncated`);
|
|
74
|
+
}
|
|
75
|
+
const header = archive.subarray(offset, offset + RECORD);
|
|
76
|
+
if (isZeroRecord(header)) {
|
|
77
|
+
const second = archive.subarray(offset + RECORD, offset + 2 * RECORD);
|
|
78
|
+
if (second.byteLength < RECORD || !isZeroRecord(second)) {
|
|
79
|
+
throw new TarFormatError(
|
|
80
|
+
`tar archive has one end-of-archive record at byte ${offset}, not two: it is truncated or damaged`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (pendingName !== undefined || pendingSize !== undefined) {
|
|
84
|
+
throw new TarFormatError(`tar archive ends after an extended header with no member for it`);
|
|
85
|
+
}
|
|
86
|
+
return entries;
|
|
87
|
+
}
|
|
88
|
+
verifyChecksum(header, offset);
|
|
89
|
+
const typeflag = String.fromCharCode(header[156] ?? 0);
|
|
90
|
+
const declaredSize = readOctal(header, 124, 12, offset);
|
|
91
|
+
const size = pendingSize ?? declaredSize;
|
|
92
|
+
const dataStart = offset + RECORD;
|
|
93
|
+
if (dataStart + padded(size) > archive.byteLength) {
|
|
94
|
+
throw new TarFormatError(`tar member at byte ${offset} runs past the end of the archive: it is truncated`);
|
|
95
|
+
}
|
|
96
|
+
const data = archive.subarray(dataStart, dataStart + size);
|
|
97
|
+
offset = dataStart + padded(size);
|
|
98
|
+
|
|
99
|
+
if (typeflag === "x") {
|
|
100
|
+
const pax = readPax(data, offset);
|
|
101
|
+
pendingName = pax.path ?? pendingName;
|
|
102
|
+
pendingSize = pax.size;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (typeflag === "g") continue;
|
|
106
|
+
if (typeflag === "L") {
|
|
107
|
+
pendingName = readCString(data, 0, data.byteLength);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const name = pendingName ?? joinPrefix(header);
|
|
112
|
+
pendingName = undefined;
|
|
113
|
+
pendingSize = undefined;
|
|
114
|
+
const mode = readOctal(header, 100, 8, offset) & 0o7777;
|
|
115
|
+
const mtimeSeconds = readOctal(header, 136, 12, offset);
|
|
116
|
+
if (typeflag === "0" || typeflag === "\0" || typeflag === "7") {
|
|
117
|
+
entries.push({ name, type: "file", mode, mtimeSeconds, data: data.slice() });
|
|
118
|
+
} else if (typeflag === "5") {
|
|
119
|
+
entries.push({ name, type: "directory", mode, mtimeSeconds, data: new Uint8Array(0) });
|
|
120
|
+
} else {
|
|
121
|
+
throw new TarFormatError(`tar member "${name}" has unsupported type "${typeflag}"`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function padded(size: number): number {
|
|
127
|
+
return Math.ceil(size / RECORD) * RECORD;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function writeHeader(archive: Uint8Array, offset: number, entry: TarEntry, size: number): void {
|
|
131
|
+
const [prefix, rest] = splitName(entry.name);
|
|
132
|
+
writeString(archive, offset + 0, NAME_LENGTH, rest);
|
|
133
|
+
writeOctal(archive, offset + 100, 8, entry.mode & 0o7777);
|
|
134
|
+
writeOctal(archive, offset + 108, 8, 0);
|
|
135
|
+
writeOctal(archive, offset + 116, 8, 0);
|
|
136
|
+
writeOctal(archive, offset + 124, 12, size);
|
|
137
|
+
writeOctal(archive, offset + 136, 12, Math.max(0, Math.floor(entry.mtimeSeconds)));
|
|
138
|
+
archive[offset + 156] = (entry.type === "directory" ? "5" : "0").charCodeAt(0);
|
|
139
|
+
writeString(archive, offset + 257, 6, "ustar");
|
|
140
|
+
writeString(archive, offset + 263, 2, "00", false);
|
|
141
|
+
writeString(archive, offset + 345, PREFIX_LENGTH, prefix);
|
|
142
|
+
// The checksum is computed with its own field read as eight spaces, then stored as six octal
|
|
143
|
+
// digits, a NUL and a space.
|
|
144
|
+
archive.fill(0x20, offset + 148, offset + 156);
|
|
145
|
+
let sum = 0;
|
|
146
|
+
for (let index = offset; index < offset + RECORD; index++) sum += archive[index] ?? 0;
|
|
147
|
+
archive.set(encoder.encode(sum.toString(8).padStart(6, "0")), offset + 148);
|
|
148
|
+
archive[offset + 154] = 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Split a name that does not fit the 99-byte name field into ustar's `prefix` / `name` pair. */
|
|
152
|
+
function splitName(name: string): [prefix: string, name: string] {
|
|
153
|
+
if (encoder.encode(name).byteLength < NAME_LENGTH) return ["", name];
|
|
154
|
+
for (let cut = name.indexOf("/"); cut !== -1; cut = name.indexOf("/", cut + 1)) {
|
|
155
|
+
const prefix = name.slice(0, cut);
|
|
156
|
+
const rest = name.slice(cut + 1);
|
|
157
|
+
if (encoder.encode(prefix).byteLength < PREFIX_LENGTH && encoder.encode(rest).byteLength < NAME_LENGTH) {
|
|
158
|
+
return [prefix, rest];
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
throw new TarFormatError(`tar member name is too long to store: "${name}"`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function joinPrefix(header: Uint8Array): string {
|
|
165
|
+
const name = readCString(header, 0, NAME_LENGTH);
|
|
166
|
+
const magic = readCString(header, 257, 6);
|
|
167
|
+
if (magic !== "ustar") return name;
|
|
168
|
+
const prefix = readCString(header, 345, PREFIX_LENGTH);
|
|
169
|
+
return prefix === "" ? name : `${prefix}/${name}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function writeString(archive: Uint8Array, at: number, length: number, value: string, terminate = true): void {
|
|
173
|
+
const bytes = encoder.encode(value);
|
|
174
|
+
if (bytes.byteLength > (terminate ? length - 1 : length)) {
|
|
175
|
+
throw new TarFormatError(`tar header field overflow: "${value}"`);
|
|
176
|
+
}
|
|
177
|
+
archive.set(bytes, at);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function writeOctal(archive: Uint8Array, at: number, length: number, value: number): void {
|
|
181
|
+
const digits = value.toString(8);
|
|
182
|
+
if (!Number.isSafeInteger(value) || value < 0 || digits.length > length - 1) {
|
|
183
|
+
throw new TarFormatError(`tar header number out of range: ${value}`);
|
|
184
|
+
}
|
|
185
|
+
writeString(archive, at, length, digits.padStart(length - 1, "0"));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function readCString(bytes: Uint8Array, at: number, length: number): string {
|
|
189
|
+
let end = at;
|
|
190
|
+
while (end < at + length && bytes[end] !== 0) end++;
|
|
191
|
+
return decoder.decode(bytes.subarray(at, end));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function readOctal(header: Uint8Array, at: number, length: number, recordOffset: number): number {
|
|
195
|
+
if (((header[at] ?? 0) & 0x80) !== 0) {
|
|
196
|
+
throw new TarFormatError(`tar header at byte ${recordOffset} uses base-256 numbers, which are not supported`);
|
|
197
|
+
}
|
|
198
|
+
const text = readCString(header, at, length).trim();
|
|
199
|
+
if (text === "") return 0;
|
|
200
|
+
if (!/^[0-7]+$/.test(text)) {
|
|
201
|
+
throw new TarFormatError(`tar header at byte ${recordOffset} has a malformed number: "${text}"`);
|
|
202
|
+
}
|
|
203
|
+
return Number.parseInt(text, 8);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function verifyChecksum(header: Uint8Array, recordOffset: number): void {
|
|
207
|
+
const stored = readOctal(header, 148, 8, recordOffset);
|
|
208
|
+
let sum = 0;
|
|
209
|
+
for (let index = 0; index < RECORD; index++) {
|
|
210
|
+
sum += index >= 148 && index < 156 ? 0x20 : (header[index] ?? 0);
|
|
211
|
+
}
|
|
212
|
+
if (sum !== stored) throw new TarFormatError(`tar header at byte ${recordOffset} fails its checksum`);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function isZeroRecord(header: Uint8Array): boolean {
|
|
216
|
+
for (const byte of header) if (byte !== 0) return false;
|
|
217
|
+
return true;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* PAX extended header records: `"<length> <key>=<value>\n"`, lengths in bytes. Only `path` and `size`
|
|
222
|
+
* matter; a record that does not parse, or a `size` that is not a non-negative integer, is refused.
|
|
223
|
+
*/
|
|
224
|
+
function readPax(data: Uint8Array, recordOffset: number): { path?: string; size?: number } {
|
|
225
|
+
const result: { path?: string; size?: number } = {};
|
|
226
|
+
const malformed = (what: string) =>
|
|
227
|
+
new TarFormatError(`tar PAX header before byte ${recordOffset} has a malformed ${what}`);
|
|
228
|
+
let cursor = 0;
|
|
229
|
+
while (cursor < data.byteLength) {
|
|
230
|
+
let space = cursor;
|
|
231
|
+
while (space < data.byteLength && data[space] !== 0x20) space++;
|
|
232
|
+
const lengthText = decoder.decode(data.subarray(cursor, space));
|
|
233
|
+
if (space >= data.byteLength || !/^[0-9]+$/.test(lengthText)) throw malformed("record");
|
|
234
|
+
const length = Number.parseInt(lengthText, 10);
|
|
235
|
+
if (!Number.isSafeInteger(length) || length <= space - cursor + 1 || cursor + length > data.byteLength) {
|
|
236
|
+
throw malformed("record");
|
|
237
|
+
}
|
|
238
|
+
if (data[cursor + length - 1] !== 0x0a) throw malformed("record");
|
|
239
|
+
const record = decoder.decode(data.subarray(space + 1, cursor + length - 1));
|
|
240
|
+
const equals = record.indexOf("=");
|
|
241
|
+
if (equals === -1) throw malformed("record");
|
|
242
|
+
const key = record.slice(0, equals);
|
|
243
|
+
const value = record.slice(equals + 1);
|
|
244
|
+
if (key === "path") result.path = value;
|
|
245
|
+
if (key === "size") {
|
|
246
|
+
const size = /^[0-9]+$/.test(value) ? Number.parseInt(value, 10) : Number.NaN;
|
|
247
|
+
if (!Number.isSafeInteger(size)) throw malformed(`size ("${value}")`);
|
|
248
|
+
result.size = size;
|
|
249
|
+
}
|
|
250
|
+
cursor += length;
|
|
251
|
+
}
|
|
252
|
+
return result;
|
|
253
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// Began as a copy of `@electric-sql/pglite` (taken under its PostgreSQL License option, © ElectricSQL
|
|
2
|
+
// — see NOTICE). Owned outright (ADR-0062); compatibility with PGlite is an anti-goal — evolve freely.
|
|
3
|
+
|
|
4
|
+
const TemplateType = {
|
|
5
|
+
part: "part",
|
|
6
|
+
container: "container",
|
|
7
|
+
} as const;
|
|
8
|
+
|
|
9
|
+
/** A literal piece of SQL (an identifier or raw text) spliced into a template without a parameter. */
|
|
10
|
+
export interface TemplatePart {
|
|
11
|
+
readonly _templateType: typeof TemplateType.part;
|
|
12
|
+
readonly str: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A nested template: its strings and its parameter values. */
|
|
16
|
+
export interface TemplateContainer {
|
|
17
|
+
readonly _templateType: typeof TemplateType.container;
|
|
18
|
+
readonly strings: TemplateStringsArray;
|
|
19
|
+
readonly values: unknown[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** A parametrized query: `$1`-numbered text and its parameters. */
|
|
23
|
+
export interface TemplatedQuery {
|
|
24
|
+
readonly query: string;
|
|
25
|
+
readonly params: unknown[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isTemplateValue(value: unknown): value is TemplatePart | TemplateContainer {
|
|
29
|
+
return typeof value === "object" && value !== null && "_templateType" in value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function addToLastAndPushWithSuffix(arr: string[], suffix: string, ...values: readonly string[]): void {
|
|
33
|
+
const lastArrIdx = arr.length - 1;
|
|
34
|
+
const lastValIdx = values.length - 1;
|
|
35
|
+
if (lastValIdx === -1) return;
|
|
36
|
+
if (lastValIdx === 0) {
|
|
37
|
+
arr[lastArrIdx] = `${arr[lastArrIdx] ?? ""}${values[0] ?? ""}${suffix}`;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
// Sandwich the values between the array's last element and the suffix.
|
|
41
|
+
arr[lastArrIdx] = `${arr[lastArrIdx] ?? ""}${values[0] ?? ""}`;
|
|
42
|
+
arr.push(...values.slice(1, lastValIdx));
|
|
43
|
+
arr.push(`${values[lastValIdx] ?? ""}${suffix}`);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Nest SQL templates without losing their parametrization:
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* pg.sql`SELECT * FROM tale ${withFilter ? sql`WHERE foo = ${fooVar}` : sql``}`
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function sql(strings: TemplateStringsArray, ...values: unknown[]): TemplateContainer {
|
|
54
|
+
const parsedStrings: string[] = [strings[0] ?? ""];
|
|
55
|
+
const parsedRaw: string[] = [strings.raw[0] ?? ""];
|
|
56
|
+
const parsedValues: unknown[] = [];
|
|
57
|
+
for (let i = 0; i < values.length; i++) {
|
|
58
|
+
const value = values[i];
|
|
59
|
+
const nextString = strings[i + 1] ?? "";
|
|
60
|
+
const nextRaw = strings.raw[i + 1] ?? "";
|
|
61
|
+
if (isTemplateValue(value) && value._templateType === TemplateType.part) {
|
|
62
|
+
addToLastAndPushWithSuffix(parsedStrings, nextString, value.str);
|
|
63
|
+
addToLastAndPushWithSuffix(parsedRaw, nextRaw, value.str);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (isTemplateValue(value) && value._templateType === TemplateType.container) {
|
|
67
|
+
addToLastAndPushWithSuffix(parsedStrings, nextString, ...value.strings);
|
|
68
|
+
addToLastAndPushWithSuffix(parsedRaw, nextRaw, ...value.strings.raw);
|
|
69
|
+
parsedValues.push(...value.values);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
parsedStrings.push(nextString);
|
|
73
|
+
parsedRaw.push(nextRaw);
|
|
74
|
+
parsedValues.push(value);
|
|
75
|
+
}
|
|
76
|
+
const templateStrings = Object.assign(parsedStrings, { raw: parsedRaw });
|
|
77
|
+
return { _templateType: TemplateType.container, strings: templateStrings, values: parsedValues };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* An identifier spliced into a template, double-quoted and not parametrized:
|
|
82
|
+
*
|
|
83
|
+
* ```ts
|
|
84
|
+
* pg.sql`SELECT * FROM ${identifier`foo`} WHERE ${identifier`id`} = ${id}`
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export function identifier(strings: TemplateStringsArray, ...values: unknown[]): TemplatePart {
|
|
88
|
+
return { _templateType: TemplateType.part, str: `"${String.raw(strings, ...values)}"` };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Raw SQL spliced into a template as it is, neither parametrized nor escaped. */
|
|
92
|
+
export function raw(strings: TemplateStringsArray, ...values: unknown[]): TemplatePart {
|
|
93
|
+
return { _templateType: TemplateType.part, str: String.raw(strings, ...values) };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Turn a template into `$1`-numbered query text and its parameters. */
|
|
97
|
+
export function query(strings: TemplateStringsArray, ...values: unknown[]): TemplatedQuery {
|
|
98
|
+
const { strings: queryStringParts, values: params } = sql(strings, ...values);
|
|
99
|
+
return {
|
|
100
|
+
query: [queryStringParts[0], ...params.flatMap((_, idx) => [`$${idx + 1}`, queryStringParts[idx + 1]])].join(""),
|
|
101
|
+
params,
|
|
102
|
+
};
|
|
103
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
// Began as a copy of `@electric-sql/pglite` (taken under its PostgreSQL License option, © ElectricSQL
|
|
2
|
+
// — see NOTICE). Owned outright (ADR-0062); compatibility with PGlite is an anti-goal — evolve freely.
|
|
3
|
+
//
|
|
4
|
+
// Based on postgres.js types.js (https://github.com/porsager/postgres/blob/master/src/types.js),
|
|
5
|
+
// published under the Unlicense.
|
|
6
|
+
|
|
7
|
+
import type { ParserOptions } from "./interface";
|
|
8
|
+
|
|
9
|
+
const JSON_parse = globalThis.JSON.parse;
|
|
10
|
+
const JSON_stringify = globalThis.JSON.stringify;
|
|
11
|
+
|
|
12
|
+
/** Built-in type OIDs. */
|
|
13
|
+
export const BOOL = 16,
|
|
14
|
+
BYTEA = 17,
|
|
15
|
+
CHAR = 18,
|
|
16
|
+
INT8 = 20,
|
|
17
|
+
INT2 = 21,
|
|
18
|
+
INT4 = 23,
|
|
19
|
+
REGPROC = 24,
|
|
20
|
+
TEXT = 25,
|
|
21
|
+
OID = 26,
|
|
22
|
+
TID = 27,
|
|
23
|
+
XID = 28,
|
|
24
|
+
CID = 29,
|
|
25
|
+
JSON = 114,
|
|
26
|
+
XML = 142,
|
|
27
|
+
PG_NODE_TREE = 194,
|
|
28
|
+
SMGR = 210,
|
|
29
|
+
PATH = 602,
|
|
30
|
+
POLYGON = 604,
|
|
31
|
+
CIDR = 650,
|
|
32
|
+
FLOAT4 = 700,
|
|
33
|
+
FLOAT8 = 701,
|
|
34
|
+
ABSTIME = 702,
|
|
35
|
+
RELTIME = 703,
|
|
36
|
+
TINTERVAL = 704,
|
|
37
|
+
CIRCLE = 718,
|
|
38
|
+
MACADDR8 = 774,
|
|
39
|
+
MONEY = 790,
|
|
40
|
+
MACADDR = 829,
|
|
41
|
+
INET = 869,
|
|
42
|
+
ACLITEM = 1033,
|
|
43
|
+
BPCHAR = 1042,
|
|
44
|
+
VARCHAR = 1043,
|
|
45
|
+
DATE = 1082,
|
|
46
|
+
TIME = 1083,
|
|
47
|
+
TIMESTAMP = 1114,
|
|
48
|
+
TIMESTAMPTZ = 1184,
|
|
49
|
+
INTERVAL = 1186,
|
|
50
|
+
TIMETZ = 1266,
|
|
51
|
+
BIT = 1560,
|
|
52
|
+
VARBIT = 1562,
|
|
53
|
+
NUMERIC = 1700,
|
|
54
|
+
REFCURSOR = 1790,
|
|
55
|
+
REGPROCEDURE = 2202,
|
|
56
|
+
REGOPER = 2203,
|
|
57
|
+
REGOPERATOR = 2204,
|
|
58
|
+
REGCLASS = 2205,
|
|
59
|
+
REGTYPE = 2206,
|
|
60
|
+
UUID = 2950,
|
|
61
|
+
TXID_SNAPSHOT = 2970,
|
|
62
|
+
PG_LSN = 3220,
|
|
63
|
+
PG_NDISTINCT = 3361,
|
|
64
|
+
PG_DEPENDENCIES = 3402,
|
|
65
|
+
TSVECTOR = 3614,
|
|
66
|
+
TSQUERY = 3615,
|
|
67
|
+
GTSVECTOR = 3642,
|
|
68
|
+
REGCONFIG = 3734,
|
|
69
|
+
REGDICTIONARY = 3769,
|
|
70
|
+
JSONB = 3802,
|
|
71
|
+
REGNAMESPACE = 4089,
|
|
72
|
+
REGROLE = 4096;
|
|
73
|
+
|
|
74
|
+
/** Turns a value's text representation into a JS value. */
|
|
75
|
+
export type Parser = (value: string, typeId?: number) => unknown;
|
|
76
|
+
/** Turns a JS value into its text representation. */
|
|
77
|
+
export type Serializer = (value: unknown) => string;
|
|
78
|
+
|
|
79
|
+
interface TypeHandler {
|
|
80
|
+
to: number;
|
|
81
|
+
from: readonly number[];
|
|
82
|
+
serialize: Serializer;
|
|
83
|
+
parse: Parser;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function serializeString(value: unknown): string {
|
|
87
|
+
if (value instanceof Date) return value.toISOString();
|
|
88
|
+
return String(value);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function serializeBoolean(value: unknown): string {
|
|
92
|
+
// Mirror PostgreSQL's own boolean input (`boolin`): accept the literals Postgres accepts, plus the JS
|
|
93
|
+
// shapes drivers send for a boolean column (1/0, "true"/"false"). Anything Postgres would reject
|
|
94
|
+
// throws rather than silently becoming 'f'.
|
|
95
|
+
if (typeof value === "boolean") return value ? "t" : "f";
|
|
96
|
+
if (typeof value === "number") {
|
|
97
|
+
if (value === 1) return "t";
|
|
98
|
+
if (value === 0) return "f";
|
|
99
|
+
} else if (typeof value === "string") {
|
|
100
|
+
const s = value.trim().toLowerCase();
|
|
101
|
+
if (["true", "t", "yes", "y", "on", "1"].includes(s)) return "t";
|
|
102
|
+
if (["false", "f", "no", "n", "off", "0"].includes(s)) return "f";
|
|
103
|
+
}
|
|
104
|
+
throw new Error("Invalid input for boolean type");
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function serializeDate(value: unknown): string {
|
|
108
|
+
if (typeof value === "string") return value;
|
|
109
|
+
if (typeof value === "number") return new Date(value).toISOString();
|
|
110
|
+
if (value instanceof Date) return value.toISOString();
|
|
111
|
+
throw new Error("Invalid input for date type");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function serializeBytea(value: unknown): string {
|
|
115
|
+
if (!(value instanceof Uint8Array)) {
|
|
116
|
+
throw new Error("Invalid input for bytea type");
|
|
117
|
+
}
|
|
118
|
+
return `\\x${Array.from(value, (byte) => byte.toString(16).padStart(2, "0")).join("")}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function parseBytea(value: string): Uint8Array {
|
|
122
|
+
const hexString = value.slice(2);
|
|
123
|
+
return Uint8Array.from({ length: hexString.length / 2 }, (_, idx) =>
|
|
124
|
+
Number.parseInt(hexString.substring(idx * 2, (idx + 1) * 2), 16),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function parseBigint(value: string): number | bigint {
|
|
129
|
+
const n = BigInt(value);
|
|
130
|
+
// In the safe range a plain number; outside it, the bigint.
|
|
131
|
+
return n < Number.MIN_SAFE_INTEGER || n > Number.MAX_SAFE_INTEGER ? n : Number(n);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function serializeJson(value: unknown): string {
|
|
135
|
+
if (typeof value === "string") return value;
|
|
136
|
+
return JSON_stringify(value, (_, inner: unknown) => (typeof inner === "bigint" ? inner.toString() : inner));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The default handlers, by the name pgwasm has always used for each family. */
|
|
140
|
+
export const types = {
|
|
141
|
+
string: { to: TEXT, from: [TEXT, VARCHAR, BPCHAR], serialize: serializeString, parse: (x: string) => x },
|
|
142
|
+
number: {
|
|
143
|
+
to: 0,
|
|
144
|
+
from: [INT2, INT4, OID, FLOAT4, FLOAT8],
|
|
145
|
+
serialize: toText,
|
|
146
|
+
parse: (x: string) => +x,
|
|
147
|
+
},
|
|
148
|
+
bigint: { to: INT8, from: [INT8], serialize: toText, parse: parseBigint },
|
|
149
|
+
json: { to: JSON, from: [JSON, JSONB], serialize: serializeJson, parse: (x: string): unknown => JSON_parse(x) },
|
|
150
|
+
boolean: { to: BOOL, from: [BOOL], serialize: serializeBoolean, parse: (x: string) => x === "t" },
|
|
151
|
+
date: {
|
|
152
|
+
to: TIMESTAMPTZ,
|
|
153
|
+
from: [DATE, TIMESTAMP, TIMESTAMPTZ],
|
|
154
|
+
serialize: serializeDate,
|
|
155
|
+
parse: (x: string) => new Date(x),
|
|
156
|
+
},
|
|
157
|
+
bytea: { to: BYTEA, from: [BYTEA], serialize: serializeBytea, parse: parseBytea },
|
|
158
|
+
} satisfies Record<string, TypeHandler>;
|
|
159
|
+
|
|
160
|
+
function typeHandlers(handlers: Record<string, TypeHandler>): {
|
|
161
|
+
parsers: Record<number | string, Parser>;
|
|
162
|
+
serializers: Record<number | string, Serializer>;
|
|
163
|
+
} {
|
|
164
|
+
const parsers: Record<number | string, Parser> = {};
|
|
165
|
+
const serializers: Record<number | string, Serializer> = {};
|
|
166
|
+
for (const [name, { to, from, serialize, parse }] of Object.entries(handlers)) {
|
|
167
|
+
serializers[to] = serialize;
|
|
168
|
+
serializers[name] = serialize;
|
|
169
|
+
parsers[name] = parse;
|
|
170
|
+
for (const oid of from) {
|
|
171
|
+
parsers[oid] = parse;
|
|
172
|
+
serializers[oid] = serialize;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return { parsers, serializers };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const defaultHandlers = typeHandlers(types);
|
|
179
|
+
|
|
180
|
+
/** The default parsers, by OID (and by family name). */
|
|
181
|
+
export const parsers = defaultHandlers.parsers;
|
|
182
|
+
/** The default serializers, by OID (and by family name). */
|
|
183
|
+
export const serializers = defaultHandlers.serializers;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The text form of a value that has no serializer for its type: the value's own `toString()`, which
|
|
187
|
+
* for a primitive is its usual text.
|
|
188
|
+
*/
|
|
189
|
+
export function toText(value: unknown): string {
|
|
190
|
+
if (typeof value === "string") return value;
|
|
191
|
+
return (value as { toString(): string }).toString();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Parse one text value of the given type OID (`null` stays `null`; an unknown type stays text). */
|
|
195
|
+
export function parseType(value: string | null, type: number, overrides?: ParserOptions): unknown {
|
|
196
|
+
if (value === null) return null;
|
|
197
|
+
const handler = overrides?.[type] ?? defaultHandlers.parsers[type];
|
|
198
|
+
return handler ? handler(value, type) : value;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const escapeBackslash = /\\/g;
|
|
202
|
+
const escapeQuote = /"/g;
|
|
203
|
+
|
|
204
|
+
function arrayEscape(x: string): string {
|
|
205
|
+
return x.replace(escapeBackslash, "\\\\").replace(escapeQuote, '\\"');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Serialize a (nested) JS array to a Postgres array literal. A non-array is returned as it is. */
|
|
209
|
+
export function arraySerializer(xs: unknown, serializer: Serializer | undefined, typarray: number): string {
|
|
210
|
+
if (!Array.isArray(xs)) return toText(xs);
|
|
211
|
+
if (xs.length === 0) return "{}";
|
|
212
|
+
// Only _box (1020) uses ';' as its array delimiter; every other type uses ','.
|
|
213
|
+
const delimiter = typarray === 1020 ? ";" : ",";
|
|
214
|
+
const first: unknown = xs[0];
|
|
215
|
+
if (Array.isArray(first)) {
|
|
216
|
+
return `{${xs.map((x: unknown) => arraySerializer(x, serializer, typarray)).join(delimiter)}}`;
|
|
217
|
+
}
|
|
218
|
+
return `{${xs
|
|
219
|
+
.map((x: unknown) => {
|
|
220
|
+
// undefined is sent as NULL.
|
|
221
|
+
if (x === undefined || x === null) return "null";
|
|
222
|
+
return `"${arrayEscape(serializer ? serializer(x) : toText(x))}"`;
|
|
223
|
+
})
|
|
224
|
+
.join(delimiter)}}`;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
interface ArrayParserState {
|
|
228
|
+
i: number;
|
|
229
|
+
char: string | null;
|
|
230
|
+
str: string;
|
|
231
|
+
quoted: boolean;
|
|
232
|
+
last: number;
|
|
233
|
+
p: string | null;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Parse a Postgres array literal, applying `parser` to each element. */
|
|
237
|
+
export function arrayParser(x: string, parser: Parser | undefined, typarray: number): unknown {
|
|
238
|
+
const state: ArrayParserState = { i: 0, char: null, str: "", quoted: false, last: 0, p: null };
|
|
239
|
+
return arrayParserLoop(state, x, parser, typarray)[0];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function arrayParserLoop(s: ArrayParserState, x: string, parser: Parser | undefined, typarray: number): unknown[] {
|
|
243
|
+
const xs: unknown[] = [];
|
|
244
|
+
const delimiter = typarray === 1020 ? ";" : ",";
|
|
245
|
+
for (; s.i < x.length; s.i++) {
|
|
246
|
+
s.char = x[s.i] ?? null;
|
|
247
|
+
if (s.quoted) {
|
|
248
|
+
if (s.char === "\\") {
|
|
249
|
+
s.str += x[++s.i] ?? "";
|
|
250
|
+
} else if (s.char === '"') {
|
|
251
|
+
xs.push(parser ? parser(s.str) : s.str);
|
|
252
|
+
s.str = "";
|
|
253
|
+
s.quoted = x[s.i + 1] === '"';
|
|
254
|
+
s.last = s.i + 2;
|
|
255
|
+
} else {
|
|
256
|
+
s.str += s.char;
|
|
257
|
+
}
|
|
258
|
+
} else if (s.char === '"') {
|
|
259
|
+
s.quoted = true;
|
|
260
|
+
} else if (s.char === "{") {
|
|
261
|
+
s.last = ++s.i;
|
|
262
|
+
xs.push(arrayParserLoop(s, x, parser, typarray));
|
|
263
|
+
} else if (s.char === "}") {
|
|
264
|
+
if (s.last < s.i) {
|
|
265
|
+
const el = x.slice(s.last, s.i);
|
|
266
|
+
xs.push(el === "NULL" && !s.quoted ? null : parser ? parser(el) : el);
|
|
267
|
+
}
|
|
268
|
+
s.quoted = false;
|
|
269
|
+
s.last = s.i + 1;
|
|
270
|
+
break;
|
|
271
|
+
} else if (s.char === delimiter && s.p !== "}" && s.p !== '"') {
|
|
272
|
+
const el = x.slice(s.last, s.i);
|
|
273
|
+
xs.push(el === "NULL" && !s.quoted ? null : parser ? parser(el) : el);
|
|
274
|
+
s.last = s.i + 1;
|
|
275
|
+
}
|
|
276
|
+
s.p = s.char;
|
|
277
|
+
}
|
|
278
|
+
if (s.last < s.i) {
|
|
279
|
+
const el = x.slice(s.last, s.i + 1);
|
|
280
|
+
xs.push(parser ? parser(el) : el);
|
|
281
|
+
}
|
|
282
|
+
return xs;
|
|
283
|
+
}
|