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

pure ESM-ify #265

Merged
merged 1 commit into from
Feb 12, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/CachedKeyDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { utf8DecodeJs } from "./utils/utf8";
import { utf8DecodeJs } from "./utils/utf8.ts";

const DEFAULT_MAX_KEY_LENGTH = 16;
const DEFAULT_MAX_LENGTH_PER_KEY = 16;
Expand Down
18 changes: 9 additions & 9 deletions src/Decoder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "./utils/symbol.dispose";
import { prettyByte } from "./utils/prettyByte";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
import { getInt64, getUint64, UINT32_MAX } from "./utils/int";
import { utf8Decode } from "./utils/utf8";
import { ensureUint8Array } from "./utils/typedArrays";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder";
import { DecodeError } from "./DecodeError";
import type { ContextOf } from "./context";
import "./utils/symbol.dispose.ts";
import { prettyByte } from "./utils/prettyByte.ts";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts";
import { getInt64, getUint64, UINT32_MAX } from "./utils/int.ts";
import { utf8Decode } from "./utils/utf8.ts";
import { ensureUint8Array } from "./utils/typedArrays.ts";
import { CachedKeyDecoder, KeyDecoder } from "./CachedKeyDecoder.ts";
import { DecodeError } from "./DecodeError.ts";
import type { ContextOf } from "./context.ts";

export type DecoderOptions<ContextType = undefined> = Readonly<
Partial<{
Expand Down
14 changes: 7 additions & 7 deletions src/Encoder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import "./utils/symbol.dispose";
import { utf8Count, utf8Encode } 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 type { ContextOf } from "./context";
import "./utils/symbol.dispose.ts";
import { utf8Count, utf8Encode } from "./utils/utf8.ts";
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec.ts";
import { setInt64, setUint64 } from "./utils/int.ts";
import { ensureUint8Array } from "./utils/typedArrays.ts";
import type { ExtData } from "./ExtData.ts";
import type { ContextOf } from "./context.ts";

export const DEFAULT_MAX_DEPTH = 100;
export const DEFAULT_INITIAL_BUFFER_SIZE = 2048;
Expand Down
4 changes: 2 additions & 2 deletions src/ExtensionCodec.ts
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.ts";
import { timestampExtension } from "./timestamp.ts";

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

/**
* It decodes a single MessagePack object in a buffer.
Expand Down
10 changes: 5 additions & 5 deletions src/decodeAsync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Decoder } from "./Decoder";
import { ensureAsyncIterable } from "./utils/stream";
import type { DecoderOptions } from "./Decoder";
import type { ReadableStreamLike } from "./utils/stream";
import type { SplitUndefined } from "./context";
import { Decoder } from "./Decoder.ts";
import { ensureAsyncIterable } from "./utils/stream.ts";
import type { DecoderOptions } from "./Decoder.ts";
import type { ReadableStreamLike } from "./utils/stream.ts";
import type { SplitUndefined } from "./context.ts";

/**
* @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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Encoder } from "./Encoder";
import type { EncoderOptions } from "./Encoder";
import type { SplitUndefined } from "./context";
import { Encoder } from "./Encoder.ts";
import type { EncoderOptions } from "./Encoder.ts";
import type { SplitUndefined } from "./context.ts";

/**
* It encodes `value` in the MessagePack format and
Expand Down
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// Main Functions:

import { encode } from "./encode";
import { encode } from "./encode.ts";
export { encode };

import { decode, decodeMulti } from "./decode";
import { decode, decodeMulti } from "./decode.ts";
export { decode, decodeMulti };

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

import { Decoder } from "./Decoder";
import { Decoder } from "./Decoder.ts";
export { Decoder };
import type { DecoderOptions } from "./Decoder";
import type { DecoderOptions } from "./Decoder.ts";
export type { DecoderOptions };
import { DecodeError } from "./DecodeError";
import { DecodeError } from "./DecodeError.ts";
export { DecodeError };

import { Encoder } from "./Encoder";
import { Encoder } from "./Encoder.ts";
export { Encoder };
import type { EncoderOptions } from "./Encoder";
import type { EncoderOptions } from "./Encoder.ts";
export type { EncoderOptions };

// Utilities for Extension Types:

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

import {
Expand All @@ -37,7 +37,7 @@ import {
decodeTimestampToTimeSpec,
encodeTimestampExtension,
decodeTimestampExtension,
} from "./timestamp";
} from "./timestamp.ts";
export {
EXT_TIMESTAMP,
encodeDateToTimeSpec,
Expand Down
4 changes: 2 additions & 2 deletions src/timestamp.ts
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.ts";
import { getInt64, setInt64 } from "./utils/int.ts";

export const EXT_TIMESTAMP = -1;

Expand Down
6 changes: 3 additions & 3 deletions test/CachedKeyDecoder.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "assert";
import { CachedKeyDecoder } from "../src/CachedKeyDecoder";
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8";
import type { KeyDecoder } from "../src/CachedKeyDecoder";
import { CachedKeyDecoder } from "../src/CachedKeyDecoder.ts";
import { utf8EncodeJs, utf8Count } from "../src/utils/utf8.ts";
import type { KeyDecoder } from "../src/CachedKeyDecoder.ts";

function tryDecode(keyDecoder: KeyDecoder, str: string): string {
const byteLength = utf8Count(str);
Expand Down
2 changes: 1 addition & 1 deletion test/ExtensionCodec.test.ts
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/index";
import { encode, decode, ExtensionCodec, decodeAsync } from "../src/index.ts";

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

describe("useBigInt64: true", () => {
before(function () {
Expand Down
2 changes: 1 addition & 1 deletion test/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { encode, decode } from "../src/index";
import { encode, decode } from "../src/index.ts";

test("Hello, world!", () => {
const encoded = encode("Hello, world!");
Expand Down
2 changes: 1 addition & 1 deletion test/codec-bigint.test.ts
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/index";
import { encode, decode, ExtensionCodec, DecodeError } from "../src/index.ts";

// There's a built-in `useBigInt64: true` option, but a custom codec might be
// better if you'd like to encode bigint to reduce the size of binaries.
Expand Down
2 changes: 1 addition & 1 deletion test/codec-float.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import * as ieee754 from "ieee754";
import { decode } from "../src/index";
import { decode } from "../src/index.ts";

const FLOAT32_TYPE = 0xca;
const FLOAT64_TYPE = 0xcb;
Expand Down
2 changes: 1 addition & 1 deletion test/codec-int.test.ts
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.ts";

const INT64SPECS = {
ZERO: 0,
Expand Down
2 changes: 1 addition & 1 deletion test/codec-timestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
decodeTimestampExtension,
decodeTimestampToTimeSpec,
encodeTimestampExtension,
} from "../src/index";
} from "../src/index.ts";

const TIME = 1556636810389;

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

(typeof Blob !== "undefined" ? describe : describe.skip)("Blob", () => {
it("decodes it with `decode()`", async function () {
Expand Down
4 changes: 2 additions & 2 deletions test/decode-max-length.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { encode, decode, decodeAsync } from "../src/index";
import type { DecoderOptions } from "../src/index";
import { encode, decode, decodeAsync } from "../src/index.ts";
import type { DecoderOptions } from "../src/index.ts";

describe("decode with max${Type}Length specified", () => {
async function* createStream<T>(input: T) {
Expand Down
4 changes: 2 additions & 2 deletions test/decode-raw-strings.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "assert";
import { encode, decode } from "../src/index";
import type { DecoderOptions } from "../src/index";
import { encode, decode } from "../src/index.ts";
import type { DecoderOptions } from "../src/index.ts";

describe("decode with rawStrings specified", () => {
const options = { rawStrings: true } satisfies DecoderOptions;
Expand Down
2 changes: 1 addition & 1 deletion test/decodeArrayStream.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decodeArrayStream } from "../src/index";
import { encode, decodeArrayStream } from "../src/index.ts";

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

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

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

describe("decodeStream", () => {
it("decodes stream", async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/edge-cases.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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/index";
import { encode, decodeAsync, decode, Encoder, Decoder, decodeMulti, decodeMultiStream } from "../src/index.ts";

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

describe("encode", () => {
context("sortKeys", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/msgpack-ext.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from "assert";
import { encode, decode, ExtData } from "../src/index";
import { encode, decode, ExtData } from "../src/index.ts";

function seq(n: number) {
const a: Array<number> = [];
Expand Down
2 changes: 1 addition & 1 deletion test/msgpack-test-suite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import assert from "assert";
import util from "util";
import { Exam } from "msgpack-test-js";
import { MsgTimestamp } from "msg-timestamp";
import { encode, decode, ExtensionCodec, EXT_TIMESTAMP, encodeTimeSpecToTimestamp } from "../src/index";
import { encode, decode, ExtensionCodec, EXT_TIMESTAMP, encodeTimeSpecToTimestamp } from "../src/index.ts";

const extensionCodec = new ExtensionCodec();
extensionCodec.register({
Expand Down
2 changes: 1 addition & 1 deletion test/prototype-pollution.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { throws } from "assert";
import { encode, decode, DecodeError } from "../src/index";
import { encode, decode, DecodeError } from "../src/index.ts";

describe("prototype pollution", () => {
context("__proto__ exists as a map key", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/reuse-instances-with-extensions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// https://github.com/msgpack/msgpack-javascript/issues/195

import { deepStrictEqual } from "assert";
import { Encoder, Decoder, ExtensionCodec } from "../src/index";
import { Encoder, Decoder, ExtensionCodec } from "../src/index.ts";

const MSGPACK_EXT_TYPE_BIGINT = 0;

Expand Down
2 changes: 1 addition & 1 deletion test/reuse-instances.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual } from "assert";
import { Encoder, Decoder, decode } from "../src/index";
import { Encoder, Decoder, decode } from "../src/index.ts";

const createStream = async function* (...args: any) {
for (const item of args) {
Expand Down
2 changes: 1 addition & 1 deletion test/whatwg-streams.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deepStrictEqual } from "assert";
import { decodeAsync, encode, decodeArrayStream } from "../src/index";
import { decodeAsync, encode, decodeArrayStream } from "../src/index.ts";

const isReadableStreamConstructorAvailable: boolean = (() => {
try {
Expand Down
6 changes: 3 additions & 3 deletions tools/fix-ext.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ for (const file of files) {
// .js => .mjs
const content = fs.readFileSync(file).toString("utf-8");
const newContent = content
.replace(/\bfrom "(\.\.?\/[^"]+)(?:\.js)?";/g, `from "$1.${ext}";`)
.replace(/\bimport "(\.\.?\/[^"]+)(?:\.js)?";/g, `import "$1.${ext}";`)
.replace(/\brequire\("(\.\.?\/[^"]+)(?:\.js)?"\)/g, `require("$1.${ext}");`)
.replace(/\bfrom "(\.\.?\/[^"]+)\.js";/g, `from "$1.${ext}";`)
.replace(/\bimport "(\.\.?\/[^"]+)\.js";/g, `import "$1.${ext}";`)
.replace(/\brequire\("(\.\.?\/[^"]+)\.js"\)/g, `require("$1.${ext}");`)
.replace(/\/\/# sourceMappingURL=(.+)\.js\.map$/, `//# sourceMappingURL=$1.${ext}.map`);
fs.writeFileSync(fileMjs, newContent);
fs.unlinkSync(file);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.dist.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": false,
"noEmitOnError": true,
"noEmit": false,
"rewriteRelativeImportExtensions": true,
"incremental": false
},
"include": ["src/**/*.ts"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig.dist.esm.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"declaration": true,
"noEmitOnError": true,
"noEmit": false,
"rewriteRelativeImportExtensions": true,
"incremental": false
},
"include": ["src/**/*.ts"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig.dist.webpack.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "ESNext",
"noEmitOnError": true,
"noEmit": false,
"allowImportingTsExtensions": false,
"outDir": "./build/webpack"
},
"include": ["src/**/*.ts"]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"noPropertyAccessFromIndexSignature": true,
"noImplicitOverride": true,
"verbatimModuleSyntax": false,
// "allowImportingTsExtensions": true,
"allowImportingTsExtensions": true,
"noEmit": true,

/* Module Resolution Options */
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.test-karma.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"outDir": "./build/karma",
"noEmitOnError": true,
"noEmit": false,
"allowImportingTsExtensions": false,
"outDir": "./build/karma",
"incremental": false
}
}
Loading