diff --git a/packages/core/dist-link.js b/packages/core/dist-link.js deleted file mode 100644 index d1e9c27..0000000 --- a/packages/core/dist-link.js +++ /dev/null @@ -1,16 +0,0 @@ -import * as fs from 'fs'; - -const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf-8')); - -for (let f of fs.readdirSync('dist')) { - const prefix = `syndicate-${pkg.version}`; - if (f.startsWith(prefix)) { - const linkname = `dist/syndicate${f.substring(prefix.length)}`; - try { - fs.unlinkSync(linkname); - } catch (e) { - if (e.code !== 'ENOENT') throw e; - } - fs.symlinkSync(f, linkname); - } -} diff --git a/packages/core/examples/box-and-client.js b/packages/core/examples/box-and-client.js index 537f62f..8e700ba 100755 --- a/packages/core/examples/box-and-client.js +++ b/packages/core/examples/box-and-client.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --es-module-specifier-resolution=node +#!/usr/bin/env node //--------------------------------------------------------------------------- // @syndicate-lang/core, an implementation of Syndicate dataspaces for JS. // Copyright (C) 2016-2021 Tony Garnock-Jones @@ -17,7 +17,7 @@ // along with this program. If not, see . //--------------------------------------------------------------------------- -import { Dataspace, Skeleton, Ground, Record, Discard, Capture, Observe } from '../lib/index'; +const { Dataspace, Skeleton, Ground, Record, Discard, Capture, Observe } = require('../dist/syndicate.js'); const __ = Discard._instance; const _$ = Capture(__); @@ -28,7 +28,7 @@ const N = 100000; console.time('box-and-client-' + N.toString()); -export function boot(thisFacet) { +function boot(thisFacet) { thisFacet.spawn('box', function (thisFacet) { thisFacet.declareField(this, 'value', 0); thisFacet.addEndpoint(() => { @@ -87,4 +87,6 @@ export function boot(thisFacet) { console.timeEnd('box-and-client-' + N.toString())); } +module.exports.boot = boot; + new Ground(boot).start(); diff --git a/packages/core/package.json b/packages/core/package.json index 50d5653..38c566b 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -11,12 +11,12 @@ "scripts": { "prepare": "npm run compile && npm run rollup", "compile": "../../node_modules/.bin/tsc --incremental", - "rollup": "../../node_modules/.bin/rollup -c syndicate.dist.js && node ./dist-link.js", + "rollup": "../../node_modules/.bin/rollup -c", "test": "../../node_modules/.bin/jest", "cover": "../../node_modules/.bin/nyc --reporter=html ../../node_modules/.bin/jest" }, - "type": "module", - "main": "lib/index.js", + "main": "dist/syndicate.js", + "module": "lib/index.js", "types": "lib/index.d.ts", "author": "Tony Garnock-Jones ", "dependencies": { diff --git a/packages/core/rollup.config.js b/packages/core/rollup.config.js new file mode 100644 index 0000000..8084a2d --- /dev/null +++ b/packages/core/rollup.config.js @@ -0,0 +1,54 @@ +import resolve from '@rollup/plugin-node-resolve'; +import { terser } from 'rollup-plugin-terser'; + +function distfile(insertion) { + const f = `syndicate${insertion}.js`; + return `dist/${f}`; +} + +function umd(insertion, extra) { + return { + file: distfile(insertion), + format: 'umd', + name: 'Syndicate', + globals: { + 'crypto': 'crypto', + }, + ... (extra || {}) + }; +} + +function es6(insertion, extra) { + return { + file: distfile('.es6' + insertion), + format: 'es', + ... (extra || {}) + }; +} + +export default [ + { + input: 'lib/index.js', + plugins: [ + resolve({ + preferBuiltins: false, + }), + ], + output: [ + umd(''), + umd('.min', { plugins: [terser()] }), + ], + }, { + input: 'lib/index.js', + plugins: [ + resolve({ + moduleDirectories: ['stubs', 'node_modules'], + preferBuiltins: false, + }), + ], + output: [ + es6(''), + es6('.min', { plugins: [terser()] }), + ], + } +]; diff --git a/packages/core/src/compiler/main.ts b/packages/core/src/compiler/main.ts index 076946f..94c9ecd 100644 --- a/packages/core/src/compiler/main.ts +++ b/packages/core/src/compiler/main.ts @@ -1,4 +1,4 @@ -import fs from 'fs'; +import * as fs from 'fs'; import * as S from '../syntax/index.js'; import { Substitution } from '../syntax/index.js'; import * as G from './grammar.js'; diff --git a/packages/core/src/runtime/randomid.ts b/packages/core/src/runtime/randomid.ts index c09e5f0..f202813 100644 --- a/packages/core/src/runtime/randomid.ts +++ b/packages/core/src/runtime/randomid.ts @@ -27,17 +27,22 @@ export function _btoa(s: string): string { } } -export function randomId(byteCount: number, hexOutput: boolean = false): string { - let buf: Uint8Array; - if (node_crypto.randomBytes !== void 0) { - buf = node_crypto.randomBytes(byteCount); - } else { - buf = new Uint8Array(byteCount); - crypto.getRandomValues(buf); - } +function _finish(hexOutput: boolean, buf: Uint8Array): string { if (hexOutput) { return Bytes.from(buf).toHex(); } else { return _btoa(String.fromCharCode.apply(null, buf as unknown as number[])).replace(/=/g,''); } } + +export const randomId = + (typeof crypto !== 'undefined' && 'getRandomValues' in crypto) + ? ((byteCount: number, hexOutput: boolean = false): string => { + const buf = new Uint8Array(byteCount); + crypto.getRandomValues(buf); + return _finish(hexOutput, buf); + }) + : ((byteCount: number, hexOutput: boolean = false): string => { + let buf = node_crypto.randomBytes(byteCount); + return _finish(hexOutput, buf); + }); diff --git a/packages/core/syndicate.dist.js b/packages/core/syndicate.dist.js deleted file mode 100644 index 641b517..0000000 --- a/packages/core/syndicate.dist.js +++ /dev/null @@ -1,41 +0,0 @@ -import pkg from './package.json'; -import resolve from '@rollup/plugin-node-resolve'; -import { terser } from 'rollup-plugin-terser'; - -function distfile(insertion) { - const f = `syndicate-${pkg.version}${insertion}.js`; - return `dist/${f}`; -} - -function umd(insertion, extra) { - return { - file: distfile(insertion), - format: 'umd', - name: 'Syndicate', - ... (extra || {}) - }; -} - -function es6(insertion, extra) { - return { - file: distfile('.es6' + insertion), - format: 'es', - ... (extra || {}) - }; -} - -export default { - input: 'lib/index.js', - plugins: [ - resolve({ - moduleDirectories: ['stubs', 'node_modules'], - preferBuiltins: false, - }), - ], - output: [ - umd(''), - umd('.min', { plugins: [terser()] }), - es6(''), - es6('.min', { plugins: [terser()] }), - ], -}