2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
2023-03-13 05:56:53 +00:00
|
|
|
import { ConcatenatedJsonParseStream } from "./concatenated_json_parse_stream.ts";
|
2024-06-25 07:35:00 +00:00
|
|
|
import { assertInvalidParse, assertValidParse } from "./_test_utils.ts";
|
2022-06-20 00:06:37 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"} '],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
[' {"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['[{"foo": "bar"}]'],
|
|
|
|
[[{ foo: "bar" }]],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"}{"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"} {"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles primitive",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["0"],
|
|
|
|
[0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["100"],
|
|
|
|
[100],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['100 200"foo"'],
|
|
|
|
[100, 200, "foo"],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['100 200{"foo": "bar"}'],
|
|
|
|
[100, 200, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['100 200["foo"]'],
|
|
|
|
[100, 200, ["foo"]],
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['"foo"'],
|
|
|
|
["foo"],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['"foo""bar"{"foo": "bar"}'],
|
|
|
|
["foo", "bar", { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['"foo""bar"["foo"]'],
|
|
|
|
["foo", "bar", ["foo"]],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['"foo""bar"0'],
|
|
|
|
["foo", "bar", 0],
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["null"],
|
|
|
|
[null],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['null null{"foo": "bar"}'],
|
|
|
|
[null, null, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['null null["foo"]'],
|
|
|
|
[null, null, ["foo"]],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["null null 0"],
|
|
|
|
[null, null, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['null null"foo"'],
|
|
|
|
[null, null, "foo"],
|
|
|
|
);
|
2023-06-27 01:32:01 +00:00
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["nullnull"],
|
|
|
|
[null, null],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["nullnull0"],
|
|
|
|
[null, null, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
['nullnull"foo"'],
|
|
|
|
[null, null, "foo"],
|
|
|
|
);
|
2022-06-20 00:06:37 +00:00
|
|
|
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["true"],
|
|
|
|
[true],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['true true{"foo": "bar"}'],
|
|
|
|
[true, true, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['true true["foo"]'],
|
|
|
|
[true, true, ["foo"]],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["true true 0"],
|
|
|
|
[true, true, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['true true"foo"'],
|
|
|
|
[true, true, "foo"],
|
|
|
|
);
|
2023-06-27 01:32:01 +00:00
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["truetrue"],
|
|
|
|
[true, true],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["truetrue0"],
|
|
|
|
[true, true, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
['truetrue"foo"'],
|
|
|
|
[true, true, "foo"],
|
|
|
|
);
|
2022-06-20 00:06:37 +00:00
|
|
|
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["false"],
|
|
|
|
[false],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['false false{"foo": "bar"}'],
|
|
|
|
[false, false, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['false false["foo"]'],
|
|
|
|
[false, false, ["foo"]],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["false false 0"],
|
|
|
|
[false, false, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['false false"foo"'],
|
|
|
|
[false, false, "foo"],
|
|
|
|
);
|
2023-06-27 01:32:01 +00:00
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["falsefalse"],
|
|
|
|
[false, false],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["falsefalse0"],
|
|
|
|
[false, false, 0],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
['falsefalse"foo"'],
|
|
|
|
[false, false, "foo"],
|
|
|
|
);
|
|
|
|
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
['nullfalsetrue0true"foo"falsenullnull'],
|
|
|
|
[null, false, true, 0, true, "foo", false, null, null],
|
|
|
|
);
|
2022-06-20 00:06:37 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles chunk",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["", '{"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
["{", '"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "b', 'ar"}'],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"', "}"],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"}', ""],
|
|
|
|
[{ foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"}', '{"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"', '}{"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }, { foo: "bar" }],
|
|
|
|
);
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"}{', '"foo": "bar"}'],
|
|
|
|
[{ foo: "bar" }, { foo: "bar" }],
|
|
|
|
);
|
2023-06-27 01:32:01 +00:00
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["tr", 'ue{"foo": "bar"}'],
|
|
|
|
[true, { foo: "bar" }],
|
|
|
|
);
|
2024-06-18 11:38:18 +00:00
|
|
|
// Invalid primitive which share some leading characters with the valid primitive
|
|
|
|
await assertInvalidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["truu"],
|
|
|
|
SyntaxError,
|
|
|
|
`Unexpected token 'u', \"truu\" is not valid JSON (parsing: 'truu')`,
|
|
|
|
);
|
2022-06-20 00:06:37 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles surrogate pair",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "👪"}{"foo": "👪"}'],
|
|
|
|
[{ foo: "👪" }, { foo: "👪" }],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles symbol between double quotes",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['"[], {}"'],
|
|
|
|
["[], {}"],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-06-27 01:32:01 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles primitives in containers",
|
2023-06-27 01:32:01 +00:00
|
|
|
async fn() {
|
|
|
|
await assertValidParse(
|
|
|
|
ConcatenatedJsonParseStream,
|
|
|
|
["[ true ]"],
|
|
|
|
[[true]],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-06-20 00:06:37 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream handles halfway chunk",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertInvalidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
['{"foo": "bar"} {"foo": '],
|
|
|
|
SyntaxError,
|
|
|
|
`Unexpected end of JSON input (parsing: ' {"foo": ')`,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "ConcatenatedJsonParseStream truncates error message",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
|
|
|
await assertInvalidParse(
|
2022-08-30 07:08:43 +00:00
|
|
|
ConcatenatedJsonParseStream,
|
2022-06-20 00:06:37 +00:00
|
|
|
[`{${"foo".repeat(100)}}`],
|
|
|
|
SyntaxError,
|
2023-09-01 02:26:59 +00:00
|
|
|
`Expected property name or '}' in JSON at position 1 (line 1 column 2) (parsing: '{foofoofoofoofoofoofoofoofoofo...')`,
|
2022-06-20 00:06:37 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
// Read the test data file
|
2024-02-27 20:18:49 +00:00
|
|
|
name: "parse() handles concatenated-json testdata",
|
2022-06-20 00:06:37 +00:00
|
|
|
async fn() {
|
2023-03-13 05:56:53 +00:00
|
|
|
const url = "./testdata/test.concatenated-json";
|
2022-06-20 00:06:37 +00:00
|
|
|
const { body } = await fetch(new URL(url, import.meta.url).toString());
|
|
|
|
const readable = body!
|
|
|
|
.pipeThrough(new TextDecoderStream())
|
2022-08-30 07:08:43 +00:00
|
|
|
.pipeThrough(new ConcatenatedJsonParseStream());
|
2022-06-20 00:06:37 +00:00
|
|
|
|
2023-11-10 19:00:28 +00:00
|
|
|
const result = await Array.fromAsync(readable);
|
2022-06-20 00:06:37 +00:00
|
|
|
|
|
|
|
assertEquals(result, [
|
|
|
|
{ "hello": "world" },
|
|
|
|
["👋", "👋", "👋"],
|
|
|
|
{ "deno": "🦕" },
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
});
|