geoplegma-js 0.0.0 → 0.0.2-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -4
- package/dist/native/index.d.ts +2 -2
- package/dist/native/napi.node +0 -0
- package/package.json +5 -4
- package/dist/native/browser.js +0 -1
- package/dist/native/napi.wasi-browser.js +0 -61
- package/dist/native/napi.wasi.cjs +0 -113
- package/dist/native/wasi-worker-browser.mjs +0 -32
- package/dist/native/wasi-worker.mjs +0 -63
package/README.md
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Introduction
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JavaScript package for GeoPlegma. At this point, it runs only DGGRID.
|
|
4
|
+
|
|
5
|
+
## Get Started
|
|
6
|
+
|
|
7
|
+
Install the library.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install geoplegma-js
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
or
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add geoplegma-js
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
or
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pnpm install geoplegma-js
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Example:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { Dggrs } from "geoplegma-js";
|
|
29
|
+
const g = new Dggrs("IVEA7H");
|
|
30
|
+
const rl = 3;
|
|
31
|
+
const bbox = [
|
|
32
|
+
[-10.0, -10.0],
|
|
33
|
+
[10.0, 10.0],
|
|
34
|
+
];
|
|
35
|
+
const a = g.zonesFromBbox(rl, bbox);
|
|
36
|
+
|
|
37
|
+
console.log("from bbox: " + a.map((v) => v.id));
|
|
38
|
+
// from bbox: 307fffffffffffff,30bfffffffffffff,31bfffffffffffff,323fffffffffffff,327fffffffffffff,32bfffffffffffff,3a7fffffffffffff
|
|
39
|
+
```
|
|
4
40
|
|
|
5
41
|
## Development
|
|
6
42
|
|
|
@@ -10,6 +46,18 @@ A starter for creating a TypeScript package.
|
|
|
10
46
|
npm install
|
|
11
47
|
```
|
|
12
48
|
|
|
49
|
+
- Get the native binding files, and run the script:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
./scripts/dev.sh
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This will:
|
|
56
|
+
|
|
57
|
+
- Build the .node files from GeoPlegma/gp-bindings/js.
|
|
58
|
+
- Copy them into GeoPlegma-js/native/.
|
|
59
|
+
- Let your JS code require('./native/something.node') without committing them.
|
|
60
|
+
|
|
13
61
|
- Run the unit tests:
|
|
14
62
|
|
|
15
63
|
```bash
|
|
@@ -21,7 +69,7 @@ npm run test
|
|
|
21
69
|
```bash
|
|
22
70
|
npm run build
|
|
23
71
|
```
|
|
24
|
-
The `build` script will build a new folder `dist` with the bundled files inside, and copy the index.node and index.d.ts files into that same folder. This folder will be used in the npm library as the source code files.
|
|
25
72
|
|
|
73
|
+
The `build` script will build a new folder `dist` with the bundled files inside, and copy the index.node and index.d.ts files into that same folder. This folder will be used in the npm library as the source code files.
|
|
26
74
|
|
|
27
|
-
NOTE: Eventually a github job will be added for this statement workflow.
|
|
75
|
+
NOTE: Eventually a github job will be added for this statement workflow.
|
package/dist/native/index.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ export declare class Dggrs {
|
|
|
4
4
|
constructor(dggrs: string)
|
|
5
5
|
zonesFromBbox(refinementLevel: number, bbox?: Array<Array<number>> | undefined | null, config?: Config | undefined | null): FlatZones
|
|
6
6
|
zoneFromPoint(refinementLevel: number, point?: Array<number> | undefined | null, config?: Config | undefined | null): FlatZones
|
|
7
|
-
zonesFromParent(relativeDepth: number, parentZoneId: string, config?: Config | undefined | null): FlatZones
|
|
8
|
-
zoneFromId(zoneId: string, config?: Config | undefined | null): FlatZones
|
|
7
|
+
zonesFromParent(relativeDepth: number, parentZoneId: string | number, config?: Config | undefined | null): FlatZones
|
|
8
|
+
zoneFromId(zoneId: string | number, config?: Config | undefined | null): FlatZones
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export interface Config {
|
package/dist/native/napi.node
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "geoplegma-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2-beta",
|
|
4
4
|
"description": "A JavaScript library for GeoPlegma.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"homepage": "https://github.com/
|
|
7
|
+
"homepage": "https://github.com/GeoPlegma/GeoPlegma-js#readme",
|
|
8
8
|
"bugs": {
|
|
9
|
-
"url": "https://github.com/
|
|
9
|
+
"url": "https://github.com/GeoPlegma/GeoPlegma-js/issues"
|
|
10
10
|
},
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/
|
|
13
|
+
"url": "git+https://github.com/GeoPlegma/GeoPlegma-js.git"
|
|
14
14
|
},
|
|
15
15
|
"author": "João Manuel <joaocsmanuel@gmail.com>",
|
|
16
16
|
"files": [
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"scripts": {
|
|
30
30
|
"build": "tsdown",
|
|
31
31
|
"dev": "tsdown --watch",
|
|
32
|
+
"build-native": "scripts/dev.sh",
|
|
32
33
|
"test": "vitest",
|
|
33
34
|
"typecheck": "tsc --noEmit",
|
|
34
35
|
"release": "bumpp && npm publish"
|
package/dist/native/browser.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from 'js-wasm32-wasi'
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createOnMessage as __wasmCreateOnMessageForFsProxy,
|
|
3
|
-
getDefaultContext as __emnapiGetDefaultContext,
|
|
4
|
-
instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
|
|
5
|
-
WASI as __WASI,
|
|
6
|
-
} from '@napi-rs/wasm-runtime'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const __wasi = new __WASI({
|
|
11
|
-
version: 'preview1',
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
const __wasmUrl = new URL('./napi.wasm32-wasi.wasm', import.meta.url).href
|
|
15
|
-
const __emnapiContext = __emnapiGetDefaultContext()
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
const __sharedMemory = new WebAssembly.Memory({
|
|
19
|
-
initial: 4000,
|
|
20
|
-
maximum: 65536,
|
|
21
|
-
shared: true,
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
instance: __napiInstance,
|
|
28
|
-
module: __wasiModule,
|
|
29
|
-
napiModule: __napiModule,
|
|
30
|
-
} = __emnapiInstantiateNapiModuleSync(__wasmFile, {
|
|
31
|
-
context: __emnapiContext,
|
|
32
|
-
asyncWorkPoolSize: 4,
|
|
33
|
-
wasi: __wasi,
|
|
34
|
-
onCreateWorker() {
|
|
35
|
-
const worker = new Worker(new URL('./wasi-worker-browser.mjs', import.meta.url), {
|
|
36
|
-
type: 'module',
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
return worker
|
|
40
|
-
},
|
|
41
|
-
overwriteImports(importObject) {
|
|
42
|
-
importObject.env = {
|
|
43
|
-
...importObject.env,
|
|
44
|
-
...importObject.napi,
|
|
45
|
-
...importObject.emnapi,
|
|
46
|
-
memory: __sharedMemory,
|
|
47
|
-
}
|
|
48
|
-
return importObject
|
|
49
|
-
},
|
|
50
|
-
beforeInit({ instance }) {
|
|
51
|
-
for (const name of Object.keys(instance.exports)) {
|
|
52
|
-
if (name.startsWith('__napi_register__')) {
|
|
53
|
-
instance.exports[name]()
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
})
|
|
58
|
-
export default __napiModule.exports
|
|
59
|
-
export const Dggrs = __napiModule.exports.Dggrs
|
|
60
|
-
export const defaultConfig = __napiModule.exports.defaultConfig
|
|
61
|
-
export const Id = __napiModule.exports.Id
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/* eslint-disable */
|
|
2
|
-
/* prettier-ignore */
|
|
3
|
-
|
|
4
|
-
/* auto-generated by NAPI-RS */
|
|
5
|
-
|
|
6
|
-
const __nodeFs = require('node:fs')
|
|
7
|
-
const __nodePath = require('node:path')
|
|
8
|
-
const { WASI: __nodeWASI } = require('node:wasi')
|
|
9
|
-
const { Worker } = require('node:worker_threads')
|
|
10
|
-
|
|
11
|
-
const {
|
|
12
|
-
createOnMessage: __wasmCreateOnMessageForFsProxy,
|
|
13
|
-
getDefaultContext: __emnapiGetDefaultContext,
|
|
14
|
-
instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
|
|
15
|
-
} = require('@napi-rs/wasm-runtime')
|
|
16
|
-
|
|
17
|
-
const __rootDir = __nodePath.parse(process.cwd()).root
|
|
18
|
-
|
|
19
|
-
const __wasi = new __nodeWASI({
|
|
20
|
-
version: 'preview1',
|
|
21
|
-
env: process.env,
|
|
22
|
-
preopens: {
|
|
23
|
-
[__rootDir]: __rootDir,
|
|
24
|
-
}
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
const __emnapiContext = __emnapiGetDefaultContext()
|
|
28
|
-
|
|
29
|
-
const __sharedMemory = new WebAssembly.Memory({
|
|
30
|
-
initial: 4000,
|
|
31
|
-
maximum: 65536,
|
|
32
|
-
shared: true,
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
let __wasmFilePath = __nodePath.join(__dirname, 'napi.wasm32-wasi.wasm')
|
|
36
|
-
const __wasmDebugFilePath = __nodePath.join(__dirname, 'napi.wasm32-wasi.debug.wasm')
|
|
37
|
-
|
|
38
|
-
if (__nodeFs.existsSync(__wasmDebugFilePath)) {
|
|
39
|
-
__wasmFilePath = __wasmDebugFilePath
|
|
40
|
-
} else if (!__nodeFs.existsSync(__wasmFilePath)) {
|
|
41
|
-
try {
|
|
42
|
-
__wasmFilePath = __nodePath.resolve('js-wasm32-wasi')
|
|
43
|
-
} catch {
|
|
44
|
-
throw new Error('Cannot find napi.wasm32-wasi.wasm file, and js-wasm32-wasi package is not installed.')
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
|
|
49
|
-
context: __emnapiContext,
|
|
50
|
-
asyncWorkPoolSize: (function() {
|
|
51
|
-
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
|
|
52
|
-
// NaN > 0 is false
|
|
53
|
-
if (threadsSizeFromEnv > 0) {
|
|
54
|
-
return threadsSizeFromEnv
|
|
55
|
-
} else {
|
|
56
|
-
return 4
|
|
57
|
-
}
|
|
58
|
-
})(),
|
|
59
|
-
reuseWorker: true,
|
|
60
|
-
wasi: __wasi,
|
|
61
|
-
onCreateWorker() {
|
|
62
|
-
const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
|
|
63
|
-
env: process.env,
|
|
64
|
-
})
|
|
65
|
-
worker.onmessage = ({ data }) => {
|
|
66
|
-
__wasmCreateOnMessageForFsProxy(__nodeFs)(data)
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// The main thread of Node.js waits for all the active handles before exiting.
|
|
70
|
-
// But Rust threads are never waited without `thread::join`.
|
|
71
|
-
// So here we hack the code of Node.js to prevent the workers from being referenced (active).
|
|
72
|
-
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
|
|
73
|
-
// a worker is consist of two handles: kPublicPort and kHandle.
|
|
74
|
-
{
|
|
75
|
-
const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
|
|
76
|
-
s.toString().includes("kPublicPort")
|
|
77
|
-
);
|
|
78
|
-
if (kPublicPort) {
|
|
79
|
-
worker[kPublicPort].ref = () => {};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
|
|
83
|
-
s.toString().includes("kHandle")
|
|
84
|
-
);
|
|
85
|
-
if (kHandle) {
|
|
86
|
-
worker[kHandle].ref = () => {};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
worker.unref();
|
|
90
|
-
}
|
|
91
|
-
return worker
|
|
92
|
-
},
|
|
93
|
-
overwriteImports(importObject) {
|
|
94
|
-
importObject.env = {
|
|
95
|
-
...importObject.env,
|
|
96
|
-
...importObject.napi,
|
|
97
|
-
...importObject.emnapi,
|
|
98
|
-
memory: __sharedMemory,
|
|
99
|
-
}
|
|
100
|
-
return importObject
|
|
101
|
-
},
|
|
102
|
-
beforeInit({ instance }) {
|
|
103
|
-
for (const name of Object.keys(instance.exports)) {
|
|
104
|
-
if (name.startsWith('__napi_register__')) {
|
|
105
|
-
instance.exports[name]()
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
})
|
|
110
|
-
module.exports = __napiModule.exports
|
|
111
|
-
module.exports.Dggrs = __napiModule.exports.Dggrs
|
|
112
|
-
module.exports.defaultConfig = __napiModule.exports.defaultConfig
|
|
113
|
-
module.exports.Id = __napiModule.exports.Id
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
|
|
2
|
-
|
|
3
|
-
const handler = new MessageHandler({
|
|
4
|
-
onLoad({ wasmModule, wasmMemory }) {
|
|
5
|
-
const wasi = new WASI({
|
|
6
|
-
print: function () {
|
|
7
|
-
// eslint-disable-next-line no-console
|
|
8
|
-
console.log.apply(console, arguments)
|
|
9
|
-
},
|
|
10
|
-
printErr: function() {
|
|
11
|
-
// eslint-disable-next-line no-console
|
|
12
|
-
console.error.apply(console, arguments)
|
|
13
|
-
},
|
|
14
|
-
})
|
|
15
|
-
return instantiateNapiModuleSync(wasmModule, {
|
|
16
|
-
childThread: true,
|
|
17
|
-
wasi,
|
|
18
|
-
overwriteImports(importObject) {
|
|
19
|
-
importObject.env = {
|
|
20
|
-
...importObject.env,
|
|
21
|
-
...importObject.napi,
|
|
22
|
-
...importObject.emnapi,
|
|
23
|
-
memory: wasmMemory,
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
})
|
|
27
|
-
},
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
globalThis.onmessage = function (e) {
|
|
31
|
-
handler.handle(e)
|
|
32
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import { createRequire } from "node:module";
|
|
3
|
-
import { parse } from "node:path";
|
|
4
|
-
import { WASI } from "node:wasi";
|
|
5
|
-
import { parentPort, Worker } from "node:worker_threads";
|
|
6
|
-
|
|
7
|
-
const require = createRequire(import.meta.url);
|
|
8
|
-
|
|
9
|
-
const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
|
|
10
|
-
|
|
11
|
-
if (parentPort) {
|
|
12
|
-
parentPort.on("message", (data) => {
|
|
13
|
-
globalThis.onmessage({ data });
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
Object.assign(globalThis, {
|
|
18
|
-
self: globalThis,
|
|
19
|
-
require,
|
|
20
|
-
Worker,
|
|
21
|
-
importScripts: function (f) {
|
|
22
|
-
;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
|
|
23
|
-
},
|
|
24
|
-
postMessage: function (msg) {
|
|
25
|
-
if (parentPort) {
|
|
26
|
-
parentPort.postMessage(msg);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const emnapiContext = getDefaultContext();
|
|
32
|
-
|
|
33
|
-
const __rootDir = parse(process.cwd()).root;
|
|
34
|
-
|
|
35
|
-
const handler = new MessageHandler({
|
|
36
|
-
onLoad({ wasmModule, wasmMemory }) {
|
|
37
|
-
const wasi = new WASI({
|
|
38
|
-
version: 'preview1',
|
|
39
|
-
env: process.env,
|
|
40
|
-
preopens: {
|
|
41
|
-
[__rootDir]: __rootDir,
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return instantiateNapiModuleSync(wasmModule, {
|
|
46
|
-
childThread: true,
|
|
47
|
-
wasi,
|
|
48
|
-
context: emnapiContext,
|
|
49
|
-
overwriteImports(importObject) {
|
|
50
|
-
importObject.env = {
|
|
51
|
-
...importObject.env,
|
|
52
|
-
...importObject.napi,
|
|
53
|
-
...importObject.emnapi,
|
|
54
|
-
memory: wasmMemory
|
|
55
|
-
};
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
globalThis.onmessage = function (e) {
|
|
62
|
-
handler.handle(e);
|
|
63
|
-
};
|