Compare commits

...

13 Commits

Author SHA1 Message Date
701804f626 feat(sdk,client): target es2015 for old chromecast compatibility
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-14 16:23:50 +02:00
d0b57ab15f fix(client,sdk): accept empty token from parent frame
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-13 13:41:35 +02:00
dc93c585eb fix(sdk,client): add listener to current frame window
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-13 12:07:52 +02:00
de330c0042 fix(sdk,client): use origin as postmessage target
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-13 11:35:31 +02:00
310dac296f feat(storage,sqlite): begin tx with context
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-13 11:23:34 +02:00
4db7576b12 feat(client,sdk): retrieve auth token from parent frame + better resize detection
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-13 11:02:24 +02:00
f5283b86ed fix(app,manifest): manifest serialization
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-11 15:08:07 +02:00
98ebd7a168 doc(app,manifest): add metadata attribute in manifest schema
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-11 11:11:00 +02:00
8ca31d05c0 feat(app,manifest): validation + extendable metadatas
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-11 11:05:09 +02:00
34c6a089b5 fix(client,sdk): permit cross-domain message communication
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-06 20:54:01 +02:00
da73b842e1 fix(sdk,client): initialize crossframe observers after window load event
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-06 19:18:36 +02:00
55d7241d95 chore(sdk,client): remove restrictive assertion
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-06 18:18:12 +02:00
240b07af66 feat(sdk,client): add edgeframe sdk api
All checks were successful
arcad/edge/pipeline/head This commit looks good
2023-04-06 18:16:17 +02:00
33 changed files with 878 additions and 200 deletions

View File

@ -50,12 +50,12 @@ pkg/sdk/client/dist/client.js: tools/esbuild/bin/esbuild node_modules
pkg/sdk/client/src/index.ts \ pkg/sdk/client/src/index.ts \
--bundle \ --bundle \
--sourcemap \ --sourcemap \
--target=es2020 \ --target=es2015 \
--format=iife \ --format=iife \
--global-name=Edge \ --global-name=Edge \
--define:global=window \ --define:global=window \
--platform=browser \ --platform=browser \
--footer:js="Edge=Edge.default;" \ --footer:js="EdgeFrame=Edge.crossFrameMessenger;Edge=Edge.client" \
--outfile=pkg/sdk/client/dist/client.js --outfile=pkg/sdk/client/dist/client.js
node_modules: node_modules:

View File

@ -0,0 +1,11 @@
package app
import (
"forge.cadoles.com/arcad/edge/pkg/app"
"forge.cadoles.com/arcad/edge/pkg/app/metadata"
)
var manifestMetadataValidators = []app.MetadataValidator{
metadata.WithMinimumRoleValidator("visitor", "user", "superuser", "admin", "superadmin"),
metadata.WithNamedPathsValidator(metadata.NamedPathAdmin, metadata.NamedPathIcon),
}

View File

@ -52,6 +52,10 @@ func PackageCommand() *cli.Command {
return errors.Wrap(err, "could not load app manifest") return errors.Wrap(err, "could not load app manifest")
} }
if valid, err := manifest.Validate(manifestMetadataValidators...); !valid {
return errors.Wrap(err, "invalid app manifest")
}
if err := os.MkdirAll(outputDir, 0o755); err != nil { if err := os.MkdirAll(outputDir, 0o755); err != nil {
return errors.Wrapf(err, "could not create directory ''%s'", outputDir) return errors.Wrapf(err, "could not create directory ''%s'", outputDir)
} }

View File

@ -110,6 +110,10 @@ func RunCommand() *cli.Command {
return errors.Wrap(err, "could not load manifest from app bundle") return errors.Wrap(err, "could not load manifest from app bundle")
} }
if valid, err := manifest.Validate(manifestMetadataValidators...); !valid {
return errors.Wrap(err, "invalid app manifest")
}
storageFile := injectAppID(ctx.String("storage-file"), manifest.ID) storageFile := injectAppID(ctx.String("storage-file"), manifest.ID)
if err := ensureDir(storageFile); err != nil { if err := ensureDir(storageFile); err != nil {

View File

@ -6,6 +6,7 @@ Une **Edge App** est une application capable de s'exécuter dans un environnemen
### Référence ### Référence
- [Fichier `manifest.yml`](./apps/manifest.md)
- [API Client](./apps/client-api/README.md) - [API Client](./apps/client-api/README.md)
- [API Serveur](./apps/server-api/README.md) - [API Serveur](./apps/server-api/README.md)

View File

@ -1,68 +1,14 @@
# API Client # API Client
## Méthodes ## Usage
### `Edge.connect(): Promise` Afin de pouvoir utiliser le SDK "client", vous devez inclure dans la page HTML de votre application la balise `<script>` suivante:
> `TODO` ```html
<script src="/edge/sdk/client.js"></script>
### `Edge.disconnect(): void`
> `TODO`
### `Edge.send(message: Object): void`
> `TODO`
### `Edge.rpc(method: string, params: Object): Promise`
> `TODO`
#### Exemple
**Côté serveur**
```js
function onInit() {
rpc.register(echo);
}
function echo(ctx, params) {
return params;
}
``` ```
**Côté client** Vous pourrez ensuite accéder aux variables globales suivantes:
```js - [`Edge`](./edge.md) - Client principal d'échange avec le serveur
Edge.connect().then(() => { - [`EdgeFrame`](./edge-frame.md)
Edge.rpc("echo", { hello: "world!" })
.then(result => console.log(result))
.catch(err => console.error(err));
});
```
### `Edge.upload(blob: Blob, metadata: Object): Promise`
> `TODO`
### `Edge.blobUrl(bucketName: string, blobId: string): string`
> `TODO`
### `Edge.externalUrl(url: string): string`
Retourne une URL "locale" permettant d'accéder à une ressource externe, en fonction de règles propres à l'application. Voir module [`fetch`](../server-api/fetch.md).
## Événements
### `"message"`
> `TODO`
#### Exemple
```js
Edge.addEventListener("message", evt => console.log(evt.detail));
```

View File

@ -0,0 +1,30 @@
# `EdgeFrame`
## Méthodes
### `EdgeFrame.addEventListener(name: string, listener: (event) => void)`
> `TODO`
## Événements
### `"title_changed"`
```typescript
interface TitleChangedEvent {
detail: {
title: string
}
}
```
### `"size_changed"`
```typescript
interface SizeChangedEvent {
detail: {
width: number
height: number
}
}
```

View File

@ -0,0 +1,68 @@
# `Edge`
## Méthodes
### `Edge.connect(): Promise`
> `TODO`
### `Edge.disconnect(): void`
> `TODO`
### `Edge.send(message: Object): void`
> `TODO`
### `Edge.rpc(method: string, params: Object): Promise`
> `TODO`
#### Exemple
**Côté serveur**
```js
function onInit() {
rpc.register(echo);
}
function echo(ctx, params) {
return params;
}
```
**Côté client**
```js
Edge.connect().then(() => {
Edge.rpc("echo", { hello: "world!" })
.then(result => console.log(result))
.catch(err => console.error(err));
});
```
### `Edge.upload(blob: Blob, metadata: Object): Promise`
> `TODO`
### `Edge.blobUrl(bucketName: string, blobId: string): string`
> `TODO`
### `Edge.externalUrl(url: string): string`
Retourne une URL "locale" permettant d'accéder à une ressource externe, en fonction de règles propres à l'application. Voir module [`fetch`](../server-api/fetch.md).
## Événements
### `"message"`
> `TODO`
#### Exemple
```js
Edge.addEventListener("message", evt => console.log(evt.detail));
```

36
doc/apps/manifest.md Normal file
View File

@ -0,0 +1,36 @@
# Le fichier `manifest.yml`
Le fichier `manifest.yml` à la racine du bundle de votre application contient des informations décrivant celles ci. Vous trouverez ci dessous un exemple commenté.
```yaml
# REQUIS - L'identifiant de votre application. Il doit être globalement unique.
# Un identifiant du type nom de domaine inversé est en général conseillé (ex: tld.mycompany.myapp)
id: tld.mycompany.myapp
# REQUIS - Le numéro de version de votre application
# Celui ci devrait respecter le format "semver 2" (voir https://semver.org/)
version: 0.0.0
# REQUIS - Le titre de votre application.
title: My App
# OPTIONNEL - Les mots-clés associés à votre applications.
tags: ["chat"]
# OPTIONNEL - La description de votre application.
# Vous pouvez utiliser la syntaxe Markdown pour la mettre en forme.
description: |>
A simple demo application
# OPTIONNEL - Métadonnées associées à l'application
metadata:
# OPTIONNEL - Liste des chemins permettant d'accéder à certains URLs identifiées (page d'administration, icône si existante, etc)
paths:
# Si défini, chemin vers la page d'administration de l'application
admin: /admin
# Si défini, chemin vers l'icône associée à l'application
icon: /my-app-icon.png
# OPTIONNEL - Role minimum requis pour pouvoir accéder à l'application
minimumRole: visitor
```

View File

@ -22,23 +22,7 @@ my-app
Ce fichier est le manifeste de votre application. Il permet au serveur d'identifier celle ci et de récupérer des informations la concernant. Ce fichier est le manifeste de votre application. Il permet au serveur d'identifier celle ci et de récupérer des informations la concernant.
```yaml [Voir le fichier `manifest.yml` d'exemple](./manifest.md)
---
# L'identifiant de votre application. Il doit être globalement unique.
# Un identifiant du type nom de domaine inversé est en général conseillé (ex: tld.mycompany.myapp)
id: tld.mycompany.myapp
# Le titre de votre application.
title: My App
# Les mots-clés associés à votre applications.
tags: ["chat"]
# La description de votre application.
# Vous pouvez utiliser la syntaxe Markdown pour la mettre en forme.
description: |>
A simple demo application
```
## 4. Créer la page d'accueil ## 4. Créer la page d'accueil

View File

@ -49,10 +49,11 @@ URL associée à l'application, ou `null` si aucune application n'a été trouv
```typescript ```typescript
interface Manifest { interface Manifest {
id: string // Identifiant de l'application id: string // Identifiant de l'application
version: string // Version de l'application version: string // Version de l'application
title: string // Titre associé à l'application title: string // Titre associé à l'application
description: string // Description associée à l'application description: string // Description associée à l'application
tags: string[] // Mots clés associés à l'application tags: string[] // Mots clés associés à l'application
metadata: { [key: string]: any } // Métadonnées associées à l'application. Voir ../manifest.md
} }
``` ```

View File

@ -4,4 +4,9 @@ title: SDK Test
version: 0.0.0 version: 0.0.0
description: | description: |
Suite de tests pour le SDK client Suite de tests pour le SDK client
tags: ["test"] tags: ["test"]
metadata:
paths:
icon: /icon.png
minimumRole: visitor

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

View File

@ -4,6 +4,7 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<title>Client SDK Test suite</title> <title>Client SDK Test suite</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/icon.png">
<link rel="stylesheet" href="/vendor/mocha.css" /> <link rel="stylesheet" href="/vendor/mocha.css" />
<style> <style>
body { body {

View File

@ -39,7 +39,6 @@ describe('App Module', function() {
.then(url => { .then(url => {
console.log("getAppUrl result:", url); console.log("getAppUrl result:", url);
chai.assert.isNotEmpty(url); chai.assert.isNotEmpty(url);
chai.assert.match(url, /^http:\/\/127\.0\.0\.1/)
}) })
}); });

View File

@ -1,4 +1,5 @@
Edge.debug = true; Edge.debug = true;
EdgeFrame.debug = true;
describe('Edge', function() { describe('Edge', function() {

View File

@ -38,7 +38,7 @@ describe('Remote Procedure Call', function () {
it('should call the add() method repetitively and keep count of the sent values', function () { it('should call the add() method repetitively and keep count of the sent values', function () {
this.timeout(10000); this.timeout(30000);
const values = []; const values = [];
for (let i = 0; i <= 1000; i++) { for (let i = 0; i <= 1000; i++) {

View File

@ -1,39 +0,0 @@
package app
import (
"forge.cadoles.com/arcad/edge/pkg/bundle"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type ID string
type Manifest struct {
ID ID `yaml:"id" json:"id"`
Version string `yaml:"version" json:"version"`
Title string `yaml:"title" json:"title"`
Description string `yaml:"description" json:"description"`
Tags []string `yaml:"tags" json:"tags"`
}
func LoadManifest(b bundle.Bundle) (*Manifest, error) {
reader, _, err := b.File("manifest.yml")
if err != nil {
return nil, errors.Wrap(err, "could not read manifest.yml")
}
defer func() {
if err := reader.Close(); err != nil {
panic(errors.WithStack(err))
}
}()
manifest := &Manifest{}
decoder := yaml.NewDecoder(reader)
if err := decoder.Decode(manifest); err != nil {
return nil, errors.Wrap(err, "could not decode manifest.yml")
}
return manifest, nil
}

85
pkg/app/manifest.go Normal file
View File

@ -0,0 +1,85 @@
package app
import (
"strings"
"forge.cadoles.com/arcad/edge/pkg/bundle"
"github.com/pkg/errors"
"golang.org/x/mod/semver"
"gopkg.in/yaml.v2"
)
type ID string
type Manifest struct {
ID ID `yaml:"id" json:"id"`
Version string `yaml:"version" json:"version"`
Title string `yaml:"title" json:"title"`
Description string `yaml:"description" json:"description"`
Tags []string `yaml:"tags" json:"tags"`
Metadata MapStr `yaml:"metadata" json:"metadata"`
}
type MetadataValidator func(map[string]any) (bool, error)
func (m *Manifest) Validate(validators ...MetadataValidator) (bool, error) {
if m.ID == "" {
return false, errors.New("'id' property should not be empty")
}
if m.Version == "" {
return false, errors.New("'version' property should not be empty")
}
version := m.Version
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
if !semver.IsValid(version) {
return false, errors.Errorf("version '%s' does not respect semver format", m.Version)
}
if m.Title == "" {
return false, errors.New("'title' property should not be empty")
}
if m.Tags != nil {
for _, t := range m.Tags {
if strings.ContainsAny(t, " \t\n\r") {
return false, errors.Errorf("tag '%s' should not contain any space or new line", t)
}
}
}
for _, v := range validators {
valid, err := v(m.Metadata)
if !valid || err != nil {
return valid, errors.WithStack(err)
}
}
return true, nil
}
func LoadManifest(b bundle.Bundle) (*Manifest, error) {
reader, _, err := b.File("manifest.yml")
if err != nil {
return nil, errors.Wrap(err, "could not read manifest.yml")
}
defer func() {
if err := reader.Close(); err != nil {
panic(errors.WithStack(err))
}
}()
manifest := &Manifest{}
decoder := yaml.NewDecoder(reader)
if err := decoder.Decode(manifest); err != nil {
return nil, errors.Wrap(err, "could not decode manifest.yml")
}
return manifest, nil
}

61
pkg/app/map_str.go Normal file
View File

@ -0,0 +1,61 @@
package app
import (
"fmt"
"github.com/pkg/errors"
)
type MapStr map[string]interface{}
func MapStrUnion(dict1 MapStr, dict2 MapStr) MapStr {
dict := MapStr{}
for k, v := range dict1 {
dict[k] = v
}
for k, v := range dict2 {
dict[k] = v
}
return dict
}
func (ms *MapStr) UnmarshalYAML(unmarshal func(interface{}) error) error {
var result map[interface{}]interface{}
err := unmarshal(&result)
if err != nil {
return errors.WithStack(err)
}
*ms = cleanUpInterfaceMap(result)
return nil
}
func cleanUpInterfaceArray(in []interface{}) []interface{} {
result := make([]interface{}, len(in))
for i, v := range in {
result[i] = cleanUpMapValue(v)
}
return result
}
func cleanUpInterfaceMap(in map[interface{}]interface{}) MapStr {
result := make(MapStr)
for k, v := range in {
result[fmt.Sprintf("%v", k)] = cleanUpMapValue(v)
}
return result
}
func cleanUpMapValue(v interface{}) interface{} {
switch v := v.(type) {
case []interface{}:
return cleanUpInterfaceArray(v)
case map[interface{}]interface{}:
return cleanUpInterfaceMap(v)
case string:
return v
default:
return fmt.Sprintf("%v", v)
}
}

View File

@ -0,0 +1,28 @@
package metadata
import (
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/pkg/errors"
)
func WithMinimumRoleValidator(roles ...string) app.MetadataValidator {
return func(metadata map[string]any) (bool, error) {
rawMinimumRole, exists := metadata["minimumRole"]
if !exists {
return true, nil
}
minimumRole, ok := rawMinimumRole.(string)
if !ok {
return false, errors.Errorf("metadata['minimumRole']: unexpected value type '%T'", rawMinimumRole)
}
for _, r := range roles {
if minimumRole == r {
return true, nil
}
}
return false, errors.Errorf("metadata['minimumRole']: unexpected role '%s'", minimumRole)
}
}

View File

@ -0,0 +1,51 @@
package metadata
import (
"strings"
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/pkg/errors"
)
type NamedPath string
const (
NamedPathAdmin NamedPath = "admin"
NamedPathIcon NamedPath = "icon"
)
func WithNamedPathsValidator(names ...NamedPath) app.MetadataValidator {
set := map[NamedPath]struct{}{}
for _, n := range names {
set[n] = struct{}{}
}
return func(metadata map[string]any) (bool, error) {
rawPaths, exists := metadata["paths"]
if !exists {
return true, nil
}
paths, ok := rawPaths.(app.MapStr)
if !ok {
return false, errors.Errorf("metadata['paths']: unexpected named path value type '%T'", rawPaths)
}
for n, p := range paths {
if _, exists := set[NamedPath(n)]; !exists {
return false, errors.Errorf("metadata['paths']: unexpected named path '%s'", n)
}
path, ok := p.(string)
if !ok {
return false, errors.Errorf("metadata['paths']['%s']: unexpected named path value type '%T'", n, path)
}
if !strings.HasPrefix(path, "/") {
return false, errors.Errorf("metadata['paths']['%s']: named path value should start with a '/'", n)
}
}
return true, nil
}
}

View File

@ -0,0 +1,7 @@
id: foo.arcad.app
version: v0.0.0
title: Foo
description: A test app
tags: ["test"]
metadata:
minimumRole: foo

View File

@ -0,0 +1,10 @@
id: foo.arcad.app
version: v0.0.0
title: Foo
description: A test app
tags: ["test"]
metadata:
paths:
invalid: /admin
icon: /my-app-icon.png
minimumRole: visitor

View File

@ -0,0 +1,10 @@
id: foo.arcad.app
version: v0.0.0
title: Foo
description: A test app
tags: ["test"]
metadata:
paths:
admin: /admin
icon: /my-app-icon.png
minimumRole: visitor

View File

@ -0,0 +1,74 @@
package metadata
import (
"io/ioutil"
"path/filepath"
"testing"
"forge.cadoles.com/arcad/edge/pkg/app"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type validatorTestCase struct {
File string
ExpectValid bool
ExpectError bool
}
var validatorTestCases = []validatorTestCase{
{
File: "valid.yml",
ExpectValid: true,
},
{
File: "invalid-paths.yml",
ExpectValid: false,
ExpectError: true,
},
{
File: "invalid-minimum-role.yml",
ExpectValid: false,
ExpectError: true,
},
}
var validators = []app.MetadataValidator{
WithMinimumRoleValidator("visitor", "user", "superuser", "admin", "superadmin"),
WithNamedPathsValidator(NamedPathAdmin, NamedPathIcon),
}
func TestManifestValidate(t *testing.T) {
for _, tc := range validatorTestCases {
func(tc *validatorTestCase) {
t.Run(tc.File, func(t *testing.T) {
data, err := ioutil.ReadFile(filepath.Join("testdata/manifests", tc.File))
if err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
var manifest app.Manifest
if err := yaml.Unmarshal(data, &manifest); err != nil {
t.Fatalf("%+v", errors.WithStack(err))
}
valid, err := manifest.Validate(validators...)
t.Logf("[RESULT] valid:%v, err:%v", valid, err)
if e, g := tc.ExpectValid, valid; e != g {
t.Errorf("valid: expected '%v', got '%v'", e, g)
}
if tc.ExpectError && err == nil {
t.Error("err should not be nil")
}
if !tc.ExpectError && err != nil {
t.Errorf("err: expected nil, got '%+v'", err)
}
})
}(&tc)
}
}

View File

@ -14,11 +14,12 @@ type Module struct {
} }
type gojaManifest struct { type gojaManifest struct {
ID string `goja:"id" json:"id"` ID string `goja:"id" json:"id"`
Version string `goja:"version" json:"version"` Version string `goja:"version" json:"version"`
Title string `goja:"title" json:"title"` Title string `goja:"title" json:"title"`
Description string `goja:"description" json:"description"` Description string `goja:"description" json:"description"`
Tags []string `goja:"tags" json:"tags"` Tags []string `goja:"tags" json:"tags"`
Metadata map[string]any `goja:"metadata" json:"metadata"`
} }
func toGojaManifest(manifest *app.Manifest) *gojaManifest { func toGojaManifest(manifest *app.Manifest) *gojaManifest {
@ -28,6 +29,7 @@ func toGojaManifest(manifest *app.Manifest) *gojaManifest {
Title: manifest.Title, Title: manifest.Title,
Description: manifest.Description, Description: manifest.Description,
Tags: manifest.Tags, Tags: manifest.Tags,
Metadata: manifest.Metadata,
} }
} }

View File

@ -3785,7 +3785,8 @@ var Edge = (() => {
// pkg/sdk/client/src/index.ts // pkg/sdk/client/src/index.ts
var src_exports = {}; var src_exports = {};
__export(src_exports, { __export(src_exports, {
default: () => src_default client: () => client,
crossFrameMessenger: () => crossFrameMessenger
}); });
// pkg/sdk/client/src/event-target.ts // pkg/sdk/client/src/event-target.ts
@ -3864,6 +3865,8 @@ var Edge = (() => {
var import_sockjs_client = __toESM(require_entry()); var import_sockjs_client = __toESM(require_entry());
var EventTypeMessage = "message"; var EventTypeMessage = "message";
var EdgeAuth = "edge-auth"; var EdgeAuth = "edge-auth";
var EdgeAuthTokenRequest = "edge_auth_token_request";
var EdgeAuthTokenResponse = "edge_auth_token_reponse";
var Client = class extends EventTarget { var Client = class extends EventTarget {
constructor(autoReconnect = true) { constructor(autoReconnect = true) {
super(); super();
@ -3871,6 +3874,7 @@ var Edge = (() => {
this._onConnectionClose = this._onConnectionClose.bind(this); this._onConnectionClose = this._onConnectionClose.bind(this);
this._onConnectionMessage = this._onConnectionMessage.bind(this); this._onConnectionMessage = this._onConnectionMessage.bind(this);
this._handleRPCResponse = this._handleRPCResponse.bind(this); this._handleRPCResponse = this._handleRPCResponse.bind(this);
this._handleEdgeAuthTokenRequest = this._handleEdgeAuthTokenRequest.bind(this);
this._rpcID = 0; this._rpcID = 0;
this._pendingRPC = {}; this._pendingRPC = {};
this._queue = []; this._queue = [];
@ -3883,12 +3887,22 @@ var Edge = (() => {
this.send = this.send.bind(this); this.send = this.send.bind(this);
this.upload = this.upload.bind(this); this.upload = this.upload.bind(this);
this.addEventListener(EventTypeMessage, this._handleRPCResponse); this.addEventListener(EventTypeMessage, this._handleRPCResponse);
window.addEventListener("message", this._handleEdgeAuthTokenRequest);
} }
connect(token = "") { connect(token = "") {
let getToken;
if (token) {
getToken = Promise.resolve(token);
} else {
getToken = this._retrieveToken();
}
return getToken.then((token2) => this._connect(token2));
}
disconnect() {
this._cleanupConnection();
}
_connect(token) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (token == "") {
token = this._getAuthCookieToken();
}
const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`; const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`;
this._log("opening connection to", url); this._log("opening connection to", url);
const conn = new import_sockjs_client.default(url); const conn = new import_sockjs_client.default(url);
@ -3919,15 +3933,65 @@ var Edge = (() => {
conn.addEventListener("close", onError); conn.addEventListener("close", onError);
}); });
} }
disconnect() { _retrieveToken() {
this._cleanupConnection(); let token = this._getAuthCookieToken();
if (token) {
return Promise.resolve(token);
}
return this._getParentFrameToken();
;
} }
_getAuthCookieToken() { _getAuthCookieToken() {
const cookie = document.cookie.split("; ").find((row) => row.startsWith(EdgeAuth)); const cookie = document.cookie.split("; ").find((row) => row.startsWith(EdgeAuth));
let token = "";
if (cookie) { if (cookie) {
return cookie.split("=")[1]; token = cookie.split("=")[1];
} }
return ""; return token;
}
_getParentFrameToken(timeout = 5e3) {
if (!window.parent || window.parent === window) {
return Promise.resolve("");
}
return new Promise((resolve, reject) => {
let timedOut = false;
const timeoutId = setTimeout(() => {
timedOut = true;
reject(new Error("Edge auth token request timed out !"));
}, timeout);
const listener = (evt) => {
const message2 = evt.data;
if (!message2 || !message2.type || !message2.data) {
return;
}
if (message2.type !== EdgeAuthTokenResponse) {
return;
}
window.removeEventListener("message", listener);
clearTimeout(timeoutId);
if (timedOut)
return;
if (!message2.data) {
reject("Unexpected auth token request response !");
return;
}
resolve(message2.data?.token || "");
};
window.addEventListener("message", listener);
const message = { type: EdgeAuthTokenRequest };
window.parent.postMessage(message, "*");
});
}
_handleEdgeAuthTokenRequest(evt) {
const message = evt.data;
if (!message || !message.type || message.type !== EdgeAuthTokenRequest) {
return;
}
if (!evt.source) {
return;
}
const token = this._getAuthCookieToken();
evt.source.postMessage({ type: EdgeAuthTokenResponse, data: { token } }, evt.origin);
} }
_onConnectionMessage(evt) { _onConnectionMessage(evt) {
const rawMessage = JSON.parse(evt.data); const rawMessage = JSON.parse(evt.data);
@ -4089,9 +4153,71 @@ var Edge = (() => {
} }
}; };
// pkg/sdk/client/src/crossframe-messenger.ts
var CrossFrameMessenger = class extends EventTarget {
constructor() {
super();
this.debug = false;
this._handleWindowMessage = this._handleWindowMessage.bind(this);
this._initObservers = this._initObservers.bind(this);
window.addEventListener("load", this._initObservers);
window.addEventListener("message", this._handleWindowMessage);
}
post(message, target = window.parent) {
if (!target)
return;
this._log("sending crossframe message", message);
target.postMessage(message, "*");
}
_log(...args) {
if (!this.debug)
return;
console.log(...args);
}
_handleWindowMessage(evt) {
const message = evt.data;
if (!message || !message.type || !message.data) {
return;
}
const event = new CustomEvent(message.type, {
cancelable: true,
detail: message.data
});
this.dispatchEvent(event);
}
_initObservers() {
this._initResizeObserver();
this._initTitleMutationObserver();
}
_initTitleMutationObserver() {
const titleObserver = new MutationObserver((mutations) => {
const title2 = mutations[0].target.textContent;
this.post({ type: "title_changed" /* TITLE_CHANGED */, data: { title: title2 } });
});
const title = document.querySelector("title");
if (!title)
return;
this.post({ type: "title_changed" /* TITLE_CHANGED */, data: { title: title.textContent } });
titleObserver.observe(title, { subtree: true, characterData: true, childList: true });
}
_initResizeObserver() {
const resizeObserver = new ResizeObserver(() => {
const rect = document.documentElement.getBoundingClientRect();
const height = rect.height;
const width = rect.width;
this.post({ type: "size_changed" /* SIZE_CHANGED */, data: { height, width } });
});
const body = document.body;
if (!body)
return;
resizeObserver.observe(document.documentElement);
}
};
// pkg/sdk/client/src/index.ts // pkg/sdk/client/src/index.ts
var src_default = new Client(); var client = new Client();
var crossFrameMessenger = new CrossFrameMessenger();
return __toCommonJS(src_exports); return __toCommonJS(src_exports);
})(); })();
Edge=Edge.default; EdgeFrame=Edge.crossFrameMessenger;Edge=Edge.client
//# sourceMappingURL=client.js.map //# sourceMappingURL=client.js.map

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,8 @@ import SockJS from 'sockjs-client';
const EventTypeMessage = "message"; const EventTypeMessage = "message";
const EdgeAuth = "edge-auth" const EdgeAuth = "edge-auth"
const EdgeAuthTokenRequest = "edge_auth_token_request"
const EdgeAuthTokenResponse = "edge_auth_token_reponse"
export class Client extends EventTarget { export class Client extends EventTarget {
@ -19,80 +21,161 @@ export class Client extends EventTarget {
constructor(autoReconnect = true) { constructor(autoReconnect = true) {
super(); super();
this._conn = null; this._conn = null;
this._onConnectionClose = this._onConnectionClose.bind(this); this._onConnectionClose = this._onConnectionClose.bind(this);
this._onConnectionMessage = this._onConnectionMessage.bind(this); this._onConnectionMessage = this._onConnectionMessage.bind(this);
this._handleRPCResponse = this._handleRPCResponse.bind(this); this._handleRPCResponse = this._handleRPCResponse.bind(this);
this._handleEdgeAuthTokenRequest = this._handleEdgeAuthTokenRequest.bind(this);
this._rpcID = 0; this._rpcID = 0;
this._pendingRPC = {}; this._pendingRPC = {};
this._queue = []; this._queue = [];
this._reconnectionDelay = 250; this._reconnectionDelay = 250;
this._autoReconnect = autoReconnect; this._autoReconnect = autoReconnect;
this.debug = false; this.debug = false;
this.connect = this.connect.bind(this); this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this); this.disconnect = this.disconnect.bind(this);
this.rpc = this.rpc.bind(this); this.rpc = this.rpc.bind(this);
this.send = this.send.bind(this); this.send = this.send.bind(this);
this.upload = this.upload.bind(this); this.upload = this.upload.bind(this);
this.addEventListener(EventTypeMessage, this._handleRPCResponse); this.addEventListener(EventTypeMessage, this._handleRPCResponse);
window.addEventListener('message', this._handleEdgeAuthTokenRequest);
} }
connect(token = "") { connect(token = ""): Promise<Client> {
return new Promise((resolve, reject) => { let getToken: Promise<string>
if (token == "") {
token = this._getAuthCookieToken()
}
const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`; if (token) {
this._log("opening connection to", url); getToken = Promise.resolve(token)
const conn: any = new SockJS(url); } else {
getToken = this._retrieveToken()
}
const onOpen = () => { return getToken.then(token => this._connect(token))
this._log('client connected');
resetHandlers();
conn.onclose = this._onConnectionClose;
conn.onmessage = this._onConnectionMessage;
this._conn = conn;
this._sendQueued();
setTimeout(() => {
this._dispatchConnect();
}, 0);
return resolve(this);
};
const onError = (evt) => {
resetHandlers();
this._scheduleReconnection();
return reject(evt);
};
const resetHandlers = () => {
conn.removeEventListener('open', onOpen);
conn.removeEventListener('close', onError);
conn.removeEventListener('error', onError);
};
conn.addEventListener('open', onOpen);
conn.addEventListener('error', onError);
conn.addEventListener('close', onError);
});
} }
disconnect() { disconnect() {
this._cleanupConnection(); this._cleanupConnection();
} }
_getAuthCookieToken() { _connect(token: string): Promise<Client> {
const cookie = document.cookie.split("; ") return new Promise((resolve, reject) => {
.find((row) => row.startsWith(EdgeAuth)); const url = `//${document.location.host}/edge/sock?${EdgeAuth}=${token}`;
this._log("opening connection to", url);
if (cookie) { const conn: any = new SockJS(url);
return cookie.split("=")[1];
const onOpen = () => {
this._log('client connected');
resetHandlers();
conn.onclose = this._onConnectionClose;
conn.onmessage = this._onConnectionMessage;
this._conn = conn;
this._sendQueued();
setTimeout(() => {
this._dispatchConnect();
}, 0);
return resolve(this);
};
const onError = (evt) => {
resetHandlers();
this._scheduleReconnection();
return reject(evt);
};
const resetHandlers = () => {
conn.removeEventListener('open', onOpen);
conn.removeEventListener('close', onError);
conn.removeEventListener('error', onError);
};
conn.addEventListener('open', onOpen);
conn.addEventListener('error', onError);
conn.addEventListener('close', onError);
})
}
_retrieveToken(): Promise<string> {
let token = this._getAuthCookieToken();
if (token) {
return Promise.resolve(token);
} }
return ""; return this._getParentFrameToken();;
}
_getAuthCookieToken(): string {
const cookie = document.cookie.split("; ")
.find((row) => row.startsWith(EdgeAuth));
let token = "";
if (cookie) {
token = cookie.split("=")[1];
}
return token;
}
_getParentFrameToken(timeout = 5000): Promise<string> {
if (!window.parent || window.parent === window) {
return Promise.resolve("");
}
return new Promise((resolve, reject) => {
let timedOut = false;
const timeoutId = setTimeout(() => {
timedOut = true;
reject(new Error("Edge auth token request timed out !"));
}, timeout);
const listener = (evt) => {
const message = evt.data;
if (!message || !message.type || !message.data) {
return
}
if (message.type !== EdgeAuthTokenResponse) {
return;
}
window.removeEventListener('message', listener);
clearTimeout(timeoutId);
if (timedOut) return;
if (!message.data) {
reject("Unexpected auth token request response !");
return;
}
resolve(message.data?.token || "");
}
window.addEventListener('message', listener);
const message = { type: EdgeAuthTokenRequest };
window.parent.postMessage(message, '*');
})
}
_handleEdgeAuthTokenRequest(evt: MessageEvent) {
const message = evt.data;
if (!message || !message.type || message.type !== EdgeAuthTokenRequest) {
return;
}
if (!evt.source) {
return;
}
const token = this._getAuthCookieToken();
// @ts-ignore
evt.source.postMessage({ type: EdgeAuthTokenResponse, data: { token }}, evt.origin);
} }
_onConnectionMessage(evt) { _onConnectionMessage(evt) {
@ -107,7 +190,7 @@ export class Client extends EventTarget {
_handleRPCResponse(evt) { _handleRPCResponse(evt) {
const { jsonrpc, id, error, result } = evt.detail; const { jsonrpc, id, error, result } = evt.detail;
if (jsonrpc !== '2.0' || id === undefined) return; if (jsonrpc !== '2.0' || id === undefined) return;
if (!evt.detail.hasOwnProperty("error") && !evt.detail.hasOwnProperty("result")) return; if (!evt.detail.hasOwnProperty("error") && !evt.detail.hasOwnProperty("result")) return;
@ -215,20 +298,20 @@ export class Client extends EventTarget {
return this._conn !== null; return this._conn !== null;
} }
upload(blob: string|Blob, metadata: any) { upload(blob: string | Blob, metadata: any) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const formData = new FormData(); const formData = new FormData();
formData.set("file", blob); formData.set("file", blob);
if (metadata) { if (metadata) {
try { try {
formData.set("metadata", JSON.stringify(metadata)); formData.set("metadata", JSON.stringify(metadata));
} catch(err) { } catch (err) {
return reject(err); return reject(err);
} }
} }
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const result = { const result = {
onProgress: null, onProgress: null,
abort: () => xhr.abort(), abort: () => xhr.abort(),
@ -238,7 +321,7 @@ export class Client extends EventTarget {
let data; let data;
try { try {
data = JSON.parse(xhr.responseText); data = JSON.parse(xhr.responseText);
} catch(err) { } catch (err) {
reject(err); reject(err);
return; return;
} }

View File

@ -0,0 +1,87 @@
import { EventTarget } from "./event-target";
enum CrossFrameMessageType {
SIZE_CHANGED = "size_changed",
TITLE_CHANGED = "title_changed"
}
interface CrossFrameMessage {
type: CrossFrameMessageType
data: { [key: string]: any }
}
export class CrossFrameMessenger extends EventTarget {
debug: boolean;
constructor() {
super()
this.debug = false;
this._handleWindowMessage = this._handleWindowMessage.bind(this);
this._initObservers = this._initObservers.bind(this);
window.addEventListener('load', this._initObservers);
window.addEventListener('message', this._handleWindowMessage)
}
post(message: CrossFrameMessage, target: Window = window.parent) {
if (!target) return;
this._log("sending crossframe message", message);
target.postMessage(message, '*');
}
_log(...args) {
if (!this.debug) return;
console.log(...args);
}
_handleWindowMessage(evt: MessageEvent) {
const message = evt.data;
if (!message || !message.type || !message.data) {
return;
}
const event = new CustomEvent(message.type, {
cancelable: true,
detail: message.data
});
this.dispatchEvent(event);
}
_initObservers() {
this._initResizeObserver();
this._initTitleMutationObserver();
}
_initTitleMutationObserver() {
const titleObserver = new MutationObserver((mutations) => {
const title = mutations[0].target.textContent;
this.post({ type: CrossFrameMessageType.TITLE_CHANGED, data: { title }});
});
const title = document.querySelector('title');
if (!title) return;
this.post({ type: CrossFrameMessageType.TITLE_CHANGED, data: { title: title.textContent }});
titleObserver.observe(title, { subtree: true, characterData: true, childList: true });
}
_initResizeObserver() {
const resizeObserver = new ResizeObserver(() => {
const rect = document.documentElement.getBoundingClientRect();
const height = rect.height;
const width = rect.width;
this.post({ type: CrossFrameMessageType.SIZE_CHANGED, data: { height, width }});
});
const body = document.body;
if (!body) return;
resizeObserver.observe(document.documentElement);
}
}

View File

@ -1,3 +1,5 @@
import { Client } from './client.js'; import { Client } from './client.js';
import { CrossFrameMessenger } from './crossframe-messenger.js';
export default new Client(); export const client = new Client();
export const crossFrameMessenger = new CrossFrameMessenger();

View File

@ -25,7 +25,7 @@ func Open(path string) (*sql.DB, error) {
func withTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error { func withTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error {
var tx *sql.Tx var tx *sql.Tx
tx, err := db.Begin() tx, err := db.BeginTx(ctx, nil)
if err != nil { if err != nil {
return errors.WithStack(err) return errors.WithStack(err)
} }