Skip to content

Commit

Permalink
Merge pull request #134 from kwonoj/feat-env-web
Browse files Browse the repository at this point in the history
feat(loadmodule): no longer accepts environment, use web target binary only
  • Loading branch information
kwonoj authored Jun 13, 2019
2 parents 44e970f + 9f27f15 commit 4c355d4
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 66 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
<a name="3.0.0"></a>
# [3.0.0](https://github.com/kwonoj/cld3-asm/compare/v2.0.1...v3.0.0) (2019-06-13)


### build

* **tsconfig:** update target ([f619336](https://github.com/kwonoj/cld3-asm/commit/f619336))


### Features

* **loadmodule:** no longer accepts environment ([4a6ecdc](https://github.com/kwonoj/cld3-asm/commit/4a6ecdc))


### BREAKING CHANGES

* **tsconfig:** runtime requires es2018
* **loadmodule:** do not support environment override



<a name="2.0.1"></a>
## [2.0.1](https://github.com/kwonoj/cld3-asm/compare/v2.0.0...v2.0.1) (2019-06-10)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const cldFactory = await loadModule();
`loadModule` loads wasm binary, initialize it, and returns factory function to create instance of cld3 [language identifier.](https://github.com/kwonoj/cld3-asm/blob/1a86bb67abcebc2cd0e90a83149292eb044e4122/src/cldAsmModule.ts#L70-L97)

```js
loadModule({timeout?: number, environment?: ENVIRONMENT}): Promise<CldFactory>
loadModule({ timeout?: number }): Promise<CldFactory>
```

It allows to specify timeout to wait until wasm binary compliation & load. By default `cld3-asm` tries to detect running environment, but for some cases (i.e electron) it is possible to override.
It allows to specify timeout to wait until wasm binary compliation & load.

## Creating language identifier

Expand Down
23 changes: 15 additions & 8 deletions bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ const validateBinaries = async (binaryFiles: Array<{ url: string; localBinaryPat
* Actually download binary from remote. This is direct invocation to wget, need local wget installation.
*
*/
const downloadSingleBinary = async (libPath: string, binaryFile: { url: string; localBinaryPath: string }) => {
const { url } = binaryFile;
await asyncExec(`wget -q --directory-prefix=${libPath} ${url}`);
const downloadSingleBinary = async (libPath: string, binaryFile: { url: string; binaryType: string; localBinaryPath: string }) => {
const { url, binaryType, localBinaryPath } = binaryFile;
const outPath = path.join(libPath, binaryType);
mkdir(outPath);
await asyncExec(`wget -O ${localBinaryPath} ${url}`);

if (!validateBinaries([binaryFile])) {
throw new Error(`Downloaded binary checksum mismatch, cannot complete bootstrap`);
Expand All @@ -74,11 +76,16 @@ const downloadSingleBinary = async (libPath: string, binaryFile: { url: string;
*/
(async () => {
const libPath = path.resolve('./src/lib');
const binaryFiles = ['cld3.js'].map(fileName => ({
url: `https://github.com/kwonoj/docker-cld3-wasm/releases/download/${version}/${fileName}`,
localBinaryPath: path.join(libPath, fileName),
type: path.extname(fileName) === '.js' ? 'hex' : ('binary' as crypto.HexBase64Latin1Encoding)
}));
const binaryFiles = ['node', 'browser'].map(binaryType => {
const fileName = `cld3_${binaryType}.js`;

return {
url: `https://github.com/kwonoj/docker-cld3-wasm/releases/download/${version}/${fileName}`,
localBinaryPath: path.join(libPath, binaryType, 'cld3.js'),
binaryType,
type: path.extname(fileName) === '.js' ? 'hex' : ('binary' as crypto.HexBase64Latin1Encoding)
};
});

const isBinaryValid = await validateBinaries(binaryFiles);

Expand Down
43 changes: 21 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"name": "cld3-asm",
"version": "2.0.1",
"version": "3.0.0",
"description": "WebAssembly based Javascript bindings for google compact language detector 3",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"browser": {
"./dist/cjs/lib/node/cld3.js": "./dist/cjs/lib/browser/cld3.js",
"./dist/esm/lib/node/cld3.js": "./dist/esm/lib/browser/cld3.js"
},
"engines": {
"npm": ">=3.0.0",
"node": ">=8.0.0"
"node": ">=10"
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
},
"cld3-version": "c536440-190202"
"cld3-version": "ade8819-190613"
},
"lint-staged": {
"*.{ts,js}": [
Expand Down Expand Up @@ -56,16 +59,16 @@
"@commitlint/cli": "^8.0.0",
"@commitlint/config-angular": "^8.0.0",
"@types/chai": "^4.1.7",
"@types/jest": "^24.0.13",
"@types/jest": "^24.0.14",
"@types/lodash": "^4.14.134",
"@types/node": "^12.0.7",
"@types/node": "^12.0.8",
"@types/shelljs": "^0.8.5",
"@types/utf8": "^2.1.6",
"chai": "^4.2.0",
"commitizen": "^3.1.1",
"conventional-changelog-cli": "^2.0.21",
"cz-conventional-changelog": "2.1.0",
"husky": "^2.4.0",
"husky": "^2.4.1",
"jest": "^24.8.0",
"jest-spin-reporter": "^1.0.2",
"lint-staged": "^8.2.0",
Expand All @@ -82,7 +85,7 @@
"utf8": "^3.0.0"
},
"dependencies": {
"emscripten-wasm-loader": "^2.2.3"
"emscripten-wasm-loader": "^3.0.2"
},
"husky": {
"hooks": {
Expand Down
2 changes: 1 addition & 1 deletion spec/cld-asm/cldLoader-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const getAsmModule = (): CldAsmModule =>
(({
cwrap: jest.fn(),
getValue: jest.fn(),
Pointer_stringify: jest.fn(() => LanguageCode.UNKNOWN),
UTF8ToString: jest.fn(() => LanguageCode.UNKNOWN),
allocateUTF8: jest.fn(),
setValue: jest.fn(),
initializeRuntime: jest.fn(),
Expand Down
4 changes: 2 additions & 2 deletions spec/cld-asm/loadModule-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { expect } from 'chai';
import { getModuleLoader as getModuleLoaderMock, isNode } from 'emscripten-wasm-loader';
import { loadModule } from '../../src/loadModule';

const cld3Mock = require('../../src/lib/cld3'); //tslint:disable-line:no-require-imports no-var-requires
const cld3Mock = require('../../src/lib/node/cld3'); //tslint:disable-line:no-require-imports no-var-requires

jest.mock('../../src/lib/cld3', () => jest.fn());
jest.mock('../../src/lib/node/cld3', () => jest.fn());
jest.mock('../../src/cldLoader');
jest.mock('emscripten-wasm-loader', () => ({
isWasmEnabled: jest.fn(),
Expand Down
1 change: 1 addition & 0 deletions src/ambient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '*/lib/node/cld3';
9 changes: 4 additions & 5 deletions src/cldLoader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ENVIRONMENT } from 'emscripten-wasm-loader';
import { CldAsmModule, LanguageResult } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { LanguageCode } from './languageCode';
Expand All @@ -17,8 +16,8 @@ const PTR_SIZE = 4;
*
* @returns {CldFactory} Factory function manages lifecycle of cld3 language identifier.
*/
export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT): CldFactory => {
const { cwrap, _free, allocateUTF8, _malloc, getValue, Pointer_stringify, setValue } = asmModule;
export const cldLoader = (asmModule: CldAsmModule): CldFactory => {
const { cwrap, _free, allocateUTF8, _malloc, getValue, UTF8ToString, setValue } = asmModule;
const cldInterface = wrapCldInterface(cwrap);

/**
Expand All @@ -35,7 +34,7 @@ export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT):
};

// grab constant values from cld3 library
const unknownIdentifier = Pointer_stringify(cldInterface.getUnknownIdentifier());
const unknownIdentifier = UTF8ToString(cldInterface.getUnknownIdentifier());
const minBytesDefault = cldInterface.getMinNumBytesDefault();
const maxBytesDefault = cldInterface.getMaxNumBytesDefault();
const maxBytesInput = cldInterface.getMaxNumBytesInput();
Expand Down Expand Up @@ -66,7 +65,7 @@ export const cldLoader = (asmModule: CldAsmModule, _environment?: ENVIRONMENT):

// be careful to match order of properties to match pointer to struct field.
const ret: LanguageResult = {
language: Pointer_stringify(languageStringPtr) as LanguageCode,
language: UTF8ToString(languageStringPtr) as LanguageCode,
probability: getValue(structPtr + PTR_SIZE * 1, 'float'),
is_reliable: !!getValue(structPtr + PTR_SIZE * 2, 'i8'),
proportion: getValue(structPtr + PTR_SIZE * 3, 'float')
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export { log, enableLogger } from './util/logger';
export { LanguageIdentifier, CldFactory } from './cldFactory';
export { LanguageResult } from './cldAsmModule';
export { LanguageCode } from './languageCode';
export { ENVIRONMENT } from 'emscripten-wasm-loader';
23 changes: 7 additions & 16 deletions src/loadModule.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
import { ENVIRONMENT, getModuleLoader, isNode } from 'emscripten-wasm-loader';
import { getModuleLoader } from 'emscripten-wasm-loader';
import { CldAsmModule } from './cldAsmModule';
import { CldFactory } from './cldFactory';
import { cldLoader } from './cldLoader';
import { log } from './util/logger';

//imports MODULARIZED emscripten preamble
import * as runtime from './lib/node/cld3';

/**
* Load, initialize wasm binary to use actual cld wasm instances.
*
* @param [InitOptions] Options to initialize cld3 wasm binary.
* @param {number} [InitOptions.timeout] - timeout to wait wasm binary compilation & load.
* @param {string | object} [InitOptions.locateBinary] - custom resolution logic for wasm binary. (not supported)
* @param {ENVIRONMENT} [InitOptions.environment] For overriding running environment
* It could be either remote endpoint url, or loader-returned object for bundler. Check examples/browser_* for references.
*
* @returns {() => Promise<CldFactory>} Function to load module
*/
const loadModule = async (
initOptions: Partial<{
timeout: number;
environment?: ENVIRONMENT;
}> = {}
) => {
//imports MODULARIZED emscripten preamble
//tslint:disable-next-line:no-require-imports no-var-requires
const runtime = require(`./lib/cld3`);

const { environment, timeout } = initOptions;
const env = environment ? environment : isNode() ? ENVIRONMENT.NODE : ENVIRONMENT.WEB;
const { timeout } = initOptions;

log(`loadModule: loading cld3 wasm binary`, { initOptions });

//https://github.com/kwonoj/docker-hunspell-wasm/issues/63
//Build module object to construct wasm binary module via emscripten preamble.
//apply overridden environment values to custom patched hunspell preamble.
const overriddenModule = { ENVIRONMENT: env };

const moduleLoader = await getModuleLoader<CldFactory, CldAsmModule>(
(runtime: CldAsmModule) => cldLoader(runtime, env),
(runtime: CldAsmModule) => cldLoader(runtime),
runtime,
overriddenModule,
undefined,
{ timeout }
);

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
"target": "es5",
"target": "es2018",
"outDir": "dist",
"lib": [
"es2016"
Expand Down

0 comments on commit 4c355d4

Please sign in to comment.