@kmamal/sdl 0.4.5 → 0.5.0

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 CHANGED
@@ -10,7 +10,7 @@ Provides window management, input events, audio playback/recording, and clipboar
10
10
  You can use it together with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to get native WebGL drawing without having to use a browser.
11
11
 
12
12
  It should work on Linux, Mac, and Windows.
13
- Prebuilt binaries are available for x64 architectures.
13
+ Prebuilt binaries are available for x64 architectures, as well as arm-based Macs.
14
14
 
15
15
 
16
16
  ## Installation
package/binding.gyp CHANGED
@@ -1,102 +1,45 @@
1
1
  {
2
- 'targets': [
3
- {
4
- 'target_name': 'sdl',
5
- 'sources': [
6
- 'src/native/binding.cpp',
7
- 'src/native/sdl-helpers.cpp',
8
- ],
9
- 'defines': [
10
- 'NAPI_VERSION=<(napi_build_version)'
11
- ],
12
- 'cflags_cc': [ '-std=c++17' ],
13
- 'conditions': [
14
- [ 'OS=="linux"', {
15
- 'cflags': [ '<!(sdl2-config --cflags)' ],
16
- 'libraries': [ '<!(sdl2-config --libs)', ],
17
- 'link_settings': {
18
- 'libraries': [
19
- '-Wl,-rpath,\'$$ORIGIN\'',
20
- ],
21
- },
22
- 'copies': [
23
- {
24
- 'destination': '<(module_root_dir)/build/Release',
25
- 'files': [
26
- '<(module_root_dir)/deps/linux/libSDL2-2.0.so.0',
27
- ],
28
- },
29
- ],
30
- } ],
31
- [ 'OS=="mac"', {
32
- 'sources': [
33
- 'src/native/cocoa-window.mm',
34
- ],
35
- 'libraries': [ '<!(sdl2-config --libs)', ],
36
- 'xcode_settings': {
37
- 'OTHER_CFLAGS': [ '-std=c++17', '<!(sdl2-config --cflags)' ],
38
- },
39
- 'link_settings': {
40
- 'libraries': [
41
- '-Wl,-rpath,@loader_path',
42
- ],
43
- },
44
- 'copies': [
45
- {
46
- 'destination': '<(module_root_dir)/build/Release',
47
- 'files': [
48
- '<(module_root_dir)/deps/mac/libSDL2-2.0.0.dylib',
49
- ],
50
- },
51
- ],
52
- } ],
53
- [ 'OS=="win"', {
54
- 'sources': [
55
- 'src/native/asprintf.c',
56
- ],
57
- 'cflags': [ '-D_REENTRANT' ],
58
- 'include_dirs': [ 'SDL2\\include' ],
59
- 'libraries': [ '-l..\\SDL2\\lib\\x64\\SDL2.lib' ],
60
- 'msbuild_settings': {
61
- 'ClCompile': {
62
- 'LanguageStandard': 'stdcpp17',
63
- },
2
+ 'targets': [{
3
+ 'target_name': 'sdl',
4
+ 'sources': [
5
+ 'src/native/binding.cpp',
6
+ 'src/native/sdl-helpers.cpp',
7
+ ],
8
+ 'defines': [ 'NAPI_VERSION=<(napi_build_version)' ],
9
+ 'conditions': [
10
+ ['OS == "linux"', {
11
+ 'cflags': [ '-D_REENTRANT' ],
12
+ 'cflags_cc': [ '-std=c++17' ],
13
+ 'include_dirs': [ '$(SDL_INC)' ],
14
+ 'libraries': [ '-L$(SDL_LIB)', '-lSDL2' ],
15
+ 'link_settings': {
16
+ 'libraries': [ "-Wl,-rpath,'$$ORIGIN'" ],
17
+ },
18
+ }],
19
+ ['OS == "mac"', {
20
+ 'sources': [ 'src/native/cocoa-window.mm' ],
21
+ 'cflags': [ '-D_THREAD_SAFE' ],
22
+ 'xcode_settings': { 'OTHER_CFLAGS': [ '-std=c++17' ] },
23
+ 'include_dirs': [
24
+ '$(SDL_INC)',
25
+ '/opt/X11/include',
26
+ ],
27
+ 'libraries': [ '-L$(SDL_LIB)', '-lSDL2' ],
28
+ 'link_settings': {
29
+ 'libraries': [ '-Wl,-rpath,@loader_path' ],
30
+ },
31
+ }],
32
+ ['OS == "win"', {
33
+ 'sources': [ 'src/native/asprintf.c' ],
34
+ 'cflags': [ '-D_REENTRANT' ],
35
+ 'msvs_settings': {
36
+ 'VCCLCompilerTool': {
37
+ 'AdditionalOptions': [ '-std:c++17' ],
64
38
  },
65
- 'copies': [
66
- {
67
- 'destination': '<(module_root_dir)/build/Release',
68
- 'files': [
69
- '<(module_root_dir)/deps/windows/SDL2.dll',
70
- ],
71
- },
72
- ],
73
- } ],
74
- ],
75
- },
76
- ],
77
- 'conditions': [
78
- [ 'OS=="mac"', {
79
- 'targets': [
80
- {
81
- 'target_name': "action_after_build",
82
- 'type': 'none',
83
- 'dependencies': [ 'sdl' ],
84
- 'actions': [
85
- {
86
- 'action_name': "fixing_dylib",
87
- 'inputs': [ '<(module_root_dir)/build/Release/sdl.node' ],
88
- 'outputs': [ 'foo' ],
89
- 'action': [
90
- 'install_name_tool',
91
- '-change',
92
- '/usr/local/opt/sdl2/lib/libSDL2-2.0.0.dylib',
93
- '@rpath/libSDL2-2.0.0.dylib',
94
- '<(module_root_dir)/build/Release/sdl.node'
95
- ],
96
- },
97
- ],
98
39
  },
99
- ],
100
- }],
101
- ],
40
+ 'include_dirs': [ '<!(echo %SDL_INC%)' ],
41
+ 'libraries': [ '-l<!(echo %SDL_LIB%)\\SDL2.lib' ],
42
+ }],
43
+ ],
44
+ }],
102
45
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
- "version": "0.4.5",
2
+ "version": "0.5.0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL2 bindings for Node.js",
5
5
  "keywords": [
6
6
  "sdl",
7
7
  "sdl2",
8
8
  "gui",
9
- "windows",
9
+ "window",
10
10
  "screen",
11
11
  "video",
12
12
  "audio",
@@ -26,25 +26,17 @@
26
26
  "types": "src/types/index.d.ts",
27
27
  "gypfile": true,
28
28
  "scripts": {
29
- "test": "tests/run-all.sh",
30
- "rebuild": "node-gyp rebuild --verbose",
31
- "prebuild": "prebuild --runtime napi --include-regex \"\\.(node|dll|dylib|so\\.0)$\" --verbose",
32
- "install": "run-script-os",
33
- "install:darwin": "prebuild-install --runtime napi --verbose || (node scripts/fetch-deps.js && node-gyp rebuild --verbose)",
34
- "install:linux": "prebuild-install --runtime napi --verbose || (node scripts/fetch-deps.js && node-gyp rebuild --verbose)",
35
- "install:win32": "prebuild-install --runtime napi --verbose || (node scripts\\fetch-deps.js && node-gyp rebuild --verbose)"
36
- },
37
- "binary": {
38
- "napi_versions": [
39
- 3
40
- ]
29
+ "test": "cd scripts && node test.mjs",
30
+ "install": "cd scripts && node install.mjs",
31
+ "download-release": "cd scripts && node download-release.mjs",
32
+ "build": "cd scripts && node build.mjs",
33
+ "upload-release": "cd scripts && node upload-release.mjs",
34
+ "release": "cd scripts && node release.mjs",
35
+ "clean": "cd scripts && node clean.mjs",
36
+ "install-deps-mac": "./scripts/install-deps-mac.sh"
41
37
  },
42
38
  "dependencies": {
43
- "bindings": "^1.5.0",
44
- "prebuild-install": "^7.1.1",
45
- "run-script-os": "^1.1.6"
46
- },
47
- "devDependencies": {
48
- "prebuild": "^11.0.3"
39
+ "@kmamal/build-sdl": "~2.24.1-5",
40
+ "tar": "^6.1.11"
49
41
  }
50
42
  }
@@ -0,0 +1,48 @@
1
+ import Fs from 'node:fs'
2
+ import Path from 'node:path'
3
+ import { execSync } from 'node:child_process'
4
+ import C from './util/common.js'
5
+
6
+ await Promise.all([
7
+ C.dir.build,
8
+ C.dir.dist,
9
+ C.dir.publish,
10
+ ].map(async (dir) => {
11
+ await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
12
+ }))
13
+
14
+ console.log("load @kmamal/build-sdl")
15
+ process.chdir(C.dir.root)
16
+ const { default: sdlPath } = await import('@kmamal/build-sdl')
17
+
18
+ console.log("build in", C.dir.build)
19
+ process.env.SDL_INC = Path.join(sdlPath, 'include')
20
+ process.env.SDL_LIB = Path.join(sdlPath, 'lib')
21
+
22
+ let archFlag = ''
23
+ if (process.env.CROSS_COMPILE_ARCH) {
24
+ archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
25
+ }
26
+
27
+ execSync(`npx node-gyp rebuild ${archFlag} -j max --verbose`, { stdio: 'inherit' })
28
+
29
+ console.log("install to", C.dir.dist)
30
+ await Fs.promises.rm(C.dir.dist, { recursive: true }).catch(() => {})
31
+ await Fs.promises.mkdir(C.dir.dist, { recursive: true })
32
+ await Promise.all([
33
+ Fs.promises.copyFile(
34
+ Path.join(C.dir.build, 'Release/sdl.node'),
35
+ Path.join(C.dir.dist, 'sdl.node'),
36
+ ),
37
+ (async () => {
38
+ const libs = await Fs.promises.readdir(process.env.SDL_LIB)
39
+ await Promise.all(libs.map(async (name) => {
40
+ if (C.platform === 'win32' && name !== 'SDL2.dll') { return }
41
+ await Fs.promises.cp(
42
+ Path.join(process.env.SDL_LIB, name),
43
+ Path.join(C.dir.dist, name),
44
+ { verbatimSymlinks: true },
45
+ )
46
+ }))
47
+ })(),
48
+ ])
@@ -0,0 +1,16 @@
1
+ import Fs from 'node:fs'
2
+ import Path from 'node:path'
3
+ import C from './util/common.js'
4
+
5
+ const dirs = [
6
+ Path.join(C.dir.root, 'node_modules'),
7
+ C.dir.build,
8
+ C.dir.dist,
9
+ C.dir.publish,
10
+ ]
11
+
12
+ console.log("delete")
13
+ await Promise.all(dirs.map(async (dir) => {
14
+ console.log(" ", dir)
15
+ await Fs.promises.rm(dir, { recursive: true }).catch(() => {})
16
+ }))
@@ -0,0 +1,17 @@
1
+ import Fs from 'node:fs'
2
+ import { once } from 'node:events'
3
+ import C from './util/common.js'
4
+ import { fetch } from './util/fetch.js'
5
+ import Tar from 'tar'
6
+
7
+ const url = `https://github.com/${C.owner}/${C.repo}/releases/download/v${C.version}/${C.assetName}`
8
+
9
+ console.log("fetch", url)
10
+ const response = await fetch(url)
11
+
12
+ console.log("unpack to", C.dir.dist)
13
+ await Fs.promises.rm(C.dir.dist, { recursive: true }).catch(() => {})
14
+ await Fs.promises.mkdir(C.dir.dist, { recursive: true })
15
+ const tar = Tar.extract({ gzip: true, C: C.dir.dist })
16
+ response.stream().pipe(tar)
17
+ await once(tar, 'finish')
@@ -0,0 +1,2 @@
1
+
2
+ brew install xquartz
@@ -0,0 +1,11 @@
1
+
2
+ if (!process.env.NODE_SDL_FROM_SOURCE) {
3
+ try {
4
+ await import('./download-release.mjs')
5
+ process.exit()
6
+ } catch (error) {}
7
+ } else {
8
+ console.log("skip download and build from source")
9
+ }
10
+
11
+ await import('./build.mjs')
@@ -0,0 +1,10 @@
1
+ import { execSync } from 'node:child_process'
2
+
3
+ await import('./clean.mjs')
4
+
5
+ process.env.NODE_SDL_FROM_SOURCE = 1
6
+ execSync('npm install', {
7
+ stdio: 'inherit',
8
+ })
9
+
10
+ await import('./upload-release.mjs')
@@ -0,0 +1,10 @@
1
+ import Path from 'node:path'
2
+ import { execSync } from 'node:child_process'
3
+ import { fstat } from 'node:fs'
4
+
5
+ process.chdir(Path.resolve(__dirname, '../tests'))
6
+ const files = await fstat.readdir('.')
7
+ for (const name of files) {
8
+ if (!name.endsWith('.test.js')) { continue }
9
+ execSync(`node ${name}`, { stdio: 'inherit' })
10
+ }
@@ -0,0 +1,86 @@
1
+ import Fs from 'node:fs'
2
+ import Path from 'node:path'
3
+ import C from './util/common.js'
4
+ import { fetch } from './util/fetch.js'
5
+ import Tar from 'tar'
6
+
7
+ const commonHeaders = {
8
+ "Accept": 'application/vnd.github+json',
9
+ "Authorization": `Bearer ${process.env.GITHUB_TOKEN}`,
10
+ 'User-Agent': `@kmamal/sdl@${C.version}`,
11
+ }
12
+
13
+ let response
14
+
15
+ getRelease: {
16
+ console.log("get release", C.version)
17
+
18
+ try {
19
+ response = await fetch(
20
+ `https://api.github.com/repos/${C.owner}/${C.repo}/releases/tags/v${C.version}`,
21
+ { headers: commonHeaders },
22
+ )
23
+ console.log("release exists", C.version)
24
+ break getRelease
25
+ } catch (error) {
26
+ console.log(error.message)
27
+ }
28
+
29
+ console.log("create release", C.version)
30
+
31
+ response = await fetch(
32
+ `https://api.github.com/repos/${C.owner}/${C.repo}/releases`,
33
+ {
34
+ method: 'POST',
35
+ headers: commonHeaders,
36
+ body: JSON.stringify({
37
+ tag_name: `v${C.version}`, // eslint-disable-line camelcase
38
+ name: `v${C.version}`,
39
+ }),
40
+ },
41
+ )
42
+ }
43
+ const releaseId = (await response.json()).id
44
+
45
+ console.log("create archive", C.assetName)
46
+ await Fs.promises.rm(C.dir.publish, { recursive: true }).catch(() => {})
47
+ await Fs.promises.mkdir(C.dir.publish, { recursive: true })
48
+ const assetPath = Path.join(C.dir.publish, C.assetName)
49
+
50
+ process.chdir(C.dir.dist)
51
+ await Tar.create(
52
+ { gzip: true, file: assetPath },
53
+ await Fs.promises.readdir('.'),
54
+ )
55
+ const buffer = await Fs.promises.readFile(assetPath)
56
+
57
+ response = await fetch(
58
+ `https://api.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets`,
59
+ { headers: commonHeaders },
60
+ )
61
+
62
+ const list = await response.json()
63
+ const asset = list.find((x) => x.name === C.assetName)
64
+ if (asset) {
65
+ console.log("delete asset", C.assetName)
66
+ await fetch(
67
+ `https://api.github.com/repos/${C.owner}/${C.repo}/releases/assets/${asset.id}`,
68
+ {
69
+ method: 'DELETE',
70
+ headers: commonHeaders,
71
+ },
72
+ )
73
+ }
74
+
75
+ console.log("upload", C.assetName)
76
+ await fetch(
77
+ `https://uploads.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets?name=${C.assetName}`,
78
+ {
79
+ method: 'POST',
80
+ headers: {
81
+ ...commonHeaders,
82
+ 'Content-Type': 'application/gzip',
83
+ },
84
+ body: buffer,
85
+ },
86
+ )
@@ -0,0 +1,28 @@
1
+ const Fs = require('node:fs')
2
+ const Path = require('node:path')
3
+
4
+ const dir = {}
5
+ dir.root = Path.resolve(__dirname, '../..')
6
+ dir.build = Path.join(dir.root, 'build')
7
+ dir.dist = Path.join(dir.root, 'dist')
8
+ dir.publish = Path.join(dir.root, 'publish')
9
+
10
+ const pkgPath = Path.join(dir.root, 'package.json')
11
+ const pkg = JSON.parse(Fs.readFileSync(pkgPath).toString())
12
+ const version = pkg.version
13
+ const [ , owner, repo ] = pkg.repository.url.match(/([^/:]+)\/([^/]+).git$/u)
14
+
15
+ const { platform, arch } = process
16
+ const targetArch = process.env.CROSS_COMPILE_ARCH || arch
17
+ const assetName = `sdl.node-v${version}-${platform}-${targetArch}.tar.gz`
18
+
19
+ module.exports = {
20
+ dir,
21
+ version,
22
+ owner,
23
+ repo,
24
+ platform,
25
+ arch,
26
+ targetArch,
27
+ assetName,
28
+ }
@@ -0,0 +1,82 @@
1
+
2
+ const _consume = async (stream) => {
3
+ const chunks = []
4
+ for await (const chunk of stream) {
5
+ chunks.push(chunk)
6
+ }
7
+ return Buffer.concat(chunks)
8
+ }
9
+
10
+ const fetch = async (_url, options = {}) => {
11
+ let url = _url
12
+ let {
13
+ maxRedirect = 5,
14
+ body = null,
15
+ } = options
16
+
17
+ const { protocol } = new URL(url)
18
+ const lib = require(protocol.slice(0, -1))
19
+
20
+ const evalPromise = (resolve, reject) => {
21
+ const request = lib.request(url, options)
22
+ .on('error', reject)
23
+ .on('response', resolve)
24
+
25
+ if (!body) {
26
+ request.end()
27
+ return
28
+ }
29
+
30
+ if (body?.length) {
31
+ if (!Buffer.isBuffer(body)) { body = Buffer.from(body) }
32
+ body = Promise.resolve(body)
33
+ } else {
34
+ body = _consume(body)
35
+ }
36
+
37
+ body.then((buffer) => {
38
+ request.setHeader('Content-Length', buffer.length)
39
+ request.end(buffer)
40
+ })
41
+ }
42
+
43
+ for (;;) {
44
+ const response = await new Promise(evalPromise)
45
+
46
+ const { statusCode } = response
47
+
48
+ if (300 <= statusCode && statusCode < 400) {
49
+ if (maxRedirect-- > 0) {
50
+ url = response.headers.location
51
+ continue
52
+ }
53
+ }
54
+
55
+ if (!(200 <= statusCode && statusCode < 300)) {
56
+ let responseBody
57
+ try { responseBody = (await _consume(response)).toString() } catch (_) {}
58
+ try { responseBody = JSON.parse(responseBody) } catch (_) {}
59
+ throw Object.assign(new Error(`bad status code ${statusCode}`), {
60
+ statusCode,
61
+ responseBody,
62
+ })
63
+ }
64
+
65
+ return {
66
+ stream () { return response },
67
+ async buffer () {
68
+ return await _consume(response)
69
+ },
70
+ async text () {
71
+ const buffer = await this.buffer()
72
+ return buffer.toString()
73
+ },
74
+ async json () {
75
+ const text = await this.text()
76
+ return JSON.parse(text)
77
+ },
78
+ }
79
+ }
80
+ }
81
+
82
+ module.exports = { fetch }
@@ -1,3 +1,5 @@
1
- const loadBindings = require('bindings')
1
+ const Path = require('path')
2
2
 
3
- module.exports = loadBindings('sdl.node')
3
+ const path = Path.resolve(__dirname, '../../dist/sdl.node')
4
+
5
+ module.exports = require(path)
@@ -1,37 +0,0 @@
1
- const Https = require('https')
2
- const Fs = require('fs')
3
- const Path = require('path')
4
- const Stream = require('stream')
5
-
6
- const { folder, name } = {
7
- darwin: { folder: 'mac', name: 'libSDL2-2.0.0.dylib' },
8
- linux: { folder: 'linux', name: 'libSDL2-2.0.so.0' },
9
- win32: { folder: 'windown', name: 'SDL2.dll' },
10
- }[process.platform]
11
-
12
- const github = 'https://raw.githubusercontent.com/kmamal/node-sdl/master/deps'
13
-
14
- ;(async () => {
15
- const dir = Path.resolve(__dirname, `../deps/${folder}`)
16
- const file = Path.join(dir, name)
17
-
18
- try {
19
- await Fs.promises.access(file, Fs.constants.F_OK)
20
- console.log("deps are ok")
21
- return
22
- } catch (err) {
23
- console.log("missing", file)
24
- }
25
-
26
- console.log("mkdir", dir)
27
- console.log("fetch", `${github}/${folder}/${name}`)
28
- console.log("write", file)
29
-
30
- await Fs.promises.mkdir(dir, { recursive: true })
31
- await Stream.promises.pipeline(
32
- await new Promise((resolve) => {
33
- Https.get(`${github}/${folder}/${name}`, resolve)
34
- }),
35
- Fs.createWriteStream(file),
36
- )
37
- })()