Skip to content

migration to ESM #218

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion .mocharc.js → .mocharc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ require("tsconfig-paths/register");

module.exports = {
diff: true,
extension: ['ts'],
extension: ['mts'],
package: '../package.json',
timeout: 10000,

"node-option": [
"loader=ts-node/esm",
],
};
2 changes: 1 addition & 1 deletion example/webpack-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"paths": {
"@msgpack/msgpack": ["../../"]
"@msgpack/msgpack": ["../../index.mjs"]
}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
Expand Down
11 changes: 6 additions & 5 deletions karma.conf.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// import * as path from "path";
const path = require("path");
// const webpack = require("webpack");

// eslint-disable-next-line import/no-default-export
export default function configure(config: any) {
module.exports = function configure(config: any) {
config.set({
customLaunchers: {
// To debug it wih IE11,
Expand All @@ -18,10 +19,10 @@ export default function configure(config: any) {

basePath: "",
frameworks: ["mocha"],
files: ["./test/karma-run.ts"],
files: ["./test/karma-run.mts"],
exclude: [],
preprocessors: {
"**/*.ts": ["webpack", "sourcemap"],
"**/*.mts": ["webpack", "sourcemap"],
},
reporters: ["dots"],
port: 9876,
Expand All @@ -36,7 +37,7 @@ export default function configure(config: any) {
mode: "production",

resolve: {
extensions: [".ts", ".tsx", ".mjs", ".js", ".json", ".wasm"],
extensions: [".ts", ".tsx", ".mts", ".mjs", ".js", ".json", ".wasm"],
alias: {
"@msgpack/msgpack": path.resolve(__dirname, "src"),
},
Expand Down Expand Up @@ -64,7 +65,7 @@ export default function configure(config: any) {
devtool: "inline-source-map",
},
mime: {
"text/x-typescript": ["ts", "tsx"],
"text/x-typescript": ["ts", "tsx", "mts"],
},
client: {
mocha: {
Expand Down
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"description": "MessagePack for ECMA-262/JavaScript/TypeScript",
"author": "The MessagePack community",
"license": "ISC",
"type": "module",
"exports": {
"import": "./dist.es5+esm/index.mjs",
"require": "./dist/index.js"
},
"main": "./dist/index.js",
"module": "./dist.es5+esm/index.mjs",
"cdn": "./dist.es5+umd/msgpack.min.js",
Expand All @@ -12,12 +17,12 @@
"sideEffects": false,
"scripts": {
"build": "npm publish --dry-run",
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json && ts-node tools/esmify.ts dist.es5+esm/*.js dist.es5+esm/*/*.js",
"prepare": "npm run clean && webpack --bail && tsc --build tsconfig.dist.json tsconfig.dist.es5+esm.json",
"prepublishOnly": "run-p 'test:dist:*' && npm run test:browser",
"clean": "rimraf build dist dist.*",
"test": "mocha 'test/**/*.test.ts'",
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.ts'",
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.ts'",
"test": "mocha 'test/**/*.test.mts'",
"test:purejs": "TEXT_ENCODING=never mocha 'test/**/*.test.mts'",
"test:te": "TEXT_ENCODING=force mocha 'test/**/*.test.mts'",
"test:dist:purejs": "TS_NODE_PROJECT=tsconfig.test-dist-es5-purejs.json npm run test:purejs -- --reporter=dot",
"test:cover": "npm run cover:clean && npm-run-all 'test:cover:*' && npm run cover:report",
"test:cover:purejs": "npx nyc --no-clean npm run test:purejs",
Expand All @@ -30,9 +35,9 @@
"test:browser:firefox": "karma start --single-run --browsers FirefoxHeadless",
"test:browser:chrome": "karma start --single-run --browsers ChromeHeadless",
"test:watch:browser": "karma start --browsers ChromeHeadless,FirefoxHeadless",
"test:watch:nodejs": "mocha -w 'test/**/*.test.ts'",
"lint": "eslint --ext .ts src test",
"lint:fix": "prettier --loglevel=warn --write 'src/**/*.ts' 'test/**/*.ts' && eslint --fix --ext .ts src test",
"test:watch:nodejs": "mocha -w 'test/**/*.test.mts'",
"lint": "eslint --ext .ts --ext .mts src test",
"lint:fix": "prettier --loglevel=warn --write 'src/**/*.mts' 'test/**/*.mts' && eslint --fix --ext .ts --ext .mts src test",
"lint:print-config": "eslint --print-config .eslintrc.js",
"update-dependencies": "npx rimraf node_modules/ package-lock.json ; npm install ; npm audit fix --force ; git restore package.json ; npm install"
},
Expand All @@ -51,7 +56,7 @@
"universal"
],
"engines": {
"node": ">= 10"
"node": ">= 14"
},
"devDependencies": {
"@bitjourney/check-es-version-webpack-plugin": "latest",
Expand Down
2 changes: 1 addition & 1 deletion src/CachedKeyDecoder.ts → src/CachedKeyDecoder.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utf8DecodeJs } from "./utils/utf8";
import { utf8DecodeJs } from "./utils/utf8.mjs";

const DEFAULT_MAX_KEY_LENGTH = 16;
const DEFAULT_MAX_LENGTH_PER_KEY = 16;
Expand Down
File renamed without changes.
14 changes: 7 additions & 7 deletions src/Decoder.ts → src/Decoder.mts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { prettyByte } from "./utils/prettyByte";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
import { getInt64, getUint64, UINT32_MAX } from "./utils/int";
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8";
import { createDataView, ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
import { DecodeError } from "./DecodeError";
import { prettyByte } from "./utils/prettyByte.mjs";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.mjs";
import { getInt64, getUint64, UINT32_MAX } from "./utils/int.mjs";
import { utf8DecodeJs, TEXT_DECODER_THRESHOLD, utf8DecodeTD } from "./utils/utf8.mjs";
import { createDataView, ensureUint8Array } from "./utils/typedArrays.mjs";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder.mjs";
import { DecodeError } from "./DecodeError.mjs";

const STATE_ARRAY = "array";
const STATE_MAP_KEY = "map_key";
Expand Down
10 changes: 5 additions & 5 deletions src/Encoder.ts → src/Encoder.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
import { setInt64, setUint64 } from "./utils/int";
import { ensureUint8Array } from "./utils/typedArrays";
import type { ExtData } from "./ExtData";
import { utf8EncodeJs, utf8Count, TEXT_ENCODER_THRESHOLD, utf8EncodeTE } from "./utils/utf8.mjs";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.mjs";
import { setInt64, setUint64 } from "./utils/int.mjs";
import { ensureUint8Array } from "./utils/typedArrays.mjs";
import type { ExtData } from "./ExtData.mjs";

export const DEFAULT_MAX_DEPTH = 100;
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048;
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/ExtensionCodec.ts → src/ExtensionCodec.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ExtensionCodec to handle MessagePack extensions

import { ExtData } from "./ExtData";
import { timestampExtension } from "./timestamp";
import { ExtData } from "./ExtData.mjs";
import { timestampExtension } from "./timestamp.mjs";

export type ExtensionDecoderType<ContextType> = (
data: Uint8Array,
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions src/decode.ts → src/decode.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Decoder } from "./Decoder";
import type { ExtensionCodecType } from "./ExtensionCodec";
import type { ContextOf, SplitUndefined } from "./context";
import { Decoder } from "./Decoder.mjs";
import type { ExtensionCodecType } from "./ExtensionCodec.mjs";
import type { ContextOf, SplitUndefined } from "./context.mjs";

export type DecodeOptions<ContextType = undefined> = Readonly<
Partial<{
Expand Down
12 changes: 6 additions & 6 deletions src/decodeAsync.ts → src/decodeAsync.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Decoder } from "./Decoder";
import { ensureAsyncIterable } from "./utils/stream";
import { defaultDecodeOptions } from "./decode";
import type { ReadableStreamLike } from "./utils/stream";
import type { DecodeOptions } from "./decode";
import type { SplitUndefined } from "./context";
import { Decoder } from "./Decoder.mjs";
import { ensureAsyncIterable } from "./utils/stream.mjs";
import { defaultDecodeOptions } from "./decode.mjs";
import type { ReadableStreamLike } from "./utils/stream.mjs";
import type { DecodeOptions } from "./decode.mjs";
import type { SplitUndefined } from "./context.mjs";

/**
* @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
Expand Down
6 changes: 3 additions & 3 deletions src/encode.ts → src/encode.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Encoder } from "./Encoder";
import type { ExtensionCodecType } from "./ExtensionCodec";
import type { ContextOf, SplitUndefined } from "./context";
import { Encoder } from "./Encoder.mjs";
import type { ExtensionCodecType } from "./ExtensionCodec.mjs";
import type { ContextOf, SplitUndefined } from "./context.mjs";

export type EncodeOptions<ContextType = undefined> = Partial<
Readonly<{
Expand Down
24 changes: 12 additions & 12 deletions src/index.ts → src/index.mts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// Main Functions:

import { encode } from "./encode";
import { encode } from "./encode.mjs";
export { encode };
import type { EncodeOptions } from "./encode";
import type { EncodeOptions } from "./encode.mjs";
export type { EncodeOptions };

import { decode, decodeMulti } from "./decode";
import { decode, decodeMulti } from "./decode.mjs";
export { decode, decodeMulti };
import type { DecodeOptions } from "./decode";
import type { DecodeOptions } from "./decode.mjs";
export { DecodeOptions };

import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync";
import { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream } from "./decodeAsync.mjs";
export { decodeAsync, decodeArrayStream, decodeMultiStream, decodeStream };

import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder";
import { DecodeError } from "./DecodeError";
import { Decoder, DataViewIndexOutOfBoundsError } from "./Decoder.mjs";
import { DecodeError } from "./DecodeError.mjs";
export { Decoder, DecodeError, DataViewIndexOutOfBoundsError };

import { Encoder } from "./Encoder";
import { Encoder } from "./Encoder.mjs";
export { Encoder };

// Utilitiies for Extension Types:

import { ExtensionCodec } from "./ExtensionCodec";
import { ExtensionCodec } from "./ExtensionCodec.mjs";
export { ExtensionCodec };
import type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec";
import type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType } from "./ExtensionCodec.mjs";
export type { ExtensionCodecType, ExtensionDecoderType, ExtensionEncoderType };
import { ExtData } from "./ExtData";
import { ExtData } from "./ExtData.mjs";
export { ExtData };

import {
Expand All @@ -36,7 +36,7 @@ import {
decodeTimestampToTimeSpec,
encodeTimestampExtension,
decodeTimestampExtension,
} from "./timestamp";
} from "./timestamp.mjs";
export {
EXT_TIMESTAMP,
encodeDateToTimeSpec,
Expand Down
4 changes: 2 additions & 2 deletions src/timestamp.ts → src/timestamp.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
import { DecodeError } from "./DecodeError";
import { getInt64, setInt64 } from "./utils/int";
import { DecodeError } from "./DecodeError.mjs";
import { getInt64, setInt64 } from "./utils/int.mjs";

export const EXT_TIMESTAMP = -1;

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion src/utils/utf8.ts → src/utils/utf8.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unnecessary-condition */
import { UINT32_MAX } from "./int";
import { UINT32_MAX } from "./int.mjs";

const TEXT_ENCODING_AVAILABLE =
(typeof process === "undefined" || process?.env?.["TEXT_ENCODING"] !== "never") &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { CachedKeyDecoder, KeyDecoder } from "../src/CachedKeyDecoder";
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8";
import { CachedKeyDecoder, KeyDecoder } from "../src/CachedKeyDecoder.mjs";
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8.mjs";

function tryDecode(keyDecoder: KeyDecoder, str: string): string {
const byteLength = utf8Count(str);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import util from "util";
import { encode, decode, ExtensionCodec, decodeAsync } from "../src";
import { encode, decode, ExtensionCodec, decodeAsync } from "../src/index.mjs";

describe("ExtensionCodec", () => {
context("timestamp", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/codec-bigint.test.ts → test/codec-bigint.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decode, ExtensionCodec, DecodeError } from "../src";
import { encode, decode, ExtensionCodec, DecodeError } from "../src/index.mjs";

const extensionCodec = new ExtensionCodec();
extensionCodec.register({
Expand Down
3 changes: 2 additions & 1 deletion test/codec-float.test.ts → test/codec-float.test.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import assert from "assert";
import { decode } from "../src";
import * as ieee754 from "ieee754";

import { decode } from "../src/index.mjs";

const FLOAT32_TYPE = 0xca;
const FLOAT64_TYPE = 0xcb;

Expand Down
2 changes: 1 addition & 1 deletion test/codec-int.test.ts → test/codec-int.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { setInt64, getInt64, getUint64, setUint64 } from "../src/utils/int";
import { setInt64, getInt64, getUint64, setUint64 } from "../src/utils/int.mjs";

const INT64SPECS = {
ZERO: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
decodeTimestampExtension,
decodeTimestampToTimeSpec,
encodeTimestampExtension,
} from "../src";
} from "../src/index.mjs";

const TIME = 1556636810389;

Expand Down
2 changes: 1 addition & 1 deletion test/decode-blob.test.ts → test/decode-blob.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decode, decodeAsync } from "@msgpack/msgpack";
import { encode, decode, decodeAsync } from "../src/index.mjs";

(typeof Blob !== "undefined" ? describe : describe.skip)("Blob", () => {
it("decodes it with `decode()`", async function () {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { encode, decode, decodeAsync } from "../src";
import type { DecodeOptions } from "../src/decode";
import { encode, decode, decodeAsync } from "../src/index.mjs";
import type { DecodeOptions } from "../src/index.mjs";

describe("decode with max${Type}Length specified", () => {
async function* createStream<T>(input: T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decodeArrayStream } from "../src";
import { encode, decodeArrayStream } from "../src/index.mjs";

describe("decodeArrayStream", () => {
const generateSampleObject = () => {
Expand Down
2 changes: 1 addition & 1 deletion test/decodeAsync.test.ts → test/decodeAsync.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decodeAsync } from "../src";
import { encode, decodeAsync } from "../src/index.mjs";

describe("decodeAsync", () => {
function wrapWithNoisyBuffer(byte: number) {
Expand Down
2 changes: 1 addition & 1 deletion test/decodeMulti.test.ts → test/decodeMulti.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decodeMulti } from "@msgpack/msgpack";
import { encode, decodeMulti } from "../src/index.mjs";

describe("decodeMulti", () => {
it("decodes multiple objects in a single binary", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decodeMultiStream } from "@msgpack/msgpack";
import { encode, decodeMultiStream } from "../src/index.mjs";

describe("decodeStream", () => {
it("decodes stream", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/deno_test.ts → test/deno_test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* eslint-disable */
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
import * as msgpack from "../mod.ts";
import * as msgpack from "../src/index.mjs";

Deno.test("Hello, world!", () => {
const encoded = msgpack.encode("Hello, world!");
Expand Down
4 changes: 2 additions & 2 deletions test/edge-cases.test.ts → test/edge-cases.test.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// kind of hand-written fuzzing data
// any errors should not break Encoder/Decoder instance states
import assert from "assert";
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src";
import { DataViewIndexOutOfBoundsError } from "../src/Decoder";
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream, DataViewIndexOutOfBoundsError,
} from "../src/index.mjs";

function testEncoder(encoder: Encoder): void {
const object = {
Expand Down
2 changes: 1 addition & 1 deletion test/encode.test.ts → test/encode.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decode } from "@msgpack/msgpack";
import { encode, decode } from "../src/index.mjs";

describe("encode", () => {
context("sortKeys", () => {
Expand Down
Loading