Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer importing polyfills (esp. node-fetch) #1

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint": "yarn lint:tsclient && yarn lint:examples",
"lint:tsclient": "xo --ignores ./examples && prettier --check {tsconfig,package}.json",
"lint:examples": "xo ./examples && tools/update-examples-parcel-version.sh --check",
"test:unit": "jest --projects test/jest/unit/",
"test:unit": "node --experimental-vm-modules node_modules/.bin/jest --projects test/jest/unit/",
"test:examples": "jest --projects test/examples/",
"test:integration:nodejs": "node test/pure-nodejs.js",
"test:integration:jest": "jest --projects test/jest/integration/",
Expand Down Expand Up @@ -78,7 +78,7 @@
"form-data": "^4.0.0",
"jsrsasign": "^10.3.0",
"ky": "^0.28.5",
"node-fetch": "3.0.0-beta.9",
"ky-universal": "^0.9.1",
"param-case": "^3.0.3",
"type-fest": "^1.3.0",
"web-streams-polyfill": "^3.1.0"
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export namespace AssetImpl {
client: HttpClient,
params?: EscrowedAssetSearchParams & PageParams,
): Promise<Page<EscrowedAsset>> {
return client.search<EscrowedAsset>(`${ASSETS_EP}/search`, params);
return client.search<EscrowedAsset>(`${ASSETS_EP}`, params);
}

export async function get(client: HttpClient, assetId: AssetId): Promise<EscrowedAsset> {
Expand Down
1 change: 1 addition & 0 deletions typescript/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ReadStream } from 'fs';

import AbortController from 'abort-controller';
import EventEmitter from 'eventemitter3';
import FormData from 'form-data';
import type { Readable } from 'readable-stream';
Expand Down
2 changes: 1 addition & 1 deletion typescript/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { paramCase } from 'param-case';
import type { PODDocument } from './document';
import type { JsonSerializable, Page } from './model.js';
import type { TokenProvider } from './token.js';
import { pipeToPolyfill } from './polyfill.js';

const DEFAULT_API_URL =
globalThis?.process?.env?.PARCEL_API_URL ?? 'https://api.oasislabs.com/parcel/v1';
Expand Down Expand Up @@ -329,6 +328,7 @@ export class Download implements AsyncIterable<Uint8Array> {
}

// Firefox's native ReadableStream is missing pipeTo.
const pipeToPolyfill = (await import('./polyfill.js')).pipeToPolyfill;
return pipeToPolyfill(body, sink);
}

Expand Down
13 changes: 0 additions & 13 deletions typescript/src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,8 @@ import {
createReadableStreamWrapper,
createWritableStreamWrapper,
} from '@mattiasbuelens/web-streams-adapter';
import AbortController from 'abort-controller';
import FormData from 'form-data';
import fetch, { Headers, Request as RequestPF, Response as ResponsePF } from 'node-fetch';
import * as webStreams from 'web-streams-polyfill/dist/ponyfill.es2018.js';

globalThis.fetch =
globalThis.fetch ??
(async (url: any, options) => fetch(url, { highWaterMark: 1e7 /* 10 MB */, ...options } as any));
globalThis.Headers = globalThis.Headers ?? Headers;
globalThis.Request = globalThis.Request ?? RequestPF;
globalThis.Response = globalThis.Response ?? ResponsePF;
globalThis.AbortController = globalThis.AbortController ?? AbortController;
globalThis.FormData = globalThis.FormData ?? FormData;
globalThis.ReadableStream = globalThis.ReadableStream ?? webStreams.ReadableStream;
globalThis.WritableStream = globalThis.WritableStream ?? webStreams.WritableStream;
const toReadableStreamPF = createReadableStreamWrapper(webStreams.ReadableStream) as unknown as (
r: globalThis.ReadableStream,
) => webStreams.ReadableStream;
Expand Down
3 changes: 2 additions & 1 deletion typescript/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import type { JsonObject, Merge } from 'type-fest';

import { appendAsLastBeforeRequest, dontCloneForAfterResponses } from './http.js';
import type { IdentityId } from './identity.js';
import './polyfill.js'; // eslint-disable-line import/no-unassigned-import
// @ts-ignore
if (!globalThis.fetch) import('./polyfill.js'); // eslint-disable-line import/no-unassigned-import

export class TokenError extends HTTPError {
name = 'TokenError';
Expand Down
12 changes: 11 additions & 1 deletion typescript/test/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ const config = {
'\\.ts$': 'ts-jest',
'\\.js$': 'babel-jest',
},
transformIgnorePatterns: ['<rootDir>/node_modules/?!(ky|node-fetch)'],
transformIgnorePatterns: ['<rootDir>/node_modules/?!(node-fetch)'],

// cf. https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
useESM: true,
},
},
// cf. https:/sindresorhus/ky-universal/issues/35
setupFiles: ["./test/jest.setup.js"],
};

export default config;
8 changes: 8 additions & 0 deletions typescript/test/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// cf. https:/sindresorhus/ky-universal/issues/35

import fetch from 'node-fetch';

globalThis.fetch = fetch;
globalThis.Request = fetch.Request;
globalThis.Response = fetch.Response;
globalThis.Headers = fetch.Headers;
1 change: 1 addition & 0 deletions typescript/test/jest/unit/token.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jwt from 'jsonwebtoken';
import { KEYUTIL } from 'jsrsasign';
import nock from 'nock';
import ky from 'ky-universal';

import {
PARCEL_RUNTIME_AUD,
Expand Down
Loading