Skip to content

Commit

Permalink
Add js to imports and fix dsnp export paths (#39)
Browse files Browse the repository at this point in the history
Problem
=======

To make sure that the generated javascript can be used directly with
node 16+ and in browsers when using es modules, we needed to use the
full path

See:
https://nodejs.org/dist/latest-v14.x/docs/api/esm.html#esm_resolution_algorithm

Solution
========
- Added the `.js`
- Added the linter to enforce it

Steps to Verify:
----------------
1. `npm run build`
1. See that the outputs are correct
1. `cd dist && npm pack`
1. Install the generated dsnp-frequency-schemas-0.0.0.tgz file in a
package that runs with node js
  • Loading branch information
wilwade authored Jul 12, 2023
1 parent f77612b commit 3fecf04
Show file tree
Hide file tree
Showing 28 changed files with 62 additions and 50 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
}
],
"allow-namespace": "off",
"import/extensions": ["error", "always", { "ignorePackages": true } ]
}
}
4 changes: 2 additions & 2 deletions cli/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getFrequencyAPI, getSignerAccountKeys } from "./services/connect";
import dsnp, { SchemaName as DsnpSchemaName } from "../dsnp";
import { getFrequencyAPI, getSignerAccountKeys } from "./services/connect.js";
import dsnp, { SchemaName as DsnpSchemaName } from "../dsnp/index.js";
import { EventRecord } from "@polkadot/types/interfaces";

export const deploy = async () => {
Expand Down
2 changes: 1 addition & 1 deletion cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deploy } from "./deploy";
import { deploy } from "./deploy.js";

export const main = async () => {
await deploy();
Expand Down
4 changes: 2 additions & 2 deletions cli/read.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getEndpoint, getFrequencyAPI } from "./services/connect";
import { getEndpoint, getFrequencyAPI } from "./services/connect.js";

import stringify from "json-stringify-pretty-compact";

import { schemas } from "../dsnp";
import { schemas } from "../dsnp/index.js";

const nameAndSchema = Array.from(schemas.entries(), ([k, v]) => [`dsnp.${k}`, JSON.stringify(v.model)]);

Expand Down
4 changes: 2 additions & 2 deletions dsnp/broadcast.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import broadcastSchema from "./broadcast";
import broadcastSchema from "./broadcast.js";

describe("Broadcast Spec", () => {
testParquetSchema(broadcastSchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const broadcast: FrequencyParquetSchema = [
{
Expand Down
2 changes: 1 addition & 1 deletion dsnp/graphChange.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import graphChangeSchema from "./graphChange";
import graphChangeSchema from "./graphChange.js";
import avro from "avro-js";

describe("Graph Change Schema", () => {
Expand Down
24 changes: 12 additions & 12 deletions dsnp/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

import broadcast from "./broadcast";
import broadcast from "./broadcast.js";
// Deprecated
// import graphChange from "./dsnp/graphChange";
import profile from "./profile";
import reaction from "./reaction";
import reply from "./reply";
import tombstone from "./tombstone";
import publicKey from "./publicKey";
import userPublicFollows from "./userPublicFollows";
import userPrivateFollows from "./userPrivateFollows";
import userPrivateConnections from "./userPrivateConnections";
import update from "./update";
// import graphChange from "./dsnp/graphChange.js";
import profile from "./profile.js";
import reaction from "./reaction.js";
import reply from "./reply.js";
import tombstone from "./tombstone.js";
import publicKey from "./publicKey.js";
import userPublicFollows from "./userPublicFollows.js";
import userPrivateFollows from "./userPrivateFollows.js";
import userPrivateConnections from "./userPrivateConnections.js";
import update from "./update.js";

export {
broadcast,
Expand Down
4 changes: 2 additions & 2 deletions dsnp/profile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import profileSchema from "./profile";
import profileSchema from "./profile.js";

describe("Profile Spec", () => {
testParquetSchema(profileSchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const profile: FrequencyParquetSchema = [
{
Expand Down
2 changes: 1 addition & 1 deletion dsnp/publicKey.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import publicKeySchema from "./publicKey";
import publicKeySchema from "./publicKey.js";
import avro from "avro-js";

describe("Public Key Schema", () => {
Expand Down
4 changes: 2 additions & 2 deletions dsnp/reaction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import reactionSchema from "./reaction";
import reactionSchema from "./reaction.js";

describe("Reaction Spec", () => {
testParquetSchema(reactionSchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/reaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const reaction: FrequencyParquetSchema = [
{
Expand Down
4 changes: 2 additions & 2 deletions dsnp/reply.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import replySchema from "./reply";
import replySchema from "./reply.js";

describe("Reply Spec", () => {
testParquetSchema(replySchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/reply.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const reply: FrequencyParquetSchema = [
{
Expand Down
4 changes: 2 additions & 2 deletions dsnp/tombstone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import tombstoneSchema from "./tombstone";
import tombstoneSchema from "./tombstone.js";

describe("Tombstone Spec", () => {
testParquetSchema(tombstoneSchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/tombstone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const tombstone: FrequencyParquetSchema = [
{
Expand Down
4 changes: 2 additions & 2 deletions dsnp/update.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testCompression, testParquetSchema } from "../test/parquet";
import { testCompression, testParquetSchema } from "../test/parquet.js";
import * as generators from "@dsnp/test-generators";
import updateSchema from "./update";
import updateSchema from "./update.js";

describe("Update Spec", () => {
testParquetSchema(updateSchema);
Expand Down
2 changes: 1 addition & 1 deletion dsnp/update.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FrequencyParquetSchema } from "../types/frequency";
import { FrequencyParquetSchema } from "../types/frequency.js";

const update: FrequencyParquetSchema = [
{
Expand Down
2 changes: 1 addition & 1 deletion dsnp/userPrivateConnections.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import privateConnectionsSchema from "./userPrivateConnections";
import privateConnectionsSchema from "./userPrivateConnections.js";
import avro from "avro-js";

describe("Private Connections Schema", () => {
Expand Down
2 changes: 1 addition & 1 deletion dsnp/userPrivateFollows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import privateFollowsSchema from "./userPrivateFollows";
import privateFollowsSchema from "./userPrivateFollows.js";
import avro from "avro-js";

describe("Private Follows Schema", () => {
Expand Down
2 changes: 1 addition & 1 deletion dsnp/userPublicFollows.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import publicFollowsSchema from "./userPublicFollows";
import publicFollowsSchema from "./userPublicFollows.js";
import avro from "avro-js";

describe("Public Follows Schema", () => {
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as dsnp from "./dsnp";
export * as parquet from "./parquet";
export * as dsnp from "./dsnp/index.js";
export * as parquet from "./parquet.js";
5 changes: 4 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ module.exports = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'jsdom',
setupFiles: ["./test/jestSetup.cjs"]
setupFiles: ["./test/jestSetup.cjs"],
moduleNameMapper: {
"^(\\.\\.?\\/.+)\\.js$": "$1"
}
};
4 changes: 2 additions & 2 deletions parquet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import { ParquetWriter, ParquetReader } from "@dsnp/parquetjs";
import { fromFrequencySchema } from "./parquet";
import { broadcast } from "./dsnp";
import { fromFrequencySchema } from "./parquet.js";
import { broadcast } from "./dsnp/index.js";

describe("Frequency Schema Conversion Test File", () => {
const [parquetSchema, writerOptions] = fromFrequencySchema(broadcast);
Expand Down
4 changes: 2 additions & 2 deletions parquet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ParquetSchema } from "@dsnp/parquetjs";
import type { ParquetType, FieldDefinition, SchemaDefinition, WriterOptions } from "@dsnp/parquetjs/dist/lib/declare";
import type { createSBBFParams } from "@dsnp/parquetjs/dist/lib/bloomFilterIO/bloomFilterWriter";
import { FrequencyParquetSchema, FrequencyParquetType, ParquetColumn } from "./types/frequency";
import * as dsnp from "./dsnp";
import { FrequencyParquetSchema, FrequencyParquetType, ParquetColumn } from "./types/frequency.js";
import * as dsnp from "./dsnp/index.js";

/**
* All supported types from Parquetjs
Expand Down
10 changes: 9 additions & 1 deletion scripts/package.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ rootPackage["exports"] = {
};

// Submodules
["parquet", "dsnp/index", "cli/deploy"].forEach((sub) => {
["parquet", "cli/deploy"].forEach((sub) => {
rootPackage["exports"][`./${sub}`] = {
types: `./${sub}.d.ts`,
require: `./cjs/${sub}.js`,
Expand All @@ -42,6 +42,14 @@ rootPackage["exports"] = {
};
});

// DSNP module
rootPackage["exports"][`./dsnp`] = {
types: `./dsnp/index.d.ts`,
require: `./cjs/dsnp/index.js`,
import: `./esm/dsnp/index.js`,
default: `./esm/dsnp/index.js`,
};

// Write it out
fs.writeFileSync(`${path.join(__dirname, "../dist", "package.json")}`, JSON.stringify(rootPackage, null, 2), (err) => {
if (err) throw new Error(err);
Expand Down
4 changes: 2 additions & 2 deletions test/parquet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable jest/no-export */
import fs from "fs";
import { ParquetWriter } from "@dsnp/parquetjs";
import { FrequencyParquetSchema } from "../types/frequency";
import { fromFrequencySchema } from "../parquet";
import { FrequencyParquetSchema } from "../types/frequency.js";
import { fromFrequencySchema } from "../parquet.js";

type RowGenerator = () => Record<string, unknown>;

Expand Down

0 comments on commit 3fecf04

Please sign in to comment.