feat(netstring)!: Update packaging for NESM/RESM bridge
BREAKING CHANGE: No longer supports direct use from CommonJS
MODIFY
packages/netstring/NEWS.md
packages/netstring/NEWS.md
## Next release
+- *BREAKING*: Removes CommonJS and UMD downgrade compatibility.
+ Supporting both Node.js ESM and the `node -r esm` shim requires the main
+ entry point module to be ESM regardless of environment.
+ UMD and CommonJS facets will likely return after all dependees have migrated
+ away from depending upon the `esm` JavaScript module emulator.
- Fixes a problem with the external visibility of TypeScript types.
## Release 0.1.0 (2021-04-26)

MODIFY
packages/netstring/index.d.ts
packages/netstring/index.d.ts
-export { netstringReader } from './src/reader.js';
-export { netstringWriter } from './src/writer.js';
+export * from './src/main.js';

MODIFY
packages/netstring/index.js
packages/netstring/index.js
-export { netstringReader } from './src/reader.js';
-export { netstringWriter } from './src/writer.js';
+export { netstringReader } from './reader.js';
+export { netstringWriter } from './writer.js';

MODIFY
packages/netstring/jsconfig.json
packages/netstring/jsconfig.json
"target": "esnext",
"module": "esnext",
"noEmit": true,
+ "downlevelIteration": true,
"strictNullChecks": true,
"moduleResolution": "node"
},

MODIFY
packages/netstring/package.json
packages/netstring/package.json
"url": "https://github.com/endojs/endo/issues"
},
"type": "module",
- "parsers": {
- "js": "mjs"
- },
- "main": "./dist/netstring.cjs",
+ "main": "./index.js",
"module": "./index.js",
- "browser": "./dist/netstring.umd.js",
- "unpkg": "./dist/netstring.umd.js",
"types": "./index.d.ts",
"exports": {
- "./package.json": "./package.json",
- ".": {
- "import": "./index.js",
- "require": "./dist/netstring.cjs",
- "browser": "./dist/netstring.umd.js"
- },
- "./reader": {
- "import": "./reader.js",
- "require": "./dist/netstring-reader.cjs",
- "browser": "./dist/netstring-reader.umd.js"
- },
- "./writer": {
- "import": "./writer.js",
- "require": "./dist/netstring-writer.cjs",
- "browser": "./dist/netstring-writer.umd.js"
- }
+ ".": "./index.js",
+ "./reader.js": "./reader.js",
+ "./writer.js": "./writer.js",
+ "./package.json": "./package.json"
},
"scripts": {
- "build": "rollup --config rollup.config.js",
- "clean": "rm -rf dist",
- "cover": "nyc ava",
+ "build": "exit 0",
"lint": "yarn lint:types && yarn lint:js",
- "lint-fix": "eslint --fix '*.js' '**/*.js' 'test*/**.js'",
- "lint:js": "eslint '*.js' '**/*.js' 'test*/**.js'",
+ "lint-fix": "eslint --fix '**/*.js'",
+ "lint:js": "eslint '**/*.js'",
"lint:types": "tsc --build jsconfig.json",
- "prepublish": "yarn clean && yarn build",
"test": "ava"
},
- "dependencies": {},
"devDependencies": {
- "@rollup/plugin-commonjs": "^13.0.0",
- "@rollup/plugin-node-resolve": "^6.1.0",
+ "@agoric/eslint-config": "^0.3.6",
"ava": "^3.12.1",
"babel-eslint": "^10.0.3",
"eslint": "^7.23.0",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-import": "^2.19.1",
"eslint-plugin-prettier": "^3.1.2",
- "nyc": "^15.1.0",
"prettier": "^1.19.1",
- "rollup": "1.31.0",
- "rollup-plugin-terser": "^5.1.3",
"typescript": "^4.0.5"
},
"files": [
"LICENSE*",
- "dist",
- "src",
- "types",
- "*.js",
- "*.d.ts"
+ "index.d.ts",
+ "index.js",
+ "reader.js",
+ "writer.js"
],
"publishConfig": {
"access": "public"

MODIFY
packages/netstring/reader.js
packages/netstring/reader.js
-export { netstringReader } from './src/reader.js';
+// @ts-check
+
+const COLON = ':'.charCodeAt(0);
+
+const decoder = new TextDecoder();
+
+/**
+ * @param {Iterable<Uint8Array> | AsyncIterable<Uint8Array>} input
+ * @param {string=} name
+ * @param {number=} capacity
+ * @returns {AsyncGenerator<Uint8Array, Uint8Array, unknown>} input
+ */
+export async function* netstringReader(
+ input,
+ name = '<unknown>',
+ capacity = 1024,
+) {
+ let length = 0;
+ let buffer = new Uint8Array(capacity);
+ let offset = 0;
+
+ for await (const chunk of input) {
+ if (length + chunk.byteLength >= capacity) {
+ while (length + chunk.byteLength >= capacity) {
+ capacity *= 2;
+ }
+ const replacement = new Uint8Array(capacity);
+ replacement.set(buffer, 0);
+ buffer = replacement;
+ }
+ buffer.set(chunk, length);
+ length += chunk.byteLength;
+
+ let drained = false;
+ while (!drained && length > 0) {
+ const colon = buffer.indexOf(COLON);
+ if (colon === 0) {
+ throw new Error(
+ `Expected number before colon at offset ${offset} of ${name}`,
+ );
+ } else if (colon > 0) {
+ const prefixBytes = buffer.subarray(0, colon);
+ const prefixString = decoder.decode(prefixBytes);
+ const contentLength = +prefixString;
+ if (Number.isNaN(contentLength)) {
+ throw new Error(
+ `Invalid netstring prefix length ${prefixString} at offset ${offset} of ${name}`,
+ );
+ }
+ const messageLength = colon + contentLength + 2;
+ if (messageLength <= length) {
+ yield buffer.subarray(colon + 1, colon + 1 + contentLength);
+ buffer.copyWithin(0, messageLength);
+ length -= messageLength;
+ offset += messageLength;
+ } else {
+ drained = true;
+ }
+ } else {
+ drained = true;
+ }
+ }
+ }
+
+ if (length > 0) {
+ throw new Error(
+ `Unexpected dangling message at offset ${offset} of ${name}`,
+ );
+ }
+ return new Uint8Array(0);
+}

DELETE
packages/netstring/rollup.config.js
-import resolve from '@rollup/plugin-node-resolve';
-import commonjs from '@rollup/plugin-commonjs';
-import { terser } from 'rollup-plugin-terser';
-
-export default [
- {
- input: 'index.js',
- output: [
- {
- file: `dist/netstring.mjs`,
- format: 'esm',
- },
- {
- file: `dist/netstring.cjs`,
- format: 'cjs',
- },
- ],
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'index.js',
- output: {
- file: `dist/netstring.umd.js`,
- format: 'umd',
- name: 'Netstring',
- },
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'index.js',
- output: {
- file: `dist/netstring.umd.min.js`,
- format: 'umd',
- name: 'Netstring',
- },
- plugins: [resolve(), commonjs(), terser()],
- },
-
- {
- input: 'reader.js',
- output: [
- {
- file: `dist/netstring-reader.mjs`,
- format: 'esm',
- },
- {
- file: `dist/netstring-reader.cjs`,
- format: 'cjs',
- },
- ],
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'reader.js',
- output: {
- file: `dist/netstring-reader.umd.js`,
- format: 'umd',
- name: 'NetstringReader',
- },
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'reader.js',
- output: {
- file: `dist/netstring-reader.umd.min.js`,
- format: 'umd',
- name: 'NetstringReader',
- },
- plugins: [resolve(), commonjs(), terser()],
- },
-
- {
- input: 'writer.js',
- output: [
- {
- file: `dist/netstring-writer.mjs`,
- format: 'esm',
- },
- {
- file: `dist/netstring-writer.cjs`,
- format: 'cjs',
- },
- ],
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'writer.js',
- output: {
- file: `dist/netstring-writer.umd.js`,
- format: 'umd',
- name: 'NetstringWriter',
- },
- plugins: [resolve(), commonjs()],
- },
- {
- input: 'writer.js',
- output: {
- file: `dist/netstring-writer.umd.min.js`,
- format: 'umd',
- name: 'NetstringWriter',
- },
- plugins: [resolve(), commonjs(), terser()],
- },
-];

DELETE
packages/netstring/src/reader.js
-// @ts-check
-
-const COLON = ':'.charCodeAt(0);
-
-const decoder = new TextDecoder();
-
-/**
- * @param {Iterable<Uint8Array> | AsyncIterable<Uint8Array>} input
- * @param {string=} name
- * @param {number=} capacity
- * @returns {AsyncGenerator<Uint8Array, Uint8Array, unknown>} input
- */
-export async function* netstringReader(
- input,
- name = '<unknown>',
- capacity = 1024,
-) {
- let length = 0;
- let buffer = new Uint8Array(capacity);
- let offset = 0;
-
- for await (const chunk of input) {
- if (length + chunk.byteLength >= capacity) {
- while (length + chunk.byteLength >= capacity) {
- capacity *= 2;
- }
- const replacement = new Uint8Array(capacity);
- replacement.set(buffer, 0);
- buffer = replacement;
- }
- buffer.set(chunk, length);
- length += chunk.byteLength;
-
- let drained = false;
- while (!drained && length > 0) {
- const colon = buffer.indexOf(COLON);
- if (colon === 0) {
- throw new Error(
- `Expected number before colon at offset ${offset} of ${name}`,
- );
- } else if (colon > 0) {
- const prefixBytes = buffer.subarray(0, colon);
- const prefixString = decoder.decode(prefixBytes);
- const contentLength = +prefixString;
- if (Number.isNaN(contentLength)) {
- throw new Error(
- `Invalid netstring prefix length ${prefixString} at offset ${offset} of ${name}`,
- );
- }
- const messageLength = colon + contentLength + 2;
- if (messageLength <= length) {
- yield buffer.subarray(colon + 1, colon + 1 + contentLength);
- buffer.copyWithin(0, messageLength);
- length -= messageLength;
- offset += messageLength;
- } else {
- drained = true;
- }
- } else {
- drained = true;
- }
- }
- }
-
- if (length > 0) {
- throw new Error(
- `Unexpected dangling message at offset ${offset} of ${name}`,
- );
- }
- return new Uint8Array(0);
-}

DELETE
packages/netstring/src/writer.js
-// @ts-check
-
-const COLON = ':'.charCodeAt(0);
-const COMMA = ','.charCodeAt(0);
-
-const encoder = new TextEncoder();
-
-/**
- * @template T
- * @template U
- * @template V
- * @typedef {{
- * next(value: U): Promise<IteratorResult<T>>,
- * return(value: V): Promise<IteratorResult<T>>,
- * throw(error: Error): Promise<IteratorResult<T>>,
- * [Symbol.asyncIterator](): Stream<T, U, V>
- * }} Stream
- */
-
-/**
- * @param {Stream<void, Uint8Array, void>} output
- * @returns {Stream<void, Uint8Array, void>}
- */
-export function netstringWriter(output) {
- const scratch = new Uint8Array(8);
-
- return {
- async next(message) {
- const { written: length = 0 } = encoder.encodeInto(
- `${message.byteLength}`,
- scratch,
- );
- scratch[length] = COLON;
-
- const { done: done1 } = await output.next(
- scratch.subarray(0, length + 1),
- );
- if (done1) {
- return output.return();
- }
-
- const { done: done2 } = await output.next(message);
- if (done2) {
- return output.return();
- }
-
- scratch[0] = COMMA;
- return output.next(scratch.subarray(0, 1));
- },
- async return() {
- return output.return();
- },
- async throw(error) {
- return output.throw(error);
- },
- [Symbol.asyncIterator]() {
- return this;
- },
- };
-}

MODIFY
packages/netstring/test/test-netstring.js
packages/netstring/test/test-netstring.js
// @ts-check
import test from 'ava';
-import { netstringReader } from '../src/reader.js';
-import { netstringWriter } from '../src/writer.js';
+import { netstringReader } from '../reader.js';
+import { netstringWriter } from '../writer.js';
import { pipe } from './stream.js';
const encoder = new TextEncoder();

MODIFY
packages/netstring/writer.js
packages/netstring/writer.js
-export { netstringWriter } from './src/writer.js';
+// @ts-check
+
+const COLON = ':'.charCodeAt(0);
+const COMMA = ','.charCodeAt(0);
+
+const encoder = new TextEncoder();
+
+/**
+ * @template T
+ * @template U
+ * @template V
+ * @typedef {{
+ * next(value: U): Promise<IteratorResult<T>>,
+ * return(value: V): Promise<IteratorResult<T>>,
+ * throw(error: Error): Promise<IteratorResult<T>>,
+ * [Symbol.asyncIterator](): Stream<T, U, V>
+ * }} Stream
+ */
+
+/**
+ * @param {Stream<void, Uint8Array, void>} output
+ * @returns {Stream<void, Uint8Array, void>}
+ */
+export function netstringWriter(output) {
+ const scratch = new Uint8Array(8);
+
+ return {
+ async next(message) {
+ const { written: length = 0 } = encoder.encodeInto(
+ `${message.byteLength}`,
+ scratch,
+ );
+ scratch[length] = COLON;
+
+ const { done: done1 } = await output.next(
+ scratch.subarray(0, length + 1),
+ );
+ if (done1) {
+ return output.return();
+ }
+
+ const { done: done2 } = await output.next(message);
+ if (done2) {
+ return output.return();
+ }
+
+ scratch[0] = COMMA;
+ return output.next(scratch.subarray(0, 1));
+ },
+ async return() {
+ return output.return();
+ },
+ async throw(error) {
+ return output.throw(error);
+ },
+ [Symbol.asyncIterator]() {
+ return this;
+ },
+ };
+}

feat(netstring)!: Update packaging for NESM/RESM bridge

BREAKING CHANGE: No longer supports direct use from CommonJS