Skip to content

Commit

Permalink
[ts] Use native node fetch removing polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell committed Apr 8, 2024
1 parent 2828e07 commit 9ec0a31
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 573 deletions.
57 changes: 2 additions & 55 deletions examples/node/streamtest.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,10 @@
/* eslint-disable no-undef */
import { geojson } from 'flatgeobuf'
import fetch from 'node-fetch'
import { ReadableStream } from 'web-streams-polyfill'

function nodeToWeb(nodeStream) {
let destroyed = false;
const listeners = {};

function start(controller) {
listeners['data'] = onData;
listeners['end'] = onData;
listeners['end'] = onDestroy;
listeners['close'] = onDestroy;
listeners['error'] = onDestroy;
for (const name in listeners) nodeStream.on(name, listeners[name]);

nodeStream.pause();

function onData(chunk) {
if (destroyed) return;
controller.enqueue(chunk);
nodeStream.pause();
}

function onDestroy(err) {
if (destroyed) return;
destroyed = true;

for (const name in listeners)
nodeStream.removeListener(name, listeners[name]);

if (err) controller.error(err);
else controller.close();
}
}

function pull() {
if (destroyed) return;
nodeStream.resume();
}

function cancel() {
destroyed = true;

for (const name in listeners)
nodeStream.removeListener(name, listeners[name]);

nodeStream.push(null);
nodeStream.pause();
nodeStream.destroy();
}

return new ReadableStream({ start: start, pull: pull, cancel: cancel });
}

async function streamtest() {
const response = await fetch('https://flatgeobuf.org/test/data/UScounties.fgb')
for await (let feature of geojson.deserialize(nodeToWeb(response.body)))
for await (let feature of geojson.deserialize(response.body))
console.log(JSON.stringify(feature, undefined, 1))
}

streamtest()
streamtest()
32 changes: 15 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,26 @@
"ol": ">3"
},
"devDependencies": {
"@babel/core": "7.24.3",
"@babel/preset-env": "7.24.3",
"@babel/core": "7.24.4",
"@babel/preset-env": "7.24.4",
"@rollup/plugin-babel": "6.0.4",
"@rollup/plugin-node-resolve": "15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-terser": "0.4.4",
"@types/geojson": "7946.0.14",
"@types/node": "20.12.2",
"@types/stream-buffers": "3.0.7",
"@typescript-eslint/eslint-plugin": "7.4.0",
"@typescript-eslint/parser": "7.4.0",
"@vitest/coverage-v8": "^1.4.0",
"eslint": "8.57.0",
"@types/node": "20.12.5",
"@typescript-eslint/eslint-plugin": "7.6.0",
"@typescript-eslint/parser": "7.6.0",
"@vitest/coverage-v8": "1.4.0",
"core-js": "3.36.1",
"eslint": "9.0.0",
"jsts": "2.11.0",
"local-web-server": "^5.3.1",
"node-fetch": "^3.3.2",
"local-web-server": "5.3.3",
"ol": "9.1.0",
"prettier": "3.2.5",
"rollup": "4.13.2",
"stream-buffers": "3.0.2",
"typedoc": "0.25.12",
"typescript": "5.4.3",
"vitest": "^1.4.0",
"web-streams-polyfill": "4.0.0"
"rollup": "4.14.1",
"to-readable-stream": "4.0.0",
"typedoc": "0.25.13",
"typescript": "5.4.4",
"vitest": "^1.4.0"
}
}
23 changes: 10 additions & 13 deletions src/ts/geojson.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { describe, it, expect } from 'vitest';
import GeoJSONWriter from 'jsts/org/locationtech/jts/io/GeoJSONWriter.js';
import WKTReader from 'jsts/org/locationtech/jts/io/WKTReader.js';
import { readFileSync } from 'fs';
import fetch from 'node-fetch';
import { arrayToStream, takeAsync } from './streams/utils.js';
import toReadableStream from 'to-readable-stream';
import 'core-js/actual/array/from-async';

import { deserialize, serialize } from './geojson.js';
import { IGeoJsonFeature } from './geojson/feature.js';
import { Rect } from './packedrtree.js';
import HeaderMeta from './header-meta.js';

global.fetch = fetch as any;

import {
FeatureCollection as GeoJsonFeatureCollection,
Point,
Expand Down Expand Up @@ -59,10 +58,8 @@ describe('geojson module', () => {
it('Point via stream', async () => {
const expected = makeFeatureCollection('POINT(1.2 -2.1)');
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
deserialize(stream) as AsyncGenerator,
);
const stream = toReadableStream(s);
const actual = await Array.fromAsync(deserialize(stream) as AsyncGenerator);
expect(actual).to.deep.equal(expected.features);
});

Expand Down Expand Up @@ -128,8 +125,8 @@ describe('geojson module', () => {
`POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))`,
);
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
const stream = toReadableStream(s);
const actual = await Array.fromAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
Expand Down Expand Up @@ -392,7 +389,7 @@ describe('geojson module', () => {

it('Should parse countries fgb produced from GDAL stream filter', async () => {
const r: Rect = { minX: 12, minY: 56, maxX: 12, maxY: 56 };
const features = await takeAsync(
const features = await Array.fromAsync(
deserialize(
'http://flatgeobuf.septima.dk/countries.fgb',
r,
Expand All @@ -410,8 +407,8 @@ describe('geojson module', () => {
it('Should parse countries fgb produced from GDAL stream no filter', async () => {
const buffer = readFileSync('./test/data/countries.fgb');
const bytes = new Uint8Array(buffer);
const stream = arrayToStream(bytes.buffer);
const features = await takeAsync(
const stream = toReadableStream(bytes.buffer);
const features = await Array.fromAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
Expand Down
11 changes: 6 additions & 5 deletions src/ts/ol.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'fs';
import 'core-js/actual/array/from-async';

import { arrayToStream, takeAsync } from './streams/utils.js';
import toReadableStream from 'to-readable-stream';
import { deserialize, serialize } from './ol.js';

import Feature from 'ol/Feature.js';
Expand Down Expand Up @@ -42,8 +43,8 @@ describe('ol module', () => {
it('Point via stream', async () => {
const expected = makeFeatureCollection('POINT(1.2 -2.1)');
const s = serialize(expected);
const stream = arrayToStream(s);
const actual = await takeAsync(
const stream = toReadableStream(s);
const actual = await Array.fromAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
Expand Down Expand Up @@ -223,8 +224,8 @@ describe('ol module', () => {
it('Should parse countries fgb produced from GDAL stream', async () => {
const buffer = readFileSync('./test/data/countries.fgb');
const bytes = new Uint8Array(buffer);
const stream = arrayToStream(bytes.buffer);
const features = await takeAsync(
const stream = toReadableStream(bytes.buffer);
const features = await Array.fromAsync(
deserialize(
stream as unknown as ReadableStream<any>,
) as AsyncGenerator,
Expand Down
82 changes: 0 additions & 82 deletions src/ts/streams/utils.ts

This file was deleted.

0 comments on commit 9ec0a31

Please sign in to comment.