Skip to content

Commit

Permalink
Merge pull request #1 from rictic/ci
Browse files Browse the repository at this point in the history
Format, lint, add github CI
  • Loading branch information
rictic authored Oct 12, 2024
2 parents 99ffac8 + b0c120a commit 8fd73d3
Show file tree
Hide file tree
Showing 19 changed files with 1,836 additions and 273 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/dist
/.wireit
/bench/bundles
/vendor
/examples
/bench
23 changes: 23 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
rules: {
'no-constant-condition': 'off',
'@typescript-eslint/await-thenable': 'off',
'@typescript-eslint/no-floating-promises': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{argsIgnorePattern: '^_', varsIgnorePattern: '^_'},
],
},
};
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Tests

on: [push, pull_request]

jobs:
tests:
timeout-minutes: 10
runs-on: ubuntu-latest

env:
WIREIT_LOGGER: 'quiet-ci'

steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm test

lint-and-format:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm

- run: npm ci
- run: npm run lint
- run: npm run format:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/node_modules
/.wireit
/bench/bundles
/.eslintcache
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist
/.wireit
/bench/bundles
/vendor
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Usage:

```js
// Full example at examples/fetch.js
import { parse } from "jsonriver";
import {parse} from 'jsonriver';

const response = await fetch(`https://jsonplaceholder.typicode.com/posts`);
const vals = parse(response.body);
Expand All @@ -22,7 +22,7 @@ for await (const val of vals) {
What does it mean that we give you a sequence of increasingly complete values? Consider this JSON:

```json
{"name":"Alex", "keys":[1,20,300]}
{"name": "Alex", "keys": [1, 20, 300]}
```

If you gave this to jsonriver one byte at a time it would yield this sequence of values:
Expand Down
4 changes: 2 additions & 2 deletions bench/jsonparse.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* Copyright Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import * as bench from "/bench.js";
import { smallJsonString } from "./data.js";
import * as bench from '/bench.js';
import {smallJsonString} from './data.js';
bench.start();
for (let i = 0; i < 100; i++) {
await JSON.parse(smallJsonString);
Expand Down
6 changes: 3 additions & 3 deletions bench/jsonriver.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* Copyright Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/
import * as bench from "/bench.js";
import { smallJsonString } from "./data.js";
import { parse } from "./bundles/bundle.min.js";
import * as bench from '/bench.js';
import {smallJsonString} from './data.js';
import {parse} from './bundles/bundle.min.js';
async function* toStream(str) {
yield str;
}
Expand Down
40 changes: 20 additions & 20 deletions bench/node-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
// it depends on a bunch of Node built-ins and the like, so just doing a
// simple microbenchmark here.

import { streamJsonParser } from "./stream-json.js";
import { parse } from "./bundles/bundle.min.js";
import { parse as unoptimizedParse } from "./bundles/baseline.min.js";
import * as fs from "node:fs";
import * as path from "node:path";
import {streamJsonParser} from './stream-json.js';
import {parse} from './bundles/bundle.min.js';
import {parse as unoptimizedParse} from './bundles/baseline.min.js';
import * as fs from 'node:fs';
import * as path from 'node:path';

const dirname = new URL(".", import.meta.url).pathname;
const dirname = new URL('.', import.meta.url).pathname;
const smallJsonString = fs.readFileSync(
path.join(dirname, `../vendor/testdata/small-file.json`),
{ encoding: "utf-8" }
{encoding: 'utf-8'},
);
const mediumJsonString = fs.readFileSync(
path.join(dirname, `../vendor/testdata/medium-file.json`),
{ encoding: "utf-8" }
{encoding: 'utf-8'},
);
const largeJsonString = fs.readFileSync(
path.join(dirname, `../vendor/testdata/large-file.json`),
{ encoding: "utf-8" }
{encoding: 'utf-8'},
);

async function* toStream(str) {
Expand All @@ -48,19 +48,19 @@ async function jsonParseOld(jsonString) {

const comparisons = [
{
name: "jsonriver",
name: 'jsonriver',
parse: jsonParse,
},
{
name: "jsonriver v0.1",
name: 'jsonriver v0.1',
parse: jsonParseOld,
},
{
name: "stream-json",
name: 'stream-json',
parse: streamJsonParser,
},
{
name: "JSON.parse",
name: 'JSON.parse',
parse: JSON.parse,
},
];
Expand Down Expand Up @@ -89,19 +89,19 @@ async function benchmarkFile(comparisons, str, name, numTimes) {
console.log(`Parsing ${name} averaged over ${numTimes} runs`);
for (let i = 0; i < comparisons.length; i++) {
console.log(
` ${comparisons[i].name.padEnd(15, " ")} ${mean(times[i])
` ${comparisons[i].name.padEnd(15, ' ')} ${mean(times[i])
.toFixed(3)
.padStart(10, " ")}ms ±${stdDev(times[i]).toFixed(2)}ms`
.padStart(10, ' ')}ms ±${stdDev(times[i]).toFixed(2)}ms`,
);
}
console.log("\n");
console.log('\n');
}

await benchmarkFile(comparisons, smallJsonString, "a small file (64KiB)", 100);
await benchmarkFile(comparisons, smallJsonString, 'a small file (64KiB)', 100);
await benchmarkFile(
comparisons,
mediumJsonString,
"a medium file (1.4MiB)",
100
'a medium file (1.4MiB)',
100,
);
await benchmarkFile(comparisons, largeJsonString, "a large file (25MiB)", 3);
await benchmarkFile(comparisons, largeJsonString, 'a large file (25MiB)', 3);
12 changes: 6 additions & 6 deletions bench/stream-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

import * as nodeStream from "node:stream";
import { createRequire } from "node:module";
import * as nodeStream from 'node:stream';
import {createRequire} from 'node:module';
const require = createRequire(import.meta.url);
const { chain } = require("stream-chain");
const { parser } = require("stream-json");
const Asm = require("stream-json/Assembler");
const {chain} = require('stream-chain');
const {parser} = require('stream-json');
const Asm = require('stream-json/Assembler');

export async function streamJsonParser(str) {
// create a node stream with just str
Expand All @@ -21,6 +21,6 @@ export async function streamJsonParser(str) {
resolve = r;
});
const asm = Asm.connectTo(chain([stream, parser()]));
asm.on("done", (asm) => resolve(asm.current));
asm.on('done', (asm) => resolve(asm.current));
return promise;
}
6 changes: 3 additions & 3 deletions examples/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/

import { parse } from "jsonriver";
import {parse} from 'jsonriver';

class Renderer {
prevEnd = 0;
Expand All @@ -16,7 +16,7 @@ class Renderer {
// We haven't gotten any of the title yet, skip it.
continue;
}
process.stdout.write(first ? "\r" : "\n");
process.stdout.write(first ? '\r' : '\n');
process.stdout.write(`${post.id}: ${post.title}`);
first = false;
this.prevEnd = i;
Expand All @@ -36,7 +36,7 @@ for await (const posts of postsStream) {
// first results immediately before the rest of the data has even arrived.
renderer.render(posts);
}
process.stdout.write("\n");
process.stdout.write('\n');

async function* delay(stream) {
for await (const chunk of stream) {
Expand Down
Loading

0 comments on commit 8fd73d3

Please sign in to comment.